blob: 5542c1e9fa2df56b8e753fe43d5d2d0e2d2e4fd0 [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
38#include <mutex>
39#include <string>
40#include <unordered_map>
41#include <utility>
42#include <vector>
43
44#include <android-base/stringprintf.h>
45#include <android-base/thread_annotations.h>
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070046#include <demangle.h>
Christopher Ferris4da25032018-03-07 13:38:48 -080047#include <private/bionic_macros.h>
48
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
57std::atomic_uint8_t PointerData::backtrace_enabled_;
58std::atomic_bool PointerData::backtrace_dump_;
59
60std::mutex PointerData::pointer_mutex_;
61std::unordered_map<uintptr_t, PointerInfoType> PointerData::pointers_ GUARDED_BY(
62 PointerData::pointer_mutex_);
63
64std::mutex PointerData::frame_mutex_;
65std::unordered_map<FrameKeyType, size_t> PointerData::key_to_index_ GUARDED_BY(
66 PointerData::frame_mutex_);
67std::unordered_map<size_t, FrameInfoType> PointerData::frames_ GUARDED_BY(PointerData::frame_mutex_);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -070068std::unordered_map<size_t, std::vector<unwindstack::LocalFrameData>> PointerData::backtraces_info_ GUARDED_BY(PointerData::frame_mutex_);
Christopher Ferris4da25032018-03-07 13:38:48 -080069constexpr size_t kBacktraceEmptyIndex = 1;
70size_t PointerData::cur_hash_index_ GUARDED_BY(PointerData::frame_mutex_);
71
72std::mutex PointerData::free_pointer_mutex_;
73std::deque<FreePointerInfoType> PointerData::free_pointers_ GUARDED_BY(
74 PointerData::free_pointer_mutex_);
75
76// Buffer to use for comparison.
77static constexpr size_t kCompareBufferSize = 512 * 1024;
78static std::vector<uint8_t> g_cmp_mem(0);
79
80static void ToggleBacktraceEnable(int, siginfo_t*, void*) {
81 g_debug->pointer->ToggleBacktraceEnabled();
82}
83
84static void EnableDump(int, siginfo_t*, void*) {
85 g_debug->pointer->EnableDumping();
86}
87
88PointerData::PointerData(DebugData* debug_data) : OptionData(debug_data) {}
89
90bool PointerData::Initialize(const Config& config) NO_THREAD_SAFETY_ANALYSIS {
91 pointers_.clear();
92 key_to_index_.clear();
93 frames_.clear();
94 free_pointers_.clear();
95 // A hash index of kBacktraceEmptyIndex indicates that we tried to get
96 // a backtrace, but there was nothing recorded.
97 cur_hash_index_ = kBacktraceEmptyIndex + 1;
98
99 backtrace_enabled_ = config.backtrace_enabled();
100 if (config.backtrace_enable_on_signal()) {
101 struct sigaction64 enable_act = {};
102 enable_act.sa_sigaction = ToggleBacktraceEnable;
103 enable_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
104 if (sigaction64(config.backtrace_signal(), &enable_act, nullptr) != 0) {
105 error_log("Unable to set up backtrace signal enable function: %s", strerror(errno));
106 return false;
107 }
108 info_log("%s: Run: 'kill -%d %d' to enable backtracing.", getprogname(),
109 config.backtrace_signal(), getpid());
110 }
111
112 if (config.options() & BACKTRACE) {
113 struct sigaction64 act = {};
114 act.sa_sigaction = EnableDump;
115 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
116 if (sigaction64(config.backtrace_dump_signal(), &act, nullptr) != 0) {
117 error_log("Unable to set up backtrace dump signal function: %s", strerror(errno));
118 return false;
119 }
120 info_log("%s: Run: 'kill -%d %d' to dump the backtrace.", getprogname(),
121 config.backtrace_dump_signal(), getpid());
122 }
123
124 backtrace_dump_ = false;
125
126 if (config.options() & FREE_TRACK) {
127 g_cmp_mem.resize(kCompareBufferSize, config.fill_free_value());
128 }
129 return true;
130}
131
132size_t PointerData::AddBacktrace(size_t num_frames) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700133 std::vector<uintptr_t> frames;
134 std::vector<unwindstack::LocalFrameData> frames_info;
135 if (g_debug->config().options() & BACKTRACE_FULL) {
136 if (!Unwind(&frames, &frames_info, num_frames)) {
137 return kBacktraceEmptyIndex;
138 }
139 } else {
140 frames.resize(num_frames);
141 num_frames = backtrace_get(frames.data(), frames.size());
142 if (num_frames == 0) {
143 return kBacktraceEmptyIndex;
144 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800145 }
146
147 FrameKeyType key{.num_frames = num_frames, .frames = frames.data()};
148 size_t hash_index;
149 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
150 auto entry = key_to_index_.find(key);
151 if (entry == key_to_index_.end()) {
152 frames.resize(num_frames);
153 hash_index = cur_hash_index_++;
154 key.frames = frames.data();
155 key_to_index_.emplace(key, hash_index);
156
157 frames_.emplace(hash_index, FrameInfoType{.references = 1, .frames = std::move(frames)});
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700158 if (g_debug->config().options() & BACKTRACE_FULL) {
159 backtraces_info_.emplace(hash_index, std::move(frames_info));
160 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800161 } else {
162 hash_index = entry->second;
163 FrameInfoType* frame_info = &frames_[hash_index];
164 frame_info->references++;
165 }
166 return hash_index;
167}
168
169void PointerData::RemoveBacktrace(size_t hash_index) {
170 if (hash_index <= kBacktraceEmptyIndex) {
171 return;
172 }
173
174 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
175 auto frame_entry = frames_.find(hash_index);
176 if (frame_entry == frames_.end()) {
177 error_log("hash_index %zu does not have matching frame data.", hash_index);
178 return;
179 }
180 FrameInfoType* frame_info = &frame_entry->second;
181 if (--frame_info->references == 0) {
182 FrameKeyType key{.num_frames = frame_info->frames.size(), .frames = frame_info->frames.data()};
183 key_to_index_.erase(key);
184 frames_.erase(hash_index);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700185 if (g_debug->config().options() & BACKTRACE_FULL) {
186 backtraces_info_.erase(hash_index);
187 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800188 }
189}
190
191void PointerData::Add(const void* ptr, size_t pointer_size) {
192 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
193 size_t hash_index = 0;
194 if (backtrace_enabled_) {
195 hash_index = AddBacktrace(g_debug->config().backtrace_frames());
196 }
197
198 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
199 pointers_[pointer] = PointerInfoType{PointerInfoType::GetEncodedSize(pointer_size), hash_index};
200}
201
202void PointerData::Remove(const void* ptr) {
203 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
204 size_t hash_index;
205 {
206 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
207 auto entry = pointers_.find(pointer);
208 if (entry == pointers_.end()) {
Iris Chang7f209a92019-01-16 11:17:15 +0800209 // Attempt to remove unknown pointer.
Christopher Ferris4da25032018-03-07 13:38:48 -0800210 error_log("No tracked pointer found for 0x%" PRIxPTR, pointer);
211 return;
212 }
213 hash_index = entry->second.hash_index;
214 pointers_.erase(pointer);
215 }
216
217 RemoveBacktrace(hash_index);
218}
219
220size_t PointerData::GetFrames(const void* ptr, uintptr_t* frames, size_t max_frames) {
221 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
222 size_t hash_index;
223 {
224 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
225 auto entry = pointers_.find(pointer);
226 if (entry == pointers_.end()) {
227 return 0;
228 }
229 hash_index = entry->second.hash_index;
230 }
231
232 if (hash_index <= kBacktraceEmptyIndex) {
233 return 0;
234 }
235
236 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
237 auto frame_entry = frames_.find(hash_index);
238 if (frame_entry == frames_.end()) {
239 return 0;
240 }
241 FrameInfoType* frame_info = &frame_entry->second;
242 if (max_frames > frame_info->frames.size()) {
243 max_frames = frame_info->frames.size();
244 }
245 memcpy(frames, &frame_info->frames[0], max_frames * sizeof(uintptr_t));
246
247 return max_frames;
248}
249
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700250void PointerData::LogBacktrace(size_t hash_index) {
251 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
252 if (g_debug->config().options() & BACKTRACE_FULL) {
253 auto backtrace_info_entry = backtraces_info_.find(hash_index);
254 if (backtrace_info_entry != backtraces_info_.end()) {
255 UnwindLog(backtrace_info_entry->second);
256 return;
257 }
258 } else {
259 auto frame_entry = frames_.find(hash_index);
260 if (frame_entry != frames_.end()) {
261 FrameInfoType* frame_info = &frame_entry->second;
262 backtrace_log(frame_info->frames.data(), frame_info->frames.size());
263 return;
264 }
265 }
266 error_log(" hash_index %zu does not have matching frame data.", hash_index);
267}
268
Iris Changb3441502019-02-12 14:00:59 +0800269void PointerData::LogFreeError(const FreePointerInfoType& info, size_t max_cmp_bytes) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800270 error_log(LOG_DIVIDER);
271 uint8_t* memory = reinterpret_cast<uint8_t*>(info.pointer);
272 error_log("+++ ALLOCATION %p USED AFTER FREE", memory);
273 uint8_t fill_free_value = g_debug->config().fill_free_value();
Iris Changb3441502019-02-12 14:00:59 +0800274 for (size_t i = 0; i < max_cmp_bytes; i++) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800275 if (memory[i] != fill_free_value) {
276 error_log(" allocation[%zu] = 0x%02x (expected 0x%02x)", i, memory[i], fill_free_value);
277 }
278 }
279
280 if (info.hash_index > kBacktraceEmptyIndex) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700281 error_log("Backtrace at time of free:");
282 LogBacktrace(info.hash_index);
Christopher Ferris4da25032018-03-07 13:38:48 -0800283 }
284
285 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +0800286 if (g_debug->config().options() & ABORT_ON_ERROR) {
287 abort();
288 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800289}
290
291void PointerData::VerifyFreedPointer(const FreePointerInfoType& info) {
292 size_t usable_size;
293 if (g_debug->HeaderEnabled()) {
294 // Check to see if the tag data has been damaged.
295 Header* header = g_debug->GetHeader(reinterpret_cast<const void*>(info.pointer));
296 if (header->tag != DEBUG_FREE_TAG) {
297 error_log(LOG_DIVIDER);
298 error_log("+++ ALLOCATION 0x%" PRIxPTR " HAS CORRUPTED HEADER TAG 0x%x AFTER FREE",
299 info.pointer, header->tag);
300 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +0800301 if (g_debug->config().options() & ABORT_ON_ERROR) {
302 abort();
303 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800304
305 // Stop processing here, it is impossible to tell how the header
306 // may have been damaged.
307 return;
308 }
309 usable_size = header->usable_size;
310 } else {
311 usable_size = g_dispatch->malloc_usable_size(reinterpret_cast<const void*>(info.pointer));
312 }
313
314 size_t bytes = (usable_size < g_debug->config().fill_on_free_bytes())
315 ? usable_size
316 : g_debug->config().fill_on_free_bytes();
Iris Changb3441502019-02-12 14:00:59 +0800317 size_t max_cmp_bytes = bytes;
Christopher Ferris4da25032018-03-07 13:38:48 -0800318 const uint8_t* memory = reinterpret_cast<const uint8_t*>(info.pointer);
319 while (bytes > 0) {
320 size_t bytes_to_cmp = (bytes < g_cmp_mem.size()) ? bytes : g_cmp_mem.size();
321 if (memcmp(memory, g_cmp_mem.data(), bytes_to_cmp) != 0) {
Iris Changb3441502019-02-12 14:00:59 +0800322 LogFreeError(info, max_cmp_bytes);
Christopher Ferris4da25032018-03-07 13:38:48 -0800323 }
324 bytes -= bytes_to_cmp;
325 memory = &memory[bytes_to_cmp];
326 }
327}
328
329void* PointerData::AddFreed(const void* ptr) {
330 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
331
332 size_t hash_index = 0;
333 size_t num_frames = g_debug->config().free_track_backtrace_num_frames();
334 if (num_frames) {
335 hash_index = AddBacktrace(num_frames);
336 }
337
338 void* last = nullptr;
339 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
340 if (free_pointers_.size() == g_debug->config().free_track_allocations()) {
341 FreePointerInfoType info(free_pointers_.front());
342 free_pointers_.pop_front();
343 VerifyFreedPointer(info);
344 RemoveBacktrace(info.hash_index);
345 last = reinterpret_cast<void*>(info.pointer);
346 }
347
348 free_pointers_.emplace_back(FreePointerInfoType{pointer, hash_index});
349 return last;
350}
351
352void PointerData::LogFreeBacktrace(const void* ptr) {
353 size_t hash_index = 0;
354 {
355 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
356 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
357 for (const auto& info : free_pointers_) {
358 if (info.pointer == pointer) {
359 hash_index = info.hash_index;
360 break;
361 }
362 }
363 }
364
365 if (hash_index <= kBacktraceEmptyIndex) {
366 return;
367 }
368
Christopher Ferris4da25032018-03-07 13:38:48 -0800369 error_log("Backtrace of original free:");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700370 LogBacktrace(hash_index);
Christopher Ferris4da25032018-03-07 13:38:48 -0800371}
372
373void PointerData::VerifyAllFreed() {
374 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
375 for (auto& free_info : free_pointers_) {
376 VerifyFreedPointer(free_info);
377 }
378}
379
380void PointerData::GetList(std::vector<ListInfoType>* list, bool only_with_backtrace)
381 REQUIRES(pointer_mutex_, frame_mutex_) {
382 for (const auto& entry : pointers_) {
383 FrameInfoType* frame_info = nullptr;
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700384 std::vector<unwindstack::LocalFrameData>* backtrace_info = nullptr;
Christopher Ferris4da25032018-03-07 13:38:48 -0800385 size_t hash_index = entry.second.hash_index;
386 if (hash_index > kBacktraceEmptyIndex) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700387 auto frame_entry = frames_.find(hash_index);
388 if (frame_entry == frames_.end()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800389 // Somehow wound up with a pointer with a valid hash_index, but
390 // no frame data. This should not be possible since adding a pointer
391 // occurs after the hash_index and frame data have been added.
392 // When removing a pointer, the pointer is deleted before the frame
393 // data.
Christopher Ferris4da25032018-03-07 13:38:48 -0800394 error_log("Pointer 0x%" PRIxPTR " hash_index %zu does not exist.", entry.first, hash_index);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700395 } else {
396 frame_info = &frame_entry->second;
397 }
398
399 if (g_debug->config().options() & BACKTRACE_FULL) {
400 auto backtrace_entry = backtraces_info_.find(hash_index);
401 if (backtrace_entry == backtraces_info_.end()) {
402 error_log("Pointer 0x%" PRIxPTR " hash_index %zu does not exist.", entry.first, hash_index);
403 } else {
404 backtrace_info = &backtrace_entry->second;
405 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800406 }
407 }
408 if (hash_index == 0 && only_with_backtrace) {
409 continue;
410 }
411
412 list->emplace_back(ListInfoType{entry.first, 1, entry.second.RealSize(),
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700413 entry.second.ZygoteChildAlloc(), frame_info, backtrace_info});
Christopher Ferris4da25032018-03-07 13:38:48 -0800414 }
415
416 // Sort by the size of the allocation.
417 std::sort(list->begin(), list->end(), [](const ListInfoType& a, const ListInfoType& b) {
418 // Put zygote child allocations first.
419 bool a_zygote_child_alloc = a.zygote_child_alloc;
420 bool b_zygote_child_alloc = b.zygote_child_alloc;
421 if (a_zygote_child_alloc && !b_zygote_child_alloc) {
422 return false;
423 }
424 if (!a_zygote_child_alloc && b_zygote_child_alloc) {
425 return true;
426 }
427
428 // Sort by size, descending order.
429 if (a.size != b.size) return a.size > b.size;
430
431 // Put pointers with no backtrace last.
432 FrameInfoType* a_frame = a.frame_info;
433 FrameInfoType* b_frame = b.frame_info;
434 if (a_frame == nullptr && b_frame != nullptr) {
435 return false;
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700436 } else if (a_frame != nullptr && b_frame == nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800437 return true;
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700438 } else if (a_frame == nullptr && b_frame == nullptr) {
439 return a.pointer < b.pointer;
Christopher Ferris4da25032018-03-07 13:38:48 -0800440 }
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700441
Christopher Ferris4da25032018-03-07 13:38:48 -0800442 // Put the pointers with longest backtrace first.
443 if (a_frame->frames.size() != b_frame->frames.size()) {
444 return a_frame->frames.size() > b_frame->frames.size();
445 }
446
447 // Last sort by pointer.
448 return a.pointer < b.pointer;
449 });
450}
451
452void PointerData::GetUniqueList(std::vector<ListInfoType>* list, bool only_with_backtrace)
453 REQUIRES(pointer_mutex_, frame_mutex_) {
454 GetList(list, only_with_backtrace);
455
456 // Remove duplicates of size/backtraces.
457 for (auto iter = list->begin(); iter != list->end();) {
458 auto dup_iter = iter + 1;
459 bool zygote_child_alloc = iter->zygote_child_alloc;
460 size_t size = iter->size;
461 FrameInfoType* frame_info = iter->frame_info;
462 for (; dup_iter != list->end(); ++dup_iter) {
463 if (zygote_child_alloc != dup_iter->zygote_child_alloc || size != dup_iter->size ||
464 frame_info != dup_iter->frame_info) {
465 break;
466 }
467 iter->num_allocations++;
468 }
469 iter = list->erase(iter + 1, dup_iter);
470 }
471}
472
473void PointerData::LogLeaks() {
474 std::vector<ListInfoType> list;
475
476 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
477 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
478 GetList(&list, false);
479
480 size_t track_count = 0;
481 for (const auto& list_info : list) {
482 error_log("+++ %s leaked block of size %zu at 0x%" PRIxPTR " (leak %zu of %zu)", getprogname(),
483 list_info.size, list_info.pointer, ++track_count, list.size());
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700484 if (list_info.backtrace_info != nullptr) {
485 error_log("Backtrace at time of allocation:");
486 UnwindLog(*list_info.backtrace_info);
487 } else if (list_info.frame_info != nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800488 error_log("Backtrace at time of allocation:");
489 backtrace_log(list_info.frame_info->frames.data(), list_info.frame_info->frames.size());
490 }
491 // Do not bother to free the pointers, we are about to exit any way.
492 }
493}
494
Christopher Ferris6c619a02019-03-01 17:59:51 -0800495void PointerData::GetAllocList(std::vector<ListInfoType>* list) {
496 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
497 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
498
499 if (pointers_.empty()) {
500 return;
501 }
502
503 GetList(list, false);
504}
505
Christopher Ferris4da25032018-03-07 13:38:48 -0800506void PointerData::GetInfo(uint8_t** info, size_t* overall_size, size_t* info_size,
507 size_t* total_memory, size_t* backtrace_size) {
508 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
509 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
510
511 if (pointers_.empty()) {
512 return;
513 }
514
515 std::vector<ListInfoType> list;
516 GetUniqueList(&list, true);
517 if (list.empty()) {
518 return;
519 }
520
521 *backtrace_size = g_debug->config().backtrace_frames();
522 *info_size = sizeof(size_t) * 2 + sizeof(uintptr_t) * *backtrace_size;
523 *overall_size = *info_size * list.size();
524 *info = reinterpret_cast<uint8_t*>(g_dispatch->calloc(*info_size, list.size()));
525 if (*info == nullptr) {
526 return;
527 }
528
529 uint8_t* data = *info;
530 *total_memory = 0;
531 for (const auto& list_info : list) {
532 FrameInfoType* frame_info = list_info.frame_info;
533 *total_memory += list_info.size * list_info.num_allocations;
534 size_t allocation_size =
535 PointerInfoType::GetEncodedSize(list_info.zygote_child_alloc, list_info.size);
536 memcpy(data, &allocation_size, sizeof(size_t));
537 memcpy(&data[sizeof(size_t)], &list_info.num_allocations, sizeof(size_t));
538 if (frame_info != nullptr) {
539 memcpy(&data[2 * sizeof(size_t)], frame_info->frames.data(),
540 frame_info->frames.size() * sizeof(uintptr_t));
541 }
542 data += *info_size;
543 }
544}
545
546bool PointerData::Exists(const void* ptr) {
547 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
548 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
549 return pointers_.count(pointer) != 0;
550}
551
552void PointerData::DumpLiveToFile(FILE* fp) {
553 std::vector<ListInfoType> list;
554
555 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
556 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
557 GetUniqueList(&list, false);
558
559 size_t total_memory = 0;
560 for (const auto& info : list) {
561 total_memory += info.size * info.num_allocations;
562 }
563
564 fprintf(fp, "Total memory: %zu\n", total_memory);
565 fprintf(fp, "Allocation records: %zd\n", list.size());
566 fprintf(fp, "Backtrace size: %zu\n", g_debug->config().backtrace_frames());
567 fprintf(fp, "\n");
568
569 for (const auto& info : list) {
570 fprintf(fp, "z %d sz %8zu num %zu bt", (info.zygote_child_alloc) ? 1 : 0, info.size,
571 info.num_allocations);
572 FrameInfoType* frame_info = info.frame_info;
573 if (frame_info != nullptr) {
574 for (size_t i = 0; i < frame_info->frames.size(); i++) {
575 if (frame_info->frames[i] == 0) {
576 break;
577 }
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700578 fprintf(fp, " %" PRIxPTR, frame_info->frames[i]);
Christopher Ferris4da25032018-03-07 13:38:48 -0800579 }
580 }
581 fprintf(fp, "\n");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700582 if (info.backtrace_info != nullptr) {
583 fprintf(fp, " bt_info");
584 for (const auto& frame : *info.backtrace_info) {
585 fprintf(fp, " {");
586 if (frame.map_info != nullptr && !frame.map_info->name.empty()) {
587 fprintf(fp, "\"%s\"", frame.map_info->name.c_str());
588 } else {
589 fprintf(fp, "\"\"");
590 }
591 fprintf(fp, " %" PRIx64, frame.rel_pc);
592 if (frame.function_name.empty()) {
593 fprintf(fp, " \"\" 0}");
594 } else {
595 fprintf(fp, " \"%s\" %" PRIx64 "}", demangle(frame.function_name.c_str()).c_str(), frame.function_offset);
596 }
597 }
598 fprintf(fp, "\n");
599 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800600 }
601}
602
603void PointerData::PrepareFork() NO_THREAD_SAFETY_ANALYSIS {
604 pointer_mutex_.lock();
605 frame_mutex_.lock();
606 free_pointer_mutex_.lock();
607}
608
609void PointerData::PostForkParent() NO_THREAD_SAFETY_ANALYSIS {
610 frame_mutex_.unlock();
611 pointer_mutex_.unlock();
612 free_pointer_mutex_.unlock();
613}
614
615void PointerData::PostForkChild() __attribute__((no_thread_safety_analysis)) {
616 // Make sure that any potential mutexes have been released and are back
617 // to an initial state.
618 frame_mutex_.try_lock();
619 frame_mutex_.unlock();
620 pointer_mutex_.try_lock();
621 pointer_mutex_.unlock();
622 free_pointer_mutex_.try_lock();
623 free_pointer_mutex_.unlock();
624}