| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 28 |  | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 29 | #include <arpa/inet.h> | 
| Xi Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 30 | #include <dlfcn.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 31 | #include <errno.h> | 
| Xi Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 32 | #include <fcntl.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 33 | #include <pthread.h> | 
| Xi Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 34 | #include <stdarg.h> | 
|  | 35 | #include <stddef.h> | 
|  | 36 | #include <stdint.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 37 | #include <stdio.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 38 | #include <stdlib.h> | 
|  | 39 | #include <string.h> | 
| Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 40 | #include <sys/param.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 41 | #include <sys/select.h> | 
| Xi Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 42 | #include <sys/socket.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 43 | #include <sys/system_properties.h> | 
| Xi Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 44 | #include <sys/types.h> | 
|  | 45 | #include <sys/un.h> | 
| Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 46 | #include <unistd.h> | 
|  | 47 | #include <unwind.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 48 |  | 
| Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 49 | #include "debug_stacktrace.h" | 
| Christopher Ferris | 88a1f52 | 2014-08-07 16:21:21 -0700 | [diff] [blame] | 50 | #include "malloc_debug_backtrace.h" | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 51 | #include "malloc_debug_common.h" | 
| Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 52 | #include "malloc_debug_disable.h" | 
| Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 53 |  | 
| Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 54 | #include "private/bionic_macros.h" | 
| Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 55 | #include "private/libc_logging.h" | 
|  | 56 | #include "private/ScopedPthreadMutexLocker.h" | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 57 |  | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 58 | // This file should be included into the build only when | 
|  | 59 | // MALLOC_LEAK_CHECK, or MALLOC_QEMU_INSTRUMENT, or both | 
|  | 60 | // macros are defined. | 
|  | 61 | #ifndef MALLOC_LEAK_CHECK | 
|  | 62 | #error MALLOC_LEAK_CHECK is not defined. | 
|  | 63 | #endif  // !MALLOC_LEAK_CHECK | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 64 |  | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 65 | extern int gMallocLeakZygoteChild; | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 66 | extern HashTable* g_hash_table; | 
| Christopher Ferris | dda1c6c | 2014-07-09 17:16:07 -0700 | [diff] [blame] | 67 | extern const MallocDebug* g_malloc_dispatch; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 68 |  | 
|  | 69 | // ============================================================================= | 
| Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 70 | // stack trace functions | 
| Andy McFadden | 39f3745 | 2009-07-21 15:25:23 -0700 | [diff] [blame] | 71 | // ============================================================================= | 
|  | 72 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 73 | #define GUARD               0x48151642 | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 74 | #define DEBUG               0 | 
|  | 75 |  | 
|  | 76 | // ============================================================================= | 
|  | 77 | // Structures | 
|  | 78 | // ============================================================================= | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 79 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 80 | struct AllocationEntry { | 
|  | 81 | HashEntry* entry; | 
|  | 82 | uint32_t guard; | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 83 | } __attribute__((aligned(MALLOC_ALIGNMENT))); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 84 |  | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 85 | static inline AllocationEntry* to_header(void* mem) { | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 86 | return reinterpret_cast<AllocationEntry*>(mem) - 1; | 
|  | 87 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 88 |  | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 89 | static inline const AllocationEntry* const_to_header(const void* mem) { | 
|  | 90 | return reinterpret_cast<const AllocationEntry*>(mem) - 1; | 
|  | 91 | } | 
|  | 92 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 93 | // ============================================================================= | 
|  | 94 | // Hash Table functions | 
|  | 95 | // ============================================================================= | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 96 |  | 
| Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 97 | static uint32_t get_hash(uintptr_t* backtrace, size_t numEntries) { | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 98 | if (backtrace == NULL) return 0; | 
|  | 99 |  | 
|  | 100 | int hash = 0; | 
|  | 101 | size_t i; | 
|  | 102 | for (i = 0 ; i < numEntries ; i++) { | 
|  | 103 | hash = (hash * 33) + (backtrace[i] >> 2); | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | return hash; | 
|  | 107 | } | 
|  | 108 |  | 
|  | 109 | static HashEntry* find_entry(HashTable* table, int slot, | 
| Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 110 | uintptr_t* backtrace, size_t numEntries, size_t size) { | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 111 | HashEntry* entry = table->slots[slot]; | 
|  | 112 | while (entry != NULL) { | 
|  | 113 | //debug_log("backtrace: %p, entry: %p entry->backtrace: %p\n", | 
|  | 114 | //        backtrace, entry, (entry != NULL) ? entry->backtrace : NULL); | 
|  | 115 | /* | 
|  | 116 | * See if the entry matches exactly.  We compare the "size" field, | 
|  | 117 | * including the flag bits. | 
|  | 118 | */ | 
|  | 119 | if (entry->size == size && entry->numEntries == numEntries && | 
| Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 120 | !memcmp(backtrace, entry->backtrace, numEntries * sizeof(uintptr_t))) { | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 121 | return entry; | 
|  | 122 | } | 
|  | 123 |  | 
|  | 124 | entry = entry->next; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | return NULL; | 
|  | 128 | } | 
|  | 129 |  | 
| Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 130 | static HashEntry* record_backtrace(uintptr_t* backtrace, size_t numEntries, size_t size) { | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 131 | size_t hash = get_hash(backtrace, numEntries); | 
|  | 132 | size_t slot = hash % HASHTABLE_SIZE; | 
|  | 133 |  | 
|  | 134 | if (size & SIZE_FLAG_MASK) { | 
|  | 135 | debug_log("malloc_debug: allocation %zx exceeds bit width\n", size); | 
|  | 136 | abort(); | 
|  | 137 | } | 
|  | 138 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 139 | if (gMallocLeakZygoteChild) { | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 140 | size |= SIZE_FLAG_ZYGOTE_CHILD; | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 141 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 142 |  | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 143 | HashEntry* entry = find_entry(g_hash_table, slot, backtrace, numEntries, size); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 144 |  | 
|  | 145 | if (entry != NULL) { | 
|  | 146 | entry->allocations++; | 
|  | 147 | } else { | 
|  | 148 | // create a new entry | 
| Christopher Ferris | dda1c6c | 2014-07-09 17:16:07 -0700 | [diff] [blame] | 149 | entry = static_cast<HashEntry*>(g_malloc_dispatch->malloc(sizeof(HashEntry) + numEntries*sizeof(uintptr_t))); | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 150 | if (!entry) { | 
| André Goddard Rosa | 5751c54 | 2010-02-05 16:03:09 -0200 | [diff] [blame] | 151 | return NULL; | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 152 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 153 | entry->allocations = 1; | 
|  | 154 | entry->slot = slot; | 
|  | 155 | entry->prev = NULL; | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 156 | entry->next = g_hash_table->slots[slot]; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 157 | entry->numEntries = numEntries; | 
|  | 158 | entry->size = size; | 
|  | 159 |  | 
| Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 160 | memcpy(entry->backtrace, backtrace, numEntries * sizeof(uintptr_t)); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 161 |  | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 162 | g_hash_table->slots[slot] = entry; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 163 |  | 
|  | 164 | if (entry->next != NULL) { | 
|  | 165 | entry->next->prev = entry; | 
|  | 166 | } | 
|  | 167 |  | 
|  | 168 | // we just added an entry, increase the size of the hashtable | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 169 | g_hash_table->count++; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
|  | 172 | return entry; | 
|  | 173 | } | 
|  | 174 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 175 | static int is_valid_entry(HashEntry* entry) { | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 176 | if (entry != NULL) { | 
|  | 177 | for (size_t i = 0; i < HASHTABLE_SIZE; ++i) { | 
|  | 178 | HashEntry* e1 = g_hash_table->slots[i]; | 
|  | 179 | while (e1 != NULL) { | 
|  | 180 | if (e1 == entry) { | 
|  | 181 | return 1; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 182 | } | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 183 | e1 = e1->next; | 
|  | 184 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 185 | } | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 186 | } | 
|  | 187 | return 0; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 190 | static void remove_entry(HashEntry* entry) { | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 191 | HashEntry* prev = entry->prev; | 
|  | 192 | HashEntry* next = entry->next; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 193 |  | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 194 | if (prev != NULL) entry->prev->next = next; | 
|  | 195 | if (next != NULL) entry->next->prev = prev; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 196 |  | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 197 | if (prev == NULL) { | 
|  | 198 | // we are the head of the list. set the head to be next | 
|  | 199 | g_hash_table->slots[entry->slot] = entry->next; | 
|  | 200 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 201 |  | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 202 | // we just removed and entry, decrease the size of the hashtable | 
|  | 203 | g_hash_table->count--; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 204 | } | 
|  | 205 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 206 | // ============================================================================= | 
| Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 207 | // malloc fill functions | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 208 | // ============================================================================= | 
|  | 209 |  | 
|  | 210 | #define CHK_FILL_FREE           0xef | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 211 | #define CHK_SENTINEL_VALUE      0xeb | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 212 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 213 | extern "C" void* fill_calloc(size_t n_elements, size_t elem_size) { | 
| Christopher Ferris | dda1c6c | 2014-07-09 17:16:07 -0700 | [diff] [blame] | 214 | return g_malloc_dispatch->calloc(n_elements, elem_size); | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 215 | } | 
|  | 216 |  | 
|  | 217 | extern "C" void* fill_malloc(size_t bytes) { | 
| Christopher Ferris | dda1c6c | 2014-07-09 17:16:07 -0700 | [diff] [blame] | 218 | void* buffer = g_malloc_dispatch->malloc(bytes); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 219 | if (buffer) { | 
|  | 220 | memset(buffer, CHK_SENTINEL_VALUE, bytes); | 
|  | 221 | } | 
|  | 222 | return buffer; | 
|  | 223 | } | 
|  | 224 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 225 | extern "C" void fill_free(void* mem) { | 
| Christopher Ferris | dda1c6c | 2014-07-09 17:16:07 -0700 | [diff] [blame] | 226 | size_t bytes = g_malloc_dispatch->malloc_usable_size(mem); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 227 | memset(mem, CHK_FILL_FREE, bytes); | 
| Christopher Ferris | dda1c6c | 2014-07-09 17:16:07 -0700 | [diff] [blame] | 228 | g_malloc_dispatch->free(mem); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 229 | } | 
|  | 230 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 231 | extern "C" void* fill_realloc(void* mem, size_t bytes) { | 
| Christopher Ferris | dda1c6c | 2014-07-09 17:16:07 -0700 | [diff] [blame] | 232 | size_t oldSize = g_malloc_dispatch->malloc_usable_size(mem); | 
|  | 233 | void* newMem = g_malloc_dispatch->realloc(mem, bytes); | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 234 | if (newMem) { | 
|  | 235 | // If this is larger than before, fill the extra with our pattern. | 
| Christopher Ferris | dda1c6c | 2014-07-09 17:16:07 -0700 | [diff] [blame] | 236 | size_t newSize = g_malloc_dispatch->malloc_usable_size(newMem); | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 237 | if (newSize > oldSize) { | 
|  | 238 | memset(reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(newMem)+oldSize), CHK_FILL_FREE, newSize-oldSize); | 
|  | 239 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 240 | } | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 241 | return newMem; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 242 | } | 
|  | 243 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 244 | extern "C" void* fill_memalign(size_t alignment, size_t bytes) { | 
| Christopher Ferris | dda1c6c | 2014-07-09 17:16:07 -0700 | [diff] [blame] | 245 | void* buffer = g_malloc_dispatch->memalign(alignment, bytes); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 246 | if (buffer) { | 
|  | 247 | memset(buffer, CHK_SENTINEL_VALUE, bytes); | 
|  | 248 | } | 
|  | 249 | return buffer; | 
|  | 250 | } | 
|  | 251 |  | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 252 | extern "C" size_t fill_malloc_usable_size(const void* mem) { | 
|  | 253 | // Since we didn't allocate extra bytes before or after, we can | 
|  | 254 | // report the normal usable size here. | 
| Christopher Ferris | dda1c6c | 2014-07-09 17:16:07 -0700 | [diff] [blame] | 255 | return g_malloc_dispatch->malloc_usable_size(mem); | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 256 | } | 
|  | 257 |  | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 258 | extern "C" struct mallinfo fill_mallinfo() { | 
| Christopher Ferris | dda1c6c | 2014-07-09 17:16:07 -0700 | [diff] [blame] | 259 | return g_malloc_dispatch->mallinfo(); | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 260 | } | 
|  | 261 |  | 
|  | 262 | extern "C" int fill_posix_memalign(void** memptr, size_t alignment, size_t size) { | 
| Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 263 | if (!powerof2(alignment)) { | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 264 | return EINVAL; | 
|  | 265 | } | 
|  | 266 | int saved_errno = errno; | 
|  | 267 | *memptr = fill_memalign(alignment, size); | 
|  | 268 | errno = saved_errno; | 
|  | 269 | return (*memptr != NULL) ? 0 : ENOMEM; | 
|  | 270 | } | 
|  | 271 |  | 
| Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 272 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 273 | extern "C" void* fill_pvalloc(size_t bytes) { | 
| Elliott Hughes | 91570ce | 2014-07-10 12:34:23 -0700 | [diff] [blame] | 274 | size_t pagesize = getpagesize(); | 
| Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 275 | size_t size = BIONIC_ALIGN(bytes, pagesize); | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 276 | if (size < bytes) { // Overflow | 
|  | 277 | return NULL; | 
|  | 278 | } | 
|  | 279 | return fill_memalign(pagesize, size); | 
|  | 280 | } | 
|  | 281 |  | 
|  | 282 | extern "C" void* fill_valloc(size_t size) { | 
| Elliott Hughes | 91570ce | 2014-07-10 12:34:23 -0700 | [diff] [blame] | 283 | return fill_memalign(getpagesize(), size); | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 284 | } | 
| Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 285 | #endif | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 286 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 287 | // ============================================================================= | 
|  | 288 | // malloc leak functions | 
|  | 289 | // ============================================================================= | 
|  | 290 |  | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 291 | static uint32_t MEMALIGN_GUARD      = 0xA1A41520; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 292 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 293 | extern "C" void* leak_malloc(size_t bytes) { | 
| Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 294 | if (DebugCallsDisabled()) { | 
|  | 295 | return g_malloc_dispatch->malloc(bytes); | 
|  | 296 | } | 
|  | 297 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 298 | // allocate enough space infront of the allocation to store the pointer for | 
|  | 299 | // the alloc structure. This will making free'ing the structer really fast! | 
|  | 300 |  | 
|  | 301 | // 1. allocate enough memory and include our header | 
|  | 302 | // 2. set the base pointer to be right after our header | 
|  | 303 |  | 
| Xi Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 304 | size_t size = bytes + sizeof(AllocationEntry); | 
|  | 305 | if (size < bytes) { // Overflow. | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 306 | errno = ENOMEM; | 
| Xi Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 307 | return NULL; | 
|  | 308 | } | 
|  | 309 |  | 
| Christopher Ferris | dda1c6c | 2014-07-09 17:16:07 -0700 | [diff] [blame] | 310 | void* base = g_malloc_dispatch->malloc(size); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 311 | if (base != NULL) { | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 312 | ScopedPthreadMutexLocker locker(&g_hash_table->lock); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 313 |  | 
| Elliott Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 314 | uintptr_t backtrace[BACKTRACE_SIZE]; | 
| Christopher Ferris | 88a1f52 | 2014-08-07 16:21:21 -0700 | [diff] [blame] | 315 | size_t numEntries = GET_BACKTRACE(backtrace, BACKTRACE_SIZE); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 316 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 317 | AllocationEntry* header = reinterpret_cast<AllocationEntry*>(base); | 
|  | 318 | header->entry = record_backtrace(backtrace, numEntries, bytes); | 
|  | 319 | header->guard = GUARD; | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 320 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 321 | // now increment base to point to after our header. | 
|  | 322 | // this should just work since our header is 8 bytes. | 
|  | 323 | base = reinterpret_cast<AllocationEntry*>(base) + 1; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 324 | } | 
|  | 325 |  | 
|  | 326 | return base; | 
|  | 327 | } | 
|  | 328 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 329 | extern "C" void leak_free(void* mem) { | 
| Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 330 | if (DebugCallsDisabled()) { | 
|  | 331 | return g_malloc_dispatch->free(mem); | 
|  | 332 | } | 
|  | 333 |  | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 334 | if (mem == NULL) { | 
|  | 335 | return; | 
|  | 336 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 337 |  | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 338 | ScopedPthreadMutexLocker locker(&g_hash_table->lock); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 339 |  | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 340 | // check the guard to make sure it is valid | 
|  | 341 | AllocationEntry* header = to_header(mem); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 342 |  | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 343 | if (header->guard != GUARD) { | 
|  | 344 | // could be a memaligned block | 
|  | 345 | if (header->guard == MEMALIGN_GUARD) { | 
|  | 346 | // For memaligned blocks, header->entry points to the memory | 
|  | 347 | // allocated through leak_malloc. | 
|  | 348 | header = to_header(header->entry); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 349 | } | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 350 | } | 
|  | 351 |  | 
|  | 352 | if (header->guard == GUARD || is_valid_entry(header->entry)) { | 
|  | 353 | // decrement the allocations | 
|  | 354 | HashEntry* entry = header->entry; | 
|  | 355 | entry->allocations--; | 
|  | 356 | if (entry->allocations <= 0) { | 
|  | 357 | remove_entry(entry); | 
| Christopher Ferris | dda1c6c | 2014-07-09 17:16:07 -0700 | [diff] [blame] | 358 | g_malloc_dispatch->free(entry); | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 359 | } | 
|  | 360 |  | 
|  | 361 | // now free the memory! | 
| Christopher Ferris | dda1c6c | 2014-07-09 17:16:07 -0700 | [diff] [blame] | 362 | g_malloc_dispatch->free(header); | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 363 | } else { | 
|  | 364 | debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n", | 
|  | 365 | header->guard, header->entry); | 
|  | 366 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 367 | } | 
|  | 368 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 369 | extern "C" void* leak_calloc(size_t n_elements, size_t elem_size) { | 
| Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 370 | if (DebugCallsDisabled()) { | 
|  | 371 | return g_malloc_dispatch->calloc(n_elements, elem_size); | 
|  | 372 | } | 
|  | 373 |  | 
| Elliott Hughes | 8e52e8f | 2014-06-04 12:07:11 -0700 | [diff] [blame] | 374 | // Fail on overflow - just to be safe even though this code runs only | 
|  | 375 | // within the debugging C library, not the production one. | 
|  | 376 | if (n_elements && SIZE_MAX / n_elements < elem_size) { | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 377 | errno = ENOMEM; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 378 | return NULL; | 
|  | 379 | } | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 380 | size_t size = n_elements * elem_size; | 
|  | 381 | void* ptr  = leak_malloc(size); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 382 | if (ptr != NULL) { | 
|  | 383 | memset(ptr, 0, size); | 
|  | 384 | } | 
|  | 385 | return ptr; | 
|  | 386 | } | 
|  | 387 |  | 
| Christopher Ferris | 6d40d34 | 2014-08-15 18:42:58 -0700 | [diff] [blame] | 388 | extern "C" size_t leak_malloc_usable_size(const void* mem) { | 
|  | 389 | if (DebugCallsDisabled()) { | 
|  | 390 | return g_malloc_dispatch->malloc_usable_size(mem); | 
|  | 391 | } | 
|  | 392 |  | 
|  | 393 | if (mem == NULL) { | 
|  | 394 | return 0; | 
|  | 395 | } | 
|  | 396 |  | 
|  | 397 | // Check the guard to make sure it is valid. | 
|  | 398 | const AllocationEntry* header = const_to_header(mem); | 
|  | 399 |  | 
|  | 400 | if (header->guard == MEMALIGN_GUARD) { | 
|  | 401 | // If this is a memalign'd pointer, then grab the header from | 
|  | 402 | // entry. | 
|  | 403 | header = const_to_header(header->entry); | 
|  | 404 | } else if (header->guard != GUARD) { | 
|  | 405 | debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n", | 
|  | 406 | header->guard, header->entry); | 
|  | 407 | return 0; | 
|  | 408 | } | 
|  | 409 |  | 
|  | 410 | size_t ret = g_malloc_dispatch->malloc_usable_size(header); | 
|  | 411 | if (ret != 0) { | 
|  | 412 | // The usable area starts at 'mem' and stops at 'header+ret'. | 
|  | 413 | return reinterpret_cast<uintptr_t>(header) + ret - reinterpret_cast<uintptr_t>(mem); | 
|  | 414 | } | 
|  | 415 | return 0; | 
|  | 416 | } | 
|  | 417 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 418 | extern "C" void* leak_realloc(void* oldMem, size_t bytes) { | 
| Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 419 | if (DebugCallsDisabled()) { | 
|  | 420 | return g_malloc_dispatch->realloc(oldMem, bytes); | 
|  | 421 | } | 
|  | 422 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 423 | if (oldMem == NULL) { | 
|  | 424 | return leak_malloc(bytes); | 
|  | 425 | } | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 426 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 427 | void* newMem = NULL; | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 428 | AllocationEntry* header = to_header(oldMem); | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 429 | if (header->guard == MEMALIGN_GUARD) { | 
|  | 430 | // Get the real header. | 
|  | 431 | header = to_header(header->entry); | 
|  | 432 | } else if (header->guard != GUARD) { | 
|  | 433 | debug_log("WARNING bad header guard: '0x%x'! and invalid entry: %p\n", | 
|  | 434 | header->guard, header->entry); | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 435 | errno = ENOMEM; | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 436 | return NULL; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 437 | } | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 438 |  | 
|  | 439 | newMem = leak_malloc(bytes); | 
|  | 440 | if (newMem != NULL) { | 
| Christopher Ferris | 6d40d34 | 2014-08-15 18:42:58 -0700 | [diff] [blame] | 441 | size_t oldSize = leak_malloc_usable_size(oldMem); | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 442 | size_t copySize = (oldSize <= bytes) ? oldSize : bytes; | 
|  | 443 | memcpy(newMem, oldMem, copySize); | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 444 | leak_free(oldMem); | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 445 | } | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 446 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 447 | return newMem; | 
|  | 448 | } | 
|  | 449 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 450 | extern "C" void* leak_memalign(size_t alignment, size_t bytes) { | 
| Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 451 | if (DebugCallsDisabled()) { | 
|  | 452 | return g_malloc_dispatch->memalign(alignment, bytes); | 
|  | 453 | } | 
|  | 454 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 455 | // we can just use malloc | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 456 | if (alignment <= MALLOC_ALIGNMENT) { | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 457 | return leak_malloc(bytes); | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 458 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 459 |  | 
|  | 460 | // need to make sure it's a power of two | 
| Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 461 | if (!powerof2(alignment)) { | 
|  | 462 | alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment); | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 463 | } | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 464 |  | 
| Elliott Hughes | e5d5f7f | 2012-10-09 17:23:09 -0700 | [diff] [blame] | 465 | // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 466 | // we will align by at least MALLOC_ALIGNMENT bytes | 
|  | 467 | // and at most alignment-MALLOC_ALIGNMENT bytes | 
|  | 468 | size_t size = (alignment-MALLOC_ALIGNMENT) + bytes; | 
| Xi Wang | 7f5aa4f | 2012-03-14 02:48:39 -0400 | [diff] [blame] | 469 | if (size < bytes) { // Overflow. | 
|  | 470 | return NULL; | 
|  | 471 | } | 
|  | 472 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 473 | void* base = leak_malloc(size); | 
|  | 474 | if (base != NULL) { | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 475 | uintptr_t ptr = reinterpret_cast<uintptr_t>(base); | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 476 | if ((ptr % alignment) == 0) { | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 477 | return base; | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 478 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 479 |  | 
|  | 480 | // align the pointer | 
|  | 481 | ptr += ((-ptr) % alignment); | 
| Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 482 |  | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 483 | // Already allocated enough space for the header. This assumes | 
|  | 484 | // that the malloc alignment is at least 8, otherwise, this is | 
|  | 485 | // not guaranteed to have the space for the header. | 
|  | 486 | AllocationEntry* header = to_header(reinterpret_cast<void*>(ptr)); | 
|  | 487 | header->guard = MEMALIGN_GUARD; | 
|  | 488 | header->entry = reinterpret_cast<HashEntry*>(base); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 489 |  | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 490 | return reinterpret_cast<void*>(ptr); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 491 | } | 
|  | 492 | return base; | 
|  | 493 | } | 
| Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 494 |  | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 495 | extern "C" struct mallinfo leak_mallinfo() { | 
| Christopher Ferris | dda1c6c | 2014-07-09 17:16:07 -0700 | [diff] [blame] | 496 | return g_malloc_dispatch->mallinfo(); | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 497 | } | 
|  | 498 |  | 
|  | 499 | extern "C" int leak_posix_memalign(void** memptr, size_t alignment, size_t size) { | 
| Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 500 | if (DebugCallsDisabled()) { | 
|  | 501 | return g_malloc_dispatch->posix_memalign(memptr, alignment, size); | 
|  | 502 | } | 
|  | 503 |  | 
| Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 504 | if (!powerof2(alignment)) { | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 505 | return EINVAL; | 
|  | 506 | } | 
|  | 507 | int saved_errno = errno; | 
|  | 508 | *memptr = leak_memalign(alignment, size); | 
|  | 509 | errno = saved_errno; | 
|  | 510 | return (*memptr != NULL) ? 0 : ENOMEM; | 
|  | 511 | } | 
|  | 512 |  | 
| Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 513 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 514 | extern "C" void* leak_pvalloc(size_t bytes) { | 
| Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 515 | if (DebugCallsDisabled()) { | 
|  | 516 | return g_malloc_dispatch->pvalloc(bytes); | 
|  | 517 | } | 
|  | 518 |  | 
| Elliott Hughes | 91570ce | 2014-07-10 12:34:23 -0700 | [diff] [blame] | 519 | size_t pagesize = getpagesize(); | 
| Christopher Ferris | 03eebcb | 2014-06-13 13:57:51 -0700 | [diff] [blame] | 520 | size_t size = BIONIC_ALIGN(bytes, pagesize); | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 521 | if (size < bytes) { // Overflow | 
|  | 522 | return NULL; | 
|  | 523 | } | 
|  | 524 | return leak_memalign(pagesize, size); | 
|  | 525 | } | 
|  | 526 |  | 
|  | 527 | extern "C" void* leak_valloc(size_t size) { | 
| Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 528 | if (DebugCallsDisabled()) { | 
|  | 529 | return g_malloc_dispatch->valloc(size); | 
|  | 530 | } | 
|  | 531 |  | 
| Elliott Hughes | 91570ce | 2014-07-10 12:34:23 -0700 | [diff] [blame] | 532 | return leak_memalign(getpagesize(), size); | 
| Christopher Ferris | a403780 | 2014-06-09 19:14:11 -0700 | [diff] [blame] | 533 | } | 
| Christopher Ferris | 861c0ef | 2014-07-24 17:52:23 -0700 | [diff] [blame] | 534 | #endif |