blob: 1ffbee236d4129850c06cf73b82f55844d6dbdec [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 Hughes32bf43f2015-09-01 16:01:50 -070049#include "debug_backtrace.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 -080058extern int gMallocLeakZygoteChild;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -070059extern HashTable* g_hash_table;
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -070060extern const MallocDebug* g_malloc_dispatch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061
62// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070063// stack trace functions
Andy McFadden39f37452009-07-21 15:25:23 -070064// =============================================================================
65
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066#define GUARD 0x48151642
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080067#define DEBUG 0
68
69// =============================================================================
70// Structures
71// =============================================================================
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070072
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073struct AllocationEntry {
74 HashEntry* entry;
75 uint32_t guard;
Christopher Ferris885f3b92013-05-21 17:48:01 -070076} __attribute__((aligned(MALLOC_ALIGNMENT)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080077
Christopher Ferris885f3b92013-05-21 17:48:01 -070078static inline AllocationEntry* to_header(void* mem) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070079 return reinterpret_cast<AllocationEntry*>(mem) - 1;
80}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081
Christopher Ferris885f3b92013-05-21 17:48:01 -070082static inline const AllocationEntry* const_to_header(const void* mem) {
83 return reinterpret_cast<const AllocationEntry*>(mem) - 1;
84}
85
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086// =============================================================================
87// Hash Table functions
88// =============================================================================
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070089
Elliott Hughes239e7a02013-01-25 17:13:45 -080090static uint32_t get_hash(uintptr_t* backtrace, size_t numEntries) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091 if (backtrace == NULL) return 0;
92
93 int hash = 0;
94 size_t i;
95 for (i = 0 ; i < numEntries ; i++) {
96 hash = (hash * 33) + (backtrace[i] >> 2);
97 }
98
99 return hash;
100}
101
102static HashEntry* find_entry(HashTable* table, int slot,
Elliott Hughes239e7a02013-01-25 17:13:45 -0800103 uintptr_t* backtrace, size_t numEntries, size_t size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800104 HashEntry* entry = table->slots[slot];
105 while (entry != NULL) {
106 //debug_log("backtrace: %p, entry: %p entry->backtrace: %p\n",
107 // backtrace, entry, (entry != NULL) ? entry->backtrace : NULL);
108 /*
109 * See if the entry matches exactly. We compare the "size" field,
110 * including the flag bits.
111 */
112 if (entry->size == size && entry->numEntries == numEntries &&
Elliott Hughes239e7a02013-01-25 17:13:45 -0800113 !memcmp(backtrace, entry->backtrace, numEntries * sizeof(uintptr_t))) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114 return entry;
115 }
116
117 entry = entry->next;
118 }
119
120 return NULL;
121}
122
Elliott Hughes239e7a02013-01-25 17:13:45 -0800123static HashEntry* record_backtrace(uintptr_t* backtrace, size_t numEntries, size_t size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124 size_t hash = get_hash(backtrace, numEntries);
125 size_t slot = hash % HASHTABLE_SIZE;
126
127 if (size & SIZE_FLAG_MASK) {
128 debug_log("malloc_debug: allocation %zx exceeds bit width\n", size);
129 abort();
130 }
131
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700132 if (gMallocLeakZygoteChild) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800133 size |= SIZE_FLAG_ZYGOTE_CHILD;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700134 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135
Christopher Ferris3a40a002015-07-11 22:47:06 -0700136 // Keep the lock held for as little time as possible to prevent deadlocks.
137 ScopedPthreadMutexLocker locker(&g_hash_table->lock);
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700138 HashEntry* entry = find_entry(g_hash_table, slot, backtrace, numEntries, size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800139 if (entry != NULL) {
140 entry->allocations++;
141 } else {
142 // create a new entry
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700143 entry = static_cast<HashEntry*>(g_malloc_dispatch->malloc(sizeof(HashEntry) + numEntries*sizeof(uintptr_t)));
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700144 if (!entry) {
André Goddard Rosa5751c542010-02-05 16:03:09 -0200145 return NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700146 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800147 entry->allocations = 1;
148 entry->slot = slot;
149 entry->prev = NULL;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700150 entry->next = g_hash_table->slots[slot];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800151 entry->numEntries = numEntries;
152 entry->size = size;
153
Elliott Hughes239e7a02013-01-25 17:13:45 -0800154 memcpy(entry->backtrace, backtrace, numEntries * sizeof(uintptr_t));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700156 g_hash_table->slots[slot] = entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157
158 if (entry->next != NULL) {
159 entry->next->prev = entry;
160 }
161
162 // we just added an entry, increase the size of the hashtable
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700163 g_hash_table->count++;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800164 }
165
166 return entry;
167}
168
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700169static int is_valid_entry(HashEntry* entry) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700170 if (entry != NULL) {
171 for (size_t i = 0; i < HASHTABLE_SIZE; ++i) {
172 HashEntry* e1 = g_hash_table->slots[i];
173 while (e1 != NULL) {
174 if (e1 == entry) {
175 return 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700177 e1 = e1->next;
178 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800179 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700180 }
181 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182}
183
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700184static void remove_entry(HashEntry* entry) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700185 HashEntry* prev = entry->prev;
186 HashEntry* next = entry->next;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800187
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700188 if (prev != NULL) entry->prev->next = next;
189 if (next != NULL) entry->next->prev = prev;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700191 if (prev == NULL) {
192 // we are the head of the list. set the head to be next
193 g_hash_table->slots[entry->slot] = entry->next;
194 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800195
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700196 // we just removed and entry, decrease the size of the hashtable
197 g_hash_table->count--;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800198}
199
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700201// malloc fill functions
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800202// =============================================================================
203
204#define CHK_FILL_FREE 0xef
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700205#define CHK_SENTINEL_VALUE 0xeb
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800206
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700207extern "C" void* fill_calloc(size_t n_elements, size_t elem_size) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700208 return g_malloc_dispatch->calloc(n_elements, elem_size);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700209}
210
211extern "C" void* fill_malloc(size_t bytes) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700212 void* buffer = g_malloc_dispatch->malloc(bytes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800213 if (buffer) {
214 memset(buffer, CHK_SENTINEL_VALUE, bytes);
215 }
216 return buffer;
217}
218
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700219extern "C" void fill_free(void* mem) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700220 size_t bytes = g_malloc_dispatch->malloc_usable_size(mem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800221 memset(mem, CHK_FILL_FREE, bytes);
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700222 g_malloc_dispatch->free(mem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800223}
224
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700225extern "C" void* fill_realloc(void* mem, size_t bytes) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700226 size_t oldSize = g_malloc_dispatch->malloc_usable_size(mem);
227 void* newMem = g_malloc_dispatch->realloc(mem, bytes);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700228 if (newMem) {
229 // If this is larger than before, fill the extra with our pattern.
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700230 size_t newSize = g_malloc_dispatch->malloc_usable_size(newMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700231 if (newSize > oldSize) {
232 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(newMem)+oldSize), CHK_FILL_FREE, newSize-oldSize);
233 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800234 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700235 return newMem;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800236}
237
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700238extern "C" void* fill_memalign(size_t alignment, size_t bytes) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700239 void* buffer = g_malloc_dispatch->memalign(alignment, bytes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240 if (buffer) {
241 memset(buffer, CHK_SENTINEL_VALUE, bytes);
242 }
243 return buffer;
244}
245
Christopher Ferris885f3b92013-05-21 17:48:01 -0700246extern "C" size_t fill_malloc_usable_size(const void* mem) {
247 // Since we didn't allocate extra bytes before or after, we can
248 // report the normal usable size here.
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700249 return g_malloc_dispatch->malloc_usable_size(mem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700250}
251
Christopher Ferrisa4037802014-06-09 19:14:11 -0700252extern "C" struct mallinfo fill_mallinfo() {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700253 return g_malloc_dispatch->mallinfo();
Christopher Ferrisa4037802014-06-09 19:14:11 -0700254}
255
256extern "C" int fill_posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700257 if (!powerof2(alignment)) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700258 return EINVAL;
259 }
260 int saved_errno = errno;
261 *memptr = fill_memalign(alignment, size);
262 errno = saved_errno;
263 return (*memptr != NULL) ? 0 : ENOMEM;
264}
265
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700266#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisa4037802014-06-09 19:14:11 -0700267extern "C" void* fill_pvalloc(size_t bytes) {
Elliott Hughes91570ce2014-07-10 12:34:23 -0700268 size_t pagesize = getpagesize();
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700269 size_t size = BIONIC_ALIGN(bytes, pagesize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700270 if (size < bytes) { // Overflow
271 return NULL;
272 }
273 return fill_memalign(pagesize, size);
274}
275
276extern "C" void* fill_valloc(size_t size) {
Elliott Hughes91570ce2014-07-10 12:34:23 -0700277 return fill_memalign(getpagesize(), size);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700278}
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700279#endif
Christopher Ferrisa4037802014-06-09 19:14:11 -0700280
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800281// =============================================================================
282// malloc leak functions
283// =============================================================================
284
Christopher Ferris885f3b92013-05-21 17:48:01 -0700285static uint32_t MEMALIGN_GUARD = 0xA1A41520;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800286
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700287extern "C" void* leak_malloc(size_t bytes) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700288 if (DebugCallsDisabled()) {
289 return g_malloc_dispatch->malloc(bytes);
290 }
291
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800292 // allocate enough space infront of the allocation to store the pointer for
293 // the alloc structure. This will making free'ing the structer really fast!
294
295 // 1. allocate enough memory and include our header
296 // 2. set the base pointer to be right after our header
297
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400298 size_t size = bytes + sizeof(AllocationEntry);
299 if (size < bytes) { // Overflow.
Christopher Ferrisa4037802014-06-09 19:14:11 -0700300 errno = ENOMEM;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400301 return NULL;
302 }
303
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700304 void* base = g_malloc_dispatch->malloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800305 if (base != NULL) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800306 uintptr_t backtrace[BACKTRACE_SIZE];
Christopher Ferris88a1f522014-08-07 16:21:21 -0700307 size_t numEntries = GET_BACKTRACE(backtrace, BACKTRACE_SIZE);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800308
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700309 AllocationEntry* header = reinterpret_cast<AllocationEntry*>(base);
310 header->entry = record_backtrace(backtrace, numEntries, bytes);
311 header->guard = GUARD;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800312
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700313 // now increment base to point to after our header.
314 // this should just work since our header is 8 bytes.
315 base = reinterpret_cast<AllocationEntry*>(base) + 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800316 }
317
318 return base;
319}
320
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700321extern "C" void leak_free(void* mem) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700322 if (DebugCallsDisabled()) {
323 return g_malloc_dispatch->free(mem);
324 }
325
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700326 if (mem == NULL) {
327 return;
328 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800329
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700330 // check the guard to make sure it is valid
331 AllocationEntry* header = to_header(mem);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800332
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700333 if (header->guard != GUARD) {
334 // could be a memaligned block
335 if (header->guard == MEMALIGN_GUARD) {
336 // For memaligned blocks, header->entry points to the memory
337 // allocated through leak_malloc.
338 header = to_header(header->entry);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800339 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700340 }
341
Christopher Ferris3a40a002015-07-11 22:47:06 -0700342 ScopedPthreadMutexLocker locker(&g_hash_table->lock);
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700343 if (header->guard == GUARD || is_valid_entry(header->entry)) {
344 // decrement the allocations
345 HashEntry* entry = header->entry;
346 entry->allocations--;
347 if (entry->allocations <= 0) {
348 remove_entry(entry);
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700349 g_malloc_dispatch->free(entry);
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700350 }
351
352 // now free the memory!
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700353 g_malloc_dispatch->free(header);
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700354 } else {
355 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
356 header->guard, header->entry);
357 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800358}
359
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700360extern "C" void* leak_calloc(size_t n_elements, size_t elem_size) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700361 if (DebugCallsDisabled()) {
362 return g_malloc_dispatch->calloc(n_elements, elem_size);
363 }
364
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700365 // Fail on overflow - just to be safe even though this code runs only
366 // within the debugging C library, not the production one.
367 if (n_elements && SIZE_MAX / n_elements < elem_size) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700368 errno = ENOMEM;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800369 return NULL;
370 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700371 size_t size = n_elements * elem_size;
372 void* ptr = leak_malloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800373 if (ptr != NULL) {
374 memset(ptr, 0, size);
375 }
376 return ptr;
377}
378
Christopher Ferris6d40d342014-08-15 18:42:58 -0700379extern "C" size_t leak_malloc_usable_size(const void* mem) {
380 if (DebugCallsDisabled()) {
381 return g_malloc_dispatch->malloc_usable_size(mem);
382 }
383
384 if (mem == NULL) {
385 return 0;
386 }
387
388 // Check the guard to make sure it is valid.
389 const AllocationEntry* header = const_to_header(mem);
390
391 if (header->guard == MEMALIGN_GUARD) {
392 // If this is a memalign'd pointer, then grab the header from
393 // entry.
394 header = const_to_header(header->entry);
395 } else if (header->guard != GUARD) {
396 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
397 header->guard, header->entry);
398 return 0;
399 }
400
401 size_t ret = g_malloc_dispatch->malloc_usable_size(header);
402 if (ret != 0) {
403 // The usable area starts at 'mem' and stops at 'header+ret'.
404 return reinterpret_cast<uintptr_t>(header) + ret - reinterpret_cast<uintptr_t>(mem);
405 }
406 return 0;
407}
408
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700409extern "C" void* leak_realloc(void* oldMem, size_t bytes) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700410 if (DebugCallsDisabled()) {
411 return g_malloc_dispatch->realloc(oldMem, bytes);
412 }
413
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800414 if (oldMem == NULL) {
415 return leak_malloc(bytes);
416 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700417
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800418 void* newMem = NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700419 AllocationEntry* header = to_header(oldMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700420 if (header->guard == MEMALIGN_GUARD) {
421 // Get the real header.
422 header = to_header(header->entry);
423 } else if (header->guard != GUARD) {
424 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
425 header->guard, header->entry);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700426 errno = ENOMEM;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700427 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800428 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700429
430 newMem = leak_malloc(bytes);
431 if (newMem != NULL) {
Christopher Ferris6d40d342014-08-15 18:42:58 -0700432 size_t oldSize = leak_malloc_usable_size(oldMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700433 size_t copySize = (oldSize <= bytes) ? oldSize : bytes;
434 memcpy(newMem, oldMem, copySize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700435 leak_free(oldMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700436 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700437
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800438 return newMem;
439}
440
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700441extern "C" void* leak_memalign(size_t alignment, size_t bytes) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700442 if (DebugCallsDisabled()) {
443 return g_malloc_dispatch->memalign(alignment, bytes);
444 }
445
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800446 // we can just use malloc
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700447 if (alignment <= MALLOC_ALIGNMENT) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800448 return leak_malloc(bytes);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700449 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800450
451 // need to make sure it's a power of two
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700452 if (!powerof2(alignment)) {
453 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700454 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800455
Elliott Hughese5d5f7f2012-10-09 17:23:09 -0700456 // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800457 // we will align by at least MALLOC_ALIGNMENT bytes
458 // and at most alignment-MALLOC_ALIGNMENT bytes
459 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400460 if (size < bytes) { // Overflow.
461 return NULL;
462 }
463
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800464 void* base = leak_malloc(size);
465 if (base != NULL) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700466 uintptr_t ptr = reinterpret_cast<uintptr_t>(base);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700467 if ((ptr % alignment) == 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800468 return base;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700469 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800470
471 // align the pointer
472 ptr += ((-ptr) % alignment);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800473
Christopher Ferris885f3b92013-05-21 17:48:01 -0700474 // Already allocated enough space for the header. This assumes
475 // that the malloc alignment is at least 8, otherwise, this is
476 // not guaranteed to have the space for the header.
477 AllocationEntry* header = to_header(reinterpret_cast<void*>(ptr));
478 header->guard = MEMALIGN_GUARD;
479 header->entry = reinterpret_cast<HashEntry*>(base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800480
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700481 return reinterpret_cast<void*>(ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800482 }
483 return base;
484}
Christopher Ferris885f3b92013-05-21 17:48:01 -0700485
Christopher Ferrisa4037802014-06-09 19:14:11 -0700486extern "C" struct mallinfo leak_mallinfo() {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700487 return g_malloc_dispatch->mallinfo();
Christopher Ferrisa4037802014-06-09 19:14:11 -0700488}
489
490extern "C" int leak_posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700491 if (DebugCallsDisabled()) {
492 return g_malloc_dispatch->posix_memalign(memptr, alignment, size);
493 }
494
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700495 if (!powerof2(alignment)) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700496 return EINVAL;
497 }
498 int saved_errno = errno;
499 *memptr = leak_memalign(alignment, size);
500 errno = saved_errno;
501 return (*memptr != NULL) ? 0 : ENOMEM;
502}
503
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700504#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisa4037802014-06-09 19:14:11 -0700505extern "C" void* leak_pvalloc(size_t bytes) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700506 if (DebugCallsDisabled()) {
507 return g_malloc_dispatch->pvalloc(bytes);
508 }
509
Elliott Hughes91570ce2014-07-10 12:34:23 -0700510 size_t pagesize = getpagesize();
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700511 size_t size = BIONIC_ALIGN(bytes, pagesize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700512 if (size < bytes) { // Overflow
513 return NULL;
514 }
515 return leak_memalign(pagesize, size);
516}
517
518extern "C" void* leak_valloc(size_t size) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700519 if (DebugCallsDisabled()) {
520 return g_malloc_dispatch->valloc(size);
521 }
522
Elliott Hughes91570ce2014-07-10 12:34:23 -0700523 return leak_memalign(getpagesize(), size);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700524}
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700525#endif