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 : #include "syzygy/agent/asan/heaps/internal_heap.h"
16 :
17 : #include "syzygy/common/align.h"
18 :
19 : namespace agent {
20 : namespace asan {
21 : namespace heaps {
22 :
23 : namespace {
24 :
25 : struct InternalHeapEntry {
26 : uint32 size;
27 : // Actually of a size such that the whole InternalHeapAlloc is of size
28 : // |size|.
29 : uint8 body[1];
30 : };
31 :
32 : const size_t kBodyOffset = offsetof(InternalHeapEntry, body);
33 :
34 : } // namespace
35 :
36 : InternalHeap::InternalHeap(MemoryNotifierInterface* memory_notifier,
37 : HeapInterface* heap)
38 E : : memory_notifier_(memory_notifier), heap_(heap) {
39 E : DCHECK_NE(static_cast<MemoryNotifierInterface*>(NULL), memory_notifier);
40 E : DCHECK_NE(static_cast<HeapInterface*>(NULL), heap);
41 : notifying_heap_ =
42 E : heap_->GetHeapFeatures() & HeapInterface::kHeapReportsReservations;
43 E : }
44 :
45 E : HeapType InternalHeap::GetHeapType() const {
46 E : return heap_->GetHeapType();
47 E : }
48 :
49 i : uint32 InternalHeap::GetHeapFeatures() const {
50 : // Endow a wrapped heap with GetAllocationSize support.
51 : return heap_->GetHeapFeatures() | kHeapSupportsGetAllocationSize |
52 i : kHeapGetAllocationSizeIsUpperBound;
53 i : }
54 :
55 E : void* InternalHeap::Allocate(size_t bytes) {
56 E : size_t size = ::common::AlignUp(bytes + kBodyOffset, kShadowRatio);
57 E : void* alloc = heap_->Allocate(size);
58 E : if (alloc == NULL)
59 i : return NULL;
60 :
61 E : InternalHeapEntry* entry = reinterpret_cast<InternalHeapEntry*>(alloc);
62 E : entry->size = size;
63 E : memory_notifier_->NotifyInternalUse(entry, size);
64 :
65 E : return entry->body;
66 E : }
67 :
68 E : bool InternalHeap::Free(void* alloc) {
69 E : if (alloc != NULL) {
70 E : uint8* bytes = reinterpret_cast<uint8*>(alloc);
71 : InternalHeapEntry* entry = reinterpret_cast<InternalHeapEntry*>(
72 E : bytes - kBodyOffset);
73 E : if (notifying_heap_) {
74 : // A notifying heap redzones the memory from which allocations are made.
75 : // We return the redzone to its initial state.
76 E : memory_notifier_->NotifyFutureHeapUse(entry, entry->size);
77 E : } else {
78 : // A non-notifying heap serves memory from greenzoned pages, so indicate
79 : // the memory has returned to the OS.
80 E : memory_notifier_->NotifyReturnedToOS(entry, entry->size);
81 : }
82 :
83 : // Adjust the allocation pointer to that of the wrapped heap.
84 E : alloc = entry;
85 : }
86 :
87 E : return heap_->Free(alloc);
88 E : }
89 :
90 i : bool InternalHeap::IsAllocated(const void* alloc) {
91 i : if (alloc != NULL) {
92 i : const uint32* header = reinterpret_cast<const uint32*>(alloc) - 1;
93 i : alloc = header;
94 : }
95 i : return heap_->IsAllocated(alloc);
96 i : }
97 :
98 E : size_t InternalHeap::GetAllocationSize(const void* alloc) {
99 E : if (alloc == NULL)
100 i : return kUnknownSize;
101 :
102 E : const uint8* bytes = reinterpret_cast<const uint8*>(alloc);
103 : const InternalHeapEntry* entry =
104 E : reinterpret_cast<const InternalHeapEntry*>(bytes - kBodyOffset);
105 E : return entry->size;
106 E : }
107 :
108 E : void InternalHeap::Lock() {
109 E : heap_->Lock();
110 E : }
111 :
112 E : void InternalHeap::Unlock() {
113 E : heap_->Unlock();
114 E : }
115 :
116 E : bool InternalHeap::TryLock() {
117 E : return heap_->TryLock();
118 E : }
119 :
120 : } // namespace heaps
121 : } // namespace asan
122 : } // namespace agent
|