| 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 |  | 
| Colin Cross | 0965c02 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 20 | #include <signal.h> | 
|  | 21 |  | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 22 | #include "android-base/macros.h" | 
|  | 23 |  | 
|  | 24 | #include "Allocator.h" | 
| Colin Cross | 0965c02 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 25 | #include "ScopedSignalHandler.h" | 
| Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 26 | #include "Tarjan.h" | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 27 |  | 
| Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 28 | namespace android { | 
|  | 29 |  | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 30 | // A range [begin, end) | 
|  | 31 | struct Range { | 
|  | 32 | uintptr_t begin; | 
|  | 33 | uintptr_t end; | 
| Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 34 |  | 
|  | 35 | size_t size() const { return end - begin; }; | 
| Colin Cross | 583a250 | 2016-04-26 17:10:04 -0700 | [diff] [blame] | 36 | bool operator==(const Range& other) const { | 
|  | 37 | return this->begin == other.begin && this->end == other.end; | 
|  | 38 | } | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 39 | bool operator!=(const Range& other) const { return !(*this == other); } | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 40 | }; | 
|  | 41 |  | 
|  | 42 | // Comparator for Ranges that returns equivalence for overlapping ranges | 
|  | 43 | struct compare_range { | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 44 | bool operator()(const Range& a, const Range& b) const { return a.end <= b.begin; } | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 45 | }; | 
|  | 46 |  | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 47 | class HeapWalker { | 
|  | 48 | public: | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 49 | explicit HeapWalker(Allocator<HeapWalker> allocator) | 
|  | 50 | : allocator_(allocator), | 
|  | 51 | allocations_(allocator), | 
|  | 52 | allocation_bytes_(0), | 
|  | 53 | roots_(allocator), | 
|  | 54 | root_vals_(allocator), | 
| Pirama Arumuga Nainar | 02ab36e | 2018-09-26 09:00:24 -0700 | [diff] [blame] | 55 | segv_handler_(), | 
| Colin Cross | d780dcb | 2018-11-27 16:14:53 -0800 | [diff] [blame] | 56 | walking_ptr_(0), | 
|  | 57 | walking_range_{0, 0}, | 
|  | 58 | segv_logged_(false), | 
|  | 59 | segv_page_count_(0) { | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 60 | valid_allocations_range_.end = 0; | 
|  | 61 | valid_allocations_range_.begin = ~valid_allocations_range_.end; | 
| Colin Cross | 3ca1976 | 2018-11-28 17:01:59 -0800 | [diff] [blame] | 62 | valid_mappings_range_.end = 0; | 
|  | 63 | valid_mappings_range_.begin = ~valid_allocations_range_.end; | 
| Colin Cross | 0965c02 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 64 |  | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 65 | segv_handler_.install( | 
|  | 66 | SIGSEGV, [=](ScopedSignalHandler& handler, int signal, siginfo_t* siginfo, void* uctx) { | 
| Colin Cross | 0965c02 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 67 | this->HandleSegFault(handler, signal, siginfo, uctx); | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 68 | }); | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 69 | } | 
| Colin Cross | 0965c02 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 70 |  | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 71 | ~HeapWalker() {} | 
|  | 72 | bool Allocation(uintptr_t begin, uintptr_t end); | 
| Colin Cross | 3ca1976 | 2018-11-28 17:01:59 -0800 | [diff] [blame] | 73 | void Mapping(uintptr_t begin, uintptr_t end); | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 74 | void Root(uintptr_t begin, uintptr_t end); | 
|  | 75 | void Root(const allocator::vector<uintptr_t>& vals); | 
|  | 76 |  | 
|  | 77 | bool DetectLeaks(); | 
|  | 78 |  | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 79 | bool Leaked(allocator::vector<Range>&, size_t limit, size_t* num_leaks, size_t* leak_bytes); | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 80 | size_t Allocations(); | 
|  | 81 | size_t AllocationBytes(); | 
|  | 82 |  | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 83 | template <class F> | 
| Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 84 | void ForEachPtrInRange(const Range& range, F&& f); | 
|  | 85 |  | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 86 | template <class F> | 
| Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 87 | void ForEachAllocation(F&& f); | 
|  | 88 |  | 
|  | 89 | struct AllocationInfo { | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 90 | bool referenced_from_root; | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 91 | }; | 
| Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 92 |  | 
|  | 93 | private: | 
| Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 94 | void RecurseRoot(const Range& root); | 
| Colin Cross | 0965c02 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 95 | bool WordContainsAllocationPtr(uintptr_t ptr, Range* range, AllocationInfo** info); | 
|  | 96 | void HandleSegFault(ScopedSignalHandler&, int, siginfo_t*, void*); | 
| Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 97 |  | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 98 | DISALLOW_COPY_AND_ASSIGN(HeapWalker); | 
|  | 99 | Allocator<HeapWalker> allocator_; | 
| Colin Cross | e4cbe0e | 2016-03-04 16:34:42 -0800 | [diff] [blame] | 100 | using AllocationMap = allocator::map<Range, AllocationInfo, compare_range>; | 
| Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 101 | AllocationMap allocations_; | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 102 | size_t allocation_bytes_; | 
|  | 103 | Range valid_allocations_range_; | 
| Colin Cross | 3ca1976 | 2018-11-28 17:01:59 -0800 | [diff] [blame] | 104 | Range valid_mappings_range_; | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 105 |  | 
|  | 106 | allocator::vector<Range> roots_; | 
|  | 107 | allocator::vector<uintptr_t> root_vals_; | 
| Colin Cross | 0965c02 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 108 |  | 
|  | 109 | ScopedSignalHandler segv_handler_; | 
| Colin Cross | d780dcb | 2018-11-27 16:14:53 -0800 | [diff] [blame] | 110 | volatile uintptr_t walking_ptr_; | 
|  | 111 | Range walking_range_; | 
|  | 112 | bool segv_logged_; | 
|  | 113 | size_t segv_page_count_; | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 114 | }; | 
|  | 115 |  | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 116 | template <class F> | 
| Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 117 | inline void HeapWalker::ForEachPtrInRange(const Range& range, F&& f) { | 
|  | 118 | uintptr_t begin = (range.begin + (sizeof(uintptr_t) - 1)) & ~(sizeof(uintptr_t) - 1); | 
|  | 119 | // TODO(ccross): we might need to consider a pointer to the end of a buffer | 
|  | 120 | // to be inside the buffer, which means the common case of a pointer to the | 
|  | 121 | // beginning of a buffer may keep two ranges live. | 
|  | 122 | for (uintptr_t i = begin; i < range.end; i += sizeof(uintptr_t)) { | 
|  | 123 | Range ref_range; | 
|  | 124 | AllocationInfo* ref_info; | 
| Colin Cross | 0965c02 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 125 | if (WordContainsAllocationPtr(i, &ref_range, &ref_info)) { | 
| Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 126 | f(ref_range, ref_info); | 
|  | 127 | } | 
|  | 128 | } | 
|  | 129 | } | 
|  | 130 |  | 
| Colin Cross | a83881e | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 131 | template <class F> | 
| Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 132 | inline void HeapWalker::ForEachAllocation(F&& f) { | 
|  | 133 | for (auto& it : allocations_) { | 
|  | 134 | const Range& range = it.first; | 
|  | 135 | HeapWalker::AllocationInfo& allocation = it.second; | 
|  | 136 | f(range, allocation); | 
|  | 137 | } | 
|  | 138 | } | 
|  | 139 |  | 
| Colin Cross | a9939e9 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 140 | }  // namespace android | 
|  | 141 |  | 
| Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 142 | #endif |