blob: 140f3eaa07f09fa135753462d94db7918d966af9 [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 Crossbcb4ed32016-01-14 15:35:40 -080034};
35
36// Comparator for Ranges that returns equivalence for overlapping ranges
37struct compare_range {
38 bool operator()(const Range& a, const Range& b) const {
39 return a.end <= b.begin;
40 }
41};
42
Colin Crossbcb4ed32016-01-14 15:35:40 -080043class HeapWalker {
44 public:
45 HeapWalker(Allocator<HeapWalker> allocator) : allocator_(allocator),
46 allocations_(allocator), allocation_bytes_(0),
Colin Cross0965c022016-04-26 16:51:32 -070047 roots_(allocator), root_vals_(allocator),
48 segv_handler_(allocator), walking_ptr_(0) {
Colin Crossbcb4ed32016-01-14 15:35:40 -080049 valid_allocations_range_.end = 0;
50 valid_allocations_range_.begin = ~valid_allocations_range_.end;
Colin Cross0965c022016-04-26 16:51:32 -070051
52 segv_handler_.install(SIGSEGV,
53 [=](ScopedSignalHandler& handler, int signal, siginfo_t* siginfo, void* uctx) {
54 this->HandleSegFault(handler, signal, siginfo, uctx);
55 });
Colin Crossbcb4ed32016-01-14 15:35:40 -080056 }
Colin Cross0965c022016-04-26 16:51:32 -070057
Colin Crossbcb4ed32016-01-14 15:35:40 -080058 ~HeapWalker() {}
59 bool Allocation(uintptr_t begin, uintptr_t end);
60 void Root(uintptr_t begin, uintptr_t end);
61 void Root(const allocator::vector<uintptr_t>& vals);
62
63 bool DetectLeaks();
64
65 bool Leaked(allocator::vector<Range>&, size_t limit, size_t* num_leaks,
66 size_t* leak_bytes);
67 size_t Allocations();
68 size_t AllocationBytes();
69
Colin Crossd6b3a2a2016-03-02 17:53:39 -080070 template<class F>
71 void ForEachPtrInRange(const Range& range, F&& f);
72
73 template<class F>
74 void ForEachAllocation(F&& f);
75
76 struct AllocationInfo {
Colin Crossbcb4ed32016-01-14 15:35:40 -080077 bool referenced_from_root;
Colin Crossbcb4ed32016-01-14 15:35:40 -080078 };
Colin Crossd6b3a2a2016-03-02 17:53:39 -080079
80 private:
81
82 void RecurseRoot(const Range& root);
Colin Cross0965c022016-04-26 16:51:32 -070083 bool WordContainsAllocationPtr(uintptr_t ptr, Range* range, AllocationInfo** info);
84 void HandleSegFault(ScopedSignalHandler&, int, siginfo_t*, void*);
Colin Crossd6b3a2a2016-03-02 17:53:39 -080085
Colin Crossbcb4ed32016-01-14 15:35:40 -080086 DISALLOW_COPY_AND_ASSIGN(HeapWalker);
87 Allocator<HeapWalker> allocator_;
Colin Crosse4cbe0e2016-03-04 16:34:42 -080088 using AllocationMap = allocator::map<Range, AllocationInfo, compare_range>;
Colin Crossd6b3a2a2016-03-02 17:53:39 -080089 AllocationMap allocations_;
Colin Crossbcb4ed32016-01-14 15:35:40 -080090 size_t allocation_bytes_;
91 Range valid_allocations_range_;
92
93 allocator::vector<Range> roots_;
94 allocator::vector<uintptr_t> root_vals_;
Colin Cross0965c022016-04-26 16:51:32 -070095
96 ScopedSignalHandler segv_handler_;
97 uintptr_t walking_ptr_;
Colin Crossbcb4ed32016-01-14 15:35:40 -080098};
99
Colin Crossd6b3a2a2016-03-02 17:53:39 -0800100template<class F>
101inline void HeapWalker::ForEachPtrInRange(const Range& range, F&& f) {
102 uintptr_t begin = (range.begin + (sizeof(uintptr_t) - 1)) & ~(sizeof(uintptr_t) - 1);
103 // TODO(ccross): we might need to consider a pointer to the end of a buffer
104 // to be inside the buffer, which means the common case of a pointer to the
105 // beginning of a buffer may keep two ranges live.
106 for (uintptr_t i = begin; i < range.end; i += sizeof(uintptr_t)) {
107 Range ref_range;
108 AllocationInfo* ref_info;
Colin Cross0965c022016-04-26 16:51:32 -0700109 if (WordContainsAllocationPtr(i, &ref_range, &ref_info)) {
Colin Crossd6b3a2a2016-03-02 17:53:39 -0800110 f(ref_range, ref_info);
111 }
112 }
113}
114
115template<class F>
116inline void HeapWalker::ForEachAllocation(F&& f) {
117 for (auto& it : allocations_) {
118 const Range& range = it.first;
119 HeapWalker::AllocationInfo& allocation = it.second;
120 f(range, allocation);
121 }
122}
123
Colin Crossbcb4ed32016-01-14 15:35:40 -0800124#endif