blob: 529a0437ef19c78e3f1597363babb742aad94aa3 [file] [log] [blame]
Colin Cross7add50d2016-01-14 15:35:40 -08001/*
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 Crossa9939e92017-06-21 13:13:00 -070018#include <string.h>
Colin Cross7add50d2016-01-14 15:35:40 -080019
20#include <functional>
21#include <iomanip>
22#include <mutex>
Colin Cross7add50d2016-01-14 15:35:40 -080023#include <sstream>
Colin Crossa83881e2017-06-22 10:50:05 -070024#include <string>
Colin Cross7a22e812016-03-04 16:36:12 -080025#include <unordered_map>
Colin Cross7add50d2016-01-14 15:35:40 -080026
Colin Cross7add50d2016-01-14 15:35:40 -080027#include <android-base/macros.h>
Colin Crossa83881e2017-06-22 10:50:05 -070028#include <backtrace.h>
Colin Cross7add50d2016-01-14 15:35:40 -080029
30#include "Allocator.h"
Colin Crossf572b912017-06-20 18:07:29 -070031#include "Binder.h"
Colin Cross7add50d2016-01-14 15:35:40 -080032#include "HeapWalker.h"
Colin Cross7a22e812016-03-04 16:36:12 -080033#include "Leak.h"
Colin Cross8e8f34c2016-03-02 17:53:39 -080034#include "LeakFolding.h"
Colin Cross7add50d2016-01-14 15:35:40 -080035#include "LeakPipe.h"
36#include "ProcessMappings.h"
37#include "PtracerThread.h"
38#include "ScopedDisableMalloc.h"
39#include "Semaphore.h"
40#include "ThreadCapture.h"
41
Colin Cross7add50d2016-01-14 15:35:40 -080042#include "bionic.h"
43#include "log.h"
Colin Crossa83881e2017-06-22 10:50:05 -070044#include "memunreachable/memunreachable.h"
Colin Cross7add50d2016-01-14 15:35:40 -080045
Colin Cross7add50d2016-01-14 15:35:40 -080046using namespace std::chrono_literals;
47
Colin Crossa9939e92017-06-21 13:13:00 -070048namespace android {
49
50const size_t Leak::contents_length;
51
Colin Cross7add50d2016-01-14 15:35:40 -080052class MemUnreachable {
53 public:
Colin Crossa83881e2017-06-22 10:50:05 -070054 MemUnreachable(pid_t pid, Allocator<void> allocator)
55 : pid_(pid), allocator_(allocator), heap_walker_(allocator_) {}
Colin Cross7add50d2016-01-14 15:35:40 -080056 bool CollectAllocations(const allocator::vector<ThreadInfo>& threads,
Colin Crossf572b912017-06-20 18:07:29 -070057 const allocator::vector<Mapping>& mappings,
58 const allocator::vector<uintptr_t>& refs);
Colin Crossa83881e2017-06-22 10:50:05 -070059 bool GetUnreachableMemory(allocator::vector<Leak>& leaks, size_t limit, size_t* num_leaks,
60 size_t* leak_bytes);
Colin Cross7add50d2016-01-14 15:35:40 -080061 size_t Allocations() { return heap_walker_.Allocations(); }
62 size_t AllocationBytes() { return heap_walker_.AllocationBytes(); }
Colin Crossa83881e2017-06-22 10:50:05 -070063
Colin Cross7add50d2016-01-14 15:35:40 -080064 private:
65 bool ClassifyMappings(const allocator::vector<Mapping>& mappings,
Colin Crossa83881e2017-06-22 10:50:05 -070066 allocator::vector<Mapping>& heap_mappings,
67 allocator::vector<Mapping>& anon_mappings,
68 allocator::vector<Mapping>& globals_mappings,
69 allocator::vector<Mapping>& stack_mappings);
Colin Cross7add50d2016-01-14 15:35:40 -080070 DISALLOW_COPY_AND_ASSIGN(MemUnreachable);
71 pid_t pid_;
72 Allocator<void> allocator_;
73 HeapWalker heap_walker_;
74};
75
76static void HeapIterate(const Mapping& heap_mapping,
Colin Crossa83881e2017-06-22 10:50:05 -070077 const std::function<void(uintptr_t, size_t)>& func) {
Colin Cross7add50d2016-01-14 15:35:40 -080078 malloc_iterate(heap_mapping.begin, heap_mapping.end - heap_mapping.begin,
Colin Crossa83881e2017-06-22 10:50:05 -070079 [](uintptr_t base, size_t size, void* arg) {
80 auto f = reinterpret_cast<const std::function<void(uintptr_t, size_t)>*>(arg);
81 (*f)(base, size);
82 },
83 const_cast<void*>(reinterpret_cast<const void*>(&func)));
Colin Cross7add50d2016-01-14 15:35:40 -080084}
85
86bool MemUnreachable::CollectAllocations(const allocator::vector<ThreadInfo>& threads,
Colin Crossf572b912017-06-20 18:07:29 -070087 const allocator::vector<Mapping>& mappings,
88 const allocator::vector<uintptr_t>& refs) {
Christopher Ferris47dea712017-05-03 17:34:29 -070089 MEM_ALOGI("searching process %d for allocations", pid_);
Colin Cross7add50d2016-01-14 15:35:40 -080090 allocator::vector<Mapping> heap_mappings{mappings};
91 allocator::vector<Mapping> anon_mappings{mappings};
92 allocator::vector<Mapping> globals_mappings{mappings};
93 allocator::vector<Mapping> stack_mappings{mappings};
Christopher Ferris47dea712017-05-03 17:34:29 -070094 if (!ClassifyMappings(mappings, heap_mappings, anon_mappings, globals_mappings, stack_mappings)) {
Colin Cross7add50d2016-01-14 15:35:40 -080095 return false;
96 }
97
98 for (auto it = heap_mappings.begin(); it != heap_mappings.end(); it++) {
Christopher Ferris47dea712017-05-03 17:34:29 -070099 MEM_ALOGV("Heap mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name);
100 HeapIterate(*it,
101 [&](uintptr_t base, size_t size) { heap_walker_.Allocation(base, base + size); });
Colin Cross7add50d2016-01-14 15:35:40 -0800102 }
103
104 for (auto it = anon_mappings.begin(); it != anon_mappings.end(); it++) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700105 MEM_ALOGV("Anon mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name);
Colin Cross7add50d2016-01-14 15:35:40 -0800106 heap_walker_.Allocation(it->begin, it->end);
107 }
108
109 for (auto it = globals_mappings.begin(); it != globals_mappings.end(); it++) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700110 MEM_ALOGV("Globals mapping %" PRIxPTR "-%" PRIxPTR " %s", it->begin, it->end, it->name);
Colin Cross7add50d2016-01-14 15:35:40 -0800111 heap_walker_.Root(it->begin, it->end);
112 }
113
114 for (auto thread_it = threads.begin(); thread_it != threads.end(); thread_it++) {
115 for (auto it = stack_mappings.begin(); it != stack_mappings.end(); it++) {
116 if (thread_it->stack.first >= it->begin && thread_it->stack.first <= it->end) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700117 MEM_ALOGV("Stack %" PRIxPTR "-%" PRIxPTR " %s", thread_it->stack.first, it->end, it->name);
Colin Cross7add50d2016-01-14 15:35:40 -0800118 heap_walker_.Root(thread_it->stack.first, it->end);
119 }
120 }
121 heap_walker_.Root(thread_it->regs);
122 }
123
Colin Crossf572b912017-06-20 18:07:29 -0700124 heap_walker_.Root(refs);
125
Christopher Ferris47dea712017-05-03 17:34:29 -0700126 MEM_ALOGI("searching done");
Colin Cross7add50d2016-01-14 15:35:40 -0800127
128 return true;
129}
130
Colin Crossa83881e2017-06-22 10:50:05 -0700131bool MemUnreachable::GetUnreachableMemory(allocator::vector<Leak>& leaks, size_t limit,
132 size_t* num_leaks, size_t* leak_bytes) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700133 MEM_ALOGI("sweeping process %d for unreachable memory", pid_);
Colin Cross7add50d2016-01-14 15:35:40 -0800134 leaks.clear();
135
Colin Cross8e8f34c2016-03-02 17:53:39 -0800136 if (!heap_walker_.DetectLeaks()) {
137 return false;
138 }
139
Colin Cross7a22e812016-03-04 16:36:12 -0800140 allocator::vector<Range> leaked1{allocator_};
141 heap_walker_.Leaked(leaked1, 0, num_leaks, leak_bytes);
142
Christopher Ferris47dea712017-05-03 17:34:29 -0700143 MEM_ALOGI("sweeping done");
Colin Cross7a22e812016-03-04 16:36:12 -0800144
Christopher Ferris47dea712017-05-03 17:34:29 -0700145 MEM_ALOGI("folding related leaks");
Colin Cross7a22e812016-03-04 16:36:12 -0800146
Colin Cross8e8f34c2016-03-02 17:53:39 -0800147 LeakFolding folding(allocator_, heap_walker_);
148 if (!folding.FoldLeaks()) {
149 return false;
150 }
151
152 allocator::vector<LeakFolding::Leak> leaked{allocator_};
153
Colin Cross7a22e812016-03-04 16:36:12 -0800154 if (!folding.Leaked(leaked, num_leaks, leak_bytes)) {
Colin Cross7add50d2016-01-14 15:35:40 -0800155 return false;
156 }
157
Colin Cross7a22e812016-03-04 16:36:12 -0800158 allocator::unordered_map<Leak::Backtrace, Leak*> backtrace_map{allocator_};
159
160 // Prevent reallocations of backing memory so we can store pointers into it
161 // in backtrace_map.
162 leaks.reserve(leaked.size());
163
Colin Crossa83881e2017-06-22 10:50:05 -0700164 for (auto& it : leaked) {
Colin Cross7a22e812016-03-04 16:36:12 -0800165 leaks.emplace_back();
166 Leak* leak = &leaks.back();
167
Colin Crossa83881e2017-06-22 10:50:05 -0700168 ssize_t num_backtrace_frames = malloc_backtrace(
169 reinterpret_cast<void*>(it.range.begin), leak->backtrace.frames, leak->backtrace.max_frames);
Colin Cross7add50d2016-01-14 15:35:40 -0800170 if (num_backtrace_frames > 0) {
Colin Cross7a22e812016-03-04 16:36:12 -0800171 leak->backtrace.num_frames = num_backtrace_frames;
172
173 auto inserted = backtrace_map.emplace(leak->backtrace, leak);
174 if (!inserted.second) {
175 // Leak with same backtrace already exists, drop this one and
176 // increment similar counts on the existing one.
177 leaks.pop_back();
178 Leak* similar_leak = inserted.first->second;
179 similar_leak->similar_count++;
180 similar_leak->similar_size += it.range.size();
181 similar_leak->similar_referenced_count += it.referenced_count;
182 similar_leak->similar_referenced_size += it.referenced_size;
183 similar_leak->total_size += it.range.size();
184 similar_leak->total_size += it.referenced_size;
185 continue;
186 }
Colin Cross7add50d2016-01-14 15:35:40 -0800187 }
Colin Cross7a22e812016-03-04 16:36:12 -0800188
189 leak->begin = it.range.begin;
190 leak->size = it.range.size();
191 leak->referenced_count = it.referenced_count;
192 leak->referenced_size = it.referenced_size;
193 leak->total_size = leak->size + leak->referenced_size;
194 memcpy(leak->contents, reinterpret_cast<void*>(it.range.begin),
Colin Crossa83881e2017-06-22 10:50:05 -0700195 std::min(leak->size, Leak::contents_length));
Colin Cross7add50d2016-01-14 15:35:40 -0800196 }
197
Christopher Ferris47dea712017-05-03 17:34:29 -0700198 MEM_ALOGI("folding done");
Colin Cross7a22e812016-03-04 16:36:12 -0800199
Colin Crossa83881e2017-06-22 10:50:05 -0700200 std::sort(leaks.begin(), leaks.end(),
201 [](const Leak& a, const Leak& b) { return a.total_size > b.total_size; });
Colin Cross7a22e812016-03-04 16:36:12 -0800202
203 if (leaks.size() > limit) {
204 leaks.resize(limit);
205 }
Colin Cross7add50d2016-01-14 15:35:40 -0800206
207 return true;
208}
209
210static bool has_prefix(const allocator::string& s, const char* prefix) {
211 int ret = s.compare(0, strlen(prefix), prefix);
212 return ret == 0;
213}
214
215bool MemUnreachable::ClassifyMappings(const allocator::vector<Mapping>& mappings,
Colin Crossa83881e2017-06-22 10:50:05 -0700216 allocator::vector<Mapping>& heap_mappings,
217 allocator::vector<Mapping>& anon_mappings,
218 allocator::vector<Mapping>& globals_mappings,
219 allocator::vector<Mapping>& stack_mappings) {
Colin Cross7add50d2016-01-14 15:35:40 -0800220 heap_mappings.clear();
221 anon_mappings.clear();
222 globals_mappings.clear();
223 stack_mappings.clear();
224
225 allocator::string current_lib{allocator_};
226
227 for (auto it = mappings.begin(); it != mappings.end(); it++) {
228 if (it->execute) {
229 current_lib = it->name;
230 continue;
231 }
232
233 if (!it->read) {
234 continue;
235 }
236
237 const allocator::string mapping_name{it->name, allocator_};
238 if (mapping_name == "[anon:.bss]") {
239 // named .bss section
240 globals_mappings.emplace_back(*it);
241 } else if (mapping_name == current_lib) {
242 // .rodata or .data section
243 globals_mappings.emplace_back(*it);
244 } else if (mapping_name == "[anon:libc_malloc]") {
245 // named malloc mapping
246 heap_mappings.emplace_back(*it);
247 } else if (has_prefix(mapping_name, "/dev/ashmem/dalvik")) {
248 // named dalvik heap mapping
249 globals_mappings.emplace_back(*it);
250 } else if (has_prefix(mapping_name, "[stack")) {
251 // named stack mapping
252 stack_mappings.emplace_back(*it);
253 } else if (mapping_name.size() == 0) {
254 globals_mappings.emplace_back(*it);
Colin Crossa83881e2017-06-22 10:50:05 -0700255 } else if (has_prefix(mapping_name, "[anon:") &&
256 mapping_name != "[anon:leak_detector_malloc]") {
Colin Cross7add50d2016-01-14 15:35:40 -0800257 // TODO(ccross): it would be nice to treat named anonymous mappings as
258 // possible leaks, but naming something in a .bss or .data section makes
259 // it impossible to distinguish them from mmaped and then named mappings.
260 globals_mappings.emplace_back(*it);
261 }
262 }
263
264 return true;
265}
266
Colin Crossa83881e2017-06-22 10:50:05 -0700267template <typename T>
Colin Cross7a22e812016-03-04 16:36:12 -0800268static inline const char* plural(T val) {
269 return (val == 1) ? "" : "s";
270}
271
Colin Cross7add50d2016-01-14 15:35:40 -0800272bool GetUnreachableMemory(UnreachableMemoryInfo& info, size_t limit) {
273 int parent_pid = getpid();
274 int parent_tid = gettid();
275
276 Heap heap;
277
278 Semaphore continue_parent_sem;
279 LeakPipe pipe;
280
281 PtracerThread thread{[&]() -> int {
282 /////////////////////////////////////////////
283 // Collection thread
284 /////////////////////////////////////////////
Christopher Ferris47dea712017-05-03 17:34:29 -0700285 MEM_ALOGI("collecting thread info for process %d...", parent_pid);
Colin Cross7add50d2016-01-14 15:35:40 -0800286
287 ThreadCapture thread_capture(parent_pid, heap);
288 allocator::vector<ThreadInfo> thread_info(heap);
289 allocator::vector<Mapping> mappings(heap);
Colin Crossf572b912017-06-20 18:07:29 -0700290 allocator::vector<uintptr_t> refs(heap);
Colin Cross7add50d2016-01-14 15:35:40 -0800291
292 // ptrace all the threads
293 if (!thread_capture.CaptureThreads()) {
Colin Crossde42af02016-01-14 15:35:40 -0800294 continue_parent_sem.Post();
Colin Cross7add50d2016-01-14 15:35:40 -0800295 return 1;
296 }
297
298 // collect register contents and stacks
299 if (!thread_capture.CapturedThreadInfo(thread_info)) {
Colin Crossde42af02016-01-14 15:35:40 -0800300 continue_parent_sem.Post();
Colin Cross7add50d2016-01-14 15:35:40 -0800301 return 1;
302 }
303
304 // snapshot /proc/pid/maps
305 if (!ProcessMappings(parent_pid, mappings)) {
Colin Crossde42af02016-01-14 15:35:40 -0800306 continue_parent_sem.Post();
Colin Cross7add50d2016-01-14 15:35:40 -0800307 return 1;
308 }
309
Colin Crossf572b912017-06-20 18:07:29 -0700310 if (!BinderReferences(refs)) {
311 continue_parent_sem.Post();
312 return 1;
313 }
314
Colin Cross7add50d2016-01-14 15:35:40 -0800315 // malloc must be enabled to call fork, at_fork handlers take the same
316 // locks as ScopedDisableMalloc. All threads are paused in ptrace, so
317 // memory state is still consistent. Unfreeze the original thread so it
318 // can drop the malloc locks, it will block until the collection thread
319 // exits.
320 thread_capture.ReleaseThread(parent_tid);
321 continue_parent_sem.Post();
322
323 // fork a process to do the heap walking
324 int ret = fork();
325 if (ret < 0) {
326 return 1;
327 } else if (ret == 0) {
328 /////////////////////////////////////////////
329 // Heap walker process
330 /////////////////////////////////////////////
331 // Examine memory state in the child using the data collected above and
332 // the CoW snapshot of the process memory contents.
333
334 if (!pipe.OpenSender()) {
335 _exit(1);
336 }
337
338 MemUnreachable unreachable{parent_pid, heap};
339
Colin Crossf572b912017-06-20 18:07:29 -0700340 if (!unreachable.CollectAllocations(thread_info, mappings, refs)) {
Colin Cross7add50d2016-01-14 15:35:40 -0800341 _exit(2);
342 }
343 size_t num_allocations = unreachable.Allocations();
344 size_t allocation_bytes = unreachable.AllocationBytes();
345
346 allocator::vector<Leak> leaks{heap};
347
348 size_t num_leaks = 0;
349 size_t leak_bytes = 0;
350 bool ok = unreachable.GetUnreachableMemory(leaks, limit, &num_leaks, &leak_bytes);
351
352 ok = ok && pipe.Sender().Send(num_allocations);
353 ok = ok && pipe.Sender().Send(allocation_bytes);
354 ok = ok && pipe.Sender().Send(num_leaks);
355 ok = ok && pipe.Sender().Send(leak_bytes);
356 ok = ok && pipe.Sender().SendVector(leaks);
357
358 if (!ok) {
359 _exit(3);
360 }
361
362 _exit(0);
363 } else {
364 // Nothing left to do in the collection thread, return immediately,
365 // releasing all the captured threads.
Christopher Ferris47dea712017-05-03 17:34:29 -0700366 MEM_ALOGI("collection thread done");
Colin Cross7add50d2016-01-14 15:35:40 -0800367 return 0;
368 }
369 }};
370
371 /////////////////////////////////////////////
372 // Original thread
373 /////////////////////////////////////////////
374
375 {
376 // Disable malloc to get a consistent view of memory
377 ScopedDisableMalloc disable_malloc;
378
379 // Start the collection thread
380 thread.Start();
381
382 // Wait for the collection thread to signal that it is ready to fork the
383 // heap walker process.
Colin Crossde42af02016-01-14 15:35:40 -0800384 continue_parent_sem.Wait(30s);
Colin Cross7add50d2016-01-14 15:35:40 -0800385
386 // Re-enable malloc so the collection thread can fork.
387 }
388
389 // Wait for the collection thread to exit
390 int ret = thread.Join();
391 if (ret != 0) {
392 return false;
393 }
394
395 // Get a pipe from the heap walker process. Transferring a new pipe fd
396 // ensures no other forked processes can have it open, so when the heap
397 // walker process dies the remote side of the pipe will close.
398 if (!pipe.OpenReceiver()) {
399 return false;
400 }
401
402 bool ok = true;
403 ok = ok && pipe.Receiver().Receive(&info.num_allocations);
404 ok = ok && pipe.Receiver().Receive(&info.allocation_bytes);
405 ok = ok && pipe.Receiver().Receive(&info.num_leaks);
406 ok = ok && pipe.Receiver().Receive(&info.leak_bytes);
407 ok = ok && pipe.Receiver().ReceiveVector(info.leaks);
408 if (!ok) {
409 return false;
410 }
411
Christopher Ferris47dea712017-05-03 17:34:29 -0700412 MEM_ALOGI("unreachable memory detection done");
413 MEM_ALOGE("%zu bytes in %zu allocation%s unreachable out of %zu bytes in %zu allocation%s",
414 info.leak_bytes, info.num_leaks, plural(info.num_leaks), info.allocation_bytes,
415 info.num_allocations, plural(info.num_allocations));
Colin Cross7add50d2016-01-14 15:35:40 -0800416 return true;
417}
418
419std::string Leak::ToString(bool log_contents) const {
Colin Cross7add50d2016-01-14 15:35:40 -0800420 std::ostringstream oss;
421
422 oss << " " << std::dec << size;
Colin Crossde42af02016-01-14 15:35:40 -0800423 oss << " bytes unreachable at ";
Colin Cross7add50d2016-01-14 15:35:40 -0800424 oss << std::hex << begin;
425 oss << std::endl;
Colin Cross7a22e812016-03-04 16:36:12 -0800426 if (referenced_count > 0) {
427 oss << std::dec;
428 oss << " referencing " << referenced_size << " unreachable bytes";
429 oss << " in " << referenced_count << " allocation" << plural(referenced_count);
430 oss << std::endl;
431 }
432 if (similar_count > 0) {
433 oss << std::dec;
434 oss << " and " << similar_size << " similar unreachable bytes";
435 oss << " in " << similar_count << " allocation" << plural(similar_count);
436 oss << std::endl;
437 if (similar_referenced_count > 0) {
438 oss << " referencing " << similar_referenced_size << " unreachable bytes";
439 oss << " in " << similar_referenced_count << " allocation" << plural(similar_referenced_count);
440 oss << std::endl;
441 }
442 }
Colin Cross7add50d2016-01-14 15:35:40 -0800443
444 if (log_contents) {
445 const int bytes_per_line = 16;
446 const size_t bytes = std::min(size, contents_length);
447
448 if (bytes == size) {
449 oss << " contents:" << std::endl;
450 } else {
Colin Cross7a22e812016-03-04 16:36:12 -0800451 oss << " first " << bytes << " bytes of contents:" << std::endl;
Colin Cross7add50d2016-01-14 15:35:40 -0800452 }
453
454 for (size_t i = 0; i < bytes; i += bytes_per_line) {
455 oss << " " << std::hex << begin + i << ": ";
456 size_t j;
457 oss << std::setfill('0');
458 for (j = i; j < bytes && j < i + bytes_per_line; j++) {
459 oss << std::setw(2) << static_cast<int>(contents[j]) << " ";
460 }
461 oss << std::setfill(' ');
462 for (; j < i + bytes_per_line; j++) {
463 oss << " ";
464 }
465 for (j = i; j < bytes && j < i + bytes_per_line; j++) {
466 char c = contents[j];
467 if (c < ' ' || c >= 0x7f) {
468 c = '.';
469 }
470 oss << c;
471 }
472 oss << std::endl;
473 }
474 }
Colin Cross7a22e812016-03-04 16:36:12 -0800475 if (backtrace.num_frames > 0) {
476 oss << backtrace_string(backtrace.frames, backtrace.num_frames);
Colin Cross7add50d2016-01-14 15:35:40 -0800477 }
478
479 return oss.str();
480}
481
482std::string UnreachableMemoryInfo::ToString(bool log_contents) const {
483 std::ostringstream oss;
484 oss << " " << leak_bytes << " bytes in ";
Colin Cross7a22e812016-03-04 16:36:12 -0800485 oss << num_leaks << " unreachable allocation" << plural(num_leaks);
Colin Cross7add50d2016-01-14 15:35:40 -0800486 oss << std::endl;
Colin Cross11185af2016-03-04 16:37:02 -0800487 oss << " ABI: '" ABI_STRING "'" << std::endl;
488 oss << std::endl;
Colin Cross7add50d2016-01-14 15:35:40 -0800489
490 for (auto it = leaks.begin(); it != leaks.end(); it++) {
Colin Crossa83881e2017-06-22 10:50:05 -0700491 oss << it->ToString(log_contents);
492 oss << std::endl;
Colin Cross7add50d2016-01-14 15:35:40 -0800493 }
494
495 return oss.str();
496}
497
Colin Crossca71f172018-05-14 14:55:39 -0700498UnreachableMemoryInfo::~UnreachableMemoryInfo() {
499 // Clear the memory that holds the leaks, otherwise the next attempt to
500 // detect leaks may find the old data (for example in the jemalloc tcache)
501 // and consider all the leaks to be referenced.
502 memset(leaks.data(), 0, leaks.capacity() * sizeof(Leak));
503
504 std::vector<Leak> tmp;
505 leaks.swap(tmp);
506
507 // Disable and re-enable malloc to flush the jemalloc tcache to make sure
508 // there are no copies of the leaked pointer addresses there.
509 malloc_disable();
510 malloc_enable();
511}
512
Colin Cross7add50d2016-01-14 15:35:40 -0800513std::string GetUnreachableMemoryString(bool log_contents, size_t limit) {
514 UnreachableMemoryInfo info;
515 if (!GetUnreachableMemory(info, limit)) {
Colin Cross72d38812017-06-13 16:41:58 -0700516 return "Failed to get unreachable memory\n"
517 "If you are trying to get unreachable memory from a system app\n"
518 "(like com.android.systemui), disable selinux first using\n"
519 "setenforce 0\n";
Colin Cross7add50d2016-01-14 15:35:40 -0800520 }
521
522 return info.ToString(log_contents);
523}
524
Colin Crossa9939e92017-06-21 13:13:00 -0700525} // namespace android
526
Colin Cross7add50d2016-01-14 15:35:40 -0800527bool LogUnreachableMemory(bool log_contents, size_t limit) {
Colin Crossa9939e92017-06-21 13:13:00 -0700528 android::UnreachableMemoryInfo info;
529 if (!android::GetUnreachableMemory(info, limit)) {
Colin Cross7add50d2016-01-14 15:35:40 -0800530 return false;
531 }
532
533 for (auto it = info.leaks.begin(); it != info.leaks.end(); it++) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700534 MEM_ALOGE("%s", it->ToString(log_contents).c_str());
Colin Cross7add50d2016-01-14 15:35:40 -0800535 }
536 return true;
537}
538
Colin Cross7add50d2016-01-14 15:35:40 -0800539bool NoLeaks() {
Colin Crossa9939e92017-06-21 13:13:00 -0700540 android::UnreachableMemoryInfo info;
541 if (!android::GetUnreachableMemory(info, 0)) {
Colin Cross7add50d2016-01-14 15:35:40 -0800542 return false;
543 }
544
545 return info.num_leaks == 0;
546}