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/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/asan_runtime.h"
22 : #include "syzygy/agent/asan/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, CreateFailed) {
70 E : HANDLE heap = asan_HeapCreate(0, 0x80000000, 0x8000);
71 E : ASSERT_TRUE(heap == NULL);
72 E : }
73 :
74 E : TEST_F(AsanRtlImplTest, Alloc) {
75 E : for (size_t size = 10; size < kMaxAllocSize; size = size * 5 + 123) {
76 E : void* mem = asan_HeapAlloc(heap_, 0, size);
77 E : ASSERT_TRUE(mem != NULL);
78 E : memset(mem, '\0', size);
79 :
80 E : size_t new_size = size;
81 E : while (new_size == size)
82 E : new_size = base::RandInt(size / 2, size * 2);
83 :
84 E : void* new_mem = asan_HeapReAlloc(heap_, 0, mem, new_size);
85 E : ASSERT_TRUE(new_mem != NULL);
86 E : ASSERT_NE(mem, new_mem);
87 :
88 E : ASSERT_TRUE(asan_HeapFree(heap_, 0, new_mem));
89 E : }
90 E : }
91 :
92 E : TEST_F(AsanRtlImplTest, Size) {
93 E : for (size_t size = 10; size < kMaxAllocSize; size = size * 5 + 123) {
94 E : void* mem = asan_HeapAlloc(heap_, 0, size);
95 E : ASSERT_TRUE(mem != NULL);
96 E : ASSERT_EQ(size, asan_HeapSize(heap_, 0, mem));
97 E : ASSERT_TRUE(asan_HeapFree(heap_, 0, mem));
98 E : }
99 E : }
100 :
101 E : TEST_F(AsanRtlImplTest, Validate) {
102 E : for (size_t size = 10; size < kMaxAllocSize; size = size * 5 + 123) {
103 E : void* mem = asan_HeapAlloc(heap_, 0, size);
104 E : ASSERT_TRUE(mem != NULL);
105 E : ASSERT_TRUE(asan_HeapValidate(heap_, 0, mem));
106 E : ASSERT_TRUE(asan_HeapFree(heap_, 0, mem));
107 E : }
108 E : }
109 :
110 E : TEST_F(AsanRtlImplTest, Compact) {
111 : // Compact should return a non-zero size.
112 E : ASSERT_LT(0U, asan_HeapCompact(heap_, 0));
113 :
114 : // TODO(siggi): It may not be possible to allocate the size returned due
115 : // to padding - fix and test.
116 E : }
117 :
118 E : TEST_F(AsanRtlImplTest, LockUnlock) {
119 : // We can't really test these, aside from not crashing.
120 E : ASSERT_TRUE(asan_HeapLock(heap_));
121 E : ASSERT_TRUE(asan_HeapUnlock(heap_));
122 E : }
123 :
124 E : TEST_F(AsanRtlImplTest, Walk) {
125 : // We assume at least two entries to walk through.
126 E : PROCESS_HEAP_ENTRY entry = {};
127 E : ASSERT_TRUE(asan_HeapWalk(heap_, &entry));
128 E : ASSERT_TRUE(asan_HeapWalk(heap_, &entry));
129 E : }
130 :
131 E : TEST_F(AsanRtlImplTest, SetQueryInformation) {
132 E : ULONG compat_flag = -1;
133 E : unsigned long ret = 0;
134 : // Get the current value of the compatibility flag.
135 : ASSERT_TRUE(
136 : asan_HeapQueryInformation(heap_, HeapCompatibilityInformation,
137 E : &compat_flag, sizeof(compat_flag), &ret));
138 E : ASSERT_EQ(sizeof(compat_flag), ret);
139 E : ASSERT_TRUE(compat_flag != -1);
140 :
141 : // Put the heap in LFH, which should always succeed, except when a debugger
142 : // is attached. When a debugger is attached, the heap is wedged in certain
143 : // debug settings.
144 E : if (base::debug::BeingDebugged()) {
145 i : LOG(WARNING) << "Can't test HeapProxy::SetInformation under debugger.";
146 i : return;
147 : }
148 :
149 E : compat_flag = 2;
150 : ASSERT_TRUE(
151 : asan_HeapSetInformation(heap_, HeapCompatibilityInformation,
152 E : &compat_flag, sizeof(compat_flag)));
153 E : }
154 :
155 E : TEST_F(AsanRtlImplTest, SetInformationWithNullHeapPtr) {
156 : // The documentation of HeapSetInformation specify that the heap handle is
157 : // optional.
158 : ASSERT_TRUE(
159 : asan_HeapSetInformation(NULL, HeapEnableTerminationOnCorruption,
160 E : NULL, 0));
161 E : }
|