1 /*
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"
35 // is set to a non-zero value.
36
37 #include <errno.h>
38 #include <stdint.h>
39 #include <stdio.h>
40
41 #include <private/bionic_config.h>
42 #include <private/bionic_malloc.h>
43
44 #include "malloc_common.h"
45 #include "malloc_limit.h"
46
47 // =============================================================================
48 // Global variables instantations.
49 // =============================================================================
50
51 // Malloc hooks globals.
52 void* (*volatile __malloc_hook)(size_t, const void*);
53 void* (*volatile __realloc_hook)(void*, size_t, const void*);
54 void (*volatile __free_hook)(void*, const void*);
55 void* (*volatile __memalign_hook)(size_t, size_t, const void*);
56 // =============================================================================
57
58 // =============================================================================
59 // Allocation functions
60 // =============================================================================
calloc(size_t n_elements,size_t elem_size)61 extern "C" void* calloc(size_t n_elements, size_t elem_size) {
62 auto dispatch_table = GetDispatchTable();
63 if (__predict_false(dispatch_table != nullptr)) {
64 return dispatch_table->calloc(n_elements, elem_size);
65 }
66 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;
71 }
72
free(void * mem)73 extern "C" void free(void* mem) {
74 auto dispatch_table = GetDispatchTable();
75 if (__predict_false(dispatch_table != nullptr)) {
76 dispatch_table->free(mem);
77 } else {
78 Malloc(free)(mem);
79 }
80 }
81
mallinfo()82 extern "C" struct mallinfo mallinfo() {
83 auto dispatch_table = GetDispatchTable();
84 if (__predict_false(dispatch_table != nullptr)) {
85 return dispatch_table->mallinfo();
86 }
87 return Malloc(mallinfo)();
88 }
89
malloc_info(int options,FILE * fp)90 extern "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
mallopt(int param,int value)98 extern "C" int mallopt(int param, int value) {
99 auto dispatch_table = GetDispatchTable();
100 if (__predict_false(dispatch_table != nullptr)) {
101 return dispatch_table->mallopt(param, value);
102 }
103 return Malloc(mallopt)(param, value);
104 }
105
malloc(size_t bytes)106 extern "C" void* malloc(size_t bytes) {
107 auto dispatch_table = GetDispatchTable();
108 if (__predict_false(dispatch_table != nullptr)) {
109 return dispatch_table->malloc(bytes);
110 }
111 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;
116 }
117
malloc_usable_size(const void * mem)118 extern "C" size_t malloc_usable_size(const void* mem) {
119 auto dispatch_table = GetDispatchTable();
120 if (__predict_false(dispatch_table != nullptr)) {
121 return dispatch_table->malloc_usable_size(mem);
122 }
123 return Malloc(malloc_usable_size)(mem);
124 }
125
memalign(size_t alignment,size_t bytes)126 extern "C" void* memalign(size_t alignment, size_t bytes) {
127 auto dispatch_table = GetDispatchTable();
128 if (__predict_false(dispatch_table != nullptr)) {
129 return dispatch_table->memalign(alignment, bytes);
130 }
131 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;
136 }
137
posix_memalign(void ** memptr,size_t alignment,size_t size)138 extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
139 auto dispatch_table = GetDispatchTable();
140 if (__predict_false(dispatch_table != nullptr)) {
141 return dispatch_table->posix_memalign(memptr, alignment, size);
142 }
143 return Malloc(posix_memalign)(memptr, alignment, size);
144 }
145
aligned_alloc(size_t alignment,size_t size)146 extern "C" void* aligned_alloc(size_t alignment, size_t size) {
147 auto dispatch_table = GetDispatchTable();
148 if (__predict_false(dispatch_table != nullptr)) {
149 return dispatch_table->aligned_alloc(alignment, size);
150 }
151 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;
156 }
157
realloc(void * old_mem,size_t bytes)158 extern "C" __attribute__((__noinline__)) void* realloc(void* old_mem, size_t bytes) {
159 auto dispatch_table = GetDispatchTable();
160 if (__predict_false(dispatch_table != nullptr)) {
161 return dispatch_table->realloc(old_mem, bytes);
162 }
163 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;
168 }
169
reallocarray(void * old_mem,size_t item_count,size_t item_size)170 extern "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)) {
173 warning_log("reallocaray(%p, %zu, %zu) failed: returning null pointer",
174 old_mem, item_count, item_size);
175 errno = ENOMEM;
176 return nullptr;
177 }
178 return realloc(old_mem, new_size);
179 }
180
181 #if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
pvalloc(size_t bytes)182 extern "C" void* pvalloc(size_t bytes) {
183 auto dispatch_table = GetDispatchTable();
184 if (__predict_false(dispatch_table != nullptr)) {
185 return dispatch_table->pvalloc(bytes);
186 }
187 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;
192 }
193
valloc(size_t bytes)194 extern "C" void* valloc(size_t bytes) {
195 auto dispatch_table = GetDispatchTable();
196 if (__predict_false(dispatch_table != nullptr)) {
197 return dispatch_table->valloc(bytes);
198 }
199 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;
204 }
205 #endif
206 // =============================================================================
207
208 // =============================================================================
209 // 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.
malloc_iterate(uintptr_t base,size_t size,void (* callback)(uintptr_t base,size_t size,void * arg),void * arg)214 extern "C" int malloc_iterate(uintptr_t base, size_t size,
215 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
216 auto dispatch_table = GetDispatchTable();
217 if (__predict_false(dispatch_table != nullptr)) {
218 return dispatch_table->iterate(base, size, callback, arg);
219 }
220 return Malloc(iterate)(base, size, callback, arg);
221 }
222
223 // Disable calls to malloc so malloc_iterate gets a consistent view of
224 // allocated memory.
malloc_disable()225 extern "C" void malloc_disable() {
226 auto dispatch_table = GetDispatchTable();
227 if (__predict_false(dispatch_table != nullptr)) {
228 return dispatch_table->malloc_disable();
229 }
230 return Malloc(malloc_disable)();
231 }
232
233 // Re-enable calls to malloc after a previous call to malloc_disable.
malloc_enable()234 extern "C" void malloc_enable() {
235 auto dispatch_table = GetDispatchTable();
236 if (__predict_false(dispatch_table != nullptr)) {
237 return dispatch_table->malloc_enable();
238 }
239 return Malloc(malloc_enable)();
240 }
241
242 #if defined(LIBC_STATIC)
malloc_backtrace(void *,uintptr_t *,size_t)243 extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) {
244 return 0;
245 }
246 #endif
247
248 #if __has_feature(hwaddress_sanitizer)
249 // FIXME: implement these in HWASan allocator.
__sanitizer_iterate(uintptr_t base __unused,size_t size __unused,void (* callback)(uintptr_t base,size_t size,void * arg)__unused,void * arg __unused)250 extern "C" int __sanitizer_iterate(uintptr_t base __unused, size_t size __unused,
251 void (*callback)(uintptr_t base, size_t size, void* arg) __unused,
252 void* arg __unused) {
253 return 0;
254 }
255
__sanitizer_malloc_disable()256 extern "C" void __sanitizer_malloc_disable() {
257 }
258
__sanitizer_malloc_enable()259 extern "C" void __sanitizer_malloc_enable() {
260 }
261
__sanitizer_malloc_info(int,FILE *)262 extern "C" int __sanitizer_malloc_info(int, FILE*) {
263 errno = ENOTSUP;
264 return -1;
265 }
266 #endif
267 // =============================================================================
268
269 // =============================================================================
270 // Platform-internal mallopt variant.
271 // =============================================================================
272 #if defined(LIBC_STATIC)
android_mallopt(int opcode,void * arg,size_t arg_size)273 extern "C" bool android_mallopt(int opcode, void* arg, size_t arg_size) {
274 if (opcode == M_SET_ALLOCATION_LIMIT_BYTES) {
275 return LimitEnable(arg, arg_size);
276 }
277 errno = ENOTSUP;
278 return false;
279 }
280 #endif
281 // =============================================================================
282