blob: faf61bf5c10b8a9d30ee86d7be6a98b6119339c0 [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
Elliott Hughes1e980b62013-01-17 18:36:06 -080054#define MAX_BACKTRACE_DEPTH 16
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070055#define ALLOCATION_TAG 0x1ee7d00d
56#define BACKLOG_TAG 0xbabecafe
57#define FREE_POISON 0xa5
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070058#define FRONT_GUARD 0xaa
59#define FRONT_GUARD_LEN (1<<5)
60#define REAR_GUARD 0xbb
61#define REAR_GUARD_LEN (1<<5)
62
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070063static void log_message(const char* format, ...) {
Elliott Hughes1e980b62013-01-17 18:36:06 -080064 va_list args;
65 va_start(args, format);
66 __libc_format_log_va_list(ANDROID_LOG_ERROR, "libc", format, args);
67 va_end(args);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070068}
69
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070070struct hdr_t {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070071 uint32_t tag;
Christopher Ferris72bbd422014-05-08 11:14:03 -070072 void* base; // Always points to the memory allocated using malloc.
Christopher Ferris885f3b92013-05-21 17:48:01 -070073 // For memory allocated in chk_memalign, this value will
74 // not be the same as the location of the start of this
75 // structure.
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070076 hdr_t* prev;
77 hdr_t* next;
Elliott Hughes239e7a02013-01-25 17:13:45 -080078 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070079 int bt_depth;
Elliott Hughes239e7a02013-01-25 17:13:45 -080080 uintptr_t freed_bt[MAX_BACKTRACE_DEPTH];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070081 int freed_bt_depth;
82 size_t size;
Elliott Hughesef0696d2013-10-08 16:16:01 -070083 uint8_t front_guard[FRONT_GUARD_LEN];
Christopher Ferris885f3b92013-05-21 17:48:01 -070084} __attribute__((packed, aligned(MALLOC_ALIGNMENT)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070085
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070086struct ftr_t {
Elliott Hughesef0696d2013-10-08 16:16:01 -070087 uint8_t rear_guard[REAR_GUARD_LEN];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070088} __attribute__((packed));
89
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070090static inline ftr_t* to_ftr(hdr_t* hdr) {
91 return reinterpret_cast<ftr_t*>(reinterpret_cast<char*>(hdr + 1) + hdr->size);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070092}
93
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070094static inline void* user(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070095 return hdr + 1;
96}
97
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070098static inline hdr_t* meta(void* user) {
99 return reinterpret_cast<hdr_t*>(user) - 1;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700100}
101
Christopher Ferris885f3b92013-05-21 17:48:01 -0700102static inline const hdr_t* const_meta(const void* user) {
103 return reinterpret_cast<const hdr_t*>(user) - 1;
104}
105
Elliott Hughes1728b232014-05-14 10:02:03 -0700106// TODO: introduce a struct for this global state.
107// There are basically two lists here, the regular list and the backlog list.
108// We should be able to remove the duplication.
109static unsigned g_allocated_block_count;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700110static hdr_t* tail;
111static hdr_t* head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700112static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
113
114static unsigned backlog_num;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700115static hdr_t* backlog_tail;
116static hdr_t* backlog_head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700117static pthread_mutex_t backlog_lock = PTHREAD_MUTEX_INITIALIZER;
118
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700119// This variable is set to the value of property libc.debug.malloc.backlog.
120// It determines the size of the backlog we use to detect multiple frees.
121static unsigned g_malloc_debug_backlog = 100;
122
123__LIBC_HIDDEN__ HashTable* g_hash_table;
124
Christopher Ferris885f3b92013-05-21 17:48:01 -0700125static inline void init_front_guard(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700126 memset(hdr->front_guard, FRONT_GUARD, FRONT_GUARD_LEN);
127}
128
Christopher Ferris885f3b92013-05-21 17:48:01 -0700129static inline bool is_front_guard_valid(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700130 for (size_t i = 0; i < FRONT_GUARD_LEN; i++) {
131 if (hdr->front_guard[i] != FRONT_GUARD) {
Elliott Hughesef0696d2013-10-08 16:16:01 -0700132 return false;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700133 }
134 }
Elliott Hughesef0696d2013-10-08 16:16:01 -0700135 return true;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700136}
137
Christopher Ferris885f3b92013-05-21 17:48:01 -0700138static inline void init_rear_guard(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700139 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700140 memset(ftr->rear_guard, REAR_GUARD, REAR_GUARD_LEN);
141}
142
Christopher Ferris885f3b92013-05-21 17:48:01 -0700143static inline bool is_rear_guard_valid(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700144 unsigned i;
145 int valid = 1;
146 int first_mismatch = -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700147 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700148 for (i = 0; i < REAR_GUARD_LEN; i++) {
149 if (ftr->rear_guard[i] != REAR_GUARD) {
150 if (first_mismatch < 0)
151 first_mismatch = i;
152 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700153 } else if (first_mismatch >= 0) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700154 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
155 first_mismatch = -1;
156 }
157 }
158
159 if (first_mismatch >= 0)
160 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
161 return valid;
162}
163
Christopher Ferris885f3b92013-05-21 17:48:01 -0700164static inline void add_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700165 hdr->prev = NULL;
166 hdr->next = *head;
167 if (*head)
168 (*head)->prev = hdr;
169 else
170 *tail = hdr;
171 *head = hdr;
172}
173
Christopher Ferris885f3b92013-05-21 17:48:01 -0700174static inline int del_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700175 if (hdr->prev) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700176 hdr->prev->next = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700177 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700178 *head = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700179 }
180 if (hdr->next) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700181 hdr->next->prev = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700182 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700183 *tail = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700184 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700185 return 0;
186}
187
Christopher Ferris885f3b92013-05-21 17:48:01 -0700188static inline void add(hdr_t* hdr, size_t size) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700189 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700190 hdr->tag = ALLOCATION_TAG;
191 hdr->size = size;
192 init_front_guard(hdr);
193 init_rear_guard(hdr);
Elliott Hughes1728b232014-05-14 10:02:03 -0700194 ++g_allocated_block_count;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700195 add_locked(hdr, &tail, &head);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700196}
197
Christopher Ferris885f3b92013-05-21 17:48:01 -0700198static inline int del(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700199 if (hdr->tag != ALLOCATION_TAG) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700200 return -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700201 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700202
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700203 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700204 del_locked(hdr, &tail, &head);
Elliott Hughes1728b232014-05-14 10:02:03 -0700205 --g_allocated_block_count;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700206 return 0;
207}
208
Christopher Ferris885f3b92013-05-21 17:48:01 -0700209static inline void poison(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700210 memset(user(hdr), FREE_POISON, hdr->size);
211}
212
Elliott Hughesef0696d2013-10-08 16:16:01 -0700213static bool was_used_after_free(hdr_t* hdr) {
214 const uint8_t* data = reinterpret_cast<const uint8_t*>(user(hdr));
215 for (size_t i = 0; i < hdr->size; i++) {
216 if (data[i] != FREE_POISON) {
217 return true;
218 }
219 }
220 return false;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700221}
222
223/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700224static inline int check_guards(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700225 *safe = 1;
226 if (!is_front_guard_valid(hdr)) {
227 if (hdr->front_guard[0] == FRONT_GUARD) {
228 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED FRONT GUARD\n",
229 user(hdr), hdr->size);
230 } else {
231 log_message("+++ ALLOCATION %p HAS A CORRUPTED FRONT GUARD "\
232 "(NOT DUMPING STACKTRACE)\n", user(hdr));
233 /* Allocation header is probably corrupt, do not print stack trace */
234 *safe = 0;
235 }
236 return 0;
237 }
238
239 if (!is_rear_guard_valid(hdr)) {
240 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED REAR GUARD\n",
241 user(hdr), hdr->size);
242 return 0;
243 }
244
245 return 1;
246}
247
248/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700249static inline int check_allocation_locked(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700250 int valid = 1;
251 *safe = 1;
252
253 if (hdr->tag != ALLOCATION_TAG && hdr->tag != BACKLOG_TAG) {
254 log_message("+++ ALLOCATION %p HAS INVALID TAG %08x (NOT DUMPING STACKTRACE)\n",
255 user(hdr), hdr->tag);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700256 // Allocation header is probably corrupt, do not dequeue or dump stack
257 // trace.
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700258 *safe = 0;
259 return 0;
260 }
261
262 if (hdr->tag == BACKLOG_TAG && was_used_after_free(hdr)) {
263 log_message("+++ ALLOCATION %p SIZE %d WAS USED AFTER BEING FREED\n",
264 user(hdr), hdr->size);
265 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700266 /* check the guards to see if it's safe to dump a stack trace */
267 check_guards(hdr, safe);
268 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700269 valid = check_guards(hdr, safe);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700270 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700271
272 if (!valid && *safe) {
273 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
274 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800275 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700276 if (hdr->tag == BACKLOG_TAG) {
277 log_message("+++ ALLOCATION %p SIZE %d FREED HERE:\n",
278 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800279 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700280 }
281 }
282
283 return valid;
284}
285
Christopher Ferris885f3b92013-05-21 17:48:01 -0700286static inline int del_and_check_locked(hdr_t* hdr,
287 hdr_t** tail, hdr_t** head, unsigned* cnt,
288 int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700289 int valid = check_allocation_locked(hdr, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700290 if (safe) {
291 (*cnt)--;
292 del_locked(hdr, tail, head);
293 }
294 return valid;
295}
296
Christopher Ferris885f3b92013-05-21 17:48:01 -0700297static inline void del_from_backlog_locked(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700298 int safe;
299 del_and_check_locked(hdr,
300 &backlog_tail, &backlog_head, &backlog_num,
301 &safe);
302 hdr->tag = 0; /* clear the tag */
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700303}
304
Christopher Ferris885f3b92013-05-21 17:48:01 -0700305static inline void del_from_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700306 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700307 del_from_backlog_locked(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700308}
309
Christopher Ferris885f3b92013-05-21 17:48:01 -0700310static inline int del_leak(hdr_t* hdr, int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700311 ScopedPthreadMutexLocker locker(&lock);
Elliott Hughes1728b232014-05-14 10:02:03 -0700312 return del_and_check_locked(hdr, &tail, &head, &g_allocated_block_count, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700313}
314
Christopher Ferris885f3b92013-05-21 17:48:01 -0700315static inline void add_to_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700316 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700317 hdr->tag = BACKLOG_TAG;
318 backlog_num++;
319 add_locked(hdr, &backlog_tail, &backlog_head);
320 poison(hdr);
321 /* If we've exceeded the maximum backlog, clear it up */
Elliott Hughes1728b232014-05-14 10:02:03 -0700322 while (backlog_num > g_malloc_debug_backlog) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700323 hdr_t* gone = backlog_tail;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700324 del_from_backlog_locked(gone);
Christopher Ferris72bbd422014-05-08 11:14:03 -0700325 Malloc(free)(gone->base);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700326 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700327}
328
Christopher Ferrisa4037802014-06-09 19:14:11 -0700329extern "C" void* chk_malloc(size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700330// log_message("%s: %s\n", __FILE__, __FUNCTION__);
331
Christopher Ferrisa4037802014-06-09 19:14:11 -0700332 size_t size = sizeof(hdr_t) + bytes + sizeof(ftr_t);
333 if (size < bytes) { // Overflow
334 errno = ENOMEM;
335 return NULL;
336 }
337 hdr_t* hdr = static_cast<hdr_t*>(Malloc(malloc)(size));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700338 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700339 hdr->base = hdr;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700340 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700341 add(hdr, bytes);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700342 return user(hdr);
343 }
344 return NULL;
345}
346
Christopher Ferris885f3b92013-05-21 17:48:01 -0700347extern "C" void* chk_memalign(size_t alignment, size_t bytes) {
348 if (alignment <= MALLOC_ALIGNMENT) {
349 return chk_malloc(bytes);
350 }
351
352 // Make the alignment a power of two.
353 if (alignment & (alignment-1)) {
354 alignment = 1L << (31 - __builtin_clz(alignment));
355 }
356
357 // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
358 // we will align by at least MALLOC_ALIGNMENT bytes
359 // and at most alignment-MALLOC_ALIGNMENT bytes
360 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
361 if (size < bytes) { // Overflow.
362 return NULL;
363 }
364
Christopher Ferris72bbd422014-05-08 11:14:03 -0700365 void* base = Malloc(malloc)(sizeof(hdr_t) + size + sizeof(ftr_t));
Christopher Ferris885f3b92013-05-21 17:48:01 -0700366 if (base != NULL) {
367 // Check that the actual pointer that will be returned is aligned
368 // properly.
369 uintptr_t ptr = reinterpret_cast<uintptr_t>(user(reinterpret_cast<hdr_t*>(base)));
370 if ((ptr % alignment) != 0) {
371 // Align the pointer.
372 ptr += ((-ptr) % alignment);
373 }
374
375 hdr_t* hdr = meta(reinterpret_cast<void*>(ptr));
376 hdr->base = base;
377 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
378 add(hdr, bytes);
379 return user(hdr);
380 }
381 return base;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700382}
383
Christopher Ferris885f3b92013-05-21 17:48:01 -0700384extern "C" void chk_free(void* ptr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700385// log_message("%s: %s\n", __FILE__, __FUNCTION__);
386
387 if (!ptr) /* ignore free(NULL) */
388 return;
389
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700390 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700391
392 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800393 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800394 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700395 if (hdr->tag == BACKLOG_TAG) {
396 log_message("+++ ALLOCATION %p SIZE %d BYTES MULTIPLY FREED!\n",
397 user(hdr), hdr->size);
398 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
399 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800400 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700401 /* hdr->freed_bt_depth should be nonzero here */
402 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
403 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800404 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700405 log_message("+++ ALLOCATION %p SIZE %d NOW BEING FREED HERE:\n",
406 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800407 log_backtrace(bt, depth);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700408 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700409 log_message("+++ ALLOCATION %p IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
410 user(hdr));
Elliott Hughes35b621c2013-01-28 16:27:36 -0800411 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700412 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700413 } else {
Elliott Hughes35b621c2013-01-28 16:27:36 -0800414 hdr->freed_bt_depth = get_backtrace(hdr->freed_bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700415 add_to_backlog(hdr);
416 }
417}
418
Christopher Ferrisa4037802014-06-09 19:14:11 -0700419extern "C" void* chk_realloc(void* ptr, size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700420// log_message("%s: %s\n", __FILE__, __FUNCTION__);
421
Elliott Hughese7e274b2012-10-12 17:05:05 -0700422 if (!ptr) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700423 return chk_malloc(bytes);
Elliott Hughese7e274b2012-10-12 17:05:05 -0700424 }
425
426#ifdef REALLOC_ZERO_BYTES_FREE
Christopher Ferrisa4037802014-06-09 19:14:11 -0700427 if (!bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700428 chk_free(ptr);
429 return NULL;
430 }
Elliott Hughese7e274b2012-10-12 17:05:05 -0700431#endif
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700432
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700433 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700434
435 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800436 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800437 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700438 if (hdr->tag == BACKLOG_TAG) {
439 log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n",
Christopher Ferrisa4037802014-06-09 19:14:11 -0700440 user(hdr), bytes, hdr->size);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700441 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
442 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800443 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700444 /* hdr->freed_bt_depth should be nonzero here */
445 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
446 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800447 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700448 log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n",
449 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800450 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700451
452 /* We take the memory out of the backlog and fall through so the
453 * reallocation below succeeds. Since we didn't really free it, we
454 * can default to this behavior.
455 */
456 del_from_backlog(hdr);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700457 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700458 log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
Christopher Ferrisa4037802014-06-09 19:14:11 -0700459 user(hdr), bytes);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800460 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700461 // just get a whole new allocation and leak the old one
Christopher Ferrisa4037802014-06-09 19:14:11 -0700462 return Malloc(realloc)(0, bytes);
463 // return realloc(user(hdr), bytes); // assuming it was allocated externally
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700464 }
465 }
466
Christopher Ferrisa4037802014-06-09 19:14:11 -0700467 size_t size = sizeof(hdr_t) + bytes + sizeof(ftr_t);
468 if (size < bytes) { // Overflow
469 errno = ENOMEM;
470 return NULL;
471 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700472 if (hdr->base != hdr) {
473 // An allocation from memalign, so create another allocation and
474 // copy the data out.
Christopher Ferrisa4037802014-06-09 19:14:11 -0700475 void* newMem = Malloc(malloc)(size);
476 if (newMem == NULL) {
477 return NULL;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700478 }
Christopher Ferrisa4037802014-06-09 19:14:11 -0700479 memcpy(newMem, hdr, sizeof(hdr_t) + hdr->size);
480 Malloc(free)(hdr->base);
481 hdr = static_cast<hdr_t*>(newMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700482 } else {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700483 hdr = static_cast<hdr_t*>(Malloc(realloc)(hdr, size));
Christopher Ferris885f3b92013-05-21 17:48:01 -0700484 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700485 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700486 hdr->base = hdr;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700487 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700488 add(hdr, bytes);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700489 return user(hdr);
490 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700491 return NULL;
492}
493
Christopher Ferrisa4037802014-06-09 19:14:11 -0700494extern "C" void* chk_calloc(size_t nmemb, size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700495// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700496 size_t total_bytes = nmemb * bytes;
497 size_t size = sizeof(hdr_t) + total_bytes + sizeof(ftr_t);
498 if (size < total_bytes || (nmemb && SIZE_MAX / nmemb < bytes)) { // Overflow
499 errno = ENOMEM;
500 return NULL;
501 }
502 hdr_t* hdr = static_cast<hdr_t*>(Malloc(calloc)(1, size));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700503 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700504 hdr->base = hdr;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800505 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700506 add(hdr, total_bytes);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700507 return user(hdr);
508 }
509 return NULL;
510}
511
Christopher Ferris885f3b92013-05-21 17:48:01 -0700512extern "C" size_t chk_malloc_usable_size(const void* ptr) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700513 // malloc_usable_size returns 0 for NULL and unknown blocks.
Christopher Ferris885f3b92013-05-21 17:48:01 -0700514 if (ptr == NULL)
515 return 0;
516
517 const hdr_t* hdr = const_meta(ptr);
518
519 // The sentinel tail is written just after the request block bytes
520 // so there is no extra room we can report here.
521 return hdr->size;
522}
523
Christopher Ferrisa4037802014-06-09 19:14:11 -0700524extern "C" struct mallinfo chk_mallinfo() {
525 return Malloc(mallinfo)();
526}
527
528extern "C" int chk_posix_memalign(void** memptr, size_t alignment, size_t size) {
529 if ((alignment & (alignment - 1)) != 0) {
530 return EINVAL;
531 }
532 int saved_errno = errno;
533 *memptr = chk_memalign(alignment, size);
534 errno = saved_errno;
535 return (*memptr != NULL) ? 0 : ENOMEM;
536}
537
538extern "C" void* chk_pvalloc(size_t bytes) {
539 size_t pagesize = sysconf(_SC_PAGESIZE);
540 size_t size = (bytes + pagesize - 1) & ~(pagesize - 1);
541 if (size < bytes) { // Overflow
542 return NULL;
543 }
544 return chk_memalign(pagesize, size);
545}
546
547extern "C" void* chk_valloc(size_t size) {
548 return chk_memalign(sysconf(_SC_PAGESIZE), size);
549}
550
Elliott Hughes9c818922013-02-01 17:07:40 -0800551static void ReportMemoryLeaks() {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800552 // Use /proc/self/exe link to obtain the program name for logging
553 // purposes. If it's not available, we set it to "<unknown>".
554 char exe[PATH_MAX];
555 int count;
556 if ((count = readlink("/proc/self/exe", exe, sizeof(exe) - 1)) == -1) {
557 strlcpy(exe, "<unknown>", sizeof(exe));
558 } else {
559 exe[count] = '\0';
560 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700561
Elliott Hughes1728b232014-05-14 10:02:03 -0700562 if (g_allocated_block_count == 0) {
Elliott Hughes1d12d572013-01-30 11:38:26 -0800563 log_message("+++ %s did not leak", exe);
564 return;
565 }
566
Elliott Hughes239e7a02013-01-25 17:13:45 -0800567 size_t index = 1;
Elliott Hughes1728b232014-05-14 10:02:03 -0700568 const size_t total = g_allocated_block_count;
Elliott Hughes239e7a02013-01-25 17:13:45 -0800569 while (head != NULL) {
570 int safe;
571 hdr_t* block = head;
572 log_message("+++ %s leaked block of size %d at %p (leak %d of %d)",
573 exe, block->size, user(block), index++, total);
574 if (del_leak(block, &safe)) {
575 /* safe == 1, because the allocation is valid */
Elliott Hughes35b621c2013-01-28 16:27:36 -0800576 log_backtrace(block->bt, block->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700577 }
Elliott Hughes239e7a02013-01-25 17:13:45 -0800578 }
579
580 while (backlog_head != NULL) {
581 del_from_backlog(backlog_tail);
582 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700583}
584
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700585extern "C" bool malloc_debug_initialize(HashTable* hash_table) {
586 g_hash_table = hash_table;
587
588 char debug_backlog[PROP_VALUE_MAX];
589 if (__system_property_get("libc.debug.malloc.backlog", debug_backlog)) {
590 g_malloc_debug_backlog = atoi(debug_backlog);
591 info_log("%s: setting backlog length to %d\n", getprogname(), g_malloc_debug_backlog);
592 }
593
Elliott Hughes35b621c2013-01-28 16:27:36 -0800594 backtrace_startup();
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700595 return true;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700596}
597
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700598extern "C" void malloc_debug_finalize(int malloc_debug_level) {
599 // We only track leaks at level 10.
600 if (malloc_debug_level == 10) {
601 ReportMemoryLeaks();
602 }
Elliott Hughes35b621c2013-01-28 16:27:36 -0800603 backtrace_shutdown();
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700604}