blob: 0ee12a7fdabc540c91dcb7659b5d5673efc3fb75 [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 Ferris2b0638e2019-09-11 19:05:29 -070041#include <platform/bionic/malloc.h>
Mitch Phillips2210b8d2020-11-25 16:48:54 -080042#include <private/ScopedPthreadMutexLocker.h>
43#include <private/bionic_config.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -080044
Mitch Phillipsf3968e82020-01-31 19:57:04 -080045#include "gwp_asan_wrappers.h"
Peter Collingbourne1e110fb2020-01-09 10:48:22 -080046#include "heap_tagging.h"
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080047#include "malloc_common.h"
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080048#include "malloc_limit.h"
Mitch Phillips3b21ada2020-01-07 15:47:47 -080049#include "malloc_tagged_pointers.h"
Peter Collingbourne5d3aa862020-09-11 15:05:17 -070050#include "memory_mitigation_state.h"
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070051
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080052// =============================================================================
53// Global variables instantations.
54// =============================================================================
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070055
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080056// Malloc hooks globals.
Christopher Ferrisdb478a62018-02-07 18:42:14 -080057void* (*volatile __malloc_hook)(size_t, const void*);
58void* (*volatile __realloc_hook)(void*, size_t, const void*);
59void (*volatile __free_hook)(void*, const void*);
60void* (*volatile __memalign_hook)(size_t, size_t, const void*);
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080061// =============================================================================
Christopher Ferris63860cb2015-11-16 17:30:32 -080062
63// =============================================================================
64// Allocation functions
65// =============================================================================
66extern "C" void* calloc(size_t n_elements, size_t elem_size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080067 auto dispatch_table = GetDispatchTable();
68 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -080069 return MaybeTagPointer(dispatch_table->calloc(n_elements, elem_size));
Christopher Ferris63860cb2015-11-16 17:30:32 -080070 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -080071 void* result = Malloc(calloc)(n_elements, elem_size);
72 if (__predict_false(result == nullptr)) {
73 warning_log("calloc(%zu, %zu) failed: returning null pointer", n_elements, elem_size);
74 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -080075 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -080076}
77
78extern "C" void free(void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080079 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -080080 mem = MaybeUntagAndCheckPointer(mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080081 if (__predict_false(dispatch_table != nullptr)) {
82 dispatch_table->free(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -080083 } else {
84 Malloc(free)(mem);
85 }
86}
87
88extern "C" struct mallinfo mallinfo() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080089 auto dispatch_table = GetDispatchTable();
90 if (__predict_false(dispatch_table != nullptr)) {
91 return dispatch_table->mallinfo();
Christopher Ferris63860cb2015-11-16 17:30:32 -080092 }
93 return Malloc(mallinfo)();
94}
95
Christopher Ferris6c619a02019-03-01 17:59:51 -080096extern "C" int malloc_info(int options, FILE* fp) {
97 auto dispatch_table = GetDispatchTable();
98 if (__predict_false(dispatch_table != nullptr)) {
99 return dispatch_table->malloc_info(options, fp);
100 }
101 return Malloc(malloc_info)(options, fp);
102}
103
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700104extern "C" int mallopt(int param, int value) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800105 auto dispatch_table = GetDispatchTable();
106 if (__predict_false(dispatch_table != nullptr)) {
107 return dispatch_table->mallopt(param, value);
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700108 }
109 return Malloc(mallopt)(param, value);
110}
111
Christopher Ferris63860cb2015-11-16 17:30:32 -0800112extern "C" void* malloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800113 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800114 void *result;
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800115 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800116 result = dispatch_table->malloc(bytes);
117 } else {
118 result = Malloc(malloc)(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800119 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800120 if (__predict_false(result == nullptr)) {
121 warning_log("malloc(%zu) failed: returning null pointer", bytes);
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800122 return nullptr;
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800123 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800124 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800125}
126
127extern "C" size_t malloc_usable_size(const void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800128 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800129 mem = MaybeUntagAndCheckPointer(mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800130 if (__predict_false(dispatch_table != nullptr)) {
131 return dispatch_table->malloc_usable_size(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800132 }
133 return Malloc(malloc_usable_size)(mem);
134}
135
136extern "C" void* memalign(size_t alignment, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800137 auto dispatch_table = GetDispatchTable();
138 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800139 return MaybeTagPointer(dispatch_table->memalign(alignment, bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800140 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800141 void* result = Malloc(memalign)(alignment, bytes);
142 if (__predict_false(result == nullptr)) {
143 warning_log("memalign(%zu, %zu) failed: returning null pointer", alignment, bytes);
144 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800145 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800146}
147
148extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800149 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800150 int result;
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800151 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800152 result = dispatch_table->posix_memalign(memptr, alignment, size);
153 } else {
154 result = Malloc(posix_memalign)(memptr, alignment, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800155 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800156 if (result == 0) {
157 *memptr = MaybeTagPointer(*memptr);
158 }
159 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800160}
161
Christopher Ferriscae21a92018-02-05 18:14:55 -0800162extern "C" void* aligned_alloc(size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800163 auto dispatch_table = GetDispatchTable();
164 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800165 return MaybeTagPointer(dispatch_table->aligned_alloc(alignment, size));
Christopher Ferriscae21a92018-02-05 18:14:55 -0800166 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800167 void* result = Malloc(aligned_alloc)(alignment, size);
168 if (__predict_false(result == nullptr)) {
169 warning_log("aligned_alloc(%zu, %zu) failed: returning null pointer", alignment, size);
170 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800171 return MaybeTagPointer(result);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800172}
173
Elliott Hughes390be502019-04-20 22:18:49 -0700174extern "C" __attribute__((__noinline__)) void* realloc(void* old_mem, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800175 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800176 old_mem = MaybeUntagAndCheckPointer(old_mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800177 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800178 return MaybeTagPointer(dispatch_table->realloc(old_mem, bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800179 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800180 void* result = Malloc(realloc)(old_mem, bytes);
181 if (__predict_false(result == nullptr && bytes != 0)) {
182 warning_log("realloc(%p, %zu) failed: returning null pointer", old_mem, bytes);
183 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800184 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800185}
186
Elliott Hughesb1770852018-09-18 12:52:42 -0700187extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) {
188 size_t new_size;
189 if (__builtin_mul_overflow(item_count, item_size, &new_size)) {
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800190 warning_log("reallocaray(%p, %zu, %zu) failed: returning null pointer",
191 old_mem, item_count, item_size);
Elliott Hughesb1770852018-09-18 12:52:42 -0700192 errno = ENOMEM;
193 return nullptr;
194 }
195 return realloc(old_mem, new_size);
196}
197
Christopher Ferris63860cb2015-11-16 17:30:32 -0800198#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
199extern "C" void* pvalloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800200 auto dispatch_table = GetDispatchTable();
201 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800202 return MaybeTagPointer(dispatch_table->pvalloc(bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800203 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800204 void* result = Malloc(pvalloc)(bytes);
205 if (__predict_false(result == nullptr)) {
206 warning_log("pvalloc(%zu) failed: returning null pointer", bytes);
207 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800208 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800209}
210
211extern "C" void* valloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800212 auto dispatch_table = GetDispatchTable();
213 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800214 return MaybeTagPointer(dispatch_table->valloc(bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800215 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800216 void* result = Malloc(valloc)(bytes);
217 if (__predict_false(result == nullptr)) {
218 warning_log("valloc(%zu) failed: returning null pointer", bytes);
219 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800220 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800221}
222#endif
Christopher Ferris63860cb2015-11-16 17:30:32 -0800223// =============================================================================
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000224
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800225struct CallbackWrapperArg {
226 void (*callback)(uintptr_t base, size_t size, void* arg);
227 void* arg;
228};
229
230void CallbackWrapper(uintptr_t base, size_t size, void* arg) {
231 CallbackWrapperArg* wrapper_arg = reinterpret_cast<CallbackWrapperArg*>(arg);
232 wrapper_arg->callback(
233 reinterpret_cast<uintptr_t>(MaybeTagPointer(reinterpret_cast<void*>(base))),
234 size, wrapper_arg->arg);
235}
236
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000237// =============================================================================
Colin Cross869691c2016-01-29 12:48:18 -0800238// Exported for use by libmemunreachable.
239// =============================================================================
240
241// Calls callback for every allocation in the anonymous heap mapping
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800242// [base, base+size). Must be called between malloc_disable and malloc_enable.
243// `base` in this can take either a tagged or untagged pointer, but we always
244// provide a tagged pointer to the `base` argument of `callback` if the kernel
245// supports tagged pointers.
Colin Cross869691c2016-01-29 12:48:18 -0800246extern "C" int malloc_iterate(uintptr_t base, size_t size,
247 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800248 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800249 // Wrap the malloc_iterate callback we were provided, in order to provide
250 // pointer tagging support.
251 CallbackWrapperArg wrapper_arg;
252 wrapper_arg.callback = callback;
253 wrapper_arg.arg = arg;
254 uintptr_t untagged_base =
255 reinterpret_cast<uintptr_t>(UntagPointer(reinterpret_cast<void*>(base)));
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800256 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800257 return dispatch_table->malloc_iterate(
258 untagged_base, size, CallbackWrapper, &wrapper_arg);
Colin Cross869691c2016-01-29 12:48:18 -0800259 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800260 return Malloc(malloc_iterate)(
261 untagged_base, size, CallbackWrapper, &wrapper_arg);
Colin Cross869691c2016-01-29 12:48:18 -0800262}
263
264// Disable calls to malloc so malloc_iterate gets a consistent view of
265// allocated memory.
266extern "C" void malloc_disable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800267 auto dispatch_table = GetDispatchTable();
268 if (__predict_false(dispatch_table != nullptr)) {
269 return dispatch_table->malloc_disable();
Colin Cross869691c2016-01-29 12:48:18 -0800270 }
271 return Malloc(malloc_disable)();
272}
273
274// Re-enable calls to malloc after a previous call to malloc_disable.
275extern "C" void malloc_enable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800276 auto dispatch_table = GetDispatchTable();
277 if (__predict_false(dispatch_table != nullptr)) {
278 return dispatch_table->malloc_enable();
Colin Cross869691c2016-01-29 12:48:18 -0800279 }
280 return Malloc(malloc_enable)();
281}
Colin Cross2d4721c2016-02-02 11:57:54 -0800282
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800283#if defined(LIBC_STATIC)
Colin Cross2d4721c2016-02-02 11:57:54 -0800284extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) {
285 return 0;
286}
287#endif
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800288
289#if __has_feature(hwaddress_sanitizer)
290// FIXME: implement these in HWASan allocator.
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800291extern "C" int __sanitizer_malloc_iterate(uintptr_t base __unused, size_t size __unused,
292 void (*callback)(uintptr_t base, size_t size, void* arg)
293 __unused,
294 void* arg __unused) {
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800295 return 0;
296}
297
298extern "C" void __sanitizer_malloc_disable() {
299}
300
301extern "C" void __sanitizer_malloc_enable() {
302}
Christopher Ferrisfa10a3a2019-03-08 10:56:17 -0800303
304extern "C" int __sanitizer_malloc_info(int, FILE*) {
305 errno = ENOTSUP;
306 return -1;
307}
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800308#endif
309// =============================================================================
310
311// =============================================================================
312// Platform-internal mallopt variant.
313// =============================================================================
314#if defined(LIBC_STATIC)
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800315extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size) {
316 if (opcode == M_SET_ALLOCATION_LIMIT_BYTES) {
317 return LimitEnable(arg, arg_size);
318 }
Peter Collingbourne1e110fb2020-01-09 10:48:22 -0800319 if (opcode == M_SET_HEAP_TAGGING_LEVEL) {
Mitch Phillips2210b8d2020-11-25 16:48:54 -0800320 ScopedPthreadMutexLocker locker(&g_heap_tagging_lock);
Peter Collingbourne1e110fb2020-01-09 10:48:22 -0800321 return SetHeapTaggingLevel(arg, arg_size);
322 }
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800323 if (opcode == M_INITIALIZE_GWP_ASAN) {
324 if (arg == nullptr || arg_size != sizeof(bool)) {
325 errno = EINVAL;
326 return false;
327 }
Mitch Phillipsbba80dc2020-02-11 14:42:14 -0800328 __libc_globals.mutate([&](libc_globals* globals) {
329 return MaybeInitGwpAsan(globals, *reinterpret_cast<bool*>(arg));
330 });
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800331 }
Peter Collingbourne5d3aa862020-09-11 15:05:17 -0700332 if (opcode == M_DISABLE_MEMORY_MITIGATIONS) {
333 return DisableMemoryMitigations(arg, arg_size);
334 }
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800335 errno = ENOTSUP;
336 return false;
337}
338#endif
339// =============================================================================
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800340
341static constexpr MallocDispatch __libc_malloc_default_dispatch __attribute__((unused)) = {
342 Malloc(calloc),
343 Malloc(free),
344 Malloc(mallinfo),
345 Malloc(malloc),
346 Malloc(malloc_usable_size),
347 Malloc(memalign),
348 Malloc(posix_memalign),
349#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
350 Malloc(pvalloc),
351#endif
352 Malloc(realloc),
353#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
354 Malloc(valloc),
355#endif
356 Malloc(malloc_iterate),
357 Malloc(malloc_disable),
358 Malloc(malloc_enable),
359 Malloc(mallopt),
360 Malloc(aligned_alloc),
361 Malloc(malloc_info),
362};
363
364const MallocDispatch* NativeAllocatorDispatch() {
365 return &__libc_malloc_default_dispatch;
366}