Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | */ |
| 28 | |
| 29 | // Contains a thin layer that calls whatever real native allocator |
| 30 | // has been defined. For the libc shared library, this allows the |
| 31 | // implementation of a debug malloc that can intercept all of the allocation |
| 32 | // calls and add special debugging code to attempt to catch allocation |
| 33 | // errors. All of the debugging code is implemented in a separate shared |
| 34 | // library that is only loaded when the property "libc.debug.malloc.options" |
| 35 | // is set to a non-zero value. There are two functions exported to |
| 36 | // allow ddms, or other external users to get information from the debug |
| 37 | // allocation. |
| 38 | // get_malloc_leak_info: Returns information about all of the known native |
| 39 | // allocations that are currently in use. |
| 40 | // free_malloc_leak_info: Frees the data allocated by the call to |
| 41 | // get_malloc_leak_info. |
Christopher Ferris | 2e1a40a | 2018-06-13 10:46:34 -0700 | [diff] [blame] | 42 | // write_malloc_leak_info: Writes the leak info data to a file. |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 43 | |
Christopher Ferris | fa10a3a | 2019-03-08 10:56:17 -0800 | [diff] [blame^] | 44 | #include <errno.h> |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 45 | #include <stdint.h> |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 46 | #include <stdio.h> |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 47 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 48 | #include <private/bionic_config.h> |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 49 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 50 | #include "malloc_common.h" |
Evgenii Stepanov | be551f5 | 2018-08-13 16:46:15 -0700 | [diff] [blame] | 51 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 52 | // ============================================================================= |
| 53 | // Global variables instantations. |
| 54 | // ============================================================================= |
Evgenii Stepanov | be551f5 | 2018-08-13 16:46:15 -0700 | [diff] [blame] | 55 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 56 | // Malloc hooks globals. |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 57 | void* (*volatile __malloc_hook)(size_t, const void*); |
| 58 | void* (*volatile __realloc_hook)(void*, size_t, const void*); |
| 59 | void (*volatile __free_hook)(void*, const void*); |
| 60 | void* (*volatile __memalign_hook)(size_t, size_t, const void*); |
| 61 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 62 | // In a VM process, this is set to 1 after fork()ing out of zygote. |
| 63 | int gMallocLeakZygoteChild = 0; |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 64 | // ============================================================================= |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 65 | |
| 66 | // ============================================================================= |
| 67 | // Allocation functions |
| 68 | // ============================================================================= |
| 69 | extern "C" void* calloc(size_t n_elements, size_t elem_size) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 70 | auto dispatch_table = GetDispatchTable(); |
| 71 | if (__predict_false(dispatch_table != nullptr)) { |
| 72 | return dispatch_table->calloc(n_elements, elem_size); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 73 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 74 | void* result = Malloc(calloc)(n_elements, elem_size); |
| 75 | if (__predict_false(result == nullptr)) { |
| 76 | warning_log("calloc(%zu, %zu) failed: returning null pointer", n_elements, elem_size); |
| 77 | } |
| 78 | return result; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | extern "C" void free(void* mem) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 82 | auto dispatch_table = GetDispatchTable(); |
| 83 | if (__predict_false(dispatch_table != nullptr)) { |
| 84 | dispatch_table->free(mem); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 85 | } else { |
| 86 | Malloc(free)(mem); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | extern "C" struct mallinfo mallinfo() { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 91 | auto dispatch_table = GetDispatchTable(); |
| 92 | if (__predict_false(dispatch_table != nullptr)) { |
| 93 | return dispatch_table->mallinfo(); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 94 | } |
| 95 | return Malloc(mallinfo)(); |
| 96 | } |
| 97 | |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 98 | extern "C" int malloc_info(int options, FILE* fp) { |
| 99 | auto dispatch_table = GetDispatchTable(); |
| 100 | if (__predict_false(dispatch_table != nullptr)) { |
| 101 | return dispatch_table->malloc_info(options, fp); |
| 102 | } |
| 103 | return Malloc(malloc_info)(options, fp); |
| 104 | } |
| 105 | |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 106 | extern "C" int mallopt(int param, int value) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 107 | auto dispatch_table = GetDispatchTable(); |
| 108 | if (__predict_false(dispatch_table != nullptr)) { |
| 109 | return dispatch_table->mallopt(param, value); |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 110 | } |
| 111 | return Malloc(mallopt)(param, value); |
| 112 | } |
| 113 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 114 | extern "C" void* malloc(size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 115 | auto dispatch_table = GetDispatchTable(); |
| 116 | if (__predict_false(dispatch_table != nullptr)) { |
| 117 | return dispatch_table->malloc(bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 118 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 119 | void* result = Malloc(malloc)(bytes); |
| 120 | if (__predict_false(result == nullptr)) { |
| 121 | warning_log("malloc(%zu) failed: returning null pointer", bytes); |
| 122 | } |
| 123 | return result; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | extern "C" size_t malloc_usable_size(const void* mem) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 127 | auto dispatch_table = GetDispatchTable(); |
| 128 | if (__predict_false(dispatch_table != nullptr)) { |
| 129 | return dispatch_table->malloc_usable_size(mem); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 130 | } |
| 131 | return Malloc(malloc_usable_size)(mem); |
| 132 | } |
| 133 | |
| 134 | extern "C" void* memalign(size_t alignment, size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 135 | auto dispatch_table = GetDispatchTable(); |
| 136 | if (__predict_false(dispatch_table != nullptr)) { |
| 137 | return dispatch_table->memalign(alignment, bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 138 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 139 | void* result = Malloc(memalign)(alignment, bytes); |
| 140 | if (__predict_false(result == nullptr)) { |
| 141 | warning_log("memalign(%zu, %zu) failed: returning null pointer", alignment, bytes); |
| 142 | } |
| 143 | return result; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 147 | auto dispatch_table = GetDispatchTable(); |
| 148 | if (__predict_false(dispatch_table != nullptr)) { |
| 149 | return dispatch_table->posix_memalign(memptr, alignment, size); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 150 | } |
| 151 | return Malloc(posix_memalign)(memptr, alignment, size); |
| 152 | } |
| 153 | |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 154 | extern "C" void* aligned_alloc(size_t alignment, size_t size) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 155 | auto dispatch_table = GetDispatchTable(); |
| 156 | if (__predict_false(dispatch_table != nullptr)) { |
| 157 | return dispatch_table->aligned_alloc(alignment, size); |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 158 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 159 | void* result = Malloc(aligned_alloc)(alignment, size); |
| 160 | if (__predict_false(result == nullptr)) { |
| 161 | warning_log("aligned_alloc(%zu, %zu) failed: returning null pointer", alignment, size); |
| 162 | } |
| 163 | return result; |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 166 | extern "C" void* realloc(void* old_mem, size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 167 | auto dispatch_table = GetDispatchTable(); |
| 168 | if (__predict_false(dispatch_table != nullptr)) { |
| 169 | return dispatch_table->realloc(old_mem, bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 170 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 171 | void* result = Malloc(realloc)(old_mem, bytes); |
| 172 | if (__predict_false(result == nullptr && bytes != 0)) { |
| 173 | warning_log("realloc(%p, %zu) failed: returning null pointer", old_mem, bytes); |
| 174 | } |
| 175 | return result; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 178 | extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) { |
| 179 | size_t new_size; |
| 180 | if (__builtin_mul_overflow(item_count, item_size, &new_size)) { |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 181 | warning_log("reallocaray(%p, %zu, %zu) failed: returning null pointer", |
| 182 | old_mem, item_count, item_size); |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 183 | errno = ENOMEM; |
| 184 | return nullptr; |
| 185 | } |
| 186 | return realloc(old_mem, new_size); |
| 187 | } |
| 188 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 189 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 190 | extern "C" void* pvalloc(size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 191 | auto dispatch_table = GetDispatchTable(); |
| 192 | if (__predict_false(dispatch_table != nullptr)) { |
| 193 | return dispatch_table->pvalloc(bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 194 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 195 | void* result = Malloc(pvalloc)(bytes); |
| 196 | if (__predict_false(result == nullptr)) { |
| 197 | warning_log("pvalloc(%zu) failed: returning null pointer", bytes); |
| 198 | } |
| 199 | return result; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | extern "C" void* valloc(size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 203 | auto dispatch_table = GetDispatchTable(); |
| 204 | if (__predict_false(dispatch_table != nullptr)) { |
| 205 | return dispatch_table->valloc(bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 206 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 207 | void* result = Malloc(valloc)(bytes); |
| 208 | if (__predict_false(result == nullptr)) { |
| 209 | warning_log("valloc(%zu) failed: returning null pointer", bytes); |
| 210 | } |
| 211 | return result; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 212 | } |
| 213 | #endif |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 214 | // ============================================================================= |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 215 | |
| 216 | // ============================================================================= |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 217 | // Exported for use by libmemunreachable. |
| 218 | // ============================================================================= |
| 219 | |
| 220 | // Calls callback for every allocation in the anonymous heap mapping |
| 221 | // [base, base+size). Must be called between malloc_disable and malloc_enable. |
| 222 | extern "C" int malloc_iterate(uintptr_t base, size_t size, |
| 223 | void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 224 | auto dispatch_table = GetDispatchTable(); |
| 225 | if (__predict_false(dispatch_table != nullptr)) { |
| 226 | return dispatch_table->iterate(base, size, callback, arg); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 227 | } |
| 228 | return Malloc(iterate)(base, size, callback, arg); |
| 229 | } |
| 230 | |
| 231 | // Disable calls to malloc so malloc_iterate gets a consistent view of |
| 232 | // allocated memory. |
| 233 | extern "C" void malloc_disable() { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 234 | auto dispatch_table = GetDispatchTable(); |
| 235 | if (__predict_false(dispatch_table != nullptr)) { |
| 236 | return dispatch_table->malloc_disable(); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 237 | } |
| 238 | return Malloc(malloc_disable)(); |
| 239 | } |
| 240 | |
| 241 | // Re-enable calls to malloc after a previous call to malloc_disable. |
| 242 | extern "C" void malloc_enable() { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 243 | auto dispatch_table = GetDispatchTable(); |
| 244 | if (__predict_false(dispatch_table != nullptr)) { |
| 245 | return dispatch_table->malloc_enable(); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 246 | } |
| 247 | return Malloc(malloc_enable)(); |
| 248 | } |
Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 249 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 250 | #if defined(LIBC_STATIC) |
Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 251 | extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) { |
| 252 | return 0; |
| 253 | } |
| 254 | #endif |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 255 | |
| 256 | #if __has_feature(hwaddress_sanitizer) |
| 257 | // FIXME: implement these in HWASan allocator. |
| 258 | extern "C" int __sanitizer_iterate(uintptr_t base __unused, size_t size __unused, |
| 259 | void (*callback)(uintptr_t base, size_t size, void* arg) __unused, |
| 260 | void* arg __unused) { |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | extern "C" void __sanitizer_malloc_disable() { |
| 265 | } |
| 266 | |
| 267 | extern "C" void __sanitizer_malloc_enable() { |
| 268 | } |
Christopher Ferris | fa10a3a | 2019-03-08 10:56:17 -0800 | [diff] [blame^] | 269 | |
| 270 | extern "C" int __sanitizer_malloc_info(int, FILE*) { |
| 271 | errno = ENOTSUP; |
| 272 | return -1; |
| 273 | } |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 274 | #endif |
| 275 | // ============================================================================= |
| 276 | |
| 277 | // ============================================================================= |
| 278 | // Platform-internal mallopt variant. |
| 279 | // ============================================================================= |
| 280 | #if defined(LIBC_STATIC) |
| 281 | extern "C" bool android_mallopt(int, void*, size_t) { |
| 282 | // There are no options supported on static executables. |
| 283 | errno = ENOTSUP; |
| 284 | return false; |
| 285 | } |
| 286 | #endif |
| 287 | // ============================================================================= |