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