blob: d366b56e876f46faae7d3eb10feb7b0d26f9c721 [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
Elliott Hughes1e980b62013-01-17 18:36:06 -080048#include "debug_mapinfo.h"
49#include "debug_stacktrace.h"
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070050#include "dlmalloc.h"
51#include "logd.h"
Elliott Hughes3b297c42012-10-11 16:08:51 -070052#include "malloc_debug_common.h"
53#include "ScopedPthreadMutexLocker.h"
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070054
Elliott Hughes1e980b62013-01-17 18:36:06 -080055static mapinfo_t* gMapInfo;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070056
57/* libc.debug.malloc.backlog */
58extern unsigned int malloc_double_free_backlog;
59
Elliott Hughes1e980b62013-01-17 18:36:06 -080060#define MAX_BACKTRACE_DEPTH 16
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070061#define ALLOCATION_TAG 0x1ee7d00d
62#define BACKLOG_TAG 0xbabecafe
63#define FREE_POISON 0xa5
64#define BACKLOG_DEFAULT_LEN 100
65#define FRONT_GUARD 0xaa
66#define FRONT_GUARD_LEN (1<<5)
67#define REAR_GUARD 0xbb
68#define REAR_GUARD_LEN (1<<5)
69
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070070static void log_message(const char* format, ...) {
Elliott Hughes1e980b62013-01-17 18:36:06 -080071 va_list args;
72 va_start(args, format);
73 __libc_format_log_va_list(ANDROID_LOG_ERROR, "libc", format, args);
74 va_end(args);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070075}
76
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070077struct hdr_t {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070078 uint32_t tag;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070079 hdr_t* prev;
80 hdr_t* next;
Elliott Hughes239e7a02013-01-25 17:13:45 -080081 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070082 int bt_depth;
Elliott Hughes239e7a02013-01-25 17:13:45 -080083 uintptr_t freed_bt[MAX_BACKTRACE_DEPTH];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070084 int freed_bt_depth;
85 size_t size;
86 char front_guard[FRONT_GUARD_LEN];
87} __attribute__((packed));
88
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070089struct ftr_t {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070090 char rear_guard[REAR_GUARD_LEN];
91} __attribute__((packed));
92
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070093static inline ftr_t* to_ftr(hdr_t* hdr) {
94 return reinterpret_cast<ftr_t*>(reinterpret_cast<char*>(hdr + 1) + hdr->size);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070095}
96
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070097static inline void* user(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070098 return hdr + 1;
99}
100
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700101static inline hdr_t* meta(void* user) {
102 return reinterpret_cast<hdr_t*>(user) - 1;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700103}
104
Elliott Hughes239e7a02013-01-25 17:13:45 -0800105static unsigned gAllocatedBlockCount;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700106static hdr_t *tail;
107static hdr_t *head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700108static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
109
110static unsigned backlog_num;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700111static hdr_t *backlog_tail;
112static hdr_t *backlog_head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700113static pthread_mutex_t backlog_lock = PTHREAD_MUTEX_INITIALIZER;
114
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700115static inline void init_front_guard(hdr_t *hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700116 memset(hdr->front_guard, FRONT_GUARD, FRONT_GUARD_LEN);
117}
118
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700119static inline bool is_front_guard_valid(hdr_t *hdr) {
120 for (size_t i = 0; i < FRONT_GUARD_LEN; i++) {
121 if (hdr->front_guard[i] != FRONT_GUARD) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700122 return 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700123 }
124 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700125 return 1;
126}
127
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700128static inline void init_rear_guard(hdr_t *hdr) {
129 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700130 memset(ftr->rear_guard, REAR_GUARD, REAR_GUARD_LEN);
131}
132
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700133static inline bool is_rear_guard_valid(hdr_t *hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700134 unsigned i;
135 int valid = 1;
136 int first_mismatch = -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700137 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700138 for (i = 0; i < REAR_GUARD_LEN; i++) {
139 if (ftr->rear_guard[i] != REAR_GUARD) {
140 if (first_mismatch < 0)
141 first_mismatch = i;
142 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700143 } else if (first_mismatch >= 0) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700144 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
145 first_mismatch = -1;
146 }
147 }
148
149 if (first_mismatch >= 0)
150 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
151 return valid;
152}
153
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700154static inline void add_locked(hdr_t *hdr, hdr_t **tail, hdr_t **head) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700155 hdr->prev = NULL;
156 hdr->next = *head;
157 if (*head)
158 (*head)->prev = hdr;
159 else
160 *tail = hdr;
161 *head = hdr;
162}
163
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700164static inline int del_locked(hdr_t *hdr, hdr_t **tail, hdr_t **head) {
165 if (hdr->prev) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700166 hdr->prev->next = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700167 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700168 *head = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700169 }
170 if (hdr->next) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700171 hdr->next->prev = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700172 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700173 *tail = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700174 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700175 return 0;
176}
177
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700178static inline void add(hdr_t *hdr, size_t size) {
179 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700180 hdr->tag = ALLOCATION_TAG;
181 hdr->size = size;
182 init_front_guard(hdr);
183 init_rear_guard(hdr);
Elliott Hughes239e7a02013-01-25 17:13:45 -0800184 ++gAllocatedBlockCount;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700185 add_locked(hdr, &tail, &head);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700186}
187
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700188static inline int del(hdr_t *hdr) {
189 if (hdr->tag != ALLOCATION_TAG) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700190 return -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700191 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700192
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700193 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700194 del_locked(hdr, &tail, &head);
Elliott Hughes239e7a02013-01-25 17:13:45 -0800195 --gAllocatedBlockCount;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700196 return 0;
197}
198
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700199static inline void poison(hdr_t *hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700200 memset(user(hdr), FREE_POISON, hdr->size);
201}
202
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700203static int was_used_after_free(hdr_t *hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700204 unsigned i;
205 const char *data = (const char *)user(hdr);
206 for (i = 0; i < hdr->size; i++)
207 if (data[i] != FREE_POISON)
208 return 1;
209 return 0;
210}
211
212/* returns 1 if valid, *safe == 1 if safe to dump stack */
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700213static inline int check_guards(hdr_t *hdr, int *safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700214 *safe = 1;
215 if (!is_front_guard_valid(hdr)) {
216 if (hdr->front_guard[0] == FRONT_GUARD) {
217 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED FRONT GUARD\n",
218 user(hdr), hdr->size);
219 } else {
220 log_message("+++ ALLOCATION %p HAS A CORRUPTED FRONT GUARD "\
221 "(NOT DUMPING STACKTRACE)\n", user(hdr));
222 /* Allocation header is probably corrupt, do not print stack trace */
223 *safe = 0;
224 }
225 return 0;
226 }
227
228 if (!is_rear_guard_valid(hdr)) {
229 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED REAR GUARD\n",
230 user(hdr), hdr->size);
231 return 0;
232 }
233
234 return 1;
235}
236
237/* returns 1 if valid, *safe == 1 if safe to dump stack */
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700238static inline int check_allocation_locked(hdr_t *hdr, int *safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700239 int valid = 1;
240 *safe = 1;
241
242 if (hdr->tag != ALLOCATION_TAG && hdr->tag != BACKLOG_TAG) {
243 log_message("+++ ALLOCATION %p HAS INVALID TAG %08x (NOT DUMPING STACKTRACE)\n",
244 user(hdr), hdr->tag);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700245 // Allocation header is probably corrupt, do not dequeue or dump stack
246 // trace.
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700247 *safe = 0;
248 return 0;
249 }
250
251 if (hdr->tag == BACKLOG_TAG && was_used_after_free(hdr)) {
252 log_message("+++ ALLOCATION %p SIZE %d WAS USED AFTER BEING FREED\n",
253 user(hdr), hdr->size);
254 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700255 /* check the guards to see if it's safe to dump a stack trace */
256 check_guards(hdr, safe);
257 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700258 valid = check_guards(hdr, safe);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700259 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700260
261 if (!valid && *safe) {
262 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
263 user(hdr), hdr->size);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800264 log_backtrace(gMapInfo, hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700265 if (hdr->tag == BACKLOG_TAG) {
266 log_message("+++ ALLOCATION %p SIZE %d FREED HERE:\n",
267 user(hdr), hdr->size);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800268 log_backtrace(gMapInfo, hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700269 }
270 }
271
272 return valid;
273}
274
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700275static inline int del_and_check_locked(hdr_t *hdr,
276 hdr_t **tail, hdr_t **head, unsigned *cnt,
277 int *safe) {
278 int valid = check_allocation_locked(hdr, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700279 if (safe) {
280 (*cnt)--;
281 del_locked(hdr, tail, head);
282 }
283 return valid;
284}
285
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700286static inline void del_from_backlog_locked(hdr_t *hdr) {
287 int safe;
288 del_and_check_locked(hdr,
289 &backlog_tail, &backlog_head, &backlog_num,
290 &safe);
291 hdr->tag = 0; /* clear the tag */
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700292}
293
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700294static inline void del_from_backlog(hdr_t *hdr) {
295 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700296 del_from_backlog_locked(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700297}
298
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700299static inline int del_leak(hdr_t *hdr, int *safe) {
300 ScopedPthreadMutexLocker locker(&lock);
Elliott Hughes239e7a02013-01-25 17:13:45 -0800301 return del_and_check_locked(hdr, &tail, &head, &gAllocatedBlockCount, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700302}
303
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700304static inline void add_to_backlog(hdr_t *hdr) {
305 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700306 hdr->tag = BACKLOG_TAG;
307 backlog_num++;
308 add_locked(hdr, &backlog_tail, &backlog_head);
309 poison(hdr);
310 /* If we've exceeded the maximum backlog, clear it up */
311 while (backlog_num > malloc_double_free_backlog) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700312 hdr_t *gone = backlog_tail;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700313 del_from_backlog_locked(gone);
314 dlfree(gone);
315 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700316}
317
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700318extern "C" void* chk_malloc(size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700319// log_message("%s: %s\n", __FILE__, __FUNCTION__);
320
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700321 hdr_t* hdr = static_cast<hdr_t*>(dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700322 if (hdr) {
323 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
324 add(hdr, size);
325 return user(hdr);
326 }
327 return NULL;
328}
329
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700330extern "C" void* chk_memalign(size_t, size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700331// log_message("%s: %s\n", __FILE__, __FUNCTION__);
332 // XXX: it's better to use malloc, than being wrong
333 return chk_malloc(bytes);
334}
335
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700336extern "C" void chk_free(void *ptr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700337// log_message("%s: %s\n", __FILE__, __FUNCTION__);
338
339 if (!ptr) /* ignore free(NULL) */
340 return;
341
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700342 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700343
344 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800345 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700346 int depth;
347 depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
348 if (hdr->tag == BACKLOG_TAG) {
349 log_message("+++ ALLOCATION %p SIZE %d BYTES MULTIPLY FREED!\n",
350 user(hdr), hdr->size);
351 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
352 user(hdr), hdr->size);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800353 log_backtrace(gMapInfo, hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700354 /* hdr->freed_bt_depth should be nonzero here */
355 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
356 user(hdr), hdr->size);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800357 log_backtrace(gMapInfo, hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700358 log_message("+++ ALLOCATION %p SIZE %d NOW BEING FREED HERE:\n",
359 user(hdr), hdr->size);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800360 log_backtrace(gMapInfo, bt, depth);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700361 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700362 log_message("+++ ALLOCATION %p IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
363 user(hdr));
Elliott Hughes1e980b62013-01-17 18:36:06 -0800364 log_backtrace(gMapInfo, bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700365 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700366 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700367 hdr->freed_bt_depth = get_backtrace(hdr->freed_bt,
368 MAX_BACKTRACE_DEPTH);
369 add_to_backlog(hdr);
370 }
371}
372
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700373extern "C" void *chk_realloc(void *ptr, size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700374// log_message("%s: %s\n", __FILE__, __FUNCTION__);
375
Elliott Hughese7e274b2012-10-12 17:05:05 -0700376 if (!ptr) {
377 return chk_malloc(size);
378 }
379
380#ifdef REALLOC_ZERO_BYTES_FREE
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700381 if (!size) {
382 chk_free(ptr);
383 return NULL;
384 }
Elliott Hughese7e274b2012-10-12 17:05:05 -0700385#endif
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700386
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700387 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700388
389 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800390 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700391 int depth;
392 depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
393 if (hdr->tag == BACKLOG_TAG) {
394 log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n",
395 user(hdr), size, hdr->size);
396 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
397 user(hdr), hdr->size);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800398 log_backtrace(gMapInfo, hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700399 /* hdr->freed_bt_depth should be nonzero here */
400 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
401 user(hdr), hdr->size);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800402 log_backtrace(gMapInfo, hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700403 log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n",
404 user(hdr), hdr->size);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800405 log_backtrace(gMapInfo, bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700406
407 /* We take the memory out of the backlog and fall through so the
408 * reallocation below succeeds. Since we didn't really free it, we
409 * can default to this behavior.
410 */
411 del_from_backlog(hdr);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700412 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700413 log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
414 user(hdr), size);
Elliott Hughes1e980b62013-01-17 18:36:06 -0800415 log_backtrace(gMapInfo, bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700416 // just get a whole new allocation and leak the old one
417 return dlrealloc(0, size);
418 // return dlrealloc(user(hdr), size); // assuming it was allocated externally
419 }
420 }
421
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700422 hdr = static_cast<hdr_t*>(dlrealloc(hdr, sizeof(hdr_t) + size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700423 if (hdr) {
424 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
425 add(hdr, size);
426 return user(hdr);
427 }
428
429 return NULL;
430}
431
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700432extern "C" void *chk_calloc(int nmemb, size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700433// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700434 size_t total_size = nmemb * size;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700435 hdr_t* hdr = static_cast<hdr_t*>(dlcalloc(1, sizeof(hdr_t) + total_size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700436 if (hdr) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800437 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700438 add(hdr, total_size);
439 return user(hdr);
440 }
441 return NULL;
442}
443
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700444static void heaptracker_free_leaked_memory() {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800445 if (gAllocatedBlockCount == 0) {
446 return;
447 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700448
Elliott Hughes239e7a02013-01-25 17:13:45 -0800449 // Use /proc/self/exe link to obtain the program name for logging
450 // purposes. If it's not available, we set it to "<unknown>".
451 char exe[PATH_MAX];
452 int count;
453 if ((count = readlink("/proc/self/exe", exe, sizeof(exe) - 1)) == -1) {
454 strlcpy(exe, "<unknown>", sizeof(exe));
455 } else {
456 exe[count] = '\0';
457 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700458
Elliott Hughes239e7a02013-01-25 17:13:45 -0800459 size_t index = 1;
460 const size_t total = gAllocatedBlockCount;
461 while (head != NULL) {
462 int safe;
463 hdr_t* block = head;
464 log_message("+++ %s leaked block of size %d at %p (leak %d of %d)",
465 exe, block->size, user(block), index++, total);
466 if (del_leak(block, &safe)) {
467 /* safe == 1, because the allocation is valid */
468 log_backtrace(gMapInfo, block->bt, block->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700469 }
Elliott Hughes239e7a02013-01-25 17:13:45 -0800470 }
471
472 while (backlog_head != NULL) {
473 del_from_backlog(backlog_tail);
474 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700475}
476
477/* Initializes malloc debugging framework.
478 * See comments on MallocDebugInit in malloc_debug_common.h
479 */
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700480extern "C" int malloc_debug_initialize() {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800481 if (!malloc_double_free_backlog) {
482 malloc_double_free_backlog = BACKLOG_DEFAULT_LEN;
483 }
484 gMapInfo = mapinfo_create(getpid());
485 return 0;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700486}
487
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700488extern "C" void malloc_debug_finalize() {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800489 heaptracker_free_leaked_memory();
490 mapinfo_destroy(gMapInfo);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700491}