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 | 2b0638e | 2019-09-11 19:05:29 -0700 | [diff] [blame] | 41 | #include <platform/bionic/malloc.h> |
Mitch Phillips | 2210b8d | 2020-11-25 16:48:54 -0800 | [diff] [blame] | 42 | #include <private/ScopedPthreadMutexLocker.h> |
| 43 | #include <private/bionic_config.h> |
Christopher Ferris | 0b23199 | 2023-10-31 15:09:37 -0700 | [diff] [blame] | 44 | #include <private/bionic_defs.h> |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 45 | |
Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 46 | #include "gwp_asan_wrappers.h" |
Peter Collingbourne | 1e110fb | 2020-01-09 10:48:22 -0800 | [diff] [blame] | 47 | #include "heap_tagging.h" |
Mitch Phillips | 9cad842 | 2021-01-20 16:03:27 -0800 | [diff] [blame] | 48 | #include "heap_zero_init.h" |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 49 | #include "malloc_common.h" |
Christopher Ferris | 1fc5ccf | 2019-02-15 18:06:15 -0800 | [diff] [blame] | 50 | #include "malloc_limit.h" |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 51 | #include "malloc_tagged_pointers.h" |
Evgenii Stepanov | be551f5 | 2018-08-13 16:46:15 -0700 | [diff] [blame] | 52 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 53 | // ============================================================================= |
| 54 | // Global variables instantations. |
| 55 | // ============================================================================= |
Evgenii Stepanov | be551f5 | 2018-08-13 16:46:15 -0700 | [diff] [blame] | 56 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 57 | // Malloc hooks globals. |
Christopher Ferris | db478a6 | 2018-02-07 18:42:14 -0800 | [diff] [blame] | 58 | void* (*volatile __malloc_hook)(size_t, const void*); |
| 59 | void* (*volatile __realloc_hook)(void*, size_t, const void*); |
| 60 | void (*volatile __free_hook)(void*, const void*); |
| 61 | void* (*volatile __memalign_hook)(size_t, size_t, const void*); |
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)) { |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 70 | return MaybeTagPointer(dispatch_table->calloc(n_elements, elem_size)); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 71 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 72 | void* result = Malloc(calloc)(n_elements, elem_size); |
| 73 | if (__predict_false(result == nullptr)) { |
| 74 | warning_log("calloc(%zu, %zu) failed: returning null pointer", n_elements, elem_size); |
| 75 | } |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 76 | return MaybeTagPointer(result); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | extern "C" void free(void* mem) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 80 | auto dispatch_table = GetDispatchTable(); |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 81 | mem = MaybeUntagAndCheckPointer(mem); |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 82 | if (__predict_false(dispatch_table != nullptr)) { |
| 83 | dispatch_table->free(mem); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 84 | } else { |
| 85 | Malloc(free)(mem); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | extern "C" struct mallinfo mallinfo() { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 90 | auto dispatch_table = GetDispatchTable(); |
| 91 | if (__predict_false(dispatch_table != nullptr)) { |
| 92 | return dispatch_table->mallinfo(); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 93 | } |
| 94 | return Malloc(mallinfo)(); |
| 95 | } |
| 96 | |
Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 97 | extern "C" int malloc_info(int options, FILE* fp) { |
| 98 | auto dispatch_table = GetDispatchTable(); |
| 99 | if (__predict_false(dispatch_table != nullptr)) { |
| 100 | return dispatch_table->malloc_info(options, fp); |
| 101 | } |
| 102 | return Malloc(malloc_info)(options, fp); |
| 103 | } |
| 104 | |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 105 | extern "C" int mallopt(int param, int value) { |
Elliott Hughes | 446b4dd | 2021-01-14 13:34:20 -0800 | [diff] [blame] | 106 | // Some are handled by libc directly rather than by the allocator. |
| 107 | if (param == M_BIONIC_SET_HEAP_TAGGING_LEVEL) { |
| 108 | ScopedPthreadMutexLocker locker(&g_heap_tagging_lock); |
| 109 | return SetHeapTaggingLevel(static_cast<HeapTaggingLevel>(value)); |
| 110 | } |
Mitch Phillips | 9cad842 | 2021-01-20 16:03:27 -0800 | [diff] [blame] | 111 | if (param == M_BIONIC_ZERO_INIT) { |
| 112 | return SetHeapZeroInitialize(value); |
Elliott Hughes | 446b4dd | 2021-01-14 13:34:20 -0800 | [diff] [blame] | 113 | } |
Christopher Ferris | b4e560e | 2023-10-26 17:00:00 -0700 | [diff] [blame] | 114 | |
Elliott Hughes | 446b4dd | 2021-01-14 13:34:20 -0800 | [diff] [blame] | 115 | // The rest we pass on... |
Christopher Ferris | b4e560e | 2023-10-26 17:00:00 -0700 | [diff] [blame] | 116 | int retval; |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 117 | auto dispatch_table = GetDispatchTable(); |
| 118 | if (__predict_false(dispatch_table != nullptr)) { |
Christopher Ferris | b4e560e | 2023-10-26 17:00:00 -0700 | [diff] [blame] | 119 | retval = dispatch_table->mallopt(param, value); |
| 120 | } else { |
| 121 | retval = Malloc(mallopt)(param, value); |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 122 | } |
Christopher Ferris | b4e560e | 2023-10-26 17:00:00 -0700 | [diff] [blame] | 123 | |
| 124 | // Track the M_DECAY_TIME mallopt calls. |
| 125 | if (param == M_DECAY_TIME && retval == 1) { |
| 126 | __libc_globals.mutate([value](libc_globals* globals) { |
Chia-hung Duan | 6abb406 | 2024-04-17 19:08:48 -0700 | [diff] [blame] | 127 | if (value <= 0) { |
Christopher Ferris | b4e560e | 2023-10-26 17:00:00 -0700 | [diff] [blame] | 128 | atomic_store(&globals->decay_time_enabled, false); |
| 129 | } else { |
| 130 | atomic_store(&globals->decay_time_enabled, true); |
| 131 | } |
| 132 | }); |
| 133 | } |
| 134 | return retval; |
Christopher Ferris | a1c0d2f | 2017-05-15 15:50:19 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 137 | extern "C" void* malloc(size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 138 | auto dispatch_table = GetDispatchTable(); |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 139 | void *result; |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 140 | if (__predict_false(dispatch_table != nullptr)) { |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 141 | result = dispatch_table->malloc(bytes); |
| 142 | } else { |
| 143 | result = Malloc(malloc)(bytes); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 144 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 145 | if (__predict_false(result == nullptr)) { |
| 146 | warning_log("malloc(%zu) failed: returning null pointer", bytes); |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 147 | return nullptr; |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 148 | } |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 149 | return MaybeTagPointer(result); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | extern "C" size_t malloc_usable_size(const void* mem) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 153 | auto dispatch_table = GetDispatchTable(); |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 154 | mem = MaybeUntagAndCheckPointer(mem); |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 155 | if (__predict_false(dispatch_table != nullptr)) { |
| 156 | return dispatch_table->malloc_usable_size(mem); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 157 | } |
| 158 | return Malloc(malloc_usable_size)(mem); |
| 159 | } |
| 160 | |
| 161 | extern "C" void* memalign(size_t alignment, size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 162 | auto dispatch_table = GetDispatchTable(); |
| 163 | if (__predict_false(dispatch_table != nullptr)) { |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 164 | return MaybeTagPointer(dispatch_table->memalign(alignment, bytes)); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 165 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 166 | void* result = Malloc(memalign)(alignment, bytes); |
| 167 | if (__predict_false(result == nullptr)) { |
| 168 | warning_log("memalign(%zu, %zu) failed: returning null pointer", alignment, bytes); |
| 169 | } |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 170 | return MaybeTagPointer(result); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | 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] | 174 | auto dispatch_table = GetDispatchTable(); |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 175 | int result; |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 176 | if (__predict_false(dispatch_table != nullptr)) { |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 177 | result = dispatch_table->posix_memalign(memptr, alignment, size); |
| 178 | } else { |
| 179 | result = Malloc(posix_memalign)(memptr, alignment, size); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 180 | } |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 181 | if (result == 0) { |
| 182 | *memptr = MaybeTagPointer(*memptr); |
| 183 | } |
| 184 | return result; |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 187 | extern "C" void* aligned_alloc(size_t alignment, size_t size) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 188 | auto dispatch_table = GetDispatchTable(); |
| 189 | if (__predict_false(dispatch_table != nullptr)) { |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 190 | return MaybeTagPointer(dispatch_table->aligned_alloc(alignment, size)); |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 191 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 192 | void* result = Malloc(aligned_alloc)(alignment, size); |
| 193 | if (__predict_false(result == nullptr)) { |
| 194 | warning_log("aligned_alloc(%zu, %zu) failed: returning null pointer", alignment, size); |
| 195 | } |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 196 | return MaybeTagPointer(result); |
Christopher Ferris | cae21a9 | 2018-02-05 18:14:55 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Elliott Hughes | 390be50 | 2019-04-20 22:18:49 -0700 | [diff] [blame] | 199 | extern "C" __attribute__((__noinline__)) void* realloc(void* old_mem, size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 200 | auto dispatch_table = GetDispatchTable(); |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 201 | old_mem = MaybeUntagAndCheckPointer(old_mem); |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 202 | if (__predict_false(dispatch_table != nullptr)) { |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 203 | return MaybeTagPointer(dispatch_table->realloc(old_mem, bytes)); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 204 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 205 | void* result = Malloc(realloc)(old_mem, bytes); |
| 206 | if (__predict_false(result == nullptr && bytes != 0)) { |
| 207 | warning_log("realloc(%p, %zu) failed: returning null pointer", old_mem, bytes); |
| 208 | } |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 209 | return MaybeTagPointer(result); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 210 | } |
| 211 | |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 212 | extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) { |
| 213 | size_t new_size; |
| 214 | if (__builtin_mul_overflow(item_count, item_size, &new_size)) { |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 215 | warning_log("reallocaray(%p, %zu, %zu) failed: returning null pointer", |
| 216 | old_mem, item_count, item_size); |
Elliott Hughes | b177085 | 2018-09-18 12:52:42 -0700 | [diff] [blame] | 217 | errno = ENOMEM; |
| 218 | return nullptr; |
| 219 | } |
| 220 | return realloc(old_mem, new_size); |
| 221 | } |
| 222 | |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 223 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 224 | extern "C" void* pvalloc(size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 225 | auto dispatch_table = GetDispatchTable(); |
| 226 | if (__predict_false(dispatch_table != nullptr)) { |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 227 | return MaybeTagPointer(dispatch_table->pvalloc(bytes)); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 228 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 229 | void* result = Malloc(pvalloc)(bytes); |
| 230 | if (__predict_false(result == nullptr)) { |
| 231 | warning_log("pvalloc(%zu) failed: returning null pointer", bytes); |
| 232 | } |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 233 | return MaybeTagPointer(result); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | extern "C" void* valloc(size_t bytes) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 237 | auto dispatch_table = GetDispatchTable(); |
| 238 | if (__predict_false(dispatch_table != nullptr)) { |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 239 | return MaybeTagPointer(dispatch_table->valloc(bytes)); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 240 | } |
Elliott Hughes | a21f6cc | 2019-02-25 13:21:04 -0800 | [diff] [blame] | 241 | void* result = Malloc(valloc)(bytes); |
| 242 | if (__predict_false(result == nullptr)) { |
| 243 | warning_log("valloc(%zu) failed: returning null pointer", bytes); |
| 244 | } |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 245 | return MaybeTagPointer(result); |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 246 | } |
| 247 | #endif |
Christopher Ferris | 63860cb | 2015-11-16 17:30:32 -0800 | [diff] [blame] | 248 | // ============================================================================= |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 249 | |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 250 | struct CallbackWrapperArg { |
| 251 | void (*callback)(uintptr_t base, size_t size, void* arg); |
| 252 | void* arg; |
| 253 | }; |
| 254 | |
| 255 | void CallbackWrapper(uintptr_t base, size_t size, void* arg) { |
| 256 | CallbackWrapperArg* wrapper_arg = reinterpret_cast<CallbackWrapperArg*>(arg); |
| 257 | wrapper_arg->callback( |
| 258 | reinterpret_cast<uintptr_t>(MaybeTagPointer(reinterpret_cast<void*>(base))), |
| 259 | size, wrapper_arg->arg); |
| 260 | } |
| 261 | |
Ryan Savitski | ecc37e3 | 2018-12-14 15:57:21 +0000 | [diff] [blame] | 262 | // ============================================================================= |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 263 | // Exported for use by libmemunreachable. |
| 264 | // ============================================================================= |
| 265 | |
| 266 | // Calls callback for every allocation in the anonymous heap mapping |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 267 | // [base, base+size). Must be called between malloc_disable and malloc_enable. |
| 268 | // `base` in this can take either a tagged or untagged pointer, but we always |
| 269 | // provide a tagged pointer to the `base` argument of `callback` if the kernel |
| 270 | // supports tagged pointers. |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 271 | extern "C" int malloc_iterate(uintptr_t base, size_t size, |
| 272 | void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 273 | auto dispatch_table = GetDispatchTable(); |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 274 | // Wrap the malloc_iterate callback we were provided, in order to provide |
| 275 | // pointer tagging support. |
| 276 | CallbackWrapperArg wrapper_arg; |
| 277 | wrapper_arg.callback = callback; |
| 278 | wrapper_arg.arg = arg; |
| 279 | uintptr_t untagged_base = |
| 280 | reinterpret_cast<uintptr_t>(UntagPointer(reinterpret_cast<void*>(base))); |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 281 | if (__predict_false(dispatch_table != nullptr)) { |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 282 | return dispatch_table->malloc_iterate( |
| 283 | untagged_base, size, CallbackWrapper, &wrapper_arg); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 284 | } |
Mitch Phillips | 3b21ada | 2020-01-07 15:47:47 -0800 | [diff] [blame] | 285 | return Malloc(malloc_iterate)( |
| 286 | untagged_base, size, CallbackWrapper, &wrapper_arg); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | // Disable calls to malloc so malloc_iterate gets a consistent view of |
| 290 | // allocated memory. |
| 291 | extern "C" void malloc_disable() { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 292 | auto dispatch_table = GetDispatchTable(); |
| 293 | if (__predict_false(dispatch_table != nullptr)) { |
| 294 | return dispatch_table->malloc_disable(); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 295 | } |
| 296 | return Malloc(malloc_disable)(); |
| 297 | } |
| 298 | |
| 299 | // Re-enable calls to malloc after a previous call to malloc_disable. |
| 300 | extern "C" void malloc_enable() { |
Christopher Ferris | 62e1e2c | 2019-02-04 12:26:02 -0800 | [diff] [blame] | 301 | auto dispatch_table = GetDispatchTable(); |
| 302 | if (__predict_false(dispatch_table != nullptr)) { |
| 303 | return dispatch_table->malloc_enable(); |
Colin Cross | 869691c | 2016-01-29 12:48:18 -0800 | [diff] [blame] | 304 | } |
| 305 | return Malloc(malloc_enable)(); |
| 306 | } |
Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 307 | |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 308 | #if defined(LIBC_STATIC) |
Colin Cross | 2d4721c | 2016-02-02 11:57:54 -0800 | [diff] [blame] | 309 | extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) { |
| 310 | return 0; |
| 311 | } |
| 312 | #endif |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 313 | |
| 314 | #if __has_feature(hwaddress_sanitizer) |
| 315 | // FIXME: implement these in HWASan allocator. |
Christopher Ferris | 6f517cd | 2019-11-08 11:28:38 -0800 | [diff] [blame] | 316 | extern "C" int __sanitizer_malloc_iterate(uintptr_t base __unused, size_t size __unused, |
| 317 | void (*callback)(uintptr_t base, size_t size, void* arg) |
| 318 | __unused, |
| 319 | void* arg __unused) { |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | extern "C" void __sanitizer_malloc_disable() { |
| 324 | } |
| 325 | |
| 326 | extern "C" void __sanitizer_malloc_enable() { |
| 327 | } |
Christopher Ferris | fa10a3a | 2019-03-08 10:56:17 -0800 | [diff] [blame] | 328 | |
| 329 | extern "C" int __sanitizer_malloc_info(int, FILE*) { |
| 330 | errno = ENOTSUP; |
| 331 | return -1; |
| 332 | } |
Christopher Ferris | e4cdbc4 | 2019-02-08 17:30:58 -0800 | [diff] [blame] | 333 | #endif |
| 334 | // ============================================================================= |
| 335 | |
Mitch Phillips | f3968e8 | 2020-01-31 19:57:04 -0800 | [diff] [blame] | 336 | static constexpr MallocDispatch __libc_malloc_default_dispatch __attribute__((unused)) = { |
| 337 | Malloc(calloc), |
| 338 | Malloc(free), |
| 339 | Malloc(mallinfo), |
| 340 | Malloc(malloc), |
| 341 | Malloc(malloc_usable_size), |
| 342 | Malloc(memalign), |
| 343 | Malloc(posix_memalign), |
| 344 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 345 | Malloc(pvalloc), |
| 346 | #endif |
| 347 | Malloc(realloc), |
| 348 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS) |
| 349 | Malloc(valloc), |
| 350 | #endif |
| 351 | Malloc(malloc_iterate), |
| 352 | Malloc(malloc_disable), |
| 353 | Malloc(malloc_enable), |
| 354 | Malloc(mallopt), |
| 355 | Malloc(aligned_alloc), |
| 356 | Malloc(malloc_info), |
| 357 | }; |
| 358 | |
| 359 | const MallocDispatch* NativeAllocatorDispatch() { |
| 360 | return &__libc_malloc_default_dispatch; |
| 361 | } |
Christopher Ferris | 0b23199 | 2023-10-31 15:09:37 -0700 | [diff] [blame] | 362 | |
| 363 | #if !defined(LIBC_STATIC) |
| 364 | void MallocInitImpl(libc_globals* globals); |
| 365 | #endif |
| 366 | |
| 367 | // Initializes memory allocation framework. |
| 368 | // This routine is called from __libc_init routines in libc_init_dynamic.cpp |
| 369 | // and libc_init_static.cpp. |
| 370 | __BIONIC_WEAK_FOR_NATIVE_BRIDGE |
| 371 | __LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals) { |
| 372 | #if !defined(LIBC_STATIC) |
| 373 | MallocInitImpl(globals); |
| 374 | #endif |
| 375 | const char* value = getenv("MALLOC_USE_APP_DEFAULTS"); |
| 376 | if (value == nullptr || value[0] == '\0') { |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | // Normal apps currently turn off zero init for performance reasons. |
| 381 | SetHeapZeroInitialize(false); |
| 382 | |
| 383 | // Do not call mallopt directly since that will try and lock the globals |
| 384 | // data structure. |
| 385 | int retval; |
| 386 | auto dispatch_table = GetDispatchTable(); |
| 387 | if (__predict_false(dispatch_table != nullptr)) { |
| 388 | retval = dispatch_table->mallopt(M_DECAY_TIME, 1); |
| 389 | } else { |
| 390 | retval = Malloc(mallopt)(M_DECAY_TIME, 1); |
| 391 | } |
| 392 | if (retval == 1) { |
| 393 | globals->decay_time_enabled = true; |
| 394 | } |
| 395 | } |