blob: 19393ecb877494985924074c5ee97f62c3d1a8c4 [file] [log] [blame]
Colin Crossbcb4ed32016-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>
18
19#include <map>
20#include <utility>
21
22#include "Allocator.h"
23#include "HeapWalker.h"
Colin Crossd6b3a2a2016-03-02 17:53:39 -080024#include "LeakFolding.h"
Colin Crossbcb4ed32016-01-14 15:35:40 -080025#include "log.h"
26
27bool HeapWalker::Allocation(uintptr_t begin, uintptr_t end) {
28 if (end == begin) {
29 end = begin + 1;
30 }
Colin Crossd6b3a2a2016-03-02 17:53:39 -080031 Range range{begin, end};
32 auto inserted = allocations_.insert(std::pair<Range, AllocationInfo>(range, AllocationInfo{}));
Colin Crossbcb4ed32016-01-14 15:35:40 -080033 if (inserted.second) {
34 valid_allocations_range_.begin = std::min(valid_allocations_range_.begin, begin);
35 valid_allocations_range_.end = std::max(valid_allocations_range_.end, end);
Colin Crossd6b3a2a2016-03-02 17:53:39 -080036 allocation_bytes_ += range.size();
Colin Crossbcb4ed32016-01-14 15:35:40 -080037 return true;
38 } else {
39 Range overlap = inserted.first->first;
40 ALOGE("range %p-%p overlaps with existing range %p-%p",
41 reinterpret_cast<void*>(begin),
42 reinterpret_cast<void*>(end),
43 reinterpret_cast<void*>(overlap.begin),
44 reinterpret_cast<void*>(overlap.end));
45 return false;
46 }
47}
48
Colin Crossd6b3a2a2016-03-02 17:53:39 -080049bool HeapWalker::IsAllocationPtr(uintptr_t ptr, Range* range, AllocationInfo** info) {
50 if (ptr >= valid_allocations_range_.begin && ptr < valid_allocations_range_.end) {
51 AllocationMap::iterator it = allocations_.find(Range{ptr, ptr + 1});
52 if (it != allocations_.end()) {
53 *range = it->first;
54 *info = &it->second;
55 return true;
56 }
57 }
58 return false;
59}
60
61void HeapWalker::RecurseRoot(const Range& root) {
62 allocator::vector<Range> to_do(1, root, allocator_);
Colin Crossbcb4ed32016-01-14 15:35:40 -080063 while (!to_do.empty()) {
64 Range range = to_do.back();
65 to_do.pop_back();
Colin Crossd6b3a2a2016-03-02 17:53:39 -080066
67 ForEachPtrInRange(range, [&](Range& ref_range, AllocationInfo* ref_info) {
68 if (!ref_info->referenced_from_root) {
69 ref_info->referenced_from_root = true;
70 to_do.push_back(ref_range);
Colin Crossbcb4ed32016-01-14 15:35:40 -080071 }
Colin Crossd6b3a2a2016-03-02 17:53:39 -080072 });
Colin Crossbcb4ed32016-01-14 15:35:40 -080073 }
74}
75
76void HeapWalker::Root(uintptr_t begin, uintptr_t end) {
77 roots_.push_back(Range{begin, end});
78}
79
80void HeapWalker::Root(const allocator::vector<uintptr_t>& vals) {
81 root_vals_.insert(root_vals_.end(), vals.begin(), vals.end());
82}
83
84size_t HeapWalker::Allocations() {
85 return allocations_.size();
86}
87
88size_t HeapWalker::AllocationBytes() {
89 return allocation_bytes_;
90}
91
92bool HeapWalker::DetectLeaks() {
Colin Crossd6b3a2a2016-03-02 17:53:39 -080093 // Recursively walk pointers from roots to mark referenced allocations
Colin Crossbcb4ed32016-01-14 15:35:40 -080094 for (auto it = roots_.begin(); it != roots_.end(); it++) {
Colin Crossd6b3a2a2016-03-02 17:53:39 -080095 RecurseRoot(*it);
Colin Crossbcb4ed32016-01-14 15:35:40 -080096 }
97
98 Range vals;
99 vals.begin = reinterpret_cast<uintptr_t>(root_vals_.data());
100 vals.end = vals.begin + root_vals_.size() * sizeof(uintptr_t);
Colin Crossbcb4ed32016-01-14 15:35:40 -0800101
Colin Crossd6b3a2a2016-03-02 17:53:39 -0800102 RecurseRoot(vals);
Colin Crossbcb4ed32016-01-14 15:35:40 -0800103
104 return true;
105}
106
107bool HeapWalker::Leaked(allocator::vector<Range>& leaked, size_t limit,
108 size_t* num_leaks_out, size_t* leak_bytes_out) {
Colin Crossbcb4ed32016-01-14 15:35:40 -0800109 leaked.clear();
110
111 size_t num_leaks = 0;
112 size_t leak_bytes = 0;
113 for (auto it = allocations_.begin(); it != allocations_.end(); it++) {
114 if (!it->second.referenced_from_root) {
115 num_leaks++;
116 leak_bytes += it->first.end - it->first.begin;
117 }
118 }
119
120 size_t n = 0;
121 for (auto it = allocations_.begin(); it != allocations_.end(); it++) {
122 if (!it->second.referenced_from_root) {
Colin Crossd6b3a2a2016-03-02 17:53:39 -0800123 if (n++ < limit) {
Colin Crossbcb4ed32016-01-14 15:35:40 -0800124 leaked.push_back(it->first);
125 }
126 }
127 }
128
129 if (num_leaks_out) {
130 *num_leaks_out = num_leaks;
131 }
132 if (leak_bytes_out) {
133 *leak_bytes_out = leak_bytes;
134 }
135
136 return true;
137}