blob: 94ba6f5ec51df96e4ce011ee3ee63c69e6686c81 [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 Ferris861c0ef2014-07-24 17:52:23 -070052#include "malloc_debug_disable.h"
Christopher Ferris03eebcb2014-06-13 13:57:51 -070053#include "private/bionic_macros.h"
54#include "private/libc_logging.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070055#include "private/ScopedPthreadMutexLocker.h"
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070056
Elliott Hughes1e980b62013-01-17 18:36:06 -080057#define MAX_BACKTRACE_DEPTH 16
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070058#define ALLOCATION_TAG 0x1ee7d00d
59#define BACKLOG_TAG 0xbabecafe
60#define FREE_POISON 0xa5
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070061#define FRONT_GUARD 0xaa
62#define FRONT_GUARD_LEN (1<<5)
63#define REAR_GUARD 0xbb
64#define REAR_GUARD_LEN (1<<5)
65
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070066static void log_message(const char* format, ...) {
Elliott Hughes1e980b62013-01-17 18:36:06 -080067 va_list args;
68 va_start(args, format);
69 __libc_format_log_va_list(ANDROID_LOG_ERROR, "libc", format, args);
70 va_end(args);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070071}
72
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070073struct hdr_t {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070074 uint32_t tag;
Christopher Ferris72bbd422014-05-08 11:14:03 -070075 void* base; // Always points to the memory allocated using malloc.
Christopher Ferris885f3b92013-05-21 17:48:01 -070076 // For memory allocated in chk_memalign, this value will
77 // not be the same as the location of the start of this
78 // structure.
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070079 hdr_t* prev;
80 hdr_t* next;
Elliott Hughes239e7a02013-01-25 17:13:45 -080081 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070082 int bt_depth;
Elliott Hughes239e7a02013-01-25 17:13:45 -080083 uintptr_t freed_bt[MAX_BACKTRACE_DEPTH];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070084 int freed_bt_depth;
85 size_t size;
Elliott Hughesef0696d2013-10-08 16:16:01 -070086 uint8_t front_guard[FRONT_GUARD_LEN];
Christopher Ferris885f3b92013-05-21 17:48:01 -070087} __attribute__((packed, aligned(MALLOC_ALIGNMENT)));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070088
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070089struct ftr_t {
Elliott Hughesef0696d2013-10-08 16:16:01 -070090 uint8_t rear_guard[REAR_GUARD_LEN];
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070091} __attribute__((packed));
92
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070093static inline ftr_t* to_ftr(hdr_t* hdr) {
94 return reinterpret_cast<ftr_t*>(reinterpret_cast<char*>(hdr + 1) + hdr->size);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070095}
96
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070097static inline void* user(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070098 return hdr + 1;
99}
100
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700101static inline hdr_t* meta(void* user) {
102 return reinterpret_cast<hdr_t*>(user) - 1;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700103}
104
Christopher Ferris885f3b92013-05-21 17:48:01 -0700105static inline const hdr_t* const_meta(const void* user) {
106 return reinterpret_cast<const hdr_t*>(user) - 1;
107}
108
Elliott Hughes1728b232014-05-14 10:02:03 -0700109// TODO: introduce a struct for this global state.
110// There are basically two lists here, the regular list and the backlog list.
111// We should be able to remove the duplication.
112static unsigned g_allocated_block_count;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700113static hdr_t* tail;
114static hdr_t* head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700115static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
116
117static unsigned backlog_num;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700118static hdr_t* backlog_tail;
119static hdr_t* backlog_head;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700120static pthread_mutex_t backlog_lock = PTHREAD_MUTEX_INITIALIZER;
121
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700122// This variable is set to the value of property libc.debug.malloc.backlog.
123// It determines the size of the backlog we use to detect multiple frees.
124static unsigned g_malloc_debug_backlog = 100;
125
126__LIBC_HIDDEN__ HashTable* g_hash_table;
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700127__LIBC_HIDDEN__ const MallocDebug* g_malloc_dispatch;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700128
Christopher Ferris885f3b92013-05-21 17:48:01 -0700129static inline void init_front_guard(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700130 memset(hdr->front_guard, FRONT_GUARD, FRONT_GUARD_LEN);
131}
132
Christopher Ferris885f3b92013-05-21 17:48:01 -0700133static inline bool is_front_guard_valid(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700134 for (size_t i = 0; i < FRONT_GUARD_LEN; i++) {
135 if (hdr->front_guard[i] != FRONT_GUARD) {
Elliott Hughesef0696d2013-10-08 16:16:01 -0700136 return false;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700137 }
138 }
Elliott Hughesef0696d2013-10-08 16:16:01 -0700139 return true;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700140}
141
Christopher Ferris885f3b92013-05-21 17:48:01 -0700142static inline void init_rear_guard(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700143 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700144 memset(ftr->rear_guard, REAR_GUARD, REAR_GUARD_LEN);
145}
146
Christopher Ferris885f3b92013-05-21 17:48:01 -0700147static inline bool is_rear_guard_valid(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700148 unsigned i;
149 int valid = 1;
150 int first_mismatch = -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700151 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700152 for (i = 0; i < REAR_GUARD_LEN; i++) {
153 if (ftr->rear_guard[i] != REAR_GUARD) {
154 if (first_mismatch < 0)
155 first_mismatch = i;
156 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700157 } else if (first_mismatch >= 0) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700158 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
159 first_mismatch = -1;
160 }
161 }
162
163 if (first_mismatch >= 0)
164 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
165 return valid;
166}
167
Christopher Ferris885f3b92013-05-21 17:48:01 -0700168static inline void add_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700169 hdr->prev = NULL;
170 hdr->next = *head;
171 if (*head)
172 (*head)->prev = hdr;
173 else
174 *tail = hdr;
175 *head = hdr;
176}
177
Christopher Ferris885f3b92013-05-21 17:48:01 -0700178static inline int del_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700179 if (hdr->prev) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700180 hdr->prev->next = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700181 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700182 *head = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700183 }
184 if (hdr->next) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700185 hdr->next->prev = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700186 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700187 *tail = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700188 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700189 return 0;
190}
191
Christopher Ferris885f3b92013-05-21 17:48:01 -0700192static inline void add(hdr_t* hdr, size_t size) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700193 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700194 hdr->tag = ALLOCATION_TAG;
195 hdr->size = size;
196 init_front_guard(hdr);
197 init_rear_guard(hdr);
Elliott Hughes1728b232014-05-14 10:02:03 -0700198 ++g_allocated_block_count;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700199 add_locked(hdr, &tail, &head);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700200}
201
Christopher Ferris885f3b92013-05-21 17:48:01 -0700202static inline int del(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700203 if (hdr->tag != ALLOCATION_TAG) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700204 return -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700205 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700206
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700207 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700208 del_locked(hdr, &tail, &head);
Elliott Hughes1728b232014-05-14 10:02:03 -0700209 --g_allocated_block_count;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700210 return 0;
211}
212
Christopher Ferris885f3b92013-05-21 17:48:01 -0700213static inline void poison(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700214 memset(user(hdr), FREE_POISON, hdr->size);
215}
216
Elliott Hughesef0696d2013-10-08 16:16:01 -0700217static bool was_used_after_free(hdr_t* hdr) {
218 const uint8_t* data = reinterpret_cast<const uint8_t*>(user(hdr));
219 for (size_t i = 0; i < hdr->size; i++) {
220 if (data[i] != FREE_POISON) {
221 return true;
222 }
223 }
224 return false;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700225}
226
227/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700228static inline int check_guards(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700229 *safe = 1;
230 if (!is_front_guard_valid(hdr)) {
231 if (hdr->front_guard[0] == FRONT_GUARD) {
232 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED FRONT GUARD\n",
233 user(hdr), hdr->size);
234 } else {
235 log_message("+++ ALLOCATION %p HAS A CORRUPTED FRONT GUARD "\
236 "(NOT DUMPING STACKTRACE)\n", user(hdr));
237 /* Allocation header is probably corrupt, do not print stack trace */
238 *safe = 0;
239 }
240 return 0;
241 }
242
243 if (!is_rear_guard_valid(hdr)) {
244 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED REAR GUARD\n",
245 user(hdr), hdr->size);
246 return 0;
247 }
248
249 return 1;
250}
251
252/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700253static inline int check_allocation_locked(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700254 int valid = 1;
255 *safe = 1;
256
257 if (hdr->tag != ALLOCATION_TAG && hdr->tag != BACKLOG_TAG) {
258 log_message("+++ ALLOCATION %p HAS INVALID TAG %08x (NOT DUMPING STACKTRACE)\n",
259 user(hdr), hdr->tag);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700260 // Allocation header is probably corrupt, do not dequeue or dump stack
261 // trace.
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700262 *safe = 0;
263 return 0;
264 }
265
266 if (hdr->tag == BACKLOG_TAG && was_used_after_free(hdr)) {
267 log_message("+++ ALLOCATION %p SIZE %d WAS USED AFTER BEING FREED\n",
268 user(hdr), hdr->size);
269 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700270 /* check the guards to see if it's safe to dump a stack trace */
271 check_guards(hdr, safe);
272 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700273 valid = check_guards(hdr, safe);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700274 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700275
276 if (!valid && *safe) {
277 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
278 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800279 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700280 if (hdr->tag == BACKLOG_TAG) {
281 log_message("+++ ALLOCATION %p SIZE %d FREED HERE:\n",
282 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800283 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700284 }
285 }
286
287 return valid;
288}
289
Christopher Ferris885f3b92013-05-21 17:48:01 -0700290static inline int del_and_check_locked(hdr_t* hdr,
291 hdr_t** tail, hdr_t** head, unsigned* cnt,
292 int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700293 int valid = check_allocation_locked(hdr, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700294 if (safe) {
295 (*cnt)--;
296 del_locked(hdr, tail, head);
297 }
298 return valid;
299}
300
Christopher Ferris885f3b92013-05-21 17:48:01 -0700301static inline void del_from_backlog_locked(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700302 int safe;
303 del_and_check_locked(hdr,
304 &backlog_tail, &backlog_head, &backlog_num,
305 &safe);
306 hdr->tag = 0; /* clear the tag */
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700307}
308
Christopher Ferris885f3b92013-05-21 17:48:01 -0700309static inline void del_from_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700310 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700311 del_from_backlog_locked(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700312}
313
Christopher Ferris885f3b92013-05-21 17:48:01 -0700314static inline int del_leak(hdr_t* hdr, int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700315 ScopedPthreadMutexLocker locker(&lock);
Elliott Hughes1728b232014-05-14 10:02:03 -0700316 return del_and_check_locked(hdr, &tail, &head, &g_allocated_block_count, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700317}
318
Christopher Ferris885f3b92013-05-21 17:48:01 -0700319static inline void add_to_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700320 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700321 hdr->tag = BACKLOG_TAG;
322 backlog_num++;
323 add_locked(hdr, &backlog_tail, &backlog_head);
324 poison(hdr);
325 /* If we've exceeded the maximum backlog, clear it up */
Elliott Hughes1728b232014-05-14 10:02:03 -0700326 while (backlog_num > g_malloc_debug_backlog) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700327 hdr_t* gone = backlog_tail;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700328 del_from_backlog_locked(gone);
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700329 g_malloc_dispatch->free(gone->base);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700330 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700331}
332
Christopher Ferrisa4037802014-06-09 19:14:11 -0700333extern "C" void* chk_malloc(size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700334// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700335 if (DebugCallsDisabled()) {
336 return g_malloc_dispatch->malloc(bytes);
337 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700338
Christopher Ferrisa4037802014-06-09 19:14:11 -0700339 size_t size = sizeof(hdr_t) + bytes + sizeof(ftr_t);
340 if (size < bytes) { // Overflow
341 errno = ENOMEM;
342 return NULL;
343 }
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700344 hdr_t* hdr = static_cast<hdr_t*>(g_malloc_dispatch->malloc(size));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700345 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700346 hdr->base = hdr;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700347 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700348 add(hdr, bytes);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700349 return user(hdr);
350 }
351 return NULL;
352}
353
Christopher Ferris885f3b92013-05-21 17:48:01 -0700354extern "C" void* chk_memalign(size_t alignment, size_t bytes) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700355 if (DebugCallsDisabled()) {
356 return g_malloc_dispatch->memalign(alignment, bytes);
357 }
358
Christopher Ferris885f3b92013-05-21 17:48:01 -0700359 if (alignment <= MALLOC_ALIGNMENT) {
360 return chk_malloc(bytes);
361 }
362
363 // Make the alignment a power of two.
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700364 if (!powerof2(alignment)) {
365 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700366 }
367
368 // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
369 // we will align by at least MALLOC_ALIGNMENT bytes
370 // and at most alignment-MALLOC_ALIGNMENT bytes
371 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
372 if (size < bytes) { // Overflow.
373 return NULL;
374 }
375
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700376 void* base = g_malloc_dispatch->malloc(sizeof(hdr_t) + size + sizeof(ftr_t));
Christopher Ferris885f3b92013-05-21 17:48:01 -0700377 if (base != NULL) {
378 // Check that the actual pointer that will be returned is aligned
379 // properly.
380 uintptr_t ptr = reinterpret_cast<uintptr_t>(user(reinterpret_cast<hdr_t*>(base)));
381 if ((ptr % alignment) != 0) {
382 // Align the pointer.
383 ptr += ((-ptr) % alignment);
384 }
385
386 hdr_t* hdr = meta(reinterpret_cast<void*>(ptr));
387 hdr->base = base;
388 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
389 add(hdr, bytes);
390 return user(hdr);
391 }
392 return base;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700393}
394
Christopher Ferris885f3b92013-05-21 17:48:01 -0700395extern "C" void chk_free(void* ptr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700396// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700397 if (DebugCallsDisabled()) {
398 return g_malloc_dispatch->free(ptr);
399 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700400
401 if (!ptr) /* ignore free(NULL) */
402 return;
403
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700404 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700405
406 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800407 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800408 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700409 if (hdr->tag == BACKLOG_TAG) {
410 log_message("+++ ALLOCATION %p SIZE %d BYTES MULTIPLY FREED!\n",
411 user(hdr), hdr->size);
412 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
413 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800414 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700415 /* hdr->freed_bt_depth should be nonzero here */
416 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
417 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800418 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700419 log_message("+++ ALLOCATION %p SIZE %d NOW BEING FREED HERE:\n",
420 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800421 log_backtrace(bt, depth);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700422 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700423 log_message("+++ ALLOCATION %p IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
424 user(hdr));
Elliott Hughes35b621c2013-01-28 16:27:36 -0800425 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700426 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700427 } else {
Elliott Hughes35b621c2013-01-28 16:27:36 -0800428 hdr->freed_bt_depth = get_backtrace(hdr->freed_bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700429 add_to_backlog(hdr);
430 }
431}
432
Christopher Ferrisa4037802014-06-09 19:14:11 -0700433extern "C" void* chk_realloc(void* ptr, size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700434// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700435 if (DebugCallsDisabled()) {
436 return g_malloc_dispatch->realloc(ptr, bytes);
437 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700438
Elliott Hughese7e274b2012-10-12 17:05:05 -0700439 if (!ptr) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700440 return chk_malloc(bytes);
Elliott Hughese7e274b2012-10-12 17:05:05 -0700441 }
442
443#ifdef REALLOC_ZERO_BYTES_FREE
Christopher Ferrisa4037802014-06-09 19:14:11 -0700444 if (!bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700445 chk_free(ptr);
446 return NULL;
447 }
Elliott Hughese7e274b2012-10-12 17:05:05 -0700448#endif
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700449
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700450 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700451
452 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800453 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800454 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700455 if (hdr->tag == BACKLOG_TAG) {
456 log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n",
Christopher Ferrisa4037802014-06-09 19:14:11 -0700457 user(hdr), bytes, hdr->size);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700458 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
459 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800460 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700461 /* hdr->freed_bt_depth should be nonzero here */
462 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
463 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800464 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700465 log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n",
466 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800467 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700468
469 /* We take the memory out of the backlog and fall through so the
470 * reallocation below succeeds. Since we didn't really free it, we
471 * can default to this behavior.
472 */
473 del_from_backlog(hdr);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700474 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700475 log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
Christopher Ferrisa4037802014-06-09 19:14:11 -0700476 user(hdr), bytes);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800477 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700478 // just get a whole new allocation and leak the old one
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700479 return g_malloc_dispatch->realloc(0, bytes);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700480 // return realloc(user(hdr), bytes); // assuming it was allocated externally
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700481 }
482 }
483
Christopher Ferrisa4037802014-06-09 19:14:11 -0700484 size_t size = sizeof(hdr_t) + bytes + sizeof(ftr_t);
485 if (size < bytes) { // Overflow
486 errno = ENOMEM;
487 return NULL;
488 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700489 if (hdr->base != hdr) {
490 // An allocation from memalign, so create another allocation and
491 // copy the data out.
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700492 void* newMem = g_malloc_dispatch->malloc(size);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700493 if (newMem == NULL) {
494 return NULL;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700495 }
Christopher Ferrisa4037802014-06-09 19:14:11 -0700496 memcpy(newMem, hdr, sizeof(hdr_t) + hdr->size);
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700497 g_malloc_dispatch->free(hdr->base);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700498 hdr = static_cast<hdr_t*>(newMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700499 } else {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700500 hdr = static_cast<hdr_t*>(g_malloc_dispatch->realloc(hdr, size));
Christopher Ferris885f3b92013-05-21 17:48:01 -0700501 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700502 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700503 hdr->base = hdr;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700504 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700505 add(hdr, bytes);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700506 return user(hdr);
507 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700508 return NULL;
509}
510
Christopher Ferrisa4037802014-06-09 19:14:11 -0700511extern "C" void* chk_calloc(size_t nmemb, size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700512// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700513 if (DebugCallsDisabled()) {
514 return g_malloc_dispatch->calloc(nmemb, bytes);
515 }
516
Christopher Ferrisa4037802014-06-09 19:14:11 -0700517 size_t total_bytes = nmemb * bytes;
518 size_t size = sizeof(hdr_t) + total_bytes + sizeof(ftr_t);
519 if (size < total_bytes || (nmemb && SIZE_MAX / nmemb < bytes)) { // Overflow
520 errno = ENOMEM;
521 return NULL;
522 }
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700523 hdr_t* hdr = static_cast<hdr_t*>(g_malloc_dispatch->calloc(1, size));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700524 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700525 hdr->base = hdr;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800526 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700527 add(hdr, total_bytes);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700528 return user(hdr);
529 }
530 return NULL;
531}
532
Christopher Ferris885f3b92013-05-21 17:48:01 -0700533extern "C" size_t chk_malloc_usable_size(const void* ptr) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700534 if (DebugCallsDisabled()) {
535 return g_malloc_dispatch->malloc_usable_size(ptr);
536 }
537
Christopher Ferris72bbd422014-05-08 11:14:03 -0700538 // malloc_usable_size returns 0 for NULL and unknown blocks.
Christopher Ferris885f3b92013-05-21 17:48:01 -0700539 if (ptr == NULL)
540 return 0;
541
542 const hdr_t* hdr = const_meta(ptr);
543
544 // The sentinel tail is written just after the request block bytes
545 // so there is no extra room we can report here.
546 return hdr->size;
547}
548
Christopher Ferrisa4037802014-06-09 19:14:11 -0700549extern "C" struct mallinfo chk_mallinfo() {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700550 return g_malloc_dispatch->mallinfo();
Christopher Ferrisa4037802014-06-09 19:14:11 -0700551}
552
553extern "C" int chk_posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700554 if (DebugCallsDisabled()) {
555 return g_malloc_dispatch->posix_memalign(memptr, alignment, size);
556 }
557
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700558 if (!powerof2(alignment)) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700559 return EINVAL;
560 }
561 int saved_errno = errno;
562 *memptr = chk_memalign(alignment, size);
563 errno = saved_errno;
564 return (*memptr != NULL) ? 0 : ENOMEM;
565}
566
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700567#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisa4037802014-06-09 19:14:11 -0700568extern "C" void* chk_pvalloc(size_t bytes) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700569 if (DebugCallsDisabled()) {
570 return g_malloc_dispatch->pvalloc(bytes);
571 }
572
Elliott Hughes91570ce2014-07-10 12:34:23 -0700573 size_t pagesize = getpagesize();
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700574 size_t size = BIONIC_ALIGN(bytes, pagesize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700575 if (size < bytes) { // Overflow
576 return NULL;
577 }
578 return chk_memalign(pagesize, size);
579}
580
581extern "C" void* chk_valloc(size_t size) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700582 if (DebugCallsDisabled()) {
583 return g_malloc_dispatch->valloc(size);
584 }
Elliott Hughes91570ce2014-07-10 12:34:23 -0700585 return chk_memalign(getpagesize(), size);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700586}
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700587#endif
Christopher Ferrisa4037802014-06-09 19:14:11 -0700588
Elliott Hughes9c818922013-02-01 17:07:40 -0800589static void ReportMemoryLeaks() {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700590 ScopedDisableDebugCalls disable;
591
Elliott Hughes239e7a02013-01-25 17:13:45 -0800592 // Use /proc/self/exe link to obtain the program name for logging
593 // purposes. If it's not available, we set it to "<unknown>".
594 char exe[PATH_MAX];
595 int count;
596 if ((count = readlink("/proc/self/exe", exe, sizeof(exe) - 1)) == -1) {
597 strlcpy(exe, "<unknown>", sizeof(exe));
598 } else {
599 exe[count] = '\0';
600 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700601
Elliott Hughes1728b232014-05-14 10:02:03 -0700602 if (g_allocated_block_count == 0) {
Elliott Hughes1d12d572013-01-30 11:38:26 -0800603 log_message("+++ %s did not leak", exe);
604 return;
605 }
606
Elliott Hughes239e7a02013-01-25 17:13:45 -0800607 size_t index = 1;
Elliott Hughes1728b232014-05-14 10:02:03 -0700608 const size_t total = g_allocated_block_count;
Elliott Hughes239e7a02013-01-25 17:13:45 -0800609 while (head != NULL) {
610 int safe;
611 hdr_t* block = head;
612 log_message("+++ %s leaked block of size %d at %p (leak %d of %d)",
613 exe, block->size, user(block), index++, total);
614 if (del_leak(block, &safe)) {
615 /* safe == 1, because the allocation is valid */
Elliott Hughes35b621c2013-01-28 16:27:36 -0800616 log_backtrace(block->bt, block->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700617 }
Elliott Hughes239e7a02013-01-25 17:13:45 -0800618 }
619
620 while (backlog_head != NULL) {
621 del_from_backlog(backlog_tail);
622 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700623}
624
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700625pthread_key_t g_debug_calls_disabled;
626
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700627extern "C" bool malloc_debug_initialize(HashTable* hash_table, const MallocDebug* malloc_dispatch) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700628 g_hash_table = hash_table;
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700629 g_malloc_dispatch = malloc_dispatch;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700630
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700631 pthread_key_create(&g_debug_calls_disabled, NULL);
632
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700633 char debug_backlog[PROP_VALUE_MAX];
634 if (__system_property_get("libc.debug.malloc.backlog", debug_backlog)) {
635 g_malloc_debug_backlog = atoi(debug_backlog);
636 info_log("%s: setting backlog length to %d\n", getprogname(), g_malloc_debug_backlog);
637 }
638
Elliott Hughes35b621c2013-01-28 16:27:36 -0800639 backtrace_startup();
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700640 return true;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700641}
642
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700643extern "C" void malloc_debug_finalize(int malloc_debug_level) {
644 // We only track leaks at level 10.
645 if (malloc_debug_level == 10) {
646 ReportMemoryLeaks();
647 }
Elliott Hughes35b621c2013-01-28 16:27:36 -0800648 backtrace_shutdown();
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700649
650 pthread_setspecific(g_debug_calls_disabled, NULL);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700651}