Home | History | Annotate | Download | only in bionic
      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 #include <stdlib.h>
     37 
     38 #include "libc_logging.h"
     39 
     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 
     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 
     53 // =============================================================================
     54 // Structures
     55 // =============================================================================
     56 
     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;
     65     uintptr_t backtrace[0];
     66 };
     67 
     68 struct HashTable {
     69     size_t count;
     70     HashEntry* slots[HASHTABLE_SIZE];
     71 };
     72 
     73 /* Entry in malloc dispatch table. */
     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);
     79 typedef size_t (*MallocDebugMallocUsableSize)(const void*);
     80 struct MallocDebug {
     81   MallocDebugMalloc malloc;
     82   MallocDebugFree free;
     83   MallocDebugCalloc calloc;
     84   MallocDebugRealloc realloc;
     85   MallocDebugMemalign memalign;
     86   MallocDebugMallocUsableSize malloc_usable_size;
     87 };
     88 
     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
     96  * debugging gets initialized for the process.
     97  *
     98  * MallocDebugInit returns:
     99  *    0 on success, -1 on failure.
    100  */
    101 typedef int (*MallocDebugInit)();
    102 typedef void (*MallocDebugFini)();
    103 
    104 // =============================================================================
    105 // log functions
    106 // =============================================================================
    107 
    108 #define debug_log(format, ...)  \
    109     __libc_format_log(ANDROID_LOG_DEBUG, "malloc_leak_check", (format), ##__VA_ARGS__ )
    110 #define error_log(format, ...)  \
    111     __libc_format_log(ANDROID_LOG_ERROR, "malloc_leak_check", (format), ##__VA_ARGS__ )
    112 #define info_log(format, ...)  \
    113     __libc_format_log(ANDROID_LOG_INFO, "malloc_leak_check", (format), ##__VA_ARGS__ )
    114 
    115 #endif  // MALLOC_DEBUG_COMMON_H
    116