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" |
Christopher Ferris | 30659fd | 2019-04-15 19:01:08 -0700 | [diff] [blame] | 35 | // is set to a non-zero value. |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 36 | |
Christopher Ferris | fa10a3a | 2019-03-08 10:56:17 -0800 | [diff] [blame] | 37 | #include <errno.h> |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 38 | #include <stdint.h> |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 39 | #include <stdio.h> |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 40 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 41 | #include <private/bionic_config.h> |
Christopher Ferris | 2b0638e | 2019-09-11 19:05:29 -0700 | [diff] [blame] | 42 | #include <platform/bionic/malloc.h> |
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 "malloc_common.h" |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 45 | #include "malloc_limit.h" |
Evgenii Stepanov | be551f5 | 2018-08-13 16:46:15 -0700 | [diff] [blame] | 46 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 47 | // ============================================================================= |
| 48 | // Global variables instantations. |
| 49 | // ============================================================================= |
Evgenii Stepanov | be551f5 | 2018-08-13 16:46:15 -0700 | [diff] [blame] | 50 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 51 | // Malloc hooks globals. |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 52 | void* (*volatile __malloc_hook)(size_t, const void*); |
| 53 | void* (*volatile __realloc_hook)(void*, size_t, const void*); |
| 54 | void (*volatile __free_hook)(void*, const void*); |
| 55 | void* (*volatile __memalign_hook)(size_t, size_t, const void*); |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 56 | // ============================================================================= |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 57 | |
| 58 | // ============================================================================= |
| 59 | // Allocation functions |
| 60 | // ============================================================================= |
| 61 | extern "C" void* calloc(size_t n_elements, size_t elem_size) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 62 | auto dispatch_table = GetDispatchTable(); |
| 63 | if (__predict_false(dispatch_table != nullptr)) { |
| 64 | return dispatch_table->calloc(n_elements, elem_size); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 65 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 66 | void* result = Malloc(calloc)(n_elements, elem_size); |
| 67 | if (__predict_false(result == nullptr)) { |
| 68 | warning_log("calloc(%zu, %zu) failed: returning null pointer", n_elements, elem_size); |
| 69 | } |
| 70 | return result; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | extern "C" void free(void* mem) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 74 | auto dispatch_table = GetDispatchTable(); |
| 75 | if (__predict_false(dispatch_table != nullptr)) { |
| 76 | dispatch_table->free(mem); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 77 | } else { |
| 78 | Malloc(free)(mem); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | extern "C" struct mallinfo mallinfo() { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 83 | auto dispatch_table = GetDispatchTable(); |
| 84 | if (__predict_false(dispatch_table != nullptr)) { |
| 85 | return dispatch_table->mallinfo(); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 86 | } |
| 87 | return Malloc(mallinfo)(); |
| 88 | } |
| 89 | |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 90 | extern "C" int malloc_info(int options, FILE* fp) { |
| 91 | auto dispatch_table = GetDispatchTable(); |
| 92 | if (__predict_false(dispatch_table != nullptr)) { |
| 93 | return dispatch_table->malloc_info(options, fp); |
| 94 | } |
| 95 | return Malloc(malloc_info)(options, fp); |
| 96 | } |
| 97 | |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 98 | extern "C" int mallopt(int param, int value) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 99 | auto dispatch_table = GetDispatchTable(); |
| 100 | if (__predict_false(dispatch_table != nullptr)) { |
| 101 | return dispatch_table->mallopt(param, value); |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 102 | } |
| 103 | return Malloc(mallopt)(param, value); |
| 104 | } |
| 105 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 106 | extern "C" void* malloc(size_t bytes) { |
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->malloc(bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 110 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 111 | void* result = Malloc(malloc)(bytes); |
| 112 | if (__predict_false(result == nullptr)) { |
| 113 | warning_log("malloc(%zu) failed: returning null pointer", bytes); |
| 114 | } |
| 115 | return result; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | extern "C" size_t malloc_usable_size(const void* mem) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 119 | auto dispatch_table = GetDispatchTable(); |
| 120 | if (__predict_false(dispatch_table != nullptr)) { |
| 121 | return dispatch_table->malloc_usable_size(mem); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 122 | } |
| 123 | return Malloc(malloc_usable_size)(mem); |
| 124 | } |
| 125 | |
| 126 | extern "C" void* memalign(size_t alignment, size_t bytes) { |
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->memalign(alignment, bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 130 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 131 | void* result = Malloc(memalign)(alignment, bytes); |
| 132 | if (__predict_false(result == nullptr)) { |
| 133 | warning_log("memalign(%zu, %zu) failed: returning null pointer", alignment, bytes); |
| 134 | } |
| 135 | return result; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | 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] | 139 | auto dispatch_table = GetDispatchTable(); |
| 140 | if (__predict_false(dispatch_table != nullptr)) { |
| 141 | return dispatch_table->posix_memalign(memptr, alignment, size); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 142 | } |
| 143 | return Malloc(posix_memalign)(memptr, alignment, size); |
| 144 | } |
| 145 | |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 146 | extern "C" void* aligned_alloc(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->aligned_alloc(alignment, size); |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 150 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 151 | void* result = Malloc(aligned_alloc)(alignment, size); |
| 152 | if (__predict_false(result == nullptr)) { |
| 153 | warning_log("aligned_alloc(%zu, %zu) failed: returning null pointer", alignment, size); |
| 154 | } |
| 155 | return result; |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Elliott Hughes | 390be50 | 2019-04-20 22:18:49 -0700 | [diff] [blame] | 158 | extern "C" __attribute__((__noinline__)) void* realloc(void* old_mem, 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->realloc(old_mem, bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 162 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 163 | void* result = Malloc(realloc)(old_mem, bytes); |
| 164 | if (__predict_false(result == nullptr && bytes != 0)) { |
| 165 | warning_log("realloc(%p, %zu) failed: returning null pointer", old_mem, bytes); |
| 166 | } |
| 167 | return result; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 168 | } |
| 169 | |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 170 | extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) { |
| 171 | size_t new_size; |
| 172 | if (__builtin_mul_overflow(item_count, item_size, &new_size)) { |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 173 | warning_log("reallocaray(%p, %zu, %zu) failed: returning null pointer", |
| 174 | old_mem, item_count, item_size); |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 175 | errno = ENOMEM; |
| 176 | return nullptr; |
| 177 | } |
| 178 | return realloc(old_mem, new_size); |
| 179 | } |
| 180 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 181 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 182 | extern "C" void* pvalloc(size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 183 | auto dispatch_table = GetDispatchTable(); |
| 184 | if (__predict_false(dispatch_table != nullptr)) { |
| 185 | return dispatch_table->pvalloc(bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 186 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 187 | void* result = Malloc(pvalloc)(bytes); |
| 188 | if (__predict_false(result == nullptr)) { |
| 189 | warning_log("pvalloc(%zu) failed: returning null pointer", bytes); |
| 190 | } |
| 191 | return result; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | extern "C" void* valloc(size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 195 | auto dispatch_table = GetDispatchTable(); |
| 196 | if (__predict_false(dispatch_table != nullptr)) { |
| 197 | return dispatch_table->valloc(bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 198 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 199 | void* result = Malloc(valloc)(bytes); |
| 200 | if (__predict_false(result == nullptr)) { |
| 201 | warning_log("valloc(%zu) failed: returning null pointer", bytes); |
| 202 | } |
| 203 | return result; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 204 | } |
| 205 | #endif |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 206 | // ============================================================================= |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 207 | |
| 208 | // ============================================================================= |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 209 | // Exported for use by libmemunreachable. |
| 210 | // ============================================================================= |
| 211 | |
| 212 | // Calls callback for every allocation in the anonymous heap mapping |
| 213 | // [base, base+size). Must be called between malloc_disable and malloc_enable. |
| 214 | extern "C" int malloc_iterate(uintptr_t base, size_t size, |
| 215 | void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 216 | auto dispatch_table = GetDispatchTable(); |
| 217 | if (__predict_false(dispatch_table != nullptr)) { |
Christopher Ferris | 6f517cd | 2019-11-08 11:28:38 -0800 | [diff] [blame] | 218 | return dispatch_table->malloc_iterate(base, size, callback, arg); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 219 | } |
Christopher Ferris | 6f517cd | 2019-11-08 11:28:38 -0800 | [diff] [blame] | 220 | return Malloc(malloc_iterate)(base, size, callback, arg); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | // Disable calls to malloc so malloc_iterate gets a consistent view of |
| 224 | // allocated memory. |
| 225 | extern "C" void malloc_disable() { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 226 | auto dispatch_table = GetDispatchTable(); |
| 227 | if (__predict_false(dispatch_table != nullptr)) { |
| 228 | return dispatch_table->malloc_disable(); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 229 | } |
| 230 | return Malloc(malloc_disable)(); |
| 231 | } |
| 232 | |
| 233 | // Re-enable calls to malloc after a previous call to malloc_disable. |
| 234 | extern "C" void malloc_enable() { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 235 | auto dispatch_table = GetDispatchTable(); |
| 236 | if (__predict_false(dispatch_table != nullptr)) { |
| 237 | return dispatch_table->malloc_enable(); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 238 | } |
| 239 | return Malloc(malloc_enable)(); |
| 240 | } |
Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 241 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 242 | #if defined(LIBC_STATIC) |
Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 243 | extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) { |
| 244 | return 0; |
| 245 | } |
| 246 | #endif |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 247 | |
| 248 | #if __has_feature(hwaddress_sanitizer) |
| 249 | // FIXME: implement these in HWASan allocator. |
Christopher Ferris | 6f517cd | 2019-11-08 11:28:38 -0800 | [diff] [blame] | 250 | extern "C" int __sanitizer_malloc_iterate(uintptr_t base __unused, size_t size __unused, |
| 251 | void (*callback)(uintptr_t base, size_t size, void* arg) |
| 252 | __unused, |
| 253 | void* arg __unused) { |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | extern "C" void __sanitizer_malloc_disable() { |
| 258 | } |
| 259 | |
| 260 | extern "C" void __sanitizer_malloc_enable() { |
| 261 | } |
Christopher Ferris | fa10a3a | 2019-03-08 10:56:17 -0800 | [diff] [blame] | 262 | |
| 263 | extern "C" int __sanitizer_malloc_info(int, FILE*) { |
| 264 | errno = ENOTSUP; |
| 265 | return -1; |
| 266 | } |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 267 | #endif |
| 268 | // ============================================================================= |
| 269 | |
| 270 | // ============================================================================= |
| 271 | // Platform-internal mallopt variant. |
| 272 | // ============================================================================= |
| 273 | #if defined(LIBC_STATIC) |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 274 | extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size) { |
| 275 | if (opcode == M_SET_ALLOCATION_LIMIT_BYTES) { |
| 276 | return LimitEnable(arg, arg_size); |
| 277 | } |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 278 | errno = ENOTSUP; |
| 279 | return false; |
| 280 | } |
| 281 | #endif |
| 282 | // ============================================================================= |