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 | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 51 | #include "private/libc_logging.h" |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 52 | #include "malloc_debug_common.h" |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 53 | #include "private/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; |
Elliott Hughes | ef0696d | 2013-10-08 16:16:01 -0700 | [diff] [blame] | 88 | uint8_t 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 { |
Elliott Hughes | ef0696d | 2013-10-08 16:16:01 -0700 | [diff] [blame] | 92 | uint8_t rear_guard[REAR_GUARD_LEN]; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 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) { |
Elliott Hughes | ef0696d | 2013-10-08 16:16:01 -0700 | [diff] [blame] | 129 | return false; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
Elliott Hughes | ef0696d | 2013-10-08 16:16:01 -0700 | [diff] [blame] | 132 | return true; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | ef0696d | 2013-10-08 16:16:01 -0700 | [diff] [blame] | 210 | static bool was_used_after_free(hdr_t* hdr) { |
| 211 | const uint8_t* data = reinterpret_cast<const uint8_t*>(user(hdr)); |
| 212 | for (size_t i = 0; i < hdr->size; i++) { |
| 213 | if (data[i] != FREE_POISON) { |
| 214 | return true; |
| 215 | } |
| 216 | } |
| 217 | return false; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | /* returns 1 if valid, *safe == 1 if safe to dump stack */ |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 221 | static inline int check_guards(hdr_t* hdr, int* safe) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 222 | *safe = 1; |
| 223 | if (!is_front_guard_valid(hdr)) { |
| 224 | if (hdr->front_guard[0] == FRONT_GUARD) { |
| 225 | log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED FRONT GUARD\n", |
| 226 | user(hdr), hdr->size); |
| 227 | } else { |
| 228 | log_message("+++ ALLOCATION %p HAS A CORRUPTED FRONT GUARD "\ |
| 229 | "(NOT DUMPING STACKTRACE)\n", user(hdr)); |
| 230 | /* Allocation header is probably corrupt, do not print stack trace */ |
| 231 | *safe = 0; |
| 232 | } |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | if (!is_rear_guard_valid(hdr)) { |
| 237 | log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED REAR GUARD\n", |
| 238 | user(hdr), hdr->size); |
| 239 | return 0; |
| 240 | } |
| 241 | |
| 242 | return 1; |
| 243 | } |
| 244 | |
| 245 | /* returns 1 if valid, *safe == 1 if safe to dump stack */ |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 246 | static inline int check_allocation_locked(hdr_t* hdr, int* safe) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 247 | int valid = 1; |
| 248 | *safe = 1; |
| 249 | |
| 250 | if (hdr->tag != ALLOCATION_TAG && hdr->tag != BACKLOG_TAG) { |
| 251 | log_message("+++ ALLOCATION %p HAS INVALID TAG %08x (NOT DUMPING STACKTRACE)\n", |
| 252 | user(hdr), hdr->tag); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 253 | // Allocation header is probably corrupt, do not dequeue or dump stack |
| 254 | // trace. |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 255 | *safe = 0; |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | if (hdr->tag == BACKLOG_TAG && was_used_after_free(hdr)) { |
| 260 | log_message("+++ ALLOCATION %p SIZE %d WAS USED AFTER BEING FREED\n", |
| 261 | user(hdr), hdr->size); |
| 262 | valid = 0; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 263 | /* check the guards to see if it's safe to dump a stack trace */ |
| 264 | check_guards(hdr, safe); |
| 265 | } else { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 266 | valid = check_guards(hdr, safe); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 267 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 268 | |
| 269 | if (!valid && *safe) { |
| 270 | log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n", |
| 271 | user(hdr), hdr->size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 272 | log_backtrace(hdr->bt, hdr->bt_depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 273 | if (hdr->tag == BACKLOG_TAG) { |
| 274 | log_message("+++ ALLOCATION %p SIZE %d FREED HERE:\n", |
| 275 | user(hdr), hdr->size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 276 | log_backtrace(hdr->freed_bt, hdr->freed_bt_depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 277 | } |
| 278 | } |
| 279 | |
| 280 | return valid; |
| 281 | } |
| 282 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 283 | static inline int del_and_check_locked(hdr_t* hdr, |
| 284 | hdr_t** tail, hdr_t** head, unsigned* cnt, |
| 285 | int* safe) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 286 | int valid = check_allocation_locked(hdr, safe); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 287 | if (safe) { |
| 288 | (*cnt)--; |
| 289 | del_locked(hdr, tail, head); |
| 290 | } |
| 291 | return valid; |
| 292 | } |
| 293 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 294 | static inline void del_from_backlog_locked(hdr_t* hdr) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 295 | int safe; |
| 296 | del_and_check_locked(hdr, |
| 297 | &backlog_tail, &backlog_head, &backlog_num, |
| 298 | &safe); |
| 299 | hdr->tag = 0; /* clear the tag */ |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 302 | static inline void del_from_backlog(hdr_t* hdr) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 303 | ScopedPthreadMutexLocker locker(&backlog_lock); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 304 | del_from_backlog_locked(hdr); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 307 | static inline int del_leak(hdr_t* hdr, int* safe) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 308 | ScopedPthreadMutexLocker locker(&lock); |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 309 | return del_and_check_locked(hdr, &tail, &head, &gAllocatedBlockCount, safe); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 312 | static inline void add_to_backlog(hdr_t* hdr) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 313 | ScopedPthreadMutexLocker locker(&backlog_lock); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 314 | hdr->tag = BACKLOG_TAG; |
| 315 | backlog_num++; |
| 316 | add_locked(hdr, &backlog_tail, &backlog_head); |
| 317 | poison(hdr); |
| 318 | /* If we've exceeded the maximum backlog, clear it up */ |
Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 319 | while (backlog_num > gMallocDebugBacklog) { |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 320 | hdr_t* gone = backlog_tail; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 321 | del_from_backlog_locked(gone); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 322 | dlfree(gone->base); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 323 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 326 | extern "C" void* chk_malloc(size_t size) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 327 | // log_message("%s: %s\n", __FILE__, __FUNCTION__); |
| 328 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 329 | 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] | 330 | if (hdr) { |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 331 | hdr->base = hdr; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 332 | hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH); |
| 333 | add(hdr, size); |
| 334 | return user(hdr); |
| 335 | } |
| 336 | return NULL; |
| 337 | } |
| 338 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 339 | extern "C" void* chk_memalign(size_t alignment, size_t bytes) { |
| 340 | if (alignment <= MALLOC_ALIGNMENT) { |
| 341 | return chk_malloc(bytes); |
| 342 | } |
| 343 | |
| 344 | // Make the alignment a power of two. |
| 345 | if (alignment & (alignment-1)) { |
| 346 | alignment = 1L << (31 - __builtin_clz(alignment)); |
| 347 | } |
| 348 | |
| 349 | // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes |
| 350 | // we will align by at least MALLOC_ALIGNMENT bytes |
| 351 | // and at most alignment-MALLOC_ALIGNMENT bytes |
| 352 | size_t size = (alignment-MALLOC_ALIGNMENT) + bytes; |
| 353 | if (size < bytes) { // Overflow. |
| 354 | return NULL; |
| 355 | } |
| 356 | |
| 357 | void* base = dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t)); |
| 358 | if (base != NULL) { |
| 359 | // Check that the actual pointer that will be returned is aligned |
| 360 | // properly. |
| 361 | uintptr_t ptr = reinterpret_cast<uintptr_t>(user(reinterpret_cast<hdr_t*>(base))); |
| 362 | if ((ptr % alignment) != 0) { |
| 363 | // Align the pointer. |
| 364 | ptr += ((-ptr) % alignment); |
| 365 | } |
| 366 | |
| 367 | hdr_t* hdr = meta(reinterpret_cast<void*>(ptr)); |
| 368 | hdr->base = base; |
| 369 | hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH); |
| 370 | add(hdr, bytes); |
| 371 | return user(hdr); |
| 372 | } |
| 373 | return base; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 376 | extern "C" void chk_free(void* ptr) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 377 | // log_message("%s: %s\n", __FILE__, __FUNCTION__); |
| 378 | |
| 379 | if (!ptr) /* ignore free(NULL) */ |
| 380 | return; |
| 381 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 382 | hdr_t* hdr = meta(ptr); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 383 | |
| 384 | if (del(hdr) < 0) { |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 385 | uintptr_t bt[MAX_BACKTRACE_DEPTH]; |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 386 | int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 387 | if (hdr->tag == BACKLOG_TAG) { |
| 388 | log_message("+++ ALLOCATION %p SIZE %d BYTES MULTIPLY FREED!\n", |
| 389 | user(hdr), hdr->size); |
| 390 | log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n", |
| 391 | user(hdr), hdr->size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 392 | log_backtrace(hdr->bt, hdr->bt_depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 393 | /* hdr->freed_bt_depth should be nonzero here */ |
| 394 | log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n", |
| 395 | user(hdr), hdr->size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 396 | log_backtrace(hdr->freed_bt, hdr->freed_bt_depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 397 | log_message("+++ ALLOCATION %p SIZE %d NOW BEING FREED HERE:\n", |
| 398 | user(hdr), hdr->size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 399 | log_backtrace(bt, depth); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 400 | } else { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 401 | log_message("+++ ALLOCATION %p IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n", |
| 402 | user(hdr)); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 403 | log_backtrace(bt, depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 404 | } |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 405 | } else { |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 406 | hdr->freed_bt_depth = get_backtrace(hdr->freed_bt, MAX_BACKTRACE_DEPTH); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 407 | add_to_backlog(hdr); |
| 408 | } |
| 409 | } |
| 410 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 411 | extern "C" void* chk_realloc(void* ptr, size_t size) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 412 | // log_message("%s: %s\n", __FILE__, __FUNCTION__); |
| 413 | |
Elliott Hughes | e7e274b | 2012-10-12 17:05:05 -0700 | [diff] [blame] | 414 | if (!ptr) { |
| 415 | return chk_malloc(size); |
| 416 | } |
| 417 | |
| 418 | #ifdef REALLOC_ZERO_BYTES_FREE |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 419 | if (!size) { |
| 420 | chk_free(ptr); |
| 421 | return NULL; |
| 422 | } |
Elliott Hughes | e7e274b | 2012-10-12 17:05:05 -0700 | [diff] [blame] | 423 | #endif |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 424 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 425 | hdr_t* hdr = meta(ptr); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 426 | |
| 427 | if (del(hdr) < 0) { |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 428 | uintptr_t bt[MAX_BACKTRACE_DEPTH]; |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 429 | int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 430 | if (hdr->tag == BACKLOG_TAG) { |
| 431 | log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n", |
| 432 | user(hdr), size, hdr->size); |
| 433 | log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n", |
| 434 | user(hdr), hdr->size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 435 | log_backtrace(hdr->bt, hdr->bt_depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 436 | /* hdr->freed_bt_depth should be nonzero here */ |
| 437 | log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n", |
| 438 | user(hdr), hdr->size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 439 | log_backtrace(hdr->freed_bt, hdr->freed_bt_depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 440 | log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n", |
| 441 | user(hdr), hdr->size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 442 | log_backtrace(bt, depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 443 | |
| 444 | /* We take the memory out of the backlog and fall through so the |
| 445 | * reallocation below succeeds. Since we didn't really free it, we |
| 446 | * can default to this behavior. |
| 447 | */ |
| 448 | del_from_backlog(hdr); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 449 | } else { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 450 | log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n", |
| 451 | user(hdr), size); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 452 | log_backtrace(bt, depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 453 | // just get a whole new allocation and leak the old one |
| 454 | return dlrealloc(0, size); |
| 455 | // return dlrealloc(user(hdr), size); // assuming it was allocated externally |
| 456 | } |
| 457 | } |
| 458 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 459 | if (hdr->base != hdr) { |
| 460 | // An allocation from memalign, so create another allocation and |
| 461 | // copy the data out. |
| 462 | void* newMem = dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t)); |
| 463 | if (newMem) { |
| 464 | memcpy(newMem, hdr, sizeof(hdr_t) + hdr->size); |
| 465 | dlfree(hdr->base); |
| 466 | hdr = static_cast<hdr_t*>(newMem); |
| 467 | } else { |
| 468 | dlfree(hdr->base); |
| 469 | hdr = NULL; |
| 470 | } |
| 471 | } else { |
| 472 | hdr = static_cast<hdr_t*>(dlrealloc(hdr, sizeof(hdr_t) + size + sizeof(ftr_t))); |
| 473 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 474 | if (hdr) { |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 475 | hdr->base = hdr; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 476 | hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH); |
| 477 | add(hdr, size); |
| 478 | return user(hdr); |
| 479 | } |
| 480 | |
| 481 | return NULL; |
| 482 | } |
| 483 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 484 | extern "C" void* chk_calloc(int nmemb, size_t size) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 485 | // log_message("%s: %s\n", __FILE__, __FUNCTION__); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 486 | size_t total_size = nmemb * size; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 487 | 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] | 488 | if (hdr) { |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 489 | hdr->base = hdr; |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 490 | hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 491 | add(hdr, total_size); |
| 492 | return user(hdr); |
| 493 | } |
| 494 | return NULL; |
| 495 | } |
| 496 | |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 497 | extern "C" size_t chk_malloc_usable_size(const void* ptr) { |
| 498 | // dlmalloc_usable_size returns 0 for NULL and unknown blocks. |
| 499 | if (ptr == NULL) |
| 500 | return 0; |
| 501 | |
| 502 | const hdr_t* hdr = const_meta(ptr); |
| 503 | |
| 504 | // The sentinel tail is written just after the request block bytes |
| 505 | // so there is no extra room we can report here. |
| 506 | return hdr->size; |
| 507 | } |
| 508 | |
Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 509 | static void ReportMemoryLeaks() { |
| 510 | // We only track leaks at level 10. |
| 511 | if (gMallocDebugLevel != 10) { |
| 512 | return; |
| 513 | } |
| 514 | |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 515 | // Use /proc/self/exe link to obtain the program name for logging |
| 516 | // purposes. If it's not available, we set it to "<unknown>". |
| 517 | char exe[PATH_MAX]; |
| 518 | int count; |
| 519 | if ((count = readlink("/proc/self/exe", exe, sizeof(exe) - 1)) == -1) { |
| 520 | strlcpy(exe, "<unknown>", sizeof(exe)); |
| 521 | } else { |
| 522 | exe[count] = '\0'; |
| 523 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 524 | |
Elliott Hughes | 1d12d57 | 2013-01-30 11:38:26 -0800 | [diff] [blame] | 525 | if (gAllocatedBlockCount == 0) { |
| 526 | log_message("+++ %s did not leak", exe); |
| 527 | return; |
| 528 | } |
| 529 | |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 530 | size_t index = 1; |
| 531 | const size_t total = gAllocatedBlockCount; |
| 532 | while (head != NULL) { |
| 533 | int safe; |
| 534 | hdr_t* block = head; |
| 535 | log_message("+++ %s leaked block of size %d at %p (leak %d of %d)", |
| 536 | exe, block->size, user(block), index++, total); |
| 537 | if (del_leak(block, &safe)) { |
| 538 | /* safe == 1, because the allocation is valid */ |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 539 | log_backtrace(block->bt, block->bt_depth); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 540 | } |
Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | while (backlog_head != NULL) { |
| 544 | del_from_backlog(backlog_tail); |
| 545 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 548 | extern "C" int malloc_debug_initialize() { |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 549 | backtrace_startup(); |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 550 | return 0; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 551 | } |
| 552 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 553 | extern "C" void malloc_debug_finalize() { |
Elliott Hughes | 9c81892 | 2013-02-01 17:07:40 -0800 | [diff] [blame] | 554 | ReportMemoryLeaks(); |
Elliott Hughes | 35b621c | 2013-01-28 16:27:36 -0800 | [diff] [blame] | 555 | backtrace_shutdown(); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 556 | } |