blob: da68c80ce47e35973b70e4cd87aada4c30ee25de [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
Mitch Phillipsf3968e82020-01-31 19:57:04 -080044#include "gwp_asan_wrappers.h"
Peter Collingbourne1e110fb2020-01-09 10:48:22 -080045#include "heap_tagging.h"
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080046#include "malloc_common.h"
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080047#include "malloc_limit.h"
Mitch Phillips3b21ada2020-01-07 15:47:47 -080048#include "malloc_tagged_pointers.h"
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070049
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080050// =============================================================================
51// Global variables instantations.
52// =============================================================================
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070053
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080054// Malloc hooks globals.
Christopher Ferrisdb478a62018-02-07 18:42:14 -080055void* (*volatile __malloc_hook)(size_t, const void*);
56void* (*volatile __realloc_hook)(void*, size_t, const void*);
57void (*volatile __free_hook)(void*, const void*);
58void* (*volatile __memalign_hook)(size_t, size_t, const void*);
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080059// =============================================================================
Christopher Ferris63860cb2015-11-16 17:30:32 -080060
61// =============================================================================
62// Allocation functions
63// =============================================================================
64extern "C" void* calloc(size_t n_elements, size_t elem_size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080065 auto dispatch_table = GetDispatchTable();
66 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -080067 return MaybeTagPointer(dispatch_table->calloc(n_elements, elem_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -080068 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -080069 void* result = Malloc(calloc)(n_elements, elem_size);
70 if (__predict_false(result == nullptr)) {
71 warning_log("calloc(%zu, %zu) failed: returning null pointer", n_elements, elem_size);
72 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -080073 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -080074}
75
76extern "C" void free(void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080077 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -080078 mem = MaybeUntagAndCheckPointer(mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080079 if (__predict_false(dispatch_table != nullptr)) {
80 dispatch_table->free(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -080081 } else {
82 Malloc(free)(mem);
83 }
84}
85
86extern "C" struct mallinfo mallinfo() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080087 auto dispatch_table = GetDispatchTable();
88 if (__predict_false(dispatch_table != nullptr)) {
89 return dispatch_table->mallinfo();
Christopher Ferris63860cb2015-11-16 17:30:32 -080090 }
91 return Malloc(mallinfo)();
92}
93
Christopher Ferris6c619a02019-03-01 17:59:51 -080094extern "C" int malloc_info(int options, FILE* fp) {
95 auto dispatch_table = GetDispatchTable();
96 if (__predict_false(dispatch_table != nullptr)) {
97 return dispatch_table->malloc_info(options, fp);
98 }
99 return Malloc(malloc_info)(options, fp);
100}
101
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700102extern "C" int mallopt(int param, int value) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800103 auto dispatch_table = GetDispatchTable();
104 if (__predict_false(dispatch_table != nullptr)) {
105 return dispatch_table->mallopt(param, value);
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700106 }
107 return Malloc(mallopt)(param, value);
108}
109
Christopher Ferris63860cb2015-11-16 17:30:32 -0800110extern "C" void* malloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800111 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800112 void *result;
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800113 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800114 result = dispatch_table->malloc(bytes);
115 } else {
116 result = Malloc(malloc)(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800117 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800118 if (__predict_false(result == nullptr)) {
119 warning_log("malloc(%zu) failed: returning null pointer", bytes);
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800120 return nullptr;
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800121 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800122 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800123}
124
125extern "C" size_t malloc_usable_size(const void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800126 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800127 mem = MaybeUntagAndCheckPointer(mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800128 if (__predict_false(dispatch_table != nullptr)) {
129 return dispatch_table->malloc_usable_size(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800130 }
131 return Malloc(malloc_usable_size)(mem);
132}
133
134extern "C" void* memalign(size_t alignment, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800135 auto dispatch_table = GetDispatchTable();
136 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800137 return MaybeTagPointer(dispatch_table->memalign(alignment, bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800138 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800139 void* result = Malloc(memalign)(alignment, bytes);
140 if (__predict_false(result == nullptr)) {
141 warning_log("memalign(%zu, %zu) failed: returning null pointer", alignment, bytes);
142 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800143 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800144}
145
146extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800147 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800148 int result;
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800149 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800150 result = dispatch_table->posix_memalign(memptr, alignment, size);
151 } else {
152 result = Malloc(posix_memalign)(memptr, alignment, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800153 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800154 if (result == 0) {
155 *memptr = MaybeTagPointer(*memptr);
156 }
157 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800158}
159
Christopher Ferriscae21a92018-02-05 18:14:55 -0800160extern "C" void* aligned_alloc(size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800161 auto dispatch_table = GetDispatchTable();
162 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800163 return MaybeTagPointer(dispatch_table->aligned_alloc(alignment, size));
Christopher Ferriscae21a92018-02-05 18:14:55 -0800164 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800165 void* result = Malloc(aligned_alloc)(alignment, size);
166 if (__predict_false(result == nullptr)) {
167 warning_log("aligned_alloc(%zu, %zu) failed: returning null pointer", alignment, size);
168 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800169 return MaybeTagPointer(result);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800170}
171
Elliott Hughes390be502019-04-20 22:18:49 -0700172extern "C" __attribute__((__noinline__)) void* realloc(void* old_mem, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800173 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800174 old_mem = MaybeUntagAndCheckPointer(old_mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800175 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800176 return MaybeTagPointer(dispatch_table->realloc(old_mem, bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800177 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800178 void* result = Malloc(realloc)(old_mem, bytes);
179 if (__predict_false(result == nullptr && bytes != 0)) {
180 warning_log("realloc(%p, %zu) failed: returning null pointer", old_mem, bytes);
181 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800182 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800183}
184
Elliott Hughesb1770852018-09-18 12:52:42 -0700185extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) {
186 size_t new_size;
187 if (__builtin_mul_overflow(item_count, item_size, &new_size)) {
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800188 warning_log("reallocaray(%p, %zu, %zu) failed: returning null pointer",
189 old_mem, item_count, item_size);
Elliott Hughesb1770852018-09-18 12:52:42 -0700190 errno = ENOMEM;
191 return nullptr;
192 }
193 return realloc(old_mem, new_size);
194}
195
Christopher Ferris63860cb2015-11-16 17:30:32 -0800196#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
197extern "C" void* pvalloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800198 auto dispatch_table = GetDispatchTable();
199 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800200 return MaybeTagPointer(dispatch_table->pvalloc(bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800201 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800202 void* result = Malloc(pvalloc)(bytes);
203 if (__predict_false(result == nullptr)) {
204 warning_log("pvalloc(%zu) failed: returning null pointer", bytes);
205 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800206 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800207}
208
209extern "C" void* valloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800210 auto dispatch_table = GetDispatchTable();
211 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800212 return MaybeTagPointer(dispatch_table->valloc(bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800213 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800214 void* result = Malloc(valloc)(bytes);
215 if (__predict_false(result == nullptr)) {
216 warning_log("valloc(%zu) failed: returning null pointer", bytes);
217 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800218 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800219}
220#endif
Christopher Ferris63860cb2015-11-16 17:30:32 -0800221// =============================================================================
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000222
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800223struct CallbackWrapperArg {
224 void (*callback)(uintptr_t base, size_t size, void* arg);
225 void* arg;
226};
227
228void CallbackWrapper(uintptr_t base, size_t size, void* arg) {
229 CallbackWrapperArg* wrapper_arg = reinterpret_cast<CallbackWrapperArg*>(arg);
230 wrapper_arg->callback(
231 reinterpret_cast<uintptr_t>(MaybeTagPointer(reinterpret_cast<void*>(base))),
232 size, wrapper_arg->arg);
233}
234
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000235// =============================================================================
Colin Cross869691c2016-01-29 12:48:18 -0800236// Exported for use by libmemunreachable.
237// =============================================================================
238
239// Calls callback for every allocation in the anonymous heap mapping
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800240// [base, base+size). Must be called between malloc_disable and malloc_enable.
241// `base` in this can take either a tagged or untagged pointer, but we always
242// provide a tagged pointer to the `base` argument of `callback` if the kernel
243// supports tagged pointers.
Colin Cross869691c2016-01-29 12:48:18 -0800244extern "C" int malloc_iterate(uintptr_t base, size_t size,
245 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800246 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800247 // Wrap the malloc_iterate callback we were provided, in order to provide
248 // pointer tagging support.
249 CallbackWrapperArg wrapper_arg;
250 wrapper_arg.callback = callback;
251 wrapper_arg.arg = arg;
252 uintptr_t untagged_base =
253 reinterpret_cast<uintptr_t>(UntagPointer(reinterpret_cast<void*>(base)));
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800254 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800255 return dispatch_table->malloc_iterate(
256 untagged_base, size, CallbackWrapper, &wrapper_arg);
Colin Cross869691c2016-01-29 12:48:18 -0800257 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800258 return Malloc(malloc_iterate)(
259 untagged_base, size, CallbackWrapper, &wrapper_arg);
Colin Cross869691c2016-01-29 12:48:18 -0800260}
261
262// Disable calls to malloc so malloc_iterate gets a consistent view of
263// allocated memory.
264extern "C" void malloc_disable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800265 auto dispatch_table = GetDispatchTable();
266 if (__predict_false(dispatch_table != nullptr)) {
267 return dispatch_table->malloc_disable();
Colin Cross869691c2016-01-29 12:48:18 -0800268 }
269 return Malloc(malloc_disable)();
270}
271
272// Re-enable calls to malloc after a previous call to malloc_disable.
273extern "C" void malloc_enable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800274 auto dispatch_table = GetDispatchTable();
275 if (__predict_false(dispatch_table != nullptr)) {
276 return dispatch_table->malloc_enable();
Colin Cross869691c2016-01-29 12:48:18 -0800277 }
278 return Malloc(malloc_enable)();
279}
Colin Cross2d4721c2016-02-02 11:57:54 -0800280
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800281#if defined(LIBC_STATIC)
Colin Cross2d4721c2016-02-02 11:57:54 -0800282extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) {
283 return 0;
284}
285#endif
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800286
287#if __has_feature(hwaddress_sanitizer)
288// FIXME: implement these in HWASan allocator.
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800289extern "C" int __sanitizer_malloc_iterate(uintptr_t base __unused, size_t size __unused,
290 void (*callback)(uintptr_t base, size_t size, void* arg)
291 __unused,
292 void* arg __unused) {
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800293 return 0;
294}
295
296extern "C" void __sanitizer_malloc_disable() {
297}
298
299extern "C" void __sanitizer_malloc_enable() {
300}
Christopher Ferrisfa10a3a2019-03-08 10:56:17 -0800301
302extern "C" int __sanitizer_malloc_info(int, FILE*) {
303 errno = ENOTSUP;
304 return -1;
305}
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800306#endif
307// =============================================================================
308
309// =============================================================================
310// Platform-internal mallopt variant.
311// =============================================================================
312#if defined(LIBC_STATIC)
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800313extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size) {
314 if (opcode == M_SET_ALLOCATION_LIMIT_BYTES) {
315 return LimitEnable(arg, arg_size);
316 }
Peter Collingbourne1e110fb2020-01-09 10:48:22 -0800317 if (opcode == M_SET_HEAP_TAGGING_LEVEL) {
318 return SetHeapTaggingLevel(arg, arg_size);
319 }
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800320 if (opcode == M_INITIALIZE_GWP_ASAN) {
321 if (arg == nullptr || arg_size != sizeof(bool)) {
322 errno = EINVAL;
323 return false;
324 }
325 return MaybeInitGwpAsan(*reinterpret_cast<bool*>(arg));
326 }
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800327 errno = ENOTSUP;
328 return false;
329}
330#endif
331// =============================================================================
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800332
333static constexpr MallocDispatch __libc_malloc_default_dispatch __attribute__((unused)) = {
334 Malloc(calloc),
335 Malloc(free),
336 Malloc(mallinfo),
337 Malloc(malloc),
338 Malloc(malloc_usable_size),
339 Malloc(memalign),
340 Malloc(posix_memalign),
341#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
342 Malloc(pvalloc),
343#endif
344 Malloc(realloc),
345#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
346 Malloc(valloc),
347#endif
348 Malloc(malloc_iterate),
349 Malloc(malloc_disable),
350 Malloc(malloc_enable),
351 Malloc(mallopt),
352 Malloc(aligned_alloc),
353 Malloc(malloc_info),
354};
355
356const MallocDispatch* NativeAllocatorDispatch() {
357 return &__libc_malloc_default_dispatch;
358}