blob: e3a35a687081969f0659fec211e21fe9126b6ac9 [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
Ryan Prichardc2adad12022-12-08 16:52:39 -080029#include <cxxabi.h>
Christopher Ferris4da25032018-03-07 13:38:48 -080030#include <errno.h>
31#include <inttypes.h>
32#include <signal.h>
33#include <stdint.h>
34#include <stdlib.h>
35#include <string.h>
36#include <sys/types.h>
37#include <unistd.h>
38
Christopher Ferrisf78486f2022-05-04 14:08:54 -070039#include <functional>
Christopher Ferris4da25032018-03-07 13:38:48 -080040#include <mutex>
41#include <string>
42#include <unordered_map>
43#include <utility>
44#include <vector>
45
46#include <android-base/stringprintf.h>
47#include <android-base/thread_annotations.h>
Josh Gao4956c372019-12-19 16:35:51 -080048#include <platform/bionic/macros.h>
Christopher Ferris4da25032018-03-07 13:38:48 -080049
50#include "Config.h"
51#include "DebugData.h"
52#include "PointerData.h"
53#include "backtrace.h"
54#include "debug_log.h"
55#include "malloc_debug.h"
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070056#include "UnwindBacktrace.h"
Christopher Ferris4da25032018-03-07 13:38:48 -080057
58std::atomic_uint8_t PointerData::backtrace_enabled_;
59std::atomic_bool PointerData::backtrace_dump_;
60
61std::mutex PointerData::pointer_mutex_;
62std::unordered_map<uintptr_t, PointerInfoType> PointerData::pointers_ GUARDED_BY(
63 PointerData::pointer_mutex_);
64
65std::mutex PointerData::frame_mutex_;
66std::unordered_map<FrameKeyType, size_t> PointerData::key_to_index_ GUARDED_BY(
67 PointerData::frame_mutex_);
68std::unordered_map<size_t, FrameInfoType> PointerData::frames_ GUARDED_BY(PointerData::frame_mutex_);
Christopher Ferris459eecb2022-01-07 13:38:10 -080069std::unordered_map<size_t, std::vector<unwindstack::FrameData>> PointerData::backtraces_info_
70 GUARDED_BY(PointerData::frame_mutex_);
Christopher Ferris4da25032018-03-07 13:38:48 -080071constexpr size_t kBacktraceEmptyIndex = 1;
72size_t PointerData::cur_hash_index_ GUARDED_BY(PointerData::frame_mutex_);
73
74std::mutex PointerData::free_pointer_mutex_;
75std::deque<FreePointerInfoType> PointerData::free_pointers_ GUARDED_BY(
76 PointerData::free_pointer_mutex_);
77
78// Buffer to use for comparison.
79static constexpr size_t kCompareBufferSize = 512 * 1024;
80static std::vector<uint8_t> g_cmp_mem(0);
81
82static void ToggleBacktraceEnable(int, siginfo_t*, void*) {
83 g_debug->pointer->ToggleBacktraceEnabled();
84}
85
86static void EnableDump(int, siginfo_t*, void*) {
87 g_debug->pointer->EnableDumping();
88}
89
90PointerData::PointerData(DebugData* debug_data) : OptionData(debug_data) {}
91
92bool PointerData::Initialize(const Config& config) NO_THREAD_SAFETY_ANALYSIS {
93 pointers_.clear();
94 key_to_index_.clear();
95 frames_.clear();
96 free_pointers_.clear();
97 // A hash index of kBacktraceEmptyIndex indicates that we tried to get
98 // a backtrace, but there was nothing recorded.
99 cur_hash_index_ = kBacktraceEmptyIndex + 1;
100
101 backtrace_enabled_ = config.backtrace_enabled();
102 if (config.backtrace_enable_on_signal()) {
103 struct sigaction64 enable_act = {};
104 enable_act.sa_sigaction = ToggleBacktraceEnable;
105 enable_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
106 if (sigaction64(config.backtrace_signal(), &enable_act, nullptr) != 0) {
107 error_log("Unable to set up backtrace signal enable function: %s", strerror(errno));
108 return false;
109 }
Christopher Ferrisc328e442019-04-01 19:31:26 -0700110 if (config.options() & VERBOSE) {
111 info_log("%s: Run: 'kill -%d %d' to enable backtracing.", getprogname(),
112 config.backtrace_signal(), getpid());
113 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800114 }
115
116 if (config.options() & BACKTRACE) {
117 struct sigaction64 act = {};
118 act.sa_sigaction = EnableDump;
119 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
120 if (sigaction64(config.backtrace_dump_signal(), &act, nullptr) != 0) {
121 error_log("Unable to set up backtrace dump signal function: %s", strerror(errno));
122 return false;
123 }
Christopher Ferrisc328e442019-04-01 19:31:26 -0700124 if (config.options() & VERBOSE) {
125 info_log("%s: Run: 'kill -%d %d' to dump the backtrace.", getprogname(),
126 config.backtrace_dump_signal(), getpid());
127 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800128 }
129
130 backtrace_dump_ = false;
131
132 if (config.options() & FREE_TRACK) {
133 g_cmp_mem.resize(kCompareBufferSize, config.fill_free_value());
134 }
135 return true;
136}
137
Christopher Ferrisa3836482022-05-13 12:09:39 -0700138static inline bool ShouldBacktraceAllocSize(size_t size_bytes) {
139 static bool only_backtrace_specific_sizes =
140 g_debug->config().options() & BACKTRACE_SPECIFIC_SIZES;
141 if (!only_backtrace_specific_sizes) {
142 return true;
143 }
144 static size_t min_size_bytes = g_debug->config().backtrace_min_size_bytes();
145 static size_t max_size_bytes = g_debug->config().backtrace_max_size_bytes();
146 return size_bytes >= min_size_bytes && size_bytes <= max_size_bytes;
147}
148
149size_t PointerData::AddBacktrace(size_t num_frames, size_t size_bytes) {
150 if (!ShouldBacktraceAllocSize(size_bytes)) {
151 return kBacktraceEmptyIndex;
152 }
153
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700154 std::vector<uintptr_t> frames;
Christopher Ferris459eecb2022-01-07 13:38:10 -0800155 std::vector<unwindstack::FrameData> frames_info;
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700156 if (g_debug->config().options() & BACKTRACE_FULL) {
157 if (!Unwind(&frames, &frames_info, num_frames)) {
158 return kBacktraceEmptyIndex;
159 }
160 } else {
161 frames.resize(num_frames);
162 num_frames = backtrace_get(frames.data(), frames.size());
163 if (num_frames == 0) {
164 return kBacktraceEmptyIndex;
165 }
Christopher Ferrisdfbc59a2022-03-23 12:22:36 -0700166 frames.resize(num_frames);
Christopher Ferris4da25032018-03-07 13:38:48 -0800167 }
168
Christopher Ferrisdfbc59a2022-03-23 12:22:36 -0700169 FrameKeyType key{.num_frames = frames.size(), .frames = frames.data()};
Christopher Ferris4da25032018-03-07 13:38:48 -0800170 size_t hash_index;
171 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
172 auto entry = key_to_index_.find(key);
173 if (entry == key_to_index_.end()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800174 hash_index = cur_hash_index_++;
175 key.frames = frames.data();
176 key_to_index_.emplace(key, hash_index);
177
178 frames_.emplace(hash_index, FrameInfoType{.references = 1, .frames = std::move(frames)});
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700179 if (g_debug->config().options() & BACKTRACE_FULL) {
180 backtraces_info_.emplace(hash_index, std::move(frames_info));
181 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800182 } else {
183 hash_index = entry->second;
184 FrameInfoType* frame_info = &frames_[hash_index];
185 frame_info->references++;
186 }
187 return hash_index;
188}
189
190void PointerData::RemoveBacktrace(size_t hash_index) {
191 if (hash_index <= kBacktraceEmptyIndex) {
192 return;
193 }
194
195 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
196 auto frame_entry = frames_.find(hash_index);
197 if (frame_entry == frames_.end()) {
198 error_log("hash_index %zu does not have matching frame data.", hash_index);
199 return;
200 }
201 FrameInfoType* frame_info = &frame_entry->second;
202 if (--frame_info->references == 0) {
203 FrameKeyType key{.num_frames = frame_info->frames.size(), .frames = frame_info->frames.data()};
204 key_to_index_.erase(key);
205 frames_.erase(hash_index);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700206 if (g_debug->config().options() & BACKTRACE_FULL) {
207 backtraces_info_.erase(hash_index);
208 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800209 }
210}
211
212void PointerData::Add(const void* ptr, size_t pointer_size) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800213 size_t hash_index = 0;
214 if (backtrace_enabled_) {
Christopher Ferrisa3836482022-05-13 12:09:39 -0700215 hash_index = AddBacktrace(g_debug->config().backtrace_frames(), pointer_size);
Christopher Ferris4da25032018-03-07 13:38:48 -0800216 }
217
218 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700219 uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr));
220 pointers_[mangled_ptr] =
221 PointerInfoType{PointerInfoType::GetEncodedSize(pointer_size), hash_index};
Christopher Ferris4da25032018-03-07 13:38:48 -0800222}
223
224void PointerData::Remove(const void* ptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800225 size_t hash_index;
226 {
227 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700228 uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr));
229 auto entry = pointers_.find(mangled_ptr);
Christopher Ferris4da25032018-03-07 13:38:48 -0800230 if (entry == pointers_.end()) {
Iris Chang7f209a92019-01-16 11:17:15 +0800231 // Attempt to remove unknown pointer.
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700232 error_log("No tracked pointer found for 0x%" PRIxPTR, DemanglePointer(mangled_ptr));
Christopher Ferris4da25032018-03-07 13:38:48 -0800233 return;
234 }
235 hash_index = entry->second.hash_index;
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700236 pointers_.erase(mangled_ptr);
Christopher Ferris4da25032018-03-07 13:38:48 -0800237 }
238
239 RemoveBacktrace(hash_index);
240}
241
242size_t PointerData::GetFrames(const void* ptr, uintptr_t* frames, size_t max_frames) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800243 size_t hash_index;
244 {
245 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700246 uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr));
247 auto entry = pointers_.find(mangled_ptr);
Christopher Ferris4da25032018-03-07 13:38:48 -0800248 if (entry == pointers_.end()) {
249 return 0;
250 }
251 hash_index = entry->second.hash_index;
252 }
253
254 if (hash_index <= kBacktraceEmptyIndex) {
255 return 0;
256 }
257
258 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
259 auto frame_entry = frames_.find(hash_index);
260 if (frame_entry == frames_.end()) {
261 return 0;
262 }
263 FrameInfoType* frame_info = &frame_entry->second;
264 if (max_frames > frame_info->frames.size()) {
265 max_frames = frame_info->frames.size();
266 }
267 memcpy(frames, &frame_info->frames[0], max_frames * sizeof(uintptr_t));
268
269 return max_frames;
270}
271
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700272void PointerData::LogBacktrace(size_t hash_index) {
273 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
274 if (g_debug->config().options() & BACKTRACE_FULL) {
275 auto backtrace_info_entry = backtraces_info_.find(hash_index);
276 if (backtrace_info_entry != backtraces_info_.end()) {
277 UnwindLog(backtrace_info_entry->second);
278 return;
279 }
280 } else {
281 auto frame_entry = frames_.find(hash_index);
282 if (frame_entry != frames_.end()) {
283 FrameInfoType* frame_info = &frame_entry->second;
284 backtrace_log(frame_info->frames.data(), frame_info->frames.size());
285 return;
286 }
287 }
288 error_log(" hash_index %zu does not have matching frame data.", hash_index);
289}
290
Iris Changb3441502019-02-12 14:00:59 +0800291void PointerData::LogFreeError(const FreePointerInfoType& info, size_t max_cmp_bytes) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800292 error_log(LOG_DIVIDER);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700293 uintptr_t pointer = DemanglePointer(info.mangled_ptr);
294 uint8_t* memory = reinterpret_cast<uint8_t*>(pointer);
Christopher Ferris4da25032018-03-07 13:38:48 -0800295 error_log("+++ ALLOCATION %p USED AFTER FREE", memory);
296 uint8_t fill_free_value = g_debug->config().fill_free_value();
Iris Changb3441502019-02-12 14:00:59 +0800297 for (size_t i = 0; i < max_cmp_bytes; i++) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800298 if (memory[i] != fill_free_value) {
299 error_log(" allocation[%zu] = 0x%02x (expected 0x%02x)", i, memory[i], fill_free_value);
300 }
301 }
302
303 if (info.hash_index > kBacktraceEmptyIndex) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700304 error_log("Backtrace at time of free:");
305 LogBacktrace(info.hash_index);
Christopher Ferris4da25032018-03-07 13:38:48 -0800306 }
307
308 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +0800309 if (g_debug->config().options() & ABORT_ON_ERROR) {
310 abort();
311 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800312}
313
314void PointerData::VerifyFreedPointer(const FreePointerInfoType& info) {
315 size_t usable_size;
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700316 uintptr_t pointer = DemanglePointer(info.mangled_ptr);
Christopher Ferris4da25032018-03-07 13:38:48 -0800317 if (g_debug->HeaderEnabled()) {
318 // Check to see if the tag data has been damaged.
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700319 Header* header = g_debug->GetHeader(reinterpret_cast<const void*>(pointer));
Christopher Ferris4da25032018-03-07 13:38:48 -0800320 if (header->tag != DEBUG_FREE_TAG) {
321 error_log(LOG_DIVIDER);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700322 error_log("+++ ALLOCATION 0x%" PRIxPTR " HAS CORRUPTED HEADER TAG 0x%x AFTER FREE", pointer,
323 header->tag);
Christopher Ferris4da25032018-03-07 13:38:48 -0800324 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +0800325 if (g_debug->config().options() & ABORT_ON_ERROR) {
326 abort();
327 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800328
329 // Stop processing here, it is impossible to tell how the header
330 // may have been damaged.
331 return;
332 }
333 usable_size = header->usable_size;
334 } else {
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700335 usable_size = g_dispatch->malloc_usable_size(reinterpret_cast<const void*>(pointer));
Christopher Ferris4da25032018-03-07 13:38:48 -0800336 }
337
338 size_t bytes = (usable_size < g_debug->config().fill_on_free_bytes())
339 ? usable_size
340 : g_debug->config().fill_on_free_bytes();
Iris Changb3441502019-02-12 14:00:59 +0800341 size_t max_cmp_bytes = bytes;
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700342 const uint8_t* memory = reinterpret_cast<const uint8_t*>(pointer);
Christopher Ferris4da25032018-03-07 13:38:48 -0800343 while (bytes > 0) {
344 size_t bytes_to_cmp = (bytes < g_cmp_mem.size()) ? bytes : g_cmp_mem.size();
345 if (memcmp(memory, g_cmp_mem.data(), bytes_to_cmp) != 0) {
Iris Changb3441502019-02-12 14:00:59 +0800346 LogFreeError(info, max_cmp_bytes);
Christopher Ferris4da25032018-03-07 13:38:48 -0800347 }
348 bytes -= bytes_to_cmp;
349 memory = &memory[bytes_to_cmp];
350 }
351}
352
Christopher Ferrisa3836482022-05-13 12:09:39 -0700353void* PointerData::AddFreed(const void* ptr, size_t size_bytes) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800354 size_t hash_index = 0;
355 size_t num_frames = g_debug->config().free_track_backtrace_num_frames();
356 if (num_frames) {
Christopher Ferrisa3836482022-05-13 12:09:39 -0700357 hash_index = AddBacktrace(num_frames, size_bytes);
Christopher Ferris4da25032018-03-07 13:38:48 -0800358 }
359
360 void* last = nullptr;
361 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
362 if (free_pointers_.size() == g_debug->config().free_track_allocations()) {
363 FreePointerInfoType info(free_pointers_.front());
364 free_pointers_.pop_front();
365 VerifyFreedPointer(info);
366 RemoveBacktrace(info.hash_index);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700367 last = reinterpret_cast<void*>(DemanglePointer(info.mangled_ptr));
Christopher Ferris4da25032018-03-07 13:38:48 -0800368 }
369
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700370 uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr));
371 free_pointers_.emplace_back(FreePointerInfoType{mangled_ptr, hash_index});
Christopher Ferris4da25032018-03-07 13:38:48 -0800372 return last;
373}
374
375void PointerData::LogFreeBacktrace(const void* ptr) {
376 size_t hash_index = 0;
377 {
378 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
379 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
380 for (const auto& info : free_pointers_) {
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700381 if (DemanglePointer(info.mangled_ptr) == pointer) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800382 hash_index = info.hash_index;
383 break;
384 }
385 }
386 }
387
388 if (hash_index <= kBacktraceEmptyIndex) {
389 return;
390 }
391
Christopher Ferris4da25032018-03-07 13:38:48 -0800392 error_log("Backtrace of original free:");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700393 LogBacktrace(hash_index);
Christopher Ferris4da25032018-03-07 13:38:48 -0800394}
395
396void PointerData::VerifyAllFreed() {
397 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
398 for (auto& free_info : free_pointers_) {
399 VerifyFreedPointer(free_info);
400 }
401}
402
403void PointerData::GetList(std::vector<ListInfoType>* list, bool only_with_backtrace)
404 REQUIRES(pointer_mutex_, frame_mutex_) {
405 for (const auto& entry : pointers_) {
406 FrameInfoType* frame_info = nullptr;
Christopher Ferris459eecb2022-01-07 13:38:10 -0800407 std::vector<unwindstack::FrameData>* backtrace_info = nullptr;
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700408 uintptr_t pointer = DemanglePointer(entry.first);
Christopher Ferris4da25032018-03-07 13:38:48 -0800409 size_t hash_index = entry.second.hash_index;
410 if (hash_index > kBacktraceEmptyIndex) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700411 auto frame_entry = frames_.find(hash_index);
412 if (frame_entry == frames_.end()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800413 // Somehow wound up with a pointer with a valid hash_index, but
414 // no frame data. This should not be possible since adding a pointer
415 // occurs after the hash_index and frame data have been added.
416 // When removing a pointer, the pointer is deleted before the frame
417 // data.
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700418 error_log("Pointer 0x%" PRIxPTR " hash_index %zu does not exist.", pointer, hash_index);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700419 } else {
420 frame_info = &frame_entry->second;
421 }
422
423 if (g_debug->config().options() & BACKTRACE_FULL) {
424 auto backtrace_entry = backtraces_info_.find(hash_index);
425 if (backtrace_entry == backtraces_info_.end()) {
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700426 error_log("Pointer 0x%" PRIxPTR " hash_index %zu does not exist.", pointer, hash_index);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700427 } else {
428 backtrace_info = &backtrace_entry->second;
429 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800430 }
431 }
432 if (hash_index == 0 && only_with_backtrace) {
433 continue;
434 }
435
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700436 list->emplace_back(ListInfoType{pointer, 1, entry.second.RealSize(),
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700437 entry.second.ZygoteChildAlloc(), frame_info, backtrace_info});
Christopher Ferris4da25032018-03-07 13:38:48 -0800438 }
439
440 // Sort by the size of the allocation.
441 std::sort(list->begin(), list->end(), [](const ListInfoType& a, const ListInfoType& b) {
442 // Put zygote child allocations first.
443 bool a_zygote_child_alloc = a.zygote_child_alloc;
444 bool b_zygote_child_alloc = b.zygote_child_alloc;
445 if (a_zygote_child_alloc && !b_zygote_child_alloc) {
446 return false;
447 }
448 if (!a_zygote_child_alloc && b_zygote_child_alloc) {
449 return true;
450 }
451
452 // Sort by size, descending order.
453 if (a.size != b.size) return a.size > b.size;
454
455 // Put pointers with no backtrace last.
456 FrameInfoType* a_frame = a.frame_info;
457 FrameInfoType* b_frame = b.frame_info;
458 if (a_frame == nullptr && b_frame != nullptr) {
459 return false;
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700460 } else if (a_frame != nullptr && b_frame == nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800461 return true;
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700462 } else if (a_frame == nullptr && b_frame == nullptr) {
463 return a.pointer < b.pointer;
Christopher Ferris4da25032018-03-07 13:38:48 -0800464 }
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700465
Christopher Ferris4da25032018-03-07 13:38:48 -0800466 // Put the pointers with longest backtrace first.
467 if (a_frame->frames.size() != b_frame->frames.size()) {
468 return a_frame->frames.size() > b_frame->frames.size();
469 }
470
471 // Last sort by pointer.
472 return a.pointer < b.pointer;
473 });
474}
475
476void PointerData::GetUniqueList(std::vector<ListInfoType>* list, bool only_with_backtrace)
477 REQUIRES(pointer_mutex_, frame_mutex_) {
478 GetList(list, only_with_backtrace);
479
480 // Remove duplicates of size/backtraces.
481 for (auto iter = list->begin(); iter != list->end();) {
482 auto dup_iter = iter + 1;
483 bool zygote_child_alloc = iter->zygote_child_alloc;
484 size_t size = iter->size;
485 FrameInfoType* frame_info = iter->frame_info;
486 for (; dup_iter != list->end(); ++dup_iter) {
487 if (zygote_child_alloc != dup_iter->zygote_child_alloc || size != dup_iter->size ||
488 frame_info != dup_iter->frame_info) {
489 break;
490 }
491 iter->num_allocations++;
492 }
493 iter = list->erase(iter + 1, dup_iter);
494 }
495}
496
497void PointerData::LogLeaks() {
498 std::vector<ListInfoType> list;
499
500 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
501 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
502 GetList(&list, false);
503
504 size_t track_count = 0;
505 for (const auto& list_info : list) {
506 error_log("+++ %s leaked block of size %zu at 0x%" PRIxPTR " (leak %zu of %zu)", getprogname(),
507 list_info.size, list_info.pointer, ++track_count, list.size());
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700508 if (list_info.backtrace_info != nullptr) {
509 error_log("Backtrace at time of allocation:");
510 UnwindLog(*list_info.backtrace_info);
511 } else if (list_info.frame_info != nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800512 error_log("Backtrace at time of allocation:");
513 backtrace_log(list_info.frame_info->frames.data(), list_info.frame_info->frames.size());
514 }
515 // Do not bother to free the pointers, we are about to exit any way.
516 }
517}
518
Christopher Ferris6c619a02019-03-01 17:59:51 -0800519void PointerData::GetAllocList(std::vector<ListInfoType>* list) {
520 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
521 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
522
523 if (pointers_.empty()) {
524 return;
525 }
526
527 GetList(list, false);
528}
529
Christopher Ferris4da25032018-03-07 13:38:48 -0800530void PointerData::GetInfo(uint8_t** info, size_t* overall_size, size_t* info_size,
531 size_t* total_memory, size_t* backtrace_size) {
532 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
533 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
534
535 if (pointers_.empty()) {
536 return;
537 }
538
539 std::vector<ListInfoType> list;
540 GetUniqueList(&list, true);
541 if (list.empty()) {
542 return;
543 }
544
545 *backtrace_size = g_debug->config().backtrace_frames();
546 *info_size = sizeof(size_t) * 2 + sizeof(uintptr_t) * *backtrace_size;
547 *overall_size = *info_size * list.size();
548 *info = reinterpret_cast<uint8_t*>(g_dispatch->calloc(*info_size, list.size()));
549 if (*info == nullptr) {
550 return;
551 }
552
553 uint8_t* data = *info;
554 *total_memory = 0;
555 for (const auto& list_info : list) {
556 FrameInfoType* frame_info = list_info.frame_info;
557 *total_memory += list_info.size * list_info.num_allocations;
558 size_t allocation_size =
559 PointerInfoType::GetEncodedSize(list_info.zygote_child_alloc, list_info.size);
560 memcpy(data, &allocation_size, sizeof(size_t));
561 memcpy(&data[sizeof(size_t)], &list_info.num_allocations, sizeof(size_t));
562 if (frame_info != nullptr) {
563 memcpy(&data[2 * sizeof(size_t)], frame_info->frames.data(),
564 frame_info->frames.size() * sizeof(uintptr_t));
565 }
566 data += *info_size;
567 }
568}
569
570bool PointerData::Exists(const void* ptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800571 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700572 uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr));
573 return pointers_.count(mangled_ptr) != 0;
Christopher Ferris4da25032018-03-07 13:38:48 -0800574}
575
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800576void PointerData::DumpLiveToFile(int fd) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800577 std::vector<ListInfoType> list;
578
579 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
580 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
581 GetUniqueList(&list, false);
582
583 size_t total_memory = 0;
584 for (const auto& info : list) {
585 total_memory += info.size * info.num_allocations;
586 }
587
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800588 dprintf(fd, "Total memory: %zu\n", total_memory);
589 dprintf(fd, "Allocation records: %zd\n", list.size());
590 dprintf(fd, "Backtrace size: %zu\n", g_debug->config().backtrace_frames());
591 dprintf(fd, "\n");
Christopher Ferris4da25032018-03-07 13:38:48 -0800592
593 for (const auto& info : list) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800594 dprintf(fd, "z %d sz %8zu num %zu bt", (info.zygote_child_alloc) ? 1 : 0, info.size,
Christopher Ferris4da25032018-03-07 13:38:48 -0800595 info.num_allocations);
596 FrameInfoType* frame_info = info.frame_info;
597 if (frame_info != nullptr) {
598 for (size_t i = 0; i < frame_info->frames.size(); i++) {
599 if (frame_info->frames[i] == 0) {
600 break;
601 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800602 dprintf(fd, " %" PRIxPTR, frame_info->frames[i]);
Christopher Ferris4da25032018-03-07 13:38:48 -0800603 }
604 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800605 dprintf(fd, "\n");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700606 if (info.backtrace_info != nullptr) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800607 dprintf(fd, " bt_info");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700608 for (const auto& frame : *info.backtrace_info) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800609 dprintf(fd, " {");
David Srbecky92b8d642021-05-13 00:03:26 +0100610 if (frame.map_info != nullptr && !frame.map_info->name().empty()) {
611 dprintf(fd, "\"%s\"", frame.map_info->name().c_str());
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700612 } else {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800613 dprintf(fd, "\"\"");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700614 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800615 dprintf(fd, " %" PRIx64, frame.rel_pc);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700616 if (frame.function_name.empty()) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800617 dprintf(fd, " \"\" 0}");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700618 } else {
Ryan Prichardc2adad12022-12-08 16:52:39 -0800619 char* demangled_name =
620 abi::__cxa_demangle(frame.function_name.c_str(), nullptr, nullptr, nullptr);
Christopher Ferris9782b872019-07-18 13:36:50 -0700621 const char* name;
622 if (demangled_name != nullptr) {
623 name = demangled_name;
624 } else {
625 name = frame.function_name.c_str();
626 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800627 dprintf(fd, " \"%s\" %" PRIx64 "}", name, frame.function_offset);
Christopher Ferris9782b872019-07-18 13:36:50 -0700628 free(demangled_name);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700629 }
630 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800631 dprintf(fd, "\n");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700632 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800633 }
634}
635
636void PointerData::PrepareFork() NO_THREAD_SAFETY_ANALYSIS {
Iris Chang76dcc472019-03-07 12:32:19 +0800637 free_pointer_mutex_.lock();
Christopher Ferris4da25032018-03-07 13:38:48 -0800638 pointer_mutex_.lock();
639 frame_mutex_.lock();
Christopher Ferris4da25032018-03-07 13:38:48 -0800640}
641
642void PointerData::PostForkParent() NO_THREAD_SAFETY_ANALYSIS {
643 frame_mutex_.unlock();
644 pointer_mutex_.unlock();
645 free_pointer_mutex_.unlock();
646}
647
648void PointerData::PostForkChild() __attribute__((no_thread_safety_analysis)) {
649 // Make sure that any potential mutexes have been released and are back
650 // to an initial state.
651 frame_mutex_.try_lock();
652 frame_mutex_.unlock();
653 pointer_mutex_.try_lock();
654 pointer_mutex_.unlock();
655 free_pointer_mutex_.try_lock();
656 free_pointer_mutex_.unlock();
657}
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700658
659void PointerData::IteratePointers(std::function<void(uintptr_t pointer)> fn) {
660 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
661 for (const auto entry : pointers_) {
662 fn(DemanglePointer(entry.first));
663 }
664}