blob: 199d7fcbebfdf6b70e26bcf5ae1ff911e76009b5 [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
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080044#include "malloc_common.h"
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080045#include "malloc_limit.h"
Mitch Phillips43d5f9d2020-01-07 15:47:47 -080046#include "malloc_tagged_pointers.h"
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070047
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080048// =============================================================================
49// Global variables instantations.
50// =============================================================================
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070051
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080052// Malloc hooks globals.
Christopher Ferrisdb478a62018-02-07 18:42:14 -080053void* (*volatile __malloc_hook)(size_t, const void*);
54void* (*volatile __realloc_hook)(void*, size_t, const void*);
55void (*volatile __free_hook)(void*, const void*);
56void* (*volatile __memalign_hook)(size_t, size_t, const void*);
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080057// =============================================================================
Christopher Ferris63860cb2015-11-16 17:30:32 -080058
59// =============================================================================
60// Allocation functions
61// =============================================================================
62extern "C" void* calloc(size_t n_elements, size_t elem_size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080063 auto dispatch_table = GetDispatchTable();
64 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips43d5f9d2020-01-07 15:47:47 -080065 return MaybeTagPointer(dispatch_table->calloc(n_elements, elem_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -080066 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -080067 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 Phillips43d5f9d2020-01-07 15:47:47 -080071 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -080072}
73
74extern "C" void free(void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080075 auto dispatch_table = GetDispatchTable();
Mitch Phillips43d5f9d2020-01-07 15:47:47 -080076 mem = MaybeUntagAndCheckPointer(mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080077 if (__predict_false(dispatch_table != nullptr)) {
78 dispatch_table->free(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -080079 } else {
80 Malloc(free)(mem);
81 }
82}
83
84extern "C" struct mallinfo mallinfo() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080085 auto dispatch_table = GetDispatchTable();
86 if (__predict_false(dispatch_table != nullptr)) {
87 return dispatch_table->mallinfo();
Christopher Ferris63860cb2015-11-16 17:30:32 -080088 }
89 return Malloc(mallinfo)();
90}
91
Christopher Ferris6c619a02019-03-01 17:59:51 -080092extern "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 Ferrisa1c0d2f2017-05-15 15:50:19 -0700100extern "C" int mallopt(int param, int value) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800101 auto dispatch_table = GetDispatchTable();
102 if (__predict_false(dispatch_table != nullptr)) {
103 return dispatch_table->mallopt(param, value);
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700104 }
105 return Malloc(mallopt)(param, value);
106}
107
Christopher Ferris63860cb2015-11-16 17:30:32 -0800108extern "C" void* malloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800109 auto dispatch_table = GetDispatchTable();
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800110 void *result;
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800111 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800112 result = dispatch_table->malloc(bytes);
113 } else {
114 result = Malloc(malloc)(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800115 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800116 if (__predict_false(result == nullptr)) {
117 warning_log("malloc(%zu) failed: returning null pointer", bytes);
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800118 return nullptr;
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800119 }
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800120 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800121}
122
123extern "C" size_t malloc_usable_size(const void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800124 auto dispatch_table = GetDispatchTable();
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800125 mem = MaybeUntagAndCheckPointer(mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800126 if (__predict_false(dispatch_table != nullptr)) {
127 return dispatch_table->malloc_usable_size(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800128 }
129 return Malloc(malloc_usable_size)(mem);
130}
131
132extern "C" void* memalign(size_t alignment, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800133 auto dispatch_table = GetDispatchTable();
134 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800135 return MaybeTagPointer(dispatch_table->memalign(alignment, bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800136 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800137 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 Phillips43d5f9d2020-01-07 15:47:47 -0800141 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800142}
143
144extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800145 auto dispatch_table = GetDispatchTable();
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800146 int result;
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800147 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800148 result = dispatch_table->posix_memalign(memptr, alignment, size);
149 } else {
150 result = Malloc(posix_memalign)(memptr, alignment, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800151 }
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800152 if (result == 0) {
153 *memptr = MaybeTagPointer(*memptr);
154 }
155 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800156}
157
Christopher Ferriscae21a92018-02-05 18:14:55 -0800158extern "C" void* aligned_alloc(size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800159 auto dispatch_table = GetDispatchTable();
160 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800161 return MaybeTagPointer(dispatch_table->aligned_alloc(alignment, size));
Christopher Ferriscae21a92018-02-05 18:14:55 -0800162 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800163 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 Phillips43d5f9d2020-01-07 15:47:47 -0800167 return MaybeTagPointer(result);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800168}
169
Elliott Hughes390be502019-04-20 22:18:49 -0700170extern "C" __attribute__((__noinline__)) void* realloc(void* old_mem, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800171 auto dispatch_table = GetDispatchTable();
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800172 old_mem = MaybeUntagAndCheckPointer(old_mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800173 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800174 return MaybeTagPointer(dispatch_table->realloc(old_mem, bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800175 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800176 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 Phillips43d5f9d2020-01-07 15:47:47 -0800180 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800181}
182
Elliott Hughesb1770852018-09-18 12:52:42 -0700183extern "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 Hughesa21f6cc2019-02-25 13:21:04 -0800186 warning_log("reallocaray(%p, %zu, %zu) failed: returning null pointer",
187 old_mem, item_count, item_size);
Elliott Hughesb1770852018-09-18 12:52:42 -0700188 errno = ENOMEM;
189 return nullptr;
190 }
191 return realloc(old_mem, new_size);
192}
193
Christopher Ferris63860cb2015-11-16 17:30:32 -0800194#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
195extern "C" void* pvalloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800196 auto dispatch_table = GetDispatchTable();
197 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800198 return MaybeTagPointer(dispatch_table->pvalloc(bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800199 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800200 void* result = Malloc(pvalloc)(bytes);
201 if (__predict_false(result == nullptr)) {
202 warning_log("pvalloc(%zu) failed: returning null pointer", bytes);
203 }
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800204 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800205}
206
207extern "C" void* valloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800208 auto dispatch_table = GetDispatchTable();
209 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800210 return MaybeTagPointer(dispatch_table->valloc(bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800211 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800212 void* result = Malloc(valloc)(bytes);
213 if (__predict_false(result == nullptr)) {
214 warning_log("valloc(%zu) failed: returning null pointer", bytes);
215 }
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800216 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800217}
218#endif
Christopher Ferris63860cb2015-11-16 17:30:32 -0800219// =============================================================================
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000220
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800221struct CallbackWrapperArg {
222 void (*callback)(uintptr_t base, size_t size, void* arg);
223 void* arg;
224};
225
226void 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 Savitskiecc37e32018-12-14 15:57:21 +0000233// =============================================================================
Colin Cross869691c2016-01-29 12:48:18 -0800234// Exported for use by libmemunreachable.
235// =============================================================================
236
237// Calls callback for every allocation in the anonymous heap mapping
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800238// [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 Cross869691c2016-01-29 12:48:18 -0800242extern "C" int malloc_iterate(uintptr_t base, size_t size,
243 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800244 auto dispatch_table = GetDispatchTable();
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800245 // 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 Ferris62e1e2c2019-02-04 12:26:02 -0800252 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800253 return dispatch_table->malloc_iterate(
254 untagged_base, size, CallbackWrapper, &wrapper_arg);
Colin Cross869691c2016-01-29 12:48:18 -0800255 }
Mitch Phillips43d5f9d2020-01-07 15:47:47 -0800256 return Malloc(malloc_iterate)(
257 untagged_base, size, CallbackWrapper, &wrapper_arg);
Colin Cross869691c2016-01-29 12:48:18 -0800258}
259
260// Disable calls to malloc so malloc_iterate gets a consistent view of
261// allocated memory.
262extern "C" void malloc_disable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800263 auto dispatch_table = GetDispatchTable();
264 if (__predict_false(dispatch_table != nullptr)) {
265 return dispatch_table->malloc_disable();
Colin Cross869691c2016-01-29 12:48:18 -0800266 }
267 return Malloc(malloc_disable)();
268}
269
270// Re-enable calls to malloc after a previous call to malloc_disable.
271extern "C" void malloc_enable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800272 auto dispatch_table = GetDispatchTable();
273 if (__predict_false(dispatch_table != nullptr)) {
274 return dispatch_table->malloc_enable();
Colin Cross869691c2016-01-29 12:48:18 -0800275 }
276 return Malloc(malloc_enable)();
277}
Colin Cross2d4721c2016-02-02 11:57:54 -0800278
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800279#if defined(LIBC_STATIC)
Colin Cross2d4721c2016-02-02 11:57:54 -0800280extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) {
281 return 0;
282}
283#endif
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800284
285#if __has_feature(hwaddress_sanitizer)
286// FIXME: implement these in HWASan allocator.
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800287extern "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 Ferrise4cdbc42019-02-08 17:30:58 -0800291 return 0;
292}
293
294extern "C" void __sanitizer_malloc_disable() {
295}
296
297extern "C" void __sanitizer_malloc_enable() {
298}
Christopher Ferrisfa10a3a2019-03-08 10:56:17 -0800299
300extern "C" int __sanitizer_malloc_info(int, FILE*) {
301 errno = ENOTSUP;
302 return -1;
303}
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800304#endif
305// =============================================================================
306
307// =============================================================================
308// Platform-internal mallopt variant.
309// =============================================================================
310#if defined(LIBC_STATIC)
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800311extern "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 Ferrise4cdbc42019-02-08 17:30:58 -0800315 errno = ENOTSUP;
316 return false;
317}
318#endif
319// =============================================================================