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 | |
| 48 | #include "dlmalloc.h" |
| 49 | #include "logd.h" |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 50 | #include "malloc_debug_check_mapinfo.h" |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 51 | #include "malloc_debug_common.h" |
| 52 | #include "ScopedPthreadMutexLocker.h" |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 53 | |
| 54 | static mapinfo *milist; |
| 55 | |
| 56 | /* libc.debug.malloc.backlog */ |
| 57 | extern unsigned int malloc_double_free_backlog; |
| 58 | |
| 59 | #define MAX_BACKTRACE_DEPTH 15 |
| 60 | #define ALLOCATION_TAG 0x1ee7d00d |
| 61 | #define BACKLOG_TAG 0xbabecafe |
| 62 | #define FREE_POISON 0xa5 |
| 63 | #define BACKLOG_DEFAULT_LEN 100 |
| 64 | #define FRONT_GUARD 0xaa |
| 65 | #define FRONT_GUARD_LEN (1<<5) |
| 66 | #define REAR_GUARD 0xbb |
| 67 | #define REAR_GUARD_LEN (1<<5) |
| 68 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 69 | static void log_message(const char* format, ...) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 70 | extern const MallocDebug __libc_malloc_default_dispatch; |
| 71 | extern const MallocDebug* __libc_malloc_dispatch; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 72 | extern pthread_mutex_t gAllocationsMutex; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 73 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 74 | va_list args; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 75 | { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 76 | ScopedPthreadMutexLocker locker(&gAllocationsMutex); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 77 | const MallocDebug* current_dispatch = __libc_malloc_dispatch; |
| 78 | __libc_malloc_dispatch = &__libc_malloc_default_dispatch; |
| 79 | va_start(args, format); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 80 | __libc_android_log_vprint(ANDROID_LOG_ERROR, "libc", format, args); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 81 | va_end(args); |
| 82 | __libc_malloc_dispatch = current_dispatch; |
| 83 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 86 | struct hdr_t { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 87 | uint32_t tag; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 88 | hdr_t* prev; |
| 89 | hdr_t* next; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 90 | intptr_t bt[MAX_BACKTRACE_DEPTH]; |
| 91 | int bt_depth; |
| 92 | intptr_t freed_bt[MAX_BACKTRACE_DEPTH]; |
| 93 | int freed_bt_depth; |
| 94 | size_t size; |
| 95 | char front_guard[FRONT_GUARD_LEN]; |
| 96 | } __attribute__((packed)); |
| 97 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 98 | struct ftr_t { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 99 | char rear_guard[REAR_GUARD_LEN]; |
| 100 | } __attribute__((packed)); |
| 101 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 102 | static inline ftr_t* to_ftr(hdr_t* hdr) { |
| 103 | return reinterpret_cast<ftr_t*>(reinterpret_cast<char*>(hdr + 1) + hdr->size); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 106 | static inline void* user(hdr_t* hdr) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 107 | return hdr + 1; |
| 108 | } |
| 109 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 110 | static inline hdr_t* meta(void* user) { |
| 111 | return reinterpret_cast<hdr_t*>(user) - 1; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 114 | static unsigned num; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 115 | static hdr_t *tail; |
| 116 | static hdr_t *head; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 117 | static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; |
| 118 | |
| 119 | static unsigned backlog_num; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 120 | static hdr_t *backlog_tail; |
| 121 | static hdr_t *backlog_head; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 122 | static pthread_mutex_t backlog_lock = PTHREAD_MUTEX_INITIALIZER; |
| 123 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 124 | extern __LIBC_HIDDEN__ int get_backtrace(intptr_t* addrs, size_t max_entries); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 125 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 126 | static void print_backtrace(const intptr_t *bt, unsigned int depth) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 127 | const mapinfo *mi; |
| 128 | unsigned int cnt; |
| 129 | unsigned int rel_pc; |
| 130 | intptr_t self_bt[MAX_BACKTRACE_DEPTH]; |
| 131 | |
| 132 | if (!bt) { |
| 133 | depth = get_backtrace(self_bt, MAX_BACKTRACE_DEPTH); |
| 134 | bt = self_bt; |
| 135 | } |
| 136 | |
| 137 | log_message("*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n"); |
| 138 | for (cnt = 0; cnt < depth && cnt < MAX_BACKTRACE_DEPTH; cnt++) { |
| 139 | mi = pc_to_mapinfo(milist, bt[cnt], &rel_pc); |
| 140 | log_message("\t#%02d pc %08x %s\n", cnt, |
| 141 | mi ? (intptr_t)rel_pc : bt[cnt], |
| 142 | mi ? mi->name : "(unknown)"); |
| 143 | } |
| 144 | } |
| 145 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 146 | static inline void init_front_guard(hdr_t *hdr) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 147 | memset(hdr->front_guard, FRONT_GUARD, FRONT_GUARD_LEN); |
| 148 | } |
| 149 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 150 | static inline bool is_front_guard_valid(hdr_t *hdr) { |
| 151 | for (size_t i = 0; i < FRONT_GUARD_LEN; i++) { |
| 152 | if (hdr->front_guard[i] != FRONT_GUARD) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 153 | return 0; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 154 | } |
| 155 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 156 | return 1; |
| 157 | } |
| 158 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 159 | static inline void init_rear_guard(hdr_t *hdr) { |
| 160 | ftr_t* ftr = to_ftr(hdr); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 161 | memset(ftr->rear_guard, REAR_GUARD, REAR_GUARD_LEN); |
| 162 | } |
| 163 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 164 | static inline bool is_rear_guard_valid(hdr_t *hdr) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 165 | unsigned i; |
| 166 | int valid = 1; |
| 167 | int first_mismatch = -1; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 168 | ftr_t* ftr = to_ftr(hdr); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 169 | for (i = 0; i < REAR_GUARD_LEN; i++) { |
| 170 | if (ftr->rear_guard[i] != REAR_GUARD) { |
| 171 | if (first_mismatch < 0) |
| 172 | first_mismatch = i; |
| 173 | valid = 0; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 174 | } else if (first_mismatch >= 0) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 175 | log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i); |
| 176 | first_mismatch = -1; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (first_mismatch >= 0) |
| 181 | log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i); |
| 182 | return valid; |
| 183 | } |
| 184 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 185 | 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] | 186 | hdr->prev = NULL; |
| 187 | hdr->next = *head; |
| 188 | if (*head) |
| 189 | (*head)->prev = hdr; |
| 190 | else |
| 191 | *tail = hdr; |
| 192 | *head = hdr; |
| 193 | } |
| 194 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 195 | static inline int del_locked(hdr_t *hdr, hdr_t **tail, hdr_t **head) { |
| 196 | if (hdr->prev) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 197 | hdr->prev->next = hdr->next; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 198 | } else { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 199 | *head = hdr->next; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 200 | } |
| 201 | if (hdr->next) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 202 | hdr->next->prev = hdr->prev; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 203 | } else { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 204 | *tail = hdr->prev; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 205 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 206 | return 0; |
| 207 | } |
| 208 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 209 | static inline void add(hdr_t *hdr, size_t size) { |
| 210 | ScopedPthreadMutexLocker locker(&lock); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 211 | hdr->tag = ALLOCATION_TAG; |
| 212 | hdr->size = size; |
| 213 | init_front_guard(hdr); |
| 214 | init_rear_guard(hdr); |
| 215 | num++; |
| 216 | add_locked(hdr, &tail, &head); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 219 | static inline int del(hdr_t *hdr) { |
| 220 | if (hdr->tag != ALLOCATION_TAG) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 221 | return -1; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 222 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 223 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 224 | ScopedPthreadMutexLocker locker(&lock); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 225 | del_locked(hdr, &tail, &head); |
| 226 | num--; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 230 | static inline void poison(hdr_t *hdr) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 231 | memset(user(hdr), FREE_POISON, hdr->size); |
| 232 | } |
| 233 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 234 | static int was_used_after_free(hdr_t *hdr) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 235 | unsigned i; |
| 236 | const char *data = (const char *)user(hdr); |
| 237 | for (i = 0; i < hdr->size; i++) |
| 238 | if (data[i] != FREE_POISON) |
| 239 | return 1; |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | /* returns 1 if valid, *safe == 1 if safe to dump stack */ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 244 | static inline int check_guards(hdr_t *hdr, int *safe) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 245 | *safe = 1; |
| 246 | if (!is_front_guard_valid(hdr)) { |
| 247 | if (hdr->front_guard[0] == FRONT_GUARD) { |
| 248 | log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED FRONT GUARD\n", |
| 249 | user(hdr), hdr->size); |
| 250 | } else { |
| 251 | log_message("+++ ALLOCATION %p HAS A CORRUPTED FRONT GUARD "\ |
| 252 | "(NOT DUMPING STACKTRACE)\n", user(hdr)); |
| 253 | /* Allocation header is probably corrupt, do not print stack trace */ |
| 254 | *safe = 0; |
| 255 | } |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | if (!is_rear_guard_valid(hdr)) { |
| 260 | log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED REAR GUARD\n", |
| 261 | user(hdr), hdr->size); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | return 1; |
| 266 | } |
| 267 | |
| 268 | /* returns 1 if valid, *safe == 1 if safe to dump stack */ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 269 | static inline int check_allocation_locked(hdr_t *hdr, int *safe) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 270 | int valid = 1; |
| 271 | *safe = 1; |
| 272 | |
| 273 | if (hdr->tag != ALLOCATION_TAG && hdr->tag != BACKLOG_TAG) { |
| 274 | log_message("+++ ALLOCATION %p HAS INVALID TAG %08x (NOT DUMPING STACKTRACE)\n", |
| 275 | user(hdr), hdr->tag); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 276 | // Allocation header is probably corrupt, do not dequeue or dump stack |
| 277 | // trace. |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 278 | *safe = 0; |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | if (hdr->tag == BACKLOG_TAG && was_used_after_free(hdr)) { |
| 283 | log_message("+++ ALLOCATION %p SIZE %d WAS USED AFTER BEING FREED\n", |
| 284 | user(hdr), hdr->size); |
| 285 | valid = 0; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 286 | /* check the guards to see if it's safe to dump a stack trace */ |
| 287 | check_guards(hdr, safe); |
| 288 | } else { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 289 | valid = check_guards(hdr, safe); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 290 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 291 | |
| 292 | if (!valid && *safe) { |
| 293 | log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n", |
| 294 | user(hdr), hdr->size); |
| 295 | print_backtrace(hdr->bt, hdr->bt_depth); |
| 296 | if (hdr->tag == BACKLOG_TAG) { |
| 297 | log_message("+++ ALLOCATION %p SIZE %d FREED HERE:\n", |
| 298 | user(hdr), hdr->size); |
| 299 | print_backtrace(hdr->freed_bt, hdr->freed_bt_depth); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | return valid; |
| 304 | } |
| 305 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 306 | static inline int del_and_check_locked(hdr_t *hdr, |
| 307 | hdr_t **tail, hdr_t **head, unsigned *cnt, |
| 308 | int *safe) { |
| 309 | int valid = check_allocation_locked(hdr, safe); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 310 | if (safe) { |
| 311 | (*cnt)--; |
| 312 | del_locked(hdr, tail, head); |
| 313 | } |
| 314 | return valid; |
| 315 | } |
| 316 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 317 | static inline void del_from_backlog_locked(hdr_t *hdr) { |
| 318 | int safe; |
| 319 | del_and_check_locked(hdr, |
| 320 | &backlog_tail, &backlog_head, &backlog_num, |
| 321 | &safe); |
| 322 | hdr->tag = 0; /* clear the tag */ |
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 | static inline void del_from_backlog(hdr_t *hdr) { |
| 326 | ScopedPthreadMutexLocker locker(&backlog_lock); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 327 | del_from_backlog_locked(hdr); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 330 | static inline int del_leak(hdr_t *hdr, int *safe) { |
| 331 | ScopedPthreadMutexLocker locker(&lock); |
| 332 | return del_and_check_locked(hdr, &tail, &head, &num, safe); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 335 | static inline void add_to_backlog(hdr_t *hdr) { |
| 336 | ScopedPthreadMutexLocker locker(&backlog_lock); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 337 | hdr->tag = BACKLOG_TAG; |
| 338 | backlog_num++; |
| 339 | add_locked(hdr, &backlog_tail, &backlog_head); |
| 340 | poison(hdr); |
| 341 | /* If we've exceeded the maximum backlog, clear it up */ |
| 342 | while (backlog_num > malloc_double_free_backlog) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 343 | hdr_t *gone = backlog_tail; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 344 | del_from_backlog_locked(gone); |
| 345 | dlfree(gone); |
| 346 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 349 | extern "C" void* chk_malloc(size_t size) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 350 | // log_message("%s: %s\n", __FILE__, __FUNCTION__); |
| 351 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 352 | 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] | 353 | if (hdr) { |
| 354 | hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH); |
| 355 | add(hdr, size); |
| 356 | return user(hdr); |
| 357 | } |
| 358 | return NULL; |
| 359 | } |
| 360 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 361 | extern "C" void* chk_memalign(size_t, size_t bytes) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 362 | // log_message("%s: %s\n", __FILE__, __FUNCTION__); |
| 363 | // XXX: it's better to use malloc, than being wrong |
| 364 | return chk_malloc(bytes); |
| 365 | } |
| 366 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 367 | extern "C" void chk_free(void *ptr) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 368 | // log_message("%s: %s\n", __FILE__, __FUNCTION__); |
| 369 | |
| 370 | if (!ptr) /* ignore free(NULL) */ |
| 371 | return; |
| 372 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 373 | hdr_t* hdr = meta(ptr); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 374 | |
| 375 | if (del(hdr) < 0) { |
| 376 | intptr_t bt[MAX_BACKTRACE_DEPTH]; |
| 377 | int depth; |
| 378 | depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH); |
| 379 | if (hdr->tag == BACKLOG_TAG) { |
| 380 | log_message("+++ ALLOCATION %p SIZE %d BYTES MULTIPLY FREED!\n", |
| 381 | user(hdr), hdr->size); |
| 382 | log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n", |
| 383 | user(hdr), hdr->size); |
| 384 | print_backtrace(hdr->bt, hdr->bt_depth); |
| 385 | /* hdr->freed_bt_depth should be nonzero here */ |
| 386 | log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n", |
| 387 | user(hdr), hdr->size); |
| 388 | print_backtrace(hdr->freed_bt, hdr->freed_bt_depth); |
| 389 | log_message("+++ ALLOCATION %p SIZE %d NOW BEING FREED HERE:\n", |
| 390 | user(hdr), hdr->size); |
| 391 | print_backtrace(bt, depth); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 392 | } else { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 393 | log_message("+++ ALLOCATION %p IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n", |
| 394 | user(hdr)); |
| 395 | print_backtrace(bt, depth); |
| 396 | /* Leak here so that we do not crash */ |
| 397 | //dlfree(user(hdr)); |
| 398 | } |
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 | hdr->freed_bt_depth = get_backtrace(hdr->freed_bt, |
| 401 | MAX_BACKTRACE_DEPTH); |
| 402 | add_to_backlog(hdr); |
| 403 | } |
| 404 | } |
| 405 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 406 | extern "C" void *chk_realloc(void *ptr, size_t size) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 407 | // log_message("%s: %s\n", __FILE__, __FUNCTION__); |
| 408 | |
Elliott Hughes | e7e274b | 2012-10-12 17:05:05 -0700 | [diff] [blame^] | 409 | if (!ptr) { |
| 410 | return chk_malloc(size); |
| 411 | } |
| 412 | |
| 413 | #ifdef REALLOC_ZERO_BYTES_FREE |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 414 | if (!size) { |
| 415 | chk_free(ptr); |
| 416 | return NULL; |
| 417 | } |
Elliott Hughes | e7e274b | 2012-10-12 17:05:05 -0700 | [diff] [blame^] | 418 | #endif |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 419 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 420 | hdr_t* hdr = meta(ptr); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 421 | |
| 422 | if (del(hdr) < 0) { |
| 423 | intptr_t bt[MAX_BACKTRACE_DEPTH]; |
| 424 | int depth; |
| 425 | depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH); |
| 426 | if (hdr->tag == BACKLOG_TAG) { |
| 427 | log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n", |
| 428 | user(hdr), size, hdr->size); |
| 429 | log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n", |
| 430 | user(hdr), hdr->size); |
| 431 | print_backtrace(hdr->bt, hdr->bt_depth); |
| 432 | /* hdr->freed_bt_depth should be nonzero here */ |
| 433 | log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n", |
| 434 | user(hdr), hdr->size); |
| 435 | print_backtrace(hdr->freed_bt, hdr->freed_bt_depth); |
| 436 | log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n", |
| 437 | user(hdr), hdr->size); |
| 438 | print_backtrace(bt, depth); |
| 439 | |
| 440 | /* We take the memory out of the backlog and fall through so the |
| 441 | * reallocation below succeeds. Since we didn't really free it, we |
| 442 | * can default to this behavior. |
| 443 | */ |
| 444 | del_from_backlog(hdr); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 445 | } else { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 446 | log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n", |
| 447 | user(hdr), size); |
| 448 | print_backtrace(bt, depth); |
| 449 | // just get a whole new allocation and leak the old one |
| 450 | return dlrealloc(0, size); |
| 451 | // return dlrealloc(user(hdr), size); // assuming it was allocated externally |
| 452 | } |
| 453 | } |
| 454 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 455 | hdr = static_cast<hdr_t*>(dlrealloc(hdr, sizeof(hdr_t) + size + sizeof(ftr_t))); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 456 | if (hdr) { |
| 457 | hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH); |
| 458 | add(hdr, size); |
| 459 | return user(hdr); |
| 460 | } |
| 461 | |
| 462 | return NULL; |
| 463 | } |
| 464 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 465 | extern "C" void *chk_calloc(int nmemb, size_t size) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 466 | // log_message("%s: %s\n", __FILE__, __FUNCTION__); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 467 | size_t total_size = nmemb * size; |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 468 | 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] | 469 | if (hdr) { |
| 470 | hdr->bt_depth = get_backtrace( |
| 471 | hdr->bt, MAX_BACKTRACE_DEPTH); |
| 472 | add(hdr, total_size); |
| 473 | return user(hdr); |
| 474 | } |
| 475 | return NULL; |
| 476 | } |
| 477 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 478 | static void heaptracker_free_leaked_memory() { |
| 479 | if (num) { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 480 | log_message("+++ THERE ARE %d LEAKED ALLOCATIONS\n", num); |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 481 | } |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 482 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 483 | hdr_t *del = NULL; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 484 | while (head) { |
| 485 | int safe; |
| 486 | del = head; |
| 487 | log_message("+++ DELETING %d BYTES OF LEAKED MEMORY AT %p (%d REMAINING)\n", |
| 488 | del->size, user(del), num); |
| 489 | if (del_leak(del, &safe)) { |
| 490 | /* safe == 1, because the allocation is valid */ |
| 491 | log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n", |
| 492 | user(del), del->size); |
| 493 | print_backtrace(del->bt, del->bt_depth); |
| 494 | } |
| 495 | dlfree(del); |
| 496 | } |
| 497 | |
| 498 | // log_message("+++ DELETING %d BACKLOGGED ALLOCATIONS\n", backlog_num); |
| 499 | while (backlog_head) { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 500 | del = backlog_tail; |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 501 | del_from_backlog(del); |
| 502 | dlfree(del); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /* Initializes malloc debugging framework. |
| 507 | * See comments on MallocDebugInit in malloc_debug_common.h |
| 508 | */ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 509 | extern "C" int malloc_debug_initialize() { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 510 | if (!malloc_double_free_backlog) |
| 511 | malloc_double_free_backlog = BACKLOG_DEFAULT_LEN; |
| 512 | milist = init_mapinfo(getpid()); |
| 513 | return 0; |
| 514 | } |
| 515 | |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 516 | extern "C" void malloc_debug_finalize() { |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 517 | heaptracker_free_leaked_memory(); |
| 518 | deinit_mapinfo(milist); |
| 519 | } |