blob: b37cc623b85d2edf470eafb8d43e353f328ce3cb [file] [log] [blame]
Colin Crossbcb4ed32016-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 Cross0965c022016-04-26 16:51:32 -070020#include <signal.h>
21
Colin Crossbcb4ed32016-01-14 15:35:40 -080022#include "android-base/macros.h"
23
24#include "Allocator.h"
Colin Cross0965c022016-04-26 16:51:32 -070025#include "ScopedSignalHandler.h"
Colin Crossd6b3a2a2016-03-02 17:53:39 -080026#include "Tarjan.h"
Colin Crossbcb4ed32016-01-14 15:35:40 -080027
Colin Crossa9939e92017-06-21 13:13:00 -070028namespace android {
29
Colin Crossbcb4ed32016-01-14 15:35:40 -080030// A range [begin, end)
31struct Range {
32 uintptr_t begin;
33 uintptr_t end;
Colin Crossd6b3a2a2016-03-02 17:53:39 -080034
35 size_t size() const { return end - begin; };
Colin Cross583a2502016-04-26 17:10:04 -070036 bool operator==(const Range& other) const {
37 return this->begin == other.begin && this->end == other.end;
38 }
Colin Crossa83881e2017-06-22 10:50:05 -070039 bool operator!=(const Range& other) const { return !(*this == other); }
Colin Crossbcb4ed32016-01-14 15:35:40 -080040};
41
42// Comparator for Ranges that returns equivalence for overlapping ranges
43struct compare_range {
Colin Crossa83881e2017-06-22 10:50:05 -070044 bool operator()(const Range& a, const Range& b) const { return a.end <= b.begin; }
Colin Crossbcb4ed32016-01-14 15:35:40 -080045};
46
Colin Crossbcb4ed32016-01-14 15:35:40 -080047class HeapWalker {
48 public:
Colin Crossa83881e2017-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 Nainar02ab36e2018-09-26 09:00:24 -070055 segv_handler_(),
Colin Crossd780dcb2018-11-27 16:14:53 -080056 walking_ptr_(0),
57 walking_range_{0, 0},
58 segv_logged_(false),
59 segv_page_count_(0) {
Colin Crossbcb4ed32016-01-14 15:35:40 -080060 valid_allocations_range_.end = 0;
61 valid_allocations_range_.begin = ~valid_allocations_range_.end;
Colin Cross0965c022016-04-26 16:51:32 -070062
Colin Crossa83881e2017-06-22 10:50:05 -070063 segv_handler_.install(
64 SIGSEGV, [=](ScopedSignalHandler& handler, int signal, siginfo_t* siginfo, void* uctx) {
Colin Cross0965c022016-04-26 16:51:32 -070065 this->HandleSegFault(handler, signal, siginfo, uctx);
Colin Crossa83881e2017-06-22 10:50:05 -070066 });
Colin Crossbcb4ed32016-01-14 15:35:40 -080067 }
Colin Cross0965c022016-04-26 16:51:32 -070068
Colin Crossbcb4ed32016-01-14 15:35:40 -080069 ~HeapWalker() {}
70 bool Allocation(uintptr_t begin, uintptr_t end);
71 void Root(uintptr_t begin, uintptr_t end);
72 void Root(const allocator::vector<uintptr_t>& vals);
73
74 bool DetectLeaks();
75
Colin Crossa83881e2017-06-22 10:50:05 -070076 bool Leaked(allocator::vector<Range>&, size_t limit, size_t* num_leaks, size_t* leak_bytes);
Colin Crossbcb4ed32016-01-14 15:35:40 -080077 size_t Allocations();
78 size_t AllocationBytes();
79
Colin Crossa83881e2017-06-22 10:50:05 -070080 template <class F>
Colin Crossd6b3a2a2016-03-02 17:53:39 -080081 void ForEachPtrInRange(const Range& range, F&& f);
82
Colin Crossa83881e2017-06-22 10:50:05 -070083 template <class F>
Colin Crossd6b3a2a2016-03-02 17:53:39 -080084 void ForEachAllocation(F&& f);
85
86 struct AllocationInfo {
Colin Crossbcb4ed32016-01-14 15:35:40 -080087 bool referenced_from_root;
Colin Crossbcb4ed32016-01-14 15:35:40 -080088 };
Colin Crossd6b3a2a2016-03-02 17:53:39 -080089
90 private:
Colin Crossd6b3a2a2016-03-02 17:53:39 -080091 void RecurseRoot(const Range& root);
Colin Cross0965c022016-04-26 16:51:32 -070092 bool WordContainsAllocationPtr(uintptr_t ptr, Range* range, AllocationInfo** info);
93 void HandleSegFault(ScopedSignalHandler&, int, siginfo_t*, void*);
Colin Crossd6b3a2a2016-03-02 17:53:39 -080094
Colin Crossbcb4ed32016-01-14 15:35:40 -080095 DISALLOW_COPY_AND_ASSIGN(HeapWalker);
96 Allocator<HeapWalker> allocator_;
Colin Crosse4cbe0e2016-03-04 16:34:42 -080097 using AllocationMap = allocator::map<Range, AllocationInfo, compare_range>;
Colin Crossd6b3a2a2016-03-02 17:53:39 -080098 AllocationMap allocations_;
Colin Crossbcb4ed32016-01-14 15:35:40 -080099 size_t allocation_bytes_;
100 Range valid_allocations_range_;
101
102 allocator::vector<Range> roots_;
103 allocator::vector<uintptr_t> root_vals_;
Colin Cross0965c022016-04-26 16:51:32 -0700104
105 ScopedSignalHandler segv_handler_;
Colin Crossd780dcb2018-11-27 16:14:53 -0800106 volatile uintptr_t walking_ptr_;
107 Range walking_range_;
108 bool segv_logged_;
109 size_t segv_page_count_;
Colin Crossbcb4ed32016-01-14 15:35:40 -0800110};
111
Colin Crossa83881e2017-06-22 10:50:05 -0700112template <class F>
Colin Crossd6b3a2a2016-03-02 17:53:39 -0800113inline void HeapWalker::ForEachPtrInRange(const Range& range, F&& f) {
114 uintptr_t begin = (range.begin + (sizeof(uintptr_t) - 1)) & ~(sizeof(uintptr_t) - 1);
115 // TODO(ccross): we might need to consider a pointer to the end of a buffer
116 // to be inside the buffer, which means the common case of a pointer to the
117 // beginning of a buffer may keep two ranges live.
118 for (uintptr_t i = begin; i < range.end; i += sizeof(uintptr_t)) {
119 Range ref_range;
120 AllocationInfo* ref_info;
Colin Cross0965c022016-04-26 16:51:32 -0700121 if (WordContainsAllocationPtr(i, &ref_range, &ref_info)) {
Colin Crossd6b3a2a2016-03-02 17:53:39 -0800122 f(ref_range, ref_info);
123 }
124 }
125}
126
Colin Crossa83881e2017-06-22 10:50:05 -0700127template <class F>
Colin Crossd6b3a2a2016-03-02 17:53:39 -0800128inline void HeapWalker::ForEachAllocation(F&& f) {
129 for (auto& it : allocations_) {
130 const Range& range = it.first;
131 HeapWalker::AllocationInfo& allocation = it.second;
132 f(range, allocation);
133 }
134}
135
Colin Crossa9939e92017-06-21 13:13:00 -0700136} // namespace android
137
Colin Crossbcb4ed32016-01-14 15:35:40 -0800138#endif