| 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 | |
| Elliott Hughes | 0ec50d8 | 2025-05-09 13:58:56 -0700 | [diff] [blame] | 38 | #include <algorithm> |
| 39 | #include <atomic> |
| 40 | #include <deque> |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 41 | #include <functional> |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 42 | #include <mutex> |
| 43 | #include <string> |
| 44 | #include <unordered_map> |
| 45 | #include <utility> |
| 46 | #include <vector> |
| 47 | |
| 48 | #include <android-base/stringprintf.h> |
| 49 | #include <android-base/thread_annotations.h> |
| Josh Gao | 4956c37 | 2019-12-19 16:35:51 -0800 | [diff] [blame] | 50 | #include <platform/bionic/macros.h> |
| Christopher Ferris | 3d47086 | 2025-08-13 16:43:58 +0000 | [diff] [blame] | 51 | #include <unwindstack/Demangle.h> |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 52 | |
| 53 | #include "Config.h" |
| 54 | #include "DebugData.h" |
| 55 | #include "PointerData.h" |
| 56 | #include "backtrace.h" |
| 57 | #include "debug_log.h" |
| 58 | #include "malloc_debug.h" |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 59 | #include "UnwindBacktrace.h" |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 60 | |
| 61 | std::atomic_uint8_t PointerData::backtrace_enabled_; |
| 62 | std::atomic_bool PointerData::backtrace_dump_; |
| 63 | |
| 64 | std::mutex PointerData::pointer_mutex_; |
| 65 | std::unordered_map<uintptr_t, PointerInfoType> PointerData::pointers_ GUARDED_BY( |
| 66 | PointerData::pointer_mutex_); |
| 67 | |
| 68 | std::mutex PointerData::frame_mutex_; |
| 69 | std::unordered_map<FrameKeyType, size_t> PointerData::key_to_index_ GUARDED_BY( |
| 70 | PointerData::frame_mutex_); |
| 71 | 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] | 72 | std::unordered_map<size_t, std::vector<unwindstack::FrameData>> PointerData::backtraces_info_ |
| 73 | GUARDED_BY(PointerData::frame_mutex_); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 74 | constexpr size_t kBacktraceEmptyIndex = 1; |
| 75 | size_t PointerData::cur_hash_index_ GUARDED_BY(PointerData::frame_mutex_); |
| 76 | |
| 77 | std::mutex PointerData::free_pointer_mutex_; |
| 78 | std::deque<FreePointerInfoType> PointerData::free_pointers_ GUARDED_BY( |
| 79 | PointerData::free_pointer_mutex_); |
| 80 | |
| 81 | // Buffer to use for comparison. |
| 82 | static constexpr size_t kCompareBufferSize = 512 * 1024; |
| 83 | static std::vector<uint8_t> g_cmp_mem(0); |
| 84 | |
| 85 | static void ToggleBacktraceEnable(int, siginfo_t*, void*) { |
| 86 | g_debug->pointer->ToggleBacktraceEnabled(); |
| 87 | } |
| 88 | |
| 89 | static void EnableDump(int, siginfo_t*, void*) { |
| 90 | g_debug->pointer->EnableDumping(); |
| 91 | } |
| 92 | |
| 93 | PointerData::PointerData(DebugData* debug_data) : OptionData(debug_data) {} |
| 94 | |
| 95 | bool PointerData::Initialize(const Config& config) NO_THREAD_SAFETY_ANALYSIS { |
| 96 | pointers_.clear(); |
| 97 | key_to_index_.clear(); |
| 98 | frames_.clear(); |
| 99 | free_pointers_.clear(); |
| 100 | // A hash index of kBacktraceEmptyIndex indicates that we tried to get |
| 101 | // a backtrace, but there was nothing recorded. |
| 102 | cur_hash_index_ = kBacktraceEmptyIndex + 1; |
| 103 | |
| 104 | backtrace_enabled_ = config.backtrace_enabled(); |
| 105 | if (config.backtrace_enable_on_signal()) { |
| 106 | struct sigaction64 enable_act = {}; |
| 107 | enable_act.sa_sigaction = ToggleBacktraceEnable; |
| 108 | enable_act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; |
| 109 | if (sigaction64(config.backtrace_signal(), &enable_act, nullptr) != 0) { |
| 110 | error_log("Unable to set up backtrace signal enable function: %s", strerror(errno)); |
| 111 | return false; |
| 112 | } |
| Christopher Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 113 | if (config.options() & VERBOSE) { |
| 114 | info_log("%s: Run: 'kill -%d %d' to enable backtracing.", getprogname(), |
| 115 | config.backtrace_signal(), getpid()); |
| 116 | } |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | if (config.options() & BACKTRACE) { |
| 120 | struct sigaction64 act = {}; |
| 121 | act.sa_sigaction = EnableDump; |
| 122 | act.sa_flags = SA_RESTART | SA_SIGINFO | SA_ONSTACK; |
| 123 | if (sigaction64(config.backtrace_dump_signal(), &act, nullptr) != 0) { |
| 124 | error_log("Unable to set up backtrace dump signal function: %s", strerror(errno)); |
| 125 | return false; |
| 126 | } |
| Christopher Ferris | c328e44 | 2019-04-01 19:31:26 -0700 | [diff] [blame] | 127 | if (config.options() & VERBOSE) { |
| 128 | info_log("%s: Run: 'kill -%d %d' to dump the backtrace.", getprogname(), |
| 129 | config.backtrace_dump_signal(), getpid()); |
| 130 | } |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | backtrace_dump_ = false; |
| 134 | |
| 135 | if (config.options() & FREE_TRACK) { |
| 136 | g_cmp_mem.resize(kCompareBufferSize, config.fill_free_value()); |
| 137 | } |
| 138 | return true; |
| 139 | } |
| 140 | |
| Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 141 | static inline bool ShouldBacktraceAllocSize(size_t size_bytes) { |
| 142 | static bool only_backtrace_specific_sizes = |
| 143 | g_debug->config().options() & BACKTRACE_SPECIFIC_SIZES; |
| 144 | if (!only_backtrace_specific_sizes) { |
| 145 | return true; |
| 146 | } |
| 147 | static size_t min_size_bytes = g_debug->config().backtrace_min_size_bytes(); |
| 148 | static size_t max_size_bytes = g_debug->config().backtrace_max_size_bytes(); |
| 149 | return size_bytes >= min_size_bytes && size_bytes <= max_size_bytes; |
| 150 | } |
| 151 | |
| 152 | size_t PointerData::AddBacktrace(size_t num_frames, size_t size_bytes) { |
| 153 | if (!ShouldBacktraceAllocSize(size_bytes)) { |
| 154 | return kBacktraceEmptyIndex; |
| 155 | } |
| 156 | |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 157 | std::vector<uintptr_t> frames; |
| Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 158 | std::vector<unwindstack::FrameData> frames_info; |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 159 | if (g_debug->config().options() & BACKTRACE_FULL) { |
| 160 | if (!Unwind(&frames, &frames_info, num_frames)) { |
| 161 | return kBacktraceEmptyIndex; |
| 162 | } |
| 163 | } else { |
| 164 | frames.resize(num_frames); |
| 165 | num_frames = backtrace_get(frames.data(), frames.size()); |
| 166 | if (num_frames == 0) { |
| 167 | return kBacktraceEmptyIndex; |
| 168 | } |
| Christopher Ferris | dfbc59a | 2022-03-23 12:22:36 -0700 | [diff] [blame] | 169 | frames.resize(num_frames); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| Christopher Ferris | dfbc59a | 2022-03-23 12:22:36 -0700 | [diff] [blame] | 172 | FrameKeyType key{.num_frames = frames.size(), .frames = frames.data()}; |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 173 | size_t hash_index; |
| 174 | std::lock_guard<std::mutex> frame_guard(frame_mutex_); |
| 175 | auto entry = key_to_index_.find(key); |
| 176 | if (entry == key_to_index_.end()) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 177 | hash_index = cur_hash_index_++; |
| 178 | key.frames = frames.data(); |
| 179 | key_to_index_.emplace(key, hash_index); |
| 180 | |
| 181 | frames_.emplace(hash_index, FrameInfoType{.references = 1, .frames = std::move(frames)}); |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 182 | if (g_debug->config().options() & BACKTRACE_FULL) { |
| 183 | backtraces_info_.emplace(hash_index, std::move(frames_info)); |
| 184 | } |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 185 | } else { |
| 186 | hash_index = entry->second; |
| 187 | FrameInfoType* frame_info = &frames_[hash_index]; |
| 188 | frame_info->references++; |
| 189 | } |
| 190 | return hash_index; |
| 191 | } |
| 192 | |
| 193 | void PointerData::RemoveBacktrace(size_t hash_index) { |
| 194 | if (hash_index <= kBacktraceEmptyIndex) { |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | std::lock_guard<std::mutex> frame_guard(frame_mutex_); |
| 199 | auto frame_entry = frames_.find(hash_index); |
| 200 | if (frame_entry == frames_.end()) { |
| 201 | error_log("hash_index %zu does not have matching frame data.", hash_index); |
| 202 | return; |
| 203 | } |
| 204 | FrameInfoType* frame_info = &frame_entry->second; |
| 205 | if (--frame_info->references == 0) { |
| 206 | FrameKeyType key{.num_frames = frame_info->frames.size(), .frames = frame_info->frames.data()}; |
| 207 | key_to_index_.erase(key); |
| 208 | frames_.erase(hash_index); |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 209 | if (g_debug->config().options() & BACKTRACE_FULL) { |
| 210 | backtraces_info_.erase(hash_index); |
| 211 | } |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | void PointerData::Add(const void* ptr, size_t pointer_size) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 216 | size_t hash_index = 0; |
| 217 | if (backtrace_enabled_) { |
| Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 218 | hash_index = AddBacktrace(g_debug->config().backtrace_frames(), pointer_size); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | std::lock_guard<std::mutex> pointer_guard(pointer_mutex_); |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 222 | uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr)); |
| 223 | pointers_[mangled_ptr] = |
| 224 | PointerInfoType{PointerInfoType::GetEncodedSize(pointer_size), hash_index}; |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void PointerData::Remove(const void* ptr) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 228 | size_t hash_index; |
| 229 | { |
| 230 | std::lock_guard<std::mutex> pointer_guard(pointer_mutex_); |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 231 | uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr)); |
| 232 | auto entry = pointers_.find(mangled_ptr); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 233 | if (entry == pointers_.end()) { |
| Iris Chang | 7f209a9 | 2019-01-16 11:17:15 +0800 | [diff] [blame] | 234 | // Attempt to remove unknown pointer. |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 235 | error_log("No tracked pointer found for 0x%" PRIxPTR, DemanglePointer(mangled_ptr)); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | hash_index = entry->second.hash_index; |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 239 | pointers_.erase(mangled_ptr); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | RemoveBacktrace(hash_index); |
| 243 | } |
| 244 | |
| 245 | 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] | 246 | size_t hash_index; |
| 247 | { |
| 248 | std::lock_guard<std::mutex> pointer_guard(pointer_mutex_); |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 249 | uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr)); |
| 250 | auto entry = pointers_.find(mangled_ptr); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 251 | if (entry == pointers_.end()) { |
| 252 | return 0; |
| 253 | } |
| 254 | hash_index = entry->second.hash_index; |
| 255 | } |
| 256 | |
| 257 | if (hash_index <= kBacktraceEmptyIndex) { |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | std::lock_guard<std::mutex> frame_guard(frame_mutex_); |
| 262 | auto frame_entry = frames_.find(hash_index); |
| 263 | if (frame_entry == frames_.end()) { |
| 264 | return 0; |
| 265 | } |
| 266 | FrameInfoType* frame_info = &frame_entry->second; |
| 267 | if (max_frames > frame_info->frames.size()) { |
| 268 | max_frames = frame_info->frames.size(); |
| 269 | } |
| 270 | memcpy(frames, &frame_info->frames[0], max_frames * sizeof(uintptr_t)); |
| 271 | |
| 272 | return max_frames; |
| 273 | } |
| 274 | |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 275 | void PointerData::LogBacktrace(size_t hash_index) { |
| 276 | std::lock_guard<std::mutex> frame_guard(frame_mutex_); |
| 277 | if (g_debug->config().options() & BACKTRACE_FULL) { |
| 278 | auto backtrace_info_entry = backtraces_info_.find(hash_index); |
| 279 | if (backtrace_info_entry != backtraces_info_.end()) { |
| 280 | UnwindLog(backtrace_info_entry->second); |
| 281 | return; |
| 282 | } |
| 283 | } else { |
| 284 | auto frame_entry = frames_.find(hash_index); |
| 285 | if (frame_entry != frames_.end()) { |
| 286 | FrameInfoType* frame_info = &frame_entry->second; |
| 287 | backtrace_log(frame_info->frames.data(), frame_info->frames.size()); |
| 288 | return; |
| 289 | } |
| 290 | } |
| 291 | error_log(" hash_index %zu does not have matching frame data.", hash_index); |
| 292 | } |
| 293 | |
| Iris Chang | b344150 | 2019-02-12 14:00:59 +0800 | [diff] [blame] | 294 | void PointerData::LogFreeError(const FreePointerInfoType& info, size_t max_cmp_bytes) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 295 | error_log(LOG_DIVIDER); |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 296 | uintptr_t pointer = DemanglePointer(info.mangled_ptr); |
| 297 | uint8_t* memory = reinterpret_cast<uint8_t*>(pointer); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 298 | error_log("+++ ALLOCATION %p USED AFTER FREE", memory); |
| 299 | uint8_t fill_free_value = g_debug->config().fill_free_value(); |
| Iris Chang | b344150 | 2019-02-12 14:00:59 +0800 | [diff] [blame] | 300 | for (size_t i = 0; i < max_cmp_bytes; i++) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 301 | if (memory[i] != fill_free_value) { |
| 302 | error_log(" allocation[%zu] = 0x%02x (expected 0x%02x)", i, memory[i], fill_free_value); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | if (info.hash_index > kBacktraceEmptyIndex) { |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 307 | error_log("Backtrace at time of free:"); |
| 308 | LogBacktrace(info.hash_index); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | error_log(LOG_DIVIDER); |
| Iris Chang | 7f209a9 | 2019-01-16 11:17:15 +0800 | [diff] [blame] | 312 | if (g_debug->config().options() & ABORT_ON_ERROR) { |
| 313 | abort(); |
| 314 | } |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | void PointerData::VerifyFreedPointer(const FreePointerInfoType& info) { |
| 318 | size_t usable_size; |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 319 | uintptr_t pointer = DemanglePointer(info.mangled_ptr); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 320 | if (g_debug->HeaderEnabled()) { |
| 321 | // Check to see if the tag data has been damaged. |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 322 | Header* header = g_debug->GetHeader(reinterpret_cast<const void*>(pointer)); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 323 | if (header->tag != DEBUG_FREE_TAG) { |
| 324 | error_log(LOG_DIVIDER); |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 325 | error_log("+++ ALLOCATION 0x%" PRIxPTR " HAS CORRUPTED HEADER TAG 0x%x AFTER FREE", pointer, |
| 326 | header->tag); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 327 | error_log(LOG_DIVIDER); |
| Iris Chang | 7f209a9 | 2019-01-16 11:17:15 +0800 | [diff] [blame] | 328 | if (g_debug->config().options() & ABORT_ON_ERROR) { |
| 329 | abort(); |
| 330 | } |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 331 | |
| 332 | // Stop processing here, it is impossible to tell how the header |
| 333 | // may have been damaged. |
| 334 | return; |
| 335 | } |
| 336 | usable_size = header->usable_size; |
| 337 | } else { |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 338 | usable_size = g_dispatch->malloc_usable_size(reinterpret_cast<const void*>(pointer)); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | size_t bytes = (usable_size < g_debug->config().fill_on_free_bytes()) |
| 342 | ? usable_size |
| 343 | : g_debug->config().fill_on_free_bytes(); |
| Iris Chang | b344150 | 2019-02-12 14:00:59 +0800 | [diff] [blame] | 344 | size_t max_cmp_bytes = bytes; |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 345 | const uint8_t* memory = reinterpret_cast<const uint8_t*>(pointer); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 346 | while (bytes > 0) { |
| 347 | size_t bytes_to_cmp = (bytes < g_cmp_mem.size()) ? bytes : g_cmp_mem.size(); |
| 348 | if (memcmp(memory, g_cmp_mem.data(), bytes_to_cmp) != 0) { |
| Iris Chang | b344150 | 2019-02-12 14:00:59 +0800 | [diff] [blame] | 349 | LogFreeError(info, max_cmp_bytes); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 350 | } |
| 351 | bytes -= bytes_to_cmp; |
| 352 | memory = &memory[bytes_to_cmp]; |
| 353 | } |
| 354 | } |
| 355 | |
| Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 356 | void* PointerData::AddFreed(const void* ptr, size_t size_bytes) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 357 | size_t hash_index = 0; |
| 358 | size_t num_frames = g_debug->config().free_track_backtrace_num_frames(); |
| 359 | if (num_frames) { |
| Christopher Ferris | a383648 | 2022-05-13 12:09:39 -0700 | [diff] [blame] | 360 | hash_index = AddBacktrace(num_frames, size_bytes); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | void* last = nullptr; |
| 364 | std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_); |
| 365 | if (free_pointers_.size() == g_debug->config().free_track_allocations()) { |
| 366 | FreePointerInfoType info(free_pointers_.front()); |
| 367 | free_pointers_.pop_front(); |
| 368 | VerifyFreedPointer(info); |
| 369 | RemoveBacktrace(info.hash_index); |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 370 | last = reinterpret_cast<void*>(DemanglePointer(info.mangled_ptr)); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 371 | } |
| 372 | |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 373 | uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr)); |
| 374 | free_pointers_.emplace_back(FreePointerInfoType{mangled_ptr, hash_index}); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 375 | return last; |
| 376 | } |
| 377 | |
| 378 | void PointerData::LogFreeBacktrace(const void* ptr) { |
| 379 | size_t hash_index = 0; |
| 380 | { |
| 381 | uintptr_t pointer = reinterpret_cast<uintptr_t>(ptr); |
| 382 | std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_); |
| 383 | for (const auto& info : free_pointers_) { |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 384 | if (DemanglePointer(info.mangled_ptr) == pointer) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 385 | hash_index = info.hash_index; |
| 386 | break; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if (hash_index <= kBacktraceEmptyIndex) { |
| 392 | return; |
| 393 | } |
| 394 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 395 | error_log("Backtrace of original free:"); |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 396 | LogBacktrace(hash_index); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | void PointerData::VerifyAllFreed() { |
| 400 | std::lock_guard<std::mutex> freed_guard(free_pointer_mutex_); |
| 401 | for (auto& free_info : free_pointers_) { |
| 402 | VerifyFreedPointer(free_info); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | void PointerData::GetList(std::vector<ListInfoType>* list, bool only_with_backtrace) |
| 407 | REQUIRES(pointer_mutex_, frame_mutex_) { |
| 408 | for (const auto& entry : pointers_) { |
| 409 | FrameInfoType* frame_info = nullptr; |
| Christopher Ferris | 459eecb | 2022-01-07 13:38:10 -0800 | [diff] [blame] | 410 | std::vector<unwindstack::FrameData>* backtrace_info = nullptr; |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 411 | uintptr_t pointer = DemanglePointer(entry.first); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 412 | size_t hash_index = entry.second.hash_index; |
| 413 | if (hash_index > kBacktraceEmptyIndex) { |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 414 | auto frame_entry = frames_.find(hash_index); |
| 415 | if (frame_entry == frames_.end()) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 416 | // Somehow wound up with a pointer with a valid hash_index, but |
| 417 | // no frame data. This should not be possible since adding a pointer |
| 418 | // occurs after the hash_index and frame data have been added. |
| 419 | // When removing a pointer, the pointer is deleted before the frame |
| 420 | // data. |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 421 | 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] | 422 | } else { |
| 423 | frame_info = &frame_entry->second; |
| 424 | } |
| 425 | |
| 426 | if (g_debug->config().options() & BACKTRACE_FULL) { |
| 427 | auto backtrace_entry = backtraces_info_.find(hash_index); |
| 428 | if (backtrace_entry == backtraces_info_.end()) { |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 429 | 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] | 430 | } else { |
| 431 | backtrace_info = &backtrace_entry->second; |
| 432 | } |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | if (hash_index == 0 && only_with_backtrace) { |
| 436 | continue; |
| 437 | } |
| 438 | |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 439 | list->emplace_back(ListInfoType{pointer, 1, entry.second.RealSize(), |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 440 | entry.second.ZygoteChildAlloc(), frame_info, backtrace_info}); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | // Sort by the size of the allocation. |
| 444 | std::sort(list->begin(), list->end(), [](const ListInfoType& a, const ListInfoType& b) { |
| 445 | // Put zygote child allocations first. |
| 446 | bool a_zygote_child_alloc = a.zygote_child_alloc; |
| 447 | bool b_zygote_child_alloc = b.zygote_child_alloc; |
| 448 | if (a_zygote_child_alloc && !b_zygote_child_alloc) { |
| 449 | return false; |
| 450 | } |
| 451 | if (!a_zygote_child_alloc && b_zygote_child_alloc) { |
| 452 | return true; |
| 453 | } |
| 454 | |
| 455 | // Sort by size, descending order. |
| 456 | if (a.size != b.size) return a.size > b.size; |
| 457 | |
| 458 | // Put pointers with no backtrace last. |
| 459 | FrameInfoType* a_frame = a.frame_info; |
| 460 | FrameInfoType* b_frame = b.frame_info; |
| 461 | if (a_frame == nullptr && b_frame != nullptr) { |
| 462 | return false; |
| Christopher Ferris | c151bc3 | 2018-05-01 12:59:37 -0700 | [diff] [blame] | 463 | } else if (a_frame != nullptr && b_frame == nullptr) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 464 | return true; |
| Christopher Ferris | c151bc3 | 2018-05-01 12:59:37 -0700 | [diff] [blame] | 465 | } else if (a_frame == nullptr && b_frame == nullptr) { |
| 466 | return a.pointer < b.pointer; |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 467 | } |
| Christopher Ferris | c151bc3 | 2018-05-01 12:59:37 -0700 | [diff] [blame] | 468 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 469 | // Put the pointers with longest backtrace first. |
| 470 | if (a_frame->frames.size() != b_frame->frames.size()) { |
| 471 | return a_frame->frames.size() > b_frame->frames.size(); |
| 472 | } |
| 473 | |
| 474 | // Last sort by pointer. |
| 475 | return a.pointer < b.pointer; |
| 476 | }); |
| 477 | } |
| 478 | |
| 479 | void PointerData::GetUniqueList(std::vector<ListInfoType>* list, bool only_with_backtrace) |
| 480 | REQUIRES(pointer_mutex_, frame_mutex_) { |
| 481 | GetList(list, only_with_backtrace); |
| 482 | |
| 483 | // Remove duplicates of size/backtraces. |
| 484 | for (auto iter = list->begin(); iter != list->end();) { |
| 485 | auto dup_iter = iter + 1; |
| 486 | bool zygote_child_alloc = iter->zygote_child_alloc; |
| 487 | size_t size = iter->size; |
| 488 | FrameInfoType* frame_info = iter->frame_info; |
| 489 | for (; dup_iter != list->end(); ++dup_iter) { |
| 490 | if (zygote_child_alloc != dup_iter->zygote_child_alloc || size != dup_iter->size || |
| 491 | frame_info != dup_iter->frame_info) { |
| 492 | break; |
| 493 | } |
| 494 | iter->num_allocations++; |
| 495 | } |
| 496 | iter = list->erase(iter + 1, dup_iter); |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | void PointerData::LogLeaks() { |
| 501 | std::vector<ListInfoType> list; |
| 502 | |
| 503 | std::lock_guard<std::mutex> pointer_guard(pointer_mutex_); |
| 504 | std::lock_guard<std::mutex> frame_guard(frame_mutex_); |
| 505 | GetList(&list, false); |
| 506 | |
| 507 | size_t track_count = 0; |
| 508 | for (const auto& list_info : list) { |
| 509 | error_log("+++ %s leaked block of size %zu at 0x%" PRIxPTR " (leak %zu of %zu)", getprogname(), |
| 510 | list_info.size, list_info.pointer, ++track_count, list.size()); |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 511 | if (list_info.backtrace_info != nullptr) { |
| 512 | error_log("Backtrace at time of allocation:"); |
| 513 | UnwindLog(*list_info.backtrace_info); |
| 514 | } else if (list_info.frame_info != nullptr) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 515 | error_log("Backtrace at time of allocation:"); |
| 516 | backtrace_log(list_info.frame_info->frames.data(), list_info.frame_info->frames.size()); |
| 517 | } |
| 518 | // Do not bother to free the pointers, we are about to exit any way. |
| 519 | } |
| 520 | } |
| 521 | |
| Christopher Ferris | 6c619a0 | 2019-03-01 17:59:51 -0800 | [diff] [blame] | 522 | void PointerData::GetAllocList(std::vector<ListInfoType>* list) { |
| 523 | std::lock_guard<std::mutex> pointer_guard(pointer_mutex_); |
| 524 | std::lock_guard<std::mutex> frame_guard(frame_mutex_); |
| 525 | |
| 526 | if (pointers_.empty()) { |
| 527 | return; |
| 528 | } |
| 529 | |
| 530 | GetList(list, false); |
| 531 | } |
| 532 | |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 533 | void PointerData::GetInfo(uint8_t** info, size_t* overall_size, size_t* info_size, |
| 534 | size_t* total_memory, size_t* backtrace_size) { |
| 535 | std::lock_guard<std::mutex> pointer_guard(pointer_mutex_); |
| 536 | std::lock_guard<std::mutex> frame_guard(frame_mutex_); |
| 537 | |
| 538 | if (pointers_.empty()) { |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | std::vector<ListInfoType> list; |
| 543 | GetUniqueList(&list, true); |
| 544 | if (list.empty()) { |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | *backtrace_size = g_debug->config().backtrace_frames(); |
| 549 | *info_size = sizeof(size_t) * 2 + sizeof(uintptr_t) * *backtrace_size; |
| 550 | *overall_size = *info_size * list.size(); |
| 551 | *info = reinterpret_cast<uint8_t*>(g_dispatch->calloc(*info_size, list.size())); |
| 552 | if (*info == nullptr) { |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | uint8_t* data = *info; |
| 557 | *total_memory = 0; |
| 558 | for (const auto& list_info : list) { |
| 559 | FrameInfoType* frame_info = list_info.frame_info; |
| 560 | *total_memory += list_info.size * list_info.num_allocations; |
| 561 | size_t allocation_size = |
| 562 | PointerInfoType::GetEncodedSize(list_info.zygote_child_alloc, list_info.size); |
| 563 | memcpy(data, &allocation_size, sizeof(size_t)); |
| 564 | memcpy(&data[sizeof(size_t)], &list_info.num_allocations, sizeof(size_t)); |
| 565 | if (frame_info != nullptr) { |
| 566 | memcpy(&data[2 * sizeof(size_t)], frame_info->frames.data(), |
| 567 | frame_info->frames.size() * sizeof(uintptr_t)); |
| 568 | } |
| 569 | data += *info_size; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | bool PointerData::Exists(const void* ptr) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 574 | std::lock_guard<std::mutex> pointer_guard(pointer_mutex_); |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 575 | uintptr_t mangled_ptr = ManglePointer(reinterpret_cast<uintptr_t>(ptr)); |
| 576 | return pointers_.count(mangled_ptr) != 0; |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 577 | } |
| 578 | |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 579 | void PointerData::DumpLiveToFile(int fd) { |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 580 | std::vector<ListInfoType> list; |
| 581 | |
| 582 | std::lock_guard<std::mutex> pointer_guard(pointer_mutex_); |
| 583 | std::lock_guard<std::mutex> frame_guard(frame_mutex_); |
| 584 | GetUniqueList(&list, false); |
| 585 | |
| 586 | size_t total_memory = 0; |
| 587 | for (const auto& info : list) { |
| 588 | total_memory += info.size * info.num_allocations; |
| 589 | } |
| 590 | |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 591 | dprintf(fd, "Total memory: %zu\n", total_memory); |
| 592 | dprintf(fd, "Allocation records: %zd\n", list.size()); |
| 593 | dprintf(fd, "Backtrace size: %zu\n", g_debug->config().backtrace_frames()); |
| 594 | dprintf(fd, "\n"); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 595 | |
| 596 | for (const auto& info : list) { |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 597 | 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] | 598 | info.num_allocations); |
| 599 | FrameInfoType* frame_info = info.frame_info; |
| 600 | if (frame_info != nullptr) { |
| 601 | for (size_t i = 0; i < frame_info->frames.size(); i++) { |
| 602 | if (frame_info->frames[i] == 0) { |
| 603 | break; |
| 604 | } |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 605 | dprintf(fd, " %" PRIxPTR, frame_info->frames[i]); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 606 | } |
| 607 | } |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 608 | dprintf(fd, "\n"); |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 609 | if (info.backtrace_info != nullptr) { |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 610 | dprintf(fd, " bt_info"); |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 611 | for (const auto& frame : *info.backtrace_info) { |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 612 | dprintf(fd, " {"); |
| David Srbecky | 92b8d64 | 2021-05-13 00:03:26 +0100 | [diff] [blame] | 613 | if (frame.map_info != nullptr && !frame.map_info->name().empty()) { |
| 614 | dprintf(fd, "\"%s\"", frame.map_info->name().c_str()); |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 615 | } else { |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 616 | dprintf(fd, "\"\""); |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 617 | } |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 618 | dprintf(fd, " %" PRIx64, frame.rel_pc); |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 619 | if (frame.function_name.empty()) { |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 620 | dprintf(fd, " \"\" 0}"); |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 621 | } else { |
| Christopher Ferris | 3d47086 | 2025-08-13 16:43:58 +0000 | [diff] [blame] | 622 | dprintf(fd, " \"%s\" %" PRIx64 "}", |
| 623 | unwindstack::DemangleNameIfNeeded(frame.function_name).c_str(), |
| 624 | frame.function_offset); |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 625 | } |
| 626 | } |
| Christopher Ferris | ff88fb0 | 2019-11-04 18:40:00 -0800 | [diff] [blame] | 627 | dprintf(fd, "\n"); |
| Christopher Ferris | 93bdd6a | 2018-04-05 11:12:38 -0700 | [diff] [blame] | 628 | } |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | |
| 632 | void PointerData::PrepareFork() NO_THREAD_SAFETY_ANALYSIS { |
| Iris Chang | 76dcc47 | 2019-03-07 12:32:19 +0800 | [diff] [blame] | 633 | free_pointer_mutex_.lock(); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 634 | pointer_mutex_.lock(); |
| 635 | frame_mutex_.lock(); |
| Christopher Ferris | 4da2503 | 2018-03-07 13:38:48 -0800 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | void PointerData::PostForkParent() NO_THREAD_SAFETY_ANALYSIS { |
| 639 | frame_mutex_.unlock(); |
| 640 | pointer_mutex_.unlock(); |
| 641 | free_pointer_mutex_.unlock(); |
| 642 | } |
| 643 | |
| 644 | void PointerData::PostForkChild() __attribute__((no_thread_safety_analysis)) { |
| 645 | // Make sure that any potential mutexes have been released and are back |
| 646 | // to an initial state. |
| 647 | frame_mutex_.try_lock(); |
| 648 | frame_mutex_.unlock(); |
| 649 | pointer_mutex_.try_lock(); |
| 650 | pointer_mutex_.unlock(); |
| 651 | free_pointer_mutex_.try_lock(); |
| 652 | free_pointer_mutex_.unlock(); |
| 653 | } |
| Christopher Ferris | f78486f | 2022-05-04 14:08:54 -0700 | [diff] [blame] | 654 | |
| 655 | void PointerData::IteratePointers(std::function<void(uintptr_t pointer)> fn) { |
| 656 | std::lock_guard<std::mutex> pointer_guard(pointer_mutex_); |
| 657 | for (const auto entry : pointers_) { |
| 658 | fn(DemanglePointer(entry.first)); |
| 659 | } |
| 660 | } |