blob: df16f4078e7cd7fe52e4edfab826454c9bc4e7ca [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
Colin Crossba5d9ff2016-04-26 16:51:32 -070017#include <errno.h>
Colin Cross7add50d2016-01-14 15:35:40 -080018#include <inttypes.h>
Colin Crossba5d9ff2016-04-26 16:51:32 -070019#include <sys/mman.h>
20#include <unistd.h>
Colin Cross7add50d2016-01-14 15:35:40 -080021
22#include <map>
23#include <utility>
24
25#include "Allocator.h"
26#include "HeapWalker.h"
Colin Cross8e8f34c2016-03-02 17:53:39 -080027#include "LeakFolding.h"
Colin Crossba5d9ff2016-04-26 16:51:32 -070028#include "ScopedSignalHandler.h"
Colin Cross7add50d2016-01-14 15:35:40 -080029#include "log.h"
30
31bool HeapWalker::Allocation(uintptr_t begin, uintptr_t end) {
32 if (end == begin) {
33 end = begin + 1;
34 }
Colin Cross8e8f34c2016-03-02 17:53:39 -080035 Range range{begin, end};
36 auto inserted = allocations_.insert(std::pair<Range, AllocationInfo>(range, AllocationInfo{}));
Colin Cross7add50d2016-01-14 15:35:40 -080037 if (inserted.second) {
38 valid_allocations_range_.begin = std::min(valid_allocations_range_.begin, begin);
39 valid_allocations_range_.end = std::max(valid_allocations_range_.end, end);
Colin Cross8e8f34c2016-03-02 17:53:39 -080040 allocation_bytes_ += range.size();
Colin Cross7add50d2016-01-14 15:35:40 -080041 return true;
42 } else {
43 Range overlap = inserted.first->first;
Colin Crosscecd6402016-04-26 17:10:04 -070044 if (overlap != range) {
Christopher Ferris47dea712017-05-03 17:34:29 -070045 MEM_ALOGE("range %p-%p overlaps with existing range %p-%p", reinterpret_cast<void*>(begin),
46 reinterpret_cast<void*>(end), reinterpret_cast<void*>(overlap.begin),
47 reinterpret_cast<void*>(overlap.end));
Colin Crosscecd6402016-04-26 17:10:04 -070048 }
Colin Cross7add50d2016-01-14 15:35:40 -080049 return false;
50 }
51}
52
Colin Crossba5d9ff2016-04-26 16:51:32 -070053bool HeapWalker::WordContainsAllocationPtr(uintptr_t word_ptr, Range* range, AllocationInfo** info) {
54 walking_ptr_ = word_ptr;
55 // This access may segfault if the process under test has done something strange,
56 // for example mprotect(PROT_NONE) on a native heap page. If so, it will be
57 // caught and handled by mmaping a zero page over the faulting page.
58 uintptr_t value = *reinterpret_cast<uintptr_t*>(word_ptr);
59 walking_ptr_ = 0;
60 if (value >= valid_allocations_range_.begin && value < valid_allocations_range_.end) {
61 AllocationMap::iterator it = allocations_.find(Range{value, value + 1});
Colin Cross8e8f34c2016-03-02 17:53:39 -080062 if (it != allocations_.end()) {
63 *range = it->first;
64 *info = &it->second;
65 return true;
66 }
67 }
68 return false;
69}
70
71void HeapWalker::RecurseRoot(const Range& root) {
72 allocator::vector<Range> to_do(1, root, allocator_);
Colin Cross7add50d2016-01-14 15:35:40 -080073 while (!to_do.empty()) {
74 Range range = to_do.back();
75 to_do.pop_back();
Colin Cross8e8f34c2016-03-02 17:53:39 -080076
77 ForEachPtrInRange(range, [&](Range& ref_range, AllocationInfo* ref_info) {
78 if (!ref_info->referenced_from_root) {
79 ref_info->referenced_from_root = true;
80 to_do.push_back(ref_range);
Colin Cross7add50d2016-01-14 15:35:40 -080081 }
Colin Cross8e8f34c2016-03-02 17:53:39 -080082 });
Colin Cross7add50d2016-01-14 15:35:40 -080083 }
84}
85
86void HeapWalker::Root(uintptr_t begin, uintptr_t end) {
87 roots_.push_back(Range{begin, end});
88}
89
90void HeapWalker::Root(const allocator::vector<uintptr_t>& vals) {
91 root_vals_.insert(root_vals_.end(), vals.begin(), vals.end());
92}
93
94size_t HeapWalker::Allocations() {
95 return allocations_.size();
96}
97
98size_t HeapWalker::AllocationBytes() {
99 return allocation_bytes_;
100}
101
102bool HeapWalker::DetectLeaks() {
Colin Cross8e8f34c2016-03-02 17:53:39 -0800103 // Recursively walk pointers from roots to mark referenced allocations
Colin Cross7add50d2016-01-14 15:35:40 -0800104 for (auto it = roots_.begin(); it != roots_.end(); it++) {
Colin Cross8e8f34c2016-03-02 17:53:39 -0800105 RecurseRoot(*it);
Colin Cross7add50d2016-01-14 15:35:40 -0800106 }
107
108 Range vals;
109 vals.begin = reinterpret_cast<uintptr_t>(root_vals_.data());
110 vals.end = vals.begin + root_vals_.size() * sizeof(uintptr_t);
Colin Cross7add50d2016-01-14 15:35:40 -0800111
Colin Cross8e8f34c2016-03-02 17:53:39 -0800112 RecurseRoot(vals);
Colin Cross7add50d2016-01-14 15:35:40 -0800113
114 return true;
115}
116
Colin Crossa83881e2017-06-22 10:50:05 -0700117bool HeapWalker::Leaked(allocator::vector<Range>& leaked, size_t limit, size_t* num_leaks_out,
118 size_t* leak_bytes_out) {
Colin Cross7add50d2016-01-14 15:35:40 -0800119 leaked.clear();
120
121 size_t num_leaks = 0;
122 size_t leak_bytes = 0;
123 for (auto it = allocations_.begin(); it != allocations_.end(); it++) {
124 if (!it->second.referenced_from_root) {
125 num_leaks++;
126 leak_bytes += it->first.end - it->first.begin;
127 }
128 }
129
130 size_t n = 0;
131 for (auto it = allocations_.begin(); it != allocations_.end(); it++) {
132 if (!it->second.referenced_from_root) {
Colin Cross8e8f34c2016-03-02 17:53:39 -0800133 if (n++ < limit) {
Colin Cross7add50d2016-01-14 15:35:40 -0800134 leaked.push_back(it->first);
135 }
136 }
137 }
138
139 if (num_leaks_out) {
140 *num_leaks_out = num_leaks;
141 }
142 if (leak_bytes_out) {
143 *leak_bytes_out = leak_bytes;
144 }
145
146 return true;
147}
Colin Crossba5d9ff2016-04-26 16:51:32 -0700148
149static bool MapOverPage(void* addr) {
150 const size_t page_size = sysconf(_SC_PAGE_SIZE);
Colin Crossa83881e2017-06-22 10:50:05 -0700151 void* page = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) & ~(page_size - 1));
Colin Crossba5d9ff2016-04-26 16:51:32 -0700152
Colin Crossa83881e2017-06-22 10:50:05 -0700153 void* ret = mmap(page, page_size, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
Colin Crossba5d9ff2016-04-26 16:51:32 -0700154 if (ret == MAP_FAILED) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700155 MEM_ALOGE("failed to map page at %p: %s", page, strerror(errno));
Colin Crossba5d9ff2016-04-26 16:51:32 -0700156 return false;
157 }
158
159 return true;
160}
161
Colin Crossa83881e2017-06-22 10:50:05 -0700162void HeapWalker::HandleSegFault(ScopedSignalHandler& handler, int signal, siginfo_t* si,
163 void* /*uctx*/) {
Colin Crossba5d9ff2016-04-26 16:51:32 -0700164 uintptr_t addr = reinterpret_cast<uintptr_t>(si->si_addr);
165 if (addr != walking_ptr_) {
166 handler.reset();
167 return;
168 }
Christopher Ferris47dea712017-05-03 17:34:29 -0700169 MEM_ALOGW("failed to read page at %p, signal %d", si->si_addr, signal);
Colin Crossba5d9ff2016-04-26 16:51:32 -0700170 if (!MapOverPage(si->si_addr)) {
171 handler.reset();
172 }
173}
174
175ScopedSignalHandler::SignalFn ScopedSignalHandler::handler_;