blob: 2403ad085936cf32e08ddb19158b21e3487a6727 [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
Colin Crossa9939e92017-06-21 13:13:00 -070031namespace android {
32
Colin Cross7add50d2016-01-14 15:35:40 -080033bool HeapWalker::Allocation(uintptr_t begin, uintptr_t end) {
34 if (end == begin) {
35 end = begin + 1;
36 }
Colin Cross8e8f34c2016-03-02 17:53:39 -080037 Range range{begin, end};
38 auto inserted = allocations_.insert(std::pair<Range, AllocationInfo>(range, AllocationInfo{}));
Colin Cross7add50d2016-01-14 15:35:40 -080039 if (inserted.second) {
40 valid_allocations_range_.begin = std::min(valid_allocations_range_.begin, begin);
41 valid_allocations_range_.end = std::max(valid_allocations_range_.end, end);
Colin Cross8e8f34c2016-03-02 17:53:39 -080042 allocation_bytes_ += range.size();
Colin Cross7add50d2016-01-14 15:35:40 -080043 return true;
44 } else {
45 Range overlap = inserted.first->first;
Colin Crosscecd6402016-04-26 17:10:04 -070046 if (overlap != range) {
Christopher Ferris47dea712017-05-03 17:34:29 -070047 MEM_ALOGE("range %p-%p overlaps with existing range %p-%p", reinterpret_cast<void*>(begin),
48 reinterpret_cast<void*>(end), reinterpret_cast<void*>(overlap.begin),
49 reinterpret_cast<void*>(overlap.end));
Colin Crosscecd6402016-04-26 17:10:04 -070050 }
Colin Cross7add50d2016-01-14 15:35:40 -080051 return false;
52 }
53}
54
Colin Crossba5d9ff2016-04-26 16:51:32 -070055bool HeapWalker::WordContainsAllocationPtr(uintptr_t word_ptr, Range* range, AllocationInfo** info) {
56 walking_ptr_ = word_ptr;
57 // This access may segfault if the process under test has done something strange,
58 // for example mprotect(PROT_NONE) on a native heap page. If so, it will be
59 // caught and handled by mmaping a zero page over the faulting page.
60 uintptr_t value = *reinterpret_cast<uintptr_t*>(word_ptr);
61 walking_ptr_ = 0;
62 if (value >= valid_allocations_range_.begin && value < valid_allocations_range_.end) {
63 AllocationMap::iterator it = allocations_.find(Range{value, value + 1});
Colin Cross8e8f34c2016-03-02 17:53:39 -080064 if (it != allocations_.end()) {
65 *range = it->first;
66 *info = &it->second;
67 return true;
68 }
69 }
70 return false;
71}
72
73void HeapWalker::RecurseRoot(const Range& root) {
74 allocator::vector<Range> to_do(1, root, allocator_);
Colin Cross7add50d2016-01-14 15:35:40 -080075 while (!to_do.empty()) {
76 Range range = to_do.back();
77 to_do.pop_back();
Colin Cross8e8f34c2016-03-02 17:53:39 -080078
79 ForEachPtrInRange(range, [&](Range& ref_range, AllocationInfo* ref_info) {
80 if (!ref_info->referenced_from_root) {
81 ref_info->referenced_from_root = true;
82 to_do.push_back(ref_range);
Colin Cross7add50d2016-01-14 15:35:40 -080083 }
Colin Cross8e8f34c2016-03-02 17:53:39 -080084 });
Colin Cross7add50d2016-01-14 15:35:40 -080085 }
86}
87
88void HeapWalker::Root(uintptr_t begin, uintptr_t end) {
89 roots_.push_back(Range{begin, end});
90}
91
92void HeapWalker::Root(const allocator::vector<uintptr_t>& vals) {
93 root_vals_.insert(root_vals_.end(), vals.begin(), vals.end());
94}
95
96size_t HeapWalker::Allocations() {
97 return allocations_.size();
98}
99
100size_t HeapWalker::AllocationBytes() {
101 return allocation_bytes_;
102}
103
104bool HeapWalker::DetectLeaks() {
Colin Cross8e8f34c2016-03-02 17:53:39 -0800105 // Recursively walk pointers from roots to mark referenced allocations
Colin Cross7add50d2016-01-14 15:35:40 -0800106 for (auto it = roots_.begin(); it != roots_.end(); it++) {
Colin Cross8e8f34c2016-03-02 17:53:39 -0800107 RecurseRoot(*it);
Colin Cross7add50d2016-01-14 15:35:40 -0800108 }
109
110 Range vals;
111 vals.begin = reinterpret_cast<uintptr_t>(root_vals_.data());
112 vals.end = vals.begin + root_vals_.size() * sizeof(uintptr_t);
Colin Cross7add50d2016-01-14 15:35:40 -0800113
Colin Cross8e8f34c2016-03-02 17:53:39 -0800114 RecurseRoot(vals);
Colin Cross7add50d2016-01-14 15:35:40 -0800115
116 return true;
117}
118
Colin Crossa83881e2017-06-22 10:50:05 -0700119bool HeapWalker::Leaked(allocator::vector<Range>& leaked, size_t limit, size_t* num_leaks_out,
120 size_t* leak_bytes_out) {
Colin Cross7add50d2016-01-14 15:35:40 -0800121 leaked.clear();
122
123 size_t num_leaks = 0;
124 size_t leak_bytes = 0;
125 for (auto it = allocations_.begin(); it != allocations_.end(); it++) {
126 if (!it->second.referenced_from_root) {
127 num_leaks++;
128 leak_bytes += it->first.end - it->first.begin;
129 }
130 }
131
132 size_t n = 0;
133 for (auto it = allocations_.begin(); it != allocations_.end(); it++) {
134 if (!it->second.referenced_from_root) {
Colin Cross8e8f34c2016-03-02 17:53:39 -0800135 if (n++ < limit) {
Colin Cross7add50d2016-01-14 15:35:40 -0800136 leaked.push_back(it->first);
137 }
138 }
139 }
140
141 if (num_leaks_out) {
142 *num_leaks_out = num_leaks;
143 }
144 if (leak_bytes_out) {
145 *leak_bytes_out = leak_bytes;
146 }
147
148 return true;
149}
Colin Crossba5d9ff2016-04-26 16:51:32 -0700150
151static bool MapOverPage(void* addr) {
152 const size_t page_size = sysconf(_SC_PAGE_SIZE);
Colin Crossa83881e2017-06-22 10:50:05 -0700153 void* page = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) & ~(page_size - 1));
Colin Crossba5d9ff2016-04-26 16:51:32 -0700154
Colin Crossa83881e2017-06-22 10:50:05 -0700155 void* ret = mmap(page, page_size, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
Colin Crossba5d9ff2016-04-26 16:51:32 -0700156 if (ret == MAP_FAILED) {
Christopher Ferris47dea712017-05-03 17:34:29 -0700157 MEM_ALOGE("failed to map page at %p: %s", page, strerror(errno));
Colin Crossba5d9ff2016-04-26 16:51:32 -0700158 return false;
159 }
160
161 return true;
162}
163
Colin Crossa83881e2017-06-22 10:50:05 -0700164void HeapWalker::HandleSegFault(ScopedSignalHandler& handler, int signal, siginfo_t* si,
165 void* /*uctx*/) {
Colin Crossba5d9ff2016-04-26 16:51:32 -0700166 uintptr_t addr = reinterpret_cast<uintptr_t>(si->si_addr);
167 if (addr != walking_ptr_) {
168 handler.reset();
169 return;
170 }
Christopher Ferris47dea712017-05-03 17:34:29 -0700171 MEM_ALOGW("failed to read page at %p, signal %d", si->si_addr, signal);
Colin Crossba5d9ff2016-04-26 16:51:32 -0700172 if (!MapOverPage(si->si_addr)) {
173 handler.reset();
174 }
175}
176
177ScopedSignalHandler::SignalFn ScopedSignalHandler::handler_;
Colin Crossa9939e92017-06-21 13:13:00 -0700178
179} // namespace android