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 36 #define HASHTABLE_SIZE 1543 37 #define BACKTRACE_SIZE 32 38 /* flag definitions, currently sharing storage with "size" */ 39 #define SIZE_FLAG_ZYGOTE_CHILD (1<<31) 40 #define SIZE_FLAG_MASK (SIZE_FLAG_ZYGOTE_CHILD) 41 42 #define MAX_SIZE_T (~(size_t)0) 43 44 // ============================================================================= 45 // Structures 46 // ============================================================================= 47 48 struct HashEntry { 49 size_t slot; 50 HashEntry* prev; 51 HashEntry* next; 52 size_t numEntries; 53 // fields above "size" are NOT sent to the host 54 size_t size; 55 size_t allocations; 56 intptr_t backtrace[0]; 57 }; 58 59 struct HashTable { 60 size_t count; 61 HashEntry* slots[HASHTABLE_SIZE]; 62 }; 63 64 /* Entry in malloc dispatch table. */ 65 typedef void* (*MallocDebugMalloc)(size_t); 66 typedef void (*MallocDebugFree)(void*); 67 typedef void* (*MallocDebugCalloc)(size_t, size_t); 68 typedef void* (*MallocDebugRealloc)(void*, size_t); 69 typedef void* (*MallocDebugMemalign)(size_t, size_t); 70 struct MallocDebug { 71 MallocDebugMalloc malloc; 72 MallocDebugFree free; 73 MallocDebugCalloc calloc; 74 MallocDebugRealloc realloc; 75 MallocDebugMemalign memalign; 76 }; 77 78 /* Malloc debugging initialization and finalization routines. 79 * 80 * These routines must be implemented in .so modules that implement malloc 81 * debugging. The are is called once per process from malloc_init_impl and 82 * malloc_fini_impl respectively. 83 * 84 * They are implemented in bionic/libc/bionic/malloc_debug_common.c when malloc 85 * debugging gets initialized for the process. 86 * 87 * MallocDebugInit returns: 88 * 0 on success, -1 on failure. 89 */ 90 typedef int (*MallocDebugInit)(); 91 typedef void (*MallocDebugFini)(); 92 93 // ============================================================================= 94 // log functions 95 // ============================================================================= 96 97 #define debug_log(format, ...) \ 98 __libc_android_log_print(ANDROID_LOG_DEBUG, "malloc_leak_check", (format), ##__VA_ARGS__ ) 99 #define error_log(format, ...) \ 100 __libc_android_log_print(ANDROID_LOG_ERROR, "malloc_leak_check", (format), ##__VA_ARGS__ ) 101 #define info_log(format, ...) \ 102 __libc_android_log_print(ANDROID_LOG_INFO, "malloc_leak_check", (format), ##__VA_ARGS__ ) 103 104 class ScopedPthreadMutexLocker { 105 public: ScopedPthreadMutexLocker(pthread_mutex_t * mu)106 explicit ScopedPthreadMutexLocker(pthread_mutex_t* mu) : mu_(mu) { 107 pthread_mutex_lock(mu_); 108 } 109 ~ScopedPthreadMutexLocker()110 ~ScopedPthreadMutexLocker() { 111 pthread_mutex_unlock(mu_); 112 } 113 114 private: 115 pthread_mutex_t* mu_; 116 }; 117 118 #endif // MALLOC_DEBUG_COMMON_H 119