Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 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 | #ifndef LIBMEMUNREACHABLE_HEAP_WALKER_H_ |
| 18 | #define LIBMEMUNREACHABLE_HEAP_WALKER_H_ |
| 19 | |
| 20 | #include "android-base/macros.h" |
| 21 | |
| 22 | #include "Allocator.h" |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 23 | #include "Tarjan.h" |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 24 | |
| 25 | // A range [begin, end) |
| 26 | struct Range { |
| 27 | uintptr_t begin; |
| 28 | uintptr_t end; |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 29 | |
| 30 | size_t size() const { return end - begin; }; |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | // Comparator for Ranges that returns equivalence for overlapping ranges |
| 34 | struct compare_range { |
| 35 | bool operator()(const Range& a, const Range& b) const { |
| 36 | return a.end <= b.begin; |
| 37 | } |
| 38 | }; |
| 39 | |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 40 | class HeapWalker { |
| 41 | public: |
| 42 | HeapWalker(Allocator<HeapWalker> allocator) : allocator_(allocator), |
| 43 | allocations_(allocator), allocation_bytes_(0), |
| 44 | roots_(allocator), root_vals_(allocator) { |
| 45 | valid_allocations_range_.end = 0; |
| 46 | valid_allocations_range_.begin = ~valid_allocations_range_.end; |
| 47 | } |
| 48 | ~HeapWalker() {} |
| 49 | bool Allocation(uintptr_t begin, uintptr_t end); |
| 50 | void Root(uintptr_t begin, uintptr_t end); |
| 51 | void Root(const allocator::vector<uintptr_t>& vals); |
| 52 | |
| 53 | bool DetectLeaks(); |
| 54 | |
| 55 | bool Leaked(allocator::vector<Range>&, size_t limit, size_t* num_leaks, |
| 56 | size_t* leak_bytes); |
| 57 | size_t Allocations(); |
| 58 | size_t AllocationBytes(); |
| 59 | |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 60 | template<class F> |
| 61 | void ForEachPtrInRange(const Range& range, F&& f); |
| 62 | |
| 63 | template<class F> |
| 64 | void ForEachAllocation(F&& f); |
| 65 | |
| 66 | struct AllocationInfo { |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 67 | bool referenced_from_root; |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 68 | }; |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 69 | |
| 70 | private: |
| 71 | |
| 72 | void RecurseRoot(const Range& root); |
| 73 | bool IsAllocationPtr(uintptr_t ptr, Range* range, AllocationInfo** info); |
| 74 | |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 75 | DISALLOW_COPY_AND_ASSIGN(HeapWalker); |
| 76 | Allocator<HeapWalker> allocator_; |
Colin Cross | e4cbe0e | 2016-03-04 16:34:42 -0800 | [diff] [blame] | 77 | using AllocationMap = allocator::map<Range, AllocationInfo, compare_range>; |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 78 | AllocationMap allocations_; |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 79 | size_t allocation_bytes_; |
| 80 | Range valid_allocations_range_; |
| 81 | |
| 82 | allocator::vector<Range> roots_; |
| 83 | allocator::vector<uintptr_t> root_vals_; |
| 84 | }; |
| 85 | |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 86 | template<class F> |
| 87 | inline void HeapWalker::ForEachPtrInRange(const Range& range, F&& f) { |
| 88 | uintptr_t begin = (range.begin + (sizeof(uintptr_t) - 1)) & ~(sizeof(uintptr_t) - 1); |
| 89 | // TODO(ccross): we might need to consider a pointer to the end of a buffer |
| 90 | // to be inside the buffer, which means the common case of a pointer to the |
| 91 | // beginning of a buffer may keep two ranges live. |
| 92 | for (uintptr_t i = begin; i < range.end; i += sizeof(uintptr_t)) { |
| 93 | Range ref_range; |
| 94 | AllocationInfo* ref_info; |
| 95 | if (IsAllocationPtr(*reinterpret_cast<uintptr_t*>(i), &ref_range, &ref_info)) { |
| 96 | f(ref_range, ref_info); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | template<class F> |
| 102 | inline void HeapWalker::ForEachAllocation(F&& f) { |
| 103 | for (auto& it : allocations_) { |
| 104 | const Range& range = it.first; |
| 105 | HeapWalker::AllocationInfo& allocation = it.second; |
| 106 | f(range, allocation); |
| 107 | } |
| 108 | } |
| 109 | |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 110 | #endif |