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