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