blob: 5129bf65edd84d25571a04584f80753bffb06253 [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#include <errno.h>
30#include <inttypes.h>
31#include <signal.h>
32#include <stdint.h>
33#include <stdlib.h>
34#include <string.h>
35#include <sys/types.h>
36#include <unistd.h>
37
Elliott Hughes0ec50d82025-05-09 13:58:56 -070038#include <algorithm>
39#include <atomic>
40#include <deque>
Christopher Ferrisf78486f2022-05-04 14:08:54 -070041#include <functional>
Christopher Ferris4da25032018-03-07 13:38:48 -080042#include <mutex>
43#include <string>
44#include <unordered_map>
45#include <utility>
46#include <vector>
47
48#include <android-base/stringprintf.h>
49#include <android-base/thread_annotations.h>
Josh Gao4956c372019-12-19 16:35:51 -080050#include <platform/bionic/macros.h>
Christopher Ferris3d470862025-08-13 16:43:58 +000051#include <unwindstack/Demangle.h>
Christopher Ferris4da25032018-03-07 13:38:48 -080052
53#include "Config.h"
54#include "DebugData.h"
55#include "PointerData.h"
56#include "backtrace.h"
57#include "debug_log.h"
58#include "malloc_debug.h"
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070059#include "UnwindBacktrace.h"
Christopher Ferris4da25032018-03-07 13:38:48 -080060
61std::atomic_uint8_t PointerData::backtrace_enabled_;
62std::atomic_bool PointerData::backtrace_dump_;
63
64std::mutex PointerData::pointer_mutex_;
65std::unordered_map<uintptr_t, PointerInfoType> PointerData::pointers_ GUARDED_BY(
66 PointerData::pointer_mutex_);
67
68std::mutex PointerData::frame_mutex_;
69std::unordered_map<FrameKeyType, size_t> PointerData::key_to_index_ GUARDED_BY(
70 PointerData::frame_mutex_);
71std::unordered_map<size_t, FrameInfoType> PointerData::frames_ GUARDED_BY(PointerData::frame_mutex_);
Christopher Ferris459eecb2022-01-07 13:38:10 -080072std::unordered_map<size_t, std::vector<unwindstack::FrameData>> PointerData::backtraces_info_
73 GUARDED_BY(PointerData::frame_mutex_);
Christopher Ferris4da25032018-03-07 13:38:48 -080074constexpr size_t kBacktraceEmptyIndex = 1;
75size_t PointerData::cur_hash_index_ GUARDED_BY(PointerData::frame_mutex_);
76
77std::mutex PointerData::free_pointer_mutex_;
78std::deque<FreePointerInfoType> PointerData::free_pointers_ GUARDED_BY(
79 PointerData::free_pointer_mutex_);
80
81// Buffer to use for comparison.
82static constexpr size_t kCompareBufferSize = 512 * 1024;
83static std::vector<uint8_t> g_cmp_mem(0);
84
85static void ToggleBacktraceEnable(int, siginfo_t*, void*) {
86 g_debug->pointer->ToggleBacktraceEnabled();
87}
88
89static void EnableDump(int, siginfo_t*, void*) {
90 g_debug->pointer->EnableDumping();
91}
92
93PointerData::PointerData(DebugData* debug_data) : OptionData(debug_data) {}
94
95bool PointerData::Initialize(const Config& config) NO_THREAD_SAFETY_ANALYSIS {
96 pointers_.clear();
97 key_to_index_.clear();
98 frames_.clear();
99 free_pointers_.clear();
100 // A hash index of kBacktraceEmptyIndex indicates that we tried to get
101 // a backtrace, but there was nothing recorded.
102 cur_hash_index_ = kBacktraceEmptyIndex + 1;
103
104 backtrace_enabled_ = config.backtrace_enabled();
105 if (config.backtrace_enable_on_signal()) {
106 struct sigaction64 enable_act = {};
107 enable_act.sa_sigaction = ToggleBacktraceEnable;
108 enable_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
109 if (sigaction64(config.backtrace_signal(), &enable_act, nullptr) != 0) {
110 error_log("Unable to set up backtrace signal enable function: %s", strerror(errno));
111 return false;
112 }
Christopher Ferrisc328e442019-04-01 19:31:26 -0700113 if (config.options() & VERBOSE) {
114 info_log("%s: Run: 'kill -%d %d' to enable backtracing.", getprogname(),
115 config.backtrace_signal(), getpid());
116 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800117 }
118
119 if (config.options() & BACKTRACE) {
120 struct sigaction64 act = {};
121 act.sa_sigaction = EnableDump;
122 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
123 if (sigaction64(config.backtrace_dump_signal(), &act, nullptr) != 0) {
124 error_log("Unable to set up backtrace dump signal function: %s", strerror(errno));
125 return false;
126 }
Christopher Ferrisc328e442019-04-01 19:31:26 -0700127 if (config.options() & VERBOSE) {
128 info_log("%s: Run: 'kill -%d %d' to dump the backtrace.", getprogname(),
129 config.backtrace_dump_signal(), getpid());
130 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800131 }
132
133 backtrace_dump_ = false;
134
135 if (config.options() & FREE_TRACK) {
136 g_cmp_mem.resize(kCompareBufferSize, config.fill_free_value());
137 }
138 return true;
139}
140
Christopher Ferrisa3836482022-05-13 12:09:39 -0700141static inline bool ShouldBacktraceAllocSize(size_t size_bytes) {
142 static bool only_backtrace_specific_sizes =
143 g_debug->config().options() & BACKTRACE_SPECIFIC_SIZES;
144 if (!only_backtrace_specific_sizes) {
145 return true;
146 }
147 static size_t min_size_bytes = g_debug->config().backtrace_min_size_bytes();
148 static size_t max_size_bytes = g_debug->config().backtrace_max_size_bytes();
149 return size_bytes >= min_size_bytes && size_bytes <= max_size_bytes;
150}
151
152size_t PointerData::AddBacktrace(size_t num_frames, size_t size_bytes) {
153 if (!ShouldBacktraceAllocSize(size_bytes)) {
154 return kBacktraceEmptyIndex;
155 }
156
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700157 std::vector<uintptr_t> frames;
Christopher Ferris459eecb2022-01-07 13:38:10 -0800158 std::vector<unwindstack::FrameData> frames_info;
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700159 if (g_debug->config().options() & BACKTRACE_FULL) {
160 if (!Unwind(&frames, &frames_info, num_frames)) {
161 return kBacktraceEmptyIndex;
162 }
163 } else {
164 frames.resize(num_frames);
165 num_frames = backtrace_get(frames.data(), frames.size());
166 if (num_frames == 0) {
167 return kBacktraceEmptyIndex;
168 }
Christopher Ferrisdfbc59a2022-03-23 12:22:36 -0700169 frames.resize(num_frames);
Christopher Ferris4da25032018-03-07 13:38:48 -0800170 }
171
Christopher Ferrisdfbc59a2022-03-23 12:22:36 -0700172 FrameKeyType key{.num_frames = frames.size(), .frames = frames.data()};
Christopher Ferris4da25032018-03-07 13:38:48 -0800173 size_t hash_index;
174 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
175 auto entry = key_to_index_.find(key);
176 if (entry == key_to_index_.end()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800177 hash_index = cur_hash_index_++;
178 key.frames = frames.data();
179 key_to_index_.emplace(key, hash_index);
180
181 frames_.emplace(hash_index, FrameInfoType{.references = 1, .frames = std::move(frames)});
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700182 if (g_debug->config().options() & BACKTRACE_FULL) {
183 backtraces_info_.emplace(hash_index, std::move(frames_info));
184 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800185 } else {
186 hash_index = entry->second;
187 FrameInfoType* frame_info = &frames_[hash_index];
188 frame_info->references++;
189 }
190 return hash_index;
191}
192
193void PointerData::RemoveBacktrace(size_t hash_index) {
194 if (hash_index <= kBacktraceEmptyIndex) {
195 return;
196 }
197
198 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
199 auto frame_entry = frames_.find(hash_index);
200 if (frame_entry == frames_.end()) {
201 error_log("hash_index %zu does not have matching frame data.", hash_index);
202 return;
203 }
204 FrameInfoType* frame_info = &frame_entry->second;
205 if (--frame_info->references == 0) {
206 FrameKeyType key{.num_frames = frame_info->frames.size(), .frames = frame_info->frames.data()};
207 key_to_index_.erase(key);
208 frames_.erase(hash_index);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700209 if (g_debug->config().options() & BACKTRACE_FULL) {
210 backtraces_info_.erase(hash_index);
211 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800212 }
213}
214
215void PointerData::Add(const void* ptr, size_t pointer_size) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800216 size_t hash_index = 0;
217 if (backtrace_enabled_) {
Christopher Ferrisa3836482022-05-13 12:09:39 -0700218 hash_index = AddBacktrace(g_debug->config().backtrace_frames(), pointer_size);
Christopher Ferris4da25032018-03-07 13:38:48 -0800219 }
220
221 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700222 uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr));
223 pointers_[mangled_ptr] =
224 PointerInfoType{PointerInfoType::GetEncodedSize(pointer_size), hash_index};
Christopher Ferris4da25032018-03-07 13:38:48 -0800225}
226
227void PointerData::Remove(const void* ptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800228 size_t hash_index;
229 {
230 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700231 uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr));
232 auto entry = pointers_.find(mangled_ptr);
Christopher Ferris4da25032018-03-07 13:38:48 -0800233 if (entry == pointers_.end()) {
Iris Chang7f209a92019-01-16 11:17:15 +0800234 // Attempt to remove unknown pointer.
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700235 error_log("No tracked pointer found for 0x%" PRIxPTR, DemanglePointer(mangled_ptr));
Christopher Ferris4da25032018-03-07 13:38:48 -0800236 return;
237 }
238 hash_index = entry->second.hash_index;
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700239 pointers_.erase(mangled_ptr);
Christopher Ferris4da25032018-03-07 13:38:48 -0800240 }
241
242 RemoveBacktrace(hash_index);
243}
244
245size_t PointerData::GetFrames(const void* ptr, uintptr_t* frames, size_t max_frames) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800246 size_t hash_index;
247 {
248 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700249 uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr));
250 auto entry = pointers_.find(mangled_ptr);
Christopher Ferris4da25032018-03-07 13:38:48 -0800251 if (entry == pointers_.end()) {
252 return 0;
253 }
254 hash_index = entry->second.hash_index;
255 }
256
257 if (hash_index <= kBacktraceEmptyIndex) {
258 return 0;
259 }
260
261 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
262 auto frame_entry = frames_.find(hash_index);
263 if (frame_entry == frames_.end()) {
264 return 0;
265 }
266 FrameInfoType* frame_info = &frame_entry->second;
267 if (max_frames > frame_info->frames.size()) {
268 max_frames = frame_info->frames.size();
269 }
270 memcpy(frames, &frame_info->frames[0], max_frames * sizeof(uintptr_t));
271
272 return max_frames;
273}
274
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700275void PointerData::LogBacktrace(size_t hash_index) {
276 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
277 if (g_debug->config().options() & BACKTRACE_FULL) {
278 auto backtrace_info_entry = backtraces_info_.find(hash_index);
279 if (backtrace_info_entry != backtraces_info_.end()) {
280 UnwindLog(backtrace_info_entry->second);
281 return;
282 }
283 } else {
284 auto frame_entry = frames_.find(hash_index);
285 if (frame_entry != frames_.end()) {
286 FrameInfoType* frame_info = &frame_entry->second;
287 backtrace_log(frame_info->frames.data(), frame_info->frames.size());
288 return;
289 }
290 }
291 error_log(" hash_index %zu does not have matching frame data.", hash_index);
292}
293
Iris Changb3441502019-02-12 14:00:59 +0800294void PointerData::LogFreeError(const FreePointerInfoType& info, size_t max_cmp_bytes) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800295 error_log(LOG_DIVIDER);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700296 uintptr_t pointer = DemanglePointer(info.mangled_ptr);
297 uint8_t* memory = reinterpret_cast<uint8_t*>(pointer);
Christopher Ferris4da25032018-03-07 13:38:48 -0800298 error_log("+++ ALLOCATION %p USED AFTER FREE", memory);
299 uint8_t fill_free_value = g_debug->config().fill_free_value();
Iris Changb3441502019-02-12 14:00:59 +0800300 for (size_t i = 0; i < max_cmp_bytes; i++) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800301 if (memory[i] != fill_free_value) {
302 error_log(" allocation[%zu] = 0x%02x (expected 0x%02x)", i, memory[i], fill_free_value);
303 }
304 }
305
306 if (info.hash_index > kBacktraceEmptyIndex) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700307 error_log("Backtrace at time of free:");
308 LogBacktrace(info.hash_index);
Christopher Ferris4da25032018-03-07 13:38:48 -0800309 }
310
311 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +0800312 if (g_debug->config().options() & ABORT_ON_ERROR) {
313 abort();
314 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800315}
316
317void PointerData::VerifyFreedPointer(const FreePointerInfoType& info) {
318 size_t usable_size;
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700319 uintptr_t pointer = DemanglePointer(info.mangled_ptr);
Christopher Ferris4da25032018-03-07 13:38:48 -0800320 if (g_debug->HeaderEnabled()) {
321 // Check to see if the tag data has been damaged.
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700322 Header* header = g_debug->GetHeader(reinterpret_cast<const void*>(pointer));
Christopher Ferris4da25032018-03-07 13:38:48 -0800323 if (header->tag != DEBUG_FREE_TAG) {
324 error_log(LOG_DIVIDER);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700325 error_log("+++ ALLOCATION 0x%" PRIxPTR " HAS CORRUPTED HEADER TAG 0x%x AFTER FREE", pointer,
326 header->tag);
Christopher Ferris4da25032018-03-07 13:38:48 -0800327 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +0800328 if (g_debug->config().options() & ABORT_ON_ERROR) {
329 abort();
330 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800331
332 // Stop processing here, it is impossible to tell how the header
333 // may have been damaged.
334 return;
335 }
336 usable_size = header->usable_size;
337 } else {
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700338 usable_size = g_dispatch->malloc_usable_size(reinterpret_cast<const void*>(pointer));
Christopher Ferris4da25032018-03-07 13:38:48 -0800339 }
340
341 size_t bytes = (usable_size < g_debug->config().fill_on_free_bytes())
342 ? usable_size
343 : g_debug->config().fill_on_free_bytes();
Iris Changb3441502019-02-12 14:00:59 +0800344 size_t max_cmp_bytes = bytes;
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700345 const uint8_t* memory = reinterpret_cast<const uint8_t*>(pointer);
Christopher Ferris4da25032018-03-07 13:38:48 -0800346 while (bytes > 0) {
347 size_t bytes_to_cmp = (bytes < g_cmp_mem.size()) ? bytes : g_cmp_mem.size();
348 if (memcmp(memory, g_cmp_mem.data(), bytes_to_cmp) != 0) {
Iris Changb3441502019-02-12 14:00:59 +0800349 LogFreeError(info, max_cmp_bytes);
Christopher Ferris4da25032018-03-07 13:38:48 -0800350 }
351 bytes -= bytes_to_cmp;
352 memory = &memory[bytes_to_cmp];
353 }
354}
355
Christopher Ferrisa3836482022-05-13 12:09:39 -0700356void* PointerData::AddFreed(const void* ptr, size_t size_bytes) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800357 size_t hash_index = 0;
358 size_t num_frames = g_debug->config().free_track_backtrace_num_frames();
359 if (num_frames) {
Christopher Ferrisa3836482022-05-13 12:09:39 -0700360 hash_index = AddBacktrace(num_frames, size_bytes);
Christopher Ferris4da25032018-03-07 13:38:48 -0800361 }
362
363 void* last = nullptr;
364 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
365 if (free_pointers_.size() == g_debug->config().free_track_allocations()) {
366 FreePointerInfoType info(free_pointers_.front());
367 free_pointers_.pop_front();
368 VerifyFreedPointer(info);
369 RemoveBacktrace(info.hash_index);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700370 last = reinterpret_cast<void*>(DemanglePointer(info.mangled_ptr));
Christopher Ferris4da25032018-03-07 13:38:48 -0800371 }
372
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700373 uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr));
374 free_pointers_.emplace_back(FreePointerInfoType{mangled_ptr, hash_index});
Christopher Ferris4da25032018-03-07 13:38:48 -0800375 return last;
376}
377
378void PointerData::LogFreeBacktrace(const void* ptr) {
379 size_t hash_index = 0;
380 {
381 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
382 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
383 for (const auto& info : free_pointers_) {
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700384 if (DemanglePointer(info.mangled_ptr) == pointer) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800385 hash_index = info.hash_index;
386 break;
387 }
388 }
389 }
390
391 if (hash_index <= kBacktraceEmptyIndex) {
392 return;
393 }
394
Christopher Ferris4da25032018-03-07 13:38:48 -0800395 error_log("Backtrace of original free:");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700396 LogBacktrace(hash_index);
Christopher Ferris4da25032018-03-07 13:38:48 -0800397}
398
399void PointerData::VerifyAllFreed() {
400 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
401 for (auto& free_info : free_pointers_) {
402 VerifyFreedPointer(free_info);
403 }
404}
405
406void PointerData::GetList(std::vector<ListInfoType>* list, bool only_with_backtrace)
407 REQUIRES(pointer_mutex_, frame_mutex_) {
408 for (const auto& entry : pointers_) {
409 FrameInfoType* frame_info = nullptr;
Christopher Ferris459eecb2022-01-07 13:38:10 -0800410 std::vector<unwindstack::FrameData>* backtrace_info = nullptr;
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700411 uintptr_t pointer = DemanglePointer(entry.first);
Christopher Ferris4da25032018-03-07 13:38:48 -0800412 size_t hash_index = entry.second.hash_index;
413 if (hash_index > kBacktraceEmptyIndex) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700414 auto frame_entry = frames_.find(hash_index);
415 if (frame_entry == frames_.end()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800416 // Somehow wound up with a pointer with a valid hash_index, but
417 // no frame data. This should not be possible since adding a pointer
418 // occurs after the hash_index and frame data have been added.
419 // When removing a pointer, the pointer is deleted before the frame
420 // data.
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700421 error_log("Pointer 0x%" PRIxPTR " hash_index %zu does not exist.", pointer, hash_index);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700422 } else {
423 frame_info = &frame_entry->second;
424 }
425
426 if (g_debug->config().options() & BACKTRACE_FULL) {
427 auto backtrace_entry = backtraces_info_.find(hash_index);
428 if (backtrace_entry == backtraces_info_.end()) {
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700429 error_log("Pointer 0x%" PRIxPTR " hash_index %zu does not exist.", pointer, hash_index);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700430 } else {
431 backtrace_info = &backtrace_entry->second;
432 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800433 }
434 }
435 if (hash_index == 0 && only_with_backtrace) {
436 continue;
437 }
438
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700439 list->emplace_back(ListInfoType{pointer, 1, entry.second.RealSize(),
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700440 entry.second.ZygoteChildAlloc(), frame_info, backtrace_info});
Christopher Ferris4da25032018-03-07 13:38:48 -0800441 }
442
443 // Sort by the size of the allocation.
444 std::sort(list->begin(), list->end(), [](const ListInfoType& a, const ListInfoType& b) {
445 // Put zygote child allocations first.
446 bool a_zygote_child_alloc = a.zygote_child_alloc;
447 bool b_zygote_child_alloc = b.zygote_child_alloc;
448 if (a_zygote_child_alloc && !b_zygote_child_alloc) {
449 return false;
450 }
451 if (!a_zygote_child_alloc && b_zygote_child_alloc) {
452 return true;
453 }
454
455 // Sort by size, descending order.
456 if (a.size != b.size) return a.size > b.size;
457
458 // Put pointers with no backtrace last.
459 FrameInfoType* a_frame = a.frame_info;
460 FrameInfoType* b_frame = b.frame_info;
461 if (a_frame == nullptr && b_frame != nullptr) {
462 return false;
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700463 } else if (a_frame != nullptr && b_frame == nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800464 return true;
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700465 } else if (a_frame == nullptr && b_frame == nullptr) {
466 return a.pointer < b.pointer;
Christopher Ferris4da25032018-03-07 13:38:48 -0800467 }
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700468
Christopher Ferris4da25032018-03-07 13:38:48 -0800469 // Put the pointers with longest backtrace first.
470 if (a_frame->frames.size() != b_frame->frames.size()) {
471 return a_frame->frames.size() > b_frame->frames.size();
472 }
473
474 // Last sort by pointer.
475 return a.pointer < b.pointer;
476 });
477}
478
479void PointerData::GetUniqueList(std::vector<ListInfoType>* list, bool only_with_backtrace)
480 REQUIRES(pointer_mutex_, frame_mutex_) {
481 GetList(list, only_with_backtrace);
482
483 // Remove duplicates of size/backtraces.
484 for (auto iter = list->begin(); iter != list->end();) {
485 auto dup_iter = iter + 1;
486 bool zygote_child_alloc = iter->zygote_child_alloc;
487 size_t size = iter->size;
488 FrameInfoType* frame_info = iter->frame_info;
489 for (; dup_iter != list->end(); ++dup_iter) {
490 if (zygote_child_alloc != dup_iter->zygote_child_alloc || size != dup_iter->size ||
491 frame_info != dup_iter->frame_info) {
492 break;
493 }
494 iter->num_allocations++;
495 }
496 iter = list->erase(iter + 1, dup_iter);
497 }
498}
499
500void PointerData::LogLeaks() {
501 std::vector<ListInfoType> list;
502
503 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
504 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
505 GetList(&list, false);
506
507 size_t track_count = 0;
508 for (const auto& list_info : list) {
509 error_log("+++ %s leaked block of size %zu at 0x%" PRIxPTR " (leak %zu of %zu)", getprogname(),
510 list_info.size, list_info.pointer, ++track_count, list.size());
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700511 if (list_info.backtrace_info != nullptr) {
512 error_log("Backtrace at time of allocation:");
513 UnwindLog(*list_info.backtrace_info);
514 } else if (list_info.frame_info != nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800515 error_log("Backtrace at time of allocation:");
516 backtrace_log(list_info.frame_info->frames.data(), list_info.frame_info->frames.size());
517 }
518 // Do not bother to free the pointers, we are about to exit any way.
519 }
520}
521
Christopher Ferris6c619a02019-03-01 17:59:51 -0800522void PointerData::GetAllocList(std::vector<ListInfoType>* list) {
523 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
524 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
525
526 if (pointers_.empty()) {
527 return;
528 }
529
530 GetList(list, false);
531}
532
Christopher Ferris4da25032018-03-07 13:38:48 -0800533void PointerData::GetInfo(uint8_t** info, size_t* overall_size, size_t* info_size,
534 size_t* total_memory, size_t* backtrace_size) {
535 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
536 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
537
538 if (pointers_.empty()) {
539 return;
540 }
541
542 std::vector<ListInfoType> list;
543 GetUniqueList(&list, true);
544 if (list.empty()) {
545 return;
546 }
547
548 *backtrace_size = g_debug->config().backtrace_frames();
549 *info_size = sizeof(size_t) * 2 + sizeof(uintptr_t) * *backtrace_size;
550 *overall_size = *info_size * list.size();
551 *info = reinterpret_cast<uint8_t*>(g_dispatch->calloc(*info_size, list.size()));
552 if (*info == nullptr) {
553 return;
554 }
555
556 uint8_t* data = *info;
557 *total_memory = 0;
558 for (const auto& list_info : list) {
559 FrameInfoType* frame_info = list_info.frame_info;
560 *total_memory += list_info.size * list_info.num_allocations;
561 size_t allocation_size =
562 PointerInfoType::GetEncodedSize(list_info.zygote_child_alloc, list_info.size);
563 memcpy(data, &allocation_size, sizeof(size_t));
564 memcpy(&data[sizeof(size_t)], &list_info.num_allocations, sizeof(size_t));
565 if (frame_info != nullptr) {
566 memcpy(&data[2 * sizeof(size_t)], frame_info->frames.data(),
567 frame_info->frames.size() * sizeof(uintptr_t));
568 }
569 data += *info_size;
570 }
571}
572
573bool PointerData::Exists(const void* ptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800574 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700575 uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr));
576 return pointers_.count(mangled_ptr) != 0;
Christopher Ferris4da25032018-03-07 13:38:48 -0800577}
578
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800579void PointerData::DumpLiveToFile(int fd) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800580 std::vector<ListInfoType> list;
581
582 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
583 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
584 GetUniqueList(&list, false);
585
586 size_t total_memory = 0;
587 for (const auto& info : list) {
588 total_memory += info.size * info.num_allocations;
589 }
590
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800591 dprintf(fd, "Total memory: %zu\n", total_memory);
592 dprintf(fd, "Allocation records: %zd\n", list.size());
593 dprintf(fd, "Backtrace size: %zu\n", g_debug->config().backtrace_frames());
594 dprintf(fd, "\n");
Christopher Ferris4da25032018-03-07 13:38:48 -0800595
596 for (const auto& info : list) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800597 dprintf(fd, "z %d sz %8zu num %zu bt", (info.zygote_child_alloc) ? 1 : 0, info.size,
Christopher Ferris4da25032018-03-07 13:38:48 -0800598 info.num_allocations);
599 FrameInfoType* frame_info = info.frame_info;
600 if (frame_info != nullptr) {
601 for (size_t i = 0; i < frame_info->frames.size(); i++) {
602 if (frame_info->frames[i] == 0) {
603 break;
604 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800605 dprintf(fd, " %" PRIxPTR, frame_info->frames[i]);
Christopher Ferris4da25032018-03-07 13:38:48 -0800606 }
607 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800608 dprintf(fd, "\n");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700609 if (info.backtrace_info != nullptr) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800610 dprintf(fd, " bt_info");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700611 for (const auto& frame : *info.backtrace_info) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800612 dprintf(fd, " {");
David Srbecky92b8d642021-05-13 00:03:26 +0100613 if (frame.map_info != nullptr && !frame.map_info->name().empty()) {
614 dprintf(fd, "\"%s\"", frame.map_info->name().c_str());
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700615 } else {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800616 dprintf(fd, "\"\"");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700617 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800618 dprintf(fd, " %" PRIx64, frame.rel_pc);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700619 if (frame.function_name.empty()) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800620 dprintf(fd, " \"\" 0}");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700621 } else {
Christopher Ferris3d470862025-08-13 16:43:58 +0000622 dprintf(fd, " \"%s\" %" PRIx64 "}",
623 unwindstack::DemangleNameIfNeeded(frame.function_name).c_str(),
624 frame.function_offset);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700625 }
626 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800627 dprintf(fd, "\n");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700628 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800629 }
630}
631
632void PointerData::PrepareFork() NO_THREAD_SAFETY_ANALYSIS {
Iris Chang76dcc472019-03-07 12:32:19 +0800633 free_pointer_mutex_.lock();
Christopher Ferris4da25032018-03-07 13:38:48 -0800634 pointer_mutex_.lock();
635 frame_mutex_.lock();
Christopher Ferris4da25032018-03-07 13:38:48 -0800636}
637
638void PointerData::PostForkParent() NO_THREAD_SAFETY_ANALYSIS {
639 frame_mutex_.unlock();
640 pointer_mutex_.unlock();
641 free_pointer_mutex_.unlock();
642}
643
644void PointerData::PostForkChild() __attribute__((no_thread_safety_analysis)) {
645 // Make sure that any potential mutexes have been released and are back
646 // to an initial state.
647 frame_mutex_.try_lock();
648 frame_mutex_.unlock();
649 pointer_mutex_.try_lock();
650 pointer_mutex_.unlock();
651 free_pointer_mutex_.try_lock();
652 free_pointer_mutex_.unlock();
653}
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700654
655void PointerData::IteratePointers(std::function<void(uintptr_t pointer)> fn) {
656 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
657 for (const auto entry : pointers_) {
658 fn(DemanglePointer(entry.first));
659 }
660}