blob: 2cc38ccf5a431f3d1bf556d8e9201175480c5ad5 [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>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040#include <sys/select.h>
Xi Wang7f5aa4f2012-03-14 02:48:39 -040041#include <sys/socket.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042#include <sys/system_properties.h>
Xi Wang7f5aa4f2012-03-14 02:48:39 -040043#include <sys/types.h>
44#include <sys/un.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070045#include <unistd.h>
46#include <unwind.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080047
Elliott Hughes1e980b62013-01-17 18:36:06 -080048#include "debug_stacktrace.h"
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080049#include "malloc_debug_common.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070050
51#include "private/libc_logging.h"
52#include "private/ScopedPthreadMutexLocker.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080053
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080054// This file should be included into the build only when
55// MALLOC_LEAK_CHECK, or MALLOC_QEMU_INSTRUMENT, or both
56// macros are defined.
57#ifndef MALLOC_LEAK_CHECK
58#error MALLOC_LEAK_CHECK is not defined.
59#endif // !MALLOC_LEAK_CHECK
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080060
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080061extern int gMallocLeakZygoteChild;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -070062extern HashTable* g_hash_table;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080063
64// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070065// stack trace functions
Andy McFadden39f37452009-07-21 15:25:23 -070066// =============================================================================
67
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068#define GUARD 0x48151642
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080069#define DEBUG 0
70
71// =============================================================================
72// Structures
73// =============================================================================
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070074
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080075struct AllocationEntry {
76 HashEntry* entry;
77 uint32_t guard;
Christopher Ferris885f3b92013-05-21 17:48:01 -070078} __attribute__((aligned(MALLOC_ALIGNMENT)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080079
Christopher Ferris885f3b92013-05-21 17:48:01 -070080static inline AllocationEntry* to_header(void* mem) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070081 return reinterpret_cast<AllocationEntry*>(mem) - 1;
82}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083
Christopher Ferris885f3b92013-05-21 17:48:01 -070084static inline const AllocationEntry* const_to_header(const void* mem) {
85 return reinterpret_cast<const AllocationEntry*>(mem) - 1;
86}
87
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080088// =============================================================================
89// Hash Table functions
90// =============================================================================
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070091
Elliott Hughes239e7a02013-01-25 17:13:45 -080092static uint32_t get_hash(uintptr_t* backtrace, size_t numEntries) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093 if (backtrace == NULL) return 0;
94
95 int hash = 0;
96 size_t i;
97 for (i = 0 ; i < numEntries ; i++) {
98 hash = (hash * 33) + (backtrace[i] >> 2);
99 }
100
101 return hash;
102}
103
104static HashEntry* find_entry(HashTable* table, int slot,
Elliott Hughes239e7a02013-01-25 17:13:45 -0800105 uintptr_t* backtrace, size_t numEntries, size_t size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106 HashEntry* entry = table->slots[slot];
107 while (entry != NULL) {
108 //debug_log("backtrace: %p, entry: %p entry->backtrace: %p\n",
109 // backtrace, entry, (entry != NULL) ? entry->backtrace : NULL);
110 /*
111 * See if the entry matches exactly. We compare the "size" field,
112 * including the flag bits.
113 */
114 if (entry->size == size && entry->numEntries == numEntries &&
Elliott Hughes239e7a02013-01-25 17:13:45 -0800115 !memcmp(backtrace, entry->backtrace, numEntries * sizeof(uintptr_t))) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116 return entry;
117 }
118
119 entry = entry->next;
120 }
121
122 return NULL;
123}
124
Elliott Hughes239e7a02013-01-25 17:13:45 -0800125static HashEntry* record_backtrace(uintptr_t* backtrace, size_t numEntries, size_t size) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800126 size_t hash = get_hash(backtrace, numEntries);
127 size_t slot = hash % HASHTABLE_SIZE;
128
129 if (size & SIZE_FLAG_MASK) {
130 debug_log("malloc_debug: allocation %zx exceeds bit width\n", size);
131 abort();
132 }
133
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700134 if (gMallocLeakZygoteChild) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135 size |= SIZE_FLAG_ZYGOTE_CHILD;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700136 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800137
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
140 if (entry != NULL) {
141 entry->allocations++;
142 } else {
143 // create a new entry
Christopher Ferris72bbd422014-05-08 11:14:03 -0700144 entry = static_cast<HashEntry*>(Malloc(malloc)(sizeof(HashEntry) + numEntries*sizeof(uintptr_t)));
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700145 if (!entry) {
André Goddard Rosa5751c542010-02-05 16:03:09 -0200146 return NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700147 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800148 entry->allocations = 1;
149 entry->slot = slot;
150 entry->prev = NULL;
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700151 entry->next = g_hash_table->slots[slot];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800152 entry->numEntries = numEntries;
153 entry->size = size;
154
Elliott Hughes239e7a02013-01-25 17:13:45 -0800155 memcpy(entry->backtrace, backtrace, numEntries * sizeof(uintptr_t));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800156
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700157 g_hash_table->slots[slot] = entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800158
159 if (entry->next != NULL) {
160 entry->next->prev = entry;
161 }
162
163 // we just added an entry, increase the size of the hashtable
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700164 g_hash_table->count++;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800165 }
166
167 return entry;
168}
169
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700170static int is_valid_entry(HashEntry* entry) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700171 if (entry != NULL) {
172 for (size_t i = 0; i < HASHTABLE_SIZE; ++i) {
173 HashEntry* e1 = g_hash_table->slots[i];
174 while (e1 != NULL) {
175 if (e1 == entry) {
176 return 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800177 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700178 e1 = e1->next;
179 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800180 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700181 }
182 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183}
184
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700185static void remove_entry(HashEntry* entry) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700186 HashEntry* prev = entry->prev;
187 HashEntry* next = entry->next;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800188
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700189 if (prev != NULL) entry->prev->next = next;
190 if (next != NULL) entry->next->prev = prev;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800191
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700192 if (prev == NULL) {
193 // we are the head of the list. set the head to be next
194 g_hash_table->slots[entry->slot] = entry->next;
195 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700197 // we just removed and entry, decrease the size of the hashtable
198 g_hash_table->count--;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800199}
200
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800201// =============================================================================
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700202// malloc fill functions
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203// =============================================================================
204
205#define CHK_FILL_FREE 0xef
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700206#define CHK_SENTINEL_VALUE 0xeb
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800207
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700208extern "C" void* fill_calloc(size_t n_elements, size_t elem_size) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700209 return Malloc(calloc)(n_elements, elem_size);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700210}
211
212extern "C" void* fill_malloc(size_t bytes) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700213 void* buffer = Malloc(malloc)(bytes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800214 if (buffer) {
215 memset(buffer, CHK_SENTINEL_VALUE, bytes);
216 }
217 return buffer;
218}
219
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700220extern "C" void fill_free(void* mem) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700221 size_t bytes = Malloc(malloc_usable_size)(mem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800222 memset(mem, CHK_FILL_FREE, bytes);
Christopher Ferris72bbd422014-05-08 11:14:03 -0700223 Malloc(free)(mem);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800224}
225
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700226extern "C" void* fill_realloc(void* mem, size_t bytes) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700227 size_t oldSize = Malloc(malloc_usable_size)(mem);
228 void* newMem = Malloc(realloc)(mem, bytes);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700229 if (newMem) {
230 // If this is larger than before, fill the extra with our pattern.
Christopher Ferris72bbd422014-05-08 11:14:03 -0700231 size_t newSize = Malloc(malloc_usable_size)(newMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700232 if (newSize > oldSize) {
233 memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(newMem)+oldSize), CHK_FILL_FREE, newSize-oldSize);
234 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800235 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700236 return newMem;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800237}
238
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700239extern "C" void* fill_memalign(size_t alignment, size_t bytes) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700240 void* buffer = Malloc(memalign)(alignment, bytes);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800241 if (buffer) {
242 memset(buffer, CHK_SENTINEL_VALUE, bytes);
243 }
244 return buffer;
245}
246
Christopher Ferris885f3b92013-05-21 17:48:01 -0700247extern "C" size_t fill_malloc_usable_size(const void* mem) {
248 // Since we didn't allocate extra bytes before or after, we can
249 // report the normal usable size here.
Christopher Ferris72bbd422014-05-08 11:14:03 -0700250 return Malloc(malloc_usable_size)(mem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700251}
252
Christopher Ferrisa4037802014-06-09 19:14:11 -0700253extern "C" struct mallinfo fill_mallinfo() {
254 return Malloc(mallinfo)();
255}
256
257extern "C" int fill_posix_memalign(void** memptr, size_t alignment, size_t size) {
258 if ((alignment & (alignment - 1)) != 0) {
259 return EINVAL;
260 }
261 int saved_errno = errno;
262 *memptr = fill_memalign(alignment, size);
263 errno = saved_errno;
264 return (*memptr != NULL) ? 0 : ENOMEM;
265}
266
267extern "C" void* fill_pvalloc(size_t bytes) {
268 size_t pagesize = sysconf(_SC_PAGESIZE);
269 size_t size = (bytes + pagesize - 1) & ~(pagesize - 1);
270 if (size < bytes) { // Overflow
271 return NULL;
272 }
273 return fill_memalign(pagesize, size);
274}
275
276extern "C" void* fill_valloc(size_t size) {
277 return fill_memalign(sysconf(_SC_PAGESIZE), size);
278}
279
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800280// =============================================================================
281// malloc leak functions
282// =============================================================================
283
Christopher Ferris885f3b92013-05-21 17:48:01 -0700284static uint32_t MEMALIGN_GUARD = 0xA1A41520;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800285
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700286extern "C" void* leak_malloc(size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800287 // allocate enough space infront of the allocation to store the pointer for
288 // the alloc structure. This will making free'ing the structer really fast!
289
290 // 1. allocate enough memory and include our header
291 // 2. set the base pointer to be right after our header
292
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400293 size_t size = bytes + sizeof(AllocationEntry);
294 if (size < bytes) { // Overflow.
Christopher Ferrisa4037802014-06-09 19:14:11 -0700295 errno = ENOMEM;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400296 return NULL;
297 }
298
Christopher Ferris72bbd422014-05-08 11:14:03 -0700299 void* base = Malloc(malloc)(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800300 if (base != NULL) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700301 ScopedPthreadMutexLocker locker(&g_hash_table->lock);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800302
Elliott Hughes239e7a02013-01-25 17:13:45 -0800303 uintptr_t backtrace[BACKTRACE_SIZE];
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700304 size_t numEntries = get_backtrace(backtrace, BACKTRACE_SIZE);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800305
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700306 AllocationEntry* header = reinterpret_cast<AllocationEntry*>(base);
307 header->entry = record_backtrace(backtrace, numEntries, bytes);
308 header->guard = GUARD;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800309
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700310 // now increment base to point to after our header.
311 // this should just work since our header is 8 bytes.
312 base = reinterpret_cast<AllocationEntry*>(base) + 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800313 }
314
315 return base;
316}
317
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700318extern "C" void leak_free(void* mem) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700319 if (mem == NULL) {
320 return;
321 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800322
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700323 ScopedPthreadMutexLocker locker(&g_hash_table->lock);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800324
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700325 // check the guard to make sure it is valid
326 AllocationEntry* header = to_header(mem);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800327
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700328 if (header->guard != GUARD) {
329 // could be a memaligned block
330 if (header->guard == MEMALIGN_GUARD) {
331 // For memaligned blocks, header->entry points to the memory
332 // allocated through leak_malloc.
333 header = to_header(header->entry);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800334 }
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700335 }
336
337 if (header->guard == GUARD || is_valid_entry(header->entry)) {
338 // decrement the allocations
339 HashEntry* entry = header->entry;
340 entry->allocations--;
341 if (entry->allocations <= 0) {
342 remove_entry(entry);
343 Malloc(free)(entry);
344 }
345
346 // now free the memory!
347 Malloc(free)(header);
348 } else {
349 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
350 header->guard, header->entry);
351 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800352}
353
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700354extern "C" void* leak_calloc(size_t n_elements, size_t elem_size) {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700355 // Fail on overflow - just to be safe even though this code runs only
356 // within the debugging C library, not the production one.
357 if (n_elements && SIZE_MAX / n_elements < elem_size) {
Christopher Ferrisa4037802014-06-09 19:14:11 -0700358 errno = ENOMEM;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800359 return NULL;
360 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700361 size_t size = n_elements * elem_size;
362 void* ptr = leak_malloc(size);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800363 if (ptr != NULL) {
364 memset(ptr, 0, size);
365 }
366 return ptr;
367}
368
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700369extern "C" void* leak_realloc(void* oldMem, size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800370 if (oldMem == NULL) {
371 return leak_malloc(bytes);
372 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700373
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800374 void* newMem = NULL;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700375 AllocationEntry* header = to_header(oldMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700376 if (header->guard == MEMALIGN_GUARD) {
377 // Get the real header.
378 header = to_header(header->entry);
379 } else if (header->guard != GUARD) {
380 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
381 header->guard, header->entry);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700382 errno = ENOMEM;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700383 return NULL;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800384 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700385
386 newMem = leak_malloc(bytes);
387 if (newMem != NULL) {
388 size_t oldSize = header->entry->size & ~SIZE_FLAG_MASK;
389 size_t copySize = (oldSize <= bytes) ? oldSize : bytes;
390 memcpy(newMem, oldMem, copySize);
Christopher Ferrisa4037802014-06-09 19:14:11 -0700391 leak_free(oldMem);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700392 }
Christopher Ferris885f3b92013-05-21 17:48:01 -0700393
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800394 return newMem;
395}
396
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700397extern "C" void* leak_memalign(size_t alignment, size_t bytes) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800398 // we can just use malloc
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700399 if (alignment <= MALLOC_ALIGNMENT) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800400 return leak_malloc(bytes);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700401 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800402
403 // need to make sure it's a power of two
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700404 if (alignment & (alignment-1)) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800405 alignment = 1L << (31 - __builtin_clz(alignment));
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700406 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800407
Elliott Hughese5d5f7f2012-10-09 17:23:09 -0700408 // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800409 // we will align by at least MALLOC_ALIGNMENT bytes
410 // and at most alignment-MALLOC_ALIGNMENT bytes
411 size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
Xi Wang7f5aa4f2012-03-14 02:48:39 -0400412 if (size < bytes) { // Overflow.
413 return NULL;
414 }
415
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800416 void* base = leak_malloc(size);
417 if (base != NULL) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700418 uintptr_t ptr = reinterpret_cast<uintptr_t>(base);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700419 if ((ptr % alignment) == 0) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800420 return base;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700421 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800422
423 // align the pointer
424 ptr += ((-ptr) % alignment);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800425
Christopher Ferris885f3b92013-05-21 17:48:01 -0700426 // Already allocated enough space for the header. This assumes
427 // that the malloc alignment is at least 8, otherwise, this is
428 // not guaranteed to have the space for the header.
429 AllocationEntry* header = to_header(reinterpret_cast<void*>(ptr));
430 header->guard = MEMALIGN_GUARD;
431 header->entry = reinterpret_cast<HashEntry*>(base);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800432
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700433 return reinterpret_cast<void*>(ptr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800434 }
435 return base;
436}
Christopher Ferris885f3b92013-05-21 17:48:01 -0700437
438extern "C" size_t leak_malloc_usable_size(const void* mem) {
439 if (mem != NULL) {
440 // Check the guard to make sure it is valid.
441 const AllocationEntry* header = const_to_header((void*)mem);
442
443 if (header->guard == MEMALIGN_GUARD) {
444 // If this is a memalign'd pointer, then grab the header from
445 // entry.
446 header = const_to_header(header->entry);
447 } else if (header->guard != GUARD) {
448 debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n",
449 header->guard, header->entry);
450 return 0;
451 }
452
Christopher Ferris72bbd422014-05-08 11:14:03 -0700453 size_t ret = Malloc(malloc_usable_size)(header);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700454 if (ret != 0) {
455 // The usable area starts at 'mem' and stops at 'header+ret'.
456 return reinterpret_cast<uintptr_t>(header) + ret - reinterpret_cast<uintptr_t>(mem);
457 }
458 }
459 return 0;
460}
Christopher Ferrisa4037802014-06-09 19:14:11 -0700461
462extern "C" struct mallinfo leak_mallinfo() {
463 return Malloc(mallinfo)();
464}
465
466extern "C" int leak_posix_memalign(void** memptr, size_t alignment, size_t size) {
467 if ((alignment & (alignment - 1)) != 0) {
468 return EINVAL;
469 }
470 int saved_errno = errno;
471 *memptr = leak_memalign(alignment, size);
472 errno = saved_errno;
473 return (*memptr != NULL) ? 0 : ENOMEM;
474}
475
476extern "C" void* leak_pvalloc(size_t bytes) {
477 size_t pagesize = sysconf(_SC_PAGESIZE);
478 size_t size = (bytes + pagesize - 1) & ~(pagesize - 1);
479 if (size < bytes) { // Overflow
480 return NULL;
481 }
482 return leak_memalign(pagesize, size);
483}
484
485extern "C" void* leak_valloc(size_t size) {
486 return leak_memalign(sysconf(_SC_PAGESIZE), size);
487}