blob: a0da3dbffea22cdd1d974c667be9e79e158a5a68 [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
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080044#include "malloc_common.h"
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -080045#include "malloc_limit.h"
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070046
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080047// =============================================================================
48// Global variables instantations.
49// =============================================================================
Evgenii Stepanovbe551f52018-08-13 16:46:15 -070050
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080051// Malloc hooks globals.
Christopher Ferrisdb478a62018-02-07 18:42:14 -080052void* (*volatile __malloc_hook)(size_t, const void*);
53void* (*volatile __realloc_hook)(void*, size_t, const void*);
54void (*volatile __free_hook)(void*, const void*);
55void* (*volatile __memalign_hook)(size_t, size_t, const void*);
Christopher Ferrise4cdbc42019-02-08 17:30:58 -080056// =============================================================================
Christopher Ferris63860cb2015-11-16 17:30:32 -080057
58// =============================================================================
59// Allocation functions
60// =============================================================================
61extern "C" void* calloc(size_t n_elements, size_t elem_size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080062 auto dispatch_table = GetDispatchTable();
63 if (__predict_false(dispatch_table != nullptr)) {
64 return dispatch_table->calloc(n_elements, elem_size);
Christopher Ferris63860cb2015-11-16 17:30:32 -080065 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -080066 void* result = Malloc(calloc)(n_elements, elem_size);
67 if (__predict_false(result == nullptr)) {
68 warning_log("calloc(%zu, %zu) failed: returning null pointer", n_elements, elem_size);
69 }
70 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -080071}
72
73extern "C" void free(void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080074 auto dispatch_table = GetDispatchTable();
75 if (__predict_false(dispatch_table != nullptr)) {
76 dispatch_table->free(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -080077 } else {
78 Malloc(free)(mem);
79 }
80}
81
82extern "C" struct mallinfo mallinfo() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080083 auto dispatch_table = GetDispatchTable();
84 if (__predict_false(dispatch_table != nullptr)) {
85 return dispatch_table->mallinfo();
Christopher Ferris63860cb2015-11-16 17:30:32 -080086 }
87 return Malloc(mallinfo)();
88}
89
Christopher Ferris6c619a02019-03-01 17:59:51 -080090extern "C" int malloc_info(int options, FILE* fp) {
91 auto dispatch_table = GetDispatchTable();
92 if (__predict_false(dispatch_table != nullptr)) {
93 return dispatch_table->malloc_info(options, fp);
94 }
95 return Malloc(malloc_info)(options, fp);
96}
97
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -070098extern "C" int mallopt(int param, int value) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -080099 auto dispatch_table = GetDispatchTable();
100 if (__predict_false(dispatch_table != nullptr)) {
101 return dispatch_table->mallopt(param, value);
Christopher Ferrisa1c0d2f2017-05-15 15:50:19 -0700102 }
103 return Malloc(mallopt)(param, value);
104}
105
Christopher Ferris63860cb2015-11-16 17:30:32 -0800106extern "C" void* malloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800107 auto dispatch_table = GetDispatchTable();
108 if (__predict_false(dispatch_table != nullptr)) {
109 return dispatch_table->malloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800110 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800111 void* result = Malloc(malloc)(bytes);
112 if (__predict_false(result == nullptr)) {
113 warning_log("malloc(%zu) failed: returning null pointer", bytes);
114 }
115 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800116}
117
118extern "C" size_t malloc_usable_size(const void* mem) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800119 auto dispatch_table = GetDispatchTable();
120 if (__predict_false(dispatch_table != nullptr)) {
121 return dispatch_table->malloc_usable_size(mem);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800122 }
123 return Malloc(malloc_usable_size)(mem);
124}
125
126extern "C" void* memalign(size_t alignment, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800127 auto dispatch_table = GetDispatchTable();
128 if (__predict_false(dispatch_table != nullptr)) {
129 return dispatch_table->memalign(alignment, bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800130 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800131 void* result = Malloc(memalign)(alignment, bytes);
132 if (__predict_false(result == nullptr)) {
133 warning_log("memalign(%zu, %zu) failed: returning null pointer", alignment, bytes);
134 }
135 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800136}
137
138extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800139 auto dispatch_table = GetDispatchTable();
140 if (__predict_false(dispatch_table != nullptr)) {
141 return dispatch_table->posix_memalign(memptr, alignment, size);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800142 }
143 return Malloc(posix_memalign)(memptr, alignment, size);
144}
145
Christopher Ferriscae21a92018-02-05 18:14:55 -0800146extern "C" void* aligned_alloc(size_t alignment, size_t size) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800147 auto dispatch_table = GetDispatchTable();
148 if (__predict_false(dispatch_table != nullptr)) {
149 return dispatch_table->aligned_alloc(alignment, size);
Christopher Ferriscae21a92018-02-05 18:14:55 -0800150 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800151 void* result = Malloc(aligned_alloc)(alignment, size);
152 if (__predict_false(result == nullptr)) {
153 warning_log("aligned_alloc(%zu, %zu) failed: returning null pointer", alignment, size);
154 }
155 return result;
Christopher Ferriscae21a92018-02-05 18:14:55 -0800156}
157
Elliott Hughes390be502019-04-20 22:18:49 -0700158extern "C" __attribute__((__noinline__)) void* realloc(void* old_mem, size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800159 auto dispatch_table = GetDispatchTable();
160 if (__predict_false(dispatch_table != nullptr)) {
161 return dispatch_table->realloc(old_mem, bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800162 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800163 void* result = Malloc(realloc)(old_mem, bytes);
164 if (__predict_false(result == nullptr && bytes != 0)) {
165 warning_log("realloc(%p, %zu) failed: returning null pointer", old_mem, bytes);
166 }
167 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800168}
169
Elliott Hughesb1770852018-09-18 12:52:42 -0700170extern "C" void* reallocarray(void* old_mem, size_t item_count, size_t item_size) {
171 size_t new_size;
172 if (__builtin_mul_overflow(item_count, item_size, &new_size)) {
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800173 warning_log("reallocaray(%p, %zu, %zu) failed: returning null pointer",
174 old_mem, item_count, item_size);
Elliott Hughesb1770852018-09-18 12:52:42 -0700175 errno = ENOMEM;
176 return nullptr;
177 }
178 return realloc(old_mem, new_size);
179}
180
Christopher Ferris63860cb2015-11-16 17:30:32 -0800181#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
182extern "C" void* pvalloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800183 auto dispatch_table = GetDispatchTable();
184 if (__predict_false(dispatch_table != nullptr)) {
185 return dispatch_table->pvalloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800186 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800187 void* result = Malloc(pvalloc)(bytes);
188 if (__predict_false(result == nullptr)) {
189 warning_log("pvalloc(%zu) failed: returning null pointer", bytes);
190 }
191 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800192}
193
194extern "C" void* valloc(size_t bytes) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800195 auto dispatch_table = GetDispatchTable();
196 if (__predict_false(dispatch_table != nullptr)) {
197 return dispatch_table->valloc(bytes);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800198 }
Elliott Hughesa21f6cc2019-02-25 13:21:04 -0800199 void* result = Malloc(valloc)(bytes);
200 if (__predict_false(result == nullptr)) {
201 warning_log("valloc(%zu) failed: returning null pointer", bytes);
202 }
203 return result;
Christopher Ferris63860cb2015-11-16 17:30:32 -0800204}
205#endif
Christopher Ferris63860cb2015-11-16 17:30:32 -0800206// =============================================================================
Ryan Savitskiecc37e32018-12-14 15:57:21 +0000207
208// =============================================================================
Colin Cross869691c2016-01-29 12:48:18 -0800209// Exported for use by libmemunreachable.
210// =============================================================================
211
212// Calls callback for every allocation in the anonymous heap mapping
213// [base, base+size). Must be called between malloc_disable and malloc_enable.
214extern "C" int malloc_iterate(uintptr_t base, size_t size,
215 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800216 auto dispatch_table = GetDispatchTable();
217 if (__predict_false(dispatch_table != nullptr)) {
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800218 return dispatch_table->malloc_iterate(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800219 }
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800220 return Malloc(malloc_iterate)(base, size, callback, arg);
Colin Cross869691c2016-01-29 12:48:18 -0800221}
222
223// Disable calls to malloc so malloc_iterate gets a consistent view of
224// allocated memory.
225extern "C" void malloc_disable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800226 auto dispatch_table = GetDispatchTable();
227 if (__predict_false(dispatch_table != nullptr)) {
228 return dispatch_table->malloc_disable();
Colin Cross869691c2016-01-29 12:48:18 -0800229 }
230 return Malloc(malloc_disable)();
231}
232
233// Re-enable calls to malloc after a previous call to malloc_disable.
234extern "C" void malloc_enable() {
Christopher Ferris62e1e2c2019-02-04 12:26:02 -0800235 auto dispatch_table = GetDispatchTable();
236 if (__predict_false(dispatch_table != nullptr)) {
237 return dispatch_table->malloc_enable();
Colin Cross869691c2016-01-29 12:48:18 -0800238 }
239 return Malloc(malloc_enable)();
240}
Colin Cross2d4721c2016-02-02 11:57:54 -0800241
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800242#if defined(LIBC_STATIC)
Colin Cross2d4721c2016-02-02 11:57:54 -0800243extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) {
244 return 0;
245}
246#endif
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800247
248#if __has_feature(hwaddress_sanitizer)
249// FIXME: implement these in HWASan allocator.
Christopher Ferris6f517cd2019-11-08 11:28:38 -0800250extern "C" int __sanitizer_malloc_iterate(uintptr_t base __unused, size_t size __unused,
251 void (*callback)(uintptr_t base, size_t size, void* arg)
252 __unused,
253 void* arg __unused) {
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800254 return 0;
255}
256
257extern "C" void __sanitizer_malloc_disable() {
258}
259
260extern "C" void __sanitizer_malloc_enable() {
261}
Christopher Ferrisfa10a3a2019-03-08 10:56:17 -0800262
263extern "C" int __sanitizer_malloc_info(int, FILE*) {
264 errno = ENOTSUP;
265 return -1;
266}
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800267#endif
268// =============================================================================
269
270// =============================================================================
271// Platform-internal mallopt variant.
272// =============================================================================
273#if defined(LIBC_STATIC)
Christopher Ferris1fc5ccf2019-02-15 18:06:15 -0800274extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size) {
275 if (opcode == M_SET_ALLOCATION_LIMIT_BYTES) {
276 return LimitEnable(arg, arg_size);
277 }
Christopher Ferrise4cdbc42019-02-08 17:30:58 -0800278 errno = ENOTSUP;
279 return false;
280}
281#endif
282// =============================================================================