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 : // Defines InternalHeap, a simple wrapper of any other HeapInterface that
16 : // adds internal-use notifications via a MemoryNotifierInterface.
17 :
18 : #ifndef SYZYGY_AGENT_ASAN_HEAPS_INTERNAL_HEAP_H_
19 : #define SYZYGY_AGENT_ASAN_HEAPS_INTERNAL_HEAP_H_
20 :
21 : #include "base/logging.h"
22 : #include "syzygy/agent/asan/heap.h"
23 : #include "syzygy/agent/asan/memory_notifier.h"
24 :
25 : namespace agent {
26 : namespace asan {
27 : namespace heaps {
28 :
29 : // An implementation of HeapInterface that wraps another HeapInterface and
30 : // a MemoryNotificationInterface. It subsequently will notify all allocations
31 : // as being for internal use. This incurs a small amount of memory overhead
32 : // per allocation to store the original size of the allocation. This heap
33 : // does *not* return allocations that are kShadowRatio aligned. Rather, it
34 : // returns allocations that sizeof(uint32_t) % kShadowRatio aligned, due to the
35 : // extra incurred header. This is not an issue as the allocations are only
36 : // for internal use and no shadow memory notations will be applied to them.
37 : class InternalHeap : public HeapInterface {
38 : public:
39 : // Constructor.
40 : // @param memory_notifier The notifier that will be used to inform the
41 : // runtime of all allocations.
42 : // @param heap The underlying heap that is being wrapped.
43 : InternalHeap(MemoryNotifierInterface* memory_notifier,
44 : HeapInterface* heap);
45 :
46 : // Destructor.
47 E : virtual ~InternalHeap() { }
48 :
49 : // @name HeapInterface functions.
50 : // @{
51 : virtual HeapType GetHeapType() const;
52 : virtual uint32_t GetHeapFeatures() const;
53 : virtual void* Allocate(uint32_t bytes);
54 : virtual bool Free(void* alloc);
55 : virtual bool IsAllocated(const void* alloc);
56 : virtual uint32_t GetAllocationSize(const void* alloc);
57 : virtual void Lock();
58 : virtual void Unlock();
59 : virtual bool TryLock();
60 : // @}
61 :
62 : protected:
63 : // The interface that will be notified of all memory use. Has its own
64 : // locking.
65 : MemoryNotifierInterface* memory_notifier_;
66 :
67 : // The underlying heap interface. Provides locking for us.
68 : HeapInterface* heap_;
69 :
70 : // This is true if the wrapped heap is a notifying heap.
71 : bool notifying_heap_;
72 :
73 : private:
74 : DISALLOW_COPY_AND_ASSIGN(InternalHeap);
75 : };
76 :
77 : } // namespace heaps
78 : } // namespace asan
79 : } // namespace agent
80 :
81 : #endif // SYZYGY_AGENT_ASAN_HEAPS_INTERNAL_HEAP_H_
|