blob: 2590ce78159a955ae1df16798f617fa2bc0e9d51 [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"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070050#include "private/libc_logging.h"
Elliott Hughes3b297c42012-10-11 16:08:51 -070051#include "malloc_debug_common.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070052#include "private/ScopedPthreadMutexLocker.h"
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070053
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070054/* libc.debug.malloc.backlog */
Elliott Hughes1728b232014-05-14 10:02:03 -070055extern unsigned int g_malloc_debug_backlog;
56extern int g_malloc_debug_level;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070057
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
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070062#define FRONT_GUARD 0xaa
63#define FRONT_GUARD_LEN (1<<5)
64#define REAR_GUARD 0xbb
65#define REAR_GUARD_LEN (1<<5)
66
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070067static void log_message(const char* format, ...) {
Elliott Hughes1e980b62013-01-17 18:36:06 -080068 va_list args;
69 va_start(args, format);
70 __libc_format_log_va_list(ANDROID_LOG_ERROR, "libc", format, args);
71 va_end(args);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070072}
73
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070074struct hdr_t {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070075 uint32_t tag;
Christopher Ferris72bbd422014-05-08 11:14:03 -070076 void* base; // Always points to the memory allocated using malloc.
Christopher Ferris885f3b92013-05-21 17:48:01 -070077 // For memory allocated in chk_memalign, this value will
78 // not be the same as the location of the start of this
79 // structure.
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070080 hdr_t* prev;
81 hdr_t* next;
Elliott Hughes239e7a02013-01-25 17:13:45 -080082 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070083 int bt_depth;
Elliott Hughes239e7a02013-01-25 17:13:45 -080084 uintptr_t freed_bt[MAX_BACKTRACE_DEPTH];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070085 int freed_bt_depth;
86 size_t size;
Elliott Hughesef0696d2013-10-08 16:16:01 -070087 uint8_t front_guard[FRONT_GUARD_LEN];
Christopher Ferris885f3b92013-05-21 17:48:01 -070088} __attribute__((packed, aligned(MALLOC_ALIGNMENT)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070089
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070090struct ftr_t {
Elliott Hughesef0696d2013-10-08 16:16:01 -070091 uint8_t rear_guard[REAR_GUARD_LEN];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070092} __attribute__((packed));
93
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070094static inline ftr_t* to_ftr(hdr_t* hdr) {
95 return reinterpret_cast<ftr_t*>(reinterpret_cast<char*>(hdr + 1) + hdr->size);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070096}
97
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070098static inline void* user(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070099 return hdr + 1;
100}
101
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700102static inline hdr_t* meta(void* user) {
103 return reinterpret_cast<hdr_t*>(user) - 1;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700104}
105
Christopher Ferris885f3b92013-05-21 17:48:01 -0700106static inline const hdr_t* const_meta(const void* user) {
107 return reinterpret_cast<const hdr_t*>(user) - 1;
108}
109
Elliott Hughes1728b232014-05-14 10:02:03 -0700110// TODO: introduce a struct for this global state.
111// There are basically two lists here, the regular list and the backlog list.
112// We should be able to remove the duplication.
113static unsigned g_allocated_block_count;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700114static hdr_t* tail;
115static hdr_t* head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700116static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
117
118static unsigned backlog_num;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700119static hdr_t* backlog_tail;
120static hdr_t* backlog_head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700121static pthread_mutex_t backlog_lock = PTHREAD_MUTEX_INITIALIZER;
122
Christopher Ferris885f3b92013-05-21 17:48:01 -0700123static inline void init_front_guard(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700124 memset(hdr->front_guard, FRONT_GUARD, FRONT_GUARD_LEN);
125}
126
Christopher Ferris885f3b92013-05-21 17:48:01 -0700127static inline bool is_front_guard_valid(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700128 for (size_t i = 0; i < FRONT_GUARD_LEN; i++) {
129 if (hdr->front_guard[i] != FRONT_GUARD) {
Elliott Hughesef0696d2013-10-08 16:16:01 -0700130 return false;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700131 }
132 }
Elliott Hughesef0696d2013-10-08 16:16:01 -0700133 return true;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700134}
135
Christopher Ferris885f3b92013-05-21 17:48:01 -0700136static inline void init_rear_guard(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700137 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700138 memset(ftr->rear_guard, REAR_GUARD, REAR_GUARD_LEN);
139}
140
Christopher Ferris885f3b92013-05-21 17:48:01 -0700141static inline bool is_rear_guard_valid(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700142 unsigned i;
143 int valid = 1;
144 int first_mismatch = -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700145 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700146 for (i = 0; i < REAR_GUARD_LEN; i++) {
147 if (ftr->rear_guard[i] != REAR_GUARD) {
148 if (first_mismatch < 0)
149 first_mismatch = i;
150 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700151 } else if (first_mismatch >= 0) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700152 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
153 first_mismatch = -1;
154 }
155 }
156
157 if (first_mismatch >= 0)
158 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
159 return valid;
160}
161
Christopher Ferris885f3b92013-05-21 17:48:01 -0700162static inline void add_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700163 hdr->prev = NULL;
164 hdr->next = *head;
165 if (*head)
166 (*head)->prev = hdr;
167 else
168 *tail = hdr;
169 *head = hdr;
170}
171
Christopher Ferris885f3b92013-05-21 17:48:01 -0700172static inline int del_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700173 if (hdr->prev) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700174 hdr->prev->next = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700175 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700176 *head = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700177 }
178 if (hdr->next) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700179 hdr->next->prev = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700180 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700181 *tail = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700182 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700183 return 0;
184}
185
Christopher Ferris885f3b92013-05-21 17:48:01 -0700186static inline void add(hdr_t* hdr, size_t size) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700187 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700188 hdr->tag = ALLOCATION_TAG;
189 hdr->size = size;
190 init_front_guard(hdr);
191 init_rear_guard(hdr);
Elliott Hughes1728b232014-05-14 10:02:03 -0700192 ++g_allocated_block_count;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700193 add_locked(hdr, &tail, &head);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700194}
195
Christopher Ferris885f3b92013-05-21 17:48:01 -0700196static inline int del(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700197 if (hdr->tag != ALLOCATION_TAG) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700198 return -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700199 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700200
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700201 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700202 del_locked(hdr, &tail, &head);
Elliott Hughes1728b232014-05-14 10:02:03 -0700203 --g_allocated_block_count;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700204 return 0;
205}
206
Christopher Ferris885f3b92013-05-21 17:48:01 -0700207static inline void poison(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700208 memset(user(hdr), FREE_POISON, hdr->size);
209}
210
Elliott Hughesef0696d2013-10-08 16:16:01 -0700211static bool was_used_after_free(hdr_t* hdr) {
212 const uint8_t* data = reinterpret_cast<const uint8_t*>(user(hdr));
213 for (size_t i = 0; i < hdr->size; i++) {
214 if (data[i] != FREE_POISON) {
215 return true;
216 }
217 }
218 return false;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700219}
220
221/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700222static inline int check_guards(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700223 *safe = 1;
224 if (!is_front_guard_valid(hdr)) {
225 if (hdr->front_guard[0] == FRONT_GUARD) {
226 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED FRONT GUARD\n",
227 user(hdr), hdr->size);
228 } else {
229 log_message("+++ ALLOCATION %p HAS A CORRUPTED FRONT GUARD "\
230 "(NOT DUMPING STACKTRACE)\n", user(hdr));
231 /* Allocation header is probably corrupt, do not print stack trace */
232 *safe = 0;
233 }
234 return 0;
235 }
236
237 if (!is_rear_guard_valid(hdr)) {
238 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED REAR GUARD\n",
239 user(hdr), hdr->size);
240 return 0;
241 }
242
243 return 1;
244}
245
246/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700247static inline int check_allocation_locked(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700248 int valid = 1;
249 *safe = 1;
250
251 if (hdr->tag != ALLOCATION_TAG && hdr->tag != BACKLOG_TAG) {
252 log_message("+++ ALLOCATION %p HAS INVALID TAG %08x (NOT DUMPING STACKTRACE)\n",
253 user(hdr), hdr->tag);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700254 // Allocation header is probably corrupt, do not dequeue or dump stack
255 // trace.
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700256 *safe = 0;
257 return 0;
258 }
259
260 if (hdr->tag == BACKLOG_TAG && was_used_after_free(hdr)) {
261 log_message("+++ ALLOCATION %p SIZE %d WAS USED AFTER BEING FREED\n",
262 user(hdr), hdr->size);
263 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700264 /* check the guards to see if it's safe to dump a stack trace */
265 check_guards(hdr, safe);
266 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700267 valid = check_guards(hdr, safe);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700268 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700269
270 if (!valid && *safe) {
271 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
272 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800273 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700274 if (hdr->tag == BACKLOG_TAG) {
275 log_message("+++ ALLOCATION %p SIZE %d FREED HERE:\n",
276 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800277 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700278 }
279 }
280
281 return valid;
282}
283
Christopher Ferris885f3b92013-05-21 17:48:01 -0700284static inline int del_and_check_locked(hdr_t* hdr,
285 hdr_t** tail, hdr_t** head, unsigned* cnt,
286 int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700287 int valid = check_allocation_locked(hdr, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700288 if (safe) {
289 (*cnt)--;
290 del_locked(hdr, tail, head);
291 }
292 return valid;
293}
294
Christopher Ferris885f3b92013-05-21 17:48:01 -0700295static inline void del_from_backlog_locked(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700296 int safe;
297 del_and_check_locked(hdr,
298 &backlog_tail, &backlog_head, &backlog_num,
299 &safe);
300 hdr->tag = 0; /* clear the tag */
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700301}
302
Christopher Ferris885f3b92013-05-21 17:48:01 -0700303static inline void del_from_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700304 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700305 del_from_backlog_locked(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700306}
307
Christopher Ferris885f3b92013-05-21 17:48:01 -0700308static inline int del_leak(hdr_t* hdr, int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700309 ScopedPthreadMutexLocker locker(&lock);
Elliott Hughes1728b232014-05-14 10:02:03 -0700310 return del_and_check_locked(hdr, &tail, &head, &g_allocated_block_count, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700311}
312
Christopher Ferris885f3b92013-05-21 17:48:01 -0700313static inline void add_to_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700314 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700315 hdr->tag = BACKLOG_TAG;
316 backlog_num++;
317 add_locked(hdr, &backlog_tail, &backlog_head);
318 poison(hdr);
319 /* If we've exceeded the maximum backlog, clear it up */
Elliott Hughes1728b232014-05-14 10:02:03 -0700320 while (backlog_num > g_malloc_debug_backlog) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700321 hdr_t* gone = backlog_tail;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700322 del_from_backlog_locked(gone);
Christopher Ferris72bbd422014-05-08 11:14:03 -0700323 Malloc(free)(gone->base);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700324 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700325}
326
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700327extern "C" void* chk_malloc(size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700328// log_message("%s: %s\n", __FILE__, __FUNCTION__);
329
Christopher Ferris72bbd422014-05-08 11:14:03 -0700330 hdr_t* hdr = static_cast<hdr_t*>(Malloc(malloc)(sizeof(hdr_t) + size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700331 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700332 hdr->base = hdr;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700333 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
334 add(hdr, size);
335 return user(hdr);
336 }
337 return NULL;
338}
339
Christopher Ferris885f3b92013-05-21 17:48:01 -0700340extern "C" void* chk_memalign(size_t alignment, size_t bytes) {
341 if (alignment <= MALLOC_ALIGNMENT) {
342 return chk_malloc(bytes);
343 }
344
345 // Make the alignment a power of two.
346 if (alignment & (alignment-1)) {
347 alignment = 1L << (31 - __builtin_clz(alignment));
348 }
349
350 // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
351 // we will align by at least MALLOC_ALIGNMENT bytes
352 // and at most alignment-MALLOC_ALIGNMENT bytes
353 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
354 if (size < bytes) { // Overflow.
355 return NULL;
356 }
357
Christopher Ferris72bbd422014-05-08 11:14:03 -0700358 void* base = Malloc(malloc)(sizeof(hdr_t) + size + sizeof(ftr_t));
Christopher Ferris885f3b92013-05-21 17:48:01 -0700359 if (base != NULL) {
360 // Check that the actual pointer that will be returned is aligned
361 // properly.
362 uintptr_t ptr = reinterpret_cast<uintptr_t>(user(reinterpret_cast<hdr_t*>(base)));
363 if ((ptr % alignment) != 0) {
364 // Align the pointer.
365 ptr += ((-ptr) % alignment);
366 }
367
368 hdr_t* hdr = meta(reinterpret_cast<void*>(ptr));
369 hdr->base = base;
370 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
371 add(hdr, bytes);
372 return user(hdr);
373 }
374 return base;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700375}
376
Christopher Ferris885f3b92013-05-21 17:48:01 -0700377extern "C" void chk_free(void* ptr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700378// log_message("%s: %s\n", __FILE__, __FUNCTION__);
379
380 if (!ptr) /* ignore free(NULL) */
381 return;
382
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("+++ ALLOCATION %p SIZE %d BYTES MULTIPLY FREED!\n",
390 user(hdr), 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 FREED HERE:\n",
399 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800400 log_backtrace(bt, depth);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700401 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700402 log_message("+++ ALLOCATION %p IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
403 user(hdr));
Elliott Hughes35b621c2013-01-28 16:27:36 -0800404 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700405 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700406 } else {
Elliott Hughes35b621c2013-01-28 16:27:36 -0800407 hdr->freed_bt_depth = get_backtrace(hdr->freed_bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700408 add_to_backlog(hdr);
409 }
410}
411
Christopher Ferris885f3b92013-05-21 17:48:01 -0700412extern "C" void* chk_realloc(void* ptr, size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700413// log_message("%s: %s\n", __FILE__, __FUNCTION__);
414
Elliott Hughese7e274b2012-10-12 17:05:05 -0700415 if (!ptr) {
416 return chk_malloc(size);
417 }
418
419#ifdef REALLOC_ZERO_BYTES_FREE
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700420 if (!size) {
421 chk_free(ptr);
422 return NULL;
423 }
Elliott Hughese7e274b2012-10-12 17:05:05 -0700424#endif
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700425
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700426 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700427
428 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800429 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800430 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700431 if (hdr->tag == BACKLOG_TAG) {
432 log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n",
433 user(hdr), size, hdr->size);
434 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
435 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800436 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700437 /* hdr->freed_bt_depth should be nonzero here */
438 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
439 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800440 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700441 log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n",
442 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800443 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700444
445 /* We take the memory out of the backlog and fall through so the
446 * reallocation below succeeds. Since we didn't really free it, we
447 * can default to this behavior.
448 */
449 del_from_backlog(hdr);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700450 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700451 log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
452 user(hdr), size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800453 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700454 // just get a whole new allocation and leak the old one
Christopher Ferris72bbd422014-05-08 11:14:03 -0700455 return Malloc(realloc)(0, size);
456 // return realloc(user(hdr), size); // assuming it was allocated externally
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700457 }
458 }
459
Christopher Ferris885f3b92013-05-21 17:48:01 -0700460 if (hdr->base != hdr) {
461 // An allocation from memalign, so create another allocation and
462 // copy the data out.
Christopher Ferris72bbd422014-05-08 11:14:03 -0700463 void* newMem = Malloc(malloc)(sizeof(hdr_t) + size + sizeof(ftr_t));
Christopher Ferris885f3b92013-05-21 17:48:01 -0700464 if (newMem) {
465 memcpy(newMem, hdr, sizeof(hdr_t) + hdr->size);
Christopher Ferris72bbd422014-05-08 11:14:03 -0700466 Malloc(free)(hdr->base);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700467 hdr = static_cast<hdr_t*>(newMem);
468 } else {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700469 Malloc(free)(hdr->base);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700470 hdr = NULL;
471 }
472 } else {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700473 hdr = static_cast<hdr_t*>(Malloc(realloc)(hdr, sizeof(hdr_t) + size + sizeof(ftr_t)));
Christopher Ferris885f3b92013-05-21 17:48:01 -0700474 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700475 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700476 hdr->base = hdr;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700477 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
478 add(hdr, size);
479 return user(hdr);
480 }
481
482 return NULL;
483}
484
Christopher Ferris885f3b92013-05-21 17:48:01 -0700485extern "C" void* chk_calloc(int nmemb, size_t size) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700486// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700487 size_t total_size = nmemb * size;
Christopher Ferris72bbd422014-05-08 11:14:03 -0700488 hdr_t* hdr = static_cast<hdr_t*>(Malloc(calloc)(1, sizeof(hdr_t) + total_size + sizeof(ftr_t)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700489 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700490 hdr->base = hdr;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800491 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700492 add(hdr, total_size);
493 return user(hdr);
494 }
495 return NULL;
496}
497
Christopher Ferris885f3b92013-05-21 17:48:01 -0700498extern "C" size_t chk_malloc_usable_size(const void* ptr) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700499 // malloc_usable_size returns 0 for NULL and unknown blocks.
Christopher Ferris885f3b92013-05-21 17:48:01 -0700500 if (ptr == NULL)
501 return 0;
502
503 const hdr_t* hdr = const_meta(ptr);
504
505 // The sentinel tail is written just after the request block bytes
506 // so there is no extra room we can report here.
507 return hdr->size;
508}
509
Elliott Hughes9c818922013-02-01 17:07:40 -0800510static void ReportMemoryLeaks() {
511 // We only track leaks at level 10.
Elliott Hughes1728b232014-05-14 10:02:03 -0700512 if (g_malloc_debug_level != 10) {
Elliott Hughes9c818922013-02-01 17:07:40 -0800513 return;
514 }
515
Elliott Hughes239e7a02013-01-25 17:13:45 -0800516 // Use /proc/self/exe link to obtain the program name for logging
517 // purposes. If it's not available, we set it to "<unknown>".
518 char exe[PATH_MAX];
519 int count;
520 if ((count = readlink("/proc/self/exe", exe, sizeof(exe) - 1)) == -1) {
521 strlcpy(exe, "<unknown>", sizeof(exe));
522 } else {
523 exe[count] = '\0';
524 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700525
Elliott Hughes1728b232014-05-14 10:02:03 -0700526 if (g_allocated_block_count == 0) {
Elliott Hughes1d12d572013-01-30 11:38:26 -0800527 log_message("+++ %s did not leak", exe);
528 return;
529 }
530
Elliott Hughes239e7a02013-01-25 17:13:45 -0800531 size_t index = 1;
Elliott Hughes1728b232014-05-14 10:02:03 -0700532 const size_t total = g_allocated_block_count;
Elliott Hughes239e7a02013-01-25 17:13:45 -0800533 while (head != NULL) {
534 int safe;
535 hdr_t* block = head;
536 log_message("+++ %s leaked block of size %d at %p (leak %d of %d)",
537 exe, block->size, user(block), index++, total);
538 if (del_leak(block, &safe)) {
539 /* safe == 1, because the allocation is valid */
Elliott Hughes35b621c2013-01-28 16:27:36 -0800540 log_backtrace(block->bt, block->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700541 }
Elliott Hughes239e7a02013-01-25 17:13:45 -0800542 }
543
544 while (backlog_head != NULL) {
545 del_from_backlog(backlog_tail);
546 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700547}
548
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700549extern "C" int malloc_debug_initialize() {
Elliott Hughes35b621c2013-01-28 16:27:36 -0800550 backtrace_startup();
Elliott Hughes1e980b62013-01-17 18:36:06 -0800551 return 0;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700552}
553
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700554extern "C" void malloc_debug_finalize() {
Elliott Hughes9c818922013-02-01 17:07:40 -0800555 ReportMemoryLeaks();
Elliott Hughes35b621c2013-01-28 16:27:36 -0800556 backtrace_shutdown();
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700557}