blob: 11578a3342d9ca346eb94feb964fe80ba1359912 [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 Hughes1728b232014-05-14 10:02:03 -070056extern unsigned int g_malloc_debug_backlog;
57extern int g_malloc_debug_level;
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
Elliott Hughes1728b232014-05-14 10:02:03 -0700111// TODO: introduce a struct for this global state.
112// There are basically two lists here, the regular list and the backlog list.
113// We should be able to remove the duplication.
114static unsigned g_allocated_block_count;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700115static hdr_t* tail;
116static hdr_t* head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700117static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
118
119static unsigned backlog_num;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700120static hdr_t* backlog_tail;
121static hdr_t* backlog_head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700122static pthread_mutex_t backlog_lock = PTHREAD_MUTEX_INITIALIZER;
123
Christopher Ferris885f3b92013-05-21 17:48:01 -0700124static inline void init_front_guard(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700125 memset(hdr->front_guard, FRONT_GUARD, FRONT_GUARD_LEN);
126}
127
Christopher Ferris885f3b92013-05-21 17:48:01 -0700128static inline bool is_front_guard_valid(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700129 for (size_t i = 0; i < FRONT_GUARD_LEN; i++) {
130 if (hdr->front_guard[i] != FRONT_GUARD) {
Elliott Hughesef0696d2013-10-08 16:16:01 -0700131 return false;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700132 }
133 }
Elliott Hughesef0696d2013-10-08 16:16:01 -0700134 return true;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700135}
136
Christopher Ferris885f3b92013-05-21 17:48:01 -0700137static inline void init_rear_guard(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700138 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700139 memset(ftr->rear_guard, REAR_GUARD, REAR_GUARD_LEN);
140}
141
Christopher Ferris885f3b92013-05-21 17:48:01 -0700142static inline bool is_rear_guard_valid(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700143 unsigned i;
144 int valid = 1;
145 int first_mismatch = -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700146 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700147 for (i = 0; i < REAR_GUARD_LEN; i++) {
148 if (ftr->rear_guard[i] != REAR_GUARD) {
149 if (first_mismatch < 0)
150 first_mismatch = i;
151 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700152 } else if (first_mismatch >= 0) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700153 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
154 first_mismatch = -1;
155 }
156 }
157
158 if (first_mismatch >= 0)
159 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
160 return valid;
161}
162
Christopher Ferris885f3b92013-05-21 17:48:01 -0700163static inline void add_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700164 hdr->prev = NULL;
165 hdr->next = *head;
166 if (*head)
167 (*head)->prev = hdr;
168 else
169 *tail = hdr;
170 *head = hdr;
171}
172
Christopher Ferris885f3b92013-05-21 17:48:01 -0700173static inline int del_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700174 if (hdr->prev) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700175 hdr->prev->next = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700176 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700177 *head = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700178 }
179 if (hdr->next) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700180 hdr->next->prev = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700181 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700182 *tail = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700183 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700184 return 0;
185}
186
Christopher Ferris885f3b92013-05-21 17:48:01 -0700187static inline void add(hdr_t* hdr, size_t size) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700188 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700189 hdr->tag = ALLOCATION_TAG;
190 hdr->size = size;
191 init_front_guard(hdr);
192 init_rear_guard(hdr);
Elliott Hughes1728b232014-05-14 10:02:03 -0700193 ++g_allocated_block_count;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700194 add_locked(hdr, &tail, &head);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700195}
196
Christopher Ferris885f3b92013-05-21 17:48:01 -0700197static inline int del(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700198 if (hdr->tag != ALLOCATION_TAG) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700199 return -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700200 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700201
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700202 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700203 del_locked(hdr, &tail, &head);
Elliott Hughes1728b232014-05-14 10:02:03 -0700204 --g_allocated_block_count;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700205 return 0;
206}
207
Christopher Ferris885f3b92013-05-21 17:48:01 -0700208static inline void poison(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700209 memset(user(hdr), FREE_POISON, hdr->size);
210}
211
Elliott Hughesef0696d2013-10-08 16:16:01 -0700212static bool was_used_after_free(hdr_t* hdr) {
213 const uint8_t* data = reinterpret_cast<const uint8_t*>(user(hdr));
214 for (size_t i = 0; i < hdr->size; i++) {
215 if (data[i] != FREE_POISON) {
216 return true;
217 }
218 }
219 return false;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700220}
221
222/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700223static inline int check_guards(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700224 *safe = 1;
225 if (!is_front_guard_valid(hdr)) {
226 if (hdr->front_guard[0] == FRONT_GUARD) {
227 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED FRONT GUARD\n",
228 user(hdr), hdr->size);
229 } else {
230 log_message("+++ ALLOCATION %p HAS A CORRUPTED FRONT GUARD "\
231 "(NOT DUMPING STACKTRACE)\n", user(hdr));
232 /* Allocation header is probably corrupt, do not print stack trace */
233 *safe = 0;
234 }
235 return 0;
236 }
237
238 if (!is_rear_guard_valid(hdr)) {
239 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED REAR GUARD\n",
240 user(hdr), hdr->size);
241 return 0;
242 }
243
244 return 1;
245}
246
247/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700248static inline int check_allocation_locked(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700249 int valid = 1;
250 *safe = 1;
251
252 if (hdr->tag != ALLOCATION_TAG && hdr->tag != BACKLOG_TAG) {
253 log_message("+++ ALLOCATION %p HAS INVALID TAG %08x (NOT DUMPING STACKTRACE)\n",
254 user(hdr), hdr->tag);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700255 // Allocation header is probably corrupt, do not dequeue or dump stack
256 // trace.
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700257 *safe = 0;
258 return 0;
259 }
260
261 if (hdr->tag == BACKLOG_TAG && was_used_after_free(hdr)) {
262 log_message("+++ ALLOCATION %p SIZE %d WAS USED AFTER BEING FREED\n",
263 user(hdr), hdr->size);
264 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700265 /* check the guards to see if it's safe to dump a stack trace */
266 check_guards(hdr, safe);
267 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700268 valid = check_guards(hdr, safe);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700269 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700270
271 if (!valid && *safe) {
272 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
273 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800274 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700275 if (hdr->tag == BACKLOG_TAG) {
276 log_message("+++ ALLOCATION %p SIZE %d FREED HERE:\n",
277 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800278 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700279 }
280 }
281
282 return valid;
283}
284
Christopher Ferris885f3b92013-05-21 17:48:01 -0700285static inline int del_and_check_locked(hdr_t* hdr,
286 hdr_t** tail, hdr_t** head, unsigned* cnt,
287 int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700288 int valid = check_allocation_locked(hdr, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700289 if (safe) {
290 (*cnt)--;
291 del_locked(hdr, tail, head);
292 }
293 return valid;
294}
295
Christopher Ferris885f3b92013-05-21 17:48:01 -0700296static inline void del_from_backlog_locked(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700297 int safe;
298 del_and_check_locked(hdr,
299 &backlog_tail, &backlog_head, &backlog_num,
300 &safe);
301 hdr->tag = 0; /* clear the tag */
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700302}
303
Christopher Ferris885f3b92013-05-21 17:48:01 -0700304static inline void del_from_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700305 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700306 del_from_backlog_locked(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700307}
308
Christopher Ferris885f3b92013-05-21 17:48:01 -0700309static inline int del_leak(hdr_t* hdr, int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700310 ScopedPthreadMutexLocker locker(&lock);
Elliott Hughes1728b232014-05-14 10:02:03 -0700311 return del_and_check_locked(hdr, &tail, &head, &g_allocated_block_count, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700312}
313
Christopher Ferris885f3b92013-05-21 17:48:01 -0700314static inline void add_to_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700315 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700316 hdr->tag = BACKLOG_TAG;
317 backlog_num++;
318 add_locked(hdr, &backlog_tail, &backlog_head);
319 poison(hdr);
320 /* If we've exceeded the maximum backlog, clear it up */
Elliott Hughes1728b232014-05-14 10:02:03 -0700321 while (backlog_num > g_malloc_debug_backlog) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700322 hdr_t* gone = backlog_tail;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700323 del_from_backlog_locked(gone);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700324 dlfree(gone->base);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700325 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700326}
327
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700328extern "C" void* chk_malloc(size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700329// log_message("%s: %s\n", __FILE__, __FUNCTION__);
330
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700331 hdr_t* hdr = static_cast<hdr_t*>(dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700332 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700333 hdr->base = hdr;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700334 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
335 add(hdr, size);
336 return user(hdr);
337 }
338 return NULL;
339}
340
Christopher Ferris885f3b92013-05-21 17:48:01 -0700341extern "C" void* chk_memalign(size_t alignment, size_t bytes) {
342 if (alignment <= MALLOC_ALIGNMENT) {
343 return chk_malloc(bytes);
344 }
345
346 // Make the alignment a power of two.
347 if (alignment & (alignment-1)) {
348 alignment = 1L << (31 - __builtin_clz(alignment));
349 }
350
351 // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
352 // we will align by at least MALLOC_ALIGNMENT bytes
353 // and at most alignment-MALLOC_ALIGNMENT bytes
354 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
355 if (size < bytes) { // Overflow.
356 return NULL;
357 }
358
359 void* base = dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t));
360 if (base != NULL) {
361 // Check that the actual pointer that will be returned is aligned
362 // properly.
363 uintptr_t ptr = reinterpret_cast<uintptr_t>(user(reinterpret_cast<hdr_t*>(base)));
364 if ((ptr % alignment) != 0) {
365 // Align the pointer.
366 ptr += ((-ptr) % alignment);
367 }
368
369 hdr_t* hdr = meta(reinterpret_cast<void*>(ptr));
370 hdr->base = base;
371 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
372 add(hdr, bytes);
373 return user(hdr);
374 }
375 return base;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700376}
377
Christopher Ferris885f3b92013-05-21 17:48:01 -0700378extern "C" void chk_free(void* ptr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700379// log_message("%s: %s\n", __FILE__, __FUNCTION__);
380
381 if (!ptr) /* ignore free(NULL) */
382 return;
383
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700384 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700385
386 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800387 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800388 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700389 if (hdr->tag == BACKLOG_TAG) {
390 log_message("+++ ALLOCATION %p SIZE %d BYTES MULTIPLY FREED!\n",
391 user(hdr), hdr->size);
392 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
393 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800394 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700395 /* hdr->freed_bt_depth should be nonzero here */
396 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
397 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800398 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700399 log_message("+++ ALLOCATION %p SIZE %d NOW BEING FREED HERE:\n",
400 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800401 log_backtrace(bt, depth);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700402 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700403 log_message("+++ ALLOCATION %p IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
404 user(hdr));
Elliott Hughes35b621c2013-01-28 16:27:36 -0800405 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700406 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700407 } else {
Elliott Hughes35b621c2013-01-28 16:27:36 -0800408 hdr->freed_bt_depth = get_backtrace(hdr->freed_bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700409 add_to_backlog(hdr);
410 }
411}
412
Christopher Ferris885f3b92013-05-21 17:48:01 -0700413extern "C" void* chk_realloc(void* ptr, size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700414// log_message("%s: %s\n", __FILE__, __FUNCTION__);
415
Elliott Hughese7e274b2012-10-12 17:05:05 -0700416 if (!ptr) {
417 return chk_malloc(size);
418 }
419
420#ifdef REALLOC_ZERO_BYTES_FREE
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700421 if (!size) {
422 chk_free(ptr);
423 return NULL;
424 }
Elliott Hughese7e274b2012-10-12 17:05:05 -0700425#endif
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700426
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700427 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700428
429 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800430 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800431 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700432 if (hdr->tag == BACKLOG_TAG) {
433 log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n",
434 user(hdr), size, hdr->size);
435 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
436 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800437 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700438 /* hdr->freed_bt_depth should be nonzero here */
439 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
440 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800441 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700442 log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n",
443 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800444 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700445
446 /* We take the memory out of the backlog and fall through so the
447 * reallocation below succeeds. Since we didn't really free it, we
448 * can default to this behavior.
449 */
450 del_from_backlog(hdr);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700451 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700452 log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
453 user(hdr), size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800454 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700455 // just get a whole new allocation and leak the old one
456 return dlrealloc(0, size);
457 // return dlrealloc(user(hdr), size); // assuming it was allocated externally
458 }
459 }
460
Christopher Ferris885f3b92013-05-21 17:48:01 -0700461 if (hdr->base != hdr) {
462 // An allocation from memalign, so create another allocation and
463 // copy the data out.
464 void* newMem = dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t));
465 if (newMem) {
466 memcpy(newMem, hdr, sizeof(hdr_t) + hdr->size);
467 dlfree(hdr->base);
468 hdr = static_cast<hdr_t*>(newMem);
469 } else {
470 dlfree(hdr->base);
471 hdr = NULL;
472 }
473 } else {
474 hdr = static_cast<hdr_t*>(dlrealloc(hdr, sizeof(hdr_t) + size + sizeof(ftr_t)));
475 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700476 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700477 hdr->base = hdr;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700478 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
479 add(hdr, size);
480 return user(hdr);
481 }
482
483 return NULL;
484}
485
Christopher Ferris885f3b92013-05-21 17:48:01 -0700486extern "C" void* chk_calloc(int nmemb, size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700487// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700488 size_t total_size = nmemb * size;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700489 hdr_t* hdr = static_cast<hdr_t*>(dlcalloc(1, sizeof(hdr_t) + total_size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700490 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700491 hdr->base = hdr;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800492 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700493 add(hdr, total_size);
494 return user(hdr);
495 }
496 return NULL;
497}
498
Christopher Ferris885f3b92013-05-21 17:48:01 -0700499extern "C" size_t chk_malloc_usable_size(const void* ptr) {
500 // dlmalloc_usable_size returns 0 for NULL and unknown blocks.
501 if (ptr == NULL)
502 return 0;
503
504 const hdr_t* hdr = const_meta(ptr);
505
506 // The sentinel tail is written just after the request block bytes
507 // so there is no extra room we can report here.
508 return hdr->size;
509}
510
Elliott Hughes9c818922013-02-01 17:07:40 -0800511static void ReportMemoryLeaks() {
512 // We only track leaks at level 10.
Elliott Hughes1728b232014-05-14 10:02:03 -0700513 if (g_malloc_debug_level != 10) {
Elliott Hughes9c818922013-02-01 17:07:40 -0800514 return;
515 }
516
Elliott Hughes239e7a02013-01-25 17:13:45 -0800517 // Use /proc/self/exe link to obtain the program name for logging
518 // purposes. If it's not available, we set it to "<unknown>".
519 char exe[PATH_MAX];
520 int count;
521 if ((count = readlink("/proc/self/exe", exe, sizeof(exe) - 1)) == -1) {
522 strlcpy(exe, "<unknown>", sizeof(exe));
523 } else {
524 exe[count] = '\0';
525 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700526
Elliott Hughes1728b232014-05-14 10:02:03 -0700527 if (g_allocated_block_count == 0) {
Elliott Hughes1d12d572013-01-30 11:38:26 -0800528 log_message("+++ %s did not leak", exe);
529 return;
530 }
531
Elliott Hughes239e7a02013-01-25 17:13:45 -0800532 size_t index = 1;
Elliott Hughes1728b232014-05-14 10:02:03 -0700533 const size_t total = g_allocated_block_count;
Elliott Hughes239e7a02013-01-25 17:13:45 -0800534 while (head != NULL) {
535 int safe;
536 hdr_t* block = head;
537 log_message("+++ %s leaked block of size %d at %p (leak %d of %d)",
538 exe, block->size, user(block), index++, total);
539 if (del_leak(block, &safe)) {
540 /* safe == 1, because the allocation is valid */
Elliott Hughes35b621c2013-01-28 16:27:36 -0800541 log_backtrace(block->bt, block->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700542 }
Elliott Hughes239e7a02013-01-25 17:13:45 -0800543 }
544
545 while (backlog_head != NULL) {
546 del_from_backlog(backlog_tail);
547 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700548}
549
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700550extern "C" int malloc_debug_initialize() {
Elliott Hughes35b621c2013-01-28 16:27:36 -0800551 backtrace_startup();
Elliott Hughes1e980b62013-01-17 18:36:06 -0800552 return 0;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700553}
554
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700555extern "C" void malloc_debug_finalize() {
Elliott Hughes9c818922013-02-01 17:07:40 -0800556 ReportMemoryLeaks();
Elliott Hughes35b621c2013-01-28 16:27:36 -0800557 backtrace_shutdown();
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700558}