blob: dee03fae15c11640b413f310077e8deb0bd8616b [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"
Christopher Ferris88a1f522014-08-07 16:21:21 -070051#include "malloc_debug_backtrace.h"
Elliott Hughes3b297c42012-10-11 16:08:51 -070052#include "malloc_debug_common.h"
Christopher Ferris861c0ef2014-07-24 17:52:23 -070053#include "malloc_debug_disable.h"
Christopher Ferris03eebcb2014-06-13 13:57:51 -070054#include "private/bionic_macros.h"
55#include "private/libc_logging.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070056#include "private/ScopedPthreadMutexLocker.h"
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
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700123// This variable is set to the value of property libc.debug.malloc.backlog.
124// It determines the size of the backlog we use to detect multiple frees.
125static unsigned g_malloc_debug_backlog = 100;
126
Christopher Ferris88a1f522014-08-07 16:21:21 -0700127// This variable is set to false if the property libc.debug.malloc.nobacktrace
128// is set to non-zero.
129__LIBC_HIDDEN__ bool g_backtrace_enabled = true;
130
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700131__LIBC_HIDDEN__ HashTable* g_hash_table;
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700132__LIBC_HIDDEN__ const MallocDebug* g_malloc_dispatch;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700133
Christopher Ferris885f3b92013-05-21 17:48:01 -0700134static inline void init_front_guard(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700135 memset(hdr->front_guard, FRONT_GUARD, FRONT_GUARD_LEN);
136}
137
Christopher Ferris885f3b92013-05-21 17:48:01 -0700138static inline bool is_front_guard_valid(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700139 for (size_t i = 0; i < FRONT_GUARD_LEN; i++) {
140 if (hdr->front_guard[i] != FRONT_GUARD) {
Elliott Hughesef0696d2013-10-08 16:16:01 -0700141 return false;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700142 }
143 }
Elliott Hughesef0696d2013-10-08 16:16:01 -0700144 return true;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700145}
146
Christopher Ferris885f3b92013-05-21 17:48:01 -0700147static inline void init_rear_guard(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700148 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700149 memset(ftr->rear_guard, REAR_GUARD, REAR_GUARD_LEN);
150}
151
Christopher Ferris885f3b92013-05-21 17:48:01 -0700152static inline bool is_rear_guard_valid(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700153 unsigned i;
154 int valid = 1;
155 int first_mismatch = -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700156 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700157 for (i = 0; i < REAR_GUARD_LEN; i++) {
158 if (ftr->rear_guard[i] != REAR_GUARD) {
159 if (first_mismatch < 0)
160 first_mismatch = i;
161 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700162 } else if (first_mismatch >= 0) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700163 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
164 first_mismatch = -1;
165 }
166 }
167
168 if (first_mismatch >= 0)
169 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
170 return valid;
171}
172
Christopher Ferris885f3b92013-05-21 17:48:01 -0700173static inline void add_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700174 hdr->prev = NULL;
175 hdr->next = *head;
176 if (*head)
177 (*head)->prev = hdr;
178 else
179 *tail = hdr;
180 *head = hdr;
181}
182
Christopher Ferris885f3b92013-05-21 17:48:01 -0700183static inline int del_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700184 if (hdr->prev) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700185 hdr->prev->next = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700186 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700187 *head = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700188 }
189 if (hdr->next) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700190 hdr->next->prev = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700191 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700192 *tail = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700193 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700194 return 0;
195}
196
Christopher Ferris885f3b92013-05-21 17:48:01 -0700197static inline void add(hdr_t* hdr, size_t size) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700198 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700199 hdr->tag = ALLOCATION_TAG;
200 hdr->size = size;
201 init_front_guard(hdr);
202 init_rear_guard(hdr);
Elliott Hughes1728b232014-05-14 10:02:03 -0700203 ++g_allocated_block_count;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700204 add_locked(hdr, &tail, &head);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700205}
206
Christopher Ferris885f3b92013-05-21 17:48:01 -0700207static inline int del(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700208 if (hdr->tag != ALLOCATION_TAG) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700209 return -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700210 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700211
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700212 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700213 del_locked(hdr, &tail, &head);
Elliott Hughes1728b232014-05-14 10:02:03 -0700214 --g_allocated_block_count;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700215 return 0;
216}
217
Christopher Ferris885f3b92013-05-21 17:48:01 -0700218static inline void poison(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700219 memset(user(hdr), FREE_POISON, hdr->size);
220}
221
Elliott Hughesef0696d2013-10-08 16:16:01 -0700222static bool was_used_after_free(hdr_t* hdr) {
223 const uint8_t* data = reinterpret_cast<const uint8_t*>(user(hdr));
224 for (size_t i = 0; i < hdr->size; i++) {
225 if (data[i] != FREE_POISON) {
226 return true;
227 }
228 }
229 return false;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700230}
231
232/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700233static inline int check_guards(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700234 *safe = 1;
235 if (!is_front_guard_valid(hdr)) {
236 if (hdr->front_guard[0] == FRONT_GUARD) {
237 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED FRONT GUARD\n",
238 user(hdr), hdr->size);
239 } else {
240 log_message("+++ ALLOCATION %p HAS A CORRUPTED FRONT GUARD "\
241 "(NOT DUMPING STACKTRACE)\n", user(hdr));
242 /* Allocation header is probably corrupt, do not print stack trace */
243 *safe = 0;
244 }
245 return 0;
246 }
247
248 if (!is_rear_guard_valid(hdr)) {
249 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED REAR GUARD\n",
250 user(hdr), hdr->size);
251 return 0;
252 }
253
254 return 1;
255}
256
257/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700258static inline int check_allocation_locked(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700259 int valid = 1;
260 *safe = 1;
261
262 if (hdr->tag != ALLOCATION_TAG && hdr->tag != BACKLOG_TAG) {
263 log_message("+++ ALLOCATION %p HAS INVALID TAG %08x (NOT DUMPING STACKTRACE)\n",
264 user(hdr), hdr->tag);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700265 // Allocation header is probably corrupt, do not dequeue or dump stack
266 // trace.
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700267 *safe = 0;
268 return 0;
269 }
270
271 if (hdr->tag == BACKLOG_TAG && was_used_after_free(hdr)) {
272 log_message("+++ ALLOCATION %p SIZE %d WAS USED AFTER BEING FREED\n",
273 user(hdr), hdr->size);
274 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700275 /* check the guards to see if it's safe to dump a stack trace */
276 check_guards(hdr, safe);
277 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700278 valid = check_guards(hdr, safe);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700279 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700280
Christopher Ferris88a1f522014-08-07 16:21:21 -0700281 if (!valid && *safe && g_backtrace_enabled) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700282 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
283 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800284 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700285 if (hdr->tag == BACKLOG_TAG) {
286 log_message("+++ ALLOCATION %p SIZE %d FREED HERE:\n",
287 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800288 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700289 }
290 }
291
292 return valid;
293}
294
Christopher Ferris885f3b92013-05-21 17:48:01 -0700295static inline int del_and_check_locked(hdr_t* hdr,
296 hdr_t** tail, hdr_t** head, unsigned* cnt,
297 int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700298 int valid = check_allocation_locked(hdr, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700299 if (safe) {
300 (*cnt)--;
301 del_locked(hdr, tail, head);
302 }
303 return valid;
304}
305
Christopher Ferris885f3b92013-05-21 17:48:01 -0700306static inline void del_from_backlog_locked(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700307 int safe;
308 del_and_check_locked(hdr,
309 &backlog_tail, &backlog_head, &backlog_num,
310 &safe);
311 hdr->tag = 0; /* clear the tag */
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700312}
313
Christopher Ferris885f3b92013-05-21 17:48:01 -0700314static inline void del_from_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700315 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700316 del_from_backlog_locked(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700317}
318
Christopher Ferris885f3b92013-05-21 17:48:01 -0700319static inline int del_leak(hdr_t* hdr, int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700320 ScopedPthreadMutexLocker locker(&lock);
Elliott Hughes1728b232014-05-14 10:02:03 -0700321 return del_and_check_locked(hdr, &tail, &head, &g_allocated_block_count, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700322}
323
Christopher Ferris885f3b92013-05-21 17:48:01 -0700324static inline void add_to_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700325 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700326 hdr->tag = BACKLOG_TAG;
327 backlog_num++;
328 add_locked(hdr, &backlog_tail, &backlog_head);
329 poison(hdr);
330 /* If we've exceeded the maximum backlog, clear it up */
Elliott Hughes1728b232014-05-14 10:02:03 -0700331 while (backlog_num > g_malloc_debug_backlog) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700332 hdr_t* gone = backlog_tail;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700333 del_from_backlog_locked(gone);
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700334 g_malloc_dispatch->free(gone->base);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700335 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700336}
337
Christopher Ferrisa4037802014-06-09 19:14:11 -0700338extern "C" void* chk_malloc(size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700339// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700340 if (DebugCallsDisabled()) {
341 return g_malloc_dispatch->malloc(bytes);
342 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700343
Christopher Ferrisa4037802014-06-09 19:14:11 -0700344 size_t size = sizeof(hdr_t) + bytes + sizeof(ftr_t);
345 if (size < bytes) { // Overflow
346 errno = ENOMEM;
347 return NULL;
348 }
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700349 hdr_t* hdr = static_cast<hdr_t*>(g_malloc_dispatch->malloc(size));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700350 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700351 hdr->base = hdr;
Christopher Ferris88a1f522014-08-07 16:21:21 -0700352 hdr->bt_depth = GET_BACKTRACE(hdr->bt, MAX_BACKTRACE_DEPTH);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700353 add(hdr, bytes);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700354 return user(hdr);
355 }
356 return NULL;
357}
358
Christopher Ferris885f3b92013-05-21 17:48:01 -0700359extern "C" void* chk_memalign(size_t alignment, size_t bytes) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700360 if (DebugCallsDisabled()) {
361 return g_malloc_dispatch->memalign(alignment, bytes);
362 }
363
Christopher Ferris885f3b92013-05-21 17:48:01 -0700364 if (alignment <= MALLOC_ALIGNMENT) {
365 return chk_malloc(bytes);
366 }
367
368 // Make the alignment a power of two.
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700369 if (!powerof2(alignment)) {
370 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700371 }
372
373 // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
374 // we will align by at least MALLOC_ALIGNMENT bytes
375 // and at most alignment-MALLOC_ALIGNMENT bytes
376 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
377 if (size < bytes) { // Overflow.
378 return NULL;
379 }
380
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700381 void* base = g_malloc_dispatch->malloc(sizeof(hdr_t) + size + sizeof(ftr_t));
Christopher Ferris885f3b92013-05-21 17:48:01 -0700382 if (base != NULL) {
383 // Check that the actual pointer that will be returned is aligned
384 // properly.
385 uintptr_t ptr = reinterpret_cast<uintptr_t>(user(reinterpret_cast<hdr_t*>(base)));
386 if ((ptr % alignment) != 0) {
387 // Align the pointer.
388 ptr += ((-ptr) % alignment);
389 }
390
391 hdr_t* hdr = meta(reinterpret_cast<void*>(ptr));
392 hdr->base = base;
Christopher Ferris88a1f522014-08-07 16:21:21 -0700393 hdr->bt_depth = GET_BACKTRACE(hdr->bt, MAX_BACKTRACE_DEPTH);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700394 add(hdr, bytes);
395 return user(hdr);
396 }
397 return base;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700398}
399
Christopher Ferris885f3b92013-05-21 17:48:01 -0700400extern "C" void chk_free(void* ptr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700401// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700402 if (DebugCallsDisabled()) {
403 return g_malloc_dispatch->free(ptr);
404 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700405
406 if (!ptr) /* ignore free(NULL) */
407 return;
408
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700409 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700410
411 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800412 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Christopher Ferris88a1f522014-08-07 16:21:21 -0700413 int depth = GET_BACKTRACE(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700414 if (hdr->tag == BACKLOG_TAG) {
415 log_message("+++ ALLOCATION %p SIZE %d BYTES MULTIPLY FREED!\n",
416 user(hdr), hdr->size);
Christopher Ferris88a1f522014-08-07 16:21:21 -0700417 if (g_backtrace_enabled) {
418 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
419 user(hdr), hdr->size);
420 log_backtrace(hdr->bt, hdr->bt_depth);
421 /* hdr->freed_bt_depth should be nonzero here */
422 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
423 user(hdr), hdr->size);
424 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
425 log_message("+++ ALLOCATION %p SIZE %d NOW BEING FREED HERE:\n",
426 user(hdr), hdr->size);
427 log_backtrace(bt, depth);
428 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700429 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700430 log_message("+++ ALLOCATION %p IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
431 user(hdr));
Christopher Ferris88a1f522014-08-07 16:21:21 -0700432 if (g_backtrace_enabled) {
433 log_backtrace(bt, depth);
434 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700435 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700436 } else {
Christopher Ferris88a1f522014-08-07 16:21:21 -0700437 hdr->freed_bt_depth = GET_BACKTRACE(hdr->freed_bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700438 add_to_backlog(hdr);
439 }
440}
441
Christopher Ferrisa4037802014-06-09 19:14:11 -0700442extern "C" void* chk_realloc(void* ptr, size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700443// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700444 if (DebugCallsDisabled()) {
445 return g_malloc_dispatch->realloc(ptr, bytes);
446 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700447
Elliott Hughese7e274b2012-10-12 17:05:05 -0700448 if (!ptr) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700449 return chk_malloc(bytes);
Elliott Hughese7e274b2012-10-12 17:05:05 -0700450 }
451
452#ifdef REALLOC_ZERO_BYTES_FREE
Christopher Ferrisa4037802014-06-09 19:14:11 -0700453 if (!bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700454 chk_free(ptr);
455 return NULL;
456 }
Elliott Hughese7e274b2012-10-12 17:05:05 -0700457#endif
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700458
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700459 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700460
461 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800462 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Christopher Ferris88a1f522014-08-07 16:21:21 -0700463 int depth = GET_BACKTRACE(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700464 if (hdr->tag == BACKLOG_TAG) {
465 log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n",
Christopher Ferrisa4037802014-06-09 19:14:11 -0700466 user(hdr), bytes, hdr->size);
Christopher Ferris88a1f522014-08-07 16:21:21 -0700467 if (g_backtrace_enabled) {
468 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
469 user(hdr), hdr->size);
470 log_backtrace(hdr->bt, hdr->bt_depth);
471 /* hdr->freed_bt_depth should be nonzero here */
472 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
473 user(hdr), hdr->size);
474 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
475 log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n",
476 user(hdr), hdr->size);
477 log_backtrace(bt, depth);
478 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700479
Christopher Ferris88a1f522014-08-07 16:21:21 -0700480 /* We take the memory out of the backlog and fall through so the
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700481 * reallocation below succeeds. Since we didn't really free it, we
482 * can default to this behavior.
483 */
484 del_from_backlog(hdr);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700485 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700486 log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
Christopher Ferrisa4037802014-06-09 19:14:11 -0700487 user(hdr), bytes);
Christopher Ferris88a1f522014-08-07 16:21:21 -0700488 if (g_backtrace_enabled) {
489 log_backtrace(bt, depth);
490 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700491 // just get a whole new allocation and leak the old one
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700492 return g_malloc_dispatch->realloc(0, bytes);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700493 // return realloc(user(hdr), bytes); // assuming it was allocated externally
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700494 }
495 }
496
Christopher Ferrisa4037802014-06-09 19:14:11 -0700497 size_t size = sizeof(hdr_t) + bytes + sizeof(ftr_t);
498 if (size < bytes) { // Overflow
499 errno = ENOMEM;
500 return NULL;
501 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700502 if (hdr->base != hdr) {
503 // An allocation from memalign, so create another allocation and
504 // copy the data out.
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700505 void* newMem = g_malloc_dispatch->malloc(size);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700506 if (newMem == NULL) {
507 return NULL;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700508 }
Christopher Ferrisa4037802014-06-09 19:14:11 -0700509 memcpy(newMem, hdr, sizeof(hdr_t) + hdr->size);
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700510 g_malloc_dispatch->free(hdr->base);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700511 hdr = static_cast<hdr_t*>(newMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700512 } else {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700513 hdr = static_cast<hdr_t*>(g_malloc_dispatch->realloc(hdr, size));
Christopher Ferris885f3b92013-05-21 17:48:01 -0700514 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700515 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700516 hdr->base = hdr;
Christopher Ferris88a1f522014-08-07 16:21:21 -0700517 hdr->bt_depth = GET_BACKTRACE(hdr->bt, MAX_BACKTRACE_DEPTH);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700518 add(hdr, bytes);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700519 return user(hdr);
520 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700521 return NULL;
522}
523
Christopher Ferrisa4037802014-06-09 19:14:11 -0700524extern "C" void* chk_calloc(size_t nmemb, size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700525// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700526 if (DebugCallsDisabled()) {
527 return g_malloc_dispatch->calloc(nmemb, bytes);
528 }
529
Christopher Ferrisa4037802014-06-09 19:14:11 -0700530 size_t total_bytes = nmemb * bytes;
531 size_t size = sizeof(hdr_t) + total_bytes + sizeof(ftr_t);
532 if (size < total_bytes || (nmemb && SIZE_MAX / nmemb < bytes)) { // Overflow
533 errno = ENOMEM;
534 return NULL;
535 }
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700536 hdr_t* hdr = static_cast<hdr_t*>(g_malloc_dispatch->calloc(1, size));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700537 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700538 hdr->base = hdr;
Christopher Ferris88a1f522014-08-07 16:21:21 -0700539 hdr->bt_depth = GET_BACKTRACE(hdr->bt, MAX_BACKTRACE_DEPTH);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700540 add(hdr, total_bytes);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700541 return user(hdr);
542 }
543 return NULL;
544}
545
Christopher Ferris885f3b92013-05-21 17:48:01 -0700546extern "C" size_t chk_malloc_usable_size(const void* ptr) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700547 if (DebugCallsDisabled()) {
548 return g_malloc_dispatch->malloc_usable_size(ptr);
549 }
550
Christopher Ferris72bbd422014-05-08 11:14:03 -0700551 // malloc_usable_size returns 0 for NULL and unknown blocks.
Christopher Ferris885f3b92013-05-21 17:48:01 -0700552 if (ptr == NULL)
553 return 0;
554
555 const hdr_t* hdr = const_meta(ptr);
556
557 // The sentinel tail is written just after the request block bytes
558 // so there is no extra room we can report here.
559 return hdr->size;
560}
561
Christopher Ferrisa4037802014-06-09 19:14:11 -0700562extern "C" struct mallinfo chk_mallinfo() {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700563 return g_malloc_dispatch->mallinfo();
Christopher Ferrisa4037802014-06-09 19:14:11 -0700564}
565
566extern "C" int chk_posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700567 if (DebugCallsDisabled()) {
568 return g_malloc_dispatch->posix_memalign(memptr, alignment, size);
569 }
570
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700571 if (!powerof2(alignment)) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700572 return EINVAL;
573 }
574 int saved_errno = errno;
575 *memptr = chk_memalign(alignment, size);
576 errno = saved_errno;
577 return (*memptr != NULL) ? 0 : ENOMEM;
578}
579
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700580#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisa4037802014-06-09 19:14:11 -0700581extern "C" void* chk_pvalloc(size_t bytes) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700582 if (DebugCallsDisabled()) {
583 return g_malloc_dispatch->pvalloc(bytes);
584 }
585
Elliott Hughes91570ce2014-07-10 12:34:23 -0700586 size_t pagesize = getpagesize();
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700587 size_t size = BIONIC_ALIGN(bytes, pagesize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700588 if (size < bytes) { // Overflow
589 return NULL;
590 }
591 return chk_memalign(pagesize, size);
592}
593
594extern "C" void* chk_valloc(size_t size) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700595 if (DebugCallsDisabled()) {
596 return g_malloc_dispatch->valloc(size);
597 }
Elliott Hughes91570ce2014-07-10 12:34:23 -0700598 return chk_memalign(getpagesize(), size);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700599}
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700600#endif
Christopher Ferrisa4037802014-06-09 19:14:11 -0700601
Elliott Hughes9c818922013-02-01 17:07:40 -0800602static void ReportMemoryLeaks() {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700603 ScopedDisableDebugCalls disable;
604
Elliott Hughes239e7a02013-01-25 17:13:45 -0800605 // Use /proc/self/exe link to obtain the program name for logging
606 // purposes. If it's not available, we set it to "<unknown>".
607 char exe[PATH_MAX];
608 int count;
609 if ((count = readlink("/proc/self/exe", exe, sizeof(exe) - 1)) == -1) {
610 strlcpy(exe, "<unknown>", sizeof(exe));
611 } else {
612 exe[count] = '\0';
613 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700614
Elliott Hughes1728b232014-05-14 10:02:03 -0700615 if (g_allocated_block_count == 0) {
Elliott Hughes1d12d572013-01-30 11:38:26 -0800616 log_message("+++ %s did not leak", exe);
617 return;
618 }
619
Elliott Hughes239e7a02013-01-25 17:13:45 -0800620 size_t index = 1;
Elliott Hughes1728b232014-05-14 10:02:03 -0700621 const size_t total = g_allocated_block_count;
Elliott Hughes239e7a02013-01-25 17:13:45 -0800622 while (head != NULL) {
623 int safe;
624 hdr_t* block = head;
625 log_message("+++ %s leaked block of size %d at %p (leak %d of %d)",
626 exe, block->size, user(block), index++, total);
Christopher Ferris88a1f522014-08-07 16:21:21 -0700627 if (del_leak(block, &safe) && g_backtrace_enabled) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800628 /* safe == 1, because the allocation is valid */
Elliott Hughes35b621c2013-01-28 16:27:36 -0800629 log_backtrace(block->bt, block->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700630 }
Elliott Hughes239e7a02013-01-25 17:13:45 -0800631 }
632
633 while (backlog_head != NULL) {
634 del_from_backlog(backlog_tail);
635 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700636}
637
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700638pthread_key_t g_debug_calls_disabled;
639
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700640extern "C" bool malloc_debug_initialize(HashTable* hash_table, const MallocDebug* malloc_dispatch) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700641 g_hash_table = hash_table;
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700642 g_malloc_dispatch = malloc_dispatch;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700643
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700644 pthread_key_create(&g_debug_calls_disabled, NULL);
645
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700646 char debug_backlog[PROP_VALUE_MAX];
647 if (__system_property_get("libc.debug.malloc.backlog", debug_backlog)) {
648 g_malloc_debug_backlog = atoi(debug_backlog);
649 info_log("%s: setting backlog length to %d\n", getprogname(), g_malloc_debug_backlog);
650 }
651
Christopher Ferris88a1f522014-08-07 16:21:21 -0700652 // Check if backtracing should be disabled.
653 char env[PROP_VALUE_MAX];
654 if (__system_property_get("libc.debug.malloc.nobacktrace", env) && atoi(env) != 0) {
655 g_backtrace_enabled = false;
656 __libc_format_log(ANDROID_LOG_INFO, "libc", "not gathering backtrace information\n");
657 }
658
659 if (g_backtrace_enabled) {
660 backtrace_startup();
661 }
662
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700663 return true;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700664}
665
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700666extern "C" void malloc_debug_finalize(int malloc_debug_level) {
667 // We only track leaks at level 10.
668 if (malloc_debug_level == 10) {
669 ReportMemoryLeaks();
670 }
Christopher Ferris88a1f522014-08-07 16:21:21 -0700671 if (g_backtrace_enabled) {
672 backtrace_shutdown();
673 }
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700674
675 pthread_setspecific(g_debug_calls_disabled, NULL);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700676}