blob: d062086f5ea426283505e4d689e28dc7350ae343 [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
Christopher Ferrisf78486f2022-05-04 14:08:54 -070038#include <functional>
Christopher Ferris4da25032018-03-07 13:38:48 -080039#include <mutex>
40#include <string>
41#include <unordered_map>
42#include <utility>
43#include <vector>
44
45#include <android-base/stringprintf.h>
46#include <android-base/thread_annotations.h>
Josh Gao4956c372019-12-19 16:35:51 -080047#include <platform/bionic/macros.h>
Christopher Ferris4da25032018-03-07 13:38:48 -080048
49#include "Config.h"
50#include "DebugData.h"
51#include "PointerData.h"
52#include "backtrace.h"
53#include "debug_log.h"
54#include "malloc_debug.h"
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070055#include "UnwindBacktrace.h"
Christopher Ferris4da25032018-03-07 13:38:48 -080056
Christopher Ferris9782b872019-07-18 13:36:50 -070057extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*);
58
Christopher Ferris4da25032018-03-07 13:38:48 -080059std::atomic_uint8_t PointerData::backtrace_enabled_;
60std::atomic_bool PointerData::backtrace_dump_;
61
62std::mutex PointerData::pointer_mutex_;
63std::unordered_map<uintptr_t, PointerInfoType> PointerData::pointers_ GUARDED_BY(
64 PointerData::pointer_mutex_);
65
66std::mutex PointerData::frame_mutex_;
67std::unordered_map<FrameKeyType, size_t> PointerData::key_to_index_ GUARDED_BY(
68 PointerData::frame_mutex_);
69std::unordered_map<size_t, FrameInfoType> PointerData::frames_ GUARDED_BY(PointerData::frame_mutex_);
Christopher Ferris459eecb2022-01-07 13:38:10 -080070std::unordered_map<size_t, std::vector<unwindstack::FrameData>> PointerData::backtraces_info_
71 GUARDED_BY(PointerData::frame_mutex_);
Christopher Ferris4da25032018-03-07 13:38:48 -080072constexpr size_t kBacktraceEmptyIndex = 1;
73size_t PointerData::cur_hash_index_ GUARDED_BY(PointerData::frame_mutex_);
74
75std::mutex PointerData::free_pointer_mutex_;
76std::deque<FreePointerInfoType> PointerData::free_pointers_ GUARDED_BY(
77 PointerData::free_pointer_mutex_);
78
79// Buffer to use for comparison.
80static constexpr size_t kCompareBufferSize = 512 * 1024;
81static std::vector<uint8_t> g_cmp_mem(0);
82
83static void ToggleBacktraceEnable(int, siginfo_t*, void*) {
84 g_debug->pointer->ToggleBacktraceEnabled();
85}
86
87static void EnableDump(int, siginfo_t*, void*) {
88 g_debug->pointer->EnableDumping();
89}
90
91PointerData::PointerData(DebugData* debug_data) : OptionData(debug_data) {}
92
93bool PointerData::Initialize(const Config& config) NO_THREAD_SAFETY_ANALYSIS {
94 pointers_.clear();
95 key_to_index_.clear();
96 frames_.clear();
97 free_pointers_.clear();
98 // A hash index of kBacktraceEmptyIndex indicates that we tried to get
99 // a backtrace, but there was nothing recorded.
100 cur_hash_index_ = kBacktraceEmptyIndex + 1;
101
102 backtrace_enabled_ = config.backtrace_enabled();
103 if (config.backtrace_enable_on_signal()) {
104 struct sigaction64 enable_act = {};
105 enable_act.sa_sigaction = ToggleBacktraceEnable;
106 enable_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
107 if (sigaction64(config.backtrace_signal(), &enable_act, nullptr) != 0) {
108 error_log("Unable to set up backtrace signal enable function: %s", strerror(errno));
109 return false;
110 }
Christopher Ferrisc328e442019-04-01 19:31:26 -0700111 if (config.options() & VERBOSE) {
112 info_log("%s: Run: 'kill -%d %d' to enable backtracing.", getprogname(),
113 config.backtrace_signal(), getpid());
114 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800115 }
116
117 if (config.options() & BACKTRACE) {
118 struct sigaction64 act = {};
119 act.sa_sigaction = EnableDump;
120 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
121 if (sigaction64(config.backtrace_dump_signal(), &act, nullptr) != 0) {
122 error_log("Unable to set up backtrace dump signal function: %s", strerror(errno));
123 return false;
124 }
Christopher Ferrisc328e442019-04-01 19:31:26 -0700125 if (config.options() & VERBOSE) {
126 info_log("%s: Run: 'kill -%d %d' to dump the backtrace.", getprogname(),
127 config.backtrace_dump_signal(), getpid());
128 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800129 }
130
131 backtrace_dump_ = false;
132
133 if (config.options() & FREE_TRACK) {
134 g_cmp_mem.resize(kCompareBufferSize, config.fill_free_value());
135 }
136 return true;
137}
138
139size_t PointerData::AddBacktrace(size_t num_frames) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700140 std::vector<uintptr_t> frames;
Christopher Ferris459eecb2022-01-07 13:38:10 -0800141 std::vector<unwindstack::FrameData> frames_info;
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700142 if (g_debug->config().options() & BACKTRACE_FULL) {
143 if (!Unwind(&frames, &frames_info, num_frames)) {
144 return kBacktraceEmptyIndex;
145 }
146 } else {
147 frames.resize(num_frames);
148 num_frames = backtrace_get(frames.data(), frames.size());
149 if (num_frames == 0) {
150 return kBacktraceEmptyIndex;
151 }
Christopher Ferrisdfbc59a2022-03-23 12:22:36 -0700152 frames.resize(num_frames);
Christopher Ferris4da25032018-03-07 13:38:48 -0800153 }
154
Christopher Ferrisdfbc59a2022-03-23 12:22:36 -0700155 FrameKeyType key{.num_frames = frames.size(), .frames = frames.data()};
Christopher Ferris4da25032018-03-07 13:38:48 -0800156 size_t hash_index;
157 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
158 auto entry = key_to_index_.find(key);
159 if (entry == key_to_index_.end()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800160 hash_index = cur_hash_index_++;
161 key.frames = frames.data();
162 key_to_index_.emplace(key, hash_index);
163
164 frames_.emplace(hash_index, FrameInfoType{.references = 1, .frames = std::move(frames)});
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700165 if (g_debug->config().options() & BACKTRACE_FULL) {
166 backtraces_info_.emplace(hash_index, std::move(frames_info));
167 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800168 } else {
169 hash_index = entry->second;
170 FrameInfoType* frame_info = &frames_[hash_index];
171 frame_info->references++;
172 }
173 return hash_index;
174}
175
176void PointerData::RemoveBacktrace(size_t hash_index) {
177 if (hash_index <= kBacktraceEmptyIndex) {
178 return;
179 }
180
181 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
182 auto frame_entry = frames_.find(hash_index);
183 if (frame_entry == frames_.end()) {
184 error_log("hash_index %zu does not have matching frame data.", hash_index);
185 return;
186 }
187 FrameInfoType* frame_info = &frame_entry->second;
188 if (--frame_info->references == 0) {
189 FrameKeyType key{.num_frames = frame_info->frames.size(), .frames = frame_info->frames.data()};
190 key_to_index_.erase(key);
191 frames_.erase(hash_index);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700192 if (g_debug->config().options() & BACKTRACE_FULL) {
193 backtraces_info_.erase(hash_index);
194 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800195 }
196}
197
198void PointerData::Add(const void* ptr, size_t pointer_size) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800199 size_t hash_index = 0;
200 if (backtrace_enabled_) {
201 hash_index = AddBacktrace(g_debug->config().backtrace_frames());
202 }
203
204 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700205 uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr));
206 pointers_[mangled_ptr] =
207 PointerInfoType{PointerInfoType::GetEncodedSize(pointer_size), hash_index};
Christopher Ferris4da25032018-03-07 13:38:48 -0800208}
209
210void PointerData::Remove(const void* ptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800211 size_t hash_index;
212 {
213 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700214 uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr));
215 auto entry = pointers_.find(mangled_ptr);
Christopher Ferris4da25032018-03-07 13:38:48 -0800216 if (entry == pointers_.end()) {
Iris Chang7f209a92019-01-16 11:17:15 +0800217 // Attempt to remove unknown pointer.
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700218 error_log("No tracked pointer found for 0x%" PRIxPTR, DemanglePointer(mangled_ptr));
Christopher Ferris4da25032018-03-07 13:38:48 -0800219 return;
220 }
221 hash_index = entry->second.hash_index;
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700222 pointers_.erase(mangled_ptr);
Christopher Ferris4da25032018-03-07 13:38:48 -0800223 }
224
225 RemoveBacktrace(hash_index);
226}
227
228size_t PointerData::GetFrames(const void* ptr, uintptr_t* frames, size_t max_frames) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800229 size_t hash_index;
230 {
231 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700232 uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr));
233 auto entry = pointers_.find(mangled_ptr);
Christopher Ferris4da25032018-03-07 13:38:48 -0800234 if (entry == pointers_.end()) {
235 return 0;
236 }
237 hash_index = entry->second.hash_index;
238 }
239
240 if (hash_index <= kBacktraceEmptyIndex) {
241 return 0;
242 }
243
244 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
245 auto frame_entry = frames_.find(hash_index);
246 if (frame_entry == frames_.end()) {
247 return 0;
248 }
249 FrameInfoType* frame_info = &frame_entry->second;
250 if (max_frames > frame_info->frames.size()) {
251 max_frames = frame_info->frames.size();
252 }
253 memcpy(frames, &frame_info->frames[0], max_frames * sizeof(uintptr_t));
254
255 return max_frames;
256}
257
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700258void PointerData::LogBacktrace(size_t hash_index) {
259 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
260 if (g_debug->config().options() & BACKTRACE_FULL) {
261 auto backtrace_info_entry = backtraces_info_.find(hash_index);
262 if (backtrace_info_entry != backtraces_info_.end()) {
263 UnwindLog(backtrace_info_entry->second);
264 return;
265 }
266 } else {
267 auto frame_entry = frames_.find(hash_index);
268 if (frame_entry != frames_.end()) {
269 FrameInfoType* frame_info = &frame_entry->second;
270 backtrace_log(frame_info->frames.data(), frame_info->frames.size());
271 return;
272 }
273 }
274 error_log(" hash_index %zu does not have matching frame data.", hash_index);
275}
276
Iris Changb3441502019-02-12 14:00:59 +0800277void PointerData::LogFreeError(const FreePointerInfoType& info, size_t max_cmp_bytes) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800278 error_log(LOG_DIVIDER);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700279 uintptr_t pointer = DemanglePointer(info.mangled_ptr);
280 uint8_t* memory = reinterpret_cast<uint8_t*>(pointer);
Christopher Ferris4da25032018-03-07 13:38:48 -0800281 error_log("+++ ALLOCATION %p USED AFTER FREE", memory);
282 uint8_t fill_free_value = g_debug->config().fill_free_value();
Iris Changb3441502019-02-12 14:00:59 +0800283 for (size_t i = 0; i < max_cmp_bytes; i++) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800284 if (memory[i] != fill_free_value) {
285 error_log(" allocation[%zu] = 0x%02x (expected 0x%02x)", i, memory[i], fill_free_value);
286 }
287 }
288
289 if (info.hash_index > kBacktraceEmptyIndex) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700290 error_log("Backtrace at time of free:");
291 LogBacktrace(info.hash_index);
Christopher Ferris4da25032018-03-07 13:38:48 -0800292 }
293
294 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +0800295 if (g_debug->config().options() & ABORT_ON_ERROR) {
296 abort();
297 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800298}
299
300void PointerData::VerifyFreedPointer(const FreePointerInfoType& info) {
301 size_t usable_size;
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700302 uintptr_t pointer = DemanglePointer(info.mangled_ptr);
Christopher Ferris4da25032018-03-07 13:38:48 -0800303 if (g_debug->HeaderEnabled()) {
304 // Check to see if the tag data has been damaged.
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700305 Header* header = g_debug->GetHeader(reinterpret_cast<const void*>(pointer));
Christopher Ferris4da25032018-03-07 13:38:48 -0800306 if (header->tag != DEBUG_FREE_TAG) {
307 error_log(LOG_DIVIDER);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700308 error_log("+++ ALLOCATION 0x%" PRIxPTR " HAS CORRUPTED HEADER TAG 0x%x AFTER FREE", pointer,
309 header->tag);
Christopher Ferris4da25032018-03-07 13:38:48 -0800310 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +0800311 if (g_debug->config().options() & ABORT_ON_ERROR) {
312 abort();
313 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800314
315 // Stop processing here, it is impossible to tell how the header
316 // may have been damaged.
317 return;
318 }
319 usable_size = header->usable_size;
320 } else {
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700321 usable_size = g_dispatch->malloc_usable_size(reinterpret_cast<const void*>(pointer));
Christopher Ferris4da25032018-03-07 13:38:48 -0800322 }
323
324 size_t bytes = (usable_size < g_debug->config().fill_on_free_bytes())
325 ? usable_size
326 : g_debug->config().fill_on_free_bytes();
Iris Changb3441502019-02-12 14:00:59 +0800327 size_t max_cmp_bytes = bytes;
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700328 const uint8_t* memory = reinterpret_cast<const uint8_t*>(pointer);
Christopher Ferris4da25032018-03-07 13:38:48 -0800329 while (bytes > 0) {
330 size_t bytes_to_cmp = (bytes < g_cmp_mem.size()) ? bytes : g_cmp_mem.size();
331 if (memcmp(memory, g_cmp_mem.data(), bytes_to_cmp) != 0) {
Iris Changb3441502019-02-12 14:00:59 +0800332 LogFreeError(info, max_cmp_bytes);
Christopher Ferris4da25032018-03-07 13:38:48 -0800333 }
334 bytes -= bytes_to_cmp;
335 memory = &memory[bytes_to_cmp];
336 }
337}
338
339void* PointerData::AddFreed(const void* ptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800340 size_t hash_index = 0;
341 size_t num_frames = g_debug->config().free_track_backtrace_num_frames();
342 if (num_frames) {
343 hash_index = AddBacktrace(num_frames);
344 }
345
346 void* last = nullptr;
347 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
348 if (free_pointers_.size() == g_debug->config().free_track_allocations()) {
349 FreePointerInfoType info(free_pointers_.front());
350 free_pointers_.pop_front();
351 VerifyFreedPointer(info);
352 RemoveBacktrace(info.hash_index);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700353 last = reinterpret_cast<void*>(DemanglePointer(info.mangled_ptr));
Christopher Ferris4da25032018-03-07 13:38:48 -0800354 }
355
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700356 uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr));
357 free_pointers_.emplace_back(FreePointerInfoType{mangled_ptr, hash_index});
Christopher Ferris4da25032018-03-07 13:38:48 -0800358 return last;
359}
360
361void PointerData::LogFreeBacktrace(const void* ptr) {
362 size_t hash_index = 0;
363 {
364 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
365 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
366 for (const auto& info : free_pointers_) {
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700367 if (DemanglePointer(info.mangled_ptr) == pointer) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800368 hash_index = info.hash_index;
369 break;
370 }
371 }
372 }
373
374 if (hash_index <= kBacktraceEmptyIndex) {
375 return;
376 }
377
Christopher Ferris4da25032018-03-07 13:38:48 -0800378 error_log("Backtrace of original free:");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700379 LogBacktrace(hash_index);
Christopher Ferris4da25032018-03-07 13:38:48 -0800380}
381
382void PointerData::VerifyAllFreed() {
383 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
384 for (auto& free_info : free_pointers_) {
385 VerifyFreedPointer(free_info);
386 }
387}
388
389void PointerData::GetList(std::vector<ListInfoType>* list, bool only_with_backtrace)
390 REQUIRES(pointer_mutex_, frame_mutex_) {
391 for (const auto& entry : pointers_) {
392 FrameInfoType* frame_info = nullptr;
Christopher Ferris459eecb2022-01-07 13:38:10 -0800393 std::vector<unwindstack::FrameData>* backtrace_info = nullptr;
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700394 uintptr_t pointer = DemanglePointer(entry.first);
Christopher Ferris4da25032018-03-07 13:38:48 -0800395 size_t hash_index = entry.second.hash_index;
396 if (hash_index > kBacktraceEmptyIndex) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700397 auto frame_entry = frames_.find(hash_index);
398 if (frame_entry == frames_.end()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800399 // Somehow wound up with a pointer with a valid hash_index, but
400 // no frame data. This should not be possible since adding a pointer
401 // occurs after the hash_index and frame data have been added.
402 // When removing a pointer, the pointer is deleted before the frame
403 // data.
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700404 error_log("Pointer 0x%" PRIxPTR " hash_index %zu does not exist.", pointer, hash_index);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700405 } else {
406 frame_info = &frame_entry->second;
407 }
408
409 if (g_debug->config().options() & BACKTRACE_FULL) {
410 auto backtrace_entry = backtraces_info_.find(hash_index);
411 if (backtrace_entry == backtraces_info_.end()) {
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700412 error_log("Pointer 0x%" PRIxPTR " hash_index %zu does not exist.", pointer, hash_index);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700413 } else {
414 backtrace_info = &backtrace_entry->second;
415 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800416 }
417 }
418 if (hash_index == 0 && only_with_backtrace) {
419 continue;
420 }
421
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700422 list->emplace_back(ListInfoType{pointer, 1, entry.second.RealSize(),
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700423 entry.second.ZygoteChildAlloc(), frame_info, backtrace_info});
Christopher Ferris4da25032018-03-07 13:38:48 -0800424 }
425
426 // Sort by the size of the allocation.
427 std::sort(list->begin(), list->end(), [](const ListInfoType& a, const ListInfoType& b) {
428 // Put zygote child allocations first.
429 bool a_zygote_child_alloc = a.zygote_child_alloc;
430 bool b_zygote_child_alloc = b.zygote_child_alloc;
431 if (a_zygote_child_alloc && !b_zygote_child_alloc) {
432 return false;
433 }
434 if (!a_zygote_child_alloc && b_zygote_child_alloc) {
435 return true;
436 }
437
438 // Sort by size, descending order.
439 if (a.size != b.size) return a.size > b.size;
440
441 // Put pointers with no backtrace last.
442 FrameInfoType* a_frame = a.frame_info;
443 FrameInfoType* b_frame = b.frame_info;
444 if (a_frame == nullptr && b_frame != nullptr) {
445 return false;
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700446 } else if (a_frame != nullptr && b_frame == nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800447 return true;
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700448 } else if (a_frame == nullptr && b_frame == nullptr) {
449 return a.pointer < b.pointer;
Christopher Ferris4da25032018-03-07 13:38:48 -0800450 }
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700451
Christopher Ferris4da25032018-03-07 13:38:48 -0800452 // Put the pointers with longest backtrace first.
453 if (a_frame->frames.size() != b_frame->frames.size()) {
454 return a_frame->frames.size() > b_frame->frames.size();
455 }
456
457 // Last sort by pointer.
458 return a.pointer < b.pointer;
459 });
460}
461
462void PointerData::GetUniqueList(std::vector<ListInfoType>* list, bool only_with_backtrace)
463 REQUIRES(pointer_mutex_, frame_mutex_) {
464 GetList(list, only_with_backtrace);
465
466 // Remove duplicates of size/backtraces.
467 for (auto iter = list->begin(); iter != list->end();) {
468 auto dup_iter = iter + 1;
469 bool zygote_child_alloc = iter->zygote_child_alloc;
470 size_t size = iter->size;
471 FrameInfoType* frame_info = iter->frame_info;
472 for (; dup_iter != list->end(); ++dup_iter) {
473 if (zygote_child_alloc != dup_iter->zygote_child_alloc || size != dup_iter->size ||
474 frame_info != dup_iter->frame_info) {
475 break;
476 }
477 iter->num_allocations++;
478 }
479 iter = list->erase(iter + 1, dup_iter);
480 }
481}
482
483void PointerData::LogLeaks() {
484 std::vector<ListInfoType> list;
485
486 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
487 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
488 GetList(&list, false);
489
490 size_t track_count = 0;
491 for (const auto& list_info : list) {
492 error_log("+++ %s leaked block of size %zu at 0x%" PRIxPTR " (leak %zu of %zu)", getprogname(),
493 list_info.size, list_info.pointer, ++track_count, list.size());
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700494 if (list_info.backtrace_info != nullptr) {
495 error_log("Backtrace at time of allocation:");
496 UnwindLog(*list_info.backtrace_info);
497 } else if (list_info.frame_info != nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800498 error_log("Backtrace at time of allocation:");
499 backtrace_log(list_info.frame_info->frames.data(), list_info.frame_info->frames.size());
500 }
501 // Do not bother to free the pointers, we are about to exit any way.
502 }
503}
504
Christopher Ferris6c619a02019-03-01 17:59:51 -0800505void PointerData::GetAllocList(std::vector<ListInfoType>* list) {
506 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
507 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
508
509 if (pointers_.empty()) {
510 return;
511 }
512
513 GetList(list, false);
514}
515
Christopher Ferris4da25032018-03-07 13:38:48 -0800516void PointerData::GetInfo(uint8_t** info, size_t* overall_size, size_t* info_size,
517 size_t* total_memory, size_t* backtrace_size) {
518 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
519 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
520
521 if (pointers_.empty()) {
522 return;
523 }
524
525 std::vector<ListInfoType> list;
526 GetUniqueList(&list, true);
527 if (list.empty()) {
528 return;
529 }
530
531 *backtrace_size = g_debug->config().backtrace_frames();
532 *info_size = sizeof(size_t) * 2 + sizeof(uintptr_t) * *backtrace_size;
533 *overall_size = *info_size * list.size();
534 *info = reinterpret_cast<uint8_t*>(g_dispatch->calloc(*info_size, list.size()));
535 if (*info == nullptr) {
536 return;
537 }
538
539 uint8_t* data = *info;
540 *total_memory = 0;
541 for (const auto& list_info : list) {
542 FrameInfoType* frame_info = list_info.frame_info;
543 *total_memory += list_info.size * list_info.num_allocations;
544 size_t allocation_size =
545 PointerInfoType::GetEncodedSize(list_info.zygote_child_alloc, list_info.size);
546 memcpy(data, &allocation_size, sizeof(size_t));
547 memcpy(&data[sizeof(size_t)], &list_info.num_allocations, sizeof(size_t));
548 if (frame_info != nullptr) {
549 memcpy(&data[2 * sizeof(size_t)], frame_info->frames.data(),
550 frame_info->frames.size() * sizeof(uintptr_t));
551 }
552 data += *info_size;
553 }
554}
555
556bool PointerData::Exists(const void* ptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800557 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700558 uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr));
559 return pointers_.count(mangled_ptr) != 0;
Christopher Ferris4da25032018-03-07 13:38:48 -0800560}
561
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800562void PointerData::DumpLiveToFile(int fd) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800563 std::vector<ListInfoType> list;
564
565 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
566 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
567 GetUniqueList(&list, false);
568
569 size_t total_memory = 0;
570 for (const auto& info : list) {
571 total_memory += info.size * info.num_allocations;
572 }
573
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800574 dprintf(fd, "Total memory: %zu\n", total_memory);
575 dprintf(fd, "Allocation records: %zd\n", list.size());
576 dprintf(fd, "Backtrace size: %zu\n", g_debug->config().backtrace_frames());
577 dprintf(fd, "\n");
Christopher Ferris4da25032018-03-07 13:38:48 -0800578
579 for (const auto& info : list) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800580 dprintf(fd, "z %d sz %8zu num %zu bt", (info.zygote_child_alloc) ? 1 : 0, info.size,
Christopher Ferris4da25032018-03-07 13:38:48 -0800581 info.num_allocations);
582 FrameInfoType* frame_info = info.frame_info;
583 if (frame_info != nullptr) {
584 for (size_t i = 0; i < frame_info->frames.size(); i++) {
585 if (frame_info->frames[i] == 0) {
586 break;
587 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800588 dprintf(fd, " %" PRIxPTR, frame_info->frames[i]);
Christopher Ferris4da25032018-03-07 13:38:48 -0800589 }
590 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800591 dprintf(fd, "\n");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700592 if (info.backtrace_info != nullptr) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800593 dprintf(fd, " bt_info");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700594 for (const auto& frame : *info.backtrace_info) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800595 dprintf(fd, " {");
David Srbecky92b8d642021-05-13 00:03:26 +0100596 if (frame.map_info != nullptr && !frame.map_info->name().empty()) {
597 dprintf(fd, "\"%s\"", frame.map_info->name().c_str());
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700598 } else {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800599 dprintf(fd, "\"\"");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700600 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800601 dprintf(fd, " %" PRIx64, frame.rel_pc);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700602 if (frame.function_name.empty()) {
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800603 dprintf(fd, " \"\" 0}");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700604 } else {
Christopher Ferris9782b872019-07-18 13:36:50 -0700605 char* demangled_name = __cxa_demangle(frame.function_name.c_str(), nullptr, nullptr,
606 nullptr);
607 const char* name;
608 if (demangled_name != nullptr) {
609 name = demangled_name;
610 } else {
611 name = frame.function_name.c_str();
612 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800613 dprintf(fd, " \"%s\" %" PRIx64 "}", name, frame.function_offset);
Christopher Ferris9782b872019-07-18 13:36:50 -0700614 free(demangled_name);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700615 }
616 }
Christopher Ferrisff88fb02019-11-04 18:40:00 -0800617 dprintf(fd, "\n");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700618 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800619 }
620}
621
622void PointerData::PrepareFork() NO_THREAD_SAFETY_ANALYSIS {
Iris Chang76dcc472019-03-07 12:32:19 +0800623 free_pointer_mutex_.lock();
Christopher Ferris4da25032018-03-07 13:38:48 -0800624 pointer_mutex_.lock();
625 frame_mutex_.lock();
Christopher Ferris4da25032018-03-07 13:38:48 -0800626}
627
628void PointerData::PostForkParent() NO_THREAD_SAFETY_ANALYSIS {
629 frame_mutex_.unlock();
630 pointer_mutex_.unlock();
631 free_pointer_mutex_.unlock();
632}
633
634void PointerData::PostForkChild() __attribute__((no_thread_safety_analysis)) {
635 // Make sure that any potential mutexes have been released and are back
636 // to an initial state.
637 frame_mutex_.try_lock();
638 frame_mutex_.unlock();
639 pointer_mutex_.try_lock();
640 pointer_mutex_.unlock();
641 free_pointer_mutex_.try_lock();
642 free_pointer_mutex_.unlock();
643}
Christopher Ferrisf78486f2022-05-04 14:08:54 -0700644
645void PointerData::IteratePointers(std::function<void(uintptr_t pointer)> fn) {
646 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
647 for (const auto entry : pointers_) {
648 fn(DemanglePointer(entry.first));
649 }
650}