blob: 441d8848244cfc3fd1917c65e82444f77bd99c6d [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"
Mitch Phillips9cad8422021-01-20 16:03:27 -080047#include "heap_zero_init.h"
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080048#include "malloc_common.h"
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080049#include "malloc_limit.h"
Mitch Phillips3b21ada2020-01-07 15:47:47 -080050#include "malloc_tagged_pointers.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) {
Elliott Hughes446b4dd2021-01-14 13:34:20 -0800105 // Some are handled by libc directly rather than by the allocator.
106 if (param == M_BIONIC_SET_HEAP_TAGGING_LEVEL) {
107 ScopedPthreadMutexLocker locker(&g_heap_tagging_lock);
108 return SetHeapTaggingLevel(static_cast<HeapTaggingLevel>(value));
109 }
Mitch Phillips9cad8422021-01-20 16:03:27 -0800110 if (param == M_BIONIC_ZERO_INIT) {
111 return SetHeapZeroInitialize(value);
Elliott Hughes446b4dd2021-01-14 13:34:20 -0800112 }
Christopher Ferrisb4e560e2023-10-26 17:00:00 -0700113
Elliott Hughes446b4dd2021-01-14 13:34:20 -0800114 // The rest we pass on...
Christopher Ferrisb4e560e2023-10-26 17:00:00 -0700115 int retval;
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800116 auto dispatch_table = GetDispatchTable();
117 if (__predict_false(dispatch_table != nullptr)) {
Christopher Ferrisb4e560e2023-10-26 17:00:00 -0700118 retval = dispatch_table->mallopt(param, value);
119 } else {
120 retval = Malloc(mallopt)(param, value);
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700121 }
Christopher Ferrisb4e560e2023-10-26 17:00:00 -0700122
123 // Track the M_DECAY_TIME mallopt calls.
124 if (param == M_DECAY_TIME && retval == 1) {
125 __libc_globals.mutate([value](libc_globals* globals) {
Chia-hung Duan6abb4062024-04-17 19:08:48 -0700126 if (value <= 0) {
Christopher Ferrisb4e560e2023-10-26 17:00:00 -0700127 atomic_store(&globals->decay_time_enabled, false);
128 } else {
129 atomic_store(&globals->decay_time_enabled, true);
130 }
131 });
132 }
133 return retval;
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700134}
135
Christopher Ferris63860cb2015-11-16 17:30:32 -0800136extern "C" void* malloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800137 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800138 void *result;
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800139 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800140 result = dispatch_table->malloc(bytes);
141 } else {
142 result = Malloc(malloc)(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800143 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800144 if (__predict_false(result == nullptr)) {
145 warning_log("malloc(%zu) failed: returning null pointer", bytes);
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800146 return nullptr;
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800147 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800148 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800149}
150
151extern "C" size_t malloc_usable_size(const void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800152 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800153 mem = MaybeUntagAndCheckPointer(mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800154 if (__predict_false(dispatch_table != nullptr)) {
155 return dispatch_table->malloc_usable_size(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800156 }
157 return Malloc(malloc_usable_size)(mem);
158}
159
160extern "C" void* memalign(size_t alignment, size_t bytes) {
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->memalign(alignment, bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800164 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800165 void* result = Malloc(memalign)(alignment, bytes);
166 if (__predict_false(result == nullptr)) {
167 warning_log("memalign(%zu, %zu) failed: returning null pointer", alignment, bytes);
168 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800169 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800170}
171
172extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800173 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800174 int result;
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800175 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800176 result = dispatch_table->posix_memalign(memptr, alignment, size);
177 } else {
178 result = Malloc(posix_memalign)(memptr, alignment, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800179 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800180 if (result == 0) {
181 *memptr = MaybeTagPointer(*memptr);
182 }
183 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800184}
185
Christopher Ferriscae21a92018-02-05 18:14:55 -0800186extern "C" void* aligned_alloc(size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800187 auto dispatch_table = GetDispatchTable();
188 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800189 return MaybeTagPointer(dispatch_table->aligned_alloc(alignment, size));
Christopher Ferriscae21a92018-02-05 18:14:55 -0800190 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800191 void* result = Malloc(aligned_alloc)(alignment, size);
192 if (__predict_false(result == nullptr)) {
193 warning_log("aligned_alloc(%zu, %zu) failed: returning null pointer", alignment, size);
194 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800195 return MaybeTagPointer(result);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800196}
197
Elliott Hughes390be502019-04-20 22:18:49 -0700198extern "C" __attribute__((__noinline__)) void* realloc(void* old_mem, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800199 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800200 old_mem = MaybeUntagAndCheckPointer(old_mem);
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800201 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800202 return MaybeTagPointer(dispatch_table->realloc(old_mem, bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800203 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800204 void* result = Malloc(realloc)(old_mem, bytes);
205 if (__predict_false(result == nullptr && bytes != 0)) {
206 warning_log("realloc(%p, %zu) failed: returning null pointer", old_mem, bytes);
207 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800208 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800209}
210
Elliott Hughesb1770852018-09-18 12:52:42 -0700211extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) {
212 size_t new_size;
213 if (__builtin_mul_overflow(item_count, item_size, &new_size)) {
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800214 warning_log("reallocaray(%p, %zu, %zu) failed: returning null pointer",
215 old_mem, item_count, item_size);
Elliott Hughesb1770852018-09-18 12:52:42 -0700216 errno = ENOMEM;
217 return nullptr;
218 }
219 return realloc(old_mem, new_size);
220}
221
Christopher Ferris63860cb2015-11-16 17:30:32 -0800222#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
223extern "C" void* pvalloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800224 auto dispatch_table = GetDispatchTable();
225 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800226 return MaybeTagPointer(dispatch_table->pvalloc(bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800227 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800228 void* result = Malloc(pvalloc)(bytes);
229 if (__predict_false(result == nullptr)) {
230 warning_log("pvalloc(%zu) failed: returning null pointer", bytes);
231 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800232 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800233}
234
235extern "C" void* valloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800236 auto dispatch_table = GetDispatchTable();
237 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800238 return MaybeTagPointer(dispatch_table->valloc(bytes));
Christopher Ferris63860cb2015-11-16 17:30:32 -0800239 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800240 void* result = Malloc(valloc)(bytes);
241 if (__predict_false(result == nullptr)) {
242 warning_log("valloc(%zu) failed: returning null pointer", bytes);
243 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800244 return MaybeTagPointer(result);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800245}
246#endif
Christopher Ferris63860cb2015-11-16 17:30:32 -0800247// =============================================================================
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000248
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800249struct CallbackWrapperArg {
250 void (*callback)(uintptr_t base, size_t size, void* arg);
251 void* arg;
252};
253
254void CallbackWrapper(uintptr_t base, size_t size, void* arg) {
255 CallbackWrapperArg* wrapper_arg = reinterpret_cast<CallbackWrapperArg*>(arg);
256 wrapper_arg->callback(
257 reinterpret_cast<uintptr_t>(MaybeTagPointer(reinterpret_cast<void*>(base))),
258 size, wrapper_arg->arg);
259}
260
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000261// =============================================================================
Colin Cross869691c2016-01-29 12:48:18 -0800262// Exported for use by libmemunreachable.
263// =============================================================================
264
265// Calls callback for every allocation in the anonymous heap mapping
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800266// [base, base+size). Must be called between malloc_disable and malloc_enable.
267// `base` in this can take either a tagged or untagged pointer, but we always
268// provide a tagged pointer to the `base` argument of `callback` if the kernel
269// supports tagged pointers.
Colin Cross869691c2016-01-29 12:48:18 -0800270extern "C" int malloc_iterate(uintptr_t base, size_t size,
271 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800272 auto dispatch_table = GetDispatchTable();
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800273 // Wrap the malloc_iterate callback we were provided, in order to provide
274 // pointer tagging support.
275 CallbackWrapperArg wrapper_arg;
276 wrapper_arg.callback = callback;
277 wrapper_arg.arg = arg;
278 uintptr_t untagged_base =
279 reinterpret_cast<uintptr_t>(UntagPointer(reinterpret_cast<void*>(base)));
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800280 if (__predict_false(dispatch_table != nullptr)) {
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800281 return dispatch_table->malloc_iterate(
282 untagged_base, size, CallbackWrapper, &wrapper_arg);
Colin Cross869691c2016-01-29 12:48:18 -0800283 }
Mitch Phillips3b21ada2020-01-07 15:47:47 -0800284 return Malloc(malloc_iterate)(
285 untagged_base, size, CallbackWrapper, &wrapper_arg);
Colin Cross869691c2016-01-29 12:48:18 -0800286}
287
288// Disable calls to malloc so malloc_iterate gets a consistent view of
289// allocated memory.
290extern "C" void malloc_disable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800291 auto dispatch_table = GetDispatchTable();
292 if (__predict_false(dispatch_table != nullptr)) {
293 return dispatch_table->malloc_disable();
Colin Cross869691c2016-01-29 12:48:18 -0800294 }
295 return Malloc(malloc_disable)();
296}
297
298// Re-enable calls to malloc after a previous call to malloc_disable.
299extern "C" void malloc_enable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800300 auto dispatch_table = GetDispatchTable();
301 if (__predict_false(dispatch_table != nullptr)) {
302 return dispatch_table->malloc_enable();
Colin Cross869691c2016-01-29 12:48:18 -0800303 }
304 return Malloc(malloc_enable)();
305}
Colin Cross2d4721c2016-02-02 11:57:54 -0800306
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800307#if defined(LIBC_STATIC)
Colin Cross2d4721c2016-02-02 11:57:54 -0800308extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) {
309 return 0;
310}
311#endif
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800312
313#if __has_feature(hwaddress_sanitizer)
314// FIXME: implement these in HWASan allocator.
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800315extern "C" int __sanitizer_malloc_iterate(uintptr_t base __unused, size_t size __unused,
316 void (*callback)(uintptr_t base, size_t size, void* arg)
317 __unused,
318 void* arg __unused) {
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800319 return 0;
320}
321
322extern "C" void __sanitizer_malloc_disable() {
323}
324
325extern "C" void __sanitizer_malloc_enable() {
326}
Christopher Ferrisfa10a3a2019-03-08 10:56:17 -0800327
328extern "C" int __sanitizer_malloc_info(int, FILE*) {
329 errno = ENOTSUP;
330 return -1;
331}
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800332#endif
333// =============================================================================
334
Mitch Phillipsf3968e82020-01-31 19:57:04 -0800335static constexpr MallocDispatch __libc_malloc_default_dispatch __attribute__((unused)) = {
336 Malloc(calloc),
337 Malloc(free),
338 Malloc(mallinfo),
339 Malloc(malloc),
340 Malloc(malloc_usable_size),
341 Malloc(memalign),
342 Malloc(posix_memalign),
343#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
344 Malloc(pvalloc),
345#endif
346 Malloc(realloc),
347#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
348 Malloc(valloc),
349#endif
350 Malloc(malloc_iterate),
351 Malloc(malloc_disable),
352 Malloc(malloc_enable),
353 Malloc(mallopt),
354 Malloc(aligned_alloc),
355 Malloc(malloc_info),
356};
357
358const MallocDispatch* NativeAllocatorDispatch() {
359 return &__libc_malloc_default_dispatch;
360}