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 | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 44 | #include <stdint.h> |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 45 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 46 | #include <private/bionic_config.h> |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 47 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 48 | #include "malloc_common.h" |
Evgenii Stepanov | be551f5 | 2018-08-13 16:46:15 -0700 | [diff] [blame] | 49 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 50 | // ============================================================================= |
| 51 | // Global variables instantations. |
| 52 | // ============================================================================= |
Evgenii Stepanov | be551f5 | 2018-08-13 16:46:15 -0700 | [diff] [blame] | 53 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 54 | // Malloc hooks globals. |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 55 | void* (*volatile __malloc_hook)(size_t, const void*); |
| 56 | void* (*volatile __realloc_hook)(void*, size_t, const void*); |
| 57 | void (*volatile __free_hook)(void*, const void*); |
| 58 | void* (*volatile __memalign_hook)(size_t, size_t, const void*); |
| 59 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 60 | // In a VM process, this is set to 1 after fork()ing out of zygote. |
| 61 | int gMallocLeakZygoteChild = 0; |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 62 | // ============================================================================= |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 63 | |
| 64 | // ============================================================================= |
| 65 | // Allocation functions |
| 66 | // ============================================================================= |
| 67 | extern "C" void* calloc(size_t n_elements, size_t elem_size) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 68 | auto dispatch_table = GetDispatchTable(); |
| 69 | if (__predict_false(dispatch_table != nullptr)) { |
| 70 | return dispatch_table->calloc(n_elements, elem_size); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 71 | } |
| 72 | return Malloc(calloc)(n_elements, elem_size); |
| 73 | } |
| 74 | |
| 75 | extern "C" void free(void* mem) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 76 | auto dispatch_table = GetDispatchTable(); |
| 77 | if (__predict_false(dispatch_table != nullptr)) { |
| 78 | dispatch_table->free(mem); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 79 | } else { |
| 80 | Malloc(free)(mem); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | extern "C" struct mallinfo mallinfo() { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 85 | auto dispatch_table = GetDispatchTable(); |
| 86 | if (__predict_false(dispatch_table != nullptr)) { |
| 87 | return dispatch_table->mallinfo(); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 88 | } |
| 89 | return Malloc(mallinfo)(); |
| 90 | } |
| 91 | |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 92 | extern "C" int mallopt(int param, int value) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 93 | auto dispatch_table = GetDispatchTable(); |
| 94 | if (__predict_false(dispatch_table != nullptr)) { |
| 95 | return dispatch_table->mallopt(param, value); |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 96 | } |
| 97 | return Malloc(mallopt)(param, value); |
| 98 | } |
| 99 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 100 | extern "C" void* malloc(size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 101 | auto dispatch_table = GetDispatchTable(); |
| 102 | if (__predict_false(dispatch_table != nullptr)) { |
| 103 | return dispatch_table->malloc(bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 104 | } |
| 105 | return Malloc(malloc)(bytes); |
| 106 | } |
| 107 | |
| 108 | extern "C" size_t malloc_usable_size(const void* mem) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 109 | auto dispatch_table = GetDispatchTable(); |
| 110 | if (__predict_false(dispatch_table != nullptr)) { |
| 111 | return dispatch_table->malloc_usable_size(mem); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 112 | } |
| 113 | return Malloc(malloc_usable_size)(mem); |
| 114 | } |
| 115 | |
| 116 | extern "C" void* memalign(size_t alignment, size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 117 | auto dispatch_table = GetDispatchTable(); |
| 118 | if (__predict_false(dispatch_table != nullptr)) { |
| 119 | return dispatch_table->memalign(alignment, bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 120 | } |
| 121 | return Malloc(memalign)(alignment, bytes); |
| 122 | } |
| 123 | |
| 124 | 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] | 125 | auto dispatch_table = GetDispatchTable(); |
| 126 | if (__predict_false(dispatch_table != nullptr)) { |
| 127 | return dispatch_table->posix_memalign(memptr, alignment, size); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 128 | } |
| 129 | return Malloc(posix_memalign)(memptr, alignment, size); |
| 130 | } |
| 131 | |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 132 | extern "C" void* aligned_alloc(size_t alignment, size_t size) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 133 | auto dispatch_table = GetDispatchTable(); |
| 134 | if (__predict_false(dispatch_table != nullptr)) { |
| 135 | return dispatch_table->aligned_alloc(alignment, size); |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 136 | } |
| 137 | return Malloc(aligned_alloc)(alignment, size); |
| 138 | } |
| 139 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 140 | extern "C" void* realloc(void* old_mem, size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 141 | auto dispatch_table = GetDispatchTable(); |
| 142 | if (__predict_false(dispatch_table != nullptr)) { |
| 143 | return dispatch_table->realloc(old_mem, bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 144 | } |
| 145 | return Malloc(realloc)(old_mem, bytes); |
| 146 | } |
| 147 | |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 148 | extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) { |
| 149 | size_t new_size; |
| 150 | if (__builtin_mul_overflow(item_count, item_size, &new_size)) { |
| 151 | errno = ENOMEM; |
| 152 | return nullptr; |
| 153 | } |
| 154 | return realloc(old_mem, new_size); |
| 155 | } |
| 156 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 157 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 158 | extern "C" void* pvalloc(size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 159 | auto dispatch_table = GetDispatchTable(); |
| 160 | if (__predict_false(dispatch_table != nullptr)) { |
| 161 | return dispatch_table->pvalloc(bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 162 | } |
| 163 | return Malloc(pvalloc)(bytes); |
| 164 | } |
| 165 | |
| 166 | extern "C" void* valloc(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->valloc(bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 170 | } |
| 171 | return Malloc(valloc)(bytes); |
| 172 | } |
| 173 | #endif |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 174 | // ============================================================================= |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 175 | |
| 176 | // ============================================================================= |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 177 | // Exported for use by libmemunreachable. |
| 178 | // ============================================================================= |
| 179 | |
| 180 | // Calls callback for every allocation in the anonymous heap mapping |
| 181 | // [base, base+size). Must be called between malloc_disable and malloc_enable. |
| 182 | extern "C" int malloc_iterate(uintptr_t base, size_t size, |
| 183 | void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 184 | auto dispatch_table = GetDispatchTable(); |
| 185 | if (__predict_false(dispatch_table != nullptr)) { |
| 186 | return dispatch_table->iterate(base, size, callback, arg); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 187 | } |
| 188 | return Malloc(iterate)(base, size, callback, arg); |
| 189 | } |
| 190 | |
| 191 | // Disable calls to malloc so malloc_iterate gets a consistent view of |
| 192 | // allocated memory. |
| 193 | extern "C" void malloc_disable() { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 194 | auto dispatch_table = GetDispatchTable(); |
| 195 | if (__predict_false(dispatch_table != nullptr)) { |
| 196 | return dispatch_table->malloc_disable(); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 197 | } |
| 198 | return Malloc(malloc_disable)(); |
| 199 | } |
| 200 | |
| 201 | // Re-enable calls to malloc after a previous call to malloc_disable. |
| 202 | extern "C" void malloc_enable() { |
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->malloc_enable(); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 206 | } |
| 207 | return Malloc(malloc_enable)(); |
| 208 | } |
Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 209 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 210 | #if defined(LIBC_STATIC) |
Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 211 | extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) { |
| 212 | return 0; |
| 213 | } |
| 214 | #endif |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 215 | |
| 216 | #if __has_feature(hwaddress_sanitizer) |
| 217 | // FIXME: implement these in HWASan allocator. |
| 218 | extern "C" int __sanitizer_iterate(uintptr_t base __unused, size_t size __unused, |
| 219 | void (*callback)(uintptr_t base, size_t size, void* arg) __unused, |
| 220 | void* arg __unused) { |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | extern "C" void __sanitizer_malloc_disable() { |
| 225 | } |
| 226 | |
| 227 | extern "C" void __sanitizer_malloc_enable() { |
| 228 | } |
| 229 | #endif |
| 230 | // ============================================================================= |
| 231 | |
| 232 | // ============================================================================= |
| 233 | // Platform-internal mallopt variant. |
| 234 | // ============================================================================= |
| 235 | #if defined(LIBC_STATIC) |
| 236 | extern "C" bool android_mallopt(int, void*, size_t) { |
| 237 | // There are no options supported on static executables. |
| 238 | errno = ENOTSUP; |
| 239 | return false; |
| 240 | } |
| 241 | #endif |
| 242 | // ============================================================================= |