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/basictypes.h"
21 : #include "base/logging.h"
22 : #include "syzygy/agent/asan/heap.h"
23 :
24 : namespace agent {
25 : namespace asan {
26 :
27 : // A heap manager is responsible for creating and managing heaps. It also acts
28 : // as a proxy between the heap function interceptors and the underlying heaps.
29 : class HeapManagerInterface {
30 : public:
31 : typedef uintptr_t HeapId;
32 :
33 : // Virtual destructor.
34 E : virtual ~HeapManagerInterface() { }
35 :
36 : // Creates a new heap.
37 : // @returns the ID of the heap that has been created.
38 : virtual HeapId CreateHeap() = 0;
39 :
40 : // Destroy a heap.
41 : // @param heap The ID of the heap to destroy.
42 : // @returns true on success, false otherwise.
43 : virtual bool DestroyHeap(HeapId heap) = 0;
44 :
45 : // Do an allocation in a given heap.
46 : // @param heap The ID of the heap that should preferably be used for the
47 : // allocation. The implementation is free to use this heap or not.
48 : // @param bytes The requested size of the allocation, in bytes.
49 : // @returns A pointer to the new allocation on success, NULL otherwise.
50 : virtual void* Allocate(HeapId heap, size_t bytes) = 0;
51 :
52 : // Free a given heap allocation.
53 : // @param heap A hint on the heap that might contain this allocation.
54 : // @param alloc The pointer to the allocation to be freed. This must be a
55 : // value that was previously returned by a call to 'Allocate'.
56 : // @returns true on failure, false otherwise.
57 : virtual bool Free(HeapId heap, void* alloc) = 0;
58 :
59 : // Returns the size of a heap allocation.
60 : // @param heap A hint on the heap that might contain this allocation.
61 : // @param alloc The pointer to the allocation whose size is to be calculated.
62 : // This must be a value that was previously returned by a call to
63 : // 'Allocate'.
64 : // @returns the size of the block on success, 0 otherwise.
65 : virtual size_t Size(HeapId heap, const void* alloc) = 0;
66 :
67 : // Locks a heap.
68 : // @param heap The ID of the heap that should be locked.
69 : virtual void Lock(HeapId heap) = 0;
70 :
71 : // Unlocks a heap.
72 : // @param heap The ID of the heap that should be unlocked.
73 : virtual void Unlock(HeapId heap) = 0;
74 :
75 : // Makes a best effort to lock the heap manager and all heaps internal
76 : // to it. This must actually lock the heap manager, and prevent new heaps
77 : // from beging created while this lock is created. It must make a best effort
78 : // to lock all the heaps under this heap manager's control. This lock will
79 : // be taken when a crash is being processed, thus can afford to be somewhat
80 : // expensive. This lock is not reentrant.
81 : virtual void BestEffortLockAll() = 0;
82 :
83 : // Unlocks all locks acquired in a previous call to BestEffortLockAll.
84 : virtual void UnlockAll() = 0;
85 : };
86 :
87 :
88 : // A utility class for locking a heap manager within a scope.
89 : class AutoHeapManagerLock {
90 : public:
91 E : explicit AutoHeapManagerLock(HeapManagerInterface* heap_manager)
92 : : heap_manager_(heap_manager) {
93 E : DCHECK_NE(static_cast<HeapManagerInterface*>(nullptr), heap_manager);
94 E : heap_manager_->BestEffortLockAll();
95 E : }
96 :
97 E : ~AutoHeapManagerLock() {
98 E : DCHECK_NE(static_cast<HeapManagerInterface*>(nullptr), heap_manager_);
99 E : heap_manager_->UnlockAll();
100 E : }
101 :
102 : private:
103 : HeapManagerInterface* heap_manager_;
104 :
105 : DISALLOW_COPY_AND_ASSIGN(AutoHeapManagerLock);
106 : };
107 :
108 : } // namespace asan
109 : } // namespace agent
110 :
111 : #endif // SYZYGY_AGENT_ASAN_HEAP_MANAGER_H_
|