blob: f00bccaaa3607409c000fd743d23688e2fb38aa3 [file] [log] [blame]
Colin Crossab7c9f12016-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#ifndef LIBMEMUNREACHABLE_HEAP_WALKER_H_
18#define LIBMEMUNREACHABLE_HEAP_WALKER_H_
19
Colin Cross1369ba42016-04-26 16:51:32 -070020#include <signal.h>
21
Colin Crossab7c9f12016-01-14 15:35:40 -080022#include "android-base/macros.h"
23
24#include "Allocator.h"
Colin Cross1369ba42016-04-26 16:51:32 -070025#include "ScopedSignalHandler.h"
Colin Crosseccc5e42016-03-02 17:53:39 -080026#include "Tarjan.h"
Colin Crossab7c9f12016-01-14 15:35:40 -080027
Colin Cross1fa81f52017-06-21 13:13:00 -070028namespace android {
29
Colin Crossab7c9f12016-01-14 15:35:40 -080030// A range [begin, end)
31struct Range {
32 uintptr_t begin;
33 uintptr_t end;
Colin Crosseccc5e42016-03-02 17:53:39 -080034
35 size_t size() const { return end - begin; };
Colin Cross0b631e72016-04-26 17:10:04 -070036 bool operator==(const Range& other) const {
37 return this->begin == other.begin && this->end == other.end;
38 }
Colin Cross401319a2017-06-22 10:50:05 -070039 bool operator!=(const Range& other) const { return !(*this == other); }
Colin Crossab7c9f12016-01-14 15:35:40 -080040};
41
42// Comparator for Ranges that returns equivalence for overlapping ranges
43struct compare_range {
Colin Cross401319a2017-06-22 10:50:05 -070044 bool operator()(const Range& a, const Range& b) const { return a.end <= b.begin; }
Colin Crossab7c9f12016-01-14 15:35:40 -080045};
46
Colin Crossab7c9f12016-01-14 15:35:40 -080047class HeapWalker {
48 public:
Colin Cross401319a2017-06-22 10:50:05 -070049 explicit HeapWalker(Allocator<HeapWalker> allocator)
50 : allocator_(allocator),
51 allocations_(allocator),
52 allocation_bytes_(0),
53 roots_(allocator),
54 root_vals_(allocator),
Colin Cross8837c722019-03-20 16:02:54 -070055 sigsegv_handler_(allocator),
56 sigbus_handler_(allocator),
Colin Crossc2c76d42018-11-27 16:14:53 -080057 walking_ptr_(0),
58 walking_range_{0, 0},
59 segv_logged_(false),
60 segv_page_count_(0) {
Colin Crossab7c9f12016-01-14 15:35:40 -080061 valid_allocations_range_.end = 0;
62 valid_allocations_range_.begin = ~valid_allocations_range_.end;
Colin Cross223069f2018-11-28 17:01:59 -080063 valid_mappings_range_.end = 0;
64 valid_mappings_range_.begin = ~valid_allocations_range_.end;
Colin Cross1369ba42016-04-26 16:51:32 -070065
Colin Cross8837c722019-03-20 16:02:54 -070066 sigsegv_handler_.install(
Colin Cross401319a2017-06-22 10:50:05 -070067 SIGSEGV, [=](ScopedSignalHandler& handler, int signal, siginfo_t* siginfo, void* uctx) {
Colin Cross1369ba42016-04-26 16:51:32 -070068 this->HandleSegFault(handler, signal, siginfo, uctx);
Colin Cross401319a2017-06-22 10:50:05 -070069 });
Colin Cross8837c722019-03-20 16:02:54 -070070 sigbus_handler_.install(
71 SIGBUS, [=](ScopedSignalHandler& handler, int signal, siginfo_t* siginfo, void* uctx) {
72 this->HandleSegFault(handler, signal, siginfo, uctx);
73 });
Colin Crossab7c9f12016-01-14 15:35:40 -080074 }
Colin Cross1369ba42016-04-26 16:51:32 -070075
Colin Crossab7c9f12016-01-14 15:35:40 -080076 ~HeapWalker() {}
77 bool Allocation(uintptr_t begin, uintptr_t end);
Colin Cross223069f2018-11-28 17:01:59 -080078 void Mapping(uintptr_t begin, uintptr_t end);
Colin Crossab7c9f12016-01-14 15:35:40 -080079 void Root(uintptr_t begin, uintptr_t end);
80 void Root(const allocator::vector<uintptr_t>& vals);
81
82 bool DetectLeaks();
83
Colin Cross401319a2017-06-22 10:50:05 -070084 bool Leaked(allocator::vector<Range>&, size_t limit, size_t* num_leaks, size_t* leak_bytes);
Colin Crossab7c9f12016-01-14 15:35:40 -080085 size_t Allocations();
86 size_t AllocationBytes();
87
Colin Cross401319a2017-06-22 10:50:05 -070088 template <class F>
Colin Crosseccc5e42016-03-02 17:53:39 -080089 void ForEachPtrInRange(const Range& range, F&& f);
90
Colin Cross401319a2017-06-22 10:50:05 -070091 template <class F>
Colin Crosseccc5e42016-03-02 17:53:39 -080092 void ForEachAllocation(F&& f);
93
94 struct AllocationInfo {
Colin Crossab7c9f12016-01-14 15:35:40 -080095 bool referenced_from_root;
Colin Crossab7c9f12016-01-14 15:35:40 -080096 };
Colin Crosseccc5e42016-03-02 17:53:39 -080097
98 private:
Colin Crosseccc5e42016-03-02 17:53:39 -080099 void RecurseRoot(const Range& root);
Colin Cross1369ba42016-04-26 16:51:32 -0700100 bool WordContainsAllocationPtr(uintptr_t ptr, Range* range, AllocationInfo** info);
101 void HandleSegFault(ScopedSignalHandler&, int, siginfo_t*, void*);
Colin Crosseccc5e42016-03-02 17:53:39 -0800102
Colin Crossab7c9f12016-01-14 15:35:40 -0800103 DISALLOW_COPY_AND_ASSIGN(HeapWalker);
104 Allocator<HeapWalker> allocator_;
Colin Cross20774fc2016-03-04 16:34:42 -0800105 using AllocationMap = allocator::map<Range, AllocationInfo, compare_range>;
Colin Crosseccc5e42016-03-02 17:53:39 -0800106 AllocationMap allocations_;
Colin Crossab7c9f12016-01-14 15:35:40 -0800107 size_t allocation_bytes_;
108 Range valid_allocations_range_;
Colin Cross223069f2018-11-28 17:01:59 -0800109 Range valid_mappings_range_;
Colin Crossab7c9f12016-01-14 15:35:40 -0800110
111 allocator::vector<Range> roots_;
112 allocator::vector<uintptr_t> root_vals_;
Colin Cross1369ba42016-04-26 16:51:32 -0700113
Colin Cross8837c722019-03-20 16:02:54 -0700114 ScopedSignalHandler sigsegv_handler_;
115 ScopedSignalHandler sigbus_handler_;
Colin Crossc2c76d42018-11-27 16:14:53 -0800116 volatile uintptr_t walking_ptr_;
117 Range walking_range_;
118 bool segv_logged_;
119 size_t segv_page_count_;
Colin Crossab7c9f12016-01-14 15:35:40 -0800120};
121
Colin Cross401319a2017-06-22 10:50:05 -0700122template <class F>
Colin Crosseccc5e42016-03-02 17:53:39 -0800123inline void HeapWalker::ForEachPtrInRange(const Range& range, F&& f) {
124 uintptr_t begin = (range.begin + (sizeof(uintptr_t) - 1)) & ~(sizeof(uintptr_t) - 1);
125 // TODO(ccross): we might need to consider a pointer to the end of a buffer
126 // to be inside the buffer, which means the common case of a pointer to the
127 // beginning of a buffer may keep two ranges live.
128 for (uintptr_t i = begin; i < range.end; i += sizeof(uintptr_t)) {
129 Range ref_range;
130 AllocationInfo* ref_info;
Colin Cross1369ba42016-04-26 16:51:32 -0700131 if (WordContainsAllocationPtr(i, &ref_range, &ref_info)) {
Colin Crosseccc5e42016-03-02 17:53:39 -0800132 f(ref_range, ref_info);
133 }
134 }
135}
136
Colin Cross401319a2017-06-22 10:50:05 -0700137template <class F>
Colin Crosseccc5e42016-03-02 17:53:39 -0800138inline void HeapWalker::ForEachAllocation(F&& f) {
139 for (auto& it : allocations_) {
140 const Range& range = it.first;
141 HeapWalker::AllocationInfo& allocation = it.second;
142 f(range, allocation);
143 }
144}
145
Colin Cross1fa81f52017-06-21 13:13:00 -0700146} // namespace android
147
Colin Crossab7c9f12016-01-14 15:35:40 -0800148#endif