blob: d9824f09f7b3d2496c6b573d851a7b5f73f9b58d [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"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070051
Christopher Ferris03eebcb2014-06-13 13:57:51 -070052#include "private/bionic_macros.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070053#include "private/libc_logging.h"
54#include "private/ScopedPthreadMutexLocker.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080055
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080056// This file should be included into the build only when
57// MALLOC_LEAK_CHECK, or MALLOC_QEMU_INSTRUMENT, or both
58// macros are defined.
59#ifndef MALLOC_LEAK_CHECK
60#error MALLOC_LEAK_CHECK is not defined.
61#endif // !MALLOC_LEAK_CHECK
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080062
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080063extern int gMallocLeakZygoteChild;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -070064extern HashTable* g_hash_table;
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -070065extern const MallocDebug* g_malloc_dispatch;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080066
67// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070068// stack trace functions
Andy McFadden39f37452009-07-21 15:25:23 -070069// =============================================================================
70
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071#define GUARD 0x48151642
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072#define DEBUG 0
73
74// =============================================================================
75// Structures
76// =============================================================================
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070077
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078struct AllocationEntry {
79 HashEntry* entry;
80 uint32_t guard;
Christopher Ferris885f3b92013-05-21 17:48:01 -070081} __attribute__((aligned(MALLOC_ALIGNMENT)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080082
Christopher Ferris885f3b92013-05-21 17:48:01 -070083static inline AllocationEntry* to_header(void* mem) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070084 return reinterpret_cast<AllocationEntry*>(mem) - 1;
85}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086
Christopher Ferris885f3b92013-05-21 17:48:01 -070087static inline const AllocationEntry* const_to_header(const void* mem) {
88 return reinterpret_cast<const AllocationEntry*>(mem) - 1;
89}
90
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091// =============================================================================
92// Hash Table functions
93// =============================================================================
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070094
Elliott Hughes239e7a02013-01-25 17:13:45 -080095static uint32_t get_hash(uintptr_t* backtrace, size_t numEntries) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080096 if (backtrace == NULL) return 0;
97
98 int hash = 0;
99 size_t i;
100 for (i = 0 ; i < numEntries ; i++) {
101 hash = (hash * 33) + (backtrace[i] >> 2);
102 }
103
104 return hash;
105}
106
107static HashEntry* find_entry(HashTable* table, int slot,
Elliott Hughes239e7a02013-01-25 17:13:45 -0800108 uintptr_t* backtrace, size_t numEntries, size_t size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109 HashEntry* entry = table->slots[slot];
110 while (entry != NULL) {
111 //debug_log("backtrace: %p, entry: %p entry->backtrace: %p\n",
112 // backtrace, entry, (entry != NULL) ? entry->backtrace : NULL);
113 /*
114 * See if the entry matches exactly. We compare the "size" field,
115 * including the flag bits.
116 */
117 if (entry->size == size && entry->numEntries == numEntries &&
Elliott Hughes239e7a02013-01-25 17:13:45 -0800118 !memcmp(backtrace, entry->backtrace, numEntries * sizeof(uintptr_t))) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800119 return entry;
120 }
121
122 entry = entry->next;
123 }
124
125 return NULL;
126}
127
Elliott Hughes239e7a02013-01-25 17:13:45 -0800128static HashEntry* record_backtrace(uintptr_t* backtrace, size_t numEntries, size_t size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800129 size_t hash = get_hash(backtrace, numEntries);
130 size_t slot = hash % HASHTABLE_SIZE;
131
132 if (size & SIZE_FLAG_MASK) {
133 debug_log("malloc_debug: allocation %zx exceeds bit width\n", size);
134 abort();
135 }
136
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700137 if (gMallocLeakZygoteChild) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138 size |= SIZE_FLAG_ZYGOTE_CHILD;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700139 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800140
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700141 HashEntry* entry = find_entry(g_hash_table, slot, backtrace, numEntries, size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142
143 if (entry != NULL) {
144 entry->allocations++;
145 } else {
146 // create a new entry
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700147 entry = static_cast<HashEntry*>(g_malloc_dispatch->malloc(sizeof(HashEntry) + numEntries*sizeof(uintptr_t)));
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700148 if (!entry) {
André Goddard Rosa5751c542010-02-05 16:03:09 -0200149 return NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700150 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800151 entry->allocations = 1;
152 entry->slot = slot;
153 entry->prev = NULL;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700154 entry->next = g_hash_table->slots[slot];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155 entry->numEntries = numEntries;
156 entry->size = size;
157
Elliott Hughes239e7a02013-01-25 17:13:45 -0800158 memcpy(entry->backtrace, backtrace, numEntries * sizeof(uintptr_t));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700160 g_hash_table->slots[slot] = entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800161
162 if (entry->next != NULL) {
163 entry->next->prev = entry;
164 }
165
166 // we just added an entry, increase the size of the hashtable
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700167 g_hash_table->count++;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800168 }
169
170 return entry;
171}
172
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700173static int is_valid_entry(HashEntry* entry) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700174 if (entry != NULL) {
175 for (size_t i = 0; i < HASHTABLE_SIZE; ++i) {
176 HashEntry* e1 = g_hash_table->slots[i];
177 while (e1 != NULL) {
178 if (e1 == entry) {
179 return 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800180 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700181 e1 = e1->next;
182 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700184 }
185 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186}
187
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700188static void remove_entry(HashEntry* entry) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700189 HashEntry* prev = entry->prev;
190 HashEntry* next = entry->next;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800191
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700192 if (prev != NULL) entry->prev->next = next;
193 if (next != NULL) entry->next->prev = prev;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800194
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700195 if (prev == NULL) {
196 // we are the head of the list. set the head to be next
197 g_hash_table->slots[entry->slot] = entry->next;
198 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800199
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700200 // we just removed and entry, decrease the size of the hashtable
201 g_hash_table->count--;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800202}
203
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800204// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700205// malloc fill functions
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800206// =============================================================================
207
208#define CHK_FILL_FREE 0xef
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700209#define CHK_SENTINEL_VALUE 0xeb
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800210
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700211extern "C" void* fill_calloc(size_t n_elements, size_t elem_size) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700212 return g_malloc_dispatch->calloc(n_elements, elem_size);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700213}
214
215extern "C" void* fill_malloc(size_t bytes) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700216 void* buffer = g_malloc_dispatch->malloc(bytes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800217 if (buffer) {
218 memset(buffer, CHK_SENTINEL_VALUE, bytes);
219 }
220 return buffer;
221}
222
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700223extern "C" void fill_free(void* mem) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700224 size_t bytes = g_malloc_dispatch->malloc_usable_size(mem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800225 memset(mem, CHK_FILL_FREE, bytes);
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700226 g_malloc_dispatch->free(mem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800227}
228
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700229extern "C" void* fill_realloc(void* mem, size_t bytes) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700230 size_t oldSize = g_malloc_dispatch->malloc_usable_size(mem);
231 void* newMem = g_malloc_dispatch->realloc(mem, bytes);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700232 if (newMem) {
233 // If this is larger than before, fill the extra with our pattern.
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700234 size_t newSize = g_malloc_dispatch->malloc_usable_size(newMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700235 if (newSize > oldSize) {
236 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(newMem)+oldSize), CHK_FILL_FREE, newSize-oldSize);
237 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800238 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700239 return newMem;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240}
241
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700242extern "C" void* fill_memalign(size_t alignment, size_t bytes) {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700243 void* buffer = g_malloc_dispatch->memalign(alignment, bytes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800244 if (buffer) {
245 memset(buffer, CHK_SENTINEL_VALUE, bytes);
246 }
247 return buffer;
248}
249
Christopher Ferris885f3b92013-05-21 17:48:01 -0700250extern "C" size_t fill_malloc_usable_size(const void* mem) {
251 // Since we didn't allocate extra bytes before or after, we can
252 // report the normal usable size here.
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700253 return g_malloc_dispatch->malloc_usable_size(mem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700254}
255
Christopher Ferrisa4037802014-06-09 19:14:11 -0700256extern "C" struct mallinfo fill_mallinfo() {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700257 return g_malloc_dispatch->mallinfo();
Christopher Ferrisa4037802014-06-09 19:14:11 -0700258}
259
260extern "C" int fill_posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700261 if (!powerof2(alignment)) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700262 return EINVAL;
263 }
264 int saved_errno = errno;
265 *memptr = fill_memalign(alignment, size);
266 errno = saved_errno;
267 return (*memptr != NULL) ? 0 : ENOMEM;
268}
269
270extern "C" void* fill_pvalloc(size_t bytes) {
Elliott Hughes91570ce2014-07-10 12:34:23 -0700271 size_t pagesize = getpagesize();
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700272 size_t size = BIONIC_ALIGN(bytes, pagesize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700273 if (size < bytes) { // Overflow
274 return NULL;
275 }
276 return fill_memalign(pagesize, size);
277}
278
279extern "C" void* fill_valloc(size_t size) {
Elliott Hughes91570ce2014-07-10 12:34:23 -0700280 return fill_memalign(getpagesize(), size);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700281}
282
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800283// =============================================================================
284// malloc leak functions
285// =============================================================================
286
Christopher Ferris885f3b92013-05-21 17:48:01 -0700287static uint32_t MEMALIGN_GUARD = 0xA1A41520;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800288
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700289extern "C" void* leak_malloc(size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800290 // allocate enough space infront of the allocation to store the pointer for
291 // the alloc structure. This will making free'ing the structer really fast!
292
293 // 1. allocate enough memory and include our header
294 // 2. set the base pointer to be right after our header
295
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400296 size_t size = bytes + sizeof(AllocationEntry);
297 if (size < bytes) { // Overflow.
Christopher Ferrisa4037802014-06-09 19:14:11 -0700298 errno = ENOMEM;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400299 return NULL;
300 }
301
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700302 void* base = g_malloc_dispatch->malloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800303 if (base != NULL) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700304 ScopedPthreadMutexLocker locker(&g_hash_table->lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800305
Elliott Hughes239e7a02013-01-25 17:13:45 -0800306 uintptr_t backtrace[BACKTRACE_SIZE];
Elliott Hughesc4d1fec2012-08-28 14:15:04 -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) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700322 if (mem == NULL) {
323 return;
324 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800325
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700326 ScopedPthreadMutexLocker locker(&g_hash_table->lock);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800327
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700328 // check the guard to make sure it is valid
329 AllocationEntry* header = to_header(mem);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800330
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700331 if (header->guard != GUARD) {
332 // could be a memaligned block
333 if (header->guard == MEMALIGN_GUARD) {
334 // For memaligned blocks, header->entry points to the memory
335 // allocated through leak_malloc.
336 header = to_header(header->entry);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800337 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700338 }
339
340 if (header->guard == GUARD || is_valid_entry(header->entry)) {
341 // decrement the allocations
342 HashEntry* entry = header->entry;
343 entry->allocations--;
344 if (entry->allocations <= 0) {
345 remove_entry(entry);
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700346 g_malloc_dispatch->free(entry);
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700347 }
348
349 // now free the memory!
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700350 g_malloc_dispatch->free(header);
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700351 } else {
352 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
353 header->guard, header->entry);
354 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800355}
356
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700357extern "C" void* leak_calloc(size_t n_elements, size_t elem_size) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700358 // Fail on overflow - just to be safe even though this code runs only
359 // within the debugging C library, not the production one.
360 if (n_elements && SIZE_MAX / n_elements < elem_size) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700361 errno = ENOMEM;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800362 return NULL;
363 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700364 size_t size = n_elements * elem_size;
365 void* ptr = leak_malloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800366 if (ptr != NULL) {
367 memset(ptr, 0, size);
368 }
369 return ptr;
370}
371
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700372extern "C" void* leak_realloc(void* oldMem, size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800373 if (oldMem == NULL) {
374 return leak_malloc(bytes);
375 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700376
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800377 void* newMem = NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700378 AllocationEntry* header = to_header(oldMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700379 if (header->guard == MEMALIGN_GUARD) {
380 // Get the real header.
381 header = to_header(header->entry);
382 } else if (header->guard != GUARD) {
383 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
384 header->guard, header->entry);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700385 errno = ENOMEM;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700386 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800387 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700388
389 newMem = leak_malloc(bytes);
390 if (newMem != NULL) {
391 size_t oldSize = header->entry->size & ~SIZE_FLAG_MASK;
392 size_t copySize = (oldSize <= bytes) ? oldSize : bytes;
393 memcpy(newMem, oldMem, copySize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700394 leak_free(oldMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700395 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700396
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800397 return newMem;
398}
399
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700400extern "C" void* leak_memalign(size_t alignment, size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800401 // we can just use malloc
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700402 if (alignment <= MALLOC_ALIGNMENT) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800403 return leak_malloc(bytes);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700404 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800405
406 // need to make sure it's a power of two
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700407 if (!powerof2(alignment)) {
408 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700409 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800410
Elliott Hughese5d5f7f2012-10-09 17:23:09 -0700411 // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800412 // we will align by at least MALLOC_ALIGNMENT bytes
413 // and at most alignment-MALLOC_ALIGNMENT bytes
414 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400415 if (size < bytes) { // Overflow.
416 return NULL;
417 }
418
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800419 void* base = leak_malloc(size);
420 if (base != NULL) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700421 uintptr_t ptr = reinterpret_cast<uintptr_t>(base);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700422 if ((ptr % alignment) == 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800423 return base;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700424 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800425
426 // align the pointer
427 ptr += ((-ptr) % alignment);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800428
Christopher Ferris885f3b92013-05-21 17:48:01 -0700429 // Already allocated enough space for the header. This assumes
430 // that the malloc alignment is at least 8, otherwise, this is
431 // not guaranteed to have the space for the header.
432 AllocationEntry* header = to_header(reinterpret_cast<void*>(ptr));
433 header->guard = MEMALIGN_GUARD;
434 header->entry = reinterpret_cast<HashEntry*>(base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800435
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700436 return reinterpret_cast<void*>(ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800437 }
438 return base;
439}
Christopher Ferris885f3b92013-05-21 17:48:01 -0700440
441extern "C" size_t leak_malloc_usable_size(const void* mem) {
442 if (mem != NULL) {
443 // Check the guard to make sure it is valid.
444 const AllocationEntry* header = const_to_header((void*)mem);
445
446 if (header->guard == MEMALIGN_GUARD) {
447 // If this is a memalign'd pointer, then grab the header from
448 // entry.
449 header = const_to_header(header->entry);
450 } else if (header->guard != GUARD) {
451 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
452 header->guard, header->entry);
453 return 0;
454 }
455
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700456 size_t ret = g_malloc_dispatch->malloc_usable_size(header);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700457 if (ret != 0) {
458 // The usable area starts at 'mem' and stops at 'header+ret'.
459 return reinterpret_cast<uintptr_t>(header) + ret - reinterpret_cast<uintptr_t>(mem);
460 }
461 }
462 return 0;
463}
Christopher Ferrisa4037802014-06-09 19:14:11 -0700464
465extern "C" struct mallinfo leak_mallinfo() {
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -0700466 return g_malloc_dispatch->mallinfo();
Christopher Ferrisa4037802014-06-09 19:14:11 -0700467}
468
469extern "C" int leak_posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700470 if (!powerof2(alignment)) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700471 return EINVAL;
472 }
473 int saved_errno = errno;
474 *memptr = leak_memalign(alignment, size);
475 errno = saved_errno;
476 return (*memptr != NULL) ? 0 : ENOMEM;
477}
478
479extern "C" void* leak_pvalloc(size_t bytes) {
Elliott Hughes91570ce2014-07-10 12:34:23 -0700480 size_t pagesize = getpagesize();
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700481 size_t size = BIONIC_ALIGN(bytes, pagesize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700482 if (size < bytes) { // Overflow
483 return NULL;
484 }
485 return leak_memalign(pagesize, size);
486}
487
488extern "C" void* leak_valloc(size_t size) {
Elliott Hughes91570ce2014-07-10 12:34:23 -0700489 return leak_memalign(getpagesize(), size);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700490}