blob: e4e4c2eb08ac386f35c09bc9431378689febe665 [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>
Christopher Ferris03eebcb2014-06-13 13:57:51 -070041#include <sys/param.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070042#include <sys/socket.h>
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070043#include <sys/system_properties.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070044#include <sys/types.h>
45#include <time.h>
46#include <unistd.h>
47#include <unwind.h>
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070048
Elliott Hughes1e980b62013-01-17 18:36:06 -080049#include "debug_mapinfo.h"
50#include "debug_stacktrace.h"
Elliott Hughes3b297c42012-10-11 16:08:51 -070051#include "malloc_debug_common.h"
Christopher Ferris03eebcb2014-06-13 13:57:51 -070052#include "private/bionic_macros.h"
53#include "private/libc_logging.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070054#include "private/ScopedPthreadMutexLocker.h"
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070055
Elliott Hughes1e980b62013-01-17 18:36:06 -080056#define MAX_BACKTRACE_DEPTH 16
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070057#define ALLOCATION_TAG 0x1ee7d00d
58#define BACKLOG_TAG 0xbabecafe
59#define FREE_POISON 0xa5
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070060#define FRONT_GUARD 0xaa
61#define FRONT_GUARD_LEN (1<<5)
62#define REAR_GUARD 0xbb
63#define REAR_GUARD_LEN (1<<5)
64
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070065static void log_message(const char* format, ...) {
Elliott Hughes1e980b62013-01-17 18:36:06 -080066 va_list args;
67 va_start(args, format);
68 __libc_format_log_va_list(ANDROID_LOG_ERROR, "libc", format, args);
69 va_end(args);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070070}
71
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070072struct hdr_t {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070073 uint32_t tag;
Christopher Ferris72bbd422014-05-08 11:14:03 -070074 void* base; // Always points to the memory allocated using malloc.
Christopher Ferris885f3b92013-05-21 17:48:01 -070075 // For memory allocated in chk_memalign, this value will
76 // not be the same as the location of the start of this
77 // structure.
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070078 hdr_t* prev;
79 hdr_t* next;
Elliott Hughes239e7a02013-01-25 17:13:45 -080080 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070081 int bt_depth;
Elliott Hughes239e7a02013-01-25 17:13:45 -080082 uintptr_t freed_bt[MAX_BACKTRACE_DEPTH];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070083 int freed_bt_depth;
84 size_t size;
Elliott Hughesef0696d2013-10-08 16:16:01 -070085 uint8_t front_guard[FRONT_GUARD_LEN];
Christopher Ferris885f3b92013-05-21 17:48:01 -070086} __attribute__((packed, aligned(MALLOC_ALIGNMENT)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070087
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070088struct ftr_t {
Elliott Hughesef0696d2013-10-08 16:16:01 -070089 uint8_t rear_guard[REAR_GUARD_LEN];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070090} __attribute__((packed));
91
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070092static inline ftr_t* to_ftr(hdr_t* hdr) {
93 return reinterpret_cast<ftr_t*>(reinterpret_cast<char*>(hdr + 1) + hdr->size);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070094}
95
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070096static inline void* user(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070097 return hdr + 1;
98}
99
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700100static inline hdr_t* meta(void* user) {
101 return reinterpret_cast<hdr_t*>(user) - 1;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700102}
103
Christopher Ferris885f3b92013-05-21 17:48:01 -0700104static inline const hdr_t* const_meta(const void* user) {
105 return reinterpret_cast<const hdr_t*>(user) - 1;
106}
107
Elliott Hughes1728b232014-05-14 10:02:03 -0700108// TODO: introduce a struct for this global state.
109// There are basically two lists here, the regular list and the backlog list.
110// We should be able to remove the duplication.
111static unsigned g_allocated_block_count;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700112static hdr_t* tail;
113static hdr_t* head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700114static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
115
116static unsigned backlog_num;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700117static hdr_t* backlog_tail;
118static hdr_t* backlog_head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700119static pthread_mutex_t backlog_lock = PTHREAD_MUTEX_INITIALIZER;
120
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700121// This variable is set to the value of property libc.debug.malloc.backlog.
122// It determines the size of the backlog we use to detect multiple frees.
123static unsigned g_malloc_debug_backlog = 100;
124
125__LIBC_HIDDEN__ HashTable* g_hash_table;
126
Christopher Ferris885f3b92013-05-21 17:48:01 -0700127static inline void init_front_guard(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700128 memset(hdr->front_guard, FRONT_GUARD, FRONT_GUARD_LEN);
129}
130
Christopher Ferris885f3b92013-05-21 17:48:01 -0700131static inline bool is_front_guard_valid(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700132 for (size_t i = 0; i < FRONT_GUARD_LEN; i++) {
133 if (hdr->front_guard[i] != FRONT_GUARD) {
Elliott Hughesef0696d2013-10-08 16:16:01 -0700134 return false;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700135 }
136 }
Elliott Hughesef0696d2013-10-08 16:16:01 -0700137 return true;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700138}
139
Christopher Ferris885f3b92013-05-21 17:48:01 -0700140static inline void init_rear_guard(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700141 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700142 memset(ftr->rear_guard, REAR_GUARD, REAR_GUARD_LEN);
143}
144
Christopher Ferris885f3b92013-05-21 17:48:01 -0700145static inline bool is_rear_guard_valid(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700146 unsigned i;
147 int valid = 1;
148 int first_mismatch = -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700149 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700150 for (i = 0; i < REAR_GUARD_LEN; i++) {
151 if (ftr->rear_guard[i] != REAR_GUARD) {
152 if (first_mismatch < 0)
153 first_mismatch = i;
154 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700155 } else if (first_mismatch >= 0) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700156 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
157 first_mismatch = -1;
158 }
159 }
160
161 if (first_mismatch >= 0)
162 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
163 return valid;
164}
165
Christopher Ferris885f3b92013-05-21 17:48:01 -0700166static inline void add_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700167 hdr->prev = NULL;
168 hdr->next = *head;
169 if (*head)
170 (*head)->prev = hdr;
171 else
172 *tail = hdr;
173 *head = hdr;
174}
175
Christopher Ferris885f3b92013-05-21 17:48:01 -0700176static inline int del_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700177 if (hdr->prev) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700178 hdr->prev->next = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700179 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700180 *head = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700181 }
182 if (hdr->next) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700183 hdr->next->prev = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700184 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700185 *tail = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700186 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700187 return 0;
188}
189
Christopher Ferris885f3b92013-05-21 17:48:01 -0700190static inline void add(hdr_t* hdr, size_t size) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700191 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700192 hdr->tag = ALLOCATION_TAG;
193 hdr->size = size;
194 init_front_guard(hdr);
195 init_rear_guard(hdr);
Elliott Hughes1728b232014-05-14 10:02:03 -0700196 ++g_allocated_block_count;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700197 add_locked(hdr, &tail, &head);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700198}
199
Christopher Ferris885f3b92013-05-21 17:48:01 -0700200static inline int del(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700201 if (hdr->tag != ALLOCATION_TAG) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700202 return -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700203 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700204
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700205 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700206 del_locked(hdr, &tail, &head);
Elliott Hughes1728b232014-05-14 10:02:03 -0700207 --g_allocated_block_count;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700208 return 0;
209}
210
Christopher Ferris885f3b92013-05-21 17:48:01 -0700211static inline void poison(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700212 memset(user(hdr), FREE_POISON, hdr->size);
213}
214
Elliott Hughesef0696d2013-10-08 16:16:01 -0700215static bool was_used_after_free(hdr_t* hdr) {
216 const uint8_t* data = reinterpret_cast<const uint8_t*>(user(hdr));
217 for (size_t i = 0; i < hdr->size; i++) {
218 if (data[i] != FREE_POISON) {
219 return true;
220 }
221 }
222 return false;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700223}
224
225/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700226static inline int check_guards(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700227 *safe = 1;
228 if (!is_front_guard_valid(hdr)) {
229 if (hdr->front_guard[0] == FRONT_GUARD) {
230 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED FRONT GUARD\n",
231 user(hdr), hdr->size);
232 } else {
233 log_message("+++ ALLOCATION %p HAS A CORRUPTED FRONT GUARD "\
234 "(NOT DUMPING STACKTRACE)\n", user(hdr));
235 /* Allocation header is probably corrupt, do not print stack trace */
236 *safe = 0;
237 }
238 return 0;
239 }
240
241 if (!is_rear_guard_valid(hdr)) {
242 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED REAR GUARD\n",
243 user(hdr), hdr->size);
244 return 0;
245 }
246
247 return 1;
248}
249
250/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700251static inline int check_allocation_locked(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700252 int valid = 1;
253 *safe = 1;
254
255 if (hdr->tag != ALLOCATION_TAG && hdr->tag != BACKLOG_TAG) {
256 log_message("+++ ALLOCATION %p HAS INVALID TAG %08x (NOT DUMPING STACKTRACE)\n",
257 user(hdr), hdr->tag);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700258 // Allocation header is probably corrupt, do not dequeue or dump stack
259 // trace.
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700260 *safe = 0;
261 return 0;
262 }
263
264 if (hdr->tag == BACKLOG_TAG && was_used_after_free(hdr)) {
265 log_message("+++ ALLOCATION %p SIZE %d WAS USED AFTER BEING FREED\n",
266 user(hdr), hdr->size);
267 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700268 /* check the guards to see if it's safe to dump a stack trace */
269 check_guards(hdr, safe);
270 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700271 valid = check_guards(hdr, safe);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700272 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700273
274 if (!valid && *safe) {
275 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
276 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800277 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700278 if (hdr->tag == BACKLOG_TAG) {
279 log_message("+++ ALLOCATION %p SIZE %d FREED HERE:\n",
280 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800281 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700282 }
283 }
284
285 return valid;
286}
287
Christopher Ferris885f3b92013-05-21 17:48:01 -0700288static inline int del_and_check_locked(hdr_t* hdr,
289 hdr_t** tail, hdr_t** head, unsigned* cnt,
290 int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700291 int valid = check_allocation_locked(hdr, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700292 if (safe) {
293 (*cnt)--;
294 del_locked(hdr, tail, head);
295 }
296 return valid;
297}
298
Christopher Ferris885f3b92013-05-21 17:48:01 -0700299static inline void del_from_backlog_locked(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700300 int safe;
301 del_and_check_locked(hdr,
302 &backlog_tail, &backlog_head, &backlog_num,
303 &safe);
304 hdr->tag = 0; /* clear the tag */
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700305}
306
Christopher Ferris885f3b92013-05-21 17:48:01 -0700307static inline void del_from_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700308 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700309 del_from_backlog_locked(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700310}
311
Christopher Ferris885f3b92013-05-21 17:48:01 -0700312static inline int del_leak(hdr_t* hdr, int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700313 ScopedPthreadMutexLocker locker(&lock);
Elliott Hughes1728b232014-05-14 10:02:03 -0700314 return del_and_check_locked(hdr, &tail, &head, &g_allocated_block_count, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700315}
316
Christopher Ferris885f3b92013-05-21 17:48:01 -0700317static inline void add_to_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700318 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700319 hdr->tag = BACKLOG_TAG;
320 backlog_num++;
321 add_locked(hdr, &backlog_tail, &backlog_head);
322 poison(hdr);
323 /* If we've exceeded the maximum backlog, clear it up */
Elliott Hughes1728b232014-05-14 10:02:03 -0700324 while (backlog_num > g_malloc_debug_backlog) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700325 hdr_t* gone = backlog_tail;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700326 del_from_backlog_locked(gone);
Christopher Ferris72bbd422014-05-08 11:14:03 -0700327 Malloc(free)(gone->base);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700328 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700329}
330
Christopher Ferrisa4037802014-06-09 19:14:11 -0700331extern "C" void* chk_malloc(size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700332// log_message("%s: %s\n", __FILE__, __FUNCTION__);
333
Christopher Ferrisa4037802014-06-09 19:14:11 -0700334 size_t size = sizeof(hdr_t) + bytes + sizeof(ftr_t);
335 if (size < bytes) { // Overflow
336 errno = ENOMEM;
337 return NULL;
338 }
339 hdr_t* hdr = static_cast<hdr_t*>(Malloc(malloc)(size));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700340 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700341 hdr->base = hdr;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700342 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700343 add(hdr, bytes);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700344 return user(hdr);
345 }
346 return NULL;
347}
348
Christopher Ferris885f3b92013-05-21 17:48:01 -0700349extern "C" void* chk_memalign(size_t alignment, size_t bytes) {
350 if (alignment <= MALLOC_ALIGNMENT) {
351 return chk_malloc(bytes);
352 }
353
354 // Make the alignment a power of two.
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700355 if (!powerof2(alignment)) {
356 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700357 }
358
359 // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
360 // we will align by at least MALLOC_ALIGNMENT bytes
361 // and at most alignment-MALLOC_ALIGNMENT bytes
362 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
363 if (size < bytes) { // Overflow.
364 return NULL;
365 }
366
Christopher Ferris72bbd422014-05-08 11:14:03 -0700367 void* base = Malloc(malloc)(sizeof(hdr_t) + size + sizeof(ftr_t));
Christopher Ferris885f3b92013-05-21 17:48:01 -0700368 if (base != NULL) {
369 // Check that the actual pointer that will be returned is aligned
370 // properly.
371 uintptr_t ptr = reinterpret_cast<uintptr_t>(user(reinterpret_cast<hdr_t*>(base)));
372 if ((ptr % alignment) != 0) {
373 // Align the pointer.
374 ptr += ((-ptr) % alignment);
375 }
376
377 hdr_t* hdr = meta(reinterpret_cast<void*>(ptr));
378 hdr->base = base;
379 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
380 add(hdr, bytes);
381 return user(hdr);
382 }
383 return base;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700384}
385
Christopher Ferris885f3b92013-05-21 17:48:01 -0700386extern "C" void chk_free(void* ptr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700387// log_message("%s: %s\n", __FILE__, __FUNCTION__);
388
389 if (!ptr) /* ignore free(NULL) */
390 return;
391
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700392 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700393
394 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800395 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800396 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700397 if (hdr->tag == BACKLOG_TAG) {
398 log_message("+++ ALLOCATION %p SIZE %d BYTES MULTIPLY FREED!\n",
399 user(hdr), hdr->size);
400 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
401 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800402 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700403 /* hdr->freed_bt_depth should be nonzero here */
404 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
405 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800406 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700407 log_message("+++ ALLOCATION %p SIZE %d NOW BEING FREED HERE:\n",
408 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800409 log_backtrace(bt, depth);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700410 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700411 log_message("+++ ALLOCATION %p IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
412 user(hdr));
Elliott Hughes35b621c2013-01-28 16:27:36 -0800413 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700414 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700415 } else {
Elliott Hughes35b621c2013-01-28 16:27:36 -0800416 hdr->freed_bt_depth = get_backtrace(hdr->freed_bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700417 add_to_backlog(hdr);
418 }
419}
420
Christopher Ferrisa4037802014-06-09 19:14:11 -0700421extern "C" void* chk_realloc(void* ptr, size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700422// log_message("%s: %s\n", __FILE__, __FUNCTION__);
423
Elliott Hughese7e274b2012-10-12 17:05:05 -0700424 if (!ptr) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700425 return chk_malloc(bytes);
Elliott Hughese7e274b2012-10-12 17:05:05 -0700426 }
427
428#ifdef REALLOC_ZERO_BYTES_FREE
Christopher Ferrisa4037802014-06-09 19:14:11 -0700429 if (!bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700430 chk_free(ptr);
431 return NULL;
432 }
Elliott Hughese7e274b2012-10-12 17:05:05 -0700433#endif
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700434
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700435 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700436
437 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800438 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800439 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700440 if (hdr->tag == BACKLOG_TAG) {
441 log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n",
Christopher Ferrisa4037802014-06-09 19:14:11 -0700442 user(hdr), bytes, hdr->size);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700443 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
444 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800445 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700446 /* hdr->freed_bt_depth should be nonzero here */
447 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
448 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800449 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700450 log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n",
451 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800452 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700453
454 /* We take the memory out of the backlog and fall through so the
455 * reallocation below succeeds. Since we didn't really free it, we
456 * can default to this behavior.
457 */
458 del_from_backlog(hdr);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700459 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700460 log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
Christopher Ferrisa4037802014-06-09 19:14:11 -0700461 user(hdr), bytes);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800462 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700463 // just get a whole new allocation and leak the old one
Christopher Ferrisa4037802014-06-09 19:14:11 -0700464 return Malloc(realloc)(0, bytes);
465 // return realloc(user(hdr), bytes); // assuming it was allocated externally
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700466 }
467 }
468
Christopher Ferrisa4037802014-06-09 19:14:11 -0700469 size_t size = sizeof(hdr_t) + bytes + sizeof(ftr_t);
470 if (size < bytes) { // Overflow
471 errno = ENOMEM;
472 return NULL;
473 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700474 if (hdr->base != hdr) {
475 // An allocation from memalign, so create another allocation and
476 // copy the data out.
Christopher Ferrisa4037802014-06-09 19:14:11 -0700477 void* newMem = Malloc(malloc)(size);
478 if (newMem == NULL) {
479 return NULL;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700480 }
Christopher Ferrisa4037802014-06-09 19:14:11 -0700481 memcpy(newMem, hdr, sizeof(hdr_t) + hdr->size);
482 Malloc(free)(hdr->base);
483 hdr = static_cast<hdr_t*>(newMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700484 } else {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700485 hdr = static_cast<hdr_t*>(Malloc(realloc)(hdr, size));
Christopher Ferris885f3b92013-05-21 17:48:01 -0700486 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700487 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700488 hdr->base = hdr;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700489 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700490 add(hdr, bytes);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700491 return user(hdr);
492 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700493 return NULL;
494}
495
Christopher Ferrisa4037802014-06-09 19:14:11 -0700496extern "C" void* chk_calloc(size_t nmemb, size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700497// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700498 size_t total_bytes = nmemb * bytes;
499 size_t size = sizeof(hdr_t) + total_bytes + sizeof(ftr_t);
500 if (size < total_bytes || (nmemb && SIZE_MAX / nmemb < bytes)) { // Overflow
501 errno = ENOMEM;
502 return NULL;
503 }
504 hdr_t* hdr = static_cast<hdr_t*>(Malloc(calloc)(1, size));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700505 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700506 hdr->base = hdr;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800507 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700508 add(hdr, total_bytes);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700509 return user(hdr);
510 }
511 return NULL;
512}
513
Christopher Ferris885f3b92013-05-21 17:48:01 -0700514extern "C" size_t chk_malloc_usable_size(const void* ptr) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700515 // malloc_usable_size returns 0 for NULL and unknown blocks.
Christopher Ferris885f3b92013-05-21 17:48:01 -0700516 if (ptr == NULL)
517 return 0;
518
519 const hdr_t* hdr = const_meta(ptr);
520
521 // The sentinel tail is written just after the request block bytes
522 // so there is no extra room we can report here.
523 return hdr->size;
524}
525
Christopher Ferrisa4037802014-06-09 19:14:11 -0700526extern "C" struct mallinfo chk_mallinfo() {
527 return Malloc(mallinfo)();
528}
529
530extern "C" int chk_posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700531 if (!powerof2(alignment)) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700532 return EINVAL;
533 }
534 int saved_errno = errno;
535 *memptr = chk_memalign(alignment, size);
536 errno = saved_errno;
537 return (*memptr != NULL) ? 0 : ENOMEM;
538}
539
540extern "C" void* chk_pvalloc(size_t bytes) {
541 size_t pagesize = sysconf(_SC_PAGESIZE);
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700542 size_t size = BIONIC_ALIGN(bytes, pagesize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700543 if (size < bytes) { // Overflow
544 return NULL;
545 }
546 return chk_memalign(pagesize, size);
547}
548
549extern "C" void* chk_valloc(size_t size) {
550 return chk_memalign(sysconf(_SC_PAGESIZE), size);
551}
552
Elliott Hughes9c818922013-02-01 17:07:40 -0800553static void ReportMemoryLeaks() {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800554 // Use /proc/self/exe link to obtain the program name for logging
555 // purposes. If it's not available, we set it to "<unknown>".
556 char exe[PATH_MAX];
557 int count;
558 if ((count = readlink("/proc/self/exe", exe, sizeof(exe) - 1)) == -1) {
559 strlcpy(exe, "<unknown>", sizeof(exe));
560 } else {
561 exe[count] = '\0';
562 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700563
Elliott Hughes1728b232014-05-14 10:02:03 -0700564 if (g_allocated_block_count == 0) {
Elliott Hughes1d12d572013-01-30 11:38:26 -0800565 log_message("+++ %s did not leak", exe);
566 return;
567 }
568
Elliott Hughes239e7a02013-01-25 17:13:45 -0800569 size_t index = 1;
Elliott Hughes1728b232014-05-14 10:02:03 -0700570 const size_t total = g_allocated_block_count;
Elliott Hughes239e7a02013-01-25 17:13:45 -0800571 while (head != NULL) {
572 int safe;
573 hdr_t* block = head;
574 log_message("+++ %s leaked block of size %d at %p (leak %d of %d)",
575 exe, block->size, user(block), index++, total);
576 if (del_leak(block, &safe)) {
577 /* safe == 1, because the allocation is valid */
Elliott Hughes35b621c2013-01-28 16:27:36 -0800578 log_backtrace(block->bt, block->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700579 }
Elliott Hughes239e7a02013-01-25 17:13:45 -0800580 }
581
582 while (backlog_head != NULL) {
583 del_from_backlog(backlog_tail);
584 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700585}
586
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700587extern "C" bool malloc_debug_initialize(HashTable* hash_table) {
588 g_hash_table = hash_table;
589
590 char debug_backlog[PROP_VALUE_MAX];
591 if (__system_property_get("libc.debug.malloc.backlog", debug_backlog)) {
592 g_malloc_debug_backlog = atoi(debug_backlog);
593 info_log("%s: setting backlog length to %d\n", getprogname(), g_malloc_debug_backlog);
594 }
595
Elliott Hughes35b621c2013-01-28 16:27:36 -0800596 backtrace_startup();
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700597 return true;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700598}
599
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700600extern "C" void malloc_debug_finalize(int malloc_debug_level) {
601 // We only track leaks at level 10.
602 if (malloc_debug_level == 10) {
603 ReportMemoryLeaks();
604 }
Elliott Hughes35b621c2013-01-28 16:27:36 -0800605 backtrace_shutdown();
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700606}