blob: 4a15f77bf994cdc44106f528793982eb451f1d10 [file] [log] [blame]
Christopher Ferris63860cb2015-11-16 17:30:32 -08001/*
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// BacktraceHeader (Optional: For the free backtrace)
43// uint8_t data (Optional: Front guard, will be a multiple of sizeof(uintptr_t))
44// allocation data
45// uint8_t data (Optional: End guard)
46//
47// If backtracing is enabled, then both BacktraceHeaders will be present.
48//
49// In the initialization function, offsets into the header will be set
50// for each different header location. The offsets are always from the
51// beginning of the Header section.
52struct Header {
53 uint32_t tag;
54 void* orig_pointer;
55 size_t size;
56 size_t usable_size;
57 size_t real_size() const { return size & ~(1U << 31); }
58 void set_zygote() { size |= 1U << 31; }
59 static size_t max_size() { return (1U << 31) - 1; }
60} __attribute__((packed));
61
62struct TrackHeader {
63 Header* prev = nullptr;
64 Header* next = nullptr;
65} __attribute__((packed));
66
67struct BacktraceHeader {
68 size_t num_frames;
69 uintptr_t frames[0];
70} __attribute__((packed));
71
72constexpr uint32_t DEBUG_TAG = 0x1ee7d00d;
73constexpr char LOG_DIVIDER[] = "*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***";
74constexpr size_t FREE_TRACK_MEM_BUFFER_SIZE = 4096;
75
76extern const MallocDispatch* g_dispatch;
77
78#endif // MALLOC_DEBUG_H