blob: 18c3ed4c3604b4c95a8edf8a8a3370fa492cb205 [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
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070055/* libc.debug.malloc.backlog */
56extern unsigned int malloc_double_free_backlog;
57
Elliott Hughes1e980b62013-01-17 18:36:06 -080058#define MAX_BACKTRACE_DEPTH 16
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070059#define ALLOCATION_TAG 0x1ee7d00d
60#define BACKLOG_TAG 0xbabecafe
61#define FREE_POISON 0xa5
62#define BACKLOG_DEFAULT_LEN 100
63#define FRONT_GUARD 0xaa
64#define FRONT_GUARD_LEN (1<<5)
65#define REAR_GUARD 0xbb
66#define REAR_GUARD_LEN (1<<5)
67
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070068static void log_message(const char* format, ...) {
Elliott Hughes1e980b62013-01-17 18:36:06 -080069 va_list args;
70 va_start(args, format);
71 __libc_format_log_va_list(ANDROID_LOG_ERROR, "libc", format, args);
72 va_end(args);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070073}
74
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070075struct hdr_t {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070076 uint32_t tag;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070077 hdr_t* prev;
78 hdr_t* next;
Elliott Hughes239e7a02013-01-25 17:13:45 -080079 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070080 int bt_depth;
Elliott Hughes239e7a02013-01-25 17:13:45 -080081 uintptr_t freed_bt[MAX_BACKTRACE_DEPTH];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070082 int freed_bt_depth;
83 size_t size;
84 char front_guard[FRONT_GUARD_LEN];
85} __attribute__((packed));
86
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070087struct ftr_t {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070088 char rear_guard[REAR_GUARD_LEN];
89} __attribute__((packed));
90
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070091static inline ftr_t* to_ftr(hdr_t* hdr) {
92 return reinterpret_cast<ftr_t*>(reinterpret_cast<char*>(hdr + 1) + hdr->size);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070093}
94
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070095static inline void* user(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070096 return hdr + 1;
97}
98
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070099static inline hdr_t* meta(void* user) {
100 return reinterpret_cast<hdr_t*>(user) - 1;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700101}
102
Elliott Hughes239e7a02013-01-25 17:13:45 -0800103static unsigned gAllocatedBlockCount;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700104static hdr_t *tail;
105static hdr_t *head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700106static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
107
108static unsigned backlog_num;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700109static hdr_t *backlog_tail;
110static hdr_t *backlog_head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700111static pthread_mutex_t backlog_lock = PTHREAD_MUTEX_INITIALIZER;
112
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700113static inline void init_front_guard(hdr_t *hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700114 memset(hdr->front_guard, FRONT_GUARD, FRONT_GUARD_LEN);
115}
116
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700117static inline bool is_front_guard_valid(hdr_t *hdr) {
118 for (size_t i = 0; i < FRONT_GUARD_LEN; i++) {
119 if (hdr->front_guard[i] != FRONT_GUARD) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700120 return 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700121 }
122 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700123 return 1;
124}
125
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700126static inline void init_rear_guard(hdr_t *hdr) {
127 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700128 memset(ftr->rear_guard, REAR_GUARD, REAR_GUARD_LEN);
129}
130
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700131static inline bool is_rear_guard_valid(hdr_t *hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700132 unsigned i;
133 int valid = 1;
134 int first_mismatch = -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700135 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700136 for (i = 0; i < REAR_GUARD_LEN; i++) {
137 if (ftr->rear_guard[i] != REAR_GUARD) {
138 if (first_mismatch < 0)
139 first_mismatch = i;
140 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700141 } else if (first_mismatch >= 0) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700142 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
143 first_mismatch = -1;
144 }
145 }
146
147 if (first_mismatch >= 0)
148 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
149 return valid;
150}
151
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700152static inline void add_locked(hdr_t *hdr, hdr_t **tail, hdr_t **head) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700153 hdr->prev = NULL;
154 hdr->next = *head;
155 if (*head)
156 (*head)->prev = hdr;
157 else
158 *tail = hdr;
159 *head = hdr;
160}
161
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700162static inline int del_locked(hdr_t *hdr, hdr_t **tail, hdr_t **head) {
163 if (hdr->prev) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700164 hdr->prev->next = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700165 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700166 *head = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700167 }
168 if (hdr->next) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700169 hdr->next->prev = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700170 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700171 *tail = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700172 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700173 return 0;
174}
175
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700176static inline void add(hdr_t *hdr, size_t size) {
177 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700178 hdr->tag = ALLOCATION_TAG;
179 hdr->size = size;
180 init_front_guard(hdr);
181 init_rear_guard(hdr);
Elliott Hughes239e7a02013-01-25 17:13:45 -0800182 ++gAllocatedBlockCount;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700183 add_locked(hdr, &tail, &head);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700184}
185
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700186static inline int del(hdr_t *hdr) {
187 if (hdr->tag != ALLOCATION_TAG) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700188 return -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700189 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700190
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700191 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700192 del_locked(hdr, &tail, &head);
Elliott Hughes239e7a02013-01-25 17:13:45 -0800193 --gAllocatedBlockCount;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700194 return 0;
195}
196
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700197static inline void poison(hdr_t *hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700198 memset(user(hdr), FREE_POISON, hdr->size);
199}
200
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700201static int was_used_after_free(hdr_t *hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700202 unsigned i;
203 const char *data = (const char *)user(hdr);
204 for (i = 0; i < hdr->size; i++)
205 if (data[i] != FREE_POISON)
206 return 1;
207 return 0;
208}
209
210/* returns 1 if valid, *safe == 1 if safe to dump stack */
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700211static inline int check_guards(hdr_t *hdr, int *safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700212 *safe = 1;
213 if (!is_front_guard_valid(hdr)) {
214 if (hdr->front_guard[0] == FRONT_GUARD) {
215 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED FRONT GUARD\n",
216 user(hdr), hdr->size);
217 } else {
218 log_message("+++ ALLOCATION %p HAS A CORRUPTED FRONT GUARD "\
219 "(NOT DUMPING STACKTRACE)\n", user(hdr));
220 /* Allocation header is probably corrupt, do not print stack trace */
221 *safe = 0;
222 }
223 return 0;
224 }
225
226 if (!is_rear_guard_valid(hdr)) {
227 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED REAR GUARD\n",
228 user(hdr), hdr->size);
229 return 0;
230 }
231
232 return 1;
233}
234
235/* returns 1 if valid, *safe == 1 if safe to dump stack */
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700236static inline int check_allocation_locked(hdr_t *hdr, int *safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700237 int valid = 1;
238 *safe = 1;
239
240 if (hdr->tag != ALLOCATION_TAG && hdr->tag != BACKLOG_TAG) {
241 log_message("+++ ALLOCATION %p HAS INVALID TAG %08x (NOT DUMPING STACKTRACE)\n",
242 user(hdr), hdr->tag);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700243 // Allocation header is probably corrupt, do not dequeue or dump stack
244 // trace.
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700245 *safe = 0;
246 return 0;
247 }
248
249 if (hdr->tag == BACKLOG_TAG && was_used_after_free(hdr)) {
250 log_message("+++ ALLOCATION %p SIZE %d WAS USED AFTER BEING FREED\n",
251 user(hdr), hdr->size);
252 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700253 /* check the guards to see if it's safe to dump a stack trace */
254 check_guards(hdr, safe);
255 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700256 valid = check_guards(hdr, safe);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700257 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700258
259 if (!valid && *safe) {
260 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
261 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800262 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700263 if (hdr->tag == BACKLOG_TAG) {
264 log_message("+++ ALLOCATION %p SIZE %d FREED HERE:\n",
265 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800266 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700267 }
268 }
269
270 return valid;
271}
272
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700273static inline int del_and_check_locked(hdr_t *hdr,
274 hdr_t **tail, hdr_t **head, unsigned *cnt,
275 int *safe) {
276 int valid = check_allocation_locked(hdr, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700277 if (safe) {
278 (*cnt)--;
279 del_locked(hdr, tail, head);
280 }
281 return valid;
282}
283
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700284static inline void del_from_backlog_locked(hdr_t *hdr) {
285 int safe;
286 del_and_check_locked(hdr,
287 &backlog_tail, &backlog_head, &backlog_num,
288 &safe);
289 hdr->tag = 0; /* clear the tag */
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700290}
291
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700292static inline void del_from_backlog(hdr_t *hdr) {
293 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700294 del_from_backlog_locked(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700295}
296
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700297static inline int del_leak(hdr_t *hdr, int *safe) {
298 ScopedPthreadMutexLocker locker(&lock);
Elliott Hughes239e7a02013-01-25 17:13:45 -0800299 return del_and_check_locked(hdr, &tail, &head, &gAllocatedBlockCount, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700300}
301
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700302static inline void add_to_backlog(hdr_t *hdr) {
303 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700304 hdr->tag = BACKLOG_TAG;
305 backlog_num++;
306 add_locked(hdr, &backlog_tail, &backlog_head);
307 poison(hdr);
308 /* If we've exceeded the maximum backlog, clear it up */
309 while (backlog_num > malloc_double_free_backlog) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700310 hdr_t *gone = backlog_tail;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700311 del_from_backlog_locked(gone);
312 dlfree(gone);
313 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700314}
315
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700316extern "C" void* chk_malloc(size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700317// log_message("%s: %s\n", __FILE__, __FUNCTION__);
318
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700319 hdr_t* hdr = static_cast<hdr_t*>(dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700320 if (hdr) {
321 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
322 add(hdr, size);
323 return user(hdr);
324 }
325 return NULL;
326}
327
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700328extern "C" void* chk_memalign(size_t, size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700329// log_message("%s: %s\n", __FILE__, __FUNCTION__);
330 // XXX: it's better to use malloc, than being wrong
331 return chk_malloc(bytes);
332}
333
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700334extern "C" void chk_free(void *ptr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700335// log_message("%s: %s\n", __FILE__, __FUNCTION__);
336
337 if (!ptr) /* ignore free(NULL) */
338 return;
339
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700340 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700341
342 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800343 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800344 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700345 if (hdr->tag == BACKLOG_TAG) {
346 log_message("+++ ALLOCATION %p SIZE %d BYTES MULTIPLY FREED!\n",
347 user(hdr), hdr->size);
348 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
349 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800350 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700351 /* hdr->freed_bt_depth should be nonzero here */
352 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
353 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800354 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700355 log_message("+++ ALLOCATION %p SIZE %d NOW BEING FREED HERE:\n",
356 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800357 log_backtrace(bt, depth);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700358 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700359 log_message("+++ ALLOCATION %p IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
360 user(hdr));
Elliott Hughes35b621c2013-01-28 16:27:36 -0800361 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700362 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700363 } else {
Elliott Hughes35b621c2013-01-28 16:27:36 -0800364 hdr->freed_bt_depth = get_backtrace(hdr->freed_bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700365 add_to_backlog(hdr);
366 }
367}
368
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700369extern "C" void *chk_realloc(void *ptr, size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700370// log_message("%s: %s\n", __FILE__, __FUNCTION__);
371
Elliott Hughese7e274b2012-10-12 17:05:05 -0700372 if (!ptr) {
373 return chk_malloc(size);
374 }
375
376#ifdef REALLOC_ZERO_BYTES_FREE
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700377 if (!size) {
378 chk_free(ptr);
379 return NULL;
380 }
Elliott Hughese7e274b2012-10-12 17:05:05 -0700381#endif
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700382
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700383 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700384
385 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800386 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800387 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700388 if (hdr->tag == BACKLOG_TAG) {
389 log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n",
390 user(hdr), size, hdr->size);
391 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
392 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800393 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700394 /* 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 Hughes35b621c2013-01-28 16:27:36 -0800397 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700398 log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n",
399 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800400 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700401
402 /* We take the memory out of the backlog and fall through so the
403 * reallocation below succeeds. Since we didn't really free it, we
404 * can default to this behavior.
405 */
406 del_from_backlog(hdr);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700407 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700408 log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
409 user(hdr), size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800410 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700411 // just get a whole new allocation and leak the old one
412 return dlrealloc(0, size);
413 // return dlrealloc(user(hdr), size); // assuming it was allocated externally
414 }
415 }
416
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700417 hdr = static_cast<hdr_t*>(dlrealloc(hdr, sizeof(hdr_t) + size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700418 if (hdr) {
419 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
420 add(hdr, size);
421 return user(hdr);
422 }
423
424 return NULL;
425}
426
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700427extern "C" void *chk_calloc(int nmemb, size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700428// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700429 size_t total_size = nmemb * size;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700430 hdr_t* hdr = static_cast<hdr_t*>(dlcalloc(1, sizeof(hdr_t) + total_size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700431 if (hdr) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800432 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700433 add(hdr, total_size);
434 return user(hdr);
435 }
436 return NULL;
437}
438
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700439static void heaptracker_free_leaked_memory() {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800440 if (gAllocatedBlockCount == 0) {
441 return;
442 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700443
Elliott Hughes239e7a02013-01-25 17:13:45 -0800444 // Use /proc/self/exe link to obtain the program name for logging
445 // purposes. If it's not available, we set it to "<unknown>".
446 char exe[PATH_MAX];
447 int count;
448 if ((count = readlink("/proc/self/exe", exe, sizeof(exe) - 1)) == -1) {
449 strlcpy(exe, "<unknown>", sizeof(exe));
450 } else {
451 exe[count] = '\0';
452 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700453
Elliott Hughes239e7a02013-01-25 17:13:45 -0800454 size_t index = 1;
455 const size_t total = gAllocatedBlockCount;
456 while (head != NULL) {
457 int safe;
458 hdr_t* block = head;
459 log_message("+++ %s leaked block of size %d at %p (leak %d of %d)",
460 exe, block->size, user(block), index++, total);
461 if (del_leak(block, &safe)) {
462 /* safe == 1, because the allocation is valid */
Elliott Hughes35b621c2013-01-28 16:27:36 -0800463 log_backtrace(block->bt, block->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700464 }
Elliott Hughes239e7a02013-01-25 17:13:45 -0800465 }
466
467 while (backlog_head != NULL) {
468 del_from_backlog(backlog_tail);
469 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700470}
471
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700472extern "C" int malloc_debug_initialize() {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800473 if (!malloc_double_free_backlog) {
474 malloc_double_free_backlog = BACKLOG_DEFAULT_LEN;
475 }
Elliott Hughes35b621c2013-01-28 16:27:36 -0800476 backtrace_startup();
Elliott Hughes1e980b62013-01-17 18:36:06 -0800477 return 0;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700478}
479
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700480extern "C" void malloc_debug_finalize() {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800481 heaptracker_free_leaked_memory();
Elliott Hughes35b621c2013-01-28 16:27:36 -0800482 backtrace_shutdown();
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700483}