blob: 30c9cc7a690c8a77e1a48193de68eca23dc20da8 [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
Peter Collingbourne1e110fb2020-01-09 10:48:22 -080044#include "heap_tagging.h"
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080045#include "malloc_common.h"
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080046#include "malloc_limit.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)) {
65 return 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 }
71 return 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();
76 if (__predict_false(dispatch_table != nullptr)) {
77 dispatch_table->free(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -080078 } else {
79 Malloc(free)(mem);
80 }
81}
82
83extern "C" struct mallinfo mallinfo() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080084 auto dispatch_table = GetDispatchTable();
85 if (__predict_false(dispatch_table != nullptr)) {
86 return dispatch_table->mallinfo();
Christopher Ferris63860cb2015-11-16 17:30:32 -080087 }
88 return Malloc(mallinfo)();
89}
90
Christopher Ferris6c619a02019-03-01 17:59:51 -080091extern "C" int malloc_info(int options, FILE* fp) {
92 auto dispatch_table = GetDispatchTable();
93 if (__predict_false(dispatch_table != nullptr)) {
94 return dispatch_table->malloc_info(options, fp);
95 }
96 return Malloc(malloc_info)(options, fp);
97}
98
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -070099extern "C" int mallopt(int param, int value) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800100 auto dispatch_table = GetDispatchTable();
101 if (__predict_false(dispatch_table != nullptr)) {
102 return dispatch_table->mallopt(param, value);
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700103 }
104 return Malloc(mallopt)(param, value);
105}
106
Christopher Ferris63860cb2015-11-16 17:30:32 -0800107extern "C" void* malloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800108 auto dispatch_table = GetDispatchTable();
109 if (__predict_false(dispatch_table != nullptr)) {
110 return dispatch_table->malloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800111 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800112 void* result = Malloc(malloc)(bytes);
113 if (__predict_false(result == nullptr)) {
114 warning_log("malloc(%zu) failed: returning null pointer", bytes);
115 }
116 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800117}
118
119extern "C" size_t malloc_usable_size(const void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800120 auto dispatch_table = GetDispatchTable();
121 if (__predict_false(dispatch_table != nullptr)) {
122 return dispatch_table->malloc_usable_size(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800123 }
124 return Malloc(malloc_usable_size)(mem);
125}
126
127extern "C" void* memalign(size_t alignment, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800128 auto dispatch_table = GetDispatchTable();
129 if (__predict_false(dispatch_table != nullptr)) {
130 return dispatch_table->memalign(alignment, bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800131 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800132 void* result = Malloc(memalign)(alignment, bytes);
133 if (__predict_false(result == nullptr)) {
134 warning_log("memalign(%zu, %zu) failed: returning null pointer", alignment, bytes);
135 }
136 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800137}
138
139extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800140 auto dispatch_table = GetDispatchTable();
141 if (__predict_false(dispatch_table != nullptr)) {
142 return dispatch_table->posix_memalign(memptr, alignment, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800143 }
144 return Malloc(posix_memalign)(memptr, alignment, size);
145}
146
Christopher Ferriscae21a92018-02-05 18:14:55 -0800147extern "C" void* aligned_alloc(size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800148 auto dispatch_table = GetDispatchTable();
149 if (__predict_false(dispatch_table != nullptr)) {
150 return dispatch_table->aligned_alloc(alignment, size);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800151 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800152 void* result = Malloc(aligned_alloc)(alignment, size);
153 if (__predict_false(result == nullptr)) {
154 warning_log("aligned_alloc(%zu, %zu) failed: returning null pointer", alignment, size);
155 }
156 return result;
Christopher Ferriscae21a92018-02-05 18:14:55 -0800157}
158
Elliott Hughes390be502019-04-20 22:18:49 -0700159extern "C" __attribute__((__noinline__)) void* realloc(void* old_mem, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800160 auto dispatch_table = GetDispatchTable();
161 if (__predict_false(dispatch_table != nullptr)) {
162 return dispatch_table->realloc(old_mem, bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800163 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800164 void* result = Malloc(realloc)(old_mem, bytes);
165 if (__predict_false(result == nullptr && bytes != 0)) {
166 warning_log("realloc(%p, %zu) failed: returning null pointer", old_mem, bytes);
167 }
168 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800169}
170
Elliott Hughesb1770852018-09-18 12:52:42 -0700171extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) {
172 size_t new_size;
173 if (__builtin_mul_overflow(item_count, item_size, &new_size)) {
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800174 warning_log("reallocaray(%p, %zu, %zu) failed: returning null pointer",
175 old_mem, item_count, item_size);
Elliott Hughesb1770852018-09-18 12:52:42 -0700176 errno = ENOMEM;
177 return nullptr;
178 }
179 return realloc(old_mem, new_size);
180}
181
Christopher Ferris63860cb2015-11-16 17:30:32 -0800182#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
183extern "C" void* pvalloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800184 auto dispatch_table = GetDispatchTable();
185 if (__predict_false(dispatch_table != nullptr)) {
186 return dispatch_table->pvalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800187 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800188 void* result = Malloc(pvalloc)(bytes);
189 if (__predict_false(result == nullptr)) {
190 warning_log("pvalloc(%zu) failed: returning null pointer", bytes);
191 }
192 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800193}
194
195extern "C" void* valloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800196 auto dispatch_table = GetDispatchTable();
197 if (__predict_false(dispatch_table != nullptr)) {
198 return dispatch_table->valloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800199 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800200 void* result = Malloc(valloc)(bytes);
201 if (__predict_false(result == nullptr)) {
202 warning_log("valloc(%zu) failed: returning null pointer", bytes);
203 }
204 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800205}
206#endif
Christopher Ferris63860cb2015-11-16 17:30:32 -0800207// =============================================================================
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000208
209// =============================================================================
Colin Cross869691c2016-01-29 12:48:18 -0800210// Exported for use by libmemunreachable.
211// =============================================================================
212
213// Calls callback for every allocation in the anonymous heap mapping
214// [base, base+size). Must be called between malloc_disable and malloc_enable.
215extern "C" int malloc_iterate(uintptr_t base, size_t size,
216 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800217 auto dispatch_table = GetDispatchTable();
218 if (__predict_false(dispatch_table != nullptr)) {
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800219 return dispatch_table->malloc_iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800220 }
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800221 return Malloc(malloc_iterate)(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800222}
223
224// Disable calls to malloc so malloc_iterate gets a consistent view of
225// allocated memory.
226extern "C" void malloc_disable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800227 auto dispatch_table = GetDispatchTable();
228 if (__predict_false(dispatch_table != nullptr)) {
229 return dispatch_table->malloc_disable();
Colin Cross869691c2016-01-29 12:48:18 -0800230 }
231 return Malloc(malloc_disable)();
232}
233
234// Re-enable calls to malloc after a previous call to malloc_disable.
235extern "C" void malloc_enable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800236 auto dispatch_table = GetDispatchTable();
237 if (__predict_false(dispatch_table != nullptr)) {
238 return dispatch_table->malloc_enable();
Colin Cross869691c2016-01-29 12:48:18 -0800239 }
240 return Malloc(malloc_enable)();
241}
Colin Cross2d4721c2016-02-02 11:57:54 -0800242
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800243#if defined(LIBC_STATIC)
Colin Cross2d4721c2016-02-02 11:57:54 -0800244extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) {
245 return 0;
246}
247#endif
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800248
249#if __has_feature(hwaddress_sanitizer)
250// FIXME: implement these in HWASan allocator.
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800251extern "C" int __sanitizer_malloc_iterate(uintptr_t base __unused, size_t size __unused,
252 void (*callback)(uintptr_t base, size_t size, void* arg)
253 __unused,
254 void* arg __unused) {
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800255 return 0;
256}
257
258extern "C" void __sanitizer_malloc_disable() {
259}
260
261extern "C" void __sanitizer_malloc_enable() {
262}
Christopher Ferrisfa10a3a2019-03-08 10:56:17 -0800263
264extern "C" int __sanitizer_malloc_info(int, FILE*) {
265 errno = ENOTSUP;
266 return -1;
267}
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800268#endif
269// =============================================================================
270
271// =============================================================================
272// Platform-internal mallopt variant.
273// =============================================================================
274#if defined(LIBC_STATIC)
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800275extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size) {
276 if (opcode == M_SET_ALLOCATION_LIMIT_BYTES) {
277 return LimitEnable(arg, arg_size);
278 }
Peter Collingbourne1e110fb2020-01-09 10:48:22 -0800279 if (opcode == M_SET_HEAP_TAGGING_LEVEL) {
280 return SetHeapTaggingLevel(arg, arg_size);
281 }
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800282 errno = ENOTSUP;
283 return false;
284}
285#endif
286// =============================================================================