blob: 1c63d4dcf14679653bb3e5a719b3c8e6b797fa3e [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;
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700126__LIBC_HIDDEN__ const MallocDebug* g_malloc_dispatch;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700127
Christopher Ferris885f3b92013-05-21 17:48:01 -0700128static inline void init_front_guard(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700129 memset(hdr->front_guard, FRONT_GUARD, FRONT_GUARD_LEN);
130}
131
Christopher Ferris885f3b92013-05-21 17:48:01 -0700132static inline bool is_front_guard_valid(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700133 for (size_t i = 0; i < FRONT_GUARD_LEN; i++) {
134 if (hdr->front_guard[i] != FRONT_GUARD) {
Elliott Hughesef0696d2013-10-08 16:16:01 -0700135 return false;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700136 }
137 }
Elliott Hughesef0696d2013-10-08 16:16:01 -0700138 return true;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700139}
140
Christopher Ferris885f3b92013-05-21 17:48:01 -0700141static inline void init_rear_guard(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700142 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700143 memset(ftr->rear_guard, REAR_GUARD, REAR_GUARD_LEN);
144}
145
Christopher Ferris885f3b92013-05-21 17:48:01 -0700146static inline bool is_rear_guard_valid(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700147 unsigned i;
148 int valid = 1;
149 int first_mismatch = -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700150 ftr_t* ftr = to_ftr(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700151 for (i = 0; i < REAR_GUARD_LEN; i++) {
152 if (ftr->rear_guard[i] != REAR_GUARD) {
153 if (first_mismatch < 0)
154 first_mismatch = i;
155 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700156 } else if (first_mismatch >= 0) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700157 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
158 first_mismatch = -1;
159 }
160 }
161
162 if (first_mismatch >= 0)
163 log_message("+++ REAR GUARD MISMATCH [%d, %d)\n", first_mismatch, i);
164 return valid;
165}
166
Christopher Ferris885f3b92013-05-21 17:48:01 -0700167static inline void add_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700168 hdr->prev = NULL;
169 hdr->next = *head;
170 if (*head)
171 (*head)->prev = hdr;
172 else
173 *tail = hdr;
174 *head = hdr;
175}
176
Christopher Ferris885f3b92013-05-21 17:48:01 -0700177static inline int del_locked(hdr_t* hdr, hdr_t** tail, hdr_t** head) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700178 if (hdr->prev) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700179 hdr->prev->next = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700180 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700181 *head = hdr->next;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700182 }
183 if (hdr->next) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700184 hdr->next->prev = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700185 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700186 *tail = hdr->prev;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700187 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700188 return 0;
189}
190
Christopher Ferris885f3b92013-05-21 17:48:01 -0700191static inline void add(hdr_t* hdr, size_t size) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700192 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700193 hdr->tag = ALLOCATION_TAG;
194 hdr->size = size;
195 init_front_guard(hdr);
196 init_rear_guard(hdr);
Elliott Hughes1728b232014-05-14 10:02:03 -0700197 ++g_allocated_block_count;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700198 add_locked(hdr, &tail, &head);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700199}
200
Christopher Ferris885f3b92013-05-21 17:48:01 -0700201static inline int del(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700202 if (hdr->tag != ALLOCATION_TAG) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700203 return -1;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700204 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700205
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700206 ScopedPthreadMutexLocker locker(&lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700207 del_locked(hdr, &tail, &head);
Elliott Hughes1728b232014-05-14 10:02:03 -0700208 --g_allocated_block_count;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700209 return 0;
210}
211
Christopher Ferris885f3b92013-05-21 17:48:01 -0700212static inline void poison(hdr_t* hdr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700213 memset(user(hdr), FREE_POISON, hdr->size);
214}
215
Elliott Hughesef0696d2013-10-08 16:16:01 -0700216static bool was_used_after_free(hdr_t* hdr) {
217 const uint8_t* data = reinterpret_cast<const uint8_t*>(user(hdr));
218 for (size_t i = 0; i < hdr->size; i++) {
219 if (data[i] != FREE_POISON) {
220 return true;
221 }
222 }
223 return false;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700224}
225
226/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700227static inline int check_guards(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700228 *safe = 1;
229 if (!is_front_guard_valid(hdr)) {
230 if (hdr->front_guard[0] == FRONT_GUARD) {
231 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED FRONT GUARD\n",
232 user(hdr), hdr->size);
233 } else {
234 log_message("+++ ALLOCATION %p HAS A CORRUPTED FRONT GUARD "\
235 "(NOT DUMPING STACKTRACE)\n", user(hdr));
236 /* Allocation header is probably corrupt, do not print stack trace */
237 *safe = 0;
238 }
239 return 0;
240 }
241
242 if (!is_rear_guard_valid(hdr)) {
243 log_message("+++ ALLOCATION %p SIZE %d HAS A CORRUPTED REAR GUARD\n",
244 user(hdr), hdr->size);
245 return 0;
246 }
247
248 return 1;
249}
250
251/* returns 1 if valid, *safe == 1 if safe to dump stack */
Christopher Ferris885f3b92013-05-21 17:48:01 -0700252static inline int check_allocation_locked(hdr_t* hdr, int* safe) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700253 int valid = 1;
254 *safe = 1;
255
256 if (hdr->tag != ALLOCATION_TAG && hdr->tag != BACKLOG_TAG) {
257 log_message("+++ ALLOCATION %p HAS INVALID TAG %08x (NOT DUMPING STACKTRACE)\n",
258 user(hdr), hdr->tag);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700259 // Allocation header is probably corrupt, do not dequeue or dump stack
260 // trace.
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700261 *safe = 0;
262 return 0;
263 }
264
265 if (hdr->tag == BACKLOG_TAG && was_used_after_free(hdr)) {
266 log_message("+++ ALLOCATION %p SIZE %d WAS USED AFTER BEING FREED\n",
267 user(hdr), hdr->size);
268 valid = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700269 /* check the guards to see if it's safe to dump a stack trace */
270 check_guards(hdr, safe);
271 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700272 valid = check_guards(hdr, safe);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700273 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700274
275 if (!valid && *safe) {
276 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
277 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800278 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700279 if (hdr->tag == BACKLOG_TAG) {
280 log_message("+++ ALLOCATION %p SIZE %d FREED HERE:\n",
281 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800282 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700283 }
284 }
285
286 return valid;
287}
288
Christopher Ferris885f3b92013-05-21 17:48:01 -0700289static inline int del_and_check_locked(hdr_t* hdr,
290 hdr_t** tail, hdr_t** head, unsigned* cnt,
291 int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700292 int valid = check_allocation_locked(hdr, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700293 if (safe) {
294 (*cnt)--;
295 del_locked(hdr, tail, head);
296 }
297 return valid;
298}
299
Christopher Ferris885f3b92013-05-21 17:48:01 -0700300static inline void del_from_backlog_locked(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700301 int safe;
302 del_and_check_locked(hdr,
303 &backlog_tail, &backlog_head, &backlog_num,
304 &safe);
305 hdr->tag = 0; /* clear the tag */
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700306}
307
Christopher Ferris885f3b92013-05-21 17:48:01 -0700308static inline void del_from_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700309 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700310 del_from_backlog_locked(hdr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700311}
312
Christopher Ferris885f3b92013-05-21 17:48:01 -0700313static inline int del_leak(hdr_t* hdr, int* safe) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700314 ScopedPthreadMutexLocker locker(&lock);
Elliott Hughes1728b232014-05-14 10:02:03 -0700315 return del_and_check_locked(hdr, &tail, &head, &g_allocated_block_count, safe);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700316}
317
Christopher Ferris885f3b92013-05-21 17:48:01 -0700318static inline void add_to_backlog(hdr_t* hdr) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700319 ScopedPthreadMutexLocker locker(&backlog_lock);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700320 hdr->tag = BACKLOG_TAG;
321 backlog_num++;
322 add_locked(hdr, &backlog_tail, &backlog_head);
323 poison(hdr);
324 /* If we've exceeded the maximum backlog, clear it up */
Elliott Hughes1728b232014-05-14 10:02:03 -0700325 while (backlog_num > g_malloc_debug_backlog) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700326 hdr_t* gone = backlog_tail;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700327 del_from_backlog_locked(gone);
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700328 g_malloc_dispatch->free(gone->base);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700329 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700330}
331
Christopher Ferrisa4037802014-06-09 19:14:11 -0700332extern "C" void* chk_malloc(size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700333// log_message("%s: %s\n", __FILE__, __FUNCTION__);
334
Christopher Ferrisa4037802014-06-09 19:14:11 -0700335 size_t size = sizeof(hdr_t) + bytes + sizeof(ftr_t);
336 if (size < bytes) { // Overflow
337 errno = ENOMEM;
338 return NULL;
339 }
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700340 hdr_t* hdr = static_cast<hdr_t*>(g_malloc_dispatch->malloc(size));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700341 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700342 hdr->base = hdr;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700343 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700344 add(hdr, bytes);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700345 return user(hdr);
346 }
347 return NULL;
348}
349
Christopher Ferris885f3b92013-05-21 17:48:01 -0700350extern "C" void* chk_memalign(size_t alignment, size_t bytes) {
351 if (alignment <= MALLOC_ALIGNMENT) {
352 return chk_malloc(bytes);
353 }
354
355 // Make the alignment a power of two.
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700356 if (!powerof2(alignment)) {
357 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700358 }
359
360 // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
361 // we will align by at least MALLOC_ALIGNMENT bytes
362 // and at most alignment-MALLOC_ALIGNMENT bytes
363 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
364 if (size < bytes) { // Overflow.
365 return NULL;
366 }
367
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700368 void* base = g_malloc_dispatch->malloc(sizeof(hdr_t) + size + sizeof(ftr_t));
Christopher Ferris885f3b92013-05-21 17:48:01 -0700369 if (base != NULL) {
370 // Check that the actual pointer that will be returned is aligned
371 // properly.
372 uintptr_t ptr = reinterpret_cast<uintptr_t>(user(reinterpret_cast<hdr_t*>(base)));
373 if ((ptr % alignment) != 0) {
374 // Align the pointer.
375 ptr += ((-ptr) % alignment);
376 }
377
378 hdr_t* hdr = meta(reinterpret_cast<void*>(ptr));
379 hdr->base = base;
380 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
381 add(hdr, bytes);
382 return user(hdr);
383 }
384 return base;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700385}
386
Christopher Ferris885f3b92013-05-21 17:48:01 -0700387extern "C" void chk_free(void* ptr) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700388// log_message("%s: %s\n", __FILE__, __FUNCTION__);
389
390 if (!ptr) /* ignore free(NULL) */
391 return;
392
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700393 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700394
395 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800396 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800397 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700398 if (hdr->tag == BACKLOG_TAG) {
399 log_message("+++ ALLOCATION %p SIZE %d BYTES MULTIPLY FREED!\n",
400 user(hdr), hdr->size);
401 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
402 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800403 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700404 /* hdr->freed_bt_depth should be nonzero here */
405 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
406 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800407 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700408 log_message("+++ ALLOCATION %p SIZE %d NOW BEING FREED HERE:\n",
409 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800410 log_backtrace(bt, depth);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700411 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700412 log_message("+++ ALLOCATION %p IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
413 user(hdr));
Elliott Hughes35b621c2013-01-28 16:27:36 -0800414 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700415 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700416 } else {
Elliott Hughes35b621c2013-01-28 16:27:36 -0800417 hdr->freed_bt_depth = get_backtrace(hdr->freed_bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700418 add_to_backlog(hdr);
419 }
420}
421
Christopher Ferrisa4037802014-06-09 19:14:11 -0700422extern "C" void* chk_realloc(void* ptr, size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700423// log_message("%s: %s\n", __FILE__, __FUNCTION__);
424
Elliott Hughese7e274b2012-10-12 17:05:05 -0700425 if (!ptr) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700426 return chk_malloc(bytes);
Elliott Hughese7e274b2012-10-12 17:05:05 -0700427 }
428
429#ifdef REALLOC_ZERO_BYTES_FREE
Christopher Ferrisa4037802014-06-09 19:14:11 -0700430 if (!bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700431 chk_free(ptr);
432 return NULL;
433 }
Elliott Hughese7e274b2012-10-12 17:05:05 -0700434#endif
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700435
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700436 hdr_t* hdr = meta(ptr);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700437
438 if (del(hdr) < 0) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800439 uintptr_t bt[MAX_BACKTRACE_DEPTH];
Elliott Hughes35b621c2013-01-28 16:27:36 -0800440 int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700441 if (hdr->tag == BACKLOG_TAG) {
442 log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n",
Christopher Ferrisa4037802014-06-09 19:14:11 -0700443 user(hdr), bytes, hdr->size);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700444 log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
445 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800446 log_backtrace(hdr->bt, hdr->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700447 /* hdr->freed_bt_depth should be nonzero here */
448 log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
449 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800450 log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700451 log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n",
452 user(hdr), hdr->size);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800453 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700454
455 /* We take the memory out of the backlog and fall through so the
456 * reallocation below succeeds. Since we didn't really free it, we
457 * can default to this behavior.
458 */
459 del_from_backlog(hdr);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700460 } else {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700461 log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
Christopher Ferrisa4037802014-06-09 19:14:11 -0700462 user(hdr), bytes);
Elliott Hughes35b621c2013-01-28 16:27:36 -0800463 log_backtrace(bt, depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700464 // just get a whole new allocation and leak the old one
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700465 return g_malloc_dispatch->realloc(0, bytes);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700466 // return realloc(user(hdr), bytes); // assuming it was allocated externally
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700467 }
468 }
469
Christopher Ferrisa4037802014-06-09 19:14:11 -0700470 size_t size = sizeof(hdr_t) + bytes + sizeof(ftr_t);
471 if (size < bytes) { // Overflow
472 errno = ENOMEM;
473 return NULL;
474 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700475 if (hdr->base != hdr) {
476 // An allocation from memalign, so create another allocation and
477 // copy the data out.
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700478 void* newMem = g_malloc_dispatch->malloc(size);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700479 if (newMem == NULL) {
480 return NULL;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700481 }
Christopher Ferrisa4037802014-06-09 19:14:11 -0700482 memcpy(newMem, hdr, sizeof(hdr_t) + hdr->size);
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700483 g_malloc_dispatch->free(hdr->base);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700484 hdr = static_cast<hdr_t*>(newMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700485 } else {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700486 hdr = static_cast<hdr_t*>(g_malloc_dispatch->realloc(hdr, size));
Christopher Ferris885f3b92013-05-21 17:48:01 -0700487 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700488 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700489 hdr->base = hdr;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700490 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700491 add(hdr, bytes);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700492 return user(hdr);
493 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700494 return NULL;
495}
496
Christopher Ferrisa4037802014-06-09 19:14:11 -0700497extern "C" void* chk_calloc(size_t nmemb, size_t bytes) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700498// log_message("%s: %s\n", __FILE__, __FUNCTION__);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700499 size_t total_bytes = nmemb * bytes;
500 size_t size = sizeof(hdr_t) + total_bytes + sizeof(ftr_t);
501 if (size < total_bytes || (nmemb && SIZE_MAX / nmemb < bytes)) { // Overflow
502 errno = ENOMEM;
503 return NULL;
504 }
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700505 hdr_t* hdr = static_cast<hdr_t*>(g_malloc_dispatch->calloc(1, size));
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700506 if (hdr) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700507 hdr->base = hdr;
Elliott Hughes1e980b62013-01-17 18:36:06 -0800508 hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700509 add(hdr, total_bytes);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700510 return user(hdr);
511 }
512 return NULL;
513}
514
Christopher Ferris885f3b92013-05-21 17:48:01 -0700515extern "C" size_t chk_malloc_usable_size(const void* ptr) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700516 // malloc_usable_size returns 0 for NULL and unknown blocks.
Christopher Ferris885f3b92013-05-21 17:48:01 -0700517 if (ptr == NULL)
518 return 0;
519
520 const hdr_t* hdr = const_meta(ptr);
521
522 // The sentinel tail is written just after the request block bytes
523 // so there is no extra room we can report here.
524 return hdr->size;
525}
526
Christopher Ferrisa4037802014-06-09 19:14:11 -0700527extern "C" struct mallinfo chk_mallinfo() {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700528 return g_malloc_dispatch->mallinfo();
Christopher Ferrisa4037802014-06-09 19:14:11 -0700529}
530
531extern "C" int chk_posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700532 if (!powerof2(alignment)) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700533 return EINVAL;
534 }
535 int saved_errno = errno;
536 *memptr = chk_memalign(alignment, size);
537 errno = saved_errno;
538 return (*memptr != NULL) ? 0 : ENOMEM;
539}
540
541extern "C" void* chk_pvalloc(size_t bytes) {
Elliott Hughes91570ce2014-07-10 12:34:23 -0700542 size_t pagesize = getpagesize();
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700543 size_t size = BIONIC_ALIGN(bytes, pagesize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700544 if (size < bytes) { // Overflow
545 return NULL;
546 }
547 return chk_memalign(pagesize, size);
548}
549
550extern "C" void* chk_valloc(size_t size) {
Elliott Hughes91570ce2014-07-10 12:34:23 -0700551 return chk_memalign(getpagesize(), size);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700552}
553
Elliott Hughes9c818922013-02-01 17:07:40 -0800554static void ReportMemoryLeaks() {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800555 // Use /proc/self/exe link to obtain the program name for logging
556 // purposes. If it's not available, we set it to "<unknown>".
557 char exe[PATH_MAX];
558 int count;
559 if ((count = readlink("/proc/self/exe", exe, sizeof(exe) - 1)) == -1) {
560 strlcpy(exe, "<unknown>", sizeof(exe));
561 } else {
562 exe[count] = '\0';
563 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700564
Elliott Hughes1728b232014-05-14 10:02:03 -0700565 if (g_allocated_block_count == 0) {
Elliott Hughes1d12d572013-01-30 11:38:26 -0800566 log_message("+++ %s did not leak", exe);
567 return;
568 }
569
Elliott Hughes239e7a02013-01-25 17:13:45 -0800570 size_t index = 1;
Elliott Hughes1728b232014-05-14 10:02:03 -0700571 const size_t total = g_allocated_block_count;
Elliott Hughes239e7a02013-01-25 17:13:45 -0800572 while (head != NULL) {
573 int safe;
574 hdr_t* block = head;
575 log_message("+++ %s leaked block of size %d at %p (leak %d of %d)",
576 exe, block->size, user(block), index++, total);
577 if (del_leak(block, &safe)) {
578 /* safe == 1, because the allocation is valid */
Elliott Hughes35b621c2013-01-28 16:27:36 -0800579 log_backtrace(block->bt, block->bt_depth);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700580 }
Elliott Hughes239e7a02013-01-25 17:13:45 -0800581 }
582
583 while (backlog_head != NULL) {
584 del_from_backlog(backlog_tail);
585 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700586}
587
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700588extern "C" bool malloc_debug_initialize(HashTable* hash_table, const MallocDebug* malloc_dispatch) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700589 g_hash_table = hash_table;
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700590 g_malloc_dispatch = malloc_dispatch;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700591
592 char debug_backlog[PROP_VALUE_MAX];
593 if (__system_property_get("libc.debug.malloc.backlog", debug_backlog)) {
594 g_malloc_debug_backlog = atoi(debug_backlog);
595 info_log("%s: setting backlog length to %d\n", getprogname(), g_malloc_debug_backlog);
596 }
597
Elliott Hughes35b621c2013-01-28 16:27:36 -0800598 backtrace_startup();
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700599 return true;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700600}
601
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700602extern "C" void malloc_debug_finalize(int malloc_debug_level) {
603 // We only track leaks at level 10.
604 if (malloc_debug_level == 10) {
605 ReportMemoryLeaks();
606 }
Elliott Hughes35b621c2013-01-28 16:27:36 -0800607 backtrace_shutdown();
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700608}