1 /*
2  * Copyright (C) 2015 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 #ifndef MALLOC_DEBUG_H
30 #define MALLOC_DEBUG_H
31 
32 #include <stdint.h>
33 
34 #include <private/bionic_malloc_dispatch.h>
35 
36 // Allocations that require a header include a variable length header.
37 // This is the order that data structures will be found. If an optional
38 // part of the header does not exist, the other parts of the header
39 // will still be in this order.
40 //   Header          (Required)
41 //   BacktraceHeader (Optional: For the allocation backtrace)
42 //   uint8_t data    (Optional: Front guard, will be a multiple of MINIMUM_ALIGNMENT_BYTES)
43 //   allocation data
44 //   uint8_t data    (Optional: End guard)
45 //
46 // If backtracing is enabled, then both BacktraceHeaders will be present.
47 //
48 // In the initialization function, offsets into the header will be set
49 // for each different header location. The offsets are always from the
50 // beginning of the Header section.
51 struct Header {
52   uint32_t tag;
53   void* orig_pointer;
54   size_t size;
55   size_t usable_size;
real_sizeHeader56   size_t real_size() const { return size & ~(1U << 31); }
set_zygoteHeader57   void set_zygote() { size |= 1U << 31; }
max_sizeHeader58   static size_t max_size() { return (1U << 31) - 1; }
59 } __attribute__((packed));
60 
61 struct BacktraceHeader {
62   size_t num_frames;
63   uintptr_t frames[0];
64 } __attribute__((packed));
65 
66 constexpr uint32_t DEBUG_TAG = 0x1ee7d00d;
67 constexpr uint32_t DEBUG_FREE_TAG = 0x1cc7dccd;
68 constexpr char LOG_DIVIDER[] = "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***";
69 constexpr size_t FREE_TRACK_MEM_BUFFER_SIZE = 4096;
70 
71 extern const MallocDispatch* g_dispatch;
72 
73 #endif // MALLOC_DEBUG_H
74