blob: 2f4e55b2aad1944cc78d4fffcc58557251d40d9a [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
43#include <stdlib.h>
44#include <pthread.h>
45#include <unistd.h>
46#include "dlmalloc.h"
47#include "malloc_debug_common.h"
48
49/*
50 * In a VM process, this is set to 1 after fork()ing out of zygote.
51 */
52int gMallocLeakZygoteChild = 0;
53
54pthread_mutex_t gAllocationsMutex = PTHREAD_MUTEX_INITIALIZER;
55HashTable gHashTable;
56
57// =============================================================================
58// output functions
59// =============================================================================
60
61static int hash_entry_compare(const void* arg1, const void* arg2)
62{
Christopher Tate52e7d3d2010-08-09 13:43:46 -070063 int result;
64
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080065 HashEntry* e1 = *(HashEntry**)arg1;
66 HashEntry* e2 = *(HashEntry**)arg2;
67
Christopher Tate52e7d3d2010-08-09 13:43:46 -070068 // if one or both arg pointers are null, deal gracefully
69 if (e1 == NULL) {
70 result = (e2 == NULL) ? 0 : 1;
71 } else if (e2 == NULL) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080072 result = -1;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080073 } else {
Christopher Tate52e7d3d2010-08-09 13:43:46 -070074 size_t nbAlloc1 = e1->allocations;
75 size_t nbAlloc2 = e2->allocations;
76 size_t size1 = e1->size & ~SIZE_FLAG_MASK;
77 size_t size2 = e2->size & ~SIZE_FLAG_MASK;
78 size_t alloc1 = nbAlloc1 * size1;
79 size_t alloc2 = nbAlloc2 * size2;
80
81 // sort in descending order by:
82 // 1) total size
83 // 2) number of allocations
84 //
85 // This is used for sorting, not determination of equality, so we don't
86 // need to compare the bit flags.
Christopher Tate52e7d3d2010-08-09 13:43:46 -070087 if (alloc1 > alloc2) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080088 result = -1;
Christopher Tate52e7d3d2010-08-09 13:43:46 -070089 } else if (alloc1 < alloc2) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080090 result = 1;
91 } else {
Christopher Tate52e7d3d2010-08-09 13:43:46 -070092 if (nbAlloc1 > nbAlloc2) {
93 result = -1;
94 } else if (nbAlloc1 < nbAlloc2) {
95 result = 1;
96 } else {
97 result = 0;
98 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080099 }
100 }
101 return result;
102}
103
104/*
105 * Retrieve native heap information.
106 *
107 * "*info" is set to a buffer we allocate
108 * "*overallSize" is set to the size of the "info" buffer
109 * "*infoSize" is set to the size of a single entry
110 * "*totalMemory" is set to the sum of all allocations we're tracking; does
111 * not include heap overhead
112 * "*backtraceSize" is set to the maximum number of entries in the back trace
113 */
114void get_malloc_leak_info(uint8_t** info, size_t* overallSize,
115 size_t* infoSize, size_t* totalMemory, size_t* backtraceSize)
116{
117 // don't do anything if we have invalid arguments
118 if (info == NULL || overallSize == NULL || infoSize == NULL ||
119 totalMemory == NULL || backtraceSize == NULL) {
120 return;
121 }
tedbo9d8be542010-10-05 13:06:06 -0700122 *totalMemory = 0;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800123
124 pthread_mutex_lock(&gAllocationsMutex);
125
126 if (gHashTable.count == 0) {
127 *info = NULL;
128 *overallSize = 0;
129 *infoSize = 0;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800130 *backtraceSize = 0;
131 goto done;
132 }
133
134 void** list = (void**)dlmalloc(sizeof(void*) * gHashTable.count);
135
136 // get the entries into an array to be sorted
137 int index = 0;
138 int i;
139 for (i = 0 ; i < HASHTABLE_SIZE ; i++) {
140 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)
151 *infoSize = (sizeof(size_t) * 2) + (sizeof(intptr_t) * BACKTRACE_SIZE);
152 *overallSize = *infoSize * gHashTable.count;
153 *backtraceSize = BACKTRACE_SIZE;
154
155 // now get A byte array big enough for this
156 *info = (uint8_t*)dlmalloc(*overallSize);
157
158 if (*info == NULL) {
159 *overallSize = 0;
The Android Open Source Project95faece2010-04-08 11:11:53 -0700160 goto out_nomem_info;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800161 }
162
163 qsort((void*)list, gHashTable.count, sizeof(void*), hash_entry_compare);
164
165 uint8_t* head = *info;
166 const int count = gHashTable.count;
167 for (i = 0 ; i < count ; i++) {
168 HashEntry* entry = list[i];
169 size_t entrySize = (sizeof(size_t) * 2) + (sizeof(intptr_t) * entry->numEntries);
170 if (entrySize < *infoSize) {
171 /* we're writing less than a full entry, clear out the rest */
The Android Open Source Project95faece2010-04-08 11:11:53 -0700172 memset(head + entrySize, 0, *infoSize - entrySize);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800173 } else {
174 /* make sure the amount we're copying doesn't exceed the limit */
175 entrySize = *infoSize;
176 }
177 memcpy(head, &(entry->size), entrySize);
178 head += *infoSize;
179 }
180
The Android Open Source Project95faece2010-04-08 11:11:53 -0700181out_nomem_info:
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800182 dlfree(list);
183
184done:
185 pthread_mutex_unlock(&gAllocationsMutex);
186}
187
188void free_malloc_leak_info(uint8_t* info)
189{
190 dlfree(info);
191}
192
193struct mallinfo mallinfo()
194{
195 return dlmallinfo();
196}
197
Ian Rogers99908912012-08-17 17:28:15 -0700198size_t malloc_usable_size(void* mem)
199{
200 return dlmalloc_usable_size(mem);
201}
202
203void* valloc(size_t bytes)
204{
205 return dlvalloc(bytes);
206}
207
208void* pvalloc(size_t bytes)
209{
210 return dlpvalloc(bytes);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800211}
212
Brian Carlstrombfc1d972012-08-20 18:28:20 -0700213int posix_memalign(void** memptr, size_t alignment, size_t size)
214{
215 return dlposix_memalign(memptr, alignment, size);
216}
217
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800218/* Support for malloc debugging.
219 * Note that if USE_DL_PREFIX is not defined, it's assumed that memory
220 * allocation routines are implemented somewhere else, so all our custom
221 * malloc routines should not be compiled at all.
222 */
223#ifdef USE_DL_PREFIX
224
225/* Table for dispatching malloc calls, initialized with default dispatchers. */
226const MallocDebug __libc_malloc_default_dispatch __attribute__((aligned(32))) =
227{
228 dlmalloc, dlfree, dlcalloc, dlrealloc, dlmemalign
229};
230
231/* Selector of dispatch table to use for dispatching malloc calls. */
232const MallocDebug* __libc_malloc_dispatch = &__libc_malloc_default_dispatch;
233
234void* malloc(size_t bytes) {
235 return __libc_malloc_dispatch->malloc(bytes);
236}
237void free(void* mem) {
238 __libc_malloc_dispatch->free(mem);
239}
240void* calloc(size_t n_elements, size_t elem_size) {
241 return __libc_malloc_dispatch->calloc(n_elements, elem_size);
242}
243void* realloc(void* oldMem, size_t bytes) {
244 return __libc_malloc_dispatch->realloc(oldMem, bytes);
245}
246void* memalign(size_t alignment, size_t bytes) {
247 return __libc_malloc_dispatch->memalign(alignment, bytes);
248}
249
250/* We implement malloc debugging only in libc.so, so code bellow
251 * must be excluded if we compile this file for static libc.a
252 */
253#ifndef LIBC_STATIC
254#include <sys/system_properties.h>
255#include <dlfcn.h>
256#include "logd.h"
257
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800258/* Table for dispatching malloc calls, depending on environment. */
259static MallocDebug gMallocUse __attribute__((aligned(32))) = {
260 dlmalloc, dlfree, dlcalloc, dlrealloc, dlmemalign
261};
262
263extern char* __progname;
264
265/* Handle to shared library where actual memory allocation is implemented.
266 * This library is loaded and memory allocation calls are redirected there
267 * when libc.debug.malloc environment variable contains value other than
268 * zero:
269 * 1 - For memory leak detections.
270 * 5 - For filling allocated / freed memory with patterns defined by
271 * CHK_SENTINEL_VALUE, and CHK_FILL_FREE macros.
272 * 10 - For adding pre-, and post- allocation stubs in order to detect
273 * buffer overruns.
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800274 * Note that emulator's memory allocation instrumentation is not controlled by
275 * libc.debug.malloc value, but rather by emulator, started with -memcheck
276 * option. Note also, that if emulator has started with -memcheck option,
277 * emulator's instrumented memory allocation will take over value saved in
278 * libc.debug.malloc. In other words, if emulator has started with -memcheck
279 * option, libc.debug.malloc value is ignored.
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800280 * Actual functionality for debug levels 1-10 is implemented in
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800281 * libc_malloc_debug_leak.so, while functionality for emultor's instrumented
282 * allocations is implemented in libc_malloc_debug_qemu.so and can be run inside
283 * the emulator only.
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800284 */
285static void* libc_malloc_impl_handle = NULL;
286
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800287/* Make sure we have MALLOC_ALIGNMENT that matches the one that is
288 * used in dlmalloc. Emulator's memchecker needs this value to properly
289 * align its guarding zones.
290 */
291#ifndef MALLOC_ALIGNMENT
292#define MALLOC_ALIGNMENT ((size_t)8U)
293#endif /* MALLOC_ALIGNMENT */
294
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700295/* This variable is set to the value of property libc.debug.malloc.backlog,
296 * when the value of libc.debug.malloc = 10. It determines the size of the
297 * backlog we use to detect multiple frees. If the property is not set, the
298 * backlog length defaults to an internal constant defined in
299 * malloc_debug_check.c
300 */
301unsigned int malloc_double_free_backlog;
302
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800303/* Initializes memory allocation framework once per process. */
304static void malloc_init_impl(void)
305{
306 const char* so_name = NULL;
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800307 MallocDebugInit malloc_debug_initialize = NULL;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800308 unsigned int qemu_running = 0;
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800309 unsigned int debug_level = 0;
310 unsigned int memcheck_enabled = 0;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800311 char env[PROP_VALUE_MAX];
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800312 char memcheck_tracing[PROP_VALUE_MAX];
Iliyan Malchev7d2e24e2012-05-29 16:46:17 -0700313 char debug_program[PROP_VALUE_MAX];
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800314
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800315 /* Get custom malloc debug level. Note that emulator started with
316 * memory checking option will have priority over debug level set in
317 * libc.debug.malloc system property. */
318 if (__system_property_get("ro.kernel.qemu", env) && atoi(env)) {
319 qemu_running = 1;
320 if (__system_property_get("ro.kernel.memcheck", memcheck_tracing)) {
321 if (memcheck_tracing[0] != '0') {
322 // Emulator has started with memory tracing enabled. Enforce it.
323 debug_level = 20;
324 memcheck_enabled = 1;
325 }
326 }
327 }
328
329 /* If debug level has not been set by memcheck option in the emulator,
330 * lets grab it from libc.debug.malloc system property. */
331 if (!debug_level && __system_property_get("libc.debug.malloc", env)) {
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800332 debug_level = atoi(env);
333 }
334
335 /* Debug level 0 means that we should use dlxxx allocation
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800336 * routines (default). */
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800337 if (!debug_level) {
338 return;
339 }
340
Iliyan Malchev7d2e24e2012-05-29 16:46:17 -0700341 /* If libc.debug.malloc.program is set and is not a substring of progname,
342 * then exit.
343 */
344 if (__system_property_get("libc.debug.malloc.program", debug_program)) {
345 if (!strstr(__progname, debug_program)) {
346 return;
347 }
348 }
349
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800350 // Lets see which .so must be loaded for the requested debug level
351 switch (debug_level) {
352 case 1:
353 case 5:
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700354 case 10: {
355 char debug_backlog[PROP_VALUE_MAX];
356 if (__system_property_get("libc.debug.malloc.backlog", debug_backlog)) {
357 malloc_double_free_backlog = atoi(debug_backlog);
358 info_log("%s: setting backlog length to %d\n",
359 __progname, malloc_double_free_backlog);
360 }
361
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800362 so_name = "/system/lib/libc_malloc_debug_leak.so";
363 break;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700364 }
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800365 case 20:
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800366 // Quick check: debug level 20 can only be handled in emulator.
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800367 if (!qemu_running) {
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800368 error_log("%s: Debug level %d can only be set in emulator\n",
369 __progname, debug_level);
370 return;
371 }
372 // Make sure that memory checking has been enabled in emulator.
373 if (!memcheck_enabled) {
374 error_log("%s: Memory checking is not enabled in the emulator\n",
375 __progname);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800376 return;
377 }
378 so_name = "/system/lib/libc_malloc_debug_qemu.so";
379 break;
380 default:
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800381 error_log("%s: Debug level %d is unknown\n",
382 __progname, debug_level);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800383 return;
384 }
385
386 // Load .so that implements the required malloc debugging functionality.
387 libc_malloc_impl_handle = dlopen(so_name, RTLD_LAZY);
388 if (libc_malloc_impl_handle == NULL) {
389 error_log("%s: Missing module %s required for malloc debug level %d\n",
390 __progname, so_name, debug_level);
391 return;
392 }
393
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800394 // Initialize malloc debugging in the loaded module.
395 malloc_debug_initialize =
396 dlsym(libc_malloc_impl_handle, "malloc_debug_initialize");
397 if (malloc_debug_initialize == NULL) {
398 error_log("%s: Initialization routine is not found in %s\n",
399 __progname, so_name);
400 dlclose(libc_malloc_impl_handle);
401 return;
402 }
403 if (malloc_debug_initialize()) {
404 dlclose(libc_malloc_impl_handle);
405 return;
406 }
407
408 if (debug_level == 20) {
409 // For memory checker we need to do extra initialization.
410 int (*memcheck_initialize)(int, const char*) =
411 dlsym(libc_malloc_impl_handle, "memcheck_initialize");
412 if (memcheck_initialize == NULL) {
413 error_log("%s: memcheck_initialize routine is not found in %s\n",
414 __progname, so_name);
415 dlclose(libc_malloc_impl_handle);
416 return;
417 }
418 if (memcheck_initialize(MALLOC_ALIGNMENT, memcheck_tracing)) {
419 dlclose(libc_malloc_impl_handle);
420 return;
421 }
422 }
423
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800424 // Initialize malloc dispatch table with appropriate routines.
425 switch (debug_level) {
426 case 1:
427 __libc_android_log_print(ANDROID_LOG_INFO, "libc",
428 "%s using MALLOC_DEBUG = %d (leak checker)\n",
429 __progname, debug_level);
430 gMallocUse.malloc =
431 dlsym(libc_malloc_impl_handle, "leak_malloc");
432 gMallocUse.free =
433 dlsym(libc_malloc_impl_handle, "leak_free");
434 gMallocUse.calloc =
435 dlsym(libc_malloc_impl_handle, "leak_calloc");
436 gMallocUse.realloc =
437 dlsym(libc_malloc_impl_handle, "leak_realloc");
438 gMallocUse.memalign =
439 dlsym(libc_malloc_impl_handle, "leak_memalign");
440 break;
441 case 5:
442 __libc_android_log_print(ANDROID_LOG_INFO, "libc",
443 "%s using MALLOC_DEBUG = %d (fill)\n",
444 __progname, debug_level);
445 gMallocUse.malloc =
446 dlsym(libc_malloc_impl_handle, "fill_malloc");
447 gMallocUse.free =
448 dlsym(libc_malloc_impl_handle, "fill_free");
449 gMallocUse.calloc = dlcalloc;
450 gMallocUse.realloc =
451 dlsym(libc_malloc_impl_handle, "fill_realloc");
452 gMallocUse.memalign =
453 dlsym(libc_malloc_impl_handle, "fill_memalign");
454 break;
455 case 10:
456 __libc_android_log_print(ANDROID_LOG_INFO, "libc",
457 "%s using MALLOC_DEBUG = %d (sentinels, fill)\n",
458 __progname, debug_level);
459 gMallocUse.malloc =
460 dlsym(libc_malloc_impl_handle, "chk_malloc");
461 gMallocUse.free =
462 dlsym(libc_malloc_impl_handle, "chk_free");
463 gMallocUse.calloc =
464 dlsym(libc_malloc_impl_handle, "chk_calloc");
465 gMallocUse.realloc =
466 dlsym(libc_malloc_impl_handle, "chk_realloc");
467 gMallocUse.memalign =
468 dlsym(libc_malloc_impl_handle, "chk_memalign");
469 break;
470 case 20:
471 __libc_android_log_print(ANDROID_LOG_INFO, "libc",
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800472 "%s[%u] using MALLOC_DEBUG = %d (instrumented for emulator)\n",
473 __progname, getpid(), debug_level);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800474 gMallocUse.malloc =
475 dlsym(libc_malloc_impl_handle, "qemu_instrumented_malloc");
476 gMallocUse.free =
477 dlsym(libc_malloc_impl_handle, "qemu_instrumented_free");
478 gMallocUse.calloc =
479 dlsym(libc_malloc_impl_handle, "qemu_instrumented_calloc");
480 gMallocUse.realloc =
481 dlsym(libc_malloc_impl_handle, "qemu_instrumented_realloc");
482 gMallocUse.memalign =
483 dlsym(libc_malloc_impl_handle, "qemu_instrumented_memalign");
484 break;
485 default:
486 break;
487 }
488
489 // Make sure dispatch table is initialized
490 if ((gMallocUse.malloc == NULL) ||
491 (gMallocUse.free == NULL) ||
492 (gMallocUse.calloc == NULL) ||
493 (gMallocUse.realloc == NULL) ||
494 (gMallocUse.memalign == NULL)) {
495 error_log("%s: Cannot initialize malloc dispatch table for debug level"
496 " %d: %p, %p, %p, %p, %p\n",
497 __progname, debug_level,
498 gMallocUse.malloc, gMallocUse.free,
499 gMallocUse.calloc, gMallocUse.realloc,
500 gMallocUse.memalign);
501 dlclose(libc_malloc_impl_handle);
502 libc_malloc_impl_handle = NULL;
503 } else {
504 __libc_malloc_dispatch = &gMallocUse;
505 }
506}
507
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700508static void malloc_fini_impl(void)
509{
510 if (libc_malloc_impl_handle) {
511 MallocDebugFini malloc_debug_finalize = NULL;
512 malloc_debug_finalize =
513 dlsym(libc_malloc_impl_handle, "malloc_debug_finalize");
514 if (malloc_debug_finalize)
515 malloc_debug_finalize();
516 }
517}
518
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800519static pthread_once_t malloc_init_once_ctl = PTHREAD_ONCE_INIT;
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700520static pthread_once_t malloc_fini_once_ctl = PTHREAD_ONCE_INIT;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800521
522#endif // !LIBC_STATIC
523#endif // USE_DL_PREFIX
524
525/* Initializes memory allocation framework.
526 * This routine is called from __libc_init routines implemented
527 * in libc_init_static.c and libc_init_dynamic.c files.
528 */
529void malloc_debug_init(void)
530{
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800531 /* We need to initialize malloc iff we implement here custom
532 * malloc routines (i.e. USE_DL_PREFIX is defined) for libc.so */
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800533#if defined(USE_DL_PREFIX) && !defined(LIBC_STATIC)
534 if (pthread_once(&malloc_init_once_ctl, malloc_init_impl)) {
535 error_log("Unable to initialize malloc_debug component.");
536 }
537#endif // USE_DL_PREFIX && !LIBC_STATIC
538}
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700539
540void malloc_debug_fini(void)
541{
542 /* We need to finalize malloc iff we implement here custom
543 * malloc routines (i.e. USE_DL_PREFIX is defined) for libc.so */
544#if defined(USE_DL_PREFIX) && !defined(LIBC_STATIC)
545 if (pthread_once(&malloc_fini_once_ctl, malloc_fini_impl)) {
546 error_log("Unable to finalize malloc_debug component.");
547 }
548#endif // USE_DL_PREFIX && !LIBC_STATIC
549}