blob: 638061b11686ba9c070dda00f388614dc5623c39 [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
Christopher Ferris4da25032018-03-07 13:38:48 -0800269void PointerData::LogFreeError(const FreePointerInfoType& info, size_t usable_size) {
270 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();
274 for (size_t i = 0; i < usable_size; i++) {
275 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();
317 const uint8_t* memory = reinterpret_cast<const uint8_t*>(info.pointer);
318 while (bytes > 0) {
319 size_t bytes_to_cmp = (bytes < g_cmp_mem.size()) ? bytes : g_cmp_mem.size();
320 if (memcmp(memory, g_cmp_mem.data(), bytes_to_cmp) != 0) {
321 LogFreeError(info, usable_size);
322 }
323 bytes -= bytes_to_cmp;
324 memory = &memory[bytes_to_cmp];
325 }
326}
327
328void* PointerData::AddFreed(const void* ptr) {
329 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
330
331 size_t hash_index = 0;
332 size_t num_frames = g_debug->config().free_track_backtrace_num_frames();
333 if (num_frames) {
334 hash_index = AddBacktrace(num_frames);
335 }
336
337 void* last = nullptr;
338 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
339 if (free_pointers_.size() == g_debug->config().free_track_allocations()) {
340 FreePointerInfoType info(free_pointers_.front());
341 free_pointers_.pop_front();
342 VerifyFreedPointer(info);
343 RemoveBacktrace(info.hash_index);
344 last = reinterpret_cast<void*>(info.pointer);
345 }
346
347 free_pointers_.emplace_back(FreePointerInfoType{pointer, hash_index});
348 return last;
349}
350
351void PointerData::LogFreeBacktrace(const void* ptr) {
352 size_t hash_index = 0;
353 {
354 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
355 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
356 for (const auto& info : free_pointers_) {
357 if (info.pointer == pointer) {
358 hash_index = info.hash_index;
359 break;
360 }
361 }
362 }
363
364 if (hash_index <= kBacktraceEmptyIndex) {
365 return;
366 }
367
Christopher Ferris4da25032018-03-07 13:38:48 -0800368 error_log("Backtrace of original free:");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700369 LogBacktrace(hash_index);
Christopher Ferris4da25032018-03-07 13:38:48 -0800370}
371
372void PointerData::VerifyAllFreed() {
373 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
374 for (auto& free_info : free_pointers_) {
375 VerifyFreedPointer(free_info);
376 }
377}
378
379void PointerData::GetList(std::vector<ListInfoType>* list, bool only_with_backtrace)
380 REQUIRES(pointer_mutex_, frame_mutex_) {
381 for (const auto& entry : pointers_) {
382 FrameInfoType* frame_info = nullptr;
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700383 std::vector<unwindstack::LocalFrameData>* backtrace_info = nullptr;
Christopher Ferris4da25032018-03-07 13:38:48 -0800384 size_t hash_index = entry.second.hash_index;
385 if (hash_index > kBacktraceEmptyIndex) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700386 auto frame_entry = frames_.find(hash_index);
387 if (frame_entry == frames_.end()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800388 // Somehow wound up with a pointer with a valid hash_index, but
389 // no frame data. This should not be possible since adding a pointer
390 // occurs after the hash_index and frame data have been added.
391 // When removing a pointer, the pointer is deleted before the frame
392 // data.
Christopher Ferris4da25032018-03-07 13:38:48 -0800393 error_log("Pointer 0x%" PRIxPTR " hash_index %zu does not exist.", entry.first, hash_index);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700394 } else {
395 frame_info = &frame_entry->second;
396 }
397
398 if (g_debug->config().options() & BACKTRACE_FULL) {
399 auto backtrace_entry = backtraces_info_.find(hash_index);
400 if (backtrace_entry == backtraces_info_.end()) {
401 error_log("Pointer 0x%" PRIxPTR " hash_index %zu does not exist.", entry.first, hash_index);
402 } else {
403 backtrace_info = &backtrace_entry->second;
404 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800405 }
406 }
407 if (hash_index == 0 && only_with_backtrace) {
408 continue;
409 }
410
411 list->emplace_back(ListInfoType{entry.first, 1, entry.second.RealSize(),
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700412 entry.second.ZygoteChildAlloc(), frame_info, backtrace_info});
Christopher Ferris4da25032018-03-07 13:38:48 -0800413 }
414
415 // Sort by the size of the allocation.
416 std::sort(list->begin(), list->end(), [](const ListInfoType& a, const ListInfoType& b) {
417 // Put zygote child allocations first.
418 bool a_zygote_child_alloc = a.zygote_child_alloc;
419 bool b_zygote_child_alloc = b.zygote_child_alloc;
420 if (a_zygote_child_alloc && !b_zygote_child_alloc) {
421 return false;
422 }
423 if (!a_zygote_child_alloc && b_zygote_child_alloc) {
424 return true;
425 }
426
427 // Sort by size, descending order.
428 if (a.size != b.size) return a.size > b.size;
429
430 // Put pointers with no backtrace last.
431 FrameInfoType* a_frame = a.frame_info;
432 FrameInfoType* b_frame = b.frame_info;
433 if (a_frame == nullptr && b_frame != nullptr) {
434 return false;
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700435 } else if (a_frame != nullptr && b_frame == nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800436 return true;
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700437 } else if (a_frame == nullptr && b_frame == nullptr) {
438 return a.pointer < b.pointer;
Christopher Ferris4da25032018-03-07 13:38:48 -0800439 }
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700440
Christopher Ferris4da25032018-03-07 13:38:48 -0800441 // Put the pointers with longest backtrace first.
442 if (a_frame->frames.size() != b_frame->frames.size()) {
443 return a_frame->frames.size() > b_frame->frames.size();
444 }
445
446 // Last sort by pointer.
447 return a.pointer < b.pointer;
448 });
449}
450
451void PointerData::GetUniqueList(std::vector<ListInfoType>* list, bool only_with_backtrace)
452 REQUIRES(pointer_mutex_, frame_mutex_) {
453 GetList(list, only_with_backtrace);
454
455 // Remove duplicates of size/backtraces.
456 for (auto iter = list->begin(); iter != list->end();) {
457 auto dup_iter = iter + 1;
458 bool zygote_child_alloc = iter->zygote_child_alloc;
459 size_t size = iter->size;
460 FrameInfoType* frame_info = iter->frame_info;
461 for (; dup_iter != list->end(); ++dup_iter) {
462 if (zygote_child_alloc != dup_iter->zygote_child_alloc || size != dup_iter->size ||
463 frame_info != dup_iter->frame_info) {
464 break;
465 }
466 iter->num_allocations++;
467 }
468 iter = list->erase(iter + 1, dup_iter);
469 }
470}
471
472void PointerData::LogLeaks() {
473 std::vector<ListInfoType> list;
474
475 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
476 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
477 GetList(&list, false);
478
479 size_t track_count = 0;
480 for (const auto& list_info : list) {
481 error_log("+++ %s leaked block of size %zu at 0x%" PRIxPTR " (leak %zu of %zu)", getprogname(),
482 list_info.size, list_info.pointer, ++track_count, list.size());
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700483 if (list_info.backtrace_info != nullptr) {
484 error_log("Backtrace at time of allocation:");
485 UnwindLog(*list_info.backtrace_info);
486 } else if (list_info.frame_info != nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800487 error_log("Backtrace at time of allocation:");
488 backtrace_log(list_info.frame_info->frames.data(), list_info.frame_info->frames.size());
489 }
490 // Do not bother to free the pointers, we are about to exit any way.
491 }
492}
493
494void PointerData::GetInfo(uint8_t** info, size_t* overall_size, size_t* info_size,
495 size_t* total_memory, size_t* backtrace_size) {
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 std::vector<ListInfoType> list;
504 GetUniqueList(&list, true);
505 if (list.empty()) {
506 return;
507 }
508
509 *backtrace_size = g_debug->config().backtrace_frames();
510 *info_size = sizeof(size_t) * 2 + sizeof(uintptr_t) * *backtrace_size;
511 *overall_size = *info_size * list.size();
512 *info = reinterpret_cast<uint8_t*>(g_dispatch->calloc(*info_size, list.size()));
513 if (*info == nullptr) {
514 return;
515 }
516
517 uint8_t* data = *info;
518 *total_memory = 0;
519 for (const auto& list_info : list) {
520 FrameInfoType* frame_info = list_info.frame_info;
521 *total_memory += list_info.size * list_info.num_allocations;
522 size_t allocation_size =
523 PointerInfoType::GetEncodedSize(list_info.zygote_child_alloc, list_info.size);
524 memcpy(data, &allocation_size, sizeof(size_t));
525 memcpy(&data[sizeof(size_t)], &list_info.num_allocations, sizeof(size_t));
526 if (frame_info != nullptr) {
527 memcpy(&data[2 * sizeof(size_t)], frame_info->frames.data(),
528 frame_info->frames.size() * sizeof(uintptr_t));
529 }
530 data += *info_size;
531 }
532}
533
534bool PointerData::Exists(const void* ptr) {
535 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
536 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
537 return pointers_.count(pointer) != 0;
538}
539
540void PointerData::DumpLiveToFile(FILE* fp) {
541 std::vector<ListInfoType> list;
542
543 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
544 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
545 GetUniqueList(&list, false);
546
547 size_t total_memory = 0;
548 for (const auto& info : list) {
549 total_memory += info.size * info.num_allocations;
550 }
551
552 fprintf(fp, "Total memory: %zu\n", total_memory);
553 fprintf(fp, "Allocation records: %zd\n", list.size());
554 fprintf(fp, "Backtrace size: %zu\n", g_debug->config().backtrace_frames());
555 fprintf(fp, "\n");
556
557 for (const auto& info : list) {
558 fprintf(fp, "z %d sz %8zu num %zu bt", (info.zygote_child_alloc) ? 1 : 0, info.size,
559 info.num_allocations);
560 FrameInfoType* frame_info = info.frame_info;
561 if (frame_info != nullptr) {
562 for (size_t i = 0; i < frame_info->frames.size(); i++) {
563 if (frame_info->frames[i] == 0) {
564 break;
565 }
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700566 fprintf(fp, " %" PRIxPTR, frame_info->frames[i]);
Christopher Ferris4da25032018-03-07 13:38:48 -0800567 }
568 }
569 fprintf(fp, "\n");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700570 if (info.backtrace_info != nullptr) {
571 fprintf(fp, " bt_info");
572 for (const auto& frame : *info.backtrace_info) {
573 fprintf(fp, " {");
574 if (frame.map_info != nullptr && !frame.map_info->name.empty()) {
575 fprintf(fp, "\"%s\"", frame.map_info->name.c_str());
576 } else {
577 fprintf(fp, "\"\"");
578 }
579 fprintf(fp, " %" PRIx64, frame.rel_pc);
580 if (frame.function_name.empty()) {
581 fprintf(fp, " \"\" 0}");
582 } else {
583 fprintf(fp, " \"%s\" %" PRIx64 "}", demangle(frame.function_name.c_str()).c_str(), frame.function_offset);
584 }
585 }
586 fprintf(fp, "\n");
587 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800588 }
589}
590
591void PointerData::PrepareFork() NO_THREAD_SAFETY_ANALYSIS {
592 pointer_mutex_.lock();
593 frame_mutex_.lock();
594 free_pointer_mutex_.lock();
595}
596
597void PointerData::PostForkParent() NO_THREAD_SAFETY_ANALYSIS {
598 frame_mutex_.unlock();
599 pointer_mutex_.unlock();
600 free_pointer_mutex_.unlock();
601}
602
603void PointerData::PostForkChild() __attribute__((no_thread_safety_analysis)) {
604 // Make sure that any potential mutexes have been released and are back
605 // to an initial state.
606 frame_mutex_.try_lock();
607 frame_mutex_.unlock();
608 pointer_mutex_.try_lock();
609 pointer_mutex_.unlock();
610 free_pointer_mutex_.try_lock();
611 free_pointer_mutex_.unlock();
612}