blob: 28be0427e69b3b1a7272e5faee3652599adc2332 [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 Hughes3b297c42012-10-11 16:08:51 -070036#include <stdlib.h>
37
Elliott Hugheseb847bc2013-10-09 15:50:50 -070038#include "private/libc_logging.h"
Elliott Hughes1e980b62013-01-17 18:36:06 -080039
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080040#define HASHTABLE_SIZE 1543
41#define BACKTRACE_SIZE 32
42/* flag definitions, currently sharing storage with "size" */
43#define SIZE_FLAG_ZYGOTE_CHILD (1<<31)
44#define SIZE_FLAG_MASK (SIZE_FLAG_ZYGOTE_CHILD)
45
46#define MAX_SIZE_T (~(size_t)0)
47
Christopher Ferris885f3b92013-05-21 17:48:01 -070048// This must match the alignment used by dlmalloc.
49#ifndef MALLOC_ALIGNMENT
50#define MALLOC_ALIGNMENT ((size_t)(2 * sizeof(void *)))
51#endif
52
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080053// =============================================================================
54// Structures
55// =============================================================================
56
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080057struct HashEntry {
58 size_t slot;
59 HashEntry* prev;
60 HashEntry* next;
61 size_t numEntries;
62 // fields above "size" are NOT sent to the host
63 size_t size;
64 size_t allocations;
Elliott Hughes239e7a02013-01-25 17:13:45 -080065 uintptr_t backtrace[0];
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080066};
67
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080068struct HashTable {
69 size_t count;
70 HashEntry* slots[HASHTABLE_SIZE];
71};
72
73/* Entry in malloc dispatch table. */
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070074typedef void* (*MallocDebugMalloc)(size_t);
75typedef void (*MallocDebugFree)(void*);
76typedef void* (*MallocDebugCalloc)(size_t, size_t);
77typedef void* (*MallocDebugRealloc)(void*, size_t);
78typedef void* (*MallocDebugMemalign)(size_t, size_t);
Christopher Ferris885f3b92013-05-21 17:48:01 -070079typedef size_t (*MallocDebugMallocUsableSize)(const void*);
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080080struct MallocDebug {
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070081 MallocDebugMalloc malloc;
82 MallocDebugFree free;
83 MallocDebugCalloc calloc;
84 MallocDebugRealloc realloc;
85 MallocDebugMemalign memalign;
Christopher Ferris885f3b92013-05-21 17:48:01 -070086 MallocDebugMallocUsableSize malloc_usable_size;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080087};
88
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070089/* Malloc debugging initialization and finalization routines.
90 *
91 * These routines must be implemented in .so modules that implement malloc
92 * debugging. The are is called once per process from malloc_init_impl and
93 * malloc_fini_impl respectively.
94 *
95 * They are implemented in bionic/libc/bionic/malloc_debug_common.c when malloc
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -080096 * debugging gets initialized for the process.
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070097 *
98 * MallocDebugInit returns:
99 * 0 on success, -1 on failure.
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800100 */
Elliott Hughesc4d1fec2012-08-28 14:15:04 -0700101typedef int (*MallocDebugInit)();
102typedef void (*MallocDebugFini)();
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700103
104// =============================================================================
105// log functions
106// =============================================================================
107
108#define debug_log(format, ...) \
Elliott Hughes1e980b62013-01-17 18:36:06 -0800109 __libc_format_log(ANDROID_LOG_DEBUG, "malloc_leak_check", (format), ##__VA_ARGS__ )
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700110#define error_log(format, ...) \
Elliott Hughes1e980b62013-01-17 18:36:06 -0800111 __libc_format_log(ANDROID_LOG_ERROR, "malloc_leak_check", (format), ##__VA_ARGS__ )
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -0700112#define info_log(format, ...) \
Elliott Hughes1e980b62013-01-17 18:36:06 -0800113 __libc_format_log(ANDROID_LOG_INFO, "malloc_leak_check", (format), ##__VA_ARGS__ )
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -0800114
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -0800115#endif // MALLOC_DEBUG_COMMON_H