Colin Cross | 7add50d | 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 | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 18 | #include <string.h> |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 19 | |
| 20 | #include <functional> |
| 21 | #include <iomanip> |
| 22 | #include <mutex> |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 23 | #include <sstream> |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 24 | #include <string> |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 25 | #include <unordered_map> |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 26 | |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 27 | #include <android-base/macros.h> |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 28 | #include <backtrace.h> |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 29 | |
| 30 | #include "Allocator.h" |
| 31 | #include "HeapWalker.h" |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 32 | #include "Leak.h" |
Colin Cross | 8e8f34c | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 33 | #include "LeakFolding.h" |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 34 | #include "LeakPipe.h" |
| 35 | #include "ProcessMappings.h" |
| 36 | #include "PtracerThread.h" |
| 37 | #include "ScopedDisableMalloc.h" |
| 38 | #include "Semaphore.h" |
| 39 | #include "ThreadCapture.h" |
| 40 | |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 41 | #include "bionic.h" |
| 42 | #include "log.h" |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 43 | #include "memunreachable/memunreachable.h" |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 44 | |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 45 | using namespace std::chrono_literals; |
| 46 | |
Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 47 | namespace android { |
| 48 | |
| 49 | const size_t Leak::contents_length; |
| 50 | |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 51 | class MemUnreachable { |
| 52 | public: |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 53 | MemUnreachable(pid_t pid, Allocator<void> allocator) |
| 54 | : pid_(pid), allocator_(allocator), heap_walker_(allocator_) {} |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 55 | bool CollectAllocations(const allocator::vector<ThreadInfo>& threads, |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 56 | const allocator::vector<Mapping>& mappings); |
| 57 | bool GetUnreachableMemory(allocator::vector<Leak>& leaks, size_t limit, size_t* num_leaks, |
| 58 | size_t* leak_bytes); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 59 | size_t Allocations() { return heap_walker_.Allocations(); } |
| 60 | size_t AllocationBytes() { return heap_walker_.AllocationBytes(); } |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 61 | |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 62 | private: |
| 63 | bool ClassifyMappings(const allocator::vector<Mapping>& mappings, |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 64 | allocator::vector<Mapping>& heap_mappings, |
| 65 | allocator::vector<Mapping>& anon_mappings, |
| 66 | allocator::vector<Mapping>& globals_mappings, |
| 67 | allocator::vector<Mapping>& stack_mappings); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 68 | DISALLOW_COPY_AND_ASSIGN(MemUnreachable); |
| 69 | pid_t pid_; |
| 70 | Allocator<void> allocator_; |
| 71 | HeapWalker heap_walker_; |
| 72 | }; |
| 73 | |
| 74 | static void HeapIterate(const Mapping& heap_mapping, |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 75 | const std::function<void(uintptr_t, size_t)>& func) { |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 76 | malloc_iterate(heap_mapping.begin, heap_mapping.end - heap_mapping.begin, |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 77 | [](uintptr_t base, size_t size, void* arg) { |
| 78 | auto f = reinterpret_cast<const std::function<void(uintptr_t, size_t)>*>(arg); |
| 79 | (*f)(base, size); |
| 80 | }, |
| 81 | const_cast<void*>(reinterpret_cast<const void*>(&func))); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | bool MemUnreachable::CollectAllocations(const allocator::vector<ThreadInfo>& threads, |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 85 | const allocator::vector<Mapping>& mappings) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 86 | MEM_ALOGI("searching process %d for allocations", pid_); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 87 | allocator::vector<Mapping> heap_mappings{mappings}; |
| 88 | allocator::vector<Mapping> anon_mappings{mappings}; |
| 89 | allocator::vector<Mapping> globals_mappings{mappings}; |
| 90 | allocator::vector<Mapping> stack_mappings{mappings}; |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 91 | if (!ClassifyMappings(mappings, heap_mappings, anon_mappings, globals_mappings, stack_mappings)) { |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | |
| 95 | for (auto it = heap_mappings.begin(); it != heap_mappings.end(); it++) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 96 | MEM_ALOGV("Heap mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name); |
| 97 | HeapIterate(*it, |
| 98 | [&](uintptr_t base, size_t size) { heap_walker_.Allocation(base, base + size); }); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | for (auto it = anon_mappings.begin(); it != anon_mappings.end(); it++) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 102 | MEM_ALOGV("Anon mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 103 | heap_walker_.Allocation(it->begin, it->end); |
| 104 | } |
| 105 | |
| 106 | for (auto it = globals_mappings.begin(); it != globals_mappings.end(); it++) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 107 | MEM_ALOGV("Globals mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 108 | heap_walker_.Root(it->begin, it->end); |
| 109 | } |
| 110 | |
| 111 | for (auto thread_it = threads.begin(); thread_it != threads.end(); thread_it++) { |
| 112 | for (auto it = stack_mappings.begin(); it != stack_mappings.end(); it++) { |
| 113 | if (thread_it->stack.first >= it->begin && thread_it->stack.first <= it->end) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 114 | MEM_ALOGV("Stack %" PRIxPTR "-%" PRIxPTR " %s", thread_it->stack.first, it->end, it->name); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 115 | heap_walker_.Root(thread_it->stack.first, it->end); |
| 116 | } |
| 117 | } |
| 118 | heap_walker_.Root(thread_it->regs); |
| 119 | } |
| 120 | |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 121 | MEM_ALOGI("searching done"); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 126 | bool MemUnreachable::GetUnreachableMemory(allocator::vector<Leak>& leaks, size_t limit, |
| 127 | size_t* num_leaks, size_t* leak_bytes) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 128 | MEM_ALOGI("sweeping process %d for unreachable memory", pid_); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 129 | leaks.clear(); |
| 130 | |
Colin Cross | 8e8f34c | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 131 | if (!heap_walker_.DetectLeaks()) { |
| 132 | return false; |
| 133 | } |
| 134 | |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 135 | allocator::vector<Range> leaked1{allocator_}; |
| 136 | heap_walker_.Leaked(leaked1, 0, num_leaks, leak_bytes); |
| 137 | |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 138 | MEM_ALOGI("sweeping done"); |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 139 | |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 140 | MEM_ALOGI("folding related leaks"); |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 141 | |
Colin Cross | 8e8f34c | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 142 | LeakFolding folding(allocator_, heap_walker_); |
| 143 | if (!folding.FoldLeaks()) { |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | allocator::vector<LeakFolding::Leak> leaked{allocator_}; |
| 148 | |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 149 | if (!folding.Leaked(leaked, num_leaks, leak_bytes)) { |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 150 | return false; |
| 151 | } |
| 152 | |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 153 | allocator::unordered_map<Leak::Backtrace, Leak*> backtrace_map{allocator_}; |
| 154 | |
| 155 | // Prevent reallocations of backing memory so we can store pointers into it |
| 156 | // in backtrace_map. |
| 157 | leaks.reserve(leaked.size()); |
| 158 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 159 | for (auto& it : leaked) { |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 160 | leaks.emplace_back(); |
| 161 | Leak* leak = &leaks.back(); |
| 162 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 163 | ssize_t num_backtrace_frames = malloc_backtrace( |
| 164 | reinterpret_cast<void*>(it.range.begin), leak->backtrace.frames, leak->backtrace.max_frames); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 165 | if (num_backtrace_frames > 0) { |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 166 | leak->backtrace.num_frames = num_backtrace_frames; |
| 167 | |
| 168 | auto inserted = backtrace_map.emplace(leak->backtrace, leak); |
| 169 | if (!inserted.second) { |
| 170 | // Leak with same backtrace already exists, drop this one and |
| 171 | // increment similar counts on the existing one. |
| 172 | leaks.pop_back(); |
| 173 | Leak* similar_leak = inserted.first->second; |
| 174 | similar_leak->similar_count++; |
| 175 | similar_leak->similar_size += it.range.size(); |
| 176 | similar_leak->similar_referenced_count += it.referenced_count; |
| 177 | similar_leak->similar_referenced_size += it.referenced_size; |
| 178 | similar_leak->total_size += it.range.size(); |
| 179 | similar_leak->total_size += it.referenced_size; |
| 180 | continue; |
| 181 | } |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 182 | } |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 183 | |
| 184 | leak->begin = it.range.begin; |
| 185 | leak->size = it.range.size(); |
| 186 | leak->referenced_count = it.referenced_count; |
| 187 | leak->referenced_size = it.referenced_size; |
| 188 | leak->total_size = leak->size + leak->referenced_size; |
| 189 | memcpy(leak->contents, reinterpret_cast<void*>(it.range.begin), |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 190 | std::min(leak->size, Leak::contents_length)); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 191 | } |
| 192 | |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 193 | MEM_ALOGI("folding done"); |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 194 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 195 | std::sort(leaks.begin(), leaks.end(), |
| 196 | [](const Leak& a, const Leak& b) { return a.total_size > b.total_size; }); |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 197 | |
| 198 | if (leaks.size() > limit) { |
| 199 | leaks.resize(limit); |
| 200 | } |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 201 | |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | static bool has_prefix(const allocator::string& s, const char* prefix) { |
| 206 | int ret = s.compare(0, strlen(prefix), prefix); |
| 207 | return ret == 0; |
| 208 | } |
| 209 | |
| 210 | bool MemUnreachable::ClassifyMappings(const allocator::vector<Mapping>& mappings, |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 211 | allocator::vector<Mapping>& heap_mappings, |
| 212 | allocator::vector<Mapping>& anon_mappings, |
| 213 | allocator::vector<Mapping>& globals_mappings, |
| 214 | allocator::vector<Mapping>& stack_mappings) { |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 215 | heap_mappings.clear(); |
| 216 | anon_mappings.clear(); |
| 217 | globals_mappings.clear(); |
| 218 | stack_mappings.clear(); |
| 219 | |
| 220 | allocator::string current_lib{allocator_}; |
| 221 | |
| 222 | for (auto it = mappings.begin(); it != mappings.end(); it++) { |
| 223 | if (it->execute) { |
| 224 | current_lib = it->name; |
| 225 | continue; |
| 226 | } |
| 227 | |
| 228 | if (!it->read) { |
| 229 | continue; |
| 230 | } |
| 231 | |
| 232 | const allocator::string mapping_name{it->name, allocator_}; |
| 233 | if (mapping_name == "[anon:.bss]") { |
| 234 | // named .bss section |
| 235 | globals_mappings.emplace_back(*it); |
| 236 | } else if (mapping_name == current_lib) { |
| 237 | // .rodata or .data section |
| 238 | globals_mappings.emplace_back(*it); |
| 239 | } else if (mapping_name == "[anon:libc_malloc]") { |
| 240 | // named malloc mapping |
| 241 | heap_mappings.emplace_back(*it); |
| 242 | } else if (has_prefix(mapping_name, "/dev/ashmem/dalvik")) { |
| 243 | // named dalvik heap mapping |
| 244 | globals_mappings.emplace_back(*it); |
| 245 | } else if (has_prefix(mapping_name, "[stack")) { |
| 246 | // named stack mapping |
| 247 | stack_mappings.emplace_back(*it); |
| 248 | } else if (mapping_name.size() == 0) { |
| 249 | globals_mappings.emplace_back(*it); |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 250 | } else if (has_prefix(mapping_name, "[anon:") && |
| 251 | mapping_name != "[anon:leak_detector_malloc]") { |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 252 | // TODO(ccross): it would be nice to treat named anonymous mappings as |
| 253 | // possible leaks, but naming something in a .bss or .data section makes |
| 254 | // it impossible to distinguish them from mmaped and then named mappings. |
| 255 | globals_mappings.emplace_back(*it); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | return true; |
| 260 | } |
| 261 | |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 262 | template <typename T> |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 263 | static inline const char* plural(T val) { |
| 264 | return (val == 1) ? "" : "s"; |
| 265 | } |
| 266 | |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 267 | bool GetUnreachableMemory(UnreachableMemoryInfo& info, size_t limit) { |
| 268 | int parent_pid = getpid(); |
| 269 | int parent_tid = gettid(); |
| 270 | |
| 271 | Heap heap; |
| 272 | |
| 273 | Semaphore continue_parent_sem; |
| 274 | LeakPipe pipe; |
| 275 | |
| 276 | PtracerThread thread{[&]() -> int { |
| 277 | ///////////////////////////////////////////// |
| 278 | // Collection thread |
| 279 | ///////////////////////////////////////////// |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 280 | MEM_ALOGI("collecting thread info for process %d...", parent_pid); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 281 | |
| 282 | ThreadCapture thread_capture(parent_pid, heap); |
| 283 | allocator::vector<ThreadInfo> thread_info(heap); |
| 284 | allocator::vector<Mapping> mappings(heap); |
| 285 | |
| 286 | // ptrace all the threads |
| 287 | if (!thread_capture.CaptureThreads()) { |
Colin Cross | de42af0 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 288 | continue_parent_sem.Post(); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 289 | return 1; |
| 290 | } |
| 291 | |
| 292 | // collect register contents and stacks |
| 293 | if (!thread_capture.CapturedThreadInfo(thread_info)) { |
Colin Cross | de42af0 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 294 | continue_parent_sem.Post(); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 295 | return 1; |
| 296 | } |
| 297 | |
| 298 | // snapshot /proc/pid/maps |
| 299 | if (!ProcessMappings(parent_pid, mappings)) { |
Colin Cross | de42af0 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 300 | continue_parent_sem.Post(); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 301 | return 1; |
| 302 | } |
| 303 | |
| 304 | // malloc must be enabled to call fork, at_fork handlers take the same |
| 305 | // locks as ScopedDisableMalloc. All threads are paused in ptrace, so |
| 306 | // memory state is still consistent. Unfreeze the original thread so it |
| 307 | // can drop the malloc locks, it will block until the collection thread |
| 308 | // exits. |
| 309 | thread_capture.ReleaseThread(parent_tid); |
| 310 | continue_parent_sem.Post(); |
| 311 | |
| 312 | // fork a process to do the heap walking |
| 313 | int ret = fork(); |
| 314 | if (ret < 0) { |
| 315 | return 1; |
| 316 | } else if (ret == 0) { |
| 317 | ///////////////////////////////////////////// |
| 318 | // Heap walker process |
| 319 | ///////////////////////////////////////////// |
| 320 | // Examine memory state in the child using the data collected above and |
| 321 | // the CoW snapshot of the process memory contents. |
| 322 | |
| 323 | if (!pipe.OpenSender()) { |
| 324 | _exit(1); |
| 325 | } |
| 326 | |
| 327 | MemUnreachable unreachable{parent_pid, heap}; |
| 328 | |
| 329 | if (!unreachable.CollectAllocations(thread_info, mappings)) { |
| 330 | _exit(2); |
| 331 | } |
| 332 | size_t num_allocations = unreachable.Allocations(); |
| 333 | size_t allocation_bytes = unreachable.AllocationBytes(); |
| 334 | |
| 335 | allocator::vector<Leak> leaks{heap}; |
| 336 | |
| 337 | size_t num_leaks = 0; |
| 338 | size_t leak_bytes = 0; |
| 339 | bool ok = unreachable.GetUnreachableMemory(leaks, limit, &num_leaks, &leak_bytes); |
| 340 | |
| 341 | ok = ok && pipe.Sender().Send(num_allocations); |
| 342 | ok = ok && pipe.Sender().Send(allocation_bytes); |
| 343 | ok = ok && pipe.Sender().Send(num_leaks); |
| 344 | ok = ok && pipe.Sender().Send(leak_bytes); |
| 345 | ok = ok && pipe.Sender().SendVector(leaks); |
| 346 | |
| 347 | if (!ok) { |
| 348 | _exit(3); |
| 349 | } |
| 350 | |
| 351 | _exit(0); |
| 352 | } else { |
| 353 | // Nothing left to do in the collection thread, return immediately, |
| 354 | // releasing all the captured threads. |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 355 | MEM_ALOGI("collection thread done"); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | }}; |
| 359 | |
| 360 | ///////////////////////////////////////////// |
| 361 | // Original thread |
| 362 | ///////////////////////////////////////////// |
| 363 | |
| 364 | { |
| 365 | // Disable malloc to get a consistent view of memory |
| 366 | ScopedDisableMalloc disable_malloc; |
| 367 | |
| 368 | // Start the collection thread |
| 369 | thread.Start(); |
| 370 | |
| 371 | // Wait for the collection thread to signal that it is ready to fork the |
| 372 | // heap walker process. |
Colin Cross | de42af0 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 373 | continue_parent_sem.Wait(30s); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 374 | |
| 375 | // Re-enable malloc so the collection thread can fork. |
| 376 | } |
| 377 | |
| 378 | // Wait for the collection thread to exit |
| 379 | int ret = thread.Join(); |
| 380 | if (ret != 0) { |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | // Get a pipe from the heap walker process. Transferring a new pipe fd |
| 385 | // ensures no other forked processes can have it open, so when the heap |
| 386 | // walker process dies the remote side of the pipe will close. |
| 387 | if (!pipe.OpenReceiver()) { |
| 388 | return false; |
| 389 | } |
| 390 | |
| 391 | bool ok = true; |
| 392 | ok = ok && pipe.Receiver().Receive(&info.num_allocations); |
| 393 | ok = ok && pipe.Receiver().Receive(&info.allocation_bytes); |
| 394 | ok = ok && pipe.Receiver().Receive(&info.num_leaks); |
| 395 | ok = ok && pipe.Receiver().Receive(&info.leak_bytes); |
| 396 | ok = ok && pipe.Receiver().ReceiveVector(info.leaks); |
| 397 | if (!ok) { |
| 398 | return false; |
| 399 | } |
| 400 | |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 401 | MEM_ALOGI("unreachable memory detection done"); |
| 402 | MEM_ALOGE("%zu bytes in %zu allocation%s unreachable out of %zu bytes in %zu allocation%s", |
| 403 | info.leak_bytes, info.num_leaks, plural(info.num_leaks), info.allocation_bytes, |
| 404 | info.num_allocations, plural(info.num_allocations)); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 405 | return true; |
| 406 | } |
| 407 | |
| 408 | std::string Leak::ToString(bool log_contents) const { |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 409 | std::ostringstream oss; |
| 410 | |
| 411 | oss << " " << std::dec << size; |
Colin Cross | de42af0 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 412 | oss << " bytes unreachable at "; |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 413 | oss << std::hex << begin; |
| 414 | oss << std::endl; |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 415 | if (referenced_count > 0) { |
| 416 | oss << std::dec; |
| 417 | oss << " referencing " << referenced_size << " unreachable bytes"; |
| 418 | oss << " in " << referenced_count << " allocation" << plural(referenced_count); |
| 419 | oss << std::endl; |
| 420 | } |
| 421 | if (similar_count > 0) { |
| 422 | oss << std::dec; |
| 423 | oss << " and " << similar_size << " similar unreachable bytes"; |
| 424 | oss << " in " << similar_count << " allocation" << plural(similar_count); |
| 425 | oss << std::endl; |
| 426 | if (similar_referenced_count > 0) { |
| 427 | oss << " referencing " << similar_referenced_size << " unreachable bytes"; |
| 428 | oss << " in " << similar_referenced_count << " allocation" << plural(similar_referenced_count); |
| 429 | oss << std::endl; |
| 430 | } |
| 431 | } |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 432 | |
| 433 | if (log_contents) { |
| 434 | const int bytes_per_line = 16; |
| 435 | const size_t bytes = std::min(size, contents_length); |
| 436 | |
| 437 | if (bytes == size) { |
| 438 | oss << " contents:" << std::endl; |
| 439 | } else { |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 440 | oss << " first " << bytes << " bytes of contents:" << std::endl; |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | for (size_t i = 0; i < bytes; i += bytes_per_line) { |
| 444 | oss << " " << std::hex << begin + i << ": "; |
| 445 | size_t j; |
| 446 | oss << std::setfill('0'); |
| 447 | for (j = i; j < bytes && j < i + bytes_per_line; j++) { |
| 448 | oss << std::setw(2) << static_cast<int>(contents[j]) << " "; |
| 449 | } |
| 450 | oss << std::setfill(' '); |
| 451 | for (; j < i + bytes_per_line; j++) { |
| 452 | oss << " "; |
| 453 | } |
| 454 | for (j = i; j < bytes && j < i + bytes_per_line; j++) { |
| 455 | char c = contents[j]; |
| 456 | if (c < ' ' || c >= 0x7f) { |
| 457 | c = '.'; |
| 458 | } |
| 459 | oss << c; |
| 460 | } |
| 461 | oss << std::endl; |
| 462 | } |
| 463 | } |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 464 | if (backtrace.num_frames > 0) { |
| 465 | oss << backtrace_string(backtrace.frames, backtrace.num_frames); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | return oss.str(); |
| 469 | } |
| 470 | |
Colin Cross | 11185af | 2016-03-04 16:37:02 -0800 | [diff] [blame] | 471 | // Figure out the abi based on defined macros. |
| 472 | #if defined(__arm__) |
| 473 | #define ABI_STRING "arm" |
| 474 | #elif defined(__aarch64__) |
| 475 | #define ABI_STRING "arm64" |
| 476 | #elif defined(__mips__) && !defined(__LP64__) |
| 477 | #define ABI_STRING "mips" |
| 478 | #elif defined(__mips__) && defined(__LP64__) |
| 479 | #define ABI_STRING "mips64" |
| 480 | #elif defined(__i386__) |
| 481 | #define ABI_STRING "x86" |
| 482 | #elif defined(__x86_64__) |
| 483 | #define ABI_STRING "x86_64" |
| 484 | #else |
| 485 | #error "Unsupported ABI" |
| 486 | #endif |
| 487 | |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 488 | std::string UnreachableMemoryInfo::ToString(bool log_contents) const { |
| 489 | std::ostringstream oss; |
| 490 | oss << " " << leak_bytes << " bytes in "; |
Colin Cross | 7a22e81 | 2016-03-04 16:36:12 -0800 | [diff] [blame] | 491 | oss << num_leaks << " unreachable allocation" << plural(num_leaks); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 492 | oss << std::endl; |
Colin Cross | 11185af | 2016-03-04 16:37:02 -0800 | [diff] [blame] | 493 | oss << " ABI: '" ABI_STRING "'" << std::endl; |
| 494 | oss << std::endl; |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 495 | |
| 496 | for (auto it = leaks.begin(); it != leaks.end(); it++) { |
Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 497 | oss << it->ToString(log_contents); |
| 498 | oss << std::endl; |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | return oss.str(); |
| 502 | } |
| 503 | |
| 504 | std::string GetUnreachableMemoryString(bool log_contents, size_t limit) { |
| 505 | UnreachableMemoryInfo info; |
| 506 | if (!GetUnreachableMemory(info, limit)) { |
Colin Cross | 72d3881 | 2017-06-13 16:41:58 -0700 | [diff] [blame] | 507 | return "Failed to get unreachable memory\n" |
| 508 | "If you are trying to get unreachable memory from a system app\n" |
| 509 | "(like com.android.systemui), disable selinux first using\n" |
| 510 | "setenforce 0\n"; |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | return info.ToString(log_contents); |
| 514 | } |
| 515 | |
Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 516 | } // namespace android |
| 517 | |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 518 | bool LogUnreachableMemory(bool log_contents, size_t limit) { |
Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 519 | android::UnreachableMemoryInfo info; |
| 520 | if (!android::GetUnreachableMemory(info, limit)) { |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 521 | return false; |
| 522 | } |
| 523 | |
| 524 | for (auto it = info.leaks.begin(); it != info.leaks.end(); it++) { |
Christopher Ferris | 47dea71 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 525 | MEM_ALOGE("%s", it->ToString(log_contents).c_str()); |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 526 | } |
| 527 | return true; |
| 528 | } |
| 529 | |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 530 | bool NoLeaks() { |
Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 531 | android::UnreachableMemoryInfo info; |
| 532 | if (!android::GetUnreachableMemory(info, 0)) { |
Colin Cross | 7add50d | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 533 | return false; |
| 534 | } |
| 535 | |
| 536 | return info.num_leaks == 0; |
| 537 | } |