blob: 863b07c14809f608e473172bb17d6d9e20755264 [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"
35// is set to a non-zero value. There are two functions exported to
36// allow ddms, or other external users to get information from the debug
37// allocation.
38// get_malloc_leak_info: Returns information about all of the known native
39// allocations that are currently in use.
40// free_malloc_leak_info: Frees the data allocated by the call to
41// get_malloc_leak_info.
42
Colin Cross869691c2016-01-29 12:48:18 -080043#include <pthread.h>
44
Christopher Ferris63860cb2015-11-16 17:30:32 -080045#include <private/bionic_config.h>
46#include <private/bionic_globals.h>
47#include <private/bionic_malloc_dispatch.h>
48
49#include "jemalloc.h"
50#define Malloc(function) je_ ## function
51
52static constexpr MallocDispatch __libc_malloc_default_dispatch
53 __attribute__((unused)) = {
54 Malloc(calloc),
55 Malloc(free),
56 Malloc(mallinfo),
57 Malloc(malloc),
58 Malloc(malloc_usable_size),
59 Malloc(memalign),
60 Malloc(posix_memalign),
61#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
62 Malloc(pvalloc),
63#endif
64 Malloc(realloc),
65#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
66 Malloc(valloc),
67#endif
Colin Cross869691c2016-01-29 12:48:18 -080068 Malloc(iterate),
69 Malloc(malloc_disable),
70 Malloc(malloc_enable),
Christopher Ferris63860cb2015-11-16 17:30:32 -080071 };
72
73// In a VM process, this is set to 1 after fork()ing out of zygote.
74int gMallocLeakZygoteChild = 0;
75
76// =============================================================================
77// Allocation functions
78// =============================================================================
79extern "C" void* calloc(size_t n_elements, size_t elem_size) {
80 auto _calloc = __libc_globals->malloc_dispatch.calloc;
81 if (__predict_false(_calloc != nullptr)) {
82 return _calloc(n_elements, elem_size);
83 }
84 return Malloc(calloc)(n_elements, elem_size);
85}
86
87extern "C" void free(void* mem) {
88 auto _free = __libc_globals->malloc_dispatch.free;
89 if (__predict_false(_free != nullptr)) {
90 _free(mem);
91 } else {
92 Malloc(free)(mem);
93 }
94}
95
96extern "C" struct mallinfo mallinfo() {
97 auto _mallinfo = __libc_globals->malloc_dispatch.mallinfo;
98 if (__predict_false(_mallinfo != nullptr)) {
99 return _mallinfo();
100 }
101 return Malloc(mallinfo)();
102}
103
104extern "C" void* malloc(size_t bytes) {
105 auto _malloc = __libc_globals->malloc_dispatch.malloc;
106 if (__predict_false(_malloc != nullptr)) {
107 return _malloc(bytes);
108 }
109 return Malloc(malloc)(bytes);
110}
111
112extern "C" size_t malloc_usable_size(const void* mem) {
113 auto _malloc_usable_size = __libc_globals->malloc_dispatch.malloc_usable_size;
114 if (__predict_false(_malloc_usable_size != nullptr)) {
115 return _malloc_usable_size(mem);
116 }
117 return Malloc(malloc_usable_size)(mem);
118}
119
120extern "C" void* memalign(size_t alignment, size_t bytes) {
121 auto _memalign = __libc_globals->malloc_dispatch.memalign;
122 if (__predict_false(_memalign != nullptr)) {
123 return _memalign(alignment, bytes);
124 }
125 return Malloc(memalign)(alignment, bytes);
126}
127
128extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
129 auto _posix_memalign = __libc_globals->malloc_dispatch.posix_memalign;
130 if (__predict_false(_posix_memalign != nullptr)) {
131 return _posix_memalign(memptr, alignment, size);
132 }
133 return Malloc(posix_memalign)(memptr, alignment, size);
134}
135
136extern "C" void* realloc(void* old_mem, size_t bytes) {
137 auto _realloc = __libc_globals->malloc_dispatch.realloc;
138 if (__predict_false(_realloc != nullptr)) {
139 return _realloc(old_mem, bytes);
140 }
141 return Malloc(realloc)(old_mem, bytes);
142}
143
144#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
145extern "C" void* pvalloc(size_t bytes) {
146 auto _pvalloc = __libc_globals->malloc_dispatch.pvalloc;
147 if (__predict_false(_pvalloc != nullptr)) {
148 return _pvalloc(bytes);
149 }
150 return Malloc(pvalloc)(bytes);
151}
152
153extern "C" void* valloc(size_t bytes) {
154 auto _valloc = __libc_globals->malloc_dispatch.valloc;
155 if (__predict_false(_valloc != nullptr)) {
156 return _valloc(bytes);
157 }
158 return Malloc(valloc)(bytes);
159}
160#endif
161
162// We implement malloc debugging only in libc.so, so the code below
163// must be excluded if we compile this file for static libc.a
164#if !defined(LIBC_STATIC)
165
166#include <dlfcn.h>
Christopher Ferris63860cb2015-11-16 17:30:32 -0800167#include <stdio.h>
168#include <stdlib.h>
169
170#include <private/libc_logging.h>
171#include <sys/system_properties.h>
172
173extern "C" int __cxa_atexit(void (*func)(void *), void *arg, void *dso);
174
175static const char* DEBUG_SHARED_LIB = "libc_malloc_debug.so";
176static const char* DEBUG_MALLOC_PROPERTY_OPTIONS = "libc.debug.malloc.options";
177static const char* DEBUG_MALLOC_PROPERTY_PROGRAM = "libc.debug.malloc.program";
178static const char* DEBUG_MALLOC_PROPERTY_ENV_ENABLED = "libc.debug.malloc.env_enabled";
179static const char* DEBUG_MALLOC_ENV_ENABLE = "LIBC_DEBUG_MALLOC_ENABLE";
180
181static void* libc_malloc_impl_handle = nullptr;
182
183static void (*g_debug_finalize_func)();
184static void (*g_debug_get_malloc_leak_info_func)(uint8_t**, size_t*, size_t*, size_t*, size_t*);
185static void (*g_debug_free_malloc_leak_info_func)(uint8_t*);
Colin Cross2d4721c2016-02-02 11:57:54 -0800186static ssize_t (*g_debug_malloc_backtrace_func)(void*, uintptr_t*, size_t);
Christopher Ferris63860cb2015-11-16 17:30:32 -0800187
188// =============================================================================
189// Log functions
190// =============================================================================
191#define error_log(format, ...) \
192 __libc_format_log(ANDROID_LOG_ERROR, "libc", (format), ##__VA_ARGS__ )
193#define info_log(format, ...) \
194 __libc_format_log(ANDROID_LOG_INFO, "libc", (format), ##__VA_ARGS__ )
195// =============================================================================
196
197// =============================================================================
198// Exported for use by ddms.
199// =============================================================================
200
201// Retrieve native heap information.
202//
203// "*info" is set to a buffer we allocate
204// "*overall_size" is set to the size of the "info" buffer
205// "*info_size" is set to the size of a single entry
206// "*total_memory" is set to the sum of all allocations we're tracking; does
207// not include heap overhead
208// "*backtrace_size" is set to the maximum number of entries in the back trace
209extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overall_size,
210 size_t* info_size, size_t* total_memory, size_t* backtrace_size) {
211 if (g_debug_get_malloc_leak_info_func == nullptr) {
212 return;
213 }
214 g_debug_get_malloc_leak_info_func(info, overall_size, info_size, total_memory, backtrace_size);
215}
216
217extern "C" void free_malloc_leak_info(uint8_t* info) {
218 if (g_debug_free_malloc_leak_info_func == nullptr) {
219 return;
220 }
221 g_debug_free_malloc_leak_info_func(info);
222}
Colin Cross869691c2016-01-29 12:48:18 -0800223
Christopher Ferris63860cb2015-11-16 17:30:32 -0800224// =============================================================================
225
226template<typename FunctionType>
227static bool InitMallocFunction(void* malloc_impl_handler, FunctionType* func, const char* prefix, const char* suffix) {
228 char symbol[128];
229 snprintf(symbol, sizeof(symbol), "%s_%s", prefix, suffix);
230 *func = reinterpret_cast<FunctionType>(dlsym(malloc_impl_handler, symbol));
231 if (*func == nullptr) {
232 error_log("%s: dlsym(\"%s\") failed", getprogname(), symbol);
233 return false;
234 }
235 return true;
236}
237
238static bool InitMalloc(void* malloc_impl_handler, MallocDispatch* table, const char* prefix) {
239 if (!InitMallocFunction<MallocCalloc>(malloc_impl_handler, &table->calloc,
240 prefix, "calloc")) {
241 return false;
242 }
243 if (!InitMallocFunction<MallocFree>(malloc_impl_handler, &table->free,
244 prefix, "free")) {
245 return false;
246 }
247 if (!InitMallocFunction<MallocMallinfo>(malloc_impl_handler, &table->mallinfo,
248 prefix, "mallinfo")) {
249 return false;
250 }
251 if (!InitMallocFunction<MallocMalloc>(malloc_impl_handler, &table->malloc,
252 prefix, "malloc")) {
253 return false;
254 }
255 if (!InitMallocFunction<MallocMallocUsableSize>(
256 malloc_impl_handler, &table->malloc_usable_size, prefix, "malloc_usable_size")) {
257 return false;
258 }
259 if (!InitMallocFunction<MallocMemalign>(malloc_impl_handler, &table->memalign,
260 prefix, "memalign")) {
261 return false;
262 }
263 if (!InitMallocFunction<MallocPosixMemalign>(malloc_impl_handler, &table->posix_memalign,
264 prefix, "posix_memalign")) {
265 return false;
266 }
267 if (!InitMallocFunction<MallocRealloc>(malloc_impl_handler, &table->realloc,
268 prefix, "realloc")) {
269 return false;
270 }
Colin Cross869691c2016-01-29 12:48:18 -0800271 if (!InitMallocFunction<MallocIterate>(malloc_impl_handler, &table->iterate,
272 prefix, "iterate")) {
273 return false;
274 }
275 if (!InitMallocFunction<MallocMallocDisable>(malloc_impl_handler, &table->malloc_disable,
276 prefix, "malloc_disable")) {
277 return false;
278 }
279 if (!InitMallocFunction<MallocMallocEnable>(malloc_impl_handler, &table->malloc_enable,
280 prefix, "malloc_enable")) {
281 return false;
282 }
Christopher Ferris63860cb2015-11-16 17:30:32 -0800283#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
284 if (!InitMallocFunction<MallocPvalloc>(malloc_impl_handler, &table->pvalloc,
285 prefix, "pvalloc")) {
286 return false;
287 }
288 if (!InitMallocFunction<MallocValloc>(malloc_impl_handler, &table->valloc,
289 prefix, "valloc")) {
290 return false;
291 }
292#endif
293
294 return true;
295}
296
297static void malloc_fini_impl(void*) {
298 // Our BSD stdio implementation doesn't close the standard streams,
299 // it only flushes them. Other unclosed FILE*s will show up as
300 // malloc leaks, but to avoid the standard streams showing up in
301 // leak reports, close them here.
302 fclose(stdin);
303 fclose(stdout);
304 fclose(stderr);
305
306 g_debug_finalize_func();
307}
308
309// Initializes memory allocation framework once per process.
310static void malloc_init_impl(libc_globals* globals) {
311 char value[PROP_VALUE_MAX];
312 if (__system_property_get(DEBUG_MALLOC_PROPERTY_OPTIONS, value) == 0 || value[0] == '\0') {
313 return;
314 }
315
316 // Check to see if only a specific program should have debug malloc enabled.
317 if (__system_property_get(DEBUG_MALLOC_PROPERTY_PROGRAM, value) != 0 &&
318 strstr(getprogname(), value) == nullptr) {
319 return;
320 }
321
322 // Check for the special environment variable instead.
323 if (__system_property_get(DEBUG_MALLOC_PROPERTY_ENV_ENABLED, value) != 0
324 && value[0] != '\0' && getenv(DEBUG_MALLOC_ENV_ENABLE) == nullptr) {
325 return;
326 }
327
328 // Load the debug malloc shared library.
329 void* malloc_impl_handle = dlopen(DEBUG_SHARED_LIB, RTLD_NOW | RTLD_LOCAL);
330 if (malloc_impl_handle == nullptr) {
331 error_log("%s: Unable to open debug malloc shared library %s: %s",
332 getprogname(), DEBUG_SHARED_LIB, dlerror());
333 return;
334 }
335
336 // Initialize malloc debugging in the loaded module.
337 void* sym = dlsym(malloc_impl_handle, "debug_initialize");
338 auto init_func = reinterpret_cast<bool (*)(const MallocDispatch*, int*)>(sym);
339 if (init_func == nullptr) {
340 error_log("%s: debug_initialize routine not found in %s", getprogname(), DEBUG_SHARED_LIB);
341 dlclose(malloc_impl_handle);
342 return;
343 }
344
345 // Get the syms for the external functions.
346 sym = dlsym(malloc_impl_handle, "debug_finalize");
347 if (sym == nullptr) {
348 error_log("%s: debug_finalize routine not found in %s", getprogname(), DEBUG_SHARED_LIB);
349 dlclose(malloc_impl_handle);
350 return;
351 }
352 g_debug_finalize_func = reinterpret_cast<void (*)()>(sym);
353
354 sym = dlsym(malloc_impl_handle, "debug_get_malloc_leak_info");
355 if (sym == nullptr) {
356 error_log("%s: debug_get_malloc_leak_info routine not found in %s", getprogname(),
357 DEBUG_SHARED_LIB);
358 dlclose(malloc_impl_handle);
359 return;
360 }
361 g_debug_get_malloc_leak_info_func = reinterpret_cast<void (*)(uint8_t**, size_t*, size_t*,
362 size_t*, size_t*)>(sym);
363
364 sym = dlsym(malloc_impl_handle, "debug_free_malloc_leak_info");
365 if (sym == nullptr) {
366 error_log("%s: debug_free_malloc_leak_info routine not found in %s", getprogname(),
367 DEBUG_SHARED_LIB);
368 dlclose(malloc_impl_handle);
369 return;
370 }
371 g_debug_free_malloc_leak_info_func = reinterpret_cast<void (*)(uint8_t*)>(sym);
372
Colin Cross2d4721c2016-02-02 11:57:54 -0800373 sym = dlsym(malloc_impl_handle, "debug_malloc_backtrace");
374 if (sym == nullptr) {
375 error_log("%s: debug_malloc_backtrace routine not found in %s", getprogname(),
376 DEBUG_SHARED_LIB);
377 dlclose(malloc_impl_handle);
378 return;
379 }
380 g_debug_malloc_backtrace_func = reinterpret_cast<ssize_t (*)(void*, uintptr_t*, size_t)>(sym);
381
Christopher Ferris63860cb2015-11-16 17:30:32 -0800382 if (!init_func(&__libc_malloc_default_dispatch, &gMallocLeakZygoteChild)) {
383 dlclose(malloc_impl_handle);
384 return;
385 }
386
387 MallocDispatch malloc_dispatch_table;
388 if (!InitMalloc(malloc_impl_handle, &malloc_dispatch_table, "debug")) {
389 g_debug_finalize_func();
390 dlclose(malloc_impl_handle);
391 return;
392 }
393
394 globals->malloc_dispatch = malloc_dispatch_table;
395 libc_malloc_impl_handle = malloc_impl_handle;
396
397 info_log("%s: malloc debug enabled", getprogname());
398
399 // Use atexit to trigger the cleanup function. This avoids a problem
400 // where another atexit function is used to cleanup allocated memory,
401 // but the finalize function was already called. This particular error
402 // seems to be triggered by a zygote spawned process calling exit.
403 int ret_value = __cxa_atexit(malloc_fini_impl, nullptr, nullptr);
404 if (ret_value != 0) {
405 error_log("failed to set atexit cleanup function: %d", ret_value);
406 }
407}
408
409// Initializes memory allocation framework.
410// This routine is called from __libc_init routines in libc_init_dynamic.cpp.
411__LIBC_HIDDEN__ void __libc_init_malloc(libc_globals* globals) {
412 malloc_init_impl(globals);
413}
414#endif // !LIBC_STATIC
Colin Cross869691c2016-01-29 12:48:18 -0800415
416// =============================================================================
417// Exported for use by libmemunreachable.
418// =============================================================================
419
420// Calls callback for every allocation in the anonymous heap mapping
421// [base, base+size). Must be called between malloc_disable and malloc_enable.
422extern "C" int malloc_iterate(uintptr_t base, size_t size,
423 void (*callback)(uintptr_t base, size_t size, void* arg), void* arg) {
424 auto _iterate = __libc_globals->malloc_dispatch.iterate;
425 if (__predict_false(_iterate != nullptr)) {
426 return _iterate(base, size, callback, arg);
427 }
428 return Malloc(iterate)(base, size, callback, arg);
429}
430
431// Disable calls to malloc so malloc_iterate gets a consistent view of
432// allocated memory.
433extern "C" void malloc_disable() {
434 auto _malloc_disable = __libc_globals->malloc_dispatch.malloc_disable;
435 if (__predict_false(_malloc_disable != nullptr)) {
436 return _malloc_disable();
437 }
438 return Malloc(malloc_disable)();
439}
440
441// Re-enable calls to malloc after a previous call to malloc_disable.
442extern "C" void malloc_enable() {
443 auto _malloc_enable = __libc_globals->malloc_dispatch.malloc_enable;
444 if (__predict_false(_malloc_enable != nullptr)) {
445 return _malloc_enable();
446 }
447 return Malloc(malloc_enable)();
448}
Colin Cross2d4721c2016-02-02 11:57:54 -0800449
450#ifndef LIBC_STATIC
451extern "C" ssize_t malloc_backtrace(void* pointer, uintptr_t* frames, size_t frame_count) {
452 if (g_debug_malloc_backtrace_func == nullptr) {
453 return 0;
454 }
455 return g_debug_malloc_backtrace_func(pointer, frames, frame_count);
456}
457#else
458extern "C" ssize_t malloc_backtrace(void*, uintptr_t*, size_t) {
459 return 0;
460}
461#endif