blob: f8745da7fde1c8b785380af50b9b3273447370a4 [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"
Josh Gao3c8fc2f2015-10-08 14:49:26 -070041#include "private/bionic_malloc_dispatch.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070042#include "private/libc_logging.h"
Elliott Hughes1e980b62013-01-17 18:36:06 -080043
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080044#define HASHTABLE_SIZE 1543
45#define BACKTRACE_SIZE 32
46/* flag definitions, currently sharing storage with "size" */
47#define SIZE_FLAG_ZYGOTE_CHILD (1<<31)
48#define SIZE_FLAG_MASK (SIZE_FLAG_ZYGOTE_CHILD)
49
Christopher Ferris72bbd422014-05-08 11:14:03 -070050// This must match the alignment used by the malloc implementation.
Christopher Ferris885f3b92013-05-21 17:48:01 -070051#ifndef MALLOC_ALIGNMENT
52#define MALLOC_ALIGNMENT ((size_t)(2 * sizeof(void *)))
53#endif
54
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080055// =============================================================================
56// Structures
57// =============================================================================
58
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080059struct HashEntry {
60 size_t slot;
61 HashEntry* prev;
62 HashEntry* next;
63 size_t numEntries;
64 // fields above "size" are NOT sent to the host
65 size_t size;
66 size_t allocations;
Elliott Hughes239e7a02013-01-25 17:13:45 -080067 uintptr_t backtrace[0];
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080068};
69
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080070struct HashTable {
Elliott Hughes8e52e8f2014-06-04 12:07:11 -070071 pthread_mutex_t lock;
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080072 size_t count;
73 HashEntry* slots[HASHTABLE_SIZE];
74};
75
Christopher Ferrisdda1c6c2014-07-09 17:16:07 -070076typedef bool (*MallocDebugInit)(HashTable*, const MallocDebug*);
Elliott Hughes8e52e8f2014-06-04 12:07:11 -070077typedef void (*MallocDebugFini)(int);
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070078
79// =============================================================================
80// log functions
81// =============================================================================
82
83#define debug_log(format, ...) \
Elliott Hughes1e980b62013-01-17 18:36:06 -080084 __libc_format_log(ANDROID_LOG_DEBUG, "malloc_leak_check", (format), ##__VA_ARGS__ )
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070085#define error_log(format, ...) \
Elliott Hughes1e980b62013-01-17 18:36:06 -080086 __libc_format_log(ANDROID_LOG_ERROR, "malloc_leak_check", (format), ##__VA_ARGS__ )
Iliyan Malcheve1dd3c22012-05-29 14:22:42 -070087#define info_log(format, ...) \
Elliott Hughes1e980b62013-01-17 18:36:06 -080088 __libc_format_log(ANDROID_LOG_INFO, "malloc_leak_check", (format), ##__VA_ARGS__ )
Vladimir Chtchetkine75fba682010-02-12 08:59:58 -080089
Vladimir Chtchetkineb74ceb22009-11-17 14:13:38 -080090#endif // MALLOC_DEBUG_COMMON_H