blob: 8cc1a28919308232159253c1e28d0fc415aee1c7 [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 }
Christopher Ferrisc328e442019-04-01 19:31:26 -0700108 if (config.options() & VERBOSE) {
109 info_log("%s: Run: 'kill -%d %d' to enable backtracing.", getprogname(),
110 config.backtrace_signal(), getpid());
111 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800112 }
113
114 if (config.options() & BACKTRACE) {
115 struct sigaction64 act = {};
116 act.sa_sigaction = EnableDump;
117 act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK;
118 if (sigaction64(config.backtrace_dump_signal(), &act, nullptr) != 0) {
119 error_log("Unable to set up backtrace dump signal function: %s", strerror(errno));
120 return false;
121 }
Christopher Ferrisc328e442019-04-01 19:31:26 -0700122 if (config.options() & VERBOSE) {
123 info_log("%s: Run: 'kill -%d %d' to dump the backtrace.", getprogname(),
124 config.backtrace_dump_signal(), getpid());
125 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800126 }
127
128 backtrace_dump_ = false;
129
130 if (config.options() & FREE_TRACK) {
131 g_cmp_mem.resize(kCompareBufferSize, config.fill_free_value());
132 }
133 return true;
134}
135
136size_t PointerData::AddBacktrace(size_t num_frames) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700137 std::vector<uintptr_t> frames;
138 std::vector<unwindstack::LocalFrameData> frames_info;
139 if (g_debug->config().options() & BACKTRACE_FULL) {
140 if (!Unwind(&frames, &frames_info, num_frames)) {
141 return kBacktraceEmptyIndex;
142 }
143 } else {
144 frames.resize(num_frames);
145 num_frames = backtrace_get(frames.data(), frames.size());
146 if (num_frames == 0) {
147 return kBacktraceEmptyIndex;
148 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800149 }
150
151 FrameKeyType key{.num_frames = num_frames, .frames = frames.data()};
152 size_t hash_index;
153 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
154 auto entry = key_to_index_.find(key);
155 if (entry == key_to_index_.end()) {
156 frames.resize(num_frames);
157 hash_index = cur_hash_index_++;
158 key.frames = frames.data();
159 key_to_index_.emplace(key, hash_index);
160
161 frames_.emplace(hash_index, FrameInfoType{.references = 1, .frames = std::move(frames)});
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700162 if (g_debug->config().options() & BACKTRACE_FULL) {
163 backtraces_info_.emplace(hash_index, std::move(frames_info));
164 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800165 } else {
166 hash_index = entry->second;
167 FrameInfoType* frame_info = &frames_[hash_index];
168 frame_info->references++;
169 }
170 return hash_index;
171}
172
173void PointerData::RemoveBacktrace(size_t hash_index) {
174 if (hash_index <= kBacktraceEmptyIndex) {
175 return;
176 }
177
178 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
179 auto frame_entry = frames_.find(hash_index);
180 if (frame_entry == frames_.end()) {
181 error_log("hash_index %zu does not have matching frame data.", hash_index);
182 return;
183 }
184 FrameInfoType* frame_info = &frame_entry->second;
185 if (--frame_info->references == 0) {
186 FrameKeyType key{.num_frames = frame_info->frames.size(), .frames = frame_info->frames.data()};
187 key_to_index_.erase(key);
188 frames_.erase(hash_index);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700189 if (g_debug->config().options() & BACKTRACE_FULL) {
190 backtraces_info_.erase(hash_index);
191 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800192 }
193}
194
195void PointerData::Add(const void* ptr, size_t pointer_size) {
196 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
197 size_t hash_index = 0;
198 if (backtrace_enabled_) {
Shibin Georgef183f392019-05-21 12:50:10 +0530199 if ((pointer_size >= g_min_alloc_to_record) &&
200 (pointer_size <= g_max_alloc_to_record)) {
201 hash_index = AddBacktrace(g_debug->config().backtrace_frames());
202 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800203 }
204
205 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
206 pointers_[pointer] = PointerInfoType{PointerInfoType::GetEncodedSize(pointer_size), hash_index};
207}
208
209void PointerData::Remove(const void* ptr) {
210 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
211 size_t hash_index;
212 {
213 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
214 auto entry = pointers_.find(pointer);
215 if (entry == pointers_.end()) {
Iris Chang7f209a92019-01-16 11:17:15 +0800216 // Attempt to remove unknown pointer.
Christopher Ferris4da25032018-03-07 13:38:48 -0800217 error_log("No tracked pointer found for 0x%" PRIxPTR, pointer);
218 return;
219 }
220 hash_index = entry->second.hash_index;
221 pointers_.erase(pointer);
222 }
223
224 RemoveBacktrace(hash_index);
225}
226
227size_t PointerData::GetFrames(const void* ptr, uintptr_t* frames, size_t max_frames) {
228 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
229 size_t hash_index;
230 {
231 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
232 auto entry = pointers_.find(pointer);
233 if (entry == pointers_.end()) {
234 return 0;
235 }
236 hash_index = entry->second.hash_index;
237 }
238
239 if (hash_index <= kBacktraceEmptyIndex) {
240 return 0;
241 }
242
243 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
244 auto frame_entry = frames_.find(hash_index);
245 if (frame_entry == frames_.end()) {
246 return 0;
247 }
248 FrameInfoType* frame_info = &frame_entry->second;
249 if (max_frames > frame_info->frames.size()) {
250 max_frames = frame_info->frames.size();
251 }
252 memcpy(frames, &frame_info->frames[0], max_frames * sizeof(uintptr_t));
253
254 return max_frames;
255}
256
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700257void PointerData::LogBacktrace(size_t hash_index) {
258 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
259 if (g_debug->config().options() & BACKTRACE_FULL) {
260 auto backtrace_info_entry = backtraces_info_.find(hash_index);
261 if (backtrace_info_entry != backtraces_info_.end()) {
262 UnwindLog(backtrace_info_entry->second);
263 return;
264 }
265 } else {
266 auto frame_entry = frames_.find(hash_index);
267 if (frame_entry != frames_.end()) {
268 FrameInfoType* frame_info = &frame_entry->second;
269 backtrace_log(frame_info->frames.data(), frame_info->frames.size());
270 return;
271 }
272 }
273 error_log(" hash_index %zu does not have matching frame data.", hash_index);
274}
275
Iris Changb3441502019-02-12 14:00:59 +0800276void PointerData::LogFreeError(const FreePointerInfoType& info, size_t max_cmp_bytes) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800277 error_log(LOG_DIVIDER);
278 uint8_t* memory = reinterpret_cast<uint8_t*>(info.pointer);
279 error_log("+++ ALLOCATION %p USED AFTER FREE", memory);
280 uint8_t fill_free_value = g_debug->config().fill_free_value();
Iris Changb3441502019-02-12 14:00:59 +0800281 for (size_t i = 0; i < max_cmp_bytes; i++) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800282 if (memory[i] != fill_free_value) {
283 error_log(" allocation[%zu] = 0x%02x (expected 0x%02x)", i, memory[i], fill_free_value);
284 }
285 }
286
287 if (info.hash_index > kBacktraceEmptyIndex) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700288 error_log("Backtrace at time of free:");
289 LogBacktrace(info.hash_index);
Christopher Ferris4da25032018-03-07 13:38:48 -0800290 }
291
292 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +0800293 if (g_debug->config().options() & ABORT_ON_ERROR) {
294 abort();
295 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800296}
297
298void PointerData::VerifyFreedPointer(const FreePointerInfoType& info) {
299 size_t usable_size;
300 if (g_debug->HeaderEnabled()) {
301 // Check to see if the tag data has been damaged.
302 Header* header = g_debug->GetHeader(reinterpret_cast<const void*>(info.pointer));
303 if (header->tag != DEBUG_FREE_TAG) {
304 error_log(LOG_DIVIDER);
305 error_log("+++ ALLOCATION 0x%" PRIxPTR " HAS CORRUPTED HEADER TAG 0x%x AFTER FREE",
306 info.pointer, header->tag);
307 error_log(LOG_DIVIDER);
Iris Chang7f209a92019-01-16 11:17:15 +0800308 if (g_debug->config().options() & ABORT_ON_ERROR) {
309 abort();
310 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800311
312 // Stop processing here, it is impossible to tell how the header
313 // may have been damaged.
314 return;
315 }
316 usable_size = header->usable_size;
317 } else {
318 usable_size = g_dispatch->malloc_usable_size(reinterpret_cast<const void*>(info.pointer));
319 }
320
321 size_t bytes = (usable_size < g_debug->config().fill_on_free_bytes())
322 ? usable_size
323 : g_debug->config().fill_on_free_bytes();
Iris Changb3441502019-02-12 14:00:59 +0800324 size_t max_cmp_bytes = bytes;
Christopher Ferris4da25032018-03-07 13:38:48 -0800325 const uint8_t* memory = reinterpret_cast<const uint8_t*>(info.pointer);
326 while (bytes > 0) {
327 size_t bytes_to_cmp = (bytes < g_cmp_mem.size()) ? bytes : g_cmp_mem.size();
328 if (memcmp(memory, g_cmp_mem.data(), bytes_to_cmp) != 0) {
Iris Changb3441502019-02-12 14:00:59 +0800329 LogFreeError(info, max_cmp_bytes);
Christopher Ferris4da25032018-03-07 13:38:48 -0800330 }
331 bytes -= bytes_to_cmp;
332 memory = &memory[bytes_to_cmp];
333 }
334}
335
336void* PointerData::AddFreed(const void* ptr) {
337 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
338
339 size_t hash_index = 0;
340 size_t num_frames = g_debug->config().free_track_backtrace_num_frames();
341 if (num_frames) {
342 hash_index = AddBacktrace(num_frames);
343 }
344
345 void* last = nullptr;
346 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
347 if (free_pointers_.size() == g_debug->config().free_track_allocations()) {
348 FreePointerInfoType info(free_pointers_.front());
349 free_pointers_.pop_front();
350 VerifyFreedPointer(info);
351 RemoveBacktrace(info.hash_index);
352 last = reinterpret_cast<void*>(info.pointer);
353 }
354
355 free_pointers_.emplace_back(FreePointerInfoType{pointer, hash_index});
356 return last;
357}
358
359void PointerData::LogFreeBacktrace(const void* ptr) {
360 size_t hash_index = 0;
361 {
362 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
363 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
364 for (const auto& info : free_pointers_) {
365 if (info.pointer == pointer) {
366 hash_index = info.hash_index;
367 break;
368 }
369 }
370 }
371
372 if (hash_index <= kBacktraceEmptyIndex) {
373 return;
374 }
375
Christopher Ferris4da25032018-03-07 13:38:48 -0800376 error_log("Backtrace of original free:");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700377 LogBacktrace(hash_index);
Christopher Ferris4da25032018-03-07 13:38:48 -0800378}
379
380void PointerData::VerifyAllFreed() {
381 std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_);
382 for (auto& free_info : free_pointers_) {
383 VerifyFreedPointer(free_info);
384 }
385}
386
387void PointerData::GetList(std::vector<ListInfoType>* list, bool only_with_backtrace)
388 REQUIRES(pointer_mutex_, frame_mutex_) {
389 for (const auto& entry : pointers_) {
390 FrameInfoType* frame_info = nullptr;
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700391 std::vector<unwindstack::LocalFrameData>* backtrace_info = nullptr;
Christopher Ferris4da25032018-03-07 13:38:48 -0800392 size_t hash_index = entry.second.hash_index;
393 if (hash_index > kBacktraceEmptyIndex) {
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700394 auto frame_entry = frames_.find(hash_index);
395 if (frame_entry == frames_.end()) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800396 // Somehow wound up with a pointer with a valid hash_index, but
397 // no frame data. This should not be possible since adding a pointer
398 // occurs after the hash_index and frame data have been added.
399 // When removing a pointer, the pointer is deleted before the frame
400 // data.
Christopher Ferris4da25032018-03-07 13:38:48 -0800401 error_log("Pointer 0x%" PRIxPTR " hash_index %zu does not exist.", entry.first, hash_index);
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700402 } else {
403 frame_info = &frame_entry->second;
404 }
405
406 if (g_debug->config().options() & BACKTRACE_FULL) {
407 auto backtrace_entry = backtraces_info_.find(hash_index);
408 if (backtrace_entry == backtraces_info_.end()) {
409 error_log("Pointer 0x%" PRIxPTR " hash_index %zu does not exist.", entry.first, hash_index);
410 } else {
411 backtrace_info = &backtrace_entry->second;
412 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800413 }
414 }
415 if (hash_index == 0 && only_with_backtrace) {
416 continue;
417 }
418
419 list->emplace_back(ListInfoType{entry.first, 1, entry.second.RealSize(),
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700420 entry.second.ZygoteChildAlloc(), frame_info, backtrace_info});
Christopher Ferris4da25032018-03-07 13:38:48 -0800421 }
422
423 // Sort by the size of the allocation.
424 std::sort(list->begin(), list->end(), [](const ListInfoType& a, const ListInfoType& b) {
425 // Put zygote child allocations first.
426 bool a_zygote_child_alloc = a.zygote_child_alloc;
427 bool b_zygote_child_alloc = b.zygote_child_alloc;
428 if (a_zygote_child_alloc && !b_zygote_child_alloc) {
429 return false;
430 }
431 if (!a_zygote_child_alloc && b_zygote_child_alloc) {
432 return true;
433 }
434
435 // Sort by size, descending order.
436 if (a.size != b.size) return a.size > b.size;
437
438 // Put pointers with no backtrace last.
439 FrameInfoType* a_frame = a.frame_info;
440 FrameInfoType* b_frame = b.frame_info;
441 if (a_frame == nullptr && b_frame != nullptr) {
442 return false;
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700443 } else if (a_frame != nullptr && b_frame == nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800444 return true;
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700445 } else if (a_frame == nullptr && b_frame == nullptr) {
446 return a.pointer < b.pointer;
Christopher Ferris4da25032018-03-07 13:38:48 -0800447 }
Christopher Ferrisc151bc32018-05-01 12:59:37 -0700448
Christopher Ferris4da25032018-03-07 13:38:48 -0800449 // Put the pointers with longest backtrace first.
450 if (a_frame->frames.size() != b_frame->frames.size()) {
451 return a_frame->frames.size() > b_frame->frames.size();
452 }
453
454 // Last sort by pointer.
455 return a.pointer < b.pointer;
456 });
457}
458
459void PointerData::GetUniqueList(std::vector<ListInfoType>* list, bool only_with_backtrace)
460 REQUIRES(pointer_mutex_, frame_mutex_) {
461 GetList(list, only_with_backtrace);
462
463 // Remove duplicates of size/backtraces.
464 for (auto iter = list->begin(); iter != list->end();) {
465 auto dup_iter = iter + 1;
466 bool zygote_child_alloc = iter->zygote_child_alloc;
467 size_t size = iter->size;
468 FrameInfoType* frame_info = iter->frame_info;
469 for (; dup_iter != list->end(); ++dup_iter) {
470 if (zygote_child_alloc != dup_iter->zygote_child_alloc || size != dup_iter->size ||
471 frame_info != dup_iter->frame_info) {
472 break;
473 }
474 iter->num_allocations++;
475 }
476 iter = list->erase(iter + 1, dup_iter);
477 }
478}
479
480void PointerData::LogLeaks() {
481 std::vector<ListInfoType> list;
482
483 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
484 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
485 GetList(&list, false);
486
487 size_t track_count = 0;
488 for (const auto& list_info : list) {
489 error_log("+++ %s leaked block of size %zu at 0x%" PRIxPTR " (leak %zu of %zu)", getprogname(),
490 list_info.size, list_info.pointer, ++track_count, list.size());
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700491 if (list_info.backtrace_info != nullptr) {
492 error_log("Backtrace at time of allocation:");
493 UnwindLog(*list_info.backtrace_info);
494 } else if (list_info.frame_info != nullptr) {
Christopher Ferris4da25032018-03-07 13:38:48 -0800495 error_log("Backtrace at time of allocation:");
496 backtrace_log(list_info.frame_info->frames.data(), list_info.frame_info->frames.size());
497 }
498 // Do not bother to free the pointers, we are about to exit any way.
499 }
500}
501
Christopher Ferris6c619a02019-03-01 17:59:51 -0800502void PointerData::GetAllocList(std::vector<ListInfoType>* list) {
503 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
504 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
505
506 if (pointers_.empty()) {
507 return;
508 }
509
510 GetList(list, false);
511}
512
Christopher Ferris4da25032018-03-07 13:38:48 -0800513void PointerData::GetInfo(uint8_t** info, size_t* overall_size, size_t* info_size,
514 size_t* total_memory, size_t* backtrace_size) {
515 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
516 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
517
518 if (pointers_.empty()) {
519 return;
520 }
521
522 std::vector<ListInfoType> list;
523 GetUniqueList(&list, true);
524 if (list.empty()) {
525 return;
526 }
527
528 *backtrace_size = g_debug->config().backtrace_frames();
529 *info_size = sizeof(size_t) * 2 + sizeof(uintptr_t) * *backtrace_size;
530 *overall_size = *info_size * list.size();
531 *info = reinterpret_cast<uint8_t*>(g_dispatch->calloc(*info_size, list.size()));
532 if (*info == nullptr) {
533 return;
534 }
535
536 uint8_t* data = *info;
537 *total_memory = 0;
538 for (const auto& list_info : list) {
539 FrameInfoType* frame_info = list_info.frame_info;
540 *total_memory += list_info.size * list_info.num_allocations;
541 size_t allocation_size =
542 PointerInfoType::GetEncodedSize(list_info.zygote_child_alloc, list_info.size);
543 memcpy(data, &allocation_size, sizeof(size_t));
544 memcpy(&data[sizeof(size_t)], &list_info.num_allocations, sizeof(size_t));
545 if (frame_info != nullptr) {
546 memcpy(&data[2 * sizeof(size_t)], frame_info->frames.data(),
547 frame_info->frames.size() * sizeof(uintptr_t));
548 }
549 data += *info_size;
550 }
551}
552
553bool PointerData::Exists(const void* ptr) {
554 uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr);
555 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
556 return pointers_.count(pointer) != 0;
557}
558
559void PointerData::DumpLiveToFile(FILE* fp) {
560 std::vector<ListInfoType> list;
561
562 std::lock_guard<std::mutex> pointer_guard(pointer_mutex_);
563 std::lock_guard<std::mutex> frame_guard(frame_mutex_);
564 GetUniqueList(&list, false);
565
566 size_t total_memory = 0;
567 for (const auto& info : list) {
568 total_memory += info.size * info.num_allocations;
569 }
570
571 fprintf(fp, "Total memory: %zu\n", total_memory);
572 fprintf(fp, "Allocation records: %zd\n", list.size());
573 fprintf(fp, "Backtrace size: %zu\n", g_debug->config().backtrace_frames());
574 fprintf(fp, "\n");
575
576 for (const auto& info : list) {
577 fprintf(fp, "z %d sz %8zu num %zu bt", (info.zygote_child_alloc) ? 1 : 0, info.size,
578 info.num_allocations);
579 FrameInfoType* frame_info = info.frame_info;
580 if (frame_info != nullptr) {
581 for (size_t i = 0; i < frame_info->frames.size(); i++) {
582 if (frame_info->frames[i] == 0) {
583 break;
584 }
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700585 fprintf(fp, " %" PRIxPTR, frame_info->frames[i]);
Christopher Ferris4da25032018-03-07 13:38:48 -0800586 }
587 }
588 fprintf(fp, "\n");
Christopher Ferris93bdd6a2018-04-05 11:12:38 -0700589 if (info.backtrace_info != nullptr) {
590 fprintf(fp, " bt_info");
591 for (const auto& frame : *info.backtrace_info) {
592 fprintf(fp, " {");
593 if (frame.map_info != nullptr && !frame.map_info->name.empty()) {
594 fprintf(fp, "\"%s\"", frame.map_info->name.c_str());
595 } else {
596 fprintf(fp, "\"\"");
597 }
598 fprintf(fp, " %" PRIx64, frame.rel_pc);
599 if (frame.function_name.empty()) {
600 fprintf(fp, " \"\" 0}");
601 } else {
602 fprintf(fp, " \"%s\" %" PRIx64 "}", demangle(frame.function_name.c_str()).c_str(), frame.function_offset);
603 }
604 }
605 fprintf(fp, "\n");
606 }
Christopher Ferris4da25032018-03-07 13:38:48 -0800607 }
608}
609
610void PointerData::PrepareFork() NO_THREAD_SAFETY_ANALYSIS {
Iris Chang76dcc472019-03-07 12:32:19 +0800611 free_pointer_mutex_.lock();
Christopher Ferris4da25032018-03-07 13:38:48 -0800612 pointer_mutex_.lock();
613 frame_mutex_.lock();
Christopher Ferris4da25032018-03-07 13:38:48 -0800614}
615
616void PointerData::PostForkParent() NO_THREAD_SAFETY_ANALYSIS {
617 frame_mutex_.unlock();
618 pointer_mutex_.unlock();
619 free_pointer_mutex_.unlock();
620}
621
622void PointerData::PostForkChild() __attribute__((no_thread_safety_analysis)) {
623 // Make sure that any potential mutexes have been released and are back
624 // to an initial state.
625 frame_mutex_.try_lock();
626 frame_mutex_.unlock();
627 pointer_mutex_.try_lock();
628 pointer_mutex_.unlock();
629 free_pointer_mutex_.try_lock();
630 free_pointer_mutex_.unlock();
631}