blob: 11a6ec192f984013334b66f3f565e657b87e0399 [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 Hughes8f2a5a02013-03-15 15:30:25 -070051#include "libc_logging.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 */
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;
88 char 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 {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070092 char rear_guard[REAR_GUARD_LEN];
93} __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) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700129 return 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700130 }
131 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700132 return 1;
133}
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
Christopher Ferris885f3b92013-05-21 17:48:01 -0700210static int was_used_after_free(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700211 unsigned i;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700212 const char* data = reinterpret_cast<const char *>(user(hdr));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700213 for (i = 0; i < hdr->size; i++)
214 if (data[i] != FREE_POISON)
215 return 1;
216 return 0;
217}
218
219/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700220static inline int check_guards(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700221 *safe = 1;
222 if (!is_front_guard_valid(hdr)) {
223 if (hdr->front_guard[0] == FRONT_GUARD) {
224 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED FRONT GUARD\n",
225 user(hdr), hdr->size);
226 } else {
227 log_message("+++ ALLOCATION %p HAS A CORRUPTED FRONT GUARD "\
228 "(NOT DUMPING STACKTRACE)\n", user(hdr));
229 /* Allocation header is probably corrupt, do not print stack trace */
230 *safe = 0;
231 }
232 return 0;
233 }
234
235 if (!is_rear_guard_valid(hdr)) {
236 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED REAR GUARD\n",
237 user(hdr), hdr->size);
238 return 0;
239 }
240
241 return 1;
242}
243
244/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700245static inline int check_allocation_locked(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700246 int valid = 1;
247 *safe = 1;
248
249 if (hdr->tag != ALLOCATION_TAG && hdr->tag != BACKLOG_TAG) {
250 log_message("+++ ALLOCATION %p HAS INVALID TAG %08x (NOT DUMPING STACKTRACE)\n",
251 user(hdr), hdr->tag);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700252 // Allocation header is probably corrupt, do not dequeue or dump stack
253 // trace.
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700254 *safe = 0;
255 return 0;
256 }
257
258 if (hdr->tag == BACKLOG_TAG && was_used_after_free(hdr)) {
259 log_message("+++ ALLOCATION %p SIZE %d WAS USED AFTER BEING FREED\n",
260 user(hdr), hdr->size);
261 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700262 /* check the guards to see if it's safe to dump a stack trace */
263 check_guards(hdr, safe);
264 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700265 valid = check_guards(hdr, safe);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700266 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700267
268 if (!valid && *safe) {
269 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
270 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800271 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700272 if (hdr->tag == BACKLOG_TAG) {
273 log_message("+++ ALLOCATION %p SIZE %d FREED HERE:\n",
274 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800275 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700276 }
277 }
278
279 return valid;
280}
281
Christopher Ferris885f3b92013-05-21 17:48:01 -0700282static inline int del_and_check_locked(hdr_t* hdr,
283 hdr_t** tail, hdr_t** head, unsigned* cnt,
284 int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700285 int valid = check_allocation_locked(hdr, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700286 if (safe) {
287 (*cnt)--;
288 del_locked(hdr, tail, head);
289 }
290 return valid;
291}
292
Christopher Ferris885f3b92013-05-21 17:48:01 -0700293static inline void del_from_backlog_locked(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700294 int safe;
295 del_and_check_locked(hdr,
296 &backlog_tail, &backlog_head, &backlog_num,
297 &safe);
298 hdr->tag = 0; /* clear the tag */
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700299}
300
Christopher Ferris885f3b92013-05-21 17:48:01 -0700301static inline void del_from_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700302 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700303 del_from_backlog_locked(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700304}
305
Christopher Ferris885f3b92013-05-21 17:48:01 -0700306static inline int del_leak(hdr_t* hdr, int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700307 ScopedPthreadMutexLocker locker(&lock);
Elliott Hughes239e7a02013-01-25 17:13:45 -0800308 return del_and_check_locked(hdr, &tail, &head, &gAllocatedBlockCount, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700309}
310
Christopher Ferris885f3b92013-05-21 17:48:01 -0700311static inline void add_to_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700312 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700313 hdr->tag = BACKLOG_TAG;
314 backlog_num++;
315 add_locked(hdr, &backlog_tail, &backlog_head);
316 poison(hdr);
317 /* If we've exceeded the maximum backlog, clear it up */
Elliott Hughes9c818922013-02-01 17:07:40 -0800318 while (backlog_num > gMallocDebugBacklog) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700319 hdr_t* gone = backlog_tail;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700320 del_from_backlog_locked(gone);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700321 dlfree(gone->base);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700322 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700323}
324
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700325extern "C" void* chk_malloc(size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700326// log_message("%s: %s\n", __FILE__, __FUNCTION__);
327
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700328 hdr_t* hdr = static_cast<hdr_t*>(dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700329 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700330 hdr->base = hdr;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700331 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
332 add(hdr, size);
333 return user(hdr);
334 }
335 return NULL;
336}
337
Christopher Ferris885f3b92013-05-21 17:48:01 -0700338extern "C" void* chk_memalign(size_t alignment, size_t bytes) {
339 if (alignment <= MALLOC_ALIGNMENT) {
340 return chk_malloc(bytes);
341 }
342
343 // Make the alignment a power of two.
344 if (alignment & (alignment-1)) {
345 alignment = 1L << (31 - __builtin_clz(alignment));
346 }
347
348 // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
349 // we will align by at least MALLOC_ALIGNMENT bytes
350 // and at most alignment-MALLOC_ALIGNMENT bytes
351 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
352 if (size < bytes) { // Overflow.
353 return NULL;
354 }
355
356 void* base = dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t));
357 if (base != NULL) {
358 // Check that the actual pointer that will be returned is aligned
359 // properly.
360 uintptr_t ptr = reinterpret_cast<uintptr_t>(user(reinterpret_cast<hdr_t*>(base)));
361 if ((ptr % alignment) != 0) {
362 // Align the pointer.
363 ptr += ((-ptr) % alignment);
364 }
365
366 hdr_t* hdr = meta(reinterpret_cast<void*>(ptr));
367 hdr->base = base;
368 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
369 add(hdr, bytes);
370 return user(hdr);
371 }
372 return base;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700373}
374
Christopher Ferris885f3b92013-05-21 17:48:01 -0700375extern "C" void chk_free(void* ptr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700376// log_message("%s: %s\n", __FILE__, __FUNCTION__);
377
378 if (!ptr) /* ignore free(NULL) */
379 return;
380
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700381 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700382
383 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800384 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800385 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700386 if (hdr->tag == BACKLOG_TAG) {
387 log_message("+++ ALLOCATION %p SIZE %d BYTES MULTIPLY FREED!\n",
388 user(hdr), hdr->size);
389 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
390 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800391 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700392 /* hdr->freed_bt_depth should be nonzero here */
393 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
394 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800395 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700396 log_message("+++ ALLOCATION %p SIZE %d NOW BEING FREED HERE:\n",
397 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800398 log_backtrace(bt, depth);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700399 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700400 log_message("+++ ALLOCATION %p IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
401 user(hdr));
Elliott Hughes35b621c2013-01-28 16:27:36 -0800402 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700403 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700404 } else {
Elliott Hughes35b621c2013-01-28 16:27:36 -0800405 hdr->freed_bt_depth = get_backtrace(hdr->freed_bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700406 add_to_backlog(hdr);
407 }
408}
409
Christopher Ferris885f3b92013-05-21 17:48:01 -0700410extern "C" void* chk_realloc(void* ptr, size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700411// log_message("%s: %s\n", __FILE__, __FUNCTION__);
412
Elliott Hughese7e274b2012-10-12 17:05:05 -0700413 if (!ptr) {
414 return chk_malloc(size);
415 }
416
417#ifdef REALLOC_ZERO_BYTES_FREE
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700418 if (!size) {
419 chk_free(ptr);
420 return NULL;
421 }
Elliott Hughese7e274b2012-10-12 17:05:05 -0700422#endif
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700423
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700424 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700425
426 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800427 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800428 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700429 if (hdr->tag == BACKLOG_TAG) {
430 log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n",
431 user(hdr), size, hdr->size);
432 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
433 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800434 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700435 /* hdr->freed_bt_depth should be nonzero here */
436 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
437 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800438 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700439 log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n",
440 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800441 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700442
443 /* We take the memory out of the backlog and fall through so the
444 * reallocation below succeeds. Since we didn't really free it, we
445 * can default to this behavior.
446 */
447 del_from_backlog(hdr);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700448 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700449 log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
450 user(hdr), size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800451 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700452 // just get a whole new allocation and leak the old one
453 return dlrealloc(0, size);
454 // return dlrealloc(user(hdr), size); // assuming it was allocated externally
455 }
456 }
457
Christopher Ferris885f3b92013-05-21 17:48:01 -0700458 if (hdr->base != hdr) {
459 // An allocation from memalign, so create another allocation and
460 // copy the data out.
461 void* newMem = dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t));
462 if (newMem) {
463 memcpy(newMem, hdr, sizeof(hdr_t) + hdr->size);
464 dlfree(hdr->base);
465 hdr = static_cast<hdr_t*>(newMem);
466 } else {
467 dlfree(hdr->base);
468 hdr = NULL;
469 }
470 } else {
471 hdr = static_cast<hdr_t*>(dlrealloc(hdr, sizeof(hdr_t) + size + sizeof(ftr_t)));
472 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700473 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700474 hdr->base = hdr;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700475 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
476 add(hdr, size);
477 return user(hdr);
478 }
479
480 return NULL;
481}
482
Christopher Ferris885f3b92013-05-21 17:48:01 -0700483extern "C" void* chk_calloc(int nmemb, size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700484// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700485 size_t total_size = nmemb * size;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700486 hdr_t* hdr = static_cast<hdr_t*>(dlcalloc(1, sizeof(hdr_t) + total_size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700487 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700488 hdr->base = hdr;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800489 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700490 add(hdr, total_size);
491 return user(hdr);
492 }
493 return NULL;
494}
495
Christopher Ferris885f3b92013-05-21 17:48:01 -0700496extern "C" size_t chk_malloc_usable_size(const void* ptr) {
497 // dlmalloc_usable_size returns 0 for NULL and unknown blocks.
498 if (ptr == NULL)
499 return 0;
500
501 const hdr_t* hdr = const_meta(ptr);
502
503 // The sentinel tail is written just after the request block bytes
504 // so there is no extra room we can report here.
505 return hdr->size;
506}
507
Elliott Hughes9c818922013-02-01 17:07:40 -0800508static void ReportMemoryLeaks() {
509 // We only track leaks at level 10.
510 if (gMallocDebugLevel != 10) {
511 return;
512 }
513
Elliott Hughes239e7a02013-01-25 17:13:45 -0800514 // Use /proc/self/exe link to obtain the program name for logging
515 // purposes. If it's not available, we set it to "<unknown>".
516 char exe[PATH_MAX];
517 int count;
518 if ((count = readlink("/proc/self/exe", exe, sizeof(exe) - 1)) == -1) {
519 strlcpy(exe, "<unknown>", sizeof(exe));
520 } else {
521 exe[count] = '\0';
522 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700523
Elliott Hughes1d12d572013-01-30 11:38:26 -0800524 if (gAllocatedBlockCount == 0) {
525 log_message("+++ %s did not leak", exe);
526 return;
527 }
528
Elliott Hughes239e7a02013-01-25 17:13:45 -0800529 size_t index = 1;
530 const size_t total = gAllocatedBlockCount;
531 while (head != NULL) {
532 int safe;
533 hdr_t* block = head;
534 log_message("+++ %s leaked block of size %d at %p (leak %d of %d)",
535 exe, block->size, user(block), index++, total);
536 if (del_leak(block, &safe)) {
537 /* safe == 1, because the allocation is valid */
Elliott Hughes35b621c2013-01-28 16:27:36 -0800538 log_backtrace(block->bt, block->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700539 }
Elliott Hughes239e7a02013-01-25 17:13:45 -0800540 }
541
542 while (backlog_head != NULL) {
543 del_from_backlog(backlog_tail);
544 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700545}
546
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700547extern "C" int malloc_debug_initialize() {
Elliott Hughes35b621c2013-01-28 16:27:36 -0800548 backtrace_startup();
Elliott Hughes1e980b62013-01-17 18:36:06 -0800549 return 0;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700550}
551
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700552extern "C" void malloc_debug_finalize() {
Elliott Hughes9c818922013-02-01 17:07:40 -0800553 ReportMemoryLeaks();
Elliott Hughes35b621c2013-01-28 16:27:36 -0800554 backtrace_shutdown();
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700555}