blob: 7dd8e3c96aa81e596ef49e2072db1367cc512fcc [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"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070051#include "private/libc_logging.h"
Elliott Hughes3b297c42012-10-11 16:08:51 -070052#include "malloc_debug_common.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070053#include "private/ScopedPthreadMutexLocker.h"
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070054
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070055/* libc.debug.malloc.backlog */
Elliott Hughes9c818922013-02-01 17:07:40 -080056extern unsigned int gMallocDebugBacklog;
57extern int gMallocDebugLevel;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070058
Elliott Hughes1e980b62013-01-17 18:36:06 -080059#define MAX_BACKTRACE_DEPTH 16
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070060#define ALLOCATION_TAG 0x1ee7d00d
61#define BACKLOG_TAG 0xbabecafe
62#define FREE_POISON 0xa5
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070063#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;
Christopher Ferris885f3b92013-05-21 17:48:01 -070077 void* base; // Always points to the memory allocated using dlmalloc.
78 // For memory allocated in chk_memalign, this value will
79 // not be the same as the location of the start of this
80 // structure.
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070081 hdr_t* prev;
82 hdr_t* next;
Elliott Hughes239e7a02013-01-25 17:13:45 -080083 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070084 int bt_depth;
Elliott Hughes239e7a02013-01-25 17:13:45 -080085 uintptr_t freed_bt[MAX_BACKTRACE_DEPTH];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070086 int freed_bt_depth;
87 size_t size;
Elliott Hughesef0696d2013-10-08 16:16:01 -070088 uint8_t front_guard[FRONT_GUARD_LEN];
Christopher Ferris885f3b92013-05-21 17:48:01 -070089} __attribute__((packed, aligned(MALLOC_ALIGNMENT)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070090
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070091struct ftr_t {
Elliott Hughesef0696d2013-10-08 16:16:01 -070092 uint8_t rear_guard[REAR_GUARD_LEN];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070093} __attribute__((packed));
94
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070095static inline ftr_t* to_ftr(hdr_t* hdr) {
96 return reinterpret_cast<ftr_t*>(reinterpret_cast<char*>(hdr + 1) + hdr->size);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070097}
98
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070099static inline void* user(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700100 return hdr + 1;
101}
102
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700103static inline hdr_t* meta(void* user) {
104 return reinterpret_cast<hdr_t*>(user) - 1;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700105}
106
Christopher Ferris885f3b92013-05-21 17:48:01 -0700107static inline const hdr_t* const_meta(const void* user) {
108 return reinterpret_cast<const hdr_t*>(user) - 1;
109}
110
111
Elliott Hughes239e7a02013-01-25 17:13:45 -0800112static unsigned gAllocatedBlockCount;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700113static hdr_t* tail;
114static hdr_t* head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700115static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
116
117static unsigned backlog_num;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700118static hdr_t* backlog_tail;
119static hdr_t* backlog_head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700120static pthread_mutex_t backlog_lock = PTHREAD_MUTEX_INITIALIZER;
121
Christopher Ferris885f3b92013-05-21 17:48:01 -0700122static inline void init_front_guard(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700123 memset(hdr->front_guard, FRONT_GUARD, FRONT_GUARD_LEN);
124}
125
Christopher Ferris885f3b92013-05-21 17:48:01 -0700126static inline bool is_front_guard_valid(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700127 for (size_t i = 0; i < FRONT_GUARD_LEN; i++) {
128 if (hdr->front_guard[i] != FRONT_GUARD) {
Elliott Hughesef0696d2013-10-08 16:16:01 -0700129 return false;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700130 }
131 }
Elliott Hughesef0696d2013-10-08 16:16:01 -0700132 return true;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700133}
134
Christopher Ferris885f3b92013-05-21 17:48:01 -0700135static inline void init_rear_guard(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700136 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700137 memset(ftr->rear_guard, REAR_GUARD, REAR_GUARD_LEN);
138}
139
Christopher Ferris885f3b92013-05-21 17:48:01 -0700140static inline bool is_rear_guard_valid(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700141 unsigned i;
142 int valid = 1;
143 int first_mismatch = -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700144 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700145 for (i = 0; i < REAR_GUARD_LEN; i++) {
146 if (ftr->rear_guard[i] != REAR_GUARD) {
147 if (first_mismatch < 0)
148 first_mismatch = i;
149 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700150 } else if (first_mismatch >= 0) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700151 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
152 first_mismatch = -1;
153 }
154 }
155
156 if (first_mismatch >= 0)
157 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
158 return valid;
159}
160
Christopher Ferris885f3b92013-05-21 17:48:01 -0700161static inline void add_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700162 hdr->prev = NULL;
163 hdr->next = *head;
164 if (*head)
165 (*head)->prev = hdr;
166 else
167 *tail = hdr;
168 *head = hdr;
169}
170
Christopher Ferris885f3b92013-05-21 17:48:01 -0700171static inline int del_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700172 if (hdr->prev) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700173 hdr->prev->next = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700174 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700175 *head = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700176 }
177 if (hdr->next) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700178 hdr->next->prev = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700179 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700180 *tail = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700181 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700182 return 0;
183}
184
Christopher Ferris885f3b92013-05-21 17:48:01 -0700185static inline void add(hdr_t* hdr, size_t size) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700186 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700187 hdr->tag = ALLOCATION_TAG;
188 hdr->size = size;
189 init_front_guard(hdr);
190 init_rear_guard(hdr);
Elliott Hughes239e7a02013-01-25 17:13:45 -0800191 ++gAllocatedBlockCount;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700192 add_locked(hdr, &tail, &head);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700193}
194
Christopher Ferris885f3b92013-05-21 17:48:01 -0700195static inline int del(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700196 if (hdr->tag != ALLOCATION_TAG) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700197 return -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700198 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700199
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700200 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700201 del_locked(hdr, &tail, &head);
Elliott Hughes239e7a02013-01-25 17:13:45 -0800202 --gAllocatedBlockCount;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700203 return 0;
204}
205
Christopher Ferris885f3b92013-05-21 17:48:01 -0700206static inline void poison(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700207 memset(user(hdr), FREE_POISON, hdr->size);
208}
209
Elliott Hughesef0696d2013-10-08 16:16:01 -0700210static bool was_used_after_free(hdr_t* hdr) {
211 const uint8_t* data = reinterpret_cast<const uint8_t*>(user(hdr));
212 for (size_t i = 0; i < hdr->size; i++) {
213 if (data[i] != FREE_POISON) {
214 return true;
215 }
216 }
217 return false;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700218}
219
220/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700221static inline int check_guards(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700222 *safe = 1;
223 if (!is_front_guard_valid(hdr)) {
224 if (hdr->front_guard[0] == FRONT_GUARD) {
225 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED FRONT GUARD\n",
226 user(hdr), hdr->size);
227 } else {
228 log_message("+++ ALLOCATION %p HAS A CORRUPTED FRONT GUARD "\
229 "(NOT DUMPING STACKTRACE)\n", user(hdr));
230 /* Allocation header is probably corrupt, do not print stack trace */
231 *safe = 0;
232 }
233 return 0;
234 }
235
236 if (!is_rear_guard_valid(hdr)) {
237 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED REAR GUARD\n",
238 user(hdr), hdr->size);
239 return 0;
240 }
241
242 return 1;
243}
244
245/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700246static inline int check_allocation_locked(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700247 int valid = 1;
248 *safe = 1;
249
250 if (hdr->tag != ALLOCATION_TAG && hdr->tag != BACKLOG_TAG) {
251 log_message("+++ ALLOCATION %p HAS INVALID TAG %08x (NOT DUMPING STACKTRACE)\n",
252 user(hdr), hdr->tag);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700253 // Allocation header is probably corrupt, do not dequeue or dump stack
254 // trace.
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700255 *safe = 0;
256 return 0;
257 }
258
259 if (hdr->tag == BACKLOG_TAG && was_used_after_free(hdr)) {
260 log_message("+++ ALLOCATION %p SIZE %d WAS USED AFTER BEING FREED\n",
261 user(hdr), hdr->size);
262 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700263 /* check the guards to see if it's safe to dump a stack trace */
264 check_guards(hdr, safe);
265 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700266 valid = check_guards(hdr, safe);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700267 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700268
269 if (!valid && *safe) {
270 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
271 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800272 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700273 if (hdr->tag == BACKLOG_TAG) {
274 log_message("+++ ALLOCATION %p SIZE %d FREED HERE:\n",
275 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800276 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700277 }
278 }
279
280 return valid;
281}
282
Christopher Ferris885f3b92013-05-21 17:48:01 -0700283static inline int del_and_check_locked(hdr_t* hdr,
284 hdr_t** tail, hdr_t** head, unsigned* cnt,
285 int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700286 int valid = check_allocation_locked(hdr, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700287 if (safe) {
288 (*cnt)--;
289 del_locked(hdr, tail, head);
290 }
291 return valid;
292}
293
Christopher Ferris885f3b92013-05-21 17:48:01 -0700294static inline void del_from_backlog_locked(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700295 int safe;
296 del_and_check_locked(hdr,
297 &backlog_tail, &backlog_head, &backlog_num,
298 &safe);
299 hdr->tag = 0; /* clear the tag */
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700300}
301
Christopher Ferris885f3b92013-05-21 17:48:01 -0700302static inline void del_from_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700303 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700304 del_from_backlog_locked(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700305}
306
Christopher Ferris885f3b92013-05-21 17:48:01 -0700307static inline int del_leak(hdr_t* hdr, int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700308 ScopedPthreadMutexLocker locker(&lock);
Elliott Hughes239e7a02013-01-25 17:13:45 -0800309 return del_and_check_locked(hdr, &tail, &head, &gAllocatedBlockCount, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700310}
311
Christopher Ferris885f3b92013-05-21 17:48:01 -0700312static inline void add_to_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700313 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700314 hdr->tag = BACKLOG_TAG;
315 backlog_num++;
316 add_locked(hdr, &backlog_tail, &backlog_head);
317 poison(hdr);
318 /* If we've exceeded the maximum backlog, clear it up */
Elliott Hughes9c818922013-02-01 17:07:40 -0800319 while (backlog_num > gMallocDebugBacklog) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700320 hdr_t* gone = backlog_tail;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700321 del_from_backlog_locked(gone);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700322 dlfree(gone->base);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700323 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700324}
325
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700326extern "C" void* chk_malloc(size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700327// log_message("%s: %s\n", __FILE__, __FUNCTION__);
328
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700329 hdr_t* hdr = static_cast<hdr_t*>(dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700330 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700331 hdr->base = hdr;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700332 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
333 add(hdr, size);
334 return user(hdr);
335 }
336 return NULL;
337}
338
Christopher Ferris885f3b92013-05-21 17:48:01 -0700339extern "C" void* chk_memalign(size_t alignment, size_t bytes) {
340 if (alignment <= MALLOC_ALIGNMENT) {
341 return chk_malloc(bytes);
342 }
343
344 // Make the alignment a power of two.
345 if (alignment & (alignment-1)) {
346 alignment = 1L << (31 - __builtin_clz(alignment));
347 }
348
349 // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
350 // we will align by at least MALLOC_ALIGNMENT bytes
351 // and at most alignment-MALLOC_ALIGNMENT bytes
352 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
353 if (size < bytes) { // Overflow.
354 return NULL;
355 }
356
357 void* base = dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t));
358 if (base != NULL) {
359 // Check that the actual pointer that will be returned is aligned
360 // properly.
361 uintptr_t ptr = reinterpret_cast<uintptr_t>(user(reinterpret_cast<hdr_t*>(base)));
362 if ((ptr % alignment) != 0) {
363 // Align the pointer.
364 ptr += ((-ptr) % alignment);
365 }
366
367 hdr_t* hdr = meta(reinterpret_cast<void*>(ptr));
368 hdr->base = base;
369 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
370 add(hdr, bytes);
371 return user(hdr);
372 }
373 return base;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700374}
375
Christopher Ferris885f3b92013-05-21 17:48:01 -0700376extern "C" void chk_free(void* ptr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700377// log_message("%s: %s\n", __FILE__, __FUNCTION__);
378
379 if (!ptr) /* ignore free(NULL) */
380 return;
381
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700382 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700383
384 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800385 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800386 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700387 if (hdr->tag == BACKLOG_TAG) {
388 log_message("+++ ALLOCATION %p SIZE %d BYTES MULTIPLY FREED!\n",
389 user(hdr), hdr->size);
390 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
391 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800392 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700393 /* hdr->freed_bt_depth should be nonzero here */
394 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
395 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800396 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700397 log_message("+++ ALLOCATION %p SIZE %d NOW BEING FREED HERE:\n",
398 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800399 log_backtrace(bt, depth);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700400 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700401 log_message("+++ ALLOCATION %p IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
402 user(hdr));
Elliott Hughes35b621c2013-01-28 16:27:36 -0800403 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700404 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700405 } else {
Elliott Hughes35b621c2013-01-28 16:27:36 -0800406 hdr->freed_bt_depth = get_backtrace(hdr->freed_bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700407 add_to_backlog(hdr);
408 }
409}
410
Christopher Ferris885f3b92013-05-21 17:48:01 -0700411extern "C" void* chk_realloc(void* ptr, size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700412// log_message("%s: %s\n", __FILE__, __FUNCTION__);
413
Elliott Hughese7e274b2012-10-12 17:05:05 -0700414 if (!ptr) {
415 return chk_malloc(size);
416 }
417
418#ifdef REALLOC_ZERO_BYTES_FREE
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700419 if (!size) {
420 chk_free(ptr);
421 return NULL;
422 }
Elliott Hughese7e274b2012-10-12 17:05:05 -0700423#endif
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700424
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700425 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700426
427 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800428 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800429 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700430 if (hdr->tag == BACKLOG_TAG) {
431 log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n",
432 user(hdr), size, hdr->size);
433 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
434 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800435 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700436 /* hdr->freed_bt_depth should be nonzero here */
437 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
438 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800439 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700440 log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n",
441 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800442 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700443
444 /* We take the memory out of the backlog and fall through so the
445 * reallocation below succeeds. Since we didn't really free it, we
446 * can default to this behavior.
447 */
448 del_from_backlog(hdr);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700449 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700450 log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
451 user(hdr), size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800452 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700453 // just get a whole new allocation and leak the old one
454 return dlrealloc(0, size);
455 // return dlrealloc(user(hdr), size); // assuming it was allocated externally
456 }
457 }
458
Christopher Ferris885f3b92013-05-21 17:48:01 -0700459 if (hdr->base != hdr) {
460 // An allocation from memalign, so create another allocation and
461 // copy the data out.
462 void* newMem = dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t));
463 if (newMem) {
464 memcpy(newMem, hdr, sizeof(hdr_t) + hdr->size);
465 dlfree(hdr->base);
466 hdr = static_cast<hdr_t*>(newMem);
467 } else {
468 dlfree(hdr->base);
469 hdr = NULL;
470 }
471 } else {
472 hdr = static_cast<hdr_t*>(dlrealloc(hdr, sizeof(hdr_t) + size + sizeof(ftr_t)));
473 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700474 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700475 hdr->base = hdr;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700476 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
477 add(hdr, size);
478 return user(hdr);
479 }
480
481 return NULL;
482}
483
Christopher Ferris885f3b92013-05-21 17:48:01 -0700484extern "C" void* chk_calloc(int nmemb, size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700485// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700486 size_t total_size = nmemb * size;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700487 hdr_t* hdr = static_cast<hdr_t*>(dlcalloc(1, sizeof(hdr_t) + total_size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700488 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700489 hdr->base = hdr;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800490 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700491 add(hdr, total_size);
492 return user(hdr);
493 }
494 return NULL;
495}
496
Christopher Ferris885f3b92013-05-21 17:48:01 -0700497extern "C" size_t chk_malloc_usable_size(const void* ptr) {
498 // dlmalloc_usable_size returns 0 for NULL and unknown blocks.
499 if (ptr == NULL)
500 return 0;
501
502 const hdr_t* hdr = const_meta(ptr);
503
504 // The sentinel tail is written just after the request block bytes
505 // so there is no extra room we can report here.
506 return hdr->size;
507}
508
Elliott Hughes9c818922013-02-01 17:07:40 -0800509static void ReportMemoryLeaks() {
510 // We only track leaks at level 10.
511 if (gMallocDebugLevel != 10) {
512 return;
513 }
514
Elliott Hughes239e7a02013-01-25 17:13:45 -0800515 // Use /proc/self/exe link to obtain the program name for logging
516 // purposes. If it's not available, we set it to "<unknown>".
517 char exe[PATH_MAX];
518 int count;
519 if ((count = readlink("/proc/self/exe", exe, sizeof(exe) - 1)) == -1) {
520 strlcpy(exe, "<unknown>", sizeof(exe));
521 } else {
522 exe[count] = '\0';
523 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700524
Elliott Hughes1d12d572013-01-30 11:38:26 -0800525 if (gAllocatedBlockCount == 0) {
526 log_message("+++ %s did not leak", exe);
527 return;
528 }
529
Elliott Hughes239e7a02013-01-25 17:13:45 -0800530 size_t index = 1;
531 const size_t total = gAllocatedBlockCount;
532 while (head != NULL) {
533 int safe;
534 hdr_t* block = head;
535 log_message("+++ %s leaked block of size %d at %p (leak %d of %d)",
536 exe, block->size, user(block), index++, total);
537 if (del_leak(block, &safe)) {
538 /* safe == 1, because the allocation is valid */
Elliott Hughes35b621c2013-01-28 16:27:36 -0800539 log_backtrace(block->bt, block->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700540 }
Elliott Hughes239e7a02013-01-25 17:13:45 -0800541 }
542
543 while (backlog_head != NULL) {
544 del_from_backlog(backlog_tail);
545 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700546}
547
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700548extern "C" int malloc_debug_initialize() {
Elliott Hughes35b621c2013-01-28 16:27:36 -0800549 backtrace_startup();
Elliott Hughes1e980b62013-01-17 18:36:06 -0800550 return 0;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700551}
552
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700553extern "C" void malloc_debug_finalize() {
Elliott Hughes9c818922013-02-01 17:07:40 -0800554 ReportMemoryLeaks();
Elliott Hughes35b621c2013-01-28 16:27:36 -0800555 backtrace_shutdown();
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700556}