blob: 3194bab652c8bb65206bbd2f8c775ffdedb177b5 [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>
Christopher Ferrisf78486f2022-05-04 14:08:54 -070036#include <functional>
Christopher Ferris4da25032018-03-07 13:38:48 -080037#include <mutex>
38#include <string>
39#include <unordered_map>
40#include <vector>
41
Josh Gao4956c372019-12-19 16:35:51 -080042#include <platform/bionic/macros.h>
Christopher Ferris459eecb2022-01-07 13:38:10 -080043#include <unwindstack/Unwinder.h>
Christopher Ferris4da25032018-03-07 13:38:48 -080044
45#include "OptionData.h"
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070046#include "UnwindBacktrace.h"
Christopher Ferris4da25032018-03-07 13:38:48 -080047
Christopher Ferris8189e772019-04-09 16:37:23 -070048extern bool* g_zygote_child;
Christopher Ferris4da25032018-03-07 13:38:48 -080049
50// Forward declarations.
51class Config;
52
53struct FrameKeyType {
54 size_t num_frames;
55 uintptr_t* frames;
56
57 bool operator==(const FrameKeyType& comp) const {
58 if (num_frames != comp.num_frames) return false;
59 for (size_t i = 0; i < num_frames; i++) {
60 if (frames[i] != comp.frames[i]) {
61 return false;
62 }
63 }
64 return true;
65 }
66};
67
68namespace std {
69template <>
70struct hash<FrameKeyType> {
71 std::size_t operator()(const FrameKeyType& key) const {
72 std::size_t cur_hash = key.frames[0];
73 // Limit the number of frames to speed up hashing.
74 size_t max_frames = (key.num_frames > 5) ? 5 : key.num_frames;
75 for (size_t i = 1; i < max_frames; i++) {
76 cur_hash ^= key.frames[i];
77 }
78 return cur_hash;
79 }
80};
81}; // namespace std
82
83struct FrameInfoType {
84 size_t references = 0;
85 std::vector<uintptr_t> frames;
86};
87
88struct PointerInfoType {
89 size_t size;
90 size_t hash_index;
91 size_t RealSize() const { return size & ~(1U << 31); }
92 bool ZygoteChildAlloc() const { return size & (1U << 31); }
Christopher Ferris8189e772019-04-09 16:37:23 -070093 static size_t GetEncodedSize(size_t size) {
94 return GetEncodedSize(*g_zygote_child, size);
95 }
Christopher Ferris4da25032018-03-07 13:38:48 -080096 static size_t GetEncodedSize(bool child_alloc, size_t size) {
97 return size | ((child_alloc) ? (1U << 31) : 0);
98 }
99 static size_t MaxSize() { return (1U << 31) - 1; }
100};
101
102struct FreePointerInfoType {
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700103 uintptr_t mangled_ptr;
Christopher Ferris4da25032018-03-07 13:38:48 -0800104 size_t hash_index;
105};
106
107struct ListInfoType {
108 uintptr_t pointer;
109 size_t num_allocations;
110 size_t size;
111 bool zygote_child_alloc;
112 FrameInfoType* frame_info;
Christopher Ferris459eecb2022-01-07 13:38:10 -0800113 std::vector<unwindstack::FrameData>* backtrace_info;
Christopher Ferris4da25032018-03-07 13:38:48 -0800114};
115
116class PointerData : public OptionData {
117 public:
Chih-Hung Hsieh770032d2019-01-02 10:59:48 -0800118 explicit PointerData(DebugData* debug_data);
Christopher Ferris4da25032018-03-07 13:38:48 -0800119 virtual ~PointerData() = default;
120
121 bool Initialize(const Config& config);
122
123 inline size_t alloc_offset() { return alloc_offset_; }
124
125 bool ShouldBacktrace() { return backtrace_enabled_ == 1; }
126 void ToggleBacktraceEnabled() { backtrace_enabled_.fetch_xor(1); }
127
128 void EnableDumping() { backtrace_dump_ = true; }
129 bool ShouldDumpAndReset() {
130 bool expected = true;
131 return backtrace_dump_.compare_exchange_strong(expected, false);
132 }
133
134 void PrepareFork();
135 void PostForkParent();
136 void PostForkChild();
137
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700138 static void IteratePointers(std::function<void(uintptr_t pointer)> fn);
139
Christopher Ferrisa3836482022-05-13 12:09:39 -0700140 static size_t AddBacktrace(size_t num_frames, size_t size_bytes);
Christopher Ferris4da25032018-03-07 13:38:48 -0800141 static void RemoveBacktrace(size_t hash_index);
142
143 static void Add(const void* pointer, size_t size);
144 static void Remove(const void* pointer);
145
Christopher Ferrisa3836482022-05-13 12:09:39 -0700146 static void* AddFreed(const void* pointer, size_t size_bytes);
Christopher Ferris4da25032018-03-07 13:38:48 -0800147 static void LogFreeError(const FreePointerInfoType& info, size_t usable_size);
148 static void LogFreeBacktrace(const void* ptr);
149 static void VerifyFreedPointer(const FreePointerInfoType& info);
150 static void VerifyAllFreed();
151
Christopher Ferris6c619a02019-03-01 17:59:51 -0800152 static void GetAllocList(std::vector<ListInfoType>* list);
Christopher Ferris4da25032018-03-07 13:38:48 -0800153 static void LogLeaks();
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800154 static void DumpLiveToFile(int fd);
Christopher Ferris4da25032018-03-07 13:38:48 -0800155
156 static void GetInfo(uint8_t** info, size_t* overall_size, size_t* info_size, size_t* total_memory,
157 size_t* backtrace_size);
158
159 static size_t GetFrames(const void* pointer, uintptr_t* frames, size_t max_frames);
160
161 static bool Exists(const void* pointer);
162
163 private:
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700164 // Only keep mangled pointers in internal data structures. This avoids
165 // problems where libmemunreachable finds these pointers and thinks they
166 // are not unreachable.
167 static inline uintptr_t ManglePointer(uintptr_t pointer) { return pointer ^ UINTPTR_MAX; }
168 static inline uintptr_t DemanglePointer(uintptr_t pointer) { return pointer ^ UINTPTR_MAX; }
169
Christopher Ferris4da25032018-03-07 13:38:48 -0800170 static std::string GetHashString(uintptr_t* frames, size_t num_frames);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700171 static void LogBacktrace(size_t hash_index);
Christopher Ferris4da25032018-03-07 13:38:48 -0800172
Christopher Ferris6c619a02019-03-01 17:59:51 -0800173 static void GetList(std::vector<ListInfoType>* list, bool only_with_backtrace);
174 static void GetUniqueList(std::vector<ListInfoType>* list, bool only_with_backtrace);
175
Christopher Ferris4da25032018-03-07 13:38:48 -0800176 size_t alloc_offset_ = 0;
177 std::vector<uint8_t> cmp_mem_;
178
179 static std::atomic_uint8_t backtrace_enabled_;
180
181 static std::atomic_bool backtrace_dump_;
182
183 static std::mutex pointer_mutex_;
184 static std::unordered_map<uintptr_t, PointerInfoType> pointers_;
185
186 static std::mutex frame_mutex_;
187 static std::unordered_map<FrameKeyType, size_t> key_to_index_;
188 static std::unordered_map<size_t, FrameInfoType> frames_;
Christopher Ferris459eecb2022-01-07 13:38:10 -0800189 static std::unordered_map<size_t, std::vector<unwindstack::FrameData>> backtraces_info_;
Christopher Ferris4da25032018-03-07 13:38:48 -0800190 static size_t cur_hash_index_;
191
192 static std::mutex free_pointer_mutex_;
193 static std::deque<FreePointerInfoType> free_pointers_;
194
Elliott Hughes5e62b342018-10-25 11:00:00 -0700195 BIONIC_DISALLOW_COPY_AND_ASSIGN(PointerData);
Christopher Ferris4da25032018-03-07 13:38:48 -0800196};