blob: 308d40b9ac5fec93522739cba21e71c1fd594a00 [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;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080065
66// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070067// stack trace functions
Andy McFadden39f37452009-07-21 15:25:23 -070068// =============================================================================
69
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080070#define GUARD 0x48151642
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080071#define DEBUG 0
72
73// =============================================================================
74// Structures
75// =============================================================================
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070076
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080077struct AllocationEntry {
78 HashEntry* entry;
79 uint32_t guard;
Christopher Ferris885f3b92013-05-21 17:48:01 -070080} __attribute__((aligned(MALLOC_ALIGNMENT)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080081
Christopher Ferris885f3b92013-05-21 17:48:01 -070082static inline AllocationEntry* to_header(void* mem) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070083 return reinterpret_cast<AllocationEntry*>(mem) - 1;
84}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080085
Christopher Ferris885f3b92013-05-21 17:48:01 -070086static inline const AllocationEntry* const_to_header(const void* mem) {
87 return reinterpret_cast<const AllocationEntry*>(mem) - 1;
88}
89
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080090// =============================================================================
91// Hash Table functions
92// =============================================================================
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070093
Elliott Hughes239e7a02013-01-25 17:13:45 -080094static uint32_t get_hash(uintptr_t* backtrace, size_t numEntries) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095 if (backtrace == NULL) return 0;
96
97 int hash = 0;
98 size_t i;
99 for (i = 0 ; i < numEntries ; i++) {
100 hash = (hash * 33) + (backtrace[i] >> 2);
101 }
102
103 return hash;
104}
105
106static HashEntry* find_entry(HashTable* table, int slot,
Elliott Hughes239e7a02013-01-25 17:13:45 -0800107 uintptr_t* backtrace, size_t numEntries, size_t size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108 HashEntry* entry = table->slots[slot];
109 while (entry != NULL) {
110 //debug_log("backtrace: %p, entry: %p entry->backtrace: %p\n",
111 // backtrace, entry, (entry != NULL) ? entry->backtrace : NULL);
112 /*
113 * See if the entry matches exactly. We compare the "size" field,
114 * including the flag bits.
115 */
116 if (entry->size == size && entry->numEntries == numEntries &&
Elliott Hughes239e7a02013-01-25 17:13:45 -0800117 !memcmp(backtrace, entry->backtrace, numEntries * sizeof(uintptr_t))) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118 return entry;
119 }
120
121 entry = entry->next;
122 }
123
124 return NULL;
125}
126
Elliott Hughes239e7a02013-01-25 17:13:45 -0800127static HashEntry* record_backtrace(uintptr_t* backtrace, size_t numEntries, size_t size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128 size_t hash = get_hash(backtrace, numEntries);
129 size_t slot = hash % HASHTABLE_SIZE;
130
131 if (size & SIZE_FLAG_MASK) {
132 debug_log("malloc_debug: allocation %zx exceeds bit width\n", size);
133 abort();
134 }
135
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700136 if (gMallocLeakZygoteChild) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800137 size |= SIZE_FLAG_ZYGOTE_CHILD;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700138 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800139
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700140 HashEntry* entry = find_entry(g_hash_table, slot, backtrace, numEntries, size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800141
142 if (entry != NULL) {
143 entry->allocations++;
144 } else {
145 // create a new entry
Christopher Ferris72bbd422014-05-08 11:14:03 -0700146 entry = static_cast<HashEntry*>(Malloc(malloc)(sizeof(HashEntry) + numEntries*sizeof(uintptr_t)));
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700147 if (!entry) {
André Goddard Rosa5751c542010-02-05 16:03:09 -0200148 return NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700149 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800150 entry->allocations = 1;
151 entry->slot = slot;
152 entry->prev = NULL;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700153 entry->next = g_hash_table->slots[slot];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800154 entry->numEntries = numEntries;
155 entry->size = size;
156
Elliott Hughes239e7a02013-01-25 17:13:45 -0800157 memcpy(entry->backtrace, backtrace, numEntries * sizeof(uintptr_t));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800158
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700159 g_hash_table->slots[slot] = entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800160
161 if (entry->next != NULL) {
162 entry->next->prev = entry;
163 }
164
165 // we just added an entry, increase the size of the hashtable
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700166 g_hash_table->count++;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167 }
168
169 return entry;
170}
171
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700172static int is_valid_entry(HashEntry* entry) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700173 if (entry != NULL) {
174 for (size_t i = 0; i < HASHTABLE_SIZE; ++i) {
175 HashEntry* e1 = g_hash_table->slots[i];
176 while (e1 != NULL) {
177 if (e1 == entry) {
178 return 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800179 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700180 e1 = e1->next;
181 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700183 }
184 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800185}
186
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700187static void remove_entry(HashEntry* entry) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700188 HashEntry* prev = entry->prev;
189 HashEntry* next = entry->next;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800190
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700191 if (prev != NULL) entry->prev->next = next;
192 if (next != NULL) entry->next->prev = prev;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800193
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700194 if (prev == NULL) {
195 // we are the head of the list. set the head to be next
196 g_hash_table->slots[entry->slot] = entry->next;
197 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800198
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700199 // we just removed and entry, decrease the size of the hashtable
200 g_hash_table->count--;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201}
202
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700204// malloc fill functions
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800205// =============================================================================
206
207#define CHK_FILL_FREE 0xef
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700208#define CHK_SENTINEL_VALUE 0xeb
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800209
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700210extern "C" void* fill_calloc(size_t n_elements, size_t elem_size) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700211 return Malloc(calloc)(n_elements, elem_size);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700212}
213
214extern "C" void* fill_malloc(size_t bytes) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700215 void* buffer = Malloc(malloc)(bytes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800216 if (buffer) {
217 memset(buffer, CHK_SENTINEL_VALUE, bytes);
218 }
219 return buffer;
220}
221
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700222extern "C" void fill_free(void* mem) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700223 size_t bytes = Malloc(malloc_usable_size)(mem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800224 memset(mem, CHK_FILL_FREE, bytes);
Christopher Ferris72bbd422014-05-08 11:14:03 -0700225 Malloc(free)(mem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800226}
227
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700228extern "C" void* fill_realloc(void* mem, size_t bytes) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700229 size_t oldSize = Malloc(malloc_usable_size)(mem);
230 void* newMem = Malloc(realloc)(mem, bytes);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700231 if (newMem) {
232 // If this is larger than before, fill the extra with our pattern.
Christopher Ferris72bbd422014-05-08 11:14:03 -0700233 size_t newSize = Malloc(malloc_usable_size)(newMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700234 if (newSize > oldSize) {
235 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(newMem)+oldSize), CHK_FILL_FREE, newSize-oldSize);
236 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800237 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700238 return newMem;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800239}
240
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700241extern "C" void* fill_memalign(size_t alignment, size_t bytes) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700242 void* buffer = Malloc(memalign)(alignment, bytes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800243 if (buffer) {
244 memset(buffer, CHK_SENTINEL_VALUE, bytes);
245 }
246 return buffer;
247}
248
Christopher Ferris885f3b92013-05-21 17:48:01 -0700249extern "C" size_t fill_malloc_usable_size(const void* mem) {
250 // Since we didn't allocate extra bytes before or after, we can
251 // report the normal usable size here.
Christopher Ferris72bbd422014-05-08 11:14:03 -0700252 return Malloc(malloc_usable_size)(mem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700253}
254
Christopher Ferrisa4037802014-06-09 19:14:11 -0700255extern "C" struct mallinfo fill_mallinfo() {
256 return Malloc(mallinfo)();
257}
258
259extern "C" int fill_posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700260 if (!powerof2(alignment)) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700261 return EINVAL;
262 }
263 int saved_errno = errno;
264 *memptr = fill_memalign(alignment, size);
265 errno = saved_errno;
266 return (*memptr != NULL) ? 0 : ENOMEM;
267}
268
269extern "C" void* fill_pvalloc(size_t bytes) {
270 size_t pagesize = sysconf(_SC_PAGESIZE);
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700271 size_t size = BIONIC_ALIGN(bytes, pagesize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700272 if (size < bytes) { // Overflow
273 return NULL;
274 }
275 return fill_memalign(pagesize, size);
276}
277
278extern "C" void* fill_valloc(size_t size) {
279 return fill_memalign(sysconf(_SC_PAGESIZE), size);
280}
281
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800282// =============================================================================
283// malloc leak functions
284// =============================================================================
285
Christopher Ferris885f3b92013-05-21 17:48:01 -0700286static uint32_t MEMALIGN_GUARD = 0xA1A41520;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800287
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700288extern "C" void* leak_malloc(size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800289 // allocate enough space infront of the allocation to store the pointer for
290 // the alloc structure. This will making free'ing the structer really fast!
291
292 // 1. allocate enough memory and include our header
293 // 2. set the base pointer to be right after our header
294
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400295 size_t size = bytes + sizeof(AllocationEntry);
296 if (size < bytes) { // Overflow.
Christopher Ferrisa4037802014-06-09 19:14:11 -0700297 errno = ENOMEM;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400298 return NULL;
299 }
300
Christopher Ferris72bbd422014-05-08 11:14:03 -0700301 void* base = Malloc(malloc)(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800302 if (base != NULL) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700303 ScopedPthreadMutexLocker locker(&g_hash_table->lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800304
Elliott Hughes239e7a02013-01-25 17:13:45 -0800305 uintptr_t backtrace[BACKTRACE_SIZE];
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700306 size_t numEntries = get_backtrace(backtrace, BACKTRACE_SIZE);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800307
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700308 AllocationEntry* header = reinterpret_cast<AllocationEntry*>(base);
309 header->entry = record_backtrace(backtrace, numEntries, bytes);
310 header->guard = GUARD;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800311
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700312 // now increment base to point to after our header.
313 // this should just work since our header is 8 bytes.
314 base = reinterpret_cast<AllocationEntry*>(base) + 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800315 }
316
317 return base;
318}
319
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700320extern "C" void leak_free(void* mem) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700321 if (mem == NULL) {
322 return;
323 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800324
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700325 ScopedPthreadMutexLocker locker(&g_hash_table->lock);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800326
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700327 // check the guard to make sure it is valid
328 AllocationEntry* header = to_header(mem);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800329
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700330 if (header->guard != GUARD) {
331 // could be a memaligned block
332 if (header->guard == MEMALIGN_GUARD) {
333 // For memaligned blocks, header->entry points to the memory
334 // allocated through leak_malloc.
335 header = to_header(header->entry);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800336 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700337 }
338
339 if (header->guard == GUARD || is_valid_entry(header->entry)) {
340 // decrement the allocations
341 HashEntry* entry = header->entry;
342 entry->allocations--;
343 if (entry->allocations <= 0) {
344 remove_entry(entry);
345 Malloc(free)(entry);
346 }
347
348 // now free the memory!
349 Malloc(free)(header);
350 } else {
351 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
352 header->guard, header->entry);
353 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800354}
355
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700356extern "C" void* leak_calloc(size_t n_elements, size_t elem_size) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700357 // Fail on overflow - just to be safe even though this code runs only
358 // within the debugging C library, not the production one.
359 if (n_elements && SIZE_MAX / n_elements < elem_size) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700360 errno = ENOMEM;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800361 return NULL;
362 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700363 size_t size = n_elements * elem_size;
364 void* ptr = leak_malloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800365 if (ptr != NULL) {
366 memset(ptr, 0, size);
367 }
368 return ptr;
369}
370
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700371extern "C" void* leak_realloc(void* oldMem, size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800372 if (oldMem == NULL) {
373 return leak_malloc(bytes);
374 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700375
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800376 void* newMem = NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700377 AllocationEntry* header = to_header(oldMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700378 if (header->guard == MEMALIGN_GUARD) {
379 // Get the real header.
380 header = to_header(header->entry);
381 } else if (header->guard != GUARD) {
382 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
383 header->guard, header->entry);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700384 errno = ENOMEM;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700385 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800386 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700387
388 newMem = leak_malloc(bytes);
389 if (newMem != NULL) {
390 size_t oldSize = header->entry->size & ~SIZE_FLAG_MASK;
391 size_t copySize = (oldSize <= bytes) ? oldSize : bytes;
392 memcpy(newMem, oldMem, copySize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700393 leak_free(oldMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700394 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700395
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800396 return newMem;
397}
398
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700399extern "C" void* leak_memalign(size_t alignment, size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800400 // we can just use malloc
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700401 if (alignment <= MALLOC_ALIGNMENT) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800402 return leak_malloc(bytes);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700403 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800404
405 // need to make sure it's a power of two
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700406 if (!powerof2(alignment)) {
407 alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700408 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800409
Elliott Hughese5d5f7f2012-10-09 17:23:09 -0700410 // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800411 // we will align by at least MALLOC_ALIGNMENT bytes
412 // and at most alignment-MALLOC_ALIGNMENT bytes
413 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400414 if (size < bytes) { // Overflow.
415 return NULL;
416 }
417
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800418 void* base = leak_malloc(size);
419 if (base != NULL) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700420 uintptr_t ptr = reinterpret_cast<uintptr_t>(base);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700421 if ((ptr % alignment) == 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800422 return base;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700423 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800424
425 // align the pointer
426 ptr += ((-ptr) % alignment);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800427
Christopher Ferris885f3b92013-05-21 17:48:01 -0700428 // Already allocated enough space for the header. This assumes
429 // that the malloc alignment is at least 8, otherwise, this is
430 // not guaranteed to have the space for the header.
431 AllocationEntry* header = to_header(reinterpret_cast<void*>(ptr));
432 header->guard = MEMALIGN_GUARD;
433 header->entry = reinterpret_cast<HashEntry*>(base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800434
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700435 return reinterpret_cast<void*>(ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800436 }
437 return base;
438}
Christopher Ferris885f3b92013-05-21 17:48:01 -0700439
440extern "C" size_t leak_malloc_usable_size(const void* mem) {
441 if (mem != NULL) {
442 // Check the guard to make sure it is valid.
443 const AllocationEntry* header = const_to_header((void*)mem);
444
445 if (header->guard == MEMALIGN_GUARD) {
446 // If this is a memalign'd pointer, then grab the header from
447 // entry.
448 header = const_to_header(header->entry);
449 } else if (header->guard != GUARD) {
450 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
451 header->guard, header->entry);
452 return 0;
453 }
454
Christopher Ferris72bbd422014-05-08 11:14:03 -0700455 size_t ret = Malloc(malloc_usable_size)(header);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700456 if (ret != 0) {
457 // The usable area starts at 'mem' and stops at 'header+ret'.
458 return reinterpret_cast<uintptr_t>(header) + ret - reinterpret_cast<uintptr_t>(mem);
459 }
460 }
461 return 0;
462}
Christopher Ferrisa4037802014-06-09 19:14:11 -0700463
464extern "C" struct mallinfo leak_mallinfo() {
465 return Malloc(mallinfo)();
466}
467
468extern "C" int leak_posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700469 if (!powerof2(alignment)) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700470 return EINVAL;
471 }
472 int saved_errno = errno;
473 *memptr = leak_memalign(alignment, size);
474 errno = saved_errno;
475 return (*memptr != NULL) ? 0 : ENOMEM;
476}
477
478extern "C" void* leak_pvalloc(size_t bytes) {
479 size_t pagesize = sysconf(_SC_PAGESIZE);
Christopher Ferris03eebcb2014-06-13 13:57:51 -0700480 size_t size = BIONIC_ALIGN(bytes, pagesize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700481 if (size < bytes) { // Overflow
482 return NULL;
483 }
484 return leak_memalign(pagesize, size);
485}
486
487extern "C" void* leak_valloc(size_t size) {
488 return leak_memalign(sysconf(_SC_PAGESIZE), size);
489}