blob: 865965a413a1e4dcf8cf6fc6105228089e153858 [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
28// A range [begin, end)
29struct Range {
30 uintptr_t begin;
31 uintptr_t end;
Colin Crossd6b3a2a2016-03-02 17:53:39 -080032
33 size_t size() const { return end - begin; };
Colin Cross583a2502016-04-26 17:10:04 -070034 bool operator==(const Range& other) const {
35 return this->begin == other.begin && this->end == other.end;
36 }
Colin Crossa83881e2017-06-22 10:50:05 -070037 bool operator!=(const Range& other) const { return !(*this == other); }
Colin Crossbcb4ed32016-01-14 15:35:40 -080038};
39
40// Comparator for Ranges that returns equivalence for overlapping ranges
41struct compare_range {
Colin Crossa83881e2017-06-22 10:50:05 -070042 bool operator()(const Range& a, const Range& b) const { return a.end <= b.begin; }
Colin Crossbcb4ed32016-01-14 15:35:40 -080043};
44
Colin Crossbcb4ed32016-01-14 15:35:40 -080045class HeapWalker {
46 public:
Colin Crossa83881e2017-06-22 10:50:05 -070047 explicit HeapWalker(Allocator<HeapWalker> allocator)
48 : allocator_(allocator),
49 allocations_(allocator),
50 allocation_bytes_(0),
51 roots_(allocator),
52 root_vals_(allocator),
53 segv_handler_(allocator),
54 walking_ptr_(0) {
Colin Crossbcb4ed32016-01-14 15:35:40 -080055 valid_allocations_range_.end = 0;
56 valid_allocations_range_.begin = ~valid_allocations_range_.end;
Colin Cross0965c022016-04-26 16:51:32 -070057
Colin Crossa83881e2017-06-22 10:50:05 -070058 segv_handler_.install(
59 SIGSEGV, [=](ScopedSignalHandler& handler, int signal, siginfo_t* siginfo, void* uctx) {
Colin Cross0965c022016-04-26 16:51:32 -070060 this->HandleSegFault(handler, signal, siginfo, uctx);
Colin Crossa83881e2017-06-22 10:50:05 -070061 });
Colin Crossbcb4ed32016-01-14 15:35:40 -080062 }
Colin Cross0965c022016-04-26 16:51:32 -070063
Colin Crossbcb4ed32016-01-14 15:35:40 -080064 ~HeapWalker() {}
65 bool Allocation(uintptr_t begin, uintptr_t end);
66 void Root(uintptr_t begin, uintptr_t end);
67 void Root(const allocator::vector<uintptr_t>& vals);
68
69 bool DetectLeaks();
70
Colin Crossa83881e2017-06-22 10:50:05 -070071 bool Leaked(allocator::vector<Range>&, size_t limit, size_t* num_leaks, size_t* leak_bytes);
Colin Crossbcb4ed32016-01-14 15:35:40 -080072 size_t Allocations();
73 size_t AllocationBytes();
74
Colin Crossa83881e2017-06-22 10:50:05 -070075 template <class F>
Colin Crossd6b3a2a2016-03-02 17:53:39 -080076 void ForEachPtrInRange(const Range& range, F&& f);
77
Colin Crossa83881e2017-06-22 10:50:05 -070078 template <class F>
Colin Crossd6b3a2a2016-03-02 17:53:39 -080079 void ForEachAllocation(F&& f);
80
81 struct AllocationInfo {
Colin Crossbcb4ed32016-01-14 15:35:40 -080082 bool referenced_from_root;
Colin Crossbcb4ed32016-01-14 15:35:40 -080083 };
Colin Crossd6b3a2a2016-03-02 17:53:39 -080084
85 private:
Colin Crossd6b3a2a2016-03-02 17:53:39 -080086 void RecurseRoot(const Range& root);
Colin Cross0965c022016-04-26 16:51:32 -070087 bool WordContainsAllocationPtr(uintptr_t ptr, Range* range, AllocationInfo** info);
88 void HandleSegFault(ScopedSignalHandler&, int, siginfo_t*, void*);
Colin Crossd6b3a2a2016-03-02 17:53:39 -080089
Colin Crossbcb4ed32016-01-14 15:35:40 -080090 DISALLOW_COPY_AND_ASSIGN(HeapWalker);
91 Allocator<HeapWalker> allocator_;
Colin Crosse4cbe0e2016-03-04 16:34:42 -080092 using AllocationMap = allocator::map<Range, AllocationInfo, compare_range>;
Colin Crossd6b3a2a2016-03-02 17:53:39 -080093 AllocationMap allocations_;
Colin Crossbcb4ed32016-01-14 15:35:40 -080094 size_t allocation_bytes_;
95 Range valid_allocations_range_;
96
97 allocator::vector<Range> roots_;
98 allocator::vector<uintptr_t> root_vals_;
Colin Cross0965c022016-04-26 16:51:32 -070099
100 ScopedSignalHandler segv_handler_;
101 uintptr_t walking_ptr_;
Colin Crossbcb4ed32016-01-14 15:35:40 -0800102};
103
Colin Crossa83881e2017-06-22 10:50:05 -0700104template <class F>
Colin Crossd6b3a2a2016-03-02 17:53:39 -0800105inline void HeapWalker::ForEachPtrInRange(const Range& range, F&& f) {
106 uintptr_t begin = (range.begin + (sizeof(uintptr_t) - 1)) & ~(sizeof(uintptr_t) - 1);
107 // TODO(ccross): we might need to consider a pointer to the end of a buffer
108 // to be inside the buffer, which means the common case of a pointer to the
109 // beginning of a buffer may keep two ranges live.
110 for (uintptr_t i = begin; i < range.end; i += sizeof(uintptr_t)) {
111 Range ref_range;
112 AllocationInfo* ref_info;
Colin Cross0965c022016-04-26 16:51:32 -0700113 if (WordContainsAllocationPtr(i, &ref_range, &ref_info)) {
Colin Crossd6b3a2a2016-03-02 17:53:39 -0800114 f(ref_range, ref_info);
115 }
116 }
117}
118
Colin Crossa83881e2017-06-22 10:50:05 -0700119template <class F>
Colin Crossd6b3a2a2016-03-02 17:53:39 -0800120inline void HeapWalker::ForEachAllocation(F&& f) {
121 for (auto& it : allocations_) {
122 const Range& range = it.first;
123 HeapWalker::AllocationInfo& allocation = it.second;
124 f(range, allocation);
125 }
126}
127
Colin Crossbcb4ed32016-01-14 15:35:40 -0800128#endif