blob: 27b6714326d76e9e916918d0c015dc534ed2297e [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
2 * Copyright (C) 2008 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 */
Xi Wang7f5aa4f2012-03-14 02:48:39 -040028
Elliott Hughes3b297c42012-10-11 16:08:51 -070029#include <arpa/inet.h>
Xi Wang7f5aa4f2012-03-14 02:48:39 -040030#include <dlfcn.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080031#include <errno.h>
Xi Wang7f5aa4f2012-03-14 02:48:39 -040032#include <fcntl.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080033#include <pthread.h>
Xi Wang7f5aa4f2012-03-14 02:48:39 -040034#include <stdarg.h>
35#include <stddef.h>
36#include <stdint.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080037#include <stdio.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080038#include <stdlib.h>
39#include <string.h>
Christopher Ferris03eebcb2014-06-13 13:57:51 -070040#include <sys/param.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080041#include <sys/select.h>
Xi Wang7f5aa4f2012-03-14 02:48:39 -040042#include <sys/socket.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080043#include <sys/system_properties.h>
Xi Wang7f5aa4f2012-03-14 02:48:39 -040044#include <sys/types.h>
45#include <sys/un.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070046#include <unistd.h>
47#include <unwind.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080048
Elliott Hughes1e980b62013-01-17 18:36:06 -080049#include "debug_stacktrace.h"
Christopher Ferris88a1f522014-08-07 16:21:21 -070050#include "malloc_debug_backtrace.h"
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080051#include "malloc_debug_common.h"
Christopher Ferris861c0ef2014-07-24 17:52:23 -070052#include "malloc_debug_disable.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070053
Christopher Ferris03eebcb2014-06-13 13:57:51 -070054#include "private/bionic_macros.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070055#include "private/libc_logging.h"
56#include "private/ScopedPthreadMutexLocker.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080058// This file should be included into the build only when
59// MALLOC_LEAK_CHECK, or MALLOC_QEMU_INSTRUMENT, or both
60// macros are defined.
61#ifndef MALLOC_LEAK_CHECK
62#error MALLOC_LEAK_CHECK is not defined.
63#endif // !MALLOC_LEAK_CHECK
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080065extern int gMallocLeakZygoteChild;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -070066extern HashTable* g_hash_table;
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -070067extern const MallocDebug* g_malloc_dispatch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068
69// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070070// stack trace functions
Andy McFadden39f37452009-07-21 15:25:23 -070071// =============================================================================
72
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073#define GUARD 0x48151642
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074#define DEBUG 0
75
76// =============================================================================
77// Structures
78// =============================================================================
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070079
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080080struct AllocationEntry {
81 HashEntry* entry;
82 uint32_t guard;
Christopher Ferris885f3b92013-05-21 17:48:01 -070083} __attribute__((aligned(MALLOC_ALIGNMENT)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084
Christopher Ferris885f3b92013-05-21 17:48:01 -070085static inline AllocationEntry* to_header(void* mem) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070086 return reinterpret_cast<AllocationEntry*>(mem) - 1;
87}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080088
Christopher Ferris885f3b92013-05-21 17:48:01 -070089static inline const AllocationEntry* const_to_header(const void* mem) {
90 return reinterpret_cast<const AllocationEntry*>(mem) - 1;
91}
92
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093// =============================================================================
94// Hash Table functions
95// =============================================================================
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070096
Elliott Hughes239e7a02013-01-25 17:13:45 -080097static uint32_t get_hash(uintptr_t* backtrace, size_t numEntries) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080098 if (backtrace == NULL) return 0;
99
100 int hash = 0;
101 size_t i;
102 for (i = 0 ; i < numEntries ; i++) {
103 hash = (hash * 33) + (backtrace[i] >> 2);
104 }
105
106 return hash;
107}
108
109static HashEntry* find_entry(HashTable* table, int slot,
Elliott Hughes239e7a02013-01-25 17:13:45 -0800110 uintptr_t* backtrace, size_t numEntries, size_t size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111 HashEntry* entry = table->slots[slot];
112 while (entry != NULL) {
113 //debug_log("backtrace: %p, entry: %p entry->backtrace: %p\n",
114 // backtrace, entry, (entry != NULL) ? entry->backtrace : NULL);
115 /*
116 * See if the entry matches exactly. We compare the "size" field,
117 * including the flag bits.
118 */
119 if (entry->size == size && entry->numEntries == numEntries &&
Elliott Hughes239e7a02013-01-25 17:13:45 -0800120 !memcmp(backtrace, entry->backtrace, numEntries * sizeof(uintptr_t))) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121 return entry;
122 }
123
124 entry = entry->next;
125 }
126
127 return NULL;
128}
129
Elliott Hughes239e7a02013-01-25 17:13:45 -0800130static HashEntry* record_backtrace(uintptr_t* backtrace, size_t numEntries, size_t size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131 size_t hash = get_hash(backtrace, numEntries);
132 size_t slot = hash % HASHTABLE_SIZE;
133
134 if (size & SIZE_FLAG_MASK) {
135 debug_log("malloc_debug: allocation %zx exceeds bit width\n", size);
136 abort();
137 }
138
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700139 if (gMallocLeakZygoteChild) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800140 size |= SIZE_FLAG_ZYGOTE_CHILD;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700141 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700143 HashEntry* entry = find_entry(g_hash_table, slot, backtrace, numEntries, size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800144
145 if (entry != NULL) {
146 entry->allocations++;
147 } else {
148 // create a new entry
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700149 entry = static_cast<HashEntry*>(g_malloc_dispatch->malloc(sizeof(HashEntry) + numEntries*sizeof(uintptr_t)));
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700150 if (!entry) {
André Goddard Rosa5751c542010-02-05 16:03:09 -0200151 return NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700152 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800153 entry->allocations = 1;
154 entry->slot = slot;
155 entry->prev = NULL;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700156 entry->next = g_hash_table->slots[slot];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157 entry->numEntries = numEntries;
158 entry->size = size;
159
Elliott Hughes239e7a02013-01-25 17:13:45 -0800160 memcpy(entry->backtrace, backtrace, numEntries * sizeof(uintptr_t));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800161
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700162 g_hash_table->slots[slot] = entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800163
164 if (entry->next != NULL) {
165 entry->next->prev = entry;
166 }
167
168 // we just added an entry, increase the size of the hashtable
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700169 g_hash_table->count++;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800170 }
171
172 return entry;
173}
174
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700175static int is_valid_entry(HashEntry* entry) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700176 if (entry != NULL) {
177 for (size_t i = 0; i < HASHTABLE_SIZE; ++i) {
178 HashEntry* e1 = g_hash_table->slots[i];
179 while (e1 != NULL) {
180 if (e1 == entry) {
181 return 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700183 e1 = e1->next;
184 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800185 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700186 }
187 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188}
189
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700190static void remove_entry(HashEntry* entry) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700191 HashEntry* prev = entry->prev;
192 HashEntry* next = entry->next;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800193
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700194 if (prev != NULL) entry->prev->next = next;
195 if (next != NULL) entry->next->prev = prev;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700197 if (prev == NULL) {
198 // we are the head of the list. set the head to be next
199 g_hash_table->slots[entry->slot] = entry->next;
200 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700202 // we just removed and entry, decrease the size of the hashtable
203 g_hash_table->count--;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800204}
205
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800206// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700207// malloc fill functions
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800208// =============================================================================
209
210#define CHK_FILL_FREE 0xef
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700211#define CHK_SENTINEL_VALUE 0xeb
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800212
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700213extern "C" void* fill_calloc(size_t n_elements, size_t elem_size) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700214 return g_malloc_dispatch->calloc(n_elements, elem_size);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700215}
216
217extern "C" void* fill_malloc(size_t bytes) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700218 void* buffer = g_malloc_dispatch->malloc(bytes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800219 if (buffer) {
220 memset(buffer, CHK_SENTINEL_VALUE, bytes);
221 }
222 return buffer;
223}
224
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700225extern "C" void fill_free(void* mem) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700226 size_t bytes = g_malloc_dispatch->malloc_usable_size(mem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800227 memset(mem, CHK_FILL_FREE, bytes);
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700228 g_malloc_dispatch->free(mem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229}
230
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700231extern "C" void* fill_realloc(void* mem, size_t bytes) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700232 size_t oldSize = g_malloc_dispatch->malloc_usable_size(mem);
233 void* newMem = g_malloc_dispatch->realloc(mem, bytes);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700234 if (newMem) {
235 // If this is larger than before, fill the extra with our pattern.
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700236 size_t newSize = g_malloc_dispatch->malloc_usable_size(newMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700237 if (newSize > oldSize) {
238 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(newMem)+oldSize), CHK_FILL_FREE, newSize-oldSize);
239 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700241 return newMem;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800242}
243
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700244extern "C" void* fill_memalign(size_t alignment, size_t bytes) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700245 void* buffer = g_malloc_dispatch->memalign(alignment, bytes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800246 if (buffer) {
247 memset(buffer, CHK_SENTINEL_VALUE, bytes);
248 }
249 return buffer;
250}
251
Christopher Ferris885f3b92013-05-21 17:48:01 -0700252extern "C" size_t fill_malloc_usable_size(const void* mem) {
253 // Since we didn't allocate extra bytes before or after, we can
254 // report the normal usable size here.
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700255 return g_malloc_dispatch->malloc_usable_size(mem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700256}
257
Christopher Ferrisa4037802014-06-09 19:14:11 -0700258extern "C" struct mallinfo fill_mallinfo() {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700259 return g_malloc_dispatch->mallinfo();
Christopher Ferrisa4037802014-06-09 19:14:11 -0700260}
261
262extern "C" int fill_posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700263 if (!powerof2(alignment)) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700264 return EINVAL;
265 }
266 int saved_errno = errno;
267 *memptr = fill_memalign(alignment, size);
268 errno = saved_errno;
269 return (*memptr != NULL) ? 0 : ENOMEM;
270}
271
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700272#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisa4037802014-06-09 19:14:11 -0700273extern "C" void* fill_pvalloc(size_t bytes) {
Elliott Hughes91570ce2014-07-10 12:34:23 -0700274 size_t pagesize = getpagesize();
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700275 size_t size = BIONIC_ALIGN(bytes, pagesize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700276 if (size < bytes) { // Overflow
277 return NULL;
278 }
279 return fill_memalign(pagesize, size);
280}
281
282extern "C" void* fill_valloc(size_t size) {
Elliott Hughes91570ce2014-07-10 12:34:23 -0700283 return fill_memalign(getpagesize(), size);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700284}
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700285#endif
Christopher Ferrisa4037802014-06-09 19:14:11 -0700286
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800287// =============================================================================
288// malloc leak functions
289// =============================================================================
290
Christopher Ferris885f3b92013-05-21 17:48:01 -0700291static uint32_t MEMALIGN_GUARD = 0xA1A41520;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800292
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700293extern "C" void* leak_malloc(size_t bytes) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700294 if (DebugCallsDisabled()) {
295 return g_malloc_dispatch->malloc(bytes);
296 }
297
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800298 // allocate enough space infront of the allocation to store the pointer for
299 // the alloc structure. This will making free'ing the structer really fast!
300
301 // 1. allocate enough memory and include our header
302 // 2. set the base pointer to be right after our header
303
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400304 size_t size = bytes + sizeof(AllocationEntry);
305 if (size < bytes) { // Overflow.
Christopher Ferrisa4037802014-06-09 19:14:11 -0700306 errno = ENOMEM;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400307 return NULL;
308 }
309
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700310 void* base = g_malloc_dispatch->malloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800311 if (base != NULL) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700312 ScopedPthreadMutexLocker locker(&g_hash_table->lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800313
Elliott Hughes239e7a02013-01-25 17:13:45 -0800314 uintptr_t backtrace[BACKTRACE_SIZE];
Christopher Ferris88a1f522014-08-07 16:21:21 -0700315 size_t numEntries = GET_BACKTRACE(backtrace, BACKTRACE_SIZE);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800316
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700317 AllocationEntry* header = reinterpret_cast<AllocationEntry*>(base);
318 header->entry = record_backtrace(backtrace, numEntries, bytes);
319 header->guard = GUARD;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800320
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700321 // now increment base to point to after our header.
322 // this should just work since our header is 8 bytes.
323 base = reinterpret_cast<AllocationEntry*>(base) + 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800324 }
325
326 return base;
327}
328
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700329extern "C" void leak_free(void* mem) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700330 if (DebugCallsDisabled()) {
331 return g_malloc_dispatch->free(mem);
332 }
333
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700334 if (mem == NULL) {
335 return;
336 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800337
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700338 ScopedPthreadMutexLocker locker(&g_hash_table->lock);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800339
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700340 // check the guard to make sure it is valid
341 AllocationEntry* header = to_header(mem);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800342
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700343 if (header->guard != GUARD) {
344 // could be a memaligned block
345 if (header->guard == MEMALIGN_GUARD) {
346 // For memaligned blocks, header->entry points to the memory
347 // allocated through leak_malloc.
348 header = to_header(header->entry);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800349 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700350 }
351
352 if (header->guard == GUARD || is_valid_entry(header->entry)) {
353 // decrement the allocations
354 HashEntry* entry = header->entry;
355 entry->allocations--;
356 if (entry->allocations <= 0) {
357 remove_entry(entry);
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700358 g_malloc_dispatch->free(entry);
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700359 }
360
361 // now free the memory!
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700362 g_malloc_dispatch->free(header);
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700363 } else {
364 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
365 header->guard, header->entry);
366 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800367}
368
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700369extern "C" void* leak_calloc(size_t n_elements, size_t elem_size) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700370 if (DebugCallsDisabled()) {
371 return g_malloc_dispatch->calloc(n_elements, elem_size);
372 }
373
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700374 // Fail on overflow - just to be safe even though this code runs only
375 // within the debugging C library, not the production one.
376 if (n_elements && SIZE_MAX / n_elements < elem_size) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700377 errno = ENOMEM;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800378 return NULL;
379 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700380 size_t size = n_elements * elem_size;
381 void* ptr = leak_malloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800382 if (ptr != NULL) {
383 memset(ptr, 0, size);
384 }
385 return ptr;
386}
387
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700388extern "C" void* leak_realloc(void* oldMem, size_t bytes) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700389 if (DebugCallsDisabled()) {
390 return g_malloc_dispatch->realloc(oldMem, bytes);
391 }
392
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800393 if (oldMem == NULL) {
394 return leak_malloc(bytes);
395 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700396
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800397 void* newMem = NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700398 AllocationEntry* header = to_header(oldMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700399 if (header->guard == MEMALIGN_GUARD) {
400 // Get the real header.
401 header = to_header(header->entry);
402 } else if (header->guard != GUARD) {
403 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
404 header->guard, header->entry);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700405 errno = ENOMEM;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700406 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800407 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700408
409 newMem = leak_malloc(bytes);
410 if (newMem != NULL) {
411 size_t oldSize = header->entry->size & ~SIZE_FLAG_MASK;
412 size_t copySize = (oldSize <= bytes) ? oldSize : bytes;
413 memcpy(newMem, oldMem, copySize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700414 leak_free(oldMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700415 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700416
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800417 return newMem;
418}
419
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700420extern "C" void* leak_memalign(size_t alignment, size_t bytes) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700421 if (DebugCallsDisabled()) {
422 return g_malloc_dispatch->memalign(alignment, bytes);
423 }
424
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800425 // we can just use malloc
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700426 if (alignment <= MALLOC_ALIGNMENT) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800427 return leak_malloc(bytes);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700428 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800429
430 // need to make sure it's a power of two
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700431 if (!powerof2(alignment)) {
432 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700433 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800434
Elliott Hughese5d5f7f2012-10-09 17:23:09 -0700435 // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800436 // we will align by at least MALLOC_ALIGNMENT bytes
437 // and at most alignment-MALLOC_ALIGNMENT bytes
438 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400439 if (size < bytes) { // Overflow.
440 return NULL;
441 }
442
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800443 void* base = leak_malloc(size);
444 if (base != NULL) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700445 uintptr_t ptr = reinterpret_cast<uintptr_t>(base);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700446 if ((ptr % alignment) == 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800447 return base;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700448 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800449
450 // align the pointer
451 ptr += ((-ptr) % alignment);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800452
Christopher Ferris885f3b92013-05-21 17:48:01 -0700453 // Already allocated enough space for the header. This assumes
454 // that the malloc alignment is at least 8, otherwise, this is
455 // not guaranteed to have the space for the header.
456 AllocationEntry* header = to_header(reinterpret_cast<void*>(ptr));
457 header->guard = MEMALIGN_GUARD;
458 header->entry = reinterpret_cast<HashEntry*>(base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800459
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700460 return reinterpret_cast<void*>(ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800461 }
462 return base;
463}
Christopher Ferris885f3b92013-05-21 17:48:01 -0700464
465extern "C" size_t leak_malloc_usable_size(const void* mem) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700466 if (DebugCallsDisabled()) {
467 return g_malloc_dispatch->malloc_usable_size(mem);
468 }
469
Christopher Ferris885f3b92013-05-21 17:48:01 -0700470 if (mem != NULL) {
471 // Check the guard to make sure it is valid.
472 const AllocationEntry* header = const_to_header((void*)mem);
473
474 if (header->guard == MEMALIGN_GUARD) {
475 // If this is a memalign'd pointer, then grab the header from
476 // entry.
477 header = const_to_header(header->entry);
478 } else if (header->guard != GUARD) {
479 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
480 header->guard, header->entry);
481 return 0;
482 }
483
Christopher Ferris5d9e1452014-08-14 12:48:04 -0700484 // TODO: Temporary workaround to avoid a crash b/16874447.
485 return header->entry->size & ~SIZE_FLAG_MASK;
486#if 0
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700487 size_t ret = g_malloc_dispatch->malloc_usable_size(header);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700488 if (ret != 0) {
489 // The usable area starts at 'mem' and stops at 'header+ret'.
490 return reinterpret_cast<uintptr_t>(header) + ret - reinterpret_cast<uintptr_t>(mem);
491 }
Christopher Ferris5d9e1452014-08-14 12:48:04 -0700492#endif
Christopher Ferris885f3b92013-05-21 17:48:01 -0700493 }
494 return 0;
495}
Christopher Ferrisa4037802014-06-09 19:14:11 -0700496
497extern "C" struct mallinfo leak_mallinfo() {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700498 return g_malloc_dispatch->mallinfo();
Christopher Ferrisa4037802014-06-09 19:14:11 -0700499}
500
501extern "C" int leak_posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700502 if (DebugCallsDisabled()) {
503 return g_malloc_dispatch->posix_memalign(memptr, alignment, size);
504 }
505
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700506 if (!powerof2(alignment)) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700507 return EINVAL;
508 }
509 int saved_errno = errno;
510 *memptr = leak_memalign(alignment, size);
511 errno = saved_errno;
512 return (*memptr != NULL) ? 0 : ENOMEM;
513}
514
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700515#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisa4037802014-06-09 19:14:11 -0700516extern "C" void* leak_pvalloc(size_t bytes) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700517 if (DebugCallsDisabled()) {
518 return g_malloc_dispatch->pvalloc(bytes);
519 }
520
Elliott Hughes91570ce2014-07-10 12:34:23 -0700521 size_t pagesize = getpagesize();
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700522 size_t size = BIONIC_ALIGN(bytes, pagesize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700523 if (size < bytes) { // Overflow
524 return NULL;
525 }
526 return leak_memalign(pagesize, size);
527}
528
529extern "C" void* leak_valloc(size_t size) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700530 if (DebugCallsDisabled()) {
531 return g_malloc_dispatch->valloc(size);
532 }
533
Elliott Hughes91570ce2014-07-10 12:34:23 -0700534 return leak_memalign(getpagesize(), size);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700535}
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700536#endif