Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 1 | /* |
| 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 Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 36 | #include <stdlib.h> |
| 37 | |
Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame^] | 38 | #include "private/libc_logging.h" |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 39 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 40 | #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 Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 48 | // 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 Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 53 | // ============================================================================= |
| 54 | // Structures |
| 55 | // ============================================================================= |
| 56 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 57 | struct 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 Hughes | 239e7a0 | 2013-01-25 17:13:45 -0800 | [diff] [blame] | 65 | uintptr_t backtrace[0]; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 66 | }; |
| 67 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 68 | struct HashTable { |
| 69 | size_t count; |
| 70 | HashEntry* slots[HASHTABLE_SIZE]; |
| 71 | }; |
| 72 | |
| 73 | /* Entry in malloc dispatch table. */ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 74 | typedef void* (*MallocDebugMalloc)(size_t); |
| 75 | typedef void (*MallocDebugFree)(void*); |
| 76 | typedef void* (*MallocDebugCalloc)(size_t, size_t); |
| 77 | typedef void* (*MallocDebugRealloc)(void*, size_t); |
| 78 | typedef void* (*MallocDebugMemalign)(size_t, size_t); |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 79 | typedef size_t (*MallocDebugMallocUsableSize)(const void*); |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 80 | struct MallocDebug { |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 81 | MallocDebugMalloc malloc; |
| 82 | MallocDebugFree free; |
| 83 | MallocDebugCalloc calloc; |
| 84 | MallocDebugRealloc realloc; |
| 85 | MallocDebugMemalign memalign; |
Christopher Ferris | 885f3b9 | 2013-05-21 17:48:01 -0700 | [diff] [blame] | 86 | MallocDebugMallocUsableSize malloc_usable_size; |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 89 | /* 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 Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 96 | * debugging gets initialized for the process. |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 97 | * |
| 98 | * MallocDebugInit returns: |
| 99 | * 0 on success, -1 on failure. |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 100 | */ |
Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 101 | typedef int (*MallocDebugInit)(); |
| 102 | typedef void (*MallocDebugFini)(); |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 103 | |
| 104 | // ============================================================================= |
| 105 | // log functions |
| 106 | // ============================================================================= |
| 107 | |
| 108 | #define debug_log(format, ...) \ |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 109 | __libc_format_log(ANDROID_LOG_DEBUG, "malloc_leak_check", (format), ##__VA_ARGS__ ) |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 110 | #define error_log(format, ...) \ |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 111 | __libc_format_log(ANDROID_LOG_ERROR, "malloc_leak_check", (format), ##__VA_ARGS__ ) |
Iliyan Malchev | e1dd3c2 | 2012-05-29 14:22:42 -0700 | [diff] [blame] | 112 | #define info_log(format, ...) \ |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 113 | __libc_format_log(ANDROID_LOG_INFO, "malloc_leak_check", (format), ##__VA_ARGS__ ) |
Vladimir Chtchetkine | 75fba68 | 2010-02-12 08:59:58 -0800 | [diff] [blame] | 114 | |
Vladimir Chtchetkine | b74ceb2 | 2009-11-17 14:13:38 -0800 | [diff] [blame] | 115 | #endif // MALLOC_DEBUG_COMMON_H |