Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <inttypes.h> |
Colin Cross | 1fa81f5 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 18 | #include <string.h> |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 19 | |
| 20 | #include <functional> |
| 21 | #include <iomanip> |
| 22 | #include <mutex> |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 23 | #include <sstream> |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 24 | #include <string> |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 25 | #include <unordered_map> |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 26 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 27 | #include <android-base/macros.h> |
Christopher Ferris | e09e7ed | 2019-09-18 10:30:33 -0700 | [diff] [blame] | 28 | #include <android-base/strings.h> |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 29 | #include <backtrace.h> |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 30 | |
| 31 | #include "Allocator.h" |
Colin Cross | 0b1e8d3 | 2022-05-11 15:17:55 -0700 | [diff] [blame] | 32 | #include "AtomicState.h" |
Colin Cross | a64425e | 2017-06-20 18:07:29 -0700 | [diff] [blame] | 33 | #include "Binder.h" |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 34 | #include "HeapWalker.h" |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 35 | #include "Leak.h" |
Colin Cross | 6f922a4 | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 36 | #include "LeakFolding.h" |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 37 | #include "LeakPipe.h" |
| 38 | #include "ProcessMappings.h" |
| 39 | #include "PtracerThread.h" |
| 40 | #include "ScopedDisableMalloc.h" |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 41 | #include "ThreadCapture.h" |
| 42 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 43 | #include "bionic.h" |
| 44 | #include "log.h" |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 45 | #include "memunreachable/memunreachable.h" |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 46 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 47 | using namespace std::chrono_literals; |
| 48 | |
Colin Cross | 1fa81f5 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 49 | namespace android { |
| 50 | |
| 51 | const size_t Leak::contents_length; |
| 52 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 53 | class MemUnreachable { |
| 54 | public: |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 55 | MemUnreachable(pid_t pid, Allocator<void> allocator) |
| 56 | : pid_(pid), allocator_(allocator), heap_walker_(allocator_) {} |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 57 | bool CollectAllocations(const allocator::vector<ThreadInfo>& threads, |
Colin Cross | a64425e | 2017-06-20 18:07:29 -0700 | [diff] [blame] | 58 | const allocator::vector<Mapping>& mappings, |
| 59 | const allocator::vector<uintptr_t>& refs); |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 60 | bool GetUnreachableMemory(allocator::vector<Leak>& leaks, size_t limit, size_t* num_leaks, |
| 61 | size_t* leak_bytes); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 62 | size_t Allocations() { return heap_walker_.Allocations(); } |
| 63 | size_t AllocationBytes() { return heap_walker_.AllocationBytes(); } |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 64 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 65 | private: |
| 66 | bool ClassifyMappings(const allocator::vector<Mapping>& mappings, |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 67 | allocator::vector<Mapping>& heap_mappings, |
| 68 | allocator::vector<Mapping>& anon_mappings, |
| 69 | allocator::vector<Mapping>& globals_mappings, |
| 70 | allocator::vector<Mapping>& stack_mappings); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 71 | DISALLOW_COPY_AND_ASSIGN(MemUnreachable); |
| 72 | pid_t pid_; |
| 73 | Allocator<void> allocator_; |
| 74 | HeapWalker heap_walker_; |
| 75 | }; |
| 76 | |
| 77 | static void HeapIterate(const Mapping& heap_mapping, |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 78 | const std::function<void(uintptr_t, size_t)>& func) { |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 79 | malloc_iterate(heap_mapping.begin, heap_mapping.end - heap_mapping.begin, |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 80 | [](uintptr_t base, size_t size, void* arg) { |
| 81 | auto f = reinterpret_cast<const std::function<void(uintptr_t, size_t)>*>(arg); |
| 82 | (*f)(base, size); |
| 83 | }, |
| 84 | const_cast<void*>(reinterpret_cast<const void*>(&func))); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | bool MemUnreachable::CollectAllocations(const allocator::vector<ThreadInfo>& threads, |
Colin Cross | a64425e | 2017-06-20 18:07:29 -0700 | [diff] [blame] | 88 | const allocator::vector<Mapping>& mappings, |
| 89 | const allocator::vector<uintptr_t>& refs) { |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 90 | MEM_ALOGI("searching process %d for allocations", pid_); |
Colin Cross | 223069f | 2018-11-28 17:01:59 -0800 | [diff] [blame] | 91 | |
| 92 | for (auto it = mappings.begin(); it != mappings.end(); it++) { |
| 93 | heap_walker_.Mapping(it->begin, it->end); |
| 94 | } |
| 95 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 96 | allocator::vector<Mapping> heap_mappings{mappings}; |
| 97 | allocator::vector<Mapping> anon_mappings{mappings}; |
| 98 | allocator::vector<Mapping> globals_mappings{mappings}; |
| 99 | allocator::vector<Mapping> stack_mappings{mappings}; |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 100 | if (!ClassifyMappings(mappings, heap_mappings, anon_mappings, globals_mappings, stack_mappings)) { |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 101 | return false; |
| 102 | } |
| 103 | |
| 104 | for (auto it = heap_mappings.begin(); it != heap_mappings.end(); it++) { |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 105 | MEM_ALOGV("Heap mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name); |
| 106 | HeapIterate(*it, |
| 107 | [&](uintptr_t base, size_t size) { heap_walker_.Allocation(base, base + size); }); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | for (auto it = anon_mappings.begin(); it != anon_mappings.end(); it++) { |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 111 | MEM_ALOGV("Anon mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 112 | heap_walker_.Allocation(it->begin, it->end); |
| 113 | } |
| 114 | |
| 115 | for (auto it = globals_mappings.begin(); it != globals_mappings.end(); it++) { |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 116 | MEM_ALOGV("Globals mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 117 | heap_walker_.Root(it->begin, it->end); |
| 118 | } |
| 119 | |
| 120 | for (auto thread_it = threads.begin(); thread_it != threads.end(); thread_it++) { |
| 121 | for (auto it = stack_mappings.begin(); it != stack_mappings.end(); it++) { |
| 122 | if (thread_it->stack.first >= it->begin && thread_it->stack.first <= it->end) { |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 123 | MEM_ALOGV("Stack %" PRIxPTR "-%" PRIxPTR " %s", thread_it->stack.first, it->end, it->name); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 124 | heap_walker_.Root(thread_it->stack.first, it->end); |
| 125 | } |
| 126 | } |
| 127 | heap_walker_.Root(thread_it->regs); |
| 128 | } |
| 129 | |
Colin Cross | a64425e | 2017-06-20 18:07:29 -0700 | [diff] [blame] | 130 | heap_walker_.Root(refs); |
| 131 | |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 132 | MEM_ALOGI("searching done"); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 133 | |
| 134 | return true; |
| 135 | } |
| 136 | |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 137 | bool MemUnreachable::GetUnreachableMemory(allocator::vector<Leak>& leaks, size_t limit, |
| 138 | size_t* num_leaks, size_t* leak_bytes) { |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 139 | MEM_ALOGI("sweeping process %d for unreachable memory", pid_); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 140 | leaks.clear(); |
| 141 | |
Colin Cross | 6f922a4 | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 142 | if (!heap_walker_.DetectLeaks()) { |
| 143 | return false; |
| 144 | } |
| 145 | |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 146 | allocator::vector<Range> leaked1{allocator_}; |
| 147 | heap_walker_.Leaked(leaked1, 0, num_leaks, leak_bytes); |
| 148 | |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 149 | MEM_ALOGI("sweeping done"); |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 150 | |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 151 | MEM_ALOGI("folding related leaks"); |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 152 | |
Colin Cross | 6f922a4 | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 153 | LeakFolding folding(allocator_, heap_walker_); |
| 154 | if (!folding.FoldLeaks()) { |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | allocator::vector<LeakFolding::Leak> leaked{allocator_}; |
| 159 | |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 160 | if (!folding.Leaked(leaked, num_leaks, leak_bytes)) { |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 161 | return false; |
| 162 | } |
| 163 | |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 164 | allocator::unordered_map<Leak::Backtrace, Leak*> backtrace_map{allocator_}; |
| 165 | |
| 166 | // Prevent reallocations of backing memory so we can store pointers into it |
| 167 | // in backtrace_map. |
| 168 | leaks.reserve(leaked.size()); |
| 169 | |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 170 | for (auto& it : leaked) { |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 171 | leaks.emplace_back(); |
| 172 | Leak* leak = &leaks.back(); |
| 173 | |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 174 | ssize_t num_backtrace_frames = malloc_backtrace( |
| 175 | reinterpret_cast<void*>(it.range.begin), leak->backtrace.frames, leak->backtrace.max_frames); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 176 | if (num_backtrace_frames > 0) { |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 177 | leak->backtrace.num_frames = num_backtrace_frames; |
| 178 | |
| 179 | auto inserted = backtrace_map.emplace(leak->backtrace, leak); |
| 180 | if (!inserted.second) { |
| 181 | // Leak with same backtrace already exists, drop this one and |
| 182 | // increment similar counts on the existing one. |
| 183 | leaks.pop_back(); |
| 184 | Leak* similar_leak = inserted.first->second; |
| 185 | similar_leak->similar_count++; |
| 186 | similar_leak->similar_size += it.range.size(); |
| 187 | similar_leak->similar_referenced_count += it.referenced_count; |
| 188 | similar_leak->similar_referenced_size += it.referenced_size; |
| 189 | similar_leak->total_size += it.range.size(); |
| 190 | similar_leak->total_size += it.referenced_size; |
| 191 | continue; |
| 192 | } |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 193 | } |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 194 | |
| 195 | leak->begin = it.range.begin; |
| 196 | leak->size = it.range.size(); |
| 197 | leak->referenced_count = it.referenced_count; |
| 198 | leak->referenced_size = it.referenced_size; |
| 199 | leak->total_size = leak->size + leak->referenced_size; |
| 200 | memcpy(leak->contents, reinterpret_cast<void*>(it.range.begin), |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 201 | std::min(leak->size, Leak::contents_length)); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 204 | MEM_ALOGI("folding done"); |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 205 | |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 206 | std::sort(leaks.begin(), leaks.end(), |
| 207 | [](const Leak& a, const Leak& b) { return a.total_size > b.total_size; }); |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 208 | |
| 209 | if (leaks.size() > limit) { |
| 210 | leaks.resize(limit); |
| 211 | } |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 212 | |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | static bool has_prefix(const allocator::string& s, const char* prefix) { |
| 217 | int ret = s.compare(0, strlen(prefix), prefix); |
| 218 | return ret == 0; |
| 219 | } |
| 220 | |
Evgenii Stepanov | 94485fa | 2019-03-19 17:17:47 -0700 | [diff] [blame] | 221 | static bool is_sanitizer_mapping(const allocator::string& s) { |
| 222 | return s == "[anon:low shadow]" || s == "[anon:high shadow]" || has_prefix(s, "[anon:hwasan"); |
| 223 | } |
| 224 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 225 | bool MemUnreachable::ClassifyMappings(const allocator::vector<Mapping>& mappings, |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 226 | allocator::vector<Mapping>& heap_mappings, |
| 227 | allocator::vector<Mapping>& anon_mappings, |
| 228 | allocator::vector<Mapping>& globals_mappings, |
| 229 | allocator::vector<Mapping>& stack_mappings) { |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 230 | heap_mappings.clear(); |
| 231 | anon_mappings.clear(); |
| 232 | globals_mappings.clear(); |
| 233 | stack_mappings.clear(); |
| 234 | |
| 235 | allocator::string current_lib{allocator_}; |
| 236 | |
| 237 | for (auto it = mappings.begin(); it != mappings.end(); it++) { |
| 238 | if (it->execute) { |
| 239 | current_lib = it->name; |
| 240 | continue; |
| 241 | } |
| 242 | |
| 243 | if (!it->read) { |
| 244 | continue; |
| 245 | } |
| 246 | |
| 247 | const allocator::string mapping_name{it->name, allocator_}; |
| 248 | if (mapping_name == "[anon:.bss]") { |
| 249 | // named .bss section |
| 250 | globals_mappings.emplace_back(*it); |
| 251 | } else if (mapping_name == current_lib) { |
| 252 | // .rodata or .data section |
| 253 | globals_mappings.emplace_back(*it); |
Christopher Ferris | e09e7ed | 2019-09-18 10:30:33 -0700 | [diff] [blame] | 254 | } else if (mapping_name == "[anon:libc_malloc]" || |
Mitch Phillips | 56c0a4b | 2020-02-12 08:50:14 -0800 | [diff] [blame] | 255 | android::base::StartsWith(mapping_name, "[anon:scudo:") || |
| 256 | android::base::StartsWith(mapping_name, "[anon:GWP-ASan")) { |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 257 | // named malloc mapping |
| 258 | heap_mappings.emplace_back(*it); |
Joel Fernandes | 9078321 | 2018-08-24 11:57:57 -0700 | [diff] [blame] | 259 | } else if (has_prefix(mapping_name, "[anon:dalvik-")) { |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 260 | // named dalvik heap mapping |
| 261 | globals_mappings.emplace_back(*it); |
| 262 | } else if (has_prefix(mapping_name, "[stack")) { |
| 263 | // named stack mapping |
| 264 | stack_mappings.emplace_back(*it); |
| 265 | } else if (mapping_name.size() == 0) { |
| 266 | globals_mappings.emplace_back(*it); |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 267 | } else if (has_prefix(mapping_name, "[anon:") && |
Evgenii Stepanov | 94485fa | 2019-03-19 17:17:47 -0700 | [diff] [blame] | 268 | mapping_name != "[anon:leak_detector_malloc]" && |
| 269 | !is_sanitizer_mapping(mapping_name)) { |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 270 | // TODO(ccross): it would be nice to treat named anonymous mappings as |
| 271 | // possible leaks, but naming something in a .bss or .data section makes |
| 272 | // it impossible to distinguish them from mmaped and then named mappings. |
| 273 | globals_mappings.emplace_back(*it); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return true; |
| 278 | } |
| 279 | |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 280 | template <typename T> |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 281 | static inline const char* plural(T val) { |
| 282 | return (val == 1) ? "" : "s"; |
| 283 | } |
| 284 | |
Colin Cross | 0b1e8d3 | 2022-05-11 15:17:55 -0700 | [diff] [blame] | 285 | enum State { |
| 286 | STARTING = 0, |
| 287 | PAUSING, |
| 288 | COLLECTING, |
| 289 | ABORT, |
| 290 | }; |
| 291 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 292 | bool GetUnreachableMemory(UnreachableMemoryInfo& info, size_t limit) { |
Colin Cross | 375c4d3 | 2019-05-20 12:55:33 -0700 | [diff] [blame] | 293 | if (info.version > 0) { |
| 294 | MEM_ALOGE("unsupported UnreachableMemoryInfo.version %zu in GetUnreachableMemory", |
| 295 | info.version); |
| 296 | return false; |
| 297 | } |
| 298 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 299 | int parent_pid = getpid(); |
| 300 | int parent_tid = gettid(); |
| 301 | |
| 302 | Heap heap; |
| 303 | |
Colin Cross | 0b1e8d3 | 2022-05-11 15:17:55 -0700 | [diff] [blame] | 304 | AtomicState<State> state(STARTING); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 305 | LeakPipe pipe; |
| 306 | |
| 307 | PtracerThread thread{[&]() -> int { |
| 308 | ///////////////////////////////////////////// |
| 309 | // Collection thread |
| 310 | ///////////////////////////////////////////// |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 311 | MEM_ALOGI("collecting thread info for process %d...", parent_pid); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 312 | |
Colin Cross | 0b1e8d3 | 2022-05-11 15:17:55 -0700 | [diff] [blame] | 313 | if (!state.transition_or(STARTING, PAUSING, [&] { |
| 314 | MEM_ALOGI("collecting thread expected state STARTING, aborting"); |
| 315 | return ABORT; |
| 316 | })) { |
| 317 | return 1; |
| 318 | } |
| 319 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 320 | ThreadCapture thread_capture(parent_pid, heap); |
| 321 | allocator::vector<ThreadInfo> thread_info(heap); |
| 322 | allocator::vector<Mapping> mappings(heap); |
Colin Cross | a64425e | 2017-06-20 18:07:29 -0700 | [diff] [blame] | 323 | allocator::vector<uintptr_t> refs(heap); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 324 | |
| 325 | // ptrace all the threads |
| 326 | if (!thread_capture.CaptureThreads()) { |
Colin Cross | 0b1e8d3 | 2022-05-11 15:17:55 -0700 | [diff] [blame] | 327 | state.set(ABORT); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 328 | return 1; |
| 329 | } |
| 330 | |
| 331 | // collect register contents and stacks |
| 332 | if (!thread_capture.CapturedThreadInfo(thread_info)) { |
Colin Cross | 0b1e8d3 | 2022-05-11 15:17:55 -0700 | [diff] [blame] | 333 | state.set(ABORT); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 334 | return 1; |
| 335 | } |
| 336 | |
| 337 | // snapshot /proc/pid/maps |
| 338 | if (!ProcessMappings(parent_pid, mappings)) { |
Colin Cross | 0b1e8d3 | 2022-05-11 15:17:55 -0700 | [diff] [blame] | 339 | state.set(ABORT); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 340 | return 1; |
| 341 | } |
| 342 | |
Colin Cross | a64425e | 2017-06-20 18:07:29 -0700 | [diff] [blame] | 343 | if (!BinderReferences(refs)) { |
Colin Cross | 0b1e8d3 | 2022-05-11 15:17:55 -0700 | [diff] [blame] | 344 | state.set(ABORT); |
| 345 | return 1; |
| 346 | } |
| 347 | |
| 348 | // Atomically update the state from PAUSING to COLLECTING. |
| 349 | // The main thread may have given up waiting for this thread to finish |
| 350 | // pausing, in which case it will have changed the state to ABORT. |
| 351 | if (!state.transition_or(PAUSING, COLLECTING, [&] { |
| 352 | MEM_ALOGI("collecting thread aborting"); |
| 353 | return ABORT; |
| 354 | })) { |
Colin Cross | a64425e | 2017-06-20 18:07:29 -0700 | [diff] [blame] | 355 | return 1; |
| 356 | } |
| 357 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 358 | // malloc must be enabled to call fork, at_fork handlers take the same |
| 359 | // locks as ScopedDisableMalloc. All threads are paused in ptrace, so |
| 360 | // memory state is still consistent. Unfreeze the original thread so it |
| 361 | // can drop the malloc locks, it will block until the collection thread |
| 362 | // exits. |
| 363 | thread_capture.ReleaseThread(parent_tid); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 364 | |
| 365 | // fork a process to do the heap walking |
| 366 | int ret = fork(); |
| 367 | if (ret < 0) { |
| 368 | return 1; |
| 369 | } else if (ret == 0) { |
| 370 | ///////////////////////////////////////////// |
| 371 | // Heap walker process |
| 372 | ///////////////////////////////////////////// |
| 373 | // Examine memory state in the child using the data collected above and |
| 374 | // the CoW snapshot of the process memory contents. |
| 375 | |
| 376 | if (!pipe.OpenSender()) { |
| 377 | _exit(1); |
| 378 | } |
| 379 | |
| 380 | MemUnreachable unreachable{parent_pid, heap}; |
| 381 | |
Colin Cross | a64425e | 2017-06-20 18:07:29 -0700 | [diff] [blame] | 382 | if (!unreachable.CollectAllocations(thread_info, mappings, refs)) { |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 383 | _exit(2); |
| 384 | } |
| 385 | size_t num_allocations = unreachable.Allocations(); |
| 386 | size_t allocation_bytes = unreachable.AllocationBytes(); |
| 387 | |
| 388 | allocator::vector<Leak> leaks{heap}; |
| 389 | |
| 390 | size_t num_leaks = 0; |
| 391 | size_t leak_bytes = 0; |
| 392 | bool ok = unreachable.GetUnreachableMemory(leaks, limit, &num_leaks, &leak_bytes); |
| 393 | |
| 394 | ok = ok && pipe.Sender().Send(num_allocations); |
| 395 | ok = ok && pipe.Sender().Send(allocation_bytes); |
| 396 | ok = ok && pipe.Sender().Send(num_leaks); |
| 397 | ok = ok && pipe.Sender().Send(leak_bytes); |
| 398 | ok = ok && pipe.Sender().SendVector(leaks); |
| 399 | |
| 400 | if (!ok) { |
| 401 | _exit(3); |
| 402 | } |
| 403 | |
| 404 | _exit(0); |
| 405 | } else { |
| 406 | // Nothing left to do in the collection thread, return immediately, |
| 407 | // releasing all the captured threads. |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 408 | MEM_ALOGI("collection thread done"); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 409 | return 0; |
| 410 | } |
| 411 | }}; |
| 412 | |
| 413 | ///////////////////////////////////////////// |
| 414 | // Original thread |
| 415 | ///////////////////////////////////////////// |
| 416 | |
| 417 | { |
| 418 | // Disable malloc to get a consistent view of memory |
| 419 | ScopedDisableMalloc disable_malloc; |
| 420 | |
| 421 | // Start the collection thread |
| 422 | thread.Start(); |
| 423 | |
| 424 | // Wait for the collection thread to signal that it is ready to fork the |
| 425 | // heap walker process. |
Colin Cross | 0b1e8d3 | 2022-05-11 15:17:55 -0700 | [diff] [blame] | 426 | if (!state.wait_for_either_of(COLLECTING, ABORT, 30s)) { |
| 427 | // The pausing didn't finish within 30 seconds, attempt to atomically |
| 428 | // update the state from PAUSING to ABORT. The collecting thread |
| 429 | // may have raced with the timeout and already updated the state to |
| 430 | // COLLECTING, in which case aborting is not necessary. |
| 431 | if (state.transition(PAUSING, ABORT)) { |
| 432 | MEM_ALOGI("main thread timed out waiting for collecting thread"); |
| 433 | } |
| 434 | } |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 435 | |
| 436 | // Re-enable malloc so the collection thread can fork. |
| 437 | } |
| 438 | |
| 439 | // Wait for the collection thread to exit |
| 440 | int ret = thread.Join(); |
| 441 | if (ret != 0) { |
| 442 | return false; |
| 443 | } |
| 444 | |
| 445 | // Get a pipe from the heap walker process. Transferring a new pipe fd |
| 446 | // ensures no other forked processes can have it open, so when the heap |
| 447 | // walker process dies the remote side of the pipe will close. |
| 448 | if (!pipe.OpenReceiver()) { |
| 449 | return false; |
| 450 | } |
| 451 | |
| 452 | bool ok = true; |
| 453 | ok = ok && pipe.Receiver().Receive(&info.num_allocations); |
| 454 | ok = ok && pipe.Receiver().Receive(&info.allocation_bytes); |
| 455 | ok = ok && pipe.Receiver().Receive(&info.num_leaks); |
| 456 | ok = ok && pipe.Receiver().Receive(&info.leak_bytes); |
| 457 | ok = ok && pipe.Receiver().ReceiveVector(info.leaks); |
| 458 | if (!ok) { |
| 459 | return false; |
| 460 | } |
| 461 | |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 462 | MEM_ALOGI("unreachable memory detection done"); |
| 463 | MEM_ALOGE("%zu bytes in %zu allocation%s unreachable out of %zu bytes in %zu allocation%s", |
| 464 | info.leak_bytes, info.num_leaks, plural(info.num_leaks), info.allocation_bytes, |
| 465 | info.num_allocations, plural(info.num_allocations)); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 466 | return true; |
| 467 | } |
| 468 | |
| 469 | std::string Leak::ToString(bool log_contents) const { |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 470 | std::ostringstream oss; |
| 471 | |
| 472 | oss << " " << std::dec << size; |
Colin Cross | 4bd8204 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 473 | oss << " bytes unreachable at "; |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 474 | oss << std::hex << begin; |
| 475 | oss << std::endl; |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 476 | if (referenced_count > 0) { |
| 477 | oss << std::dec; |
| 478 | oss << " referencing " << referenced_size << " unreachable bytes"; |
| 479 | oss << " in " << referenced_count << " allocation" << plural(referenced_count); |
| 480 | oss << std::endl; |
| 481 | } |
| 482 | if (similar_count > 0) { |
| 483 | oss << std::dec; |
| 484 | oss << " and " << similar_size << " similar unreachable bytes"; |
| 485 | oss << " in " << similar_count << " allocation" << plural(similar_count); |
| 486 | oss << std::endl; |
| 487 | if (similar_referenced_count > 0) { |
| 488 | oss << " referencing " << similar_referenced_size << " unreachable bytes"; |
| 489 | oss << " in " << similar_referenced_count << " allocation" << plural(similar_referenced_count); |
| 490 | oss << std::endl; |
| 491 | } |
| 492 | } |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 493 | |
| 494 | if (log_contents) { |
| 495 | const int bytes_per_line = 16; |
| 496 | const size_t bytes = std::min(size, contents_length); |
| 497 | |
| 498 | if (bytes == size) { |
| 499 | oss << " contents:" << std::endl; |
| 500 | } else { |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 501 | oss << " first " << bytes << " bytes of contents:" << std::endl; |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | for (size_t i = 0; i < bytes; i += bytes_per_line) { |
| 505 | oss << " " << std::hex << begin + i << ": "; |
| 506 | size_t j; |
| 507 | oss << std::setfill('0'); |
| 508 | for (j = i; j < bytes && j < i + bytes_per_line; j++) { |
| 509 | oss << std::setw(2) << static_cast<int>(contents[j]) << " "; |
| 510 | } |
| 511 | oss << std::setfill(' '); |
| 512 | for (; j < i + bytes_per_line; j++) { |
| 513 | oss << " "; |
| 514 | } |
| 515 | for (j = i; j < bytes && j < i + bytes_per_line; j++) { |
| 516 | char c = contents[j]; |
| 517 | if (c < ' ' || c >= 0x7f) { |
| 518 | c = '.'; |
| 519 | } |
| 520 | oss << c; |
| 521 | } |
| 522 | oss << std::endl; |
| 523 | } |
| 524 | } |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 525 | if (backtrace.num_frames > 0) { |
| 526 | oss << backtrace_string(backtrace.frames, backtrace.num_frames); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | return oss.str(); |
| 530 | } |
| 531 | |
| 532 | std::string UnreachableMemoryInfo::ToString(bool log_contents) const { |
| 533 | std::ostringstream oss; |
| 534 | oss << " " << leak_bytes << " bytes in "; |
Colin Cross | 783edb0 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 535 | oss << num_leaks << " unreachable allocation" << plural(num_leaks); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 536 | oss << std::endl; |
Colin Cross | 6dad00c | 2016-03-04 16:37:02 -0800 | [diff] [blame] | 537 | oss << " ABI: '" ABI_STRING "'" << std::endl; |
| 538 | oss << std::endl; |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 539 | |
| 540 | for (auto it = leaks.begin(); it != leaks.end(); it++) { |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 541 | oss << it->ToString(log_contents); |
| 542 | oss << std::endl; |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | return oss.str(); |
| 546 | } |
| 547 | |
Colin Cross | 2bc0d78 | 2018-05-14 14:55:39 -0700 | [diff] [blame] | 548 | UnreachableMemoryInfo::~UnreachableMemoryInfo() { |
| 549 | // Clear the memory that holds the leaks, otherwise the next attempt to |
| 550 | // detect leaks may find the old data (for example in the jemalloc tcache) |
| 551 | // and consider all the leaks to be referenced. |
| 552 | memset(leaks.data(), 0, leaks.capacity() * sizeof(Leak)); |
| 553 | |
| 554 | std::vector<Leak> tmp; |
| 555 | leaks.swap(tmp); |
| 556 | |
| 557 | // Disable and re-enable malloc to flush the jemalloc tcache to make sure |
| 558 | // there are no copies of the leaked pointer addresses there. |
| 559 | malloc_disable(); |
| 560 | malloc_enable(); |
| 561 | } |
| 562 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 563 | std::string GetUnreachableMemoryString(bool log_contents, size_t limit) { |
| 564 | UnreachableMemoryInfo info; |
| 565 | if (!GetUnreachableMemory(info, limit)) { |
Colin Cross | c204277 | 2017-06-13 16:41:58 -0700 | [diff] [blame] | 566 | return "Failed to get unreachable memory\n" |
| 567 | "If you are trying to get unreachable memory from a system app\n" |
| 568 | "(like com.android.systemui), disable selinux first using\n" |
| 569 | "setenforce 0\n"; |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | return info.ToString(log_contents); |
| 573 | } |
| 574 | |
Colin Cross | 1fa81f5 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 575 | } // namespace android |
| 576 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 577 | bool LogUnreachableMemory(bool log_contents, size_t limit) { |
Colin Cross | 1fa81f5 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 578 | android::UnreachableMemoryInfo info; |
| 579 | if (!android::GetUnreachableMemory(info, limit)) { |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 580 | return false; |
| 581 | } |
| 582 | |
| 583 | for (auto it = info.leaks.begin(); it != info.leaks.end(); it++) { |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 584 | MEM_ALOGE("%s", it->ToString(log_contents).c_str()); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 585 | } |
| 586 | return true; |
| 587 | } |
| 588 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 589 | bool NoLeaks() { |
Colin Cross | 1fa81f5 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 590 | android::UnreachableMemoryInfo info; |
| 591 | if (!android::GetUnreachableMemory(info, 0)) { |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 592 | return false; |
| 593 | } |
| 594 | |
| 595 | return info.num_leaks == 0; |
| 596 | } |