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