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