1 : // Copyright 2014 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Declares a minimal heap manager interface.
16 :
17 : #ifndef SYZYGY_AGENT_ASAN_HEAP_MANAGER_H_
18 : #define SYZYGY_AGENT_ASAN_HEAP_MANAGER_H_
19 :
20 : #include "base/logging.h"
21 : #include "syzygy/agent/asan/heap.h"
22 :
23 : namespace agent {
24 : namespace asan {
25 :
26 : // A heap manager is responsible for creating and managing heaps. It also acts
27 : // as a proxy between the heap function interceptors and the underlying heaps.
28 : class HeapManagerInterface {
29 : public:
30 : typedef uintptr_t HeapId;
31 :
32 : // Virtual destructor.
33 E : virtual ~HeapManagerInterface() { }
34 :
35 : // Creates a new heap.
36 : // @returns the ID of the heap that has been created.
37 : virtual HeapId CreateHeap() = 0;
38 :
39 : // Destroy a heap.
40 : // @param heap The ID of the heap to destroy.
41 : // @returns true on success, false otherwise.
42 : virtual bool DestroyHeap(HeapId heap) = 0;
43 :
44 : // Do an allocation in a given heap.
45 : // @param heap The ID of the heap that should preferably be used for the
46 : // allocation. The implementation is free to use this heap or not.
47 : // @param bytes The requested size of the allocation, in bytes.
48 : // @returns A pointer to the new allocation on success, NULL otherwise.
49 : virtual void* Allocate(HeapId heap, uint32_t bytes) = 0;
50 :
51 : // Free a given heap allocation.
52 : // @param heap A hint on the heap that might contain this allocation.
53 : // @param alloc The pointer to the allocation to be freed. This must be a
54 : // value that was previously returned by a call to 'Allocate'.
55 : // @returns true on failure, false otherwise.
56 : virtual bool Free(HeapId heap, void* alloc) = 0;
57 :
58 : // Returns the size of a heap allocation.
59 : // @param heap A hint on the heap that might contain this allocation.
60 : // @param alloc The pointer to the allocation whose size is to be calculated.
61 : // This must be a value that was previously returned by a call to
62 : // 'Allocate'.
63 : // @returns the size of the block on success, 0 otherwise.
64 : virtual uint32_t Size(HeapId heap, const void* alloc) = 0;
65 :
66 : // Locks a heap.
67 : // @param heap The ID of the heap that should be locked.
68 : virtual void Lock(HeapId heap) = 0;
69 :
70 : // Unlocks a heap.
71 : // @param heap The ID of the heap that should be unlocked.
72 : virtual void Unlock(HeapId heap) = 0;
73 :
74 : // Makes a best effort to lock the heap manager and all heaps internal
75 : // to it. This must actually lock the heap manager, and prevent new heaps
76 : // from beging created while this lock is created. It must make a best effort
77 : // to lock all the heaps under this heap manager's control. This lock will
78 : // be taken when a crash is being processed, thus can afford to be somewhat
79 : // expensive. This lock is not reentrant.
80 : virtual void BestEffortLockAll() = 0;
81 :
82 : // Unlocks all locks acquired in a previous call to BestEffortLockAll.
83 : virtual void UnlockAll() = 0;
84 : };
85 :
86 :
87 : // A utility class for locking a heap manager within a scope.
88 : class AutoHeapManagerLock {
89 : public:
90 E : explicit AutoHeapManagerLock(HeapManagerInterface* heap_manager)
91 E : : heap_manager_(heap_manager) {
92 E : DCHECK_NE(static_cast<HeapManagerInterface*>(nullptr), heap_manager);
93 E : heap_manager_->BestEffortLockAll();
94 E : }
95 :
96 E : ~AutoHeapManagerLock() {
97 E : DCHECK_NE(static_cast<HeapManagerInterface*>(nullptr), heap_manager_);
98 E : heap_manager_->UnlockAll();
99 E : }
100 :
101 : private:
102 : HeapManagerInterface* heap_manager_;
103 :
104 : DISALLOW_COPY_AND_ASSIGN(AutoHeapManagerLock);
105 : };
106 :
107 : } // namespace asan
108 : } // namespace agent
109 :
110 : #endif // SYZYGY_AGENT_ASAN_HEAP_MANAGER_H_
|