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 | #include <inttypes.h> |
| 18 | |
| 19 | #include <map> |
| 20 | #include <utility> |
| 21 | |
| 22 | #include "Allocator.h" |
| 23 | #include "HeapWalker.h" |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 24 | #include "LeakFolding.h" |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 25 | #include "log.h" |
| 26 | |
| 27 | bool HeapWalker::Allocation(uintptr_t begin, uintptr_t end) { |
| 28 | if (end == begin) { |
| 29 | end = begin + 1; |
| 30 | } |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 31 | Range range{begin, end}; |
| 32 | auto inserted = allocations_.insert(std::pair<Range, AllocationInfo>(range, AllocationInfo{})); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 33 | 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 Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 36 | allocation_bytes_ += range.size(); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 37 | 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 Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 49 | bool 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 | |
| 61 | void HeapWalker::RecurseRoot(const Range& root) { |
| 62 | allocator::vector<Range> to_do(1, root, allocator_); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 63 | while (!to_do.empty()) { |
| 64 | Range range = to_do.back(); |
| 65 | to_do.pop_back(); |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 66 | |
| 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 Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 71 | } |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 72 | }); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | void HeapWalker::Root(uintptr_t begin, uintptr_t end) { |
| 77 | roots_.push_back(Range{begin, end}); |
| 78 | } |
| 79 | |
| 80 | void HeapWalker::Root(const allocator::vector<uintptr_t>& vals) { |
| 81 | root_vals_.insert(root_vals_.end(), vals.begin(), vals.end()); |
| 82 | } |
| 83 | |
| 84 | size_t HeapWalker::Allocations() { |
| 85 | return allocations_.size(); |
| 86 | } |
| 87 | |
| 88 | size_t HeapWalker::AllocationBytes() { |
| 89 | return allocation_bytes_; |
| 90 | } |
| 91 | |
| 92 | bool HeapWalker::DetectLeaks() { |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 93 | // Recursively walk pointers from roots to mark referenced allocations |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 94 | for (auto it = roots_.begin(); it != roots_.end(); it++) { |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 95 | RecurseRoot(*it); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 96 | } |
| 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 Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 101 | |
Colin Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 102 | RecurseRoot(vals); |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 103 | |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | bool HeapWalker::Leaked(allocator::vector<Range>& leaked, size_t limit, |
| 108 | size_t* num_leaks_out, size_t* leak_bytes_out) { |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 109 | 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 Cross | d6b3a2a | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 123 | if (n++ < limit) { |
Colin Cross | bcb4ed3 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 124 | 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 | } |