blob: 9e3db080dbe7cc21d3ecb7cc184e0f02aa36a8f1 [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),
Pirama Arumuga Nainar2583afd2018-09-26 09:00:24 -070055 segv_handler_(),
Colin Crossc2c76d42018-11-27 16:14:53 -080056 walking_ptr_(0),
57 walking_range_{0, 0},
58 segv_logged_(false),
59 segv_page_count_(0) {
Colin Crossab7c9f12016-01-14 15:35:40 -080060 valid_allocations_range_.end = 0;
61 valid_allocations_range_.begin = ~valid_allocations_range_.end;
Colin Cross223069f2018-11-28 17:01:59 -080062 valid_mappings_range_.end = 0;
63 valid_mappings_range_.begin = ~valid_allocations_range_.end;
Colin Cross1369ba42016-04-26 16:51:32 -070064
Colin Cross401319a2017-06-22 10:50:05 -070065 segv_handler_.install(
66 SIGSEGV, [=](ScopedSignalHandler& handler, int signal, siginfo_t* siginfo, void* uctx) {
Colin Cross1369ba42016-04-26 16:51:32 -070067 this->HandleSegFault(handler, signal, siginfo, uctx);
Colin Cross401319a2017-06-22 10:50:05 -070068 });
Colin Crossab7c9f12016-01-14 15:35:40 -080069 }
Colin Cross1369ba42016-04-26 16:51:32 -070070
Colin Crossab7c9f12016-01-14 15:35:40 -080071 ~HeapWalker() {}
72 bool Allocation(uintptr_t begin, uintptr_t end);
Colin Cross223069f2018-11-28 17:01:59 -080073 void Mapping(uintptr_t begin, uintptr_t end);
Colin Crossab7c9f12016-01-14 15:35:40 -080074 void Root(uintptr_t begin, uintptr_t end);
75 void Root(const allocator::vector<uintptr_t>& vals);
76
77 bool DetectLeaks();
78
Colin Cross401319a2017-06-22 10:50:05 -070079 bool Leaked(allocator::vector<Range>&, size_t limit, size_t* num_leaks, size_t* leak_bytes);
Colin Crossab7c9f12016-01-14 15:35:40 -080080 size_t Allocations();
81 size_t AllocationBytes();
82
Colin Cross401319a2017-06-22 10:50:05 -070083 template <class F>
Colin Crosseccc5e42016-03-02 17:53:39 -080084 void ForEachPtrInRange(const Range& range, F&& f);
85
Colin Cross401319a2017-06-22 10:50:05 -070086 template <class F>
Colin Crosseccc5e42016-03-02 17:53:39 -080087 void ForEachAllocation(F&& f);
88
89 struct AllocationInfo {
Colin Crossab7c9f12016-01-14 15:35:40 -080090 bool referenced_from_root;
Colin Crossab7c9f12016-01-14 15:35:40 -080091 };
Colin Crosseccc5e42016-03-02 17:53:39 -080092
93 private:
Colin Crosseccc5e42016-03-02 17:53:39 -080094 void RecurseRoot(const Range& root);
Colin Cross1369ba42016-04-26 16:51:32 -070095 bool WordContainsAllocationPtr(uintptr_t ptr, Range* range, AllocationInfo** info);
96 void HandleSegFault(ScopedSignalHandler&, int, siginfo_t*, void*);
Colin Crosseccc5e42016-03-02 17:53:39 -080097
Colin Crossab7c9f12016-01-14 15:35:40 -080098 DISALLOW_COPY_AND_ASSIGN(HeapWalker);
99 Allocator<HeapWalker> allocator_;
Colin Cross20774fc2016-03-04 16:34:42 -0800100 using AllocationMap = allocator::map<Range, AllocationInfo, compare_range>;
Colin Crosseccc5e42016-03-02 17:53:39 -0800101 AllocationMap allocations_;
Colin Crossab7c9f12016-01-14 15:35:40 -0800102 size_t allocation_bytes_;
103 Range valid_allocations_range_;
Colin Cross223069f2018-11-28 17:01:59 -0800104 Range valid_mappings_range_;
Colin Crossab7c9f12016-01-14 15:35:40 -0800105
106 allocator::vector<Range> roots_;
107 allocator::vector<uintptr_t> root_vals_;
Colin Cross1369ba42016-04-26 16:51:32 -0700108
109 ScopedSignalHandler segv_handler_;
Colin Crossc2c76d42018-11-27 16:14:53 -0800110 volatile uintptr_t walking_ptr_;
111 Range walking_range_;
112 bool segv_logged_;
113 size_t segv_page_count_;
Colin Crossab7c9f12016-01-14 15:35:40 -0800114};
115
Colin Cross401319a2017-06-22 10:50:05 -0700116template <class F>
Colin Crosseccc5e42016-03-02 17:53:39 -0800117inline 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 Cross1369ba42016-04-26 16:51:32 -0700125 if (WordContainsAllocationPtr(i, &ref_range, &ref_info)) {
Colin Crosseccc5e42016-03-02 17:53:39 -0800126 f(ref_range, ref_info);
127 }
128 }
129}
130
Colin Cross401319a2017-06-22 10:50:05 -0700131template <class F>
Colin Crosseccc5e42016-03-02 17:53:39 -0800132inline 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 Cross1fa81f52017-06-21 13:13:00 -0700140} // namespace android
141
Colin Crossab7c9f12016-01-14 15:35:40 -0800142#endif