| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 38 | #include <functional> | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 39 | #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 Gao | 4956c37 | 2019-12-19 16:35:51 -0800 | [diff] [blame] | 47 | #include <platform/bionic/macros.h> | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 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 Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 55 | #include "UnwindBacktrace.h" | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 56 |  | 
| Christopher Ferris | 9782b87 | 2019-07-18 13:36:50 -0700 | [diff] [blame] | 57 | extern "C" char* __cxa_demangle(const char*, char*, size_t*, int*); | 
|  | 58 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 59 | std::atomic_uint8_t PointerData::backtrace_enabled_; | 
|  | 60 | std::atomic_bool PointerData::backtrace_dump_; | 
|  | 61 |  | 
|  | 62 | std::mutex PointerData::pointer_mutex_; | 
|  | 63 | std::unordered_map<uintptr_t, PointerInfoType> PointerData::pointers_ GUARDED_BY( | 
|  | 64 | PointerData::pointer_mutex_); | 
|  | 65 |  | 
|  | 66 | std::mutex PointerData::frame_mutex_; | 
|  | 67 | std::unordered_map<FrameKeyType, size_t> PointerData::key_to_index_ GUARDED_BY( | 
|  | 68 | PointerData::frame_mutex_); | 
|  | 69 | std::unordered_map<size_t, FrameInfoType> PointerData::frames_ GUARDED_BY(PointerData::frame_mutex_); | 
| Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 70 | std::unordered_map<size_t, std::vector<unwindstack::FrameData>> PointerData::backtraces_info_ | 
|  | 71 | GUARDED_BY(PointerData::frame_mutex_); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 72 | constexpr size_t kBacktraceEmptyIndex = 1; | 
|  | 73 | size_t PointerData::cur_hash_index_ GUARDED_BY(PointerData::frame_mutex_); | 
|  | 74 |  | 
|  | 75 | std::mutex PointerData::free_pointer_mutex_; | 
|  | 76 | std::deque<FreePointerInfoType> PointerData::free_pointers_ GUARDED_BY( | 
|  | 77 | PointerData::free_pointer_mutex_); | 
|  | 78 |  | 
|  | 79 | // Buffer to use for comparison. | 
|  | 80 | static constexpr size_t kCompareBufferSize = 512 * 1024; | 
|  | 81 | static std::vector<uint8_t> g_cmp_mem(0); | 
|  | 82 |  | 
|  | 83 | static void ToggleBacktraceEnable(int, siginfo_t*, void*) { | 
|  | 84 | g_debug->pointer->ToggleBacktraceEnabled(); | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | static void EnableDump(int, siginfo_t*, void*) { | 
|  | 88 | g_debug->pointer->EnableDumping(); | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | PointerData::PointerData(DebugData* debug_data) : OptionData(debug_data) {} | 
|  | 92 |  | 
|  | 93 | bool 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 Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 111 | if (config.options() & VERBOSE) { | 
|  | 112 | info_log("%s: Run: 'kill -%d %d' to enable backtracing.", getprogname(), | 
|  | 113 | config.backtrace_signal(), getpid()); | 
|  | 114 | } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 115 | } | 
|  | 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 Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 125 | 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 Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 129 | } | 
|  | 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 |  | 
|  | 139 | size_t PointerData::AddBacktrace(size_t num_frames) { | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 140 | std::vector<uintptr_t> frames; | 
| Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 141 | std::vector<unwindstack::FrameData> frames_info; | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 142 | 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 Ferris | dfbc59a | 2022-03-23 12:22:36 -0700 | [diff] [blame] | 152 | frames.resize(num_frames); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 153 | } | 
|  | 154 |  | 
| Christopher Ferris | dfbc59a | 2022-03-23 12:22:36 -0700 | [diff] [blame] | 155 | FrameKeyType key{.num_frames = frames.size(), .frames = frames.data()}; | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 156 | 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 Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 160 | 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 Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 165 | if (g_debug->config().options() & BACKTRACE_FULL) { | 
|  | 166 | backtraces_info_.emplace(hash_index, std::move(frames_info)); | 
|  | 167 | } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 168 | } else { | 
|  | 169 | hash_index = entry->second; | 
|  | 170 | FrameInfoType* frame_info = &frames_[hash_index]; | 
|  | 171 | frame_info->references++; | 
|  | 172 | } | 
|  | 173 | return hash_index; | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | void 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 Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 192 | if (g_debug->config().options() & BACKTRACE_FULL) { | 
|  | 193 | backtraces_info_.erase(hash_index); | 
|  | 194 | } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 195 | } | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | void PointerData::Add(const void* ptr, size_t pointer_size) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 199 | 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 Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 205 | uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr)); | 
|  | 206 | pointers_[mangled_ptr] = | 
|  | 207 | PointerInfoType{PointerInfoType::GetEncodedSize(pointer_size), hash_index}; | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 208 | } | 
|  | 209 |  | 
|  | 210 | void PointerData::Remove(const void* ptr) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 211 | size_t hash_index; | 
|  | 212 | { | 
|  | 213 | std::lock_guard<std::mutex> pointer_guard(pointer_mutex_); | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 214 | uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr)); | 
|  | 215 | auto entry = pointers_.find(mangled_ptr); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 216 | if (entry == pointers_.end()) { | 
| Iris Chang | 7f209a9 | 2019-01-16 11:17:15 +0800 | [diff] [blame] | 217 | // Attempt to remove unknown pointer. | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 218 | error_log("No tracked pointer found for 0x%" PRIxPTR, DemanglePointer(mangled_ptr)); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 219 | return; | 
|  | 220 | } | 
|  | 221 | hash_index = entry->second.hash_index; | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 222 | pointers_.erase(mangled_ptr); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 223 | } | 
|  | 224 |  | 
|  | 225 | RemoveBacktrace(hash_index); | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | size_t PointerData::GetFrames(const void* ptr, uintptr_t* frames, size_t max_frames) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 229 | size_t hash_index; | 
|  | 230 | { | 
|  | 231 | std::lock_guard<std::mutex> pointer_guard(pointer_mutex_); | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 232 | uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr)); | 
|  | 233 | auto entry = pointers_.find(mangled_ptr); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 234 | 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 Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 258 | void 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 Chang | b344150 | 2019-02-12 14:00:59 +0800 | [diff] [blame] | 277 | void PointerData::LogFreeError(const FreePointerInfoType& info, size_t max_cmp_bytes) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 278 | error_log(LOG_DIVIDER); | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 279 | uintptr_t pointer = DemanglePointer(info.mangled_ptr); | 
|  | 280 | uint8_t* memory = reinterpret_cast<uint8_t*>(pointer); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 281 | error_log("+++ ALLOCATION %p USED AFTER FREE", memory); | 
|  | 282 | uint8_t fill_free_value = g_debug->config().fill_free_value(); | 
| Iris Chang | b344150 | 2019-02-12 14:00:59 +0800 | [diff] [blame] | 283 | for (size_t i = 0; i < max_cmp_bytes; i++) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 284 | 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 Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 290 | error_log("Backtrace at time of free:"); | 
|  | 291 | LogBacktrace(info.hash_index); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 292 | } | 
|  | 293 |  | 
|  | 294 | error_log(LOG_DIVIDER); | 
| Iris Chang | 7f209a9 | 2019-01-16 11:17:15 +0800 | [diff] [blame] | 295 | if (g_debug->config().options() & ABORT_ON_ERROR) { | 
|  | 296 | abort(); | 
|  | 297 | } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 298 | } | 
|  | 299 |  | 
|  | 300 | void PointerData::VerifyFreedPointer(const FreePointerInfoType& info) { | 
|  | 301 | size_t usable_size; | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 302 | uintptr_t pointer = DemanglePointer(info.mangled_ptr); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 303 | if (g_debug->HeaderEnabled()) { | 
|  | 304 | // Check to see if the tag data has been damaged. | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 305 | Header* header = g_debug->GetHeader(reinterpret_cast<const void*>(pointer)); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 306 | if (header->tag != DEBUG_FREE_TAG) { | 
|  | 307 | error_log(LOG_DIVIDER); | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 308 | error_log("+++ ALLOCATION 0x%" PRIxPTR " HAS CORRUPTED HEADER TAG 0x%x AFTER FREE", pointer, | 
|  | 309 | header->tag); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 310 | error_log(LOG_DIVIDER); | 
| Iris Chang | 7f209a9 | 2019-01-16 11:17:15 +0800 | [diff] [blame] | 311 | if (g_debug->config().options() & ABORT_ON_ERROR) { | 
|  | 312 | abort(); | 
|  | 313 | } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 314 |  | 
|  | 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 Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 321 | usable_size = g_dispatch->malloc_usable_size(reinterpret_cast<const void*>(pointer)); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 322 | } | 
|  | 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 Chang | b344150 | 2019-02-12 14:00:59 +0800 | [diff] [blame] | 327 | size_t max_cmp_bytes = bytes; | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 328 | const uint8_t* memory = reinterpret_cast<const uint8_t*>(pointer); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 329 | 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 Chang | b344150 | 2019-02-12 14:00:59 +0800 | [diff] [blame] | 332 | LogFreeError(info, max_cmp_bytes); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 333 | } | 
|  | 334 | bytes -= bytes_to_cmp; | 
|  | 335 | memory = &memory[bytes_to_cmp]; | 
|  | 336 | } | 
|  | 337 | } | 
|  | 338 |  | 
|  | 339 | void* PointerData::AddFreed(const void* ptr) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 340 | 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 Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 353 | last = reinterpret_cast<void*>(DemanglePointer(info.mangled_ptr)); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 354 | } | 
|  | 355 |  | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 356 | uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr)); | 
|  | 357 | free_pointers_.emplace_back(FreePointerInfoType{mangled_ptr, hash_index}); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 358 | return last; | 
|  | 359 | } | 
|  | 360 |  | 
|  | 361 | void 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 Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 367 | if (DemanglePointer(info.mangled_ptr) == pointer) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 368 | hash_index = info.hash_index; | 
|  | 369 | break; | 
|  | 370 | } | 
|  | 371 | } | 
|  | 372 | } | 
|  | 373 |  | 
|  | 374 | if (hash_index <= kBacktraceEmptyIndex) { | 
|  | 375 | return; | 
|  | 376 | } | 
|  | 377 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 378 | error_log("Backtrace of original free:"); | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 379 | LogBacktrace(hash_index); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 380 | } | 
|  | 381 |  | 
|  | 382 | void 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 |  | 
|  | 389 | void 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 Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 393 | std::vector<unwindstack::FrameData>* backtrace_info = nullptr; | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 394 | uintptr_t pointer = DemanglePointer(entry.first); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 395 | size_t hash_index = entry.second.hash_index; | 
|  | 396 | if (hash_index > kBacktraceEmptyIndex) { | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 397 | auto frame_entry = frames_.find(hash_index); | 
|  | 398 | if (frame_entry == frames_.end()) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 399 | // 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 Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 404 | error_log("Pointer 0x%" PRIxPTR " hash_index %zu does not exist.", pointer, hash_index); | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 405 | } 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 Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 412 | error_log("Pointer 0x%" PRIxPTR " hash_index %zu does not exist.", pointer, hash_index); | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 413 | } else { | 
|  | 414 | backtrace_info = &backtrace_entry->second; | 
|  | 415 | } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 416 | } | 
|  | 417 | } | 
|  | 418 | if (hash_index == 0 && only_with_backtrace) { | 
|  | 419 | continue; | 
|  | 420 | } | 
|  | 421 |  | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 422 | list->emplace_back(ListInfoType{pointer, 1, entry.second.RealSize(), | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 423 | entry.second.ZygoteChildAlloc(), frame_info, backtrace_info}); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 424 | } | 
|  | 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 Ferris | c151bc3 | 2018-05-01 12:59:37 -0700 | [diff] [blame] | 446 | } else if (a_frame != nullptr && b_frame == nullptr) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 447 | return true; | 
| Christopher Ferris | c151bc3 | 2018-05-01 12:59:37 -0700 | [diff] [blame] | 448 | } else if (a_frame == nullptr && b_frame == nullptr) { | 
|  | 449 | return a.pointer < b.pointer; | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 450 | } | 
| Christopher Ferris | c151bc3 | 2018-05-01 12:59:37 -0700 | [diff] [blame] | 451 |  | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 452 | // 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 |  | 
|  | 462 | void 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 |  | 
|  | 483 | void 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 Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 494 | 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 Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 498 | 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 Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 505 | void 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 Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 516 | void 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 |  | 
|  | 556 | bool PointerData::Exists(const void* ptr) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 557 | std::lock_guard<std::mutex> pointer_guard(pointer_mutex_); | 
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 558 | uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr)); | 
|  | 559 | return pointers_.count(mangled_ptr) != 0; | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 560 | } | 
|  | 561 |  | 
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 562 | void PointerData::DumpLiveToFile(int fd) { | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 563 | 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 Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 574 | 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 Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 578 |  | 
|  | 579 | for (const auto& info : list) { | 
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 580 | dprintf(fd, "z %d  sz %8zu  num    %zu  bt", (info.zygote_child_alloc) ? 1 : 0, info.size, | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 581 | 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 Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 588 | dprintf(fd, " %" PRIxPTR, frame_info->frames[i]); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 589 | } | 
|  | 590 | } | 
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 591 | dprintf(fd, "\n"); | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 592 | if (info.backtrace_info != nullptr) { | 
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 593 | dprintf(fd, "  bt_info"); | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 594 | for (const auto& frame : *info.backtrace_info) { | 
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 595 | dprintf(fd, " {"); | 
| David Srbecky | 92b8d64 | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 596 | if (frame.map_info != nullptr && !frame.map_info->name().empty()) { | 
|  | 597 | dprintf(fd, "\"%s\"", frame.map_info->name().c_str()); | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 598 | } else { | 
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 599 | dprintf(fd, "\"\""); | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 600 | } | 
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 601 | dprintf(fd, " %" PRIx64, frame.rel_pc); | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 602 | if (frame.function_name.empty()) { | 
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 603 | dprintf(fd, " \"\" 0}"); | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 604 | } else { | 
| Christopher Ferris | 9782b87 | 2019-07-18 13:36:50 -0700 | [diff] [blame] | 605 | 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 Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 613 | dprintf(fd, " \"%s\" %" PRIx64 "}", name, frame.function_offset); | 
| Christopher Ferris | 9782b87 | 2019-07-18 13:36:50 -0700 | [diff] [blame] | 614 | free(demangled_name); | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 615 | } | 
|  | 616 | } | 
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 617 | dprintf(fd, "\n"); | 
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 618 | } | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 619 | } | 
|  | 620 | } | 
|  | 621 |  | 
|  | 622 | void PointerData::PrepareFork() NO_THREAD_SAFETY_ANALYSIS { | 
| Iris Chang | 76dcc47 | 2019-03-07 12:32:19 +0800 | [diff] [blame] | 623 | free_pointer_mutex_.lock(); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 624 | pointer_mutex_.lock(); | 
|  | 625 | frame_mutex_.lock(); | 
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 626 | } | 
|  | 627 |  | 
|  | 628 | void PointerData::PostForkParent() NO_THREAD_SAFETY_ANALYSIS { | 
|  | 629 | frame_mutex_.unlock(); | 
|  | 630 | pointer_mutex_.unlock(); | 
|  | 631 | free_pointer_mutex_.unlock(); | 
|  | 632 | } | 
|  | 633 |  | 
|  | 634 | void 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 Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 644 |  | 
|  | 645 | void 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 | } |