blob: e08083fb42def79619642f61aa374b1692e7b8c8 [file] [log] [blame]
Christopher Ferris63860cb2015-11-16 17:30:32 -08001/*
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 Ferris30659fd2019-04-15 19:01:08 -070035// is set to a non-zero value.
Christopher Ferris63860cb2015-11-16 17:30:32 -080036
Christopher Ferrisfa10a3a2019-03-08 10:56:17 -080037#include <errno.h>
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080038#include <stdint.h>
Christopher Ferris6c619a02019-03-01 17:59:51 -080039#include <stdio.h>
Colin Cross869691c2016-01-29 12:48:18 -080040
Christopher Ferris63860cb2015-11-16 17:30:32 -080041#include <private/bionic_config.h>
Christopher Ferris2b0638e2019-09-11 19:05:29 -070042#include <platform/bionic/malloc.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080043
Peter Collingbourne1e110fb2020-01-09 10:48:22 -080044#include "heap_tagging.h"
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080045#include "malloc_common.h"
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080046#include "malloc_limit.h"
Mitch Phillips3b21ada2020-01-07 15:47:47 -080047#include "malloc_tagged_pointers.h"
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070048
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080049// =============================================================================
50// Global variables instantations.
51// =============================================================================
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070052
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080053// Malloc hooks globals.
Christopher Ferrisdb478a62018-02-07 18:42:14 -080054void* (*volatile __malloc_hook)(size_t, const void*);
55void* (*volatile __realloc_hook)(void*, size_t, const void*);
56void (*volatile __free_hook)(void*, const void*);
57void* (*volatile __memalign_hook)(size_t, size_t, const void*);
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080058// =============================================================================
Christopher Ferris63860cb2015-11-16 17:30:32 -080059
60// =============================================================================
61// Allocation functions
62// =============================================================================
63extern "C" void* calloc(size_t n_elements, size_t elem_size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080064 auto dispatch_table = GetDispatchTable();
65 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -080066 return MaybeTagPointer(dispatch_table->calloc(n_elements, elem_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -080067 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -080068 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 Phillips3b21ada2020-01-07 15:47:47 -080072 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -080073}
74
75extern "C" void free(void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080076 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -080077 mem = MaybeUntagAndCheckPointer(mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080078 if (__predict_false(dispatch_table != nullptr)) {
79 dispatch_table->free(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -080080 } else {
81 Malloc(free)(mem);
82 }
83}
84
85extern "C" struct mallinfo mallinfo() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080086 auto dispatch_table = GetDispatchTable();
87 if (__predict_false(dispatch_table != nullptr)) {
88 return dispatch_table->mallinfo();
Christopher Ferris63860cb2015-11-16 17:30:32 -080089 }
90 return Malloc(mallinfo)();
91}
92
Christopher Ferris6c619a02019-03-01 17:59:51 -080093extern "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 Ferrisa1c0d2f2017-05-15 15:50:19 -0700101extern "C" int mallopt(int param, int value) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800102 auto dispatch_table = GetDispatchTable();
103 if (__predict_false(dispatch_table != nullptr)) {
104 return dispatch_table->mallopt(param, value);
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700105 }
106 return Malloc(mallopt)(param, value);
107}
108
Christopher Ferris63860cb2015-11-16 17:30:32 -0800109extern "C" void* malloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800110 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800111 void *result;
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800112 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800113 result = dispatch_table->malloc(bytes);
114 } else {
115 result = Malloc(malloc)(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800116 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800117 if (__predict_false(result == nullptr)) {
118 warning_log("malloc(%zu) failed: returning null pointer", bytes);
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800119 return nullptr;
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800120 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800121 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800122}
123
124extern "C" size_t malloc_usable_size(const void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800125 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800126 mem = MaybeUntagAndCheckPointer(mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800127 if (__predict_false(dispatch_table != nullptr)) {
128 return dispatch_table->malloc_usable_size(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800129 }
130 return Malloc(malloc_usable_size)(mem);
131}
132
133extern "C" void* memalign(size_t alignment, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800134 auto dispatch_table = GetDispatchTable();
135 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800136 return MaybeTagPointer(dispatch_table->memalign(alignment, bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800137 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800138 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 Phillips3b21ada2020-01-07 15:47:47 -0800142 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800143}
144
145extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800146 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800147 int result;
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800148 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800149 result = dispatch_table->posix_memalign(memptr, alignment, size);
150 } else {
151 result = Malloc(posix_memalign)(memptr, alignment, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800152 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800153 if (result == 0) {
154 *memptr = MaybeTagPointer(*memptr);
155 }
156 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800157}
158
Christopher Ferriscae21a92018-02-05 18:14:55 -0800159extern "C" void* aligned_alloc(size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800160 auto dispatch_table = GetDispatchTable();
161 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800162 return MaybeTagPointer(dispatch_table->aligned_alloc(alignment, size));
Christopher Ferriscae21a92018-02-05 18:14:55 -0800163 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800164 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 Phillips3b21ada2020-01-07 15:47:47 -0800168 return MaybeTagPointer(result);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800169}
170
Elliott Hughes390be502019-04-20 22:18:49 -0700171extern "C" __attribute__((__noinline__)) void* realloc(void* old_mem, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800172 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800173 old_mem = MaybeUntagAndCheckPointer(old_mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800174 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800175 return MaybeTagPointer(dispatch_table->realloc(old_mem, bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800176 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800177 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 Phillips3b21ada2020-01-07 15:47:47 -0800181 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800182}
183
Elliott Hughesb1770852018-09-18 12:52:42 -0700184extern "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 Hughesa21f6cc2019-02-25 13:21:04 -0800187 warning_log("reallocaray(%p, %zu, %zu) failed: returning null pointer",
188 old_mem, item_count, item_size);
Elliott Hughesb1770852018-09-18 12:52:42 -0700189 errno = ENOMEM;
190 return nullptr;
191 }
192 return realloc(old_mem, new_size);
193}
194
Christopher Ferris63860cb2015-11-16 17:30:32 -0800195#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
196extern "C" void* pvalloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800197 auto dispatch_table = GetDispatchTable();
198 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800199 return MaybeTagPointer(dispatch_table->pvalloc(bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800200 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800201 void* result = Malloc(pvalloc)(bytes);
202 if (__predict_false(result == nullptr)) {
203 warning_log("pvalloc(%zu) failed: returning null pointer", bytes);
204 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800205 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800206}
207
208extern "C" void* valloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800209 auto dispatch_table = GetDispatchTable();
210 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800211 return MaybeTagPointer(dispatch_table->valloc(bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800212 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800213 void* result = Malloc(valloc)(bytes);
214 if (__predict_false(result == nullptr)) {
215 warning_log("valloc(%zu) failed: returning null pointer", bytes);
216 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800217 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800218}
219#endif
Christopher Ferris63860cb2015-11-16 17:30:32 -0800220// =============================================================================
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000221
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800222struct CallbackWrapperArg {
223 void (*callback)(uintptr_t base, size_t size, void* arg);
224 void* arg;
225};
226
227void 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 Savitskiecc37e32018-12-14 15:57:21 +0000234// =============================================================================
Colin Cross869691c2016-01-29 12:48:18 -0800235// Exported for use by libmemunreachable.
236// =============================================================================
237
238// Calls callback for every allocation in the anonymous heap mapping
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800239// [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 Cross869691c2016-01-29 12:48:18 -0800243extern "C" int malloc_iterate(uintptr_t base, size_t size,
244 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800245 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800246 // 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 Ferris62e1e2c2019-02-04 12:26:02 -0800253 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800254 return dispatch_table->malloc_iterate(
255 untagged_base, size, CallbackWrapper, &wrapper_arg);
Colin Cross869691c2016-01-29 12:48:18 -0800256 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800257 return Malloc(malloc_iterate)(
258 untagged_base, size, CallbackWrapper, &wrapper_arg);
Colin Cross869691c2016-01-29 12:48:18 -0800259}
260
261// Disable calls to malloc so malloc_iterate gets a consistent view of
262// allocated memory.
263extern "C" void malloc_disable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800264 auto dispatch_table = GetDispatchTable();
265 if (__predict_false(dispatch_table != nullptr)) {
266 return dispatch_table->malloc_disable();
Colin Cross869691c2016-01-29 12:48:18 -0800267 }
268 return Malloc(malloc_disable)();
269}
270
271// Re-enable calls to malloc after a previous call to malloc_disable.
272extern "C" void malloc_enable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800273 auto dispatch_table = GetDispatchTable();
274 if (__predict_false(dispatch_table != nullptr)) {
275 return dispatch_table->malloc_enable();
Colin Cross869691c2016-01-29 12:48:18 -0800276 }
277 return Malloc(malloc_enable)();
278}
Colin Cross2d4721c2016-02-02 11:57:54 -0800279
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800280#if defined(LIBC_STATIC)
Colin Cross2d4721c2016-02-02 11:57:54 -0800281extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) {
282 return 0;
283}
284#endif
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800285
286#if __has_feature(hwaddress_sanitizer)
287// FIXME: implement these in HWASan allocator.
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800288extern "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 Ferrise4cdbc42019-02-08 17:30:58 -0800292 return 0;
293}
294
295extern "C" void __sanitizer_malloc_disable() {
296}
297
298extern "C" void __sanitizer_malloc_enable() {
299}
Christopher Ferrisfa10a3a2019-03-08 10:56:17 -0800300
301extern "C" int __sanitizer_malloc_info(int, FILE*) {
302 errno = ENOTSUP;
303 return -1;
304}
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800305#endif
306// =============================================================================
307
308// =============================================================================
309// Platform-internal mallopt variant.
310// =============================================================================
311#if defined(LIBC_STATIC)
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800312extern "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 Collingbourne1e110fb2020-01-09 10:48:22 -0800316 if (opcode == M_SET_HEAP_TAGGING_LEVEL) {
317 return SetHeapTaggingLevel(arg, arg_size);
318 }
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800319 errno = ENOTSUP;
320 return false;
321}
322#endif
323// =============================================================================