Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 29 | #include <arpa/inet.h> |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 30 | #include <dlfcn.h> |
| 31 | #include <errno.h> |
| 32 | #include <errno.h> |
| 33 | #include <fcntl.h> |
| 34 | #include <pthread.h> |
| 35 | #include <stdarg.h> |
| 36 | #include <stdbool.h> |
| 37 | #include <stddef.h> |
| 38 | #include <stdio.h> |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 41 | #include <sys/socket.h> |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 42 | #include <sys/system_properties.h> |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 43 | #include <sys/types.h> |
| 44 | #include <time.h> |
| 45 | #include <unistd.h> |
| 46 | #include <unwind.h> |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 47 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 48 | #include "debug_mapinfo.h" |
| 49 | #include "debug_stacktrace.h" |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 50 | #include "dlmalloc.h" |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 51 | #include "libc_logging.h" |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 52 | #include "malloc_debug_common.h" |
| 53 | #include "ScopedPthreadMutexLocker.h" |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 54 | |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 55 | /* libc.debug.malloc.backlog */ |
Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 56 | extern unsigned int gMallocDebugBacklog; |
| 57 | extern int gMallocDebugLevel; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 58 | |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 59 | #define MAX_BACKTRACE_DEPTH 16 |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 60 | #define ALLOCATION_TAG 0x1ee7d00d |
| 61 | #define BACKLOG_TAG 0xbabecafe |
| 62 | #define FREE_POISON 0xa5 |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 63 | #define FRONT_GUARD 0xaa |
| 64 | #define FRONT_GUARD_LEN (1<<5) |
| 65 | #define REAR_GUARD 0xbb |
| 66 | #define REAR_GUARD_LEN (1<<5) |
| 67 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 68 | static void log_message(const char* format, ...) { |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 69 | va_list args; |
| 70 | va_start(args, format); |
| 71 | __libc_format_log_va_list(ANDROID_LOG_ERROR, "libc", format, args); |
| 72 | va_end(args); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 75 | struct hdr_t { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 76 | uint32_t tag; |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 77 | void* base; // Always points to the memory allocated using dlmalloc. |
| 78 | // For memory allocated in chk_memalign, this value will |
| 79 | // not be the same as the location of the start of this |
| 80 | // structure. |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 81 | hdr_t* prev; |
| 82 | hdr_t* next; |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 83 | uintptr_t bt[MAX_BACKTRACE_DEPTH]; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 84 | int bt_depth; |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 85 | uintptr_t freed_bt[MAX_BACKTRACE_DEPTH]; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 86 | int freed_bt_depth; |
| 87 | size_t size; |
| 88 | char front_guard[FRONT_GUARD_LEN]; |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 89 | } __attribute__((packed, aligned(MALLOC_ALIGNMENT))); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 90 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 91 | struct ftr_t { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 92 | char rear_guard[REAR_GUARD_LEN]; |
| 93 | } __attribute__((packed)); |
| 94 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 95 | static inline ftr_t* to_ftr(hdr_t* hdr) { |
| 96 | return reinterpret_cast<ftr_t*>(reinterpret_cast<char*>(hdr + 1) + hdr->size); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 99 | static inline void* user(hdr_t* hdr) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 100 | return hdr + 1; |
| 101 | } |
| 102 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 103 | static inline hdr_t* meta(void* user) { |
| 104 | return reinterpret_cast<hdr_t*>(user) - 1; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 107 | static inline const hdr_t* const_meta(const void* user) { |
| 108 | return reinterpret_cast<const hdr_t*>(user) - 1; |
| 109 | } |
| 110 | |
| 111 | |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 112 | static unsigned gAllocatedBlockCount; |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 113 | static hdr_t* tail; |
| 114 | static hdr_t* head; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 115 | static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; |
| 116 | |
| 117 | static unsigned backlog_num; |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 118 | static hdr_t* backlog_tail; |
| 119 | static hdr_t* backlog_head; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 120 | static pthread_mutex_t backlog_lock = PTHREAD_MUTEX_INITIALIZER; |
| 121 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 122 | static inline void init_front_guard(hdr_t* hdr) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 123 | memset(hdr->front_guard, FRONT_GUARD, FRONT_GUARD_LEN); |
| 124 | } |
| 125 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 126 | static inline bool is_front_guard_valid(hdr_t* hdr) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 127 | for (size_t i = 0; i < FRONT_GUARD_LEN; i++) { |
| 128 | if (hdr->front_guard[i] != FRONT_GUARD) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 129 | return 0; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 132 | return 1; |
| 133 | } |
| 134 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 135 | static inline void init_rear_guard(hdr_t* hdr) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 136 | ftr_t* ftr = to_ftr(hdr); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 137 | memset(ftr->rear_guard, REAR_GUARD, REAR_GUARD_LEN); |
| 138 | } |
| 139 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 140 | static inline bool is_rear_guard_valid(hdr_t* hdr) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 141 | unsigned i; |
| 142 | int valid = 1; |
| 143 | int first_mismatch = -1; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 144 | ftr_t* ftr = to_ftr(hdr); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 145 | for (i = 0; i < REAR_GUARD_LEN; i++) { |
| 146 | if (ftr->rear_guard[i] != REAR_GUARD) { |
| 147 | if (first_mismatch < 0) |
| 148 | first_mismatch = i; |
| 149 | valid = 0; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 150 | } else if (first_mismatch >= 0) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 151 | log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i); |
| 152 | first_mismatch = -1; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | if (first_mismatch >= 0) |
| 157 | log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i); |
| 158 | return valid; |
| 159 | } |
| 160 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 161 | static inline void add_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 162 | hdr->prev = NULL; |
| 163 | hdr->next = *head; |
| 164 | if (*head) |
| 165 | (*head)->prev = hdr; |
| 166 | else |
| 167 | *tail = hdr; |
| 168 | *head = hdr; |
| 169 | } |
| 170 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 171 | static inline int del_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 172 | if (hdr->prev) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 173 | hdr->prev->next = hdr->next; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 174 | } else { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 175 | *head = hdr->next; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 176 | } |
| 177 | if (hdr->next) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 178 | hdr->next->prev = hdr->prev; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 179 | } else { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 180 | *tail = hdr->prev; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 181 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 182 | return 0; |
| 183 | } |
| 184 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 185 | static inline void add(hdr_t* hdr, size_t size) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 186 | ScopedPthreadMutexLocker locker(&lock); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 187 | hdr->tag = ALLOCATION_TAG; |
| 188 | hdr->size = size; |
| 189 | init_front_guard(hdr); |
| 190 | init_rear_guard(hdr); |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 191 | ++gAllocatedBlockCount; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 192 | add_locked(hdr, &tail, &head); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 195 | static inline int del(hdr_t* hdr) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 196 | if (hdr->tag != ALLOCATION_TAG) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 197 | return -1; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 198 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 199 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 200 | ScopedPthreadMutexLocker locker(&lock); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 201 | del_locked(hdr, &tail, &head); |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 202 | --gAllocatedBlockCount; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 203 | return 0; |
| 204 | } |
| 205 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 206 | static inline void poison(hdr_t* hdr) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 207 | memset(user(hdr), FREE_POISON, hdr->size); |
| 208 | } |
| 209 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 210 | static int was_used_after_free(hdr_t* hdr) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 211 | unsigned i; |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 212 | const char* data = reinterpret_cast<const char *>(user(hdr)); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 213 | for (i = 0; i < hdr->size; i++) |
| 214 | if (data[i] != FREE_POISON) |
| 215 | return 1; |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | /* returns 1 if valid, *safe == 1 if safe to dump stack */ |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 220 | static inline int check_guards(hdr_t* hdr, int* safe) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 221 | *safe = 1; |
| 222 | if (!is_front_guard_valid(hdr)) { |
| 223 | if (hdr->front_guard[0] == FRONT_GUARD) { |
| 224 | log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED FRONT GUARD\n", |
| 225 | user(hdr), hdr->size); |
| 226 | } else { |
| 227 | log_message("+++ ALLOCATION %p HAS A CORRUPTED FRONT GUARD "\ |
| 228 | "(NOT DUMPING STACKTRACE)\n", user(hdr)); |
| 229 | /* Allocation header is probably corrupt, do not print stack trace */ |
| 230 | *safe = 0; |
| 231 | } |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | if (!is_rear_guard_valid(hdr)) { |
| 236 | log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED REAR GUARD\n", |
| 237 | user(hdr), hdr->size); |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | return 1; |
| 242 | } |
| 243 | |
| 244 | /* returns 1 if valid, *safe == 1 if safe to dump stack */ |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 245 | static inline int check_allocation_locked(hdr_t* hdr, int* safe) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 246 | int valid = 1; |
| 247 | *safe = 1; |
| 248 | |
| 249 | if (hdr->tag != ALLOCATION_TAG && hdr->tag != BACKLOG_TAG) { |
| 250 | log_message("+++ ALLOCATION %p HAS INVALID TAG %08x (NOT DUMPING STACKTRACE)\n", |
| 251 | user(hdr), hdr->tag); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 252 | // Allocation header is probably corrupt, do not dequeue or dump stack |
| 253 | // trace. |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 254 | *safe = 0; |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | if (hdr->tag == BACKLOG_TAG && was_used_after_free(hdr)) { |
| 259 | log_message("+++ ALLOCATION %p SIZE %d WAS USED AFTER BEING FREED\n", |
| 260 | user(hdr), hdr->size); |
| 261 | valid = 0; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 262 | /* check the guards to see if it's safe to dump a stack trace */ |
| 263 | check_guards(hdr, safe); |
| 264 | } else { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 265 | valid = check_guards(hdr, safe); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 266 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 267 | |
| 268 | if (!valid && *safe) { |
| 269 | log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n", |
| 270 | user(hdr), hdr->size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 271 | log_backtrace(hdr->bt, hdr->bt_depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 272 | if (hdr->tag == BACKLOG_TAG) { |
| 273 | log_message("+++ ALLOCATION %p SIZE %d FREED HERE:\n", |
| 274 | user(hdr), hdr->size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 275 | log_backtrace(hdr->freed_bt, hdr->freed_bt_depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
| 279 | return valid; |
| 280 | } |
| 281 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 282 | static inline int del_and_check_locked(hdr_t* hdr, |
| 283 | hdr_t** tail, hdr_t** head, unsigned* cnt, |
| 284 | int* safe) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 285 | int valid = check_allocation_locked(hdr, safe); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 286 | if (safe) { |
| 287 | (*cnt)--; |
| 288 | del_locked(hdr, tail, head); |
| 289 | } |
| 290 | return valid; |
| 291 | } |
| 292 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 293 | static inline void del_from_backlog_locked(hdr_t* hdr) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 294 | int safe; |
| 295 | del_and_check_locked(hdr, |
| 296 | &backlog_tail, &backlog_head, &backlog_num, |
| 297 | &safe); |
| 298 | hdr->tag = 0; /* clear the tag */ |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 301 | static inline void del_from_backlog(hdr_t* hdr) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 302 | ScopedPthreadMutexLocker locker(&backlog_lock); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 303 | del_from_backlog_locked(hdr); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 304 | } |
| 305 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 306 | static inline int del_leak(hdr_t* hdr, int* safe) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 307 | ScopedPthreadMutexLocker locker(&lock); |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 308 | return del_and_check_locked(hdr, &tail, &head, &gAllocatedBlockCount, safe); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 311 | static inline void add_to_backlog(hdr_t* hdr) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 312 | ScopedPthreadMutexLocker locker(&backlog_lock); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 313 | hdr->tag = BACKLOG_TAG; |
| 314 | backlog_num++; |
| 315 | add_locked(hdr, &backlog_tail, &backlog_head); |
| 316 | poison(hdr); |
| 317 | /* If we've exceeded the maximum backlog, clear it up */ |
Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 318 | while (backlog_num > gMallocDebugBacklog) { |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 319 | hdr_t* gone = backlog_tail; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 320 | del_from_backlog_locked(gone); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 321 | dlfree(gone->base); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 322 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 325 | extern "C" void* chk_malloc(size_t size) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 326 | // log_message("%s: %s\n", __FILE__, __FUNCTION__); |
| 327 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 328 | hdr_t* hdr = static_cast<hdr_t*>(dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t))); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 329 | if (hdr) { |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 330 | hdr->base = hdr; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 331 | hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH); |
| 332 | add(hdr, size); |
| 333 | return user(hdr); |
| 334 | } |
| 335 | return NULL; |
| 336 | } |
| 337 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 338 | extern "C" void* chk_memalign(size_t alignment, size_t bytes) { |
| 339 | if (alignment <= MALLOC_ALIGNMENT) { |
| 340 | return chk_malloc(bytes); |
| 341 | } |
| 342 | |
| 343 | // Make the alignment a power of two. |
| 344 | if (alignment & (alignment-1)) { |
| 345 | alignment = 1L << (31 - __builtin_clz(alignment)); |
| 346 | } |
| 347 | |
| 348 | // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes |
| 349 | // we will align by at least MALLOC_ALIGNMENT bytes |
| 350 | // and at most alignment-MALLOC_ALIGNMENT bytes |
| 351 | size_t size = (alignment-MALLOC_ALIGNMENT) + bytes; |
| 352 | if (size < bytes) { // Overflow. |
| 353 | return NULL; |
| 354 | } |
| 355 | |
| 356 | void* base = dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t)); |
| 357 | if (base != NULL) { |
| 358 | // Check that the actual pointer that will be returned is aligned |
| 359 | // properly. |
| 360 | uintptr_t ptr = reinterpret_cast<uintptr_t>(user(reinterpret_cast<hdr_t*>(base))); |
| 361 | if ((ptr % alignment) != 0) { |
| 362 | // Align the pointer. |
| 363 | ptr += ((-ptr) % alignment); |
| 364 | } |
| 365 | |
| 366 | hdr_t* hdr = meta(reinterpret_cast<void*>(ptr)); |
| 367 | hdr->base = base; |
| 368 | hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH); |
| 369 | add(hdr, bytes); |
| 370 | return user(hdr); |
| 371 | } |
| 372 | return base; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 375 | extern "C" void chk_free(void* ptr) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 376 | // log_message("%s: %s\n", __FILE__, __FUNCTION__); |
| 377 | |
| 378 | if (!ptr) /* ignore free(NULL) */ |
| 379 | return; |
| 380 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 381 | hdr_t* hdr = meta(ptr); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 382 | |
| 383 | if (del(hdr) < 0) { |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 384 | uintptr_t bt[MAX_BACKTRACE_DEPTH]; |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 385 | int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 386 | if (hdr->tag == BACKLOG_TAG) { |
| 387 | log_message("+++ ALLOCATION %p SIZE %d BYTES MULTIPLY FREED!\n", |
| 388 | user(hdr), hdr->size); |
| 389 | log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n", |
| 390 | user(hdr), hdr->size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 391 | log_backtrace(hdr->bt, hdr->bt_depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 392 | /* hdr->freed_bt_depth should be nonzero here */ |
| 393 | log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n", |
| 394 | user(hdr), hdr->size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 395 | log_backtrace(hdr->freed_bt, hdr->freed_bt_depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 396 | log_message("+++ ALLOCATION %p SIZE %d NOW BEING FREED HERE:\n", |
| 397 | user(hdr), hdr->size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 398 | log_backtrace(bt, depth); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 399 | } else { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 400 | log_message("+++ ALLOCATION %p IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n", |
| 401 | user(hdr)); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 402 | log_backtrace(bt, depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 403 | } |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 404 | } else { |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 405 | hdr->freed_bt_depth = get_backtrace(hdr->freed_bt, MAX_BACKTRACE_DEPTH); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 406 | add_to_backlog(hdr); |
| 407 | } |
| 408 | } |
| 409 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 410 | extern "C" void* chk_realloc(void* ptr, size_t size) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 411 | // log_message("%s: %s\n", __FILE__, __FUNCTION__); |
| 412 | |
Elliott Hughes | e7e274b | 2012-10-12 17:05:05 -0700 | [diff] [blame] | 413 | if (!ptr) { |
| 414 | return chk_malloc(size); |
| 415 | } |
| 416 | |
| 417 | #ifdef REALLOC_ZERO_BYTES_FREE |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 418 | if (!size) { |
| 419 | chk_free(ptr); |
| 420 | return NULL; |
| 421 | } |
Elliott Hughes | e7e274b | 2012-10-12 17:05:05 -0700 | [diff] [blame] | 422 | #endif |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 423 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 424 | hdr_t* hdr = meta(ptr); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 425 | |
| 426 | if (del(hdr) < 0) { |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 427 | uintptr_t bt[MAX_BACKTRACE_DEPTH]; |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 428 | int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 429 | if (hdr->tag == BACKLOG_TAG) { |
| 430 | log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n", |
| 431 | user(hdr), size, hdr->size); |
| 432 | log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n", |
| 433 | user(hdr), hdr->size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 434 | log_backtrace(hdr->bt, hdr->bt_depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 435 | /* hdr->freed_bt_depth should be nonzero here */ |
| 436 | log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n", |
| 437 | user(hdr), hdr->size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 438 | log_backtrace(hdr->freed_bt, hdr->freed_bt_depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 439 | log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n", |
| 440 | user(hdr), hdr->size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 441 | log_backtrace(bt, depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 442 | |
| 443 | /* We take the memory out of the backlog and fall through so the |
| 444 | * reallocation below succeeds. Since we didn't really free it, we |
| 445 | * can default to this behavior. |
| 446 | */ |
| 447 | del_from_backlog(hdr); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 448 | } else { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 449 | log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n", |
| 450 | user(hdr), size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 451 | log_backtrace(bt, depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 452 | // just get a whole new allocation and leak the old one |
| 453 | return dlrealloc(0, size); |
| 454 | // return dlrealloc(user(hdr), size); // assuming it was allocated externally |
| 455 | } |
| 456 | } |
| 457 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 458 | if (hdr->base != hdr) { |
| 459 | // An allocation from memalign, so create another allocation and |
| 460 | // copy the data out. |
| 461 | void* newMem = dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t)); |
| 462 | if (newMem) { |
| 463 | memcpy(newMem, hdr, sizeof(hdr_t) + hdr->size); |
| 464 | dlfree(hdr->base); |
| 465 | hdr = static_cast<hdr_t*>(newMem); |
| 466 | } else { |
| 467 | dlfree(hdr->base); |
| 468 | hdr = NULL; |
| 469 | } |
| 470 | } else { |
| 471 | hdr = static_cast<hdr_t*>(dlrealloc(hdr, sizeof(hdr_t) + size + sizeof(ftr_t))); |
| 472 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 473 | if (hdr) { |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 474 | hdr->base = hdr; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 475 | hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH); |
| 476 | add(hdr, size); |
| 477 | return user(hdr); |
| 478 | } |
| 479 | |
| 480 | return NULL; |
| 481 | } |
| 482 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 483 | extern "C" void* chk_calloc(int nmemb, size_t size) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 484 | // log_message("%s: %s\n", __FILE__, __FUNCTION__); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 485 | size_t total_size = nmemb * size; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 486 | hdr_t* hdr = static_cast<hdr_t*>(dlcalloc(1, sizeof(hdr_t) + total_size + sizeof(ftr_t))); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 487 | if (hdr) { |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 488 | hdr->base = hdr; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 489 | hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 490 | add(hdr, total_size); |
| 491 | return user(hdr); |
| 492 | } |
| 493 | return NULL; |
| 494 | } |
| 495 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame^] | 496 | extern "C" size_t chk_malloc_usable_size(const void* ptr) { |
| 497 | // dlmalloc_usable_size returns 0 for NULL and unknown blocks. |
| 498 | if (ptr == NULL) |
| 499 | return 0; |
| 500 | |
| 501 | const hdr_t* hdr = const_meta(ptr); |
| 502 | |
| 503 | // The sentinel tail is written just after the request block bytes |
| 504 | // so there is no extra room we can report here. |
| 505 | return hdr->size; |
| 506 | } |
| 507 | |
Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 508 | static void ReportMemoryLeaks() { |
| 509 | // We only track leaks at level 10. |
| 510 | if (gMallocDebugLevel != 10) { |
| 511 | return; |
| 512 | } |
| 513 | |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 514 | // Use /proc/self/exe link to obtain the program name for logging |
| 515 | // purposes. If it's not available, we set it to "<unknown>". |
| 516 | char exe[PATH_MAX]; |
| 517 | int count; |
| 518 | if ((count = readlink("/proc/self/exe", exe, sizeof(exe) - 1)) == -1) { |
| 519 | strlcpy(exe, "<unknown>", sizeof(exe)); |
| 520 | } else { |
| 521 | exe[count] = '\0'; |
| 522 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 523 | |
Elliott Hughes | 1d12d57 | 2013-01-30 11:38:26 -0800 | [diff] [blame] | 524 | if (gAllocatedBlockCount == 0) { |
| 525 | log_message("+++ %s did not leak", exe); |
| 526 | return; |
| 527 | } |
| 528 | |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 529 | size_t index = 1; |
| 530 | const size_t total = gAllocatedBlockCount; |
| 531 | while (head != NULL) { |
| 532 | int safe; |
| 533 | hdr_t* block = head; |
| 534 | log_message("+++ %s leaked block of size %d at %p (leak %d of %d)", |
| 535 | exe, block->size, user(block), index++, total); |
| 536 | if (del_leak(block, &safe)) { |
| 537 | /* safe == 1, because the allocation is valid */ |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 538 | log_backtrace(block->bt, block->bt_depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 539 | } |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | while (backlog_head != NULL) { |
| 543 | del_from_backlog(backlog_tail); |
| 544 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 547 | extern "C" int malloc_debug_initialize() { |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 548 | backtrace_startup(); |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 549 | return 0; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 552 | extern "C" void malloc_debug_finalize() { |
Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 553 | ReportMemoryLeaks(); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 554 | backtrace_shutdown(); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 555 | } |