blob: 7926a1f27f5687a0d7bdc19dc0a931cab361371d [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"
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080050#include "malloc_debug_common.h"
Christopher Ferris861c0ef2014-07-24 17:52:23 -070051#include "malloc_debug_disable.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070052
Christopher Ferris03eebcb2014-06-13 13:57:51 -070053#include "private/bionic_macros.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070054#include "private/libc_logging.h"
55#include "private/ScopedPthreadMutexLocker.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080057// This file should be included into the build only when
58// MALLOC_LEAK_CHECK, or MALLOC_QEMU_INSTRUMENT, or both
59// macros are defined.
60#ifndef MALLOC_LEAK_CHECK
61#error MALLOC_LEAK_CHECK is not defined.
62#endif // !MALLOC_LEAK_CHECK
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080064extern int gMallocLeakZygoteChild;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -070065extern HashTable* g_hash_table;
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -070066extern const MallocDebug* g_malloc_dispatch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080067
68// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070069// stack trace functions
Andy McFadden39f37452009-07-21 15:25:23 -070070// =============================================================================
71
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072#define GUARD 0x48151642
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073#define DEBUG 0
74
75// =============================================================================
76// Structures
77// =============================================================================
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070078
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079struct AllocationEntry {
80 HashEntry* entry;
81 uint32_t guard;
Christopher Ferris885f3b92013-05-21 17:48:01 -070082} __attribute__((aligned(MALLOC_ALIGNMENT)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083
Christopher Ferris885f3b92013-05-21 17:48:01 -070084static inline AllocationEntry* to_header(void* mem) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070085 return reinterpret_cast<AllocationEntry*>(mem) - 1;
86}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087
Christopher Ferris885f3b92013-05-21 17:48:01 -070088static inline const AllocationEntry* const_to_header(const void* mem) {
89 return reinterpret_cast<const AllocationEntry*>(mem) - 1;
90}
91
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092// =============================================================================
93// Hash Table functions
94// =============================================================================
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070095
Elliott Hughes239e7a02013-01-25 17:13:45 -080096static uint32_t get_hash(uintptr_t* backtrace, size_t numEntries) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080097 if (backtrace == NULL) return 0;
98
99 int hash = 0;
100 size_t i;
101 for (i = 0 ; i < numEntries ; i++) {
102 hash = (hash * 33) + (backtrace[i] >> 2);
103 }
104
105 return hash;
106}
107
108static HashEntry* find_entry(HashTable* table, int slot,
Elliott Hughes239e7a02013-01-25 17:13:45 -0800109 uintptr_t* backtrace, size_t numEntries, size_t size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110 HashEntry* entry = table->slots[slot];
111 while (entry != NULL) {
112 //debug_log("backtrace: %p, entry: %p entry->backtrace: %p\n",
113 // backtrace, entry, (entry != NULL) ? entry->backtrace : NULL);
114 /*
115 * See if the entry matches exactly. We compare the "size" field,
116 * including the flag bits.
117 */
118 if (entry->size == size && entry->numEntries == numEntries &&
Elliott Hughes239e7a02013-01-25 17:13:45 -0800119 !memcmp(backtrace, entry->backtrace, numEntries * sizeof(uintptr_t))) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800120 return entry;
121 }
122
123 entry = entry->next;
124 }
125
126 return NULL;
127}
128
Elliott Hughes239e7a02013-01-25 17:13:45 -0800129static HashEntry* record_backtrace(uintptr_t* backtrace, size_t numEntries, size_t size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130 size_t hash = get_hash(backtrace, numEntries);
131 size_t slot = hash % HASHTABLE_SIZE;
132
133 if (size & SIZE_FLAG_MASK) {
134 debug_log("malloc_debug: allocation %zx exceeds bit width\n", size);
135 abort();
136 }
137
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700138 if (gMallocLeakZygoteChild) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800139 size |= SIZE_FLAG_ZYGOTE_CHILD;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700140 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700142 HashEntry* entry = find_entry(g_hash_table, slot, backtrace, numEntries, size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800143
144 if (entry != NULL) {
145 entry->allocations++;
146 } else {
147 // create a new entry
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700148 entry = static_cast<HashEntry*>(g_malloc_dispatch->malloc(sizeof(HashEntry) + numEntries*sizeof(uintptr_t)));
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700149 if (!entry) {
André Goddard Rosa5751c542010-02-05 16:03:09 -0200150 return NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700151 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800152 entry->allocations = 1;
153 entry->slot = slot;
154 entry->prev = NULL;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700155 entry->next = g_hash_table->slots[slot];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800156 entry->numEntries = numEntries;
157 entry->size = size;
158
Elliott Hughes239e7a02013-01-25 17:13:45 -0800159 memcpy(entry->backtrace, backtrace, numEntries * sizeof(uintptr_t));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800160
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700161 g_hash_table->slots[slot] = entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800162
163 if (entry->next != NULL) {
164 entry->next->prev = entry;
165 }
166
167 // we just added an entry, increase the size of the hashtable
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700168 g_hash_table->count++;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800169 }
170
171 return entry;
172}
173
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700174static int is_valid_entry(HashEntry* entry) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700175 if (entry != NULL) {
176 for (size_t i = 0; i < HASHTABLE_SIZE; ++i) {
177 HashEntry* e1 = g_hash_table->slots[i];
178 while (e1 != NULL) {
179 if (e1 == entry) {
180 return 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800181 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700182 e1 = e1->next;
183 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800184 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700185 }
186 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800187}
188
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700189static void remove_entry(HashEntry* entry) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700190 HashEntry* prev = entry->prev;
191 HashEntry* next = entry->next;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800192
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700193 if (prev != NULL) entry->prev->next = next;
194 if (next != NULL) entry->next->prev = prev;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800195
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700196 if (prev == NULL) {
197 // we are the head of the list. set the head to be next
198 g_hash_table->slots[entry->slot] = entry->next;
199 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700201 // we just removed and entry, decrease the size of the hashtable
202 g_hash_table->count--;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203}
204
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800205// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700206// malloc fill functions
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800207// =============================================================================
208
209#define CHK_FILL_FREE 0xef
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700210#define CHK_SENTINEL_VALUE 0xeb
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800211
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700212extern "C" void* fill_calloc(size_t n_elements, size_t elem_size) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700213 return g_malloc_dispatch->calloc(n_elements, elem_size);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700214}
215
216extern "C" void* fill_malloc(size_t bytes) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700217 void* buffer = g_malloc_dispatch->malloc(bytes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218 if (buffer) {
219 memset(buffer, CHK_SENTINEL_VALUE, bytes);
220 }
221 return buffer;
222}
223
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700224extern "C" void fill_free(void* mem) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700225 size_t bytes = g_malloc_dispatch->malloc_usable_size(mem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226 memset(mem, CHK_FILL_FREE, bytes);
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700227 g_malloc_dispatch->free(mem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228}
229
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700230extern "C" void* fill_realloc(void* mem, size_t bytes) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700231 size_t oldSize = g_malloc_dispatch->malloc_usable_size(mem);
232 void* newMem = g_malloc_dispatch->realloc(mem, bytes);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700233 if (newMem) {
234 // If this is larger than before, fill the extra with our pattern.
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700235 size_t newSize = g_malloc_dispatch->malloc_usable_size(newMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700236 if (newSize > oldSize) {
237 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(newMem)+oldSize), CHK_FILL_FREE, newSize-oldSize);
238 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800239 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700240 return newMem;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800241}
242
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700243extern "C" void* fill_memalign(size_t alignment, size_t bytes) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700244 void* buffer = g_malloc_dispatch->memalign(alignment, bytes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800245 if (buffer) {
246 memset(buffer, CHK_SENTINEL_VALUE, bytes);
247 }
248 return buffer;
249}
250
Christopher Ferris885f3b92013-05-21 17:48:01 -0700251extern "C" size_t fill_malloc_usable_size(const void* mem) {
252 // Since we didn't allocate extra bytes before or after, we can
253 // report the normal usable size here.
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700254 return g_malloc_dispatch->malloc_usable_size(mem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700255}
256
Christopher Ferrisa4037802014-06-09 19:14:11 -0700257extern "C" struct mallinfo fill_mallinfo() {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700258 return g_malloc_dispatch->mallinfo();
Christopher Ferrisa4037802014-06-09 19:14:11 -0700259}
260
261extern "C" int fill_posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700262 if (!powerof2(alignment)) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700263 return EINVAL;
264 }
265 int saved_errno = errno;
266 *memptr = fill_memalign(alignment, size);
267 errno = saved_errno;
268 return (*memptr != NULL) ? 0 : ENOMEM;
269}
270
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700271#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisa4037802014-06-09 19:14:11 -0700272extern "C" void* fill_pvalloc(size_t bytes) {
Elliott Hughes91570ce2014-07-10 12:34:23 -0700273 size_t pagesize = getpagesize();
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700274 size_t size = BIONIC_ALIGN(bytes, pagesize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700275 if (size < bytes) { // Overflow
276 return NULL;
277 }
278 return fill_memalign(pagesize, size);
279}
280
281extern "C" void* fill_valloc(size_t size) {
Elliott Hughes91570ce2014-07-10 12:34:23 -0700282 return fill_memalign(getpagesize(), size);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700283}
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700284#endif
Christopher Ferrisa4037802014-06-09 19:14:11 -0700285
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800286// =============================================================================
287// malloc leak functions
288// =============================================================================
289
Christopher Ferris885f3b92013-05-21 17:48:01 -0700290static uint32_t MEMALIGN_GUARD = 0xA1A41520;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800291
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700292extern "C" void* leak_malloc(size_t bytes) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700293 if (DebugCallsDisabled()) {
294 return g_malloc_dispatch->malloc(bytes);
295 }
296
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800297 // allocate enough space infront of the allocation to store the pointer for
298 // the alloc structure. This will making free'ing the structer really fast!
299
300 // 1. allocate enough memory and include our header
301 // 2. set the base pointer to be right after our header
302
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400303 size_t size = bytes + sizeof(AllocationEntry);
304 if (size < bytes) { // Overflow.
Christopher Ferrisa4037802014-06-09 19:14:11 -0700305 errno = ENOMEM;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400306 return NULL;
307 }
308
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700309 void* base = g_malloc_dispatch->malloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800310 if (base != NULL) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700311 ScopedPthreadMutexLocker locker(&g_hash_table->lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800312
Elliott Hughes239e7a02013-01-25 17:13:45 -0800313 uintptr_t backtrace[BACKTRACE_SIZE];
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700314 size_t numEntries = get_backtrace(backtrace, BACKTRACE_SIZE);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800315
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700316 AllocationEntry* header = reinterpret_cast<AllocationEntry*>(base);
317 header->entry = record_backtrace(backtrace, numEntries, bytes);
318 header->guard = GUARD;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800319
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700320 // now increment base to point to after our header.
321 // this should just work since our header is 8 bytes.
322 base = reinterpret_cast<AllocationEntry*>(base) + 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800323 }
324
325 return base;
326}
327
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700328extern "C" void leak_free(void* mem) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700329 if (DebugCallsDisabled()) {
330 return g_malloc_dispatch->free(mem);
331 }
332
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700333 if (mem == NULL) {
334 return;
335 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800336
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700337 ScopedPthreadMutexLocker locker(&g_hash_table->lock);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800338
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700339 // check the guard to make sure it is valid
340 AllocationEntry* header = to_header(mem);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800341
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700342 if (header->guard != GUARD) {
343 // could be a memaligned block
344 if (header->guard == MEMALIGN_GUARD) {
345 // For memaligned blocks, header->entry points to the memory
346 // allocated through leak_malloc.
347 header = to_header(header->entry);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800348 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700349 }
350
351 if (header->guard == GUARD || is_valid_entry(header->entry)) {
352 // decrement the allocations
353 HashEntry* entry = header->entry;
354 entry->allocations--;
355 if (entry->allocations <= 0) {
356 remove_entry(entry);
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700357 g_malloc_dispatch->free(entry);
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700358 }
359
360 // now free the memory!
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700361 g_malloc_dispatch->free(header);
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700362 } else {
363 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
364 header->guard, header->entry);
365 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800366}
367
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700368extern "C" void* leak_calloc(size_t n_elements, size_t elem_size) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700369 if (DebugCallsDisabled()) {
370 return g_malloc_dispatch->calloc(n_elements, elem_size);
371 }
372
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700373 // Fail on overflow - just to be safe even though this code runs only
374 // within the debugging C library, not the production one.
375 if (n_elements && SIZE_MAX / n_elements < elem_size) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700376 errno = ENOMEM;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800377 return NULL;
378 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700379 size_t size = n_elements * elem_size;
380 void* ptr = leak_malloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800381 if (ptr != NULL) {
382 memset(ptr, 0, size);
383 }
384 return ptr;
385}
386
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700387extern "C" void* leak_realloc(void* oldMem, size_t bytes) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700388 if (DebugCallsDisabled()) {
389 return g_malloc_dispatch->realloc(oldMem, bytes);
390 }
391
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800392 if (oldMem == NULL) {
393 return leak_malloc(bytes);
394 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700395
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800396 void* newMem = NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700397 AllocationEntry* header = to_header(oldMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700398 if (header->guard == MEMALIGN_GUARD) {
399 // Get the real header.
400 header = to_header(header->entry);
401 } else if (header->guard != GUARD) {
402 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
403 header->guard, header->entry);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700404 errno = ENOMEM;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700405 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800406 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700407
408 newMem = leak_malloc(bytes);
409 if (newMem != NULL) {
410 size_t oldSize = header->entry->size & ~SIZE_FLAG_MASK;
411 size_t copySize = (oldSize <= bytes) ? oldSize : bytes;
412 memcpy(newMem, oldMem, copySize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700413 leak_free(oldMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700414 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700415
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800416 return newMem;
417}
418
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700419extern "C" void* leak_memalign(size_t alignment, size_t bytes) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700420 if (DebugCallsDisabled()) {
421 return g_malloc_dispatch->memalign(alignment, bytes);
422 }
423
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800424 // we can just use malloc
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700425 if (alignment <= MALLOC_ALIGNMENT) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800426 return leak_malloc(bytes);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700427 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800428
429 // need to make sure it's a power of two
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700430 if (!powerof2(alignment)) {
431 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700432 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800433
Elliott Hughese5d5f7f2012-10-09 17:23:09 -0700434 // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800435 // we will align by at least MALLOC_ALIGNMENT bytes
436 // and at most alignment-MALLOC_ALIGNMENT bytes
437 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400438 if (size < bytes) { // Overflow.
439 return NULL;
440 }
441
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800442 void* base = leak_malloc(size);
443 if (base != NULL) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700444 uintptr_t ptr = reinterpret_cast<uintptr_t>(base);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700445 if ((ptr % alignment) == 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800446 return base;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700447 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800448
449 // align the pointer
450 ptr += ((-ptr) % alignment);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800451
Christopher Ferris885f3b92013-05-21 17:48:01 -0700452 // Already allocated enough space for the header. This assumes
453 // that the malloc alignment is at least 8, otherwise, this is
454 // not guaranteed to have the space for the header.
455 AllocationEntry* header = to_header(reinterpret_cast<void*>(ptr));
456 header->guard = MEMALIGN_GUARD;
457 header->entry = reinterpret_cast<HashEntry*>(base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800458
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700459 return reinterpret_cast<void*>(ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800460 }
461 return base;
462}
Christopher Ferris885f3b92013-05-21 17:48:01 -0700463
464extern "C" size_t leak_malloc_usable_size(const void* mem) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700465 if (DebugCallsDisabled()) {
466 return g_malloc_dispatch->malloc_usable_size(mem);
467 }
468
Christopher Ferris885f3b92013-05-21 17:48:01 -0700469 if (mem != NULL) {
470 // Check the guard to make sure it is valid.
471 const AllocationEntry* header = const_to_header((void*)mem);
472
473 if (header->guard == MEMALIGN_GUARD) {
474 // If this is a memalign'd pointer, then grab the header from
475 // entry.
476 header = const_to_header(header->entry);
477 } else if (header->guard != GUARD) {
478 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
479 header->guard, header->entry);
480 return 0;
481 }
482
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700483 size_t ret = g_malloc_dispatch->malloc_usable_size(header);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700484 if (ret != 0) {
485 // The usable area starts at 'mem' and stops at 'header+ret'.
486 return reinterpret_cast<uintptr_t>(header) + ret - reinterpret_cast<uintptr_t>(mem);
487 }
488 }
489 return 0;
490}
Christopher Ferrisa4037802014-06-09 19:14:11 -0700491
492extern "C" struct mallinfo leak_mallinfo() {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700493 return g_malloc_dispatch->mallinfo();
Christopher Ferrisa4037802014-06-09 19:14:11 -0700494}
495
496extern "C" int leak_posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700497 if (DebugCallsDisabled()) {
498 return g_malloc_dispatch->posix_memalign(memptr, alignment, size);
499 }
500
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700501 if (!powerof2(alignment)) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700502 return EINVAL;
503 }
504 int saved_errno = errno;
505 *memptr = leak_memalign(alignment, size);
506 errno = saved_errno;
507 return (*memptr != NULL) ? 0 : ENOMEM;
508}
509
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700510#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisa4037802014-06-09 19:14:11 -0700511extern "C" void* leak_pvalloc(size_t bytes) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700512 if (DebugCallsDisabled()) {
513 return g_malloc_dispatch->pvalloc(bytes);
514 }
515
Elliott Hughes91570ce2014-07-10 12:34:23 -0700516 size_t pagesize = getpagesize();
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700517 size_t size = BIONIC_ALIGN(bytes, pagesize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700518 if (size < bytes) { // Overflow
519 return NULL;
520 }
521 return leak_memalign(pagesize, size);
522}
523
524extern "C" void* leak_valloc(size_t size) {
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700525 if (DebugCallsDisabled()) {
526 return g_malloc_dispatch->valloc(size);
527 }
528
Elliott Hughes91570ce2014-07-10 12:34:23 -0700529 return leak_memalign(getpagesize(), size);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700530}
Christopher Ferris861c0ef2014-07-24 17:52:23 -0700531#endif