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