blob: 6955c9a03b6675827b3582e2a65a495ea64e947e [file] [log] [blame]
Christopher Ferris4da25032018-03-07 13:38:48 -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#pragma once
30
31#include <stdint.h>
32#include <stdio.h>
33
34#include <atomic>
35#include <deque>
36#include <mutex>
37#include <string>
38#include <unordered_map>
39#include <vector>
40
41#include <private/bionic_macros.h>
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070042#include <unwindstack/LocalUnwinder.h>
Christopher Ferris4da25032018-03-07 13:38:48 -080043
44#include "OptionData.h"
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070045#include "UnwindBacktrace.h"
Christopher Ferris4da25032018-03-07 13:38:48 -080046
47extern int* g_malloc_zygote_child;
48
49// Forward declarations.
50class Config;
51
52struct FrameKeyType {
53 size_t num_frames;
54 uintptr_t* frames;
55
56 bool operator==(const FrameKeyType& comp) const {
57 if (num_frames != comp.num_frames) return false;
58 for (size_t i = 0; i < num_frames; i++) {
59 if (frames[i] != comp.frames[i]) {
60 return false;
61 }
62 }
63 return true;
64 }
65};
66
67namespace std {
68template <>
69struct hash<FrameKeyType> {
70 std::size_t operator()(const FrameKeyType& key) const {
71 std::size_t cur_hash = key.frames[0];
72 // Limit the number of frames to speed up hashing.
73 size_t max_frames = (key.num_frames > 5) ? 5 : key.num_frames;
74 for (size_t i = 1; i < max_frames; i++) {
75 cur_hash ^= key.frames[i];
76 }
77 return cur_hash;
78 }
79};
80}; // namespace std
81
82struct FrameInfoType {
83 size_t references = 0;
84 std::vector<uintptr_t> frames;
85};
86
87struct PointerInfoType {
88 size_t size;
89 size_t hash_index;
90 size_t RealSize() const { return size & ~(1U << 31); }
91 bool ZygoteChildAlloc() const { return size & (1U << 31); }
92 static size_t GetEncodedSize(size_t size) { return GetEncodedSize(*g_malloc_zygote_child, size); }
93 static size_t GetEncodedSize(bool child_alloc, size_t size) {
94 return size | ((child_alloc) ? (1U << 31) : 0);
95 }
96 static size_t MaxSize() { return (1U << 31) - 1; }
97};
98
99struct FreePointerInfoType {
100 uintptr_t pointer;
101 size_t hash_index;
102};
103
104struct ListInfoType {
105 uintptr_t pointer;
106 size_t num_allocations;
107 size_t size;
108 bool zygote_child_alloc;
109 FrameInfoType* frame_info;
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700110 std::vector<unwindstack::LocalFrameData>* backtrace_info;
Christopher Ferris4da25032018-03-07 13:38:48 -0800111};
112
113class PointerData : public OptionData {
114 public:
Chih-Hung Hsieh770032d2019-01-02 10:59:48 -0800115 explicit PointerData(DebugData* debug_data);
Christopher Ferris4da25032018-03-07 13:38:48 -0800116 virtual ~PointerData() = default;
117
118 bool Initialize(const Config& config);
119
120 inline size_t alloc_offset() { return alloc_offset_; }
121
122 bool ShouldBacktrace() { return backtrace_enabled_ == 1; }
123 void ToggleBacktraceEnabled() { backtrace_enabled_.fetch_xor(1); }
124
125 void EnableDumping() { backtrace_dump_ = true; }
126 bool ShouldDumpAndReset() {
127 bool expected = true;
128 return backtrace_dump_.compare_exchange_strong(expected, false);
129 }
130
131 void PrepareFork();
132 void PostForkParent();
133 void PostForkChild();
134
135 static void GetList(std::vector<ListInfoType>* list, bool only_with_backtrace);
136 static void GetUniqueList(std::vector<ListInfoType>* list, bool only_with_backtrace);
137
138 static size_t AddBacktrace(size_t num_frames);
139 static void RemoveBacktrace(size_t hash_index);
140
141 static void Add(const void* pointer, size_t size);
142 static void Remove(const void* pointer);
143
144 typedef std::unordered_map<uintptr_t, PointerInfoType>::iterator iterator;
145 static iterator begin() { return pointers_.begin(); }
146 static iterator end() { return pointers_.end(); }
147
148 static void* AddFreed(const void* pointer);
149 static void LogFreeError(const FreePointerInfoType& info, size_t usable_size);
150 static void LogFreeBacktrace(const void* ptr);
151 static void VerifyFreedPointer(const FreePointerInfoType& info);
152 static void VerifyAllFreed();
153
154 static void LogLeaks();
155 static void DumpLiveToFile(FILE* fp);
156
157 static void GetInfo(uint8_t** info, size_t* overall_size, size_t* info_size, size_t* total_memory,
158 size_t* backtrace_size);
159
160 static size_t GetFrames(const void* pointer, uintptr_t* frames, size_t max_frames);
161
162 static bool Exists(const void* pointer);
163
164 private:
165 static std::string GetHashString(uintptr_t* frames, size_t num_frames);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700166 static void LogBacktrace(size_t hash_index);
Christopher Ferris4da25032018-03-07 13:38:48 -0800167
168 size_t alloc_offset_ = 0;
169 std::vector<uint8_t> cmp_mem_;
170
171 static std::atomic_uint8_t backtrace_enabled_;
172
173 static std::atomic_bool backtrace_dump_;
174
175 static std::mutex pointer_mutex_;
176 static std::unordered_map<uintptr_t, PointerInfoType> pointers_;
177
178 static std::mutex frame_mutex_;
179 static std::unordered_map<FrameKeyType, size_t> key_to_index_;
180 static std::unordered_map<size_t, FrameInfoType> frames_;
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700181 static std::unordered_map<size_t, std::vector<unwindstack::LocalFrameData>> backtraces_info_;
Christopher Ferris4da25032018-03-07 13:38:48 -0800182 static size_t cur_hash_index_;
183
184 static std::mutex free_pointer_mutex_;
185 static std::deque<FreePointerInfoType> free_pointers_;
186
Elliott Hughes5e62b342018-10-25 11:00:00 -0700187 BIONIC_DISALLOW_COPY_AND_ASSIGN(PointerData);
Christopher Ferris4da25032018-03-07 13:38:48 -0800188};