blob: 248a2db25dcae5ee8f12cbc0f1dc9f27c89296a7 [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
49#include "dlmalloc.h"
50#include "ScopedPthreadMutexLocker.h"
51
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080052/*
53 * In a VM process, this is set to 1 after fork()ing out of zygote.
54 */
55int gMallocLeakZygoteChild = 0;
56
57pthread_mutex_t gAllocationsMutex = PTHREAD_MUTEX_INITIALIZER;
58HashTable gHashTable;
59
60// =============================================================================
61// output functions
62// =============================================================================
63
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070064static int hash_entry_compare(const void* arg1, const void* arg2) {
Christopher Tate52e7d3d2010-08-09 13:43:46 -070065 int result;
66
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070067 const HashEntry* e1 = *static_cast<HashEntry* const*>(arg1);
68 const HashEntry* e2 = *static_cast<HashEntry* const*>(arg2);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080069
Christopher Tate52e7d3d2010-08-09 13:43:46 -070070 // if one or both arg pointers are null, deal gracefully
71 if (e1 == NULL) {
72 result = (e2 == NULL) ? 0 : 1;
73 } else if (e2 == NULL) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080074 result = -1;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080075 } else {
Christopher Tate52e7d3d2010-08-09 13:43:46 -070076 size_t nbAlloc1 = e1->allocations;
77 size_t nbAlloc2 = e2->allocations;
78 size_t size1 = e1->size & ~SIZE_FLAG_MASK;
79 size_t size2 = e2->size & ~SIZE_FLAG_MASK;
80 size_t alloc1 = nbAlloc1 * size1;
81 size_t alloc2 = nbAlloc2 * size2;
82
83 // sort in descending order by:
84 // 1) total size
85 // 2) number of allocations
86 //
87 // This is used for sorting, not determination of equality, so we don't
88 // need to compare the bit flags.
Christopher Tate52e7d3d2010-08-09 13:43:46 -070089 if (alloc1 > alloc2) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080090 result = -1;
Christopher Tate52e7d3d2010-08-09 13:43:46 -070091 } else if (alloc1 < alloc2) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080092 result = 1;
93 } else {
Christopher Tate52e7d3d2010-08-09 13:43:46 -070094 if (nbAlloc1 > nbAlloc2) {
95 result = -1;
96 } else if (nbAlloc1 < nbAlloc2) {
97 result = 1;
98 } else {
99 result = 0;
100 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800101 }
102 }
103 return result;
104}
105
106/*
107 * Retrieve native heap information.
108 *
109 * "*info" is set to a buffer we allocate
110 * "*overallSize" is set to the size of the "info" buffer
111 * "*infoSize" is set to the size of a single entry
112 * "*totalMemory" is set to the sum of all allocations we're tracking; does
113 * not include heap overhead
114 * "*backtraceSize" is set to the maximum number of entries in the back trace
115 */
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700116extern "C" void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
117 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800118 // don't do anything if we have invalid arguments
119 if (info == NULL || overallSize == NULL || infoSize == NULL ||
120 totalMemory == NULL || backtraceSize == NULL) {
121 return;
122 }
tedbo9d8be542010-10-05 13:06:06 -0700123 *totalMemory = 0;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800124
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700125 ScopedPthreadMutexLocker locker(&gAllocationsMutex);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800126
127 if (gHashTable.count == 0) {
128 *info = NULL;
129 *overallSize = 0;
130 *infoSize = 0;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800131 *backtraceSize = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700132 return;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800133 }
134
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700135 HashEntry** list = static_cast<HashEntry**>(dlmalloc(sizeof(void*) * gHashTable.count));
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800136
137 // get the entries into an array to be sorted
138 int index = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700139 for (size_t i = 0 ; i < HASHTABLE_SIZE ; ++i) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800140 HashEntry* entry = gHashTable.slots[i];
141 while (entry != NULL) {
142 list[index] = entry;
143 *totalMemory = *totalMemory +
144 ((entry->size & ~SIZE_FLAG_MASK) * entry->allocations);
145 index++;
146 entry = entry->next;
147 }
148 }
149
150 // XXX: the protocol doesn't allow variable size for the stack trace (yet)
Elliott Hughes239e7a02013-01-25 17:13:45 -0800151 *infoSize = (sizeof(size_t) * 2) + (sizeof(uintptr_t) * BACKTRACE_SIZE);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800152 *overallSize = *infoSize * gHashTable.count;
153 *backtraceSize = BACKTRACE_SIZE;
154
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700155 // now get a byte array big enough for this
156 *info = static_cast<uint8_t*>(dlmalloc(*overallSize));
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800157
158 if (*info == NULL) {
159 *overallSize = 0;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700160 dlfree(list);
161 return;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800162 }
163
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700164 qsort(list, gHashTable.count, sizeof(void*), hash_entry_compare);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800165
166 uint8_t* head = *info;
167 const int count = gHashTable.count;
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700168 for (int i = 0 ; i < count ; ++i) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800169 HashEntry* entry = list[i];
Elliott Hughes239e7a02013-01-25 17:13:45 -0800170 size_t entrySize = (sizeof(size_t) * 2) + (sizeof(uintptr_t) * entry->numEntries);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800171 if (entrySize < *infoSize) {
172 /* we're writing less than a full entry, clear out the rest */
The Android Open Source Project95faece2010-04-08 11:11:53 -0700173 memset(head + entrySize, 0, *infoSize - entrySize);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800174 } else {
175 /* make sure the amount we're copying doesn't exceed the limit */
176 entrySize = *infoSize;
177 }
178 memcpy(head, &(entry->size), entrySize);
179 head += *infoSize;
180 }
181
182 dlfree(list);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800183}
184
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700185extern "C" void free_malloc_leak_info(uint8_t* info) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800186 dlfree(info);
187}
188
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700189extern "C" struct mallinfo mallinfo() {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800190 return dlmallinfo();
191}
192
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700193extern "C" size_t malloc_usable_size(void* mem) {
Ian Rogers99908912012-08-17 17:28:15 -0700194 return dlmalloc_usable_size(mem);
195}
196
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700197extern "C" void* valloc(size_t bytes) {
Ian Rogers99908912012-08-17 17:28:15 -0700198 return dlvalloc(bytes);
199}
200
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700201extern "C" void* pvalloc(size_t bytes) {
Ian Rogers99908912012-08-17 17:28:15 -0700202 return dlpvalloc(bytes);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800203}
204
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700205extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) {
Brian Carlstrombfc1d972012-08-20 18:28:20 -0700206 return dlposix_memalign(memptr, alignment, size);
207}
208
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800209/* Support for malloc debugging.
210 * Note that if USE_DL_PREFIX is not defined, it's assumed that memory
211 * allocation routines are implemented somewhere else, so all our custom
212 * malloc routines should not be compiled at all.
213 */
214#ifdef USE_DL_PREFIX
215
216/* Table for dispatching malloc calls, initialized with default dispatchers. */
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700217extern const MallocDebug __libc_malloc_default_dispatch;
218const MallocDebug __libc_malloc_default_dispatch __attribute__((aligned(32))) = {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800219 dlmalloc, dlfree, dlcalloc, dlrealloc, dlmemalign
220};
221
222/* Selector of dispatch table to use for dispatching malloc calls. */
223const MallocDebug* __libc_malloc_dispatch = &__libc_malloc_default_dispatch;
224
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700225extern "C" void* malloc(size_t bytes) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800226 return __libc_malloc_dispatch->malloc(bytes);
227}
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700228
229extern "C" void free(void* mem) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800230 __libc_malloc_dispatch->free(mem);
231}
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700232
233extern "C" void* calloc(size_t n_elements, size_t elem_size) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800234 return __libc_malloc_dispatch->calloc(n_elements, elem_size);
235}
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700236
237extern "C" void* realloc(void* oldMem, size_t bytes) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800238 return __libc_malloc_dispatch->realloc(oldMem, bytes);
239}
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700240
241extern "C" void* memalign(size_t alignment, size_t bytes) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800242 return __libc_malloc_dispatch->memalign(alignment, bytes);
243}
244
Elliott Hughesdb492b32013-01-03 15:44:03 -0800245/* We implement malloc debugging only in libc.so, so code below
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800246 * must be excluded if we compile this file for static libc.a
247 */
248#ifndef LIBC_STATIC
249#include <sys/system_properties.h>
250#include <dlfcn.h>
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700251#include <stdio.h>
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800252#include "logd.h"
253
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800254/* Table for dispatching malloc calls, depending on environment. */
255static MallocDebug gMallocUse __attribute__((aligned(32))) = {
256 dlmalloc, dlfree, dlcalloc, dlrealloc, dlmemalign
257};
258
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700259extern char* __progname;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800260
261/* Handle to shared library where actual memory allocation is implemented.
262 * This library is loaded and memory allocation calls are redirected there
263 * when libc.debug.malloc environment variable contains value other than
264 * zero:
265 * 1 - For memory leak detections.
266 * 5 - For filling allocated / freed memory with patterns defined by
267 * CHK_SENTINEL_VALUE, and CHK_FILL_FREE macros.
268 * 10 - For adding pre-, and post- allocation stubs in order to detect
269 * buffer overruns.
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800270 * Note that emulator's memory allocation instrumentation is not controlled by
271 * libc.debug.malloc value, but rather by emulator, started with -memcheck
272 * option. Note also, that if emulator has started with -memcheck option,
273 * emulator's instrumented memory allocation will take over value saved in
274 * libc.debug.malloc. In other words, if emulator has started with -memcheck
275 * option, libc.debug.malloc value is ignored.
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800276 * Actual functionality for debug levels 1-10 is implemented in
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800277 * libc_malloc_debug_leak.so, while functionality for emultor's instrumented
278 * allocations is implemented in libc_malloc_debug_qemu.so and can be run inside
279 * the emulator only.
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800280 */
281static void* libc_malloc_impl_handle = NULL;
282
Elliott Hughesdb492b32013-01-03 15:44:03 -0800283// This must match the alignment used by dlmalloc.
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800284#ifndef MALLOC_ALIGNMENT
Elliott Hughesdb492b32013-01-03 15:44:03 -0800285#define MALLOC_ALIGNMENT ((size_t)(2 * sizeof(void *)))
286#endif
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800287
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700288/* This variable is set to the value of property libc.debug.malloc.backlog,
289 * when the value of libc.debug.malloc = 10. It determines the size of the
290 * backlog we use to detect multiple frees. If the property is not set, the
Elliott Hughes9c818922013-02-01 17:07:40 -0800291 * backlog length defaults to BACKLOG_DEFAULT_LEN.
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700292 */
Elliott Hughes9c818922013-02-01 17:07:40 -0800293unsigned int gMallocDebugBacklog;
294#define BACKLOG_DEFAULT_LEN 100
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700295
Elliott Hughes9c818922013-02-01 17:07:40 -0800296/* The value of libc.debug.malloc. */
297int gMallocDebugLevel;
298
299static void InitMalloc(MallocDebug* table, const char* prefix) {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800300 __libc_format_log(ANDROID_LOG_INFO, "libc", "%s: using libc.debug.malloc %d (%s)\n",
Elliott Hughes9c818922013-02-01 17:07:40 -0800301 __progname, gMallocDebugLevel, prefix);
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700302
303 char symbol[128];
304
305 snprintf(symbol, sizeof(symbol), "%s_malloc", prefix);
306 table->malloc = reinterpret_cast<MallocDebugMalloc>(dlsym(libc_malloc_impl_handle, symbol));
307 if (table->malloc == NULL) {
308 error_log("%s: dlsym(\"%s\") failed", __progname, symbol);
309 }
310
311 snprintf(symbol, sizeof(symbol), "%s_free", prefix);
312 table->free = reinterpret_cast<MallocDebugFree>(dlsym(libc_malloc_impl_handle, symbol));
313 if (table->free == NULL) {
314 error_log("%s: dlsym(\"%s\") failed", __progname, symbol);
315 }
316
317 snprintf(symbol, sizeof(symbol), "%s_calloc", prefix);
318 table->calloc = reinterpret_cast<MallocDebugCalloc>(dlsym(libc_malloc_impl_handle, symbol));
319 if (table->calloc == NULL) {
320 error_log("%s: dlsym(\"%s\") failed", __progname, symbol);
321 }
322
323 snprintf(symbol, sizeof(symbol), "%s_realloc", prefix);
324 table->realloc = reinterpret_cast<MallocDebugRealloc>(dlsym(libc_malloc_impl_handle, symbol));
325 if (table->realloc == NULL) {
326 error_log("%s: dlsym(\"%s\") failed", __progname, symbol);
327 }
328
329 snprintf(symbol, sizeof(symbol), "%s_memalign", prefix);
330 table->memalign = reinterpret_cast<MallocDebugMemalign>(dlsym(libc_malloc_impl_handle, symbol));
331 if (table->memalign == NULL) {
332 error_log("%s: dlsym(\"%s\") failed", __progname, symbol);
333 }
334}
335
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800336/* Initializes memory allocation framework once per process. */
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700337static void malloc_init_impl() {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800338 const char* so_name = NULL;
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800339 MallocDebugInit malloc_debug_initialize = NULL;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800340 unsigned int qemu_running = 0;
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800341 unsigned int memcheck_enabled = 0;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800342 char env[PROP_VALUE_MAX];
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800343 char memcheck_tracing[PROP_VALUE_MAX];
Iliyan Malchev7d2e24e2012-05-29 16:46:17 -0700344 char debug_program[PROP_VALUE_MAX];
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800345
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800346 /* Get custom malloc debug level. Note that emulator started with
347 * memory checking option will have priority over debug level set in
348 * libc.debug.malloc system property. */
349 if (__system_property_get("ro.kernel.qemu", env) && atoi(env)) {
350 qemu_running = 1;
351 if (__system_property_get("ro.kernel.memcheck", memcheck_tracing)) {
352 if (memcheck_tracing[0] != '0') {
353 // Emulator has started with memory tracing enabled. Enforce it.
Elliott Hughes9c818922013-02-01 17:07:40 -0800354 gMallocDebugLevel = 20;
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800355 memcheck_enabled = 1;
356 }
357 }
358 }
359
360 /* If debug level has not been set by memcheck option in the emulator,
361 * lets grab it from libc.debug.malloc system property. */
Elliott Hughes9c818922013-02-01 17:07:40 -0800362 if (gMallocDebugLevel == 0 && __system_property_get("libc.debug.malloc", env)) {
363 gMallocDebugLevel = atoi(env);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800364 }
365
366 /* Debug level 0 means that we should use dlxxx allocation
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800367 * routines (default). */
Elliott Hughes9c818922013-02-01 17:07:40 -0800368 if (gMallocDebugLevel == 0) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800369 return;
370 }
371
Iliyan Malchev7d2e24e2012-05-29 16:46:17 -0700372 /* If libc.debug.malloc.program is set and is not a substring of progname,
373 * then exit.
374 */
375 if (__system_property_get("libc.debug.malloc.program", debug_program)) {
376 if (!strstr(__progname, debug_program)) {
377 return;
378 }
379 }
380
Elliott Hughes84f8b5f2013-01-22 18:35:14 -0800381 // mksh is way too leaky. http://b/7291287.
Elliott Hughes9c818922013-02-01 17:07:40 -0800382 if (gMallocDebugLevel >= 10) {
Elliott Hughes239e7a02013-01-25 17:13:45 -0800383 if (strcmp(__progname, "sh") == 0 || strcmp(__progname, "/system/bin/sh") == 0) {
384 return;
385 }
Elliott Hughes84f8b5f2013-01-22 18:35:14 -0800386 }
387
Elliott Hughese5d5f7f2012-10-09 17:23:09 -0700388 // Choose the appropriate .so for the requested debug level.
Elliott Hughes9c818922013-02-01 17:07:40 -0800389 switch (gMallocDebugLevel) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800390 case 1:
391 case 5:
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700392 case 10: {
393 char debug_backlog[PROP_VALUE_MAX];
394 if (__system_property_get("libc.debug.malloc.backlog", debug_backlog)) {
Elliott Hughes9c818922013-02-01 17:07:40 -0800395 gMallocDebugBacklog = atoi(debug_backlog);
396 info_log("%s: setting backlog length to %d\n", __progname, gMallocDebugBacklog);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700397 }
Elliott Hughes9c818922013-02-01 17:07:40 -0800398 if (gMallocDebugBacklog == 0) {
399 gMallocDebugBacklog = BACKLOG_DEFAULT_LEN;
400 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800401 so_name = "/system/lib/libc_malloc_debug_leak.so";
402 break;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700403 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800404 case 20:
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800405 // Quick check: debug level 20 can only be handled in emulator.
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800406 if (!qemu_running) {
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800407 error_log("%s: Debug level %d can only be set in emulator\n",
Elliott Hughes9c818922013-02-01 17:07:40 -0800408 __progname, gMallocDebugLevel);
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800409 return;
410 }
411 // Make sure that memory checking has been enabled in emulator.
412 if (!memcheck_enabled) {
413 error_log("%s: Memory checking is not enabled in the emulator\n",
414 __progname);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800415 return;
416 }
417 so_name = "/system/lib/libc_malloc_debug_qemu.so";
418 break;
419 default:
Elliott Hughes9c818922013-02-01 17:07:40 -0800420 error_log("%s: Debug level %d is unknown\n", __progname, gMallocDebugLevel);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800421 return;
422 }
423
424 // Load .so that implements the required malloc debugging functionality.
425 libc_malloc_impl_handle = dlopen(so_name, RTLD_LAZY);
426 if (libc_malloc_impl_handle == NULL) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700427 error_log("%s: Missing module %s required for malloc debug level %d: %s",
Elliott Hughes9c818922013-02-01 17:07:40 -0800428 __progname, so_name, gMallocDebugLevel, dlerror());
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800429 return;
430 }
431
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800432 // Initialize malloc debugging in the loaded module.
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700433 malloc_debug_initialize = reinterpret_cast<MallocDebugInit>(dlsym(libc_malloc_impl_handle,
434 "malloc_debug_initialize"));
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800435 if (malloc_debug_initialize == NULL) {
436 error_log("%s: Initialization routine is not found in %s\n",
437 __progname, so_name);
438 dlclose(libc_malloc_impl_handle);
439 return;
440 }
Elliott Hughes1e980b62013-01-17 18:36:06 -0800441 if (malloc_debug_initialize() == -1) {
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800442 dlclose(libc_malloc_impl_handle);
443 return;
444 }
445
Elliott Hughes9c818922013-02-01 17:07:40 -0800446 if (gMallocDebugLevel == 20) {
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800447 // For memory checker we need to do extra initialization.
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700448 typedef int (*MemCheckInit)(int, const char*);
449 MemCheckInit memcheck_initialize =
450 reinterpret_cast<MemCheckInit>(dlsym(libc_malloc_impl_handle,
451 "memcheck_initialize"));
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800452 if (memcheck_initialize == NULL) {
453 error_log("%s: memcheck_initialize routine is not found in %s\n",
454 __progname, so_name);
455 dlclose(libc_malloc_impl_handle);
456 return;
457 }
Elliott Hughesdb492b32013-01-03 15:44:03 -0800458
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800459 if (memcheck_initialize(MALLOC_ALIGNMENT, memcheck_tracing)) {
460 dlclose(libc_malloc_impl_handle);
461 return;
462 }
463 }
464
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800465 // Initialize malloc dispatch table with appropriate routines.
Elliott Hughes9c818922013-02-01 17:07:40 -0800466 switch (gMallocDebugLevel) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800467 case 1:
Elliott Hughes9c818922013-02-01 17:07:40 -0800468 InitMalloc(&gMallocUse, "leak");
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800469 break;
470 case 5:
Elliott Hughes9c818922013-02-01 17:07:40 -0800471 InitMalloc(&gMallocUse, "fill");
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800472 break;
473 case 10:
Elliott Hughes9c818922013-02-01 17:07:40 -0800474 InitMalloc(&gMallocUse, "chk");
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800475 break;
476 case 20:
Elliott Hughes9c818922013-02-01 17:07:40 -0800477 InitMalloc(&gMallocUse, "qemu_instrumented");
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800478 break;
479 default:
480 break;
481 }
482
483 // Make sure dispatch table is initialized
484 if ((gMallocUse.malloc == NULL) ||
485 (gMallocUse.free == NULL) ||
486 (gMallocUse.calloc == NULL) ||
487 (gMallocUse.realloc == NULL) ||
488 (gMallocUse.memalign == NULL)) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700489 error_log("%s: some symbols for libc.debug.malloc level %d were not found (see above)",
Elliott Hughes9c818922013-02-01 17:07:40 -0800490 __progname, gMallocDebugLevel);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800491 dlclose(libc_malloc_impl_handle);
492 libc_malloc_impl_handle = NULL;
493 } else {
494 __libc_malloc_dispatch = &gMallocUse;
495 }
496}
497
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700498static void malloc_fini_impl() {
Elliott Hughes1e980b62013-01-17 18:36:06 -0800499 // Our BSD stdio implementation doesn't close the standard streams, it only flushes them.
500 // And it doesn't do that until its atexit handler (_cleanup) is run, and we run first!
501 // It's great that other unclosed FILE*s show up as malloc leaks, but we need to manually
502 // clean up the standard streams ourselves.
503 fclose(stdin);
504 fclose(stdout);
505 fclose(stderr);
506
507 if (libc_malloc_impl_handle != NULL) {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700508 MallocDebugFini malloc_debug_finalize =
509 reinterpret_cast<MallocDebugFini>(dlsym(libc_malloc_impl_handle,
510 "malloc_debug_finalize"));
Elliott Hughes1e980b62013-01-17 18:36:06 -0800511 if (malloc_debug_finalize != NULL) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700512 malloc_debug_finalize();
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700513 }
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700514 }
515}
516
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800517static pthread_once_t malloc_init_once_ctl = PTHREAD_ONCE_INIT;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700518static pthread_once_t malloc_fini_once_ctl = PTHREAD_ONCE_INIT;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800519
520#endif // !LIBC_STATIC
521#endif // USE_DL_PREFIX
522
523/* Initializes memory allocation framework.
524 * This routine is called from __libc_init routines implemented
525 * in libc_init_static.c and libc_init_dynamic.c files.
526 */
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700527extern "C" void malloc_debug_init() {
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800528 /* We need to initialize malloc iff we implement here custom
529 * malloc routines (i.e. USE_DL_PREFIX is defined) for libc.so */
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800530#if defined(USE_DL_PREFIX) && !defined(LIBC_STATIC)
Elliott Hughesdb492b32013-01-03 15:44:03 -0800531 if (pthread_once(&malloc_init_once_ctl, malloc_init_impl)) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800532 error_log("Unable to initialize malloc_debug component.");
533 }
534#endif // USE_DL_PREFIX && !LIBC_STATIC
535}
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700536
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700537extern "C" void malloc_debug_fini() {
Elliott Hughesdb492b32013-01-03 15:44:03 -0800538 /* We need to finalize malloc iff we implement here custom
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700539 * malloc routines (i.e. USE_DL_PREFIX is defined) for libc.so */
540#if defined(USE_DL_PREFIX) && !defined(LIBC_STATIC)
Elliott Hughesdb492b32013-01-03 15:44:03 -0800541 if (pthread_once(&malloc_fini_once_ctl, malloc_fini_impl)) {
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700542 error_log("Unable to finalize malloc_debug component.");
543 }
544#endif // USE_DL_PREFIX && !LIBC_STATIC
545}