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