blob: 6b7006d15ccba26b3d87eea30ac26f78d2e2064a [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"
Peter Collingbourne5d3aa862020-09-11 15:05:17 -070049#include "memory_mitigation_state.h"
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070050
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080051// =============================================================================
52// Global variables instantations.
53// =============================================================================
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070054
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080055// Malloc hooks globals.
Christopher Ferrisdb478a62018-02-07 18:42:14 -080056void* (*volatile __malloc_hook)(size_t, const void*);
57void* (*volatile __realloc_hook)(void*, size_t, const void*);
58void (*volatile __free_hook)(void*, const void*);
59void* (*volatile __memalign_hook)(size_t, size_t, const void*);
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080060// =============================================================================
Christopher Ferris63860cb2015-11-16 17:30:32 -080061
62// =============================================================================
63// Allocation functions
64// =============================================================================
65extern "C" void* calloc(size_t n_elements, size_t elem_size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080066 auto dispatch_table = GetDispatchTable();
67 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -080068 return MaybeTagPointer(dispatch_table->calloc(n_elements, elem_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -080069 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -080070 void* result = Malloc(calloc)(n_elements, elem_size);
71 if (__predict_false(result == nullptr)) {
72 warning_log("calloc(%zu, %zu) failed: returning null pointer", n_elements, elem_size);
73 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -080074 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -080075}
76
77extern "C" void free(void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080078 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -080079 mem = MaybeUntagAndCheckPointer(mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080080 if (__predict_false(dispatch_table != nullptr)) {
81 dispatch_table->free(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -080082 } else {
83 Malloc(free)(mem);
84 }
85}
86
87extern "C" struct mallinfo mallinfo() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080088 auto dispatch_table = GetDispatchTable();
89 if (__predict_false(dispatch_table != nullptr)) {
90 return dispatch_table->mallinfo();
Christopher Ferris63860cb2015-11-16 17:30:32 -080091 }
92 return Malloc(mallinfo)();
93}
94
Christopher Ferris6c619a02019-03-01 17:59:51 -080095extern "C" int malloc_info(int options, FILE* fp) {
96 auto dispatch_table = GetDispatchTable();
97 if (__predict_false(dispatch_table != nullptr)) {
98 return dispatch_table->malloc_info(options, fp);
99 }
100 return Malloc(malloc_info)(options, fp);
101}
102
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700103extern "C" int mallopt(int param, int value) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800104 auto dispatch_table = GetDispatchTable();
105 if (__predict_false(dispatch_table != nullptr)) {
106 return dispatch_table->mallopt(param, value);
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700107 }
108 return Malloc(mallopt)(param, value);
109}
110
Christopher Ferris63860cb2015-11-16 17:30:32 -0800111extern "C" void* malloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800112 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800113 void *result;
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800114 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800115 result = dispatch_table->malloc(bytes);
116 } else {
117 result = Malloc(malloc)(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800118 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800119 if (__predict_false(result == nullptr)) {
120 warning_log("malloc(%zu) failed: returning null pointer", bytes);
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800121 return nullptr;
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800122 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800123 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800124}
125
126extern "C" size_t malloc_usable_size(const void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800127 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800128 mem = MaybeUntagAndCheckPointer(mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800129 if (__predict_false(dispatch_table != nullptr)) {
130 return dispatch_table->malloc_usable_size(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800131 }
132 return Malloc(malloc_usable_size)(mem);
133}
134
135extern "C" void* memalign(size_t alignment, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800136 auto dispatch_table = GetDispatchTable();
137 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800138 return MaybeTagPointer(dispatch_table->memalign(alignment, bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800139 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800140 void* result = Malloc(memalign)(alignment, bytes);
141 if (__predict_false(result == nullptr)) {
142 warning_log("memalign(%zu, %zu) failed: returning null pointer", alignment, bytes);
143 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800144 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800145}
146
147extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800148 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800149 int result;
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800150 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800151 result = dispatch_table->posix_memalign(memptr, alignment, size);
152 } else {
153 result = Malloc(posix_memalign)(memptr, alignment, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800154 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800155 if (result == 0) {
156 *memptr = MaybeTagPointer(*memptr);
157 }
158 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800159}
160
Christopher Ferriscae21a92018-02-05 18:14:55 -0800161extern "C" void* aligned_alloc(size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800162 auto dispatch_table = GetDispatchTable();
163 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800164 return MaybeTagPointer(dispatch_table->aligned_alloc(alignment, size));
Christopher Ferriscae21a92018-02-05 18:14:55 -0800165 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800166 void* result = Malloc(aligned_alloc)(alignment, size);
167 if (__predict_false(result == nullptr)) {
168 warning_log("aligned_alloc(%zu, %zu) failed: returning null pointer", alignment, size);
169 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800170 return MaybeTagPointer(result);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800171}
172
Elliott Hughes390be502019-04-20 22:18:49 -0700173extern "C" __attribute__((__noinline__)) void* realloc(void* old_mem, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800174 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800175 old_mem = MaybeUntagAndCheckPointer(old_mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800176 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800177 return MaybeTagPointer(dispatch_table->realloc(old_mem, bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800178 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800179 void* result = Malloc(realloc)(old_mem, bytes);
180 if (__predict_false(result == nullptr && bytes != 0)) {
181 warning_log("realloc(%p, %zu) failed: returning null pointer", old_mem, bytes);
182 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800183 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800184}
185
Elliott Hughesb1770852018-09-18 12:52:42 -0700186extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) {
187 size_t new_size;
188 if (__builtin_mul_overflow(item_count, item_size, &new_size)) {
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800189 warning_log("reallocaray(%p, %zu, %zu) failed: returning null pointer",
190 old_mem, item_count, item_size);
Elliott Hughesb1770852018-09-18 12:52:42 -0700191 errno = ENOMEM;
192 return nullptr;
193 }
194 return realloc(old_mem, new_size);
195}
196
Christopher Ferris63860cb2015-11-16 17:30:32 -0800197#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
198extern "C" void* pvalloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800199 auto dispatch_table = GetDispatchTable();
200 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800201 return MaybeTagPointer(dispatch_table->pvalloc(bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800202 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800203 void* result = Malloc(pvalloc)(bytes);
204 if (__predict_false(result == nullptr)) {
205 warning_log("pvalloc(%zu) failed: returning null pointer", bytes);
206 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800207 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800208}
209
210extern "C" void* valloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800211 auto dispatch_table = GetDispatchTable();
212 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800213 return MaybeTagPointer(dispatch_table->valloc(bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800214 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800215 void* result = Malloc(valloc)(bytes);
216 if (__predict_false(result == nullptr)) {
217 warning_log("valloc(%zu) failed: returning null pointer", bytes);
218 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800219 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800220}
221#endif
Christopher Ferris63860cb2015-11-16 17:30:32 -0800222// =============================================================================
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000223
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800224struct CallbackWrapperArg {
225 void (*callback)(uintptr_t base, size_t size, void* arg);
226 void* arg;
227};
228
229void CallbackWrapper(uintptr_t base, size_t size, void* arg) {
230 CallbackWrapperArg* wrapper_arg = reinterpret_cast<CallbackWrapperArg*>(arg);
231 wrapper_arg->callback(
232 reinterpret_cast<uintptr_t>(MaybeTagPointer(reinterpret_cast<void*>(base))),
233 size, wrapper_arg->arg);
234}
235
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000236// =============================================================================
Colin Cross869691c2016-01-29 12:48:18 -0800237// Exported for use by libmemunreachable.
238// =============================================================================
239
240// Calls callback for every allocation in the anonymous heap mapping
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800241// [base, base+size). Must be called between malloc_disable and malloc_enable.
242// `base` in this can take either a tagged or untagged pointer, but we always
243// provide a tagged pointer to the `base` argument of `callback` if the kernel
244// supports tagged pointers.
Colin Cross869691c2016-01-29 12:48:18 -0800245extern "C" int malloc_iterate(uintptr_t base, size_t size,
246 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800247 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800248 // Wrap the malloc_iterate callback we were provided, in order to provide
249 // pointer tagging support.
250 CallbackWrapperArg wrapper_arg;
251 wrapper_arg.callback = callback;
252 wrapper_arg.arg = arg;
253 uintptr_t untagged_base =
254 reinterpret_cast<uintptr_t>(UntagPointer(reinterpret_cast<void*>(base)));
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800255 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800256 return dispatch_table->malloc_iterate(
257 untagged_base, size, CallbackWrapper, &wrapper_arg);
Colin Cross869691c2016-01-29 12:48:18 -0800258 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800259 return Malloc(malloc_iterate)(
260 untagged_base, size, CallbackWrapper, &wrapper_arg);
Colin Cross869691c2016-01-29 12:48:18 -0800261}
262
263// Disable calls to malloc so malloc_iterate gets a consistent view of
264// allocated memory.
265extern "C" void malloc_disable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800266 auto dispatch_table = GetDispatchTable();
267 if (__predict_false(dispatch_table != nullptr)) {
268 return dispatch_table->malloc_disable();
Colin Cross869691c2016-01-29 12:48:18 -0800269 }
270 return Malloc(malloc_disable)();
271}
272
273// Re-enable calls to malloc after a previous call to malloc_disable.
274extern "C" void malloc_enable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800275 auto dispatch_table = GetDispatchTable();
276 if (__predict_false(dispatch_table != nullptr)) {
277 return dispatch_table->malloc_enable();
Colin Cross869691c2016-01-29 12:48:18 -0800278 }
279 return Malloc(malloc_enable)();
280}
Colin Cross2d4721c2016-02-02 11:57:54 -0800281
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800282#if defined(LIBC_STATIC)
Colin Cross2d4721c2016-02-02 11:57:54 -0800283extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) {
284 return 0;
285}
286#endif
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800287
288#if __has_feature(hwaddress_sanitizer)
289// FIXME: implement these in HWASan allocator.
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800290extern "C" int __sanitizer_malloc_iterate(uintptr_t base __unused, size_t size __unused,
291 void (*callback)(uintptr_t base, size_t size, void* arg)
292 __unused,
293 void* arg __unused) {
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800294 return 0;
295}
296
297extern "C" void __sanitizer_malloc_disable() {
298}
299
300extern "C" void __sanitizer_malloc_enable() {
301}
Christopher Ferrisfa10a3a2019-03-08 10:56:17 -0800302
303extern "C" int __sanitizer_malloc_info(int, FILE*) {
304 errno = ENOTSUP;
305 return -1;
306}
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800307#endif
308// =============================================================================
309
310// =============================================================================
311// Platform-internal mallopt variant.
312// =============================================================================
313#if defined(LIBC_STATIC)
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800314extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size) {
315 if (opcode == M_SET_ALLOCATION_LIMIT_BYTES) {
316 return LimitEnable(arg, arg_size);
317 }
Peter Collingbourne1e110fb2020-01-09 10:48:22 -0800318 if (opcode == M_SET_HEAP_TAGGING_LEVEL) {
319 return SetHeapTaggingLevel(arg, arg_size);
320 }
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800321 if (opcode == M_INITIALIZE_GWP_ASAN) {
322 if (arg == nullptr || arg_size != sizeof(bool)) {
323 errno = EINVAL;
324 return false;
325 }
Mitch Phillipsbba80dc2020-02-11 14:42:14 -0800326 __libc_globals.mutate([&](libc_globals* globals) {
327 return MaybeInitGwpAsan(globals, *reinterpret_cast<bool*>(arg));
328 });
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800329 }
Peter Collingbourne5d3aa862020-09-11 15:05:17 -0700330 if (opcode == M_DISABLE_MEMORY_MITIGATIONS) {
331 return DisableMemoryMitigations(arg, arg_size);
332 }
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800333 errno = ENOTSUP;
334 return false;
335}
336#endif
337// =============================================================================
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800338
339static constexpr MallocDispatch __libc_malloc_default_dispatch __attribute__((unused)) = {
340 Malloc(calloc),
341 Malloc(free),
342 Malloc(mallinfo),
343 Malloc(malloc),
344 Malloc(malloc_usable_size),
345 Malloc(memalign),
346 Malloc(posix_memalign),
347#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
348 Malloc(pvalloc),
349#endif
350 Malloc(realloc),
351#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
352 Malloc(valloc),
353#endif
354 Malloc(malloc_iterate),
355 Malloc(malloc_disable),
356 Malloc(malloc_enable),
357 Malloc(mallopt),
358 Malloc(aligned_alloc),
359 Malloc(malloc_info),
360};
361
362const MallocDispatch* NativeAllocatorDispatch() {
363 return &__libc_malloc_default_dispatch;
364}