Colin Cross | 7767318 | 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 | |
Colin Cross | f8bf327 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 17 | #include <errno.h> |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 18 | #include <inttypes.h> |
Peter Collingbourne | b4a37ff | 2020-01-10 19:15:35 -0800 | [diff] [blame^] | 19 | #include <sys/auxv.h> |
Colin Cross | f8bf327 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 20 | #include <sys/mman.h> |
| 21 | #include <unistd.h> |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 22 | |
Peter Collingbourne | b4a37ff | 2020-01-10 19:15:35 -0800 | [diff] [blame^] | 23 | #include <bionic/mte_kernel.h> |
| 24 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 25 | #include <map> |
| 26 | #include <utility> |
| 27 | |
| 28 | #include "Allocator.h" |
| 29 | #include "HeapWalker.h" |
Colin Cross | 6f922a4 | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 30 | #include "LeakFolding.h" |
Colin Cross | f8bf327 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 31 | #include "ScopedSignalHandler.h" |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 32 | #include "log.h" |
| 33 | |
Colin Cross | 1fa81f5 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 34 | namespace android { |
| 35 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 36 | bool HeapWalker::Allocation(uintptr_t begin, uintptr_t end) { |
| 37 | if (end == begin) { |
| 38 | end = begin + 1; |
| 39 | } |
Colin Cross | 6f922a4 | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 40 | Range range{begin, end}; |
Colin Cross | 223069f | 2018-11-28 17:01:59 -0800 | [diff] [blame] | 41 | if (valid_mappings_range_.end != 0 && |
| 42 | (begin < valid_mappings_range_.begin || end > valid_mappings_range_.end)) { |
| 43 | MEM_LOG_ALWAYS_FATAL("allocation %p-%p is outside mapping range %p-%p", |
| 44 | reinterpret_cast<void*>(begin), reinterpret_cast<void*>(end), |
| 45 | reinterpret_cast<void*>(valid_mappings_range_.begin), |
| 46 | reinterpret_cast<void*>(valid_mappings_range_.end)); |
| 47 | } |
Colin Cross | 6f922a4 | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 48 | auto inserted = allocations_.insert(std::pair<Range, AllocationInfo>(range, AllocationInfo{})); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 49 | if (inserted.second) { |
| 50 | valid_allocations_range_.begin = std::min(valid_allocations_range_.begin, begin); |
| 51 | valid_allocations_range_.end = std::max(valid_allocations_range_.end, end); |
Colin Cross | 6f922a4 | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 52 | allocation_bytes_ += range.size(); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 53 | return true; |
| 54 | } else { |
| 55 | Range overlap = inserted.first->first; |
Colin Cross | 1f99706 | 2016-04-26 17:10:04 -0700 | [diff] [blame] | 56 | if (overlap != range) { |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 57 | MEM_ALOGE("range %p-%p overlaps with existing range %p-%p", reinterpret_cast<void*>(begin), |
| 58 | reinterpret_cast<void*>(end), reinterpret_cast<void*>(overlap.begin), |
| 59 | reinterpret_cast<void*>(overlap.end)); |
Colin Cross | 1f99706 | 2016-04-26 17:10:04 -0700 | [diff] [blame] | 60 | } |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | |
Peter Collingbourne | b4a37ff | 2020-01-10 19:15:35 -0800 | [diff] [blame^] | 65 | // Sanitizers and MTE may consider certain memory inaccessible through certain pointers. |
| 66 | // With MTE we set PSTATE.TCO during the access to suppress tag checks. |
Evgenii Stepanov | 94485fa | 2019-03-19 17:17:47 -0700 | [diff] [blame] | 67 | static uintptr_t ReadWordAtAddressUnsafe(uintptr_t word_ptr) |
| 68 | __attribute__((no_sanitize("address", "hwaddress"))) { |
Peter Collingbourne | b4a37ff | 2020-01-10 19:15:35 -0800 | [diff] [blame^] | 69 | #if defined(__aarch64__) |
| 70 | #if defined(ANDROID_EXPERIMENTAL_MTE) |
| 71 | static bool mte = getauxval(AT_HWCAP2) & HWCAP2_MTE; |
| 72 | #else |
| 73 | static bool mte = false; |
| 74 | #endif |
| 75 | if (mte) { |
| 76 | // Disable tag checks. |
| 77 | __asm__ __volatile__(".arch_extension mte; msr tco, #1"); |
| 78 | } |
| 79 | #endif |
| 80 | |
| 81 | // Load a word from memory without ASAN/HWASAN/MTE checks. |
| 82 | uintptr_t retval = *reinterpret_cast<uintptr_t*>(word_ptr); |
| 83 | |
| 84 | #if defined(__aarch64__) |
| 85 | if (mte) { |
| 86 | // Re-enable tag checks. |
| 87 | __asm__ __volatile__(".arch_extension mte; msr tco, #0"); |
| 88 | } |
| 89 | #endif |
| 90 | return retval; |
Evgenii Stepanov | 94485fa | 2019-03-19 17:17:47 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Colin Cross | f8bf327 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 93 | bool HeapWalker::WordContainsAllocationPtr(uintptr_t word_ptr, Range* range, AllocationInfo** info) { |
| 94 | walking_ptr_ = word_ptr; |
| 95 | // This access may segfault if the process under test has done something strange, |
| 96 | // for example mprotect(PROT_NONE) on a native heap page. If so, it will be |
| 97 | // caught and handled by mmaping a zero page over the faulting page. |
Evgenii Stepanov | 94485fa | 2019-03-19 17:17:47 -0700 | [diff] [blame] | 98 | uintptr_t value = ReadWordAtAddressUnsafe(word_ptr); |
Colin Cross | f8bf327 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 99 | walking_ptr_ = 0; |
| 100 | if (value >= valid_allocations_range_.begin && value < valid_allocations_range_.end) { |
| 101 | AllocationMap::iterator it = allocations_.find(Range{value, value + 1}); |
Colin Cross | 6f922a4 | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 102 | if (it != allocations_.end()) { |
| 103 | *range = it->first; |
| 104 | *info = &it->second; |
| 105 | return true; |
| 106 | } |
| 107 | } |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | void HeapWalker::RecurseRoot(const Range& root) { |
| 112 | allocator::vector<Range> to_do(1, root, allocator_); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 113 | while (!to_do.empty()) { |
| 114 | Range range = to_do.back(); |
| 115 | to_do.pop_back(); |
Colin Cross | 6f922a4 | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 116 | |
Colin Cross | c2c76d4 | 2018-11-27 16:14:53 -0800 | [diff] [blame] | 117 | walking_range_ = range; |
Colin Cross | 6f922a4 | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 118 | ForEachPtrInRange(range, [&](Range& ref_range, AllocationInfo* ref_info) { |
| 119 | if (!ref_info->referenced_from_root) { |
| 120 | ref_info->referenced_from_root = true; |
| 121 | to_do.push_back(ref_range); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 122 | } |
Colin Cross | 6f922a4 | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 123 | }); |
Colin Cross | c2c76d4 | 2018-11-27 16:14:53 -0800 | [diff] [blame] | 124 | walking_range_ = Range{0, 0}; |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Colin Cross | 223069f | 2018-11-28 17:01:59 -0800 | [diff] [blame] | 128 | void HeapWalker::Mapping(uintptr_t begin, uintptr_t end) { |
| 129 | valid_mappings_range_.begin = std::min(valid_mappings_range_.begin, begin); |
| 130 | valid_mappings_range_.end = std::max(valid_mappings_range_.end, end); |
| 131 | } |
| 132 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 133 | void HeapWalker::Root(uintptr_t begin, uintptr_t end) { |
| 134 | roots_.push_back(Range{begin, end}); |
| 135 | } |
| 136 | |
| 137 | void HeapWalker::Root(const allocator::vector<uintptr_t>& vals) { |
| 138 | root_vals_.insert(root_vals_.end(), vals.begin(), vals.end()); |
| 139 | } |
| 140 | |
| 141 | size_t HeapWalker::Allocations() { |
| 142 | return allocations_.size(); |
| 143 | } |
| 144 | |
| 145 | size_t HeapWalker::AllocationBytes() { |
| 146 | return allocation_bytes_; |
| 147 | } |
| 148 | |
| 149 | bool HeapWalker::DetectLeaks() { |
Colin Cross | 6f922a4 | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 150 | // Recursively walk pointers from roots to mark referenced allocations |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 151 | for (auto it = roots_.begin(); it != roots_.end(); it++) { |
Colin Cross | 6f922a4 | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 152 | RecurseRoot(*it); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | Range vals; |
| 156 | vals.begin = reinterpret_cast<uintptr_t>(root_vals_.data()); |
| 157 | vals.end = vals.begin + root_vals_.size() * sizeof(uintptr_t); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 158 | |
Colin Cross | 6f922a4 | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 159 | RecurseRoot(vals); |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 160 | |
Colin Cross | c2c76d4 | 2018-11-27 16:14:53 -0800 | [diff] [blame] | 161 | if (segv_page_count_ > 0) { |
| 162 | MEM_ALOGE("%zu pages skipped due to segfaults", segv_page_count_); |
| 163 | } |
| 164 | |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 165 | return true; |
| 166 | } |
| 167 | |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 168 | bool HeapWalker::Leaked(allocator::vector<Range>& leaked, size_t limit, size_t* num_leaks_out, |
| 169 | size_t* leak_bytes_out) { |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 170 | leaked.clear(); |
| 171 | |
| 172 | size_t num_leaks = 0; |
| 173 | size_t leak_bytes = 0; |
| 174 | for (auto it = allocations_.begin(); it != allocations_.end(); it++) { |
| 175 | if (!it->second.referenced_from_root) { |
| 176 | num_leaks++; |
| 177 | leak_bytes += it->first.end - it->first.begin; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | size_t n = 0; |
| 182 | for (auto it = allocations_.begin(); it != allocations_.end(); it++) { |
| 183 | if (!it->second.referenced_from_root) { |
Colin Cross | 6f922a4 | 2016-03-02 17:53:39 -0800 | [diff] [blame] | 184 | if (n++ < limit) { |
Colin Cross | 7767318 | 2016-01-14 15:35:40 -0800 | [diff] [blame] | 185 | leaked.push_back(it->first); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (num_leaks_out) { |
| 191 | *num_leaks_out = num_leaks; |
| 192 | } |
| 193 | if (leak_bytes_out) { |
| 194 | *leak_bytes_out = leak_bytes; |
| 195 | } |
| 196 | |
| 197 | return true; |
| 198 | } |
Colin Cross | f8bf327 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 199 | |
| 200 | static bool MapOverPage(void* addr) { |
| 201 | const size_t page_size = sysconf(_SC_PAGE_SIZE); |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 202 | void* page = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(addr) & ~(page_size - 1)); |
Colin Cross | f8bf327 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 203 | |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 204 | void* ret = mmap(page, page_size, PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0); |
Colin Cross | f8bf327 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 205 | if (ret == MAP_FAILED) { |
Christopher Ferris | 56b8d86 | 2017-05-03 17:34:29 -0700 | [diff] [blame] | 206 | MEM_ALOGE("failed to map page at %p: %s", page, strerror(errno)); |
Colin Cross | f8bf327 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 207 | return false; |
| 208 | } |
| 209 | |
| 210 | return true; |
| 211 | } |
| 212 | |
Colin Cross | 401319a | 2017-06-22 10:50:05 -0700 | [diff] [blame] | 213 | void HeapWalker::HandleSegFault(ScopedSignalHandler& handler, int signal, siginfo_t* si, |
| 214 | void* /*uctx*/) { |
Colin Cross | f8bf327 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 215 | uintptr_t addr = reinterpret_cast<uintptr_t>(si->si_addr); |
| 216 | if (addr != walking_ptr_) { |
| 217 | handler.reset(); |
| 218 | return; |
| 219 | } |
Colin Cross | c2c76d4 | 2018-11-27 16:14:53 -0800 | [diff] [blame] | 220 | if (!segv_logged_) { |
| 221 | MEM_ALOGW("failed to read page at %p, signal %d", si->si_addr, signal); |
| 222 | if (walking_range_.begin != 0U) { |
| 223 | MEM_ALOGW("while walking range %p-%p", reinterpret_cast<void*>(walking_range_.begin), |
| 224 | reinterpret_cast<void*>(walking_range_.end)); |
| 225 | } |
| 226 | segv_logged_ = true; |
| 227 | } |
| 228 | segv_page_count_++; |
Colin Cross | f8bf327 | 2016-04-26 16:51:32 -0700 | [diff] [blame] | 229 | if (!MapOverPage(si->si_addr)) { |
| 230 | handler.reset(); |
| 231 | } |
| 232 | } |
| 233 | |
Colin Cross | 8837c72 | 2019-03-20 16:02:54 -0700 | [diff] [blame] | 234 | Allocator<ScopedSignalHandler::SignalFnMap>::unique_ptr ScopedSignalHandler::handler_map_; |
Colin Cross | 1fa81f5 | 2017-06-21 13:13:00 -0700 | [diff] [blame] | 235 | |
| 236 | } // namespace android |