blob: fb2f03d0676ee85bfc174240a662bab109aefc7d [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/*
30 * Contains declarations of types and constants used by malloc leak
31 * detection code in both, libc and libc_malloc_debug libraries.
32 */
33#ifndef MALLOC_DEBUG_COMMON_H
34#define MALLOC_DEBUG_COMMON_H
35
Elliott Hughes8e52e8f2014-06-04 12:07:11 -070036#include <pthread.h>
37#include <stdint.h>
Elliott Hughes3b297c42012-10-11 16:08:51 -070038#include <stdlib.h>
39
Christopher Ferris63619642014-06-16 23:35:53 -070040#include "private/bionic_config.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070041#include "private/libc_logging.h"
Elliott Hughes1e980b62013-01-17 18:36:06 -080042
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080043#define HASHTABLE_SIZE 1543
44#define BACKTRACE_SIZE 32
45/* flag definitions, currently sharing storage with "size" */
46#define SIZE_FLAG_ZYGOTE_CHILD (1<<31)
47#define SIZE_FLAG_MASK (SIZE_FLAG_ZYGOTE_CHILD)
48
Christopher Ferris72bbd422014-05-08 11:14:03 -070049// This must match the alignment used by the malloc implementation.
Christopher Ferris885f3b92013-05-21 17:48:01 -070050#ifndef MALLOC_ALIGNMENT
51#define MALLOC_ALIGNMENT ((size_t)(2 * sizeof(void *)))
52#endif
53
Christopher Ferris72bbd422014-05-08 11:14:03 -070054#ifdef USE_JEMALLOC
55#include "jemalloc.h"
56#define Malloc(function) je_ ## function
57#else
58#ifndef USE_DLMALLOC
59#error "Either one of USE_DLMALLOC or USE_JEMALLOC must be defined."
60#endif
61#include "dlmalloc.h"
62#define Malloc(function) dl ## function
63#endif
64
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080065// =============================================================================
66// Structures
67// =============================================================================
68
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080069struct HashEntry {
70 size_t slot;
71 HashEntry* prev;
72 HashEntry* next;
73 size_t numEntries;
74 // fields above "size" are NOT sent to the host
75 size_t size;
76 size_t allocations;
Elliott Hughes239e7a02013-01-25 17:13:45 -080077 uintptr_t backtrace[0];
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080078};
79
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080080struct HashTable {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -070081 pthread_mutex_t lock;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080082 size_t count;
83 HashEntry* slots[HASHTABLE_SIZE];
84};
85
86/* Entry in malloc dispatch table. */
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070087typedef void* (*MallocDebugCalloc)(size_t, size_t);
Christopher Ferrisa4037802014-06-09 19:14:11 -070088typedef void (*MallocDebugFree)(void*);
89typedef struct mallinfo (*MallocDebugMallinfo)();
90typedef void* (*MallocDebugMalloc)(size_t);
Christopher Ferris885f3b92013-05-21 17:48:01 -070091typedef size_t (*MallocDebugMallocUsableSize)(const void*);
Christopher Ferrisa4037802014-06-09 19:14:11 -070092typedef void* (*MallocDebugMemalign)(size_t, size_t);
93typedef int (*MallocDebugPosixMemalign)(void**, size_t, size_t);
Dan Alberte5fdaa42014-06-14 01:04:31 +000094#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisa4037802014-06-09 19:14:11 -070095typedef void* (*MallocDebugPvalloc)(size_t);
Dan Alberte5fdaa42014-06-14 01:04:31 +000096#endif
Christopher Ferrisa4037802014-06-09 19:14:11 -070097typedef void* (*MallocDebugRealloc)(void*, size_t);
Dan Alberte5fdaa42014-06-14 01:04:31 +000098#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisa4037802014-06-09 19:14:11 -070099typedef void* (*MallocDebugValloc)(size_t);
Dan Alberte5fdaa42014-06-14 01:04:31 +0000100#endif
101
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800102struct MallocDebug {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700103 MallocDebugCalloc calloc;
Christopher Ferrisa4037802014-06-09 19:14:11 -0700104 MallocDebugFree free;
105 MallocDebugMallinfo mallinfo;
106 MallocDebugMalloc malloc;
Christopher Ferris885f3b92013-05-21 17:48:01 -0700107 MallocDebugMallocUsableSize malloc_usable_size;
Christopher Ferrisa4037802014-06-09 19:14:11 -0700108 MallocDebugMemalign memalign;
109 MallocDebugPosixMemalign posix_memalign;
Dan Alberte5fdaa42014-06-14 01:04:31 +0000110#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisa4037802014-06-09 19:14:11 -0700111 MallocDebugPvalloc pvalloc;
Dan Alberte5fdaa42014-06-14 01:04:31 +0000112#endif
Christopher Ferrisa4037802014-06-09 19:14:11 -0700113 MallocDebugRealloc realloc;
Dan Alberte5fdaa42014-06-14 01:04:31 +0000114#if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
Christopher Ferrisa4037802014-06-09 19:14:11 -0700115 MallocDebugValloc valloc;
Dan Alberte5fdaa42014-06-14 01:04:31 +0000116#endif
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800117};
118
Elliott Hughes8e52e8f2014-06-04 12:07:11 -0700119typedef bool (*MallocDebugInit)(HashTable*);
120typedef void (*MallocDebugFini)(int);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700121
122// =============================================================================
123// log functions
124// =============================================================================
125
126#define debug_log(format, ...) \
Elliott Hughes1e980b62013-01-17 18:36:06 -0800127 __libc_format_log(ANDROID_LOG_DEBUG, "malloc_leak_check", (format), ##__VA_ARGS__ )
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700128#define error_log(format, ...) \
Elliott Hughes1e980b62013-01-17 18:36:06 -0800129 __libc_format_log(ANDROID_LOG_ERROR, "malloc_leak_check", (format), ##__VA_ARGS__ )
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700130#define info_log(format, ...) \
Elliott Hughes1e980b62013-01-17 18:36:06 -0800131 __libc_format_log(ANDROID_LOG_INFO, "malloc_leak_check", (format), ##__VA_ARGS__ )
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800132
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800133#endif // MALLOC_DEBUG_COMMON_H