blob: 5ad3486f3ad4bfdb69764d3c81c669321480929d [file] [log] [blame]
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -07001/*
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 Malcheve1dd3c22012-05-29 14:22:42 -070029#include <arpa/inet.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070030#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 Malcheve1dd3c22012-05-29 14:22:42 -070039#include <stdlib.h>
40#include <string.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070041#include <sys/socket.h>
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070042#include <sys/system_properties.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070043#include <sys/types.h>
44#include <time.h>
45#include <unistd.h>
46#include <unwind.h>
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070047
48#include "dlmalloc.h"
49#include "logd.h"
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070050#include "malloc_debug_check_mapinfo.h"
Elliott Hughes3b297c42012-10-11 16:08:51 -070051#include "malloc_debug_common.h"
52#include "ScopedPthreadMutexLocker.h"
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070053
54static mapinfo *milist;
55
56/* libc.debug.malloc.backlog */
57extern 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 Hughesc4d1fec2012-08-28 14:15:04 -070069static void log_message(const char* format, ...) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070070 extern const MallocDebug __libc_malloc_default_dispatch;
71 extern const MallocDebug* __libc_malloc_dispatch;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070072 extern pthread_mutex_t gAllocationsMutex;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070073
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070074 va_list args;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070075 {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070076 ScopedPthreadMutexLocker locker(&gAllocationsMutex);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070077 const MallocDebug* current_dispatch = __libc_malloc_dispatch;
78 __libc_malloc_dispatch = &__libc_malloc_default_dispatch;
79 va_start(args, format);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070080 __libc_android_log_vprint(ANDROID_LOG_ERROR, "libc", format, args);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070081 va_end(args);
82 __libc_malloc_dispatch = current_dispatch;
83 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070084}
85
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070086struct hdr_t {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070087 uint32_t tag;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070088 hdr_t* prev;
89 hdr_t* next;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070090 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 Hughesc4d1fec2012-08-28 14:15:04 -070098struct ftr_t {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070099 char rear_guard[REAR_GUARD_LEN];
100} __attribute__((packed));
101
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700102static inline ftr_t* to_ftr(hdr_t* hdr) {
103 return reinterpret_cast<ftr_t*>(reinterpret_cast<char*>(hdr + 1) + hdr->size);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700104}
105
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700106static inline void* user(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700107 return hdr + 1;
108}
109
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700110static inline hdr_t* meta(void* user) {
111 return reinterpret_cast<hdr_t*>(user) - 1;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700112}
113
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700114static unsigned num;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700115static hdr_t *tail;
116static hdr_t *head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700117static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
118
119static unsigned backlog_num;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700120static hdr_t *backlog_tail;
121static hdr_t *backlog_head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700122static pthread_mutex_t backlog_lock = PTHREAD_MUTEX_INITIALIZER;
123
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700124extern __LIBC_HIDDEN__ int get_backtrace(intptr_t* addrs, size_t max_entries);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700125
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700126static void print_backtrace(const intptr_t *bt, unsigned int depth) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700127 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 Hughesc4d1fec2012-08-28 14:15:04 -0700146static inline void init_front_guard(hdr_t *hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700147 memset(hdr->front_guard, FRONT_GUARD, FRONT_GUARD_LEN);
148}
149
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700150static 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 Malcheve1dd3c22012-05-29 14:22:42 -0700153 return 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700154 }
155 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700156 return 1;
157}
158
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700159static inline void init_rear_guard(hdr_t *hdr) {
160 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700161 memset(ftr->rear_guard, REAR_GUARD, REAR_GUARD_LEN);
162}
163
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700164static inline bool is_rear_guard_valid(hdr_t *hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700165 unsigned i;
166 int valid = 1;
167 int first_mismatch = -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700168 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700169 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 Hughesc4d1fec2012-08-28 14:15:04 -0700174 } else if (first_mismatch >= 0) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700175 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 Hughesc4d1fec2012-08-28 14:15:04 -0700185static inline void add_locked(hdr_t *hdr, hdr_t **tail, hdr_t **head) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700186 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 Hughesc4d1fec2012-08-28 14:15:04 -0700195static inline int del_locked(hdr_t *hdr, hdr_t **tail, hdr_t **head) {
196 if (hdr->prev) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700197 hdr->prev->next = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700198 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700199 *head = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700200 }
201 if (hdr->next) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700202 hdr->next->prev = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700203 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700204 *tail = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700205 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700206 return 0;
207}
208
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700209static inline void add(hdr_t *hdr, size_t size) {
210 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700211 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 Malcheve1dd3c22012-05-29 14:22:42 -0700217}
218
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700219static inline int del(hdr_t *hdr) {
220 if (hdr->tag != ALLOCATION_TAG) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700221 return -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700222 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700223
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700224 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700225 del_locked(hdr, &tail, &head);
226 num--;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700227 return 0;
228}
229
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700230static inline void poison(hdr_t *hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700231 memset(user(hdr), FREE_POISON, hdr->size);
232}
233
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700234static int was_used_after_free(hdr_t *hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700235 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 Hughesc4d1fec2012-08-28 14:15:04 -0700244static inline int check_guards(hdr_t *hdr, int *safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700245 *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 Hughesc4d1fec2012-08-28 14:15:04 -0700269static inline int check_allocation_locked(hdr_t *hdr, int *safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700270 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 Hughesc4d1fec2012-08-28 14:15:04 -0700276 // Allocation header is probably corrupt, do not dequeue or dump stack
277 // trace.
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700278 *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 Hughesc4d1fec2012-08-28 14:15:04 -0700286 /* check the guards to see if it's safe to dump a stack trace */
287 check_guards(hdr, safe);
288 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700289 valid = check_guards(hdr, safe);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700290 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700291
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 Hughesc4d1fec2012-08-28 14:15:04 -0700306static 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 Malcheve1dd3c22012-05-29 14:22:42 -0700310 if (safe) {
311 (*cnt)--;
312 del_locked(hdr, tail, head);
313 }
314 return valid;
315}
316
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700317static 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 Malcheve1dd3c22012-05-29 14:22:42 -0700323}
324
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700325static inline void del_from_backlog(hdr_t *hdr) {
326 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700327 del_from_backlog_locked(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700328}
329
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700330static 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 Malcheve1dd3c22012-05-29 14:22:42 -0700333}
334
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700335static inline void add_to_backlog(hdr_t *hdr) {
336 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700337 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 Hughesc4d1fec2012-08-28 14:15:04 -0700343 hdr_t *gone = backlog_tail;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700344 del_from_backlog_locked(gone);
345 dlfree(gone);
346 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700347}
348
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700349extern "C" void* chk_malloc(size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700350// log_message("%s: %s\n", __FILE__, __FUNCTION__);
351
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700352 hdr_t* hdr = static_cast<hdr_t*>(dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700353 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 Hughesc4d1fec2012-08-28 14:15:04 -0700361extern "C" void* chk_memalign(size_t, size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700362// 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 Hughesc4d1fec2012-08-28 14:15:04 -0700367extern "C" void chk_free(void *ptr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700368// log_message("%s: %s\n", __FILE__, __FUNCTION__);
369
370 if (!ptr) /* ignore free(NULL) */
371 return;
372
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700373 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700374
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 Hughesc4d1fec2012-08-28 14:15:04 -0700392 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700393 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 Hughesc4d1fec2012-08-28 14:15:04 -0700399 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700400 hdr->freed_bt_depth = get_backtrace(hdr->freed_bt,
401 MAX_BACKTRACE_DEPTH);
402 add_to_backlog(hdr);
403 }
404}
405
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700406extern "C" void *chk_realloc(void *ptr, size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700407// log_message("%s: %s\n", __FILE__, __FUNCTION__);
408
Elliott Hughese7e274b2012-10-12 17:05:05 -0700409 if (!ptr) {
410 return chk_malloc(size);
411 }
412
413#ifdef REALLOC_ZERO_BYTES_FREE
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700414 if (!size) {
415 chk_free(ptr);
416 return NULL;
417 }
Elliott Hughese7e274b2012-10-12 17:05:05 -0700418#endif
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700419
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700420 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700421
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 Hughesc4d1fec2012-08-28 14:15:04 -0700445 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700446 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 Hughesc4d1fec2012-08-28 14:15:04 -0700455 hdr = static_cast<hdr_t*>(dlrealloc(hdr, sizeof(hdr_t) + size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700456 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 Hughesc4d1fec2012-08-28 14:15:04 -0700465extern "C" void *chk_calloc(int nmemb, size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700466// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700467 size_t total_size = nmemb * size;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700468 hdr_t* hdr = static_cast<hdr_t*>(dlcalloc(1, sizeof(hdr_t) + total_size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700469 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 Hughesc4d1fec2012-08-28 14:15:04 -0700478static void heaptracker_free_leaked_memory() {
479 if (num) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700480 log_message("+++ THERE ARE %d LEAKED ALLOCATIONS\n", num);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700481 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700482
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700483 hdr_t *del = NULL;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700484 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 Hughesc4d1fec2012-08-28 14:15:04 -0700500 del = backlog_tail;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700501 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 Hughesc4d1fec2012-08-28 14:15:04 -0700509extern "C" int malloc_debug_initialize() {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700510 if (!malloc_double_free_backlog)
511 malloc_double_free_backlog = BACKLOG_DEFAULT_LEN;
512 milist = init_mapinfo(getpid());
513 return 0;
514}
515
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700516extern "C" void malloc_debug_finalize() {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700517 heaptracker_free_leaked_memory();
518 deinit_mapinfo(milist);
519}