1 : // Copyright 2012 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/rtl_impl.h"
16 :
17 : #include <windows.h> // NOLINT
18 :
19 : #include "base/rand_util.h"
20 : #include "gtest/gtest.h"
21 : #include "syzygy/agent/asan/runtime.h"
22 : #include "syzygy/agent/asan/shadow.h"
23 : #include "syzygy/agent/asan/unittest_util.h"
24 : #include "syzygy/core/unittest_util.h"
25 :
26 : namespace {
27 :
28 : class AsanRtlImplTest : public testing::TestWithAsanLogger {
29 : public:
30 E : AsanRtlImplTest() : heap_(NULL) {
31 E : }
32 :
33 E : void SetUp() override {
34 E : testing::TestWithAsanLogger::SetUp();
35 E : asan_runtime_.SetUp(std::wstring());
36 E : agent::asan::SetUpRtl(&asan_runtime_);
37 E : heap_ = asan_HeapCreate(0, 0, 0);
38 E : ASSERT_TRUE(heap_ != NULL);
39 E : }
40 :
41 E : void TearDown() override {
42 E : if (heap_ != NULL) {
43 E : asan_HeapDestroy(heap_);
44 E : heap_ = NULL;
45 : }
46 E : agent::asan::TearDownRtl();
47 E : asan_runtime_.TearDown();
48 E : testing::TestWithAsanLogger::TearDown();
49 E : }
50 :
51 : protected:
52 : agent::asan::AsanRuntime asan_runtime_;
53 :
54 : // Arbitrary constant for all size limit.
55 : static const size_t kMaxAllocSize = 134584;
56 :
57 : // Scratch heap handle valid from SetUp to TearDown.
58 : HANDLE heap_;
59 : };
60 :
61 : } // namespace
62 :
63 E : TEST_F(AsanRtlImplTest, CreateDestroy) {
64 E : HANDLE heap = asan_HeapCreate(0, 0, 0);
65 E : ASSERT_TRUE(heap != NULL);
66 E : ASSERT_TRUE(asan_HeapDestroy(heap));
67 E : }
68 :
69 E : TEST_F(AsanRtlImplTest, Alloc) {
70 E : for (size_t size = 10; size < kMaxAllocSize; size = size * 5 + 123) {
71 E : void* mem = asan_HeapAlloc(heap_, 0, size);
72 E : ASSERT_TRUE(mem != NULL);
73 E : ::memset(mem, '\0', size);
74 :
75 E : size_t new_size = size;
76 E : while (new_size == size)
77 E : new_size = base::RandInt(size / 2, size * 2);
78 :
79 E : void* new_mem = asan_HeapReAlloc(heap_, 0, mem, new_size);
80 E : ASSERT_TRUE(new_mem != NULL);
81 E : ASSERT_NE(mem, new_mem);
82 :
83 E : ASSERT_TRUE(asan_HeapFree(heap_, 0, new_mem));
84 E : }
85 E : }
86 :
87 E : TEST_F(AsanRtlImplTest, Size) {
88 E : for (size_t size = 10; size < kMaxAllocSize; size = size * 5 + 123) {
89 E : void* mem = asan_HeapAlloc(heap_, 0, size);
90 E : ASSERT_TRUE(mem != NULL);
91 E : ASSERT_EQ(size, asan_HeapSize(heap_, 0, mem));
92 E : ASSERT_TRUE(asan_HeapFree(heap_, 0, mem));
93 E : }
94 E : }
95 :
96 E : TEST_F(AsanRtlImplTest, Validate) {
97 E : for (size_t size = 10; size < kMaxAllocSize; size = size * 5 + 123) {
98 E : void* mem = asan_HeapAlloc(heap_, 0, size);
99 E : ASSERT_TRUE(mem != NULL);
100 E : ASSERT_TRUE(asan_HeapValidate(heap_, 0, mem));
101 E : ASSERT_TRUE(asan_HeapFree(heap_, 0, mem));
102 E : }
103 E : }
104 :
105 E : TEST_F(AsanRtlImplTest, Compact) {
106 : // Compact isn't supported by the current heap implementation and should
107 : // always return 0.
108 E : ASSERT_EQ(0U, asan_HeapCompact(heap_, 0));
109 E : }
110 :
111 E : TEST_F(AsanRtlImplTest, LockUnlock) {
112 : // We can't really test these, aside from not crashing.
113 E : ASSERT_TRUE(asan_HeapLock(heap_));
114 E : ASSERT_TRUE(asan_HeapUnlock(heap_));
115 E : }
116 :
117 E : TEST_F(AsanRtlImplTest, Walk) {
118 : // Walk isn't supported by the current heap implementation.
119 E : PROCESS_HEAP_ENTRY entry = {};
120 E : ASSERT_FALSE(asan_HeapWalk(heap_, &entry));
121 E : }
122 :
123 E : TEST_F(AsanRtlImplTest, SetQueryInformation) {
124 E : ULONG compat_flag = ULONG_MAX;
125 E : unsigned long ret = 0;
126 : // QueryInformation isn't supported by the current heap implementation and
127 : // should always return false.
128 : ASSERT_FALSE(
129 : asan_HeapQueryInformation(heap_, HeapCompatibilityInformation,
130 E : &compat_flag, sizeof(compat_flag), &ret));
131 :
132 : // Put the heap in LFH, which should always succeed, except when a debugger
133 : // is attached. When a debugger is attached, the heap is wedged in certain
134 : // debug settings.
135 E : if (base::debug::BeingDebugged()) {
136 i : LOG(WARNING) << "Can't test HeapProxy::SetInformation under debugger.";
137 i : return;
138 : }
139 :
140 : // SetInformation isn't supported by the current heap implementation and
141 : // should always return true.
142 : ASSERT_TRUE(
143 : asan_HeapSetInformation(heap_, HeapCompatibilityInformation,
144 E : &compat_flag, sizeof(compat_flag)));
145 E : }
146 :
147 E : TEST_F(AsanRtlImplTest, SetInformationWithNullHeapPtr) {
148 : // The documentation of HeapSetInformation specify that the heap handle is
149 : // optional.
150 : ASSERT_TRUE(
151 : asan_HeapSetInformation(NULL, HeapEnableTerminationOnCorruption,
152 E : NULL, 0));
153 E : }
|