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 #include <string.h>
19
20 #include <functional>
21 #include <iomanip>
22 #include <mutex>
23 #include <sstream>
24 #include <string>
25 #include <unordered_map>
26
27 #include <android-base/macros.h>
28 #include <android-base/strings.h>
29 #include <backtrace.h>
30
31 #include "Allocator.h"
32 #include "AtomicState.h"
33 #include "Binder.h"
34 #include "HeapWalker.h"
35 #include "Leak.h"
36 #include "LeakFolding.h"
37 #include "LeakPipe.h"
38 #include "ProcessMappings.h"
39 #include "PtracerThread.h"
40 #include "ScopedDisableMalloc.h"
41 #include "ThreadCapture.h"
42
43 #include "bionic.h"
44 #include "log.h"
45 #include "memunreachable/memunreachable.h"
46
47 using namespace std::chrono_literals;
48
49 namespace android {
50
51 const size_t Leak::contents_length;
52
53 class MemUnreachable {
54 public:
MemUnreachable(pid_t pid,Allocator<void> allocator)55 MemUnreachable(pid_t pid, Allocator<void> allocator)
56 : pid_(pid), allocator_(allocator), heap_walker_(allocator_) {}
57 bool CollectAllocations(const allocator::vector<ThreadInfo>& threads,
58 const allocator::vector<Mapping>& mappings,
59 const allocator::vector<uintptr_t>& refs);
60 bool GetUnreachableMemory(allocator::vector<Leak>& leaks, size_t limit, size_t* num_leaks,
61 size_t* leak_bytes);
Allocations()62 size_t Allocations() { return heap_walker_.Allocations(); }
AllocationBytes()63 size_t AllocationBytes() { return heap_walker_.AllocationBytes(); }
64
65 private:
66 bool ClassifyMappings(const allocator::vector<Mapping>& mappings,
67 allocator::vector<Mapping>& heap_mappings,
68 allocator::vector<Mapping>& anon_mappings,
69 allocator::vector<Mapping>& globals_mappings,
70 allocator::vector<Mapping>& stack_mappings);
71 DISALLOW_COPY_AND_ASSIGN(MemUnreachable);
72 pid_t pid_;
73 Allocator<void> allocator_;
74 HeapWalker heap_walker_;
75 };
76
HeapIterate(const Mapping & heap_mapping,const std::function<void (uintptr_t,size_t)> & func)77 static void HeapIterate(const Mapping& heap_mapping,
78 const std::function<void(uintptr_t, size_t)>& func) {
79 malloc_iterate(heap_mapping.begin, heap_mapping.end - heap_mapping.begin,
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)));
85 }
86
CollectAllocations(const allocator::vector<ThreadInfo> & threads,const allocator::vector<Mapping> & mappings,const allocator::vector<uintptr_t> & refs)87 bool MemUnreachable::CollectAllocations(const allocator::vector<ThreadInfo>& threads,
88 const allocator::vector<Mapping>& mappings,
89 const allocator::vector<uintptr_t>& refs) {
90 MEM_ALOGI("searching process %d for allocations", pid_);
91
92 for (auto it = mappings.begin(); it != mappings.end(); it++) {
93 heap_walker_.Mapping(it->begin, it->end);
94 }
95
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};
100 if (!ClassifyMappings(mappings, heap_mappings, anon_mappings, globals_mappings, stack_mappings)) {
101 return false;
102 }
103
104 for (auto it = heap_mappings.begin(); it != heap_mappings.end(); it++) {
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); });
108 }
109
110 for (auto it = anon_mappings.begin(); it != anon_mappings.end(); it++) {
111 MEM_ALOGV("Anon mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name);
112 heap_walker_.Allocation(it->begin, it->end);
113 }
114
115 for (auto it = globals_mappings.begin(); it != globals_mappings.end(); it++) {
116 MEM_ALOGV("Globals mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name);
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) {
123 MEM_ALOGV("Stack %" PRIxPTR "-%" PRIxPTR " %s", thread_it->stack.first, it->end, it->name);
124 heap_walker_.Root(thread_it->stack.first, it->end);
125 }
126 }
127 heap_walker_.Root(thread_it->regs);
128 }
129
130 heap_walker_.Root(refs);
131
132 MEM_ALOGI("searching done");
133
134 return true;
135 }
136
GetUnreachableMemory(allocator::vector<Leak> & leaks,size_t limit,size_t * num_leaks,size_t * leak_bytes)137 bool MemUnreachable::GetUnreachableMemory(allocator::vector<Leak>& leaks, size_t limit,
138 size_t* num_leaks, size_t* leak_bytes) {
139 MEM_ALOGI("sweeping process %d for unreachable memory", pid_);
140 leaks.clear();
141
142 if (!heap_walker_.DetectLeaks()) {
143 return false;
144 }
145
146 allocator::vector<Range> leaked1{allocator_};
147 heap_walker_.Leaked(leaked1, 0, num_leaks, leak_bytes);
148
149 MEM_ALOGI("sweeping done");
150
151 MEM_ALOGI("folding related leaks");
152
153 LeakFolding folding(allocator_, heap_walker_);
154 if (!folding.FoldLeaks()) {
155 return false;
156 }
157
158 allocator::vector<LeakFolding::Leak> leaked{allocator_};
159
160 if (!folding.Leaked(leaked, num_leaks, leak_bytes)) {
161 return false;
162 }
163
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
170 for (auto& it : leaked) {
171 leaks.emplace_back();
172 Leak* leak = &leaks.back();
173
174 ssize_t num_backtrace_frames = malloc_backtrace(
175 reinterpret_cast<void*>(it.range.begin), leak->backtrace.frames, leak->backtrace.max_frames);
176 if (num_backtrace_frames > 0) {
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 }
193 }
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),
201 std::min(leak->size, Leak::contents_length));
202 }
203
204 MEM_ALOGI("folding done");
205
206 std::sort(leaks.begin(), leaks.end(),
207 [](const Leak& a, const Leak& b) { return a.total_size > b.total_size; });
208
209 if (leaks.size() > limit) {
210 leaks.resize(limit);
211 }
212
213 return true;
214 }
215
has_prefix(const allocator::string & s,const char * prefix)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
is_sanitizer_mapping(const allocator::string & s)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
ClassifyMappings(const allocator::vector<Mapping> & mappings,allocator::vector<Mapping> & heap_mappings,allocator::vector<Mapping> & anon_mappings,allocator::vector<Mapping> & globals_mappings,allocator::vector<Mapping> & stack_mappings)225 bool MemUnreachable::ClassifyMappings(const allocator::vector<Mapping>& mappings,
226 allocator::vector<Mapping>& heap_mappings,
227 allocator::vector<Mapping>& anon_mappings,
228 allocator::vector<Mapping>& globals_mappings,
229 allocator::vector<Mapping>& stack_mappings) {
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:libmemunreachable stack]") {
249 // the ptracer thread's stack, ignore it.
250 } else if (mapping_name == "[anon:.bss]") {
251 // named .bss section
252 globals_mappings.emplace_back(*it);
253 } else if (mapping_name == current_lib) {
254 // .rodata or .data section
255 globals_mappings.emplace_back(*it);
256 } else if (mapping_name == "[anon:libc_malloc]" ||
257 android::base::StartsWith(mapping_name, "[anon:scudo:") ||
258 android::base::StartsWith(mapping_name, "[anon:GWP-ASan")) {
259 // named malloc mapping
260 heap_mappings.emplace_back(*it);
261 } else if (has_prefix(mapping_name, "[anon:dalvik-")) {
262 // named dalvik heap mapping
263 globals_mappings.emplace_back(*it);
264 } else if (has_prefix(mapping_name, "[stack")) {
265 // named stack mapping
266 stack_mappings.emplace_back(*it);
267 } else if (mapping_name.size() == 0) {
268 globals_mappings.emplace_back(*it);
269 } else if (has_prefix(mapping_name, "[anon:") &&
270 mapping_name != "[anon:leak_detector_malloc]" &&
271 !is_sanitizer_mapping(mapping_name)) {
272 // TODO(ccross): it would be nice to treat named anonymous mappings as
273 // possible leaks, but naming something in a .bss or .data section makes
274 // it impossible to distinguish them from mmaped and then named mappings.
275 globals_mappings.emplace_back(*it);
276 }
277 }
278
279 return true;
280 }
281
282 template <typename T>
plural(T val)283 static inline const char* plural(T val) {
284 return (val == 1) ? "" : "s";
285 }
286
287 enum State {
288 STARTING = 0,
289 PAUSING,
290 COLLECTING,
291 ABORT,
292 };
293
GetUnreachableMemory(UnreachableMemoryInfo & info,size_t limit)294 bool GetUnreachableMemory(UnreachableMemoryInfo& info, size_t limit) {
295 if (info.version > 0) {
296 MEM_ALOGE("unsupported UnreachableMemoryInfo.version %zu in GetUnreachableMemory",
297 info.version);
298 return false;
299 }
300
301 int parent_pid = getpid();
302 int parent_tid = gettid();
303
304 Heap heap;
305
306 AtomicState<State> state(STARTING);
307 LeakPipe pipe;
308
309 PtracerThread thread{[&]() -> int {
310 /////////////////////////////////////////////
311 // Collection thread
312 /////////////////////////////////////////////
313 MEM_ALOGI("collecting thread info for process %d...", parent_pid);
314
315 if (!state.transition_or(STARTING, PAUSING, [&] {
316 MEM_ALOGI("collecting thread expected state STARTING, aborting");
317 return ABORT;
318 })) {
319 return 1;
320 }
321
322 ThreadCapture thread_capture(parent_pid, heap);
323 allocator::vector<ThreadInfo> thread_info(heap);
324 allocator::vector<Mapping> mappings(heap);
325 allocator::vector<uintptr_t> refs(heap);
326
327 // ptrace all the threads
328 if (!thread_capture.CaptureThreads()) {
329 state.set(ABORT);
330 return 1;
331 }
332
333 // collect register contents and stacks
334 if (!thread_capture.CapturedThreadInfo(thread_info)) {
335 state.set(ABORT);
336 return 1;
337 }
338
339 // snapshot /proc/pid/maps
340 if (!ProcessMappings(parent_pid, mappings)) {
341 state.set(ABORT);
342 return 1;
343 }
344
345 if (!BinderReferences(refs)) {
346 state.set(ABORT);
347 return 1;
348 }
349
350 // Atomically update the state from PAUSING to COLLECTING.
351 // The main thread may have given up waiting for this thread to finish
352 // pausing, in which case it will have changed the state to ABORT.
353 if (!state.transition_or(PAUSING, COLLECTING, [&] {
354 MEM_ALOGI("collecting thread aborting");
355 return ABORT;
356 })) {
357 return 1;
358 }
359
360 // malloc must be enabled to call fork, at_fork handlers take the same
361 // locks as ScopedDisableMalloc. All threads are paused in ptrace, so
362 // memory state is still consistent. Unfreeze the original thread so it
363 // can drop the malloc locks, it will block until the collection thread
364 // exits.
365 thread_capture.ReleaseThread(parent_tid);
366
367 // fork a process to do the heap walking
368 int ret = fork();
369 if (ret < 0) {
370 return 1;
371 } else if (ret == 0) {
372 /////////////////////////////////////////////
373 // Heap walker process
374 /////////////////////////////////////////////
375 // Examine memory state in the child using the data collected above and
376 // the CoW snapshot of the process memory contents.
377
378 if (!pipe.OpenSender()) {
379 _exit(1);
380 }
381
382 MemUnreachable unreachable{parent_pid, heap};
383
384 if (!unreachable.CollectAllocations(thread_info, mappings, refs)) {
385 _exit(2);
386 }
387 size_t num_allocations = unreachable.Allocations();
388 size_t allocation_bytes = unreachable.AllocationBytes();
389
390 allocator::vector<Leak> leaks{heap};
391
392 size_t num_leaks = 0;
393 size_t leak_bytes = 0;
394 bool ok = unreachable.GetUnreachableMemory(leaks, limit, &num_leaks, &leak_bytes);
395
396 ok = ok && pipe.Sender().Send(num_allocations);
397 ok = ok && pipe.Sender().Send(allocation_bytes);
398 ok = ok && pipe.Sender().Send(num_leaks);
399 ok = ok && pipe.Sender().Send(leak_bytes);
400 ok = ok && pipe.Sender().SendVector(leaks);
401
402 if (!ok) {
403 _exit(3);
404 }
405
406 _exit(0);
407 } else {
408 // Nothing left to do in the collection thread, return immediately,
409 // releasing all the captured threads.
410 MEM_ALOGI("collection thread done");
411 return 0;
412 }
413 }};
414
415 /////////////////////////////////////////////
416 // Original thread
417 /////////////////////////////////////////////
418
419 {
420 // Disable malloc to get a consistent view of memory
421 ScopedDisableMalloc disable_malloc;
422
423 // Start the collection thread
424 thread.Start();
425
426 // Wait for the collection thread to signal that it is ready to fork the
427 // heap walker process.
428 if (!state.wait_for_either_of(COLLECTING, ABORT, 30s)) {
429 // The pausing didn't finish within 30 seconds, attempt to atomically
430 // update the state from PAUSING to ABORT. The collecting thread
431 // may have raced with the timeout and already updated the state to
432 // COLLECTING, in which case aborting is not necessary.
433 if (state.transition(PAUSING, ABORT)) {
434 MEM_ALOGI("main thread timed out waiting for collecting thread");
435 }
436 }
437
438 // Re-enable malloc so the collection thread can fork.
439 }
440
441 // Wait for the collection thread to exit
442 int ret = thread.Join();
443 if (ret != 0) {
444 return false;
445 }
446
447 // Get a pipe from the heap walker process. Transferring a new pipe fd
448 // ensures no other forked processes can have it open, so when the heap
449 // walker process dies the remote side of the pipe will close.
450 if (!pipe.OpenReceiver()) {
451 return false;
452 }
453
454 bool ok = true;
455 ok = ok && pipe.Receiver().Receive(&info.num_allocations);
456 ok = ok && pipe.Receiver().Receive(&info.allocation_bytes);
457 ok = ok && pipe.Receiver().Receive(&info.num_leaks);
458 ok = ok && pipe.Receiver().Receive(&info.leak_bytes);
459 ok = ok && pipe.Receiver().ReceiveVector(info.leaks);
460 if (!ok) {
461 return false;
462 }
463
464 MEM_ALOGI("unreachable memory detection done");
465 MEM_ALOGE("%zu bytes in %zu allocation%s unreachable out of %zu bytes in %zu allocation%s",
466 info.leak_bytes, info.num_leaks, plural(info.num_leaks), info.allocation_bytes,
467 info.num_allocations, plural(info.num_allocations));
468 return true;
469 }
470
ToString(bool log_contents) const471 std::string Leak::ToString(bool log_contents) const {
472 std::ostringstream oss;
473
474 oss << " " << std::dec << size;
475 oss << " bytes unreachable at ";
476 oss << std::hex << begin;
477 oss << std::endl;
478 if (referenced_count > 0) {
479 oss << std::dec;
480 oss << " referencing " << referenced_size << " unreachable bytes";
481 oss << " in " << referenced_count << " allocation" << plural(referenced_count);
482 oss << std::endl;
483 }
484 if (similar_count > 0) {
485 oss << std::dec;
486 oss << " and " << similar_size << " similar unreachable bytes";
487 oss << " in " << similar_count << " allocation" << plural(similar_count);
488 oss << std::endl;
489 if (similar_referenced_count > 0) {
490 oss << " referencing " << similar_referenced_size << " unreachable bytes";
491 oss << " in " << similar_referenced_count << " allocation" << plural(similar_referenced_count);
492 oss << std::endl;
493 }
494 }
495
496 if (log_contents) {
497 const int bytes_per_line = 16;
498 const size_t bytes = std::min(size, contents_length);
499
500 if (bytes == size) {
501 oss << " contents:" << std::endl;
502 } else {
503 oss << " first " << bytes << " bytes of contents:" << std::endl;
504 }
505
506 for (size_t i = 0; i < bytes; i += bytes_per_line) {
507 oss << " " << std::hex << begin + i << ": ";
508 size_t j;
509 oss << std::setfill('0');
510 for (j = i; j < bytes && j < i + bytes_per_line; j++) {
511 oss << std::setw(2) << static_cast<int>(contents[j]) << " ";
512 }
513 oss << std::setfill(' ');
514 for (; j < i + bytes_per_line; j++) {
515 oss << " ";
516 }
517 for (j = i; j < bytes && j < i + bytes_per_line; j++) {
518 char c = contents[j];
519 if (c < ' ' || c >= 0x7f) {
520 c = '.';
521 }
522 oss << c;
523 }
524 oss << std::endl;
525 }
526 }
527 if (backtrace.num_frames > 0) {
528 oss << backtrace_string(backtrace.frames, backtrace.num_frames);
529 }
530
531 return oss.str();
532 }
533
ToString(bool log_contents) const534 std::string UnreachableMemoryInfo::ToString(bool log_contents) const {
535 std::ostringstream oss;
536 oss << " " << leak_bytes << " bytes in ";
537 oss << num_leaks << " unreachable allocation" << plural(num_leaks);
538 oss << std::endl;
539 oss << " ABI: '" ABI_STRING "'" << std::endl;
540 oss << std::endl;
541
542 for (auto it = leaks.begin(); it != leaks.end(); it++) {
543 oss << it->ToString(log_contents);
544 oss << std::endl;
545 }
546
547 return oss.str();
548 }
549
~UnreachableMemoryInfo()550 UnreachableMemoryInfo::~UnreachableMemoryInfo() {
551 // Clear the memory that holds the leaks, otherwise the next attempt to
552 // detect leaks may find the old data (for example in the jemalloc tcache)
553 // and consider all the leaks to be referenced.
554 memset(leaks.data(), 0, leaks.capacity() * sizeof(Leak));
555
556 std::vector<Leak> tmp;
557 leaks.swap(tmp);
558
559 // Disable and re-enable malloc to flush the jemalloc tcache to make sure
560 // there are no copies of the leaked pointer addresses there.
561 malloc_disable();
562 malloc_enable();
563 }
564
GetUnreachableMemoryString(bool log_contents,size_t limit)565 std::string GetUnreachableMemoryString(bool log_contents, size_t limit) {
566 UnreachableMemoryInfo info;
567 if (!GetUnreachableMemory(info, limit)) {
568 return "Failed to get unreachable memory\n"
569 "If you are trying to get unreachable memory from a system app\n"
570 "(like com.android.systemui), disable selinux first using\n"
571 "setenforce 0\n";
572 }
573
574 return info.ToString(log_contents);
575 }
576
577 } // namespace android
578
LogUnreachableMemory(bool log_contents,size_t limit)579 bool LogUnreachableMemory(bool log_contents, size_t limit) {
580 android::UnreachableMemoryInfo info;
581 if (!android::GetUnreachableMemory(info, limit)) {
582 return false;
583 }
584
585 for (auto it = info.leaks.begin(); it != info.leaks.end(); it++) {
586 MEM_ALOGE("%s", it->ToString(log_contents).c_str());
587 }
588 return true;
589 }
590
NoLeaks()591 bool NoLeaks() {
592 android::UnreachableMemoryInfo info;
593 if (!android::GetUnreachableMemory(info, 0)) {
594 return false;
595 }
596
597 return info.num_leaks == 0;
598 }
599