blob: db3f995ef507922a7571c03057e7731f82558255 [file] [log] [blame]
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -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/*
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -080030 * Contains definition of structures, global variables, and implementation of
31 * routines that are used by malloc leak detection code and other components in
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080032 * the system. The trick is that some components expect these data and
33 * routines to be defined / implemented in libc.so library, regardless
34 * whether or not MALLOC_LEAK_CHECK macro is defined. To make things even
35 * more tricky, malloc leak detection code, implemented in
36 * libc_malloc_debug.so also requires access to these variables and routines
37 * (to fill allocation entry hash table, for example). So, all relevant
38 * variables and routines are defined / implemented here and exported
39 * to all, leak detection code and other components via dynamic (libc.so),
40 * or static (libc.a) linking.
41 */
42
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080043#include "malloc_debug_common.h"
44
Elliott Hughes3b297c42012-10-11 16:08:51 -070045#include <pthread.h>
46#include <stdlib.h>
47#include <unistd.h>
48
Elliott Hugheseb847bc2013-10-09 15:50:50 -070049#include "private/ScopedPthreadMutexLocker.h"
Elliott Hughes3b297c42012-10-11 16:08:51 -070050
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080051/*
52 * In a VM process, this is set to 1 after fork()ing out of zygote.
53 */
54int gMallocLeakZygoteChild = 0;
55
Elliott Hughes6b7987c2014-05-16 15:23:54 -070056__LIBC_HIDDEN__ pthread_mutex_t g_allocations_mutex = PTHREAD_MUTEX_INITIALIZER;
57__LIBC_HIDDEN__ HashTable g_hash_table;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080058
59// =============================================================================
60// output functions
61// =============================================================================
62
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070063static int hash_entry_compare(const void* arg1, const void* arg2) {
Christopher Tate52e7d3d2010-08-09 13:43:46 -070064 int result;
65
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070066 const HashEntry* e1 = *static_cast<HashEntry* const*>(arg1);
67 const HashEntry* e2 = *static_cast<HashEntry* const*>(arg2);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080068
Christopher Tate52e7d3d2010-08-09 13:43:46 -070069 // if one or both arg pointers are null, deal gracefully
70 if (e1 == NULL) {
71 result = (e2 == NULL) ? 0 : 1;
72 } else if (e2 == NULL) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080073 result = -1;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080074 } else {
Christopher Tate52e7d3d2010-08-09 13:43:46 -070075 size_t nbAlloc1 = e1->allocations;
76 size_t nbAlloc2 = e2->allocations;
77 size_t size1 = e1->size & ~SIZE_FLAG_MASK;
78 size_t size2 = e2->size & ~SIZE_FLAG_MASK;
79 size_t alloc1 = nbAlloc1 * size1;
80 size_t alloc2 = nbAlloc2 * size2;
81
82 // sort in descending order by:
83 // 1) total size
84 // 2) number of allocations
85 //
86 // This is used for sorting, not determination of equality, so we don't
87 // need to compare the bit flags.
Christopher Tate52e7d3d2010-08-09 13:43:46 -070088 if (alloc1 > alloc2) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080089 result = -1;
Christopher Tate52e7d3d2010-08-09 13:43:46 -070090 } else if (alloc1 < alloc2) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080091 result = 1;
92 } else {
Christopher Tate52e7d3d2010-08-09 13:43:46 -070093 if (nbAlloc1 > nbAlloc2) {
94 result = -1;
95 } else if (nbAlloc1 < nbAlloc2) {
96 result = 1;
97 } else {
98 result = 0;
99 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800100 }
101 }
102 return result;
103}
104
105/*
106 * Retrieve native heap information.
107 *
108 * "*info" is set to a buffer we allocate
109 * "*overallSize" is set to the size of the "info" buffer
110 * "*infoSize" is set to the size of a single entry
111 * "*totalMemory" is set to the sum of all allocations we're tracking; does
112 * not include heap overhead
113 * "*backtraceSize" is set to the maximum number of entries in the back trace
114 */
Elliott Hughes7c9923d2014-05-16 16:29:55 -0700115
116// Exported for use by ddms.
117extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700118 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800119 // don't do anything if we have invalid arguments
120 if (info == NULL || overallSize == NULL || infoSize == NULL ||
121 totalMemory == NULL || backtraceSize == NULL) {
122 return;
123 }
tedbo9d8be542010-10-05 13:06:06 -0700124 *totalMemory = 0;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800125
Elliott Hughes1728b232014-05-14 10:02:03 -0700126 ScopedPthreadMutexLocker locker(&g_allocations_mutex);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800127
Elliott Hughes1728b232014-05-14 10:02:03 -0700128 if (g_hash_table.count == 0) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800129 *info = NULL;
130 *overallSize = 0;
131 *infoSize = 0;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800132 *backtraceSize = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700133 return;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800134 }
135
Christopher Ferris72bbd422014-05-08 11:14:03 -0700136 HashEntry** list = static_cast<HashEntry**>(Malloc(malloc)(sizeof(void*) * g_hash_table.count));
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800137
138 // get the entries into an array to be sorted
139 int index = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700140 for (size_t i = 0 ; i < HASHTABLE_SIZE ; ++i) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700141 HashEntry* entry = g_hash_table.slots[i];
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800142 while (entry != NULL) {
143 list[index] = entry;
144 *totalMemory = *totalMemory +
145 ((entry->size & ~SIZE_FLAG_MASK) * entry->allocations);
146 index++;
147 entry = entry->next;
148 }
149 }
150
151 // XXX: the protocol doesn't allow variable size for the stack trace (yet)
Elliott Hughes239e7a02013-01-25 17:13:45 -0800152 *infoSize = (sizeof(size_t) * 2) + (sizeof(uintptr_t) * BACKTRACE_SIZE);
Elliott Hughes1728b232014-05-14 10:02:03 -0700153 *overallSize = *infoSize * g_hash_table.count;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800154 *backtraceSize = BACKTRACE_SIZE;
155
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700156 // now get a byte array big enough for this
Christopher Ferris72bbd422014-05-08 11:14:03 -0700157 *info = static_cast<uint8_t*>(Malloc(malloc)(*overallSize));
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800158
159 if (*info == NULL) {
160 *overallSize = 0;
Christopher Ferris72bbd422014-05-08 11:14:03 -0700161 Malloc(free)(list);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700162 return;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800163 }
164
Elliott Hughes1728b232014-05-14 10:02:03 -0700165 qsort(list, g_hash_table.count, sizeof(void*), hash_entry_compare);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800166
167 uint8_t* head = *info;
Elliott Hughes1728b232014-05-14 10:02:03 -0700168 const int count = g_hash_table.count;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700169 for (int i = 0 ; i < count ; ++i) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800170 HashEntry* entry = list[i];
Elliott Hughes239e7a02013-01-25 17:13:45 -0800171 size_t entrySize = (sizeof(size_t) * 2) + (sizeof(uintptr_t) * entry->numEntries);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800172 if (entrySize < *infoSize) {
173 /* we're writing less than a full entry, clear out the rest */
The Android Open Source Project95faece2010-04-08 11:11:53 -0700174 memset(head + entrySize, 0, *infoSize - entrySize);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800175 } else {
176 /* make sure the amount we're copying doesn't exceed the limit */
177 entrySize = *infoSize;
178 }
179 memcpy(head, &(entry->size), entrySize);
180 head += *infoSize;
181 }
182
Christopher Ferris72bbd422014-05-08 11:14:03 -0700183 Malloc(free)(list);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800184}
185
Elliott Hughes7c9923d2014-05-16 16:29:55 -0700186// Exported for use by ddms.
187extern "C" void free_malloc_leak_info(uint8_t* info) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700188 Malloc(free)(info);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800189}
190
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700191extern "C" struct mallinfo mallinfo() {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700192 return Malloc(mallinfo)();
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800193}
194
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700195extern "C" void* valloc(size_t bytes) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700196 return Malloc(valloc)(bytes);
Ian Rogers99908912012-08-17 17:28:15 -0700197}
198
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700199extern "C" void* pvalloc(size_t bytes) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700200 return Malloc(pvalloc)(bytes);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800201}
202
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700203extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700204 return Malloc(posix_memalign)(memptr, alignment, size);
Brian Carlstrombfc1d972012-08-20 18:28:20 -0700205}
206
Christopher Ferris72bbd422014-05-08 11:14:03 -0700207// Support for malloc debugging.
208// Table for dispatching malloc calls, initialized with default dispatchers.
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700209extern const MallocDebug __libc_malloc_default_dispatch;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700210const MallocDebug __libc_malloc_default_dispatch __attribute__((aligned(32))) =
211{
Christopher Ferris72bbd422014-05-08 11:14:03 -0700212 Malloc(malloc), Malloc(free), Malloc(calloc), Malloc(realloc), Malloc(memalign), Malloc(malloc_usable_size),
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800213};
214
215/* Selector of dispatch table to use for dispatching malloc calls. */
216const MallocDebug* __libc_malloc_dispatch = &__libc_malloc_default_dispatch;
217
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700218extern "C" void* malloc(size_t bytes) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800219 return __libc_malloc_dispatch->malloc(bytes);
220}
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700221
222extern "C" void free(void* mem) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800223 __libc_malloc_dispatch->free(mem);
224}
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700225
226extern "C" void* calloc(size_t n_elements, size_t elem_size) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800227 return __libc_malloc_dispatch->calloc(n_elements, elem_size);
228}
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700229
230extern "C" void* realloc(void* oldMem, size_t bytes) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800231 return __libc_malloc_dispatch->realloc(oldMem, bytes);
232}
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700233
234extern "C" void* memalign(size_t alignment, size_t bytes) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800235 return __libc_malloc_dispatch->memalign(alignment, bytes);
236}
237
Christopher Ferris885f3b92013-05-21 17:48:01 -0700238extern "C" size_t malloc_usable_size(const void* mem) {
239 return __libc_malloc_dispatch->malloc_usable_size(mem);
240}
241
Elliott Hughesdb492b32013-01-03 15:44:03 -0800242/* We implement malloc debugging only in libc.so, so code below
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800243 * must be excluded if we compile this file for static libc.a
244 */
245#ifndef LIBC_STATIC
246#include <sys/system_properties.h>
247#include <dlfcn.h>
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700248#include <stdio.h>
Elliott Hugheseb847bc2013-10-09 15:50:50 -0700249#include "private/libc_logging.h"
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800250
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800251/* Table for dispatching malloc calls, depending on environment. */
Elliott Hughes1728b232014-05-14 10:02:03 -0700252static MallocDebug g_malloc_dispatch_table __attribute__((aligned(32))) = {
Christopher Ferris72bbd422014-05-08 11:14:03 -0700253 Malloc(malloc), Malloc(free), Malloc(calloc), Malloc(realloc), Malloc(memalign), Malloc(malloc_usable_size)
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800254};
255
Elliott Hughese4ccf5a2013-02-07 12:06:44 -0800256extern const char* __progname;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800257
258/* Handle to shared library where actual memory allocation is implemented.
259 * This library is loaded and memory allocation calls are redirected there
260 * when libc.debug.malloc environment variable contains value other than
261 * zero:
262 * 1 - For memory leak detections.
263 * 5 - For filling allocated / freed memory with patterns defined by
264 * CHK_SENTINEL_VALUE, and CHK_FILL_FREE macros.
265 * 10 - For adding pre-, and post- allocation stubs in order to detect
266 * buffer overruns.
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800267 * Note that emulator's memory allocation instrumentation is not controlled by
268 * libc.debug.malloc value, but rather by emulator, started with -memcheck
269 * option. Note also, that if emulator has started with -memcheck option,
270 * emulator's instrumented memory allocation will take over value saved in
271 * libc.debug.malloc. In other words, if emulator has started with -memcheck
272 * option, libc.debug.malloc value is ignored.
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800273 * Actual functionality for debug levels 1-10 is implemented in
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800274 * libc_malloc_debug_leak.so, while functionality for emultor's instrumented
275 * allocations is implemented in libc_malloc_debug_qemu.so and can be run inside
Christopher Ferris885f3b92013-05-21 17:48:01 -0700276 * the emulator only.
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800277 */
278static void* libc_malloc_impl_handle = NULL;
279
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700280/* This variable is set to the value of property libc.debug.malloc.backlog,
281 * when the value of libc.debug.malloc = 10. It determines the size of the
282 * backlog we use to detect multiple frees. If the property is not set, the
Elliott Hughes9c818922013-02-01 17:07:40 -0800283 * backlog length defaults to BACKLOG_DEFAULT_LEN.
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700284 */
Elliott Hughes6b7987c2014-05-16 15:23:54 -0700285__LIBC_HIDDEN__ unsigned int g_malloc_debug_backlog;
Elliott Hughes9c818922013-02-01 17:07:40 -0800286#define BACKLOG_DEFAULT_LEN 100
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700287
Elliott Hughes9c818922013-02-01 17:07:40 -0800288/* The value of libc.debug.malloc. */
Elliott Hughes6b7987c2014-05-16 15:23:54 -0700289__LIBC_HIDDEN__ int g_malloc_debug_level;
Elliott Hughes9c818922013-02-01 17:07:40 -0800290
Christopher Ferris885f3b92013-05-21 17:48:01 -0700291template<typename FunctionType>
Nick Kralevich35c18622013-10-03 14:59:05 -0700292static void InitMallocFunction(void* malloc_impl_handler, FunctionType* func, const char* prefix, const char* suffix) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700293 char symbol[128];
294 snprintf(symbol, sizeof(symbol), "%s_%s", prefix, suffix);
295 *func = reinterpret_cast<FunctionType>(dlsym(malloc_impl_handler, symbol));
296 if (*func == NULL) {
297 error_log("%s: dlsym(\"%s\") failed", __progname, symbol);
298 }
299}
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700300
Christopher Ferris885f3b92013-05-21 17:48:01 -0700301static void InitMalloc(void* malloc_impl_handler, MallocDebug* table, const char* prefix) {
302 __libc_format_log(ANDROID_LOG_INFO, "libc", "%s: using libc.debug.malloc %d (%s)\n",
Elliott Hughes1728b232014-05-14 10:02:03 -0700303 __progname, g_malloc_debug_level, prefix);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700304
Christopher Ferris885f3b92013-05-21 17:48:01 -0700305 InitMallocFunction<MallocDebugMalloc>(malloc_impl_handler, &table->malloc, prefix, "malloc");
306 InitMallocFunction<MallocDebugFree>(malloc_impl_handler, &table->free, prefix, "free");
307 InitMallocFunction<MallocDebugCalloc>(malloc_impl_handler, &table->calloc, prefix, "calloc");
308 InitMallocFunction<MallocDebugRealloc>(malloc_impl_handler, &table->realloc, prefix, "realloc");
309 InitMallocFunction<MallocDebugMemalign>(malloc_impl_handler, &table->memalign, prefix, "memalign");
310 InitMallocFunction<MallocDebugMallocUsableSize>(malloc_impl_handler, &table->malloc_usable_size, prefix, "malloc_usable_size");
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700311}
312
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800313/* Initializes memory allocation framework once per process. */
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700314static void malloc_init_impl() {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800315 const char* so_name = NULL;
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800316 MallocDebugInit malloc_debug_initialize = NULL;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800317 unsigned int qemu_running = 0;
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800318 unsigned int memcheck_enabled = 0;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800319 char env[PROP_VALUE_MAX];
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800320 char memcheck_tracing[PROP_VALUE_MAX];
Iliyan Malchev7d2e24e2012-05-29 16:46:17 -0700321 char debug_program[PROP_VALUE_MAX];
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800322
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800323 /* Get custom malloc debug level. Note that emulator started with
324 * memory checking option will have priority over debug level set in
325 * libc.debug.malloc system property. */
326 if (__system_property_get("ro.kernel.qemu", env) && atoi(env)) {
327 qemu_running = 1;
328 if (__system_property_get("ro.kernel.memcheck", memcheck_tracing)) {
329 if (memcheck_tracing[0] != '0') {
330 // Emulator has started with memory tracing enabled. Enforce it.
Elliott Hughes1728b232014-05-14 10:02:03 -0700331 g_malloc_debug_level = 20;
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800332 memcheck_enabled = 1;
333 }
334 }
335 }
336
337 /* If debug level has not been set by memcheck option in the emulator,
338 * lets grab it from libc.debug.malloc system property. */
Elliott Hughes1728b232014-05-14 10:02:03 -0700339 if (g_malloc_debug_level == 0 && __system_property_get("libc.debug.malloc", env)) {
340 g_malloc_debug_level = atoi(env);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800341 }
342
Christopher Ferris72bbd422014-05-08 11:14:03 -0700343 /* Debug level 0 means that we should use default allocation routines. */
Elliott Hughes1728b232014-05-14 10:02:03 -0700344 if (g_malloc_debug_level == 0) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800345 return;
346 }
347
Iliyan Malchev7d2e24e2012-05-29 16:46:17 -0700348 /* If libc.debug.malloc.program is set and is not a substring of progname,
349 * then exit.
350 */
351 if (__system_property_get("libc.debug.malloc.program", debug_program)) {
352 if (!strstr(__progname, debug_program)) {
353 return;
354 }
355 }
356
Elliott Hughes84f8b5f2013-01-22 18:35:14 -0800357 // mksh is way too leaky. http://b/7291287.
Elliott Hughes1728b232014-05-14 10:02:03 -0700358 if (g_malloc_debug_level >= 10) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800359 if (strcmp(__progname, "sh") == 0 || strcmp(__progname, "/system/bin/sh") == 0) {
360 return;
361 }
Elliott Hughes84f8b5f2013-01-22 18:35:14 -0800362 }
363
Elliott Hughese5d5f7f2012-10-09 17:23:09 -0700364 // Choose the appropriate .so for the requested debug level.
Elliott Hughes1728b232014-05-14 10:02:03 -0700365 switch (g_malloc_debug_level) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800366 case 1:
367 case 5:
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700368 case 10: {
369 char debug_backlog[PROP_VALUE_MAX];
370 if (__system_property_get("libc.debug.malloc.backlog", debug_backlog)) {
Elliott Hughes1728b232014-05-14 10:02:03 -0700371 g_malloc_debug_backlog = atoi(debug_backlog);
372 info_log("%s: setting backlog length to %d\n", __progname, g_malloc_debug_backlog);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700373 }
Elliott Hughes1728b232014-05-14 10:02:03 -0700374 if (g_malloc_debug_backlog == 0) {
375 g_malloc_debug_backlog = BACKLOG_DEFAULT_LEN;
Elliott Hughes9c818922013-02-01 17:07:40 -0800376 }
Elliott Hughes64b29632014-04-01 13:48:30 -0700377 so_name = "libc_malloc_debug_leak.so";
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800378 break;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700379 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800380 case 20:
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800381 // Quick check: debug level 20 can only be handled in emulator.
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800382 if (!qemu_running) {
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800383 error_log("%s: Debug level %d can only be set in emulator\n",
Elliott Hughes1728b232014-05-14 10:02:03 -0700384 __progname, g_malloc_debug_level);
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800385 return;
386 }
387 // Make sure that memory checking has been enabled in emulator.
388 if (!memcheck_enabled) {
389 error_log("%s: Memory checking is not enabled in the emulator\n",
390 __progname);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800391 return;
392 }
Elliott Hughes64b29632014-04-01 13:48:30 -0700393 so_name = "libc_malloc_debug_qemu.so";
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800394 break;
395 default:
Elliott Hughes1728b232014-05-14 10:02:03 -0700396 error_log("%s: Debug level %d is unknown\n", __progname, g_malloc_debug_level);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800397 return;
398 }
399
400 // Load .so that implements the required malloc debugging functionality.
Christopher Ferris885f3b92013-05-21 17:48:01 -0700401 void* malloc_impl_handle = dlopen(so_name, RTLD_LAZY);
402 if (malloc_impl_handle == NULL) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700403 error_log("%s: Missing module %s required for malloc debug level %d: %s",
Elliott Hughes1728b232014-05-14 10:02:03 -0700404 __progname, so_name, g_malloc_debug_level, dlerror());
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800405 return;
406 }
407
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800408 // Initialize malloc debugging in the loaded module.
Christopher Ferris885f3b92013-05-21 17:48:01 -0700409 malloc_debug_initialize = reinterpret_cast<MallocDebugInit>(dlsym(malloc_impl_handle,
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700410 "malloc_debug_initialize"));
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800411 if (malloc_debug_initialize == NULL) {
412 error_log("%s: Initialization routine is not found in %s\n",
413 __progname, so_name);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700414 dlclose(malloc_impl_handle);
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800415 return;
416 }
Elliott Hughes1e980b62013-01-17 18:36:06 -0800417 if (malloc_debug_initialize() == -1) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700418 dlclose(malloc_impl_handle);
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800419 return;
420 }
421
Elliott Hughes1728b232014-05-14 10:02:03 -0700422 if (g_malloc_debug_level == 20) {
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800423 // For memory checker we need to do extra initialization.
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700424 typedef int (*MemCheckInit)(int, const char*);
425 MemCheckInit memcheck_initialize =
Christopher Ferris885f3b92013-05-21 17:48:01 -0700426 reinterpret_cast<MemCheckInit>(dlsym(malloc_impl_handle,
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700427 "memcheck_initialize"));
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800428 if (memcheck_initialize == NULL) {
429 error_log("%s: memcheck_initialize routine is not found in %s\n",
430 __progname, so_name);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700431 dlclose(malloc_impl_handle);
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800432 return;
433 }
Elliott Hughesdb492b32013-01-03 15:44:03 -0800434
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800435 if (memcheck_initialize(MALLOC_ALIGNMENT, memcheck_tracing)) {
Christopher Ferris885f3b92013-05-21 17:48:01 -0700436 dlclose(malloc_impl_handle);
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800437 return;
438 }
439 }
440
Christopher Ferris885f3b92013-05-21 17:48:01 -0700441
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800442 // Initialize malloc dispatch table with appropriate routines.
Elliott Hughes1728b232014-05-14 10:02:03 -0700443 switch (g_malloc_debug_level) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800444 case 1:
Elliott Hughes1728b232014-05-14 10:02:03 -0700445 InitMalloc(malloc_impl_handle, &g_malloc_dispatch_table, "leak");
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800446 break;
447 case 5:
Elliott Hughes1728b232014-05-14 10:02:03 -0700448 InitMalloc(malloc_impl_handle, &g_malloc_dispatch_table, "fill");
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800449 break;
450 case 10:
Elliott Hughes1728b232014-05-14 10:02:03 -0700451 InitMalloc(malloc_impl_handle, &g_malloc_dispatch_table, "chk");
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800452 break;
453 case 20:
Elliott Hughes1728b232014-05-14 10:02:03 -0700454 InitMalloc(malloc_impl_handle, &g_malloc_dispatch_table, "qemu_instrumented");
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800455 break;
456 default:
457 break;
458 }
459
460 // Make sure dispatch table is initialized
Elliott Hughes1728b232014-05-14 10:02:03 -0700461 if ((g_malloc_dispatch_table.malloc == NULL) ||
462 (g_malloc_dispatch_table.free == NULL) ||
463 (g_malloc_dispatch_table.calloc == NULL) ||
464 (g_malloc_dispatch_table.realloc == NULL) ||
465 (g_malloc_dispatch_table.memalign == NULL) ||
466 (g_malloc_dispatch_table.malloc_usable_size == NULL)) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700467 error_log("%s: some symbols for libc.debug.malloc level %d were not found (see above)",
Elliott Hughes1728b232014-05-14 10:02:03 -0700468 __progname, g_malloc_debug_level);
Christopher Ferris885f3b92013-05-21 17:48:01 -0700469 dlclose(malloc_impl_handle);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800470 } else {
Elliott Hughes1728b232014-05-14 10:02:03 -0700471 __libc_malloc_dispatch = &g_malloc_dispatch_table;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700472 libc_malloc_impl_handle = malloc_impl_handle;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800473 }
474}
475
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700476static void malloc_fini_impl() {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800477 // Our BSD stdio implementation doesn't close the standard streams, it only flushes them.
478 // And it doesn't do that until its atexit handler (_cleanup) is run, and we run first!
479 // It's great that other unclosed FILE*s show up as malloc leaks, but we need to manually
480 // clean up the standard streams ourselves.
481 fclose(stdin);
482 fclose(stdout);
483 fclose(stderr);
484
485 if (libc_malloc_impl_handle != NULL) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700486 MallocDebugFini malloc_debug_finalize =
487 reinterpret_cast<MallocDebugFini>(dlsym(libc_malloc_impl_handle,
488 "malloc_debug_finalize"));
Elliott Hughes1e980b62013-01-17 18:36:06 -0800489 if (malloc_debug_finalize != NULL) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700490 malloc_debug_finalize();
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700491 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700492 }
493}
494
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800495static pthread_once_t malloc_init_once_ctl = PTHREAD_ONCE_INIT;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700496static pthread_once_t malloc_fini_once_ctl = PTHREAD_ONCE_INIT;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800497
498#endif // !LIBC_STATIC
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800499
500/* Initializes memory allocation framework.
501 * This routine is called from __libc_init routines implemented
502 * in libc_init_static.c and libc_init_dynamic.c files.
503 */
Kito Chengea489742013-04-12 16:13:34 +0800504extern "C" __LIBC_HIDDEN__ void malloc_debug_init() {
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800505 /* We need to initialize malloc iff we implement here custom
506 * malloc routines (i.e. USE_DL_PREFIX is defined) for libc.so */
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800507#if defined(USE_DL_PREFIX) && !defined(LIBC_STATIC)
Elliott Hughesdb492b32013-01-03 15:44:03 -0800508 if (pthread_once(&malloc_init_once_ctl, malloc_init_impl)) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800509 error_log("Unable to initialize malloc_debug component.");
510 }
511#endif // USE_DL_PREFIX && !LIBC_STATIC
512}
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700513
Kito Chengea489742013-04-12 16:13:34 +0800514extern "C" __LIBC_HIDDEN__ void malloc_debug_fini() {
Elliott Hughesdb492b32013-01-03 15:44:03 -0800515 /* We need to finalize malloc iff we implement here custom
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700516 * malloc routines (i.e. USE_DL_PREFIX is defined) for libc.so */
517#if defined(USE_DL_PREFIX) && !defined(LIBC_STATIC)
Elliott Hughesdb492b32013-01-03 15:44:03 -0800518 if (pthread_once(&malloc_fini_once_ctl, malloc_fini_impl)) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700519 error_log("Unable to finalize malloc_debug component.");
520 }
521#endif // USE_DL_PREFIX && !LIBC_STATIC
522}