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_shadow.h"
16 :
17 : #include "base/rand_util.h"
18 : #include "gtest/gtest.h"
19 : #include "syzygy/common/align.h"
20 :
21 : namespace agent {
22 : namespace asan {
23 :
24 : namespace {
25 :
26 : // A derived class to expose protected members for unit-testing.
27 : class TestShadow : public Shadow {
28 : public:
29 : using Shadow::Reset;
30 : using Shadow::kShadowSize;
31 : using Shadow::shadow_;
32 : };
33 :
34 : } // namespace
35 :
36 E : TEST(ShadowTest, PoisonUnpoisonAccess) {
37 : // Reset the shadow memory.
38 E : TestShadow::Reset();
39 E : for (size_t count = 0; count < 100; ++count) {
40 : // Use a random 8-byte aligned end address.
41 E : const size_t size = base::RandInt(1, 16384);
42 : const uint8* end_addr =
43 E : reinterpret_cast<const uint8*>(base::RandInt(65536, 10*1024*1024) * 8);
44 E : const uint8* start_addr = end_addr - size;
45 :
46 E : for (size_t i = 0; i < size; ++i) {
47 E : EXPECT_TRUE(Shadow::IsAccessible(start_addr + i));
48 E : }
49 :
50 E : Shadow::Poison(start_addr, size, Shadow::kHeapNonAccessibleByteMask);
51 E : for (size_t i = 0; i < size; ++i)
52 E : EXPECT_FALSE(Shadow::IsAccessible(start_addr + i));
53 E : EXPECT_TRUE(Shadow::IsAccessible(start_addr - 1));
54 E : EXPECT_TRUE(Shadow::IsAccessible(start_addr + size));
55 :
56 : const size_t aligned_size = common::AlignUp(size,
57 E : Shadow::kShadowGranularity);
58 E : const uint8* aligned_start_addr = end_addr - aligned_size;
59 E : Shadow::Unpoison(aligned_start_addr, aligned_size);
60 E : for (size_t i = 0; i < size; ++i) {
61 E : EXPECT_TRUE(Shadow::IsAccessible(start_addr + i));
62 E : }
63 E : }
64 E : }
65 :
66 E : TEST(ShadowTest, SetUpAndTearDown) {
67 : // Reset the shadow memory.
68 E : TestShadow::Reset();
69 :
70 : // Don't check all the shadow bytes otherwise this test will take too much
71 : // time.
72 E : const size_t kLookupInterval = 25;
73 :
74 E : intptr_t shadow_array_start = reinterpret_cast<intptr_t>(TestShadow::shadow_);
75 E : size_t shadow_start = shadow_array_start >> 3;
76 E : size_t shadow_end = shadow_start + (TestShadow::kShadowSize >> 3);
77 :
78 E : const size_t non_addressable_memory_end = (0x10000 >> 3);
79 :
80 E : Shadow::SetUp();
81 E : for (size_t i = shadow_start; i < shadow_end; i += kLookupInterval) {
82 E : ASSERT_EQ(Shadow::kAsanMemoryByte, TestShadow::shadow_[i]);
83 E : }
84 E : for (size_t i = 0; i < non_addressable_memory_end; i += kLookupInterval) {
85 E : ASSERT_EQ(Shadow::kInvalidAddress, TestShadow::shadow_[i]);
86 E : }
87 E : Shadow::TearDown();
88 E : for (size_t i = shadow_start; i < shadow_end; i += kLookupInterval) {
89 E : ASSERT_EQ(Shadow::kHeapAddressableByte, TestShadow::shadow_[i]);
90 E : }
91 E : for (size_t i = 0; i < non_addressable_memory_end; i += kLookupInterval) {
92 E : ASSERT_EQ(Shadow::kHeapAddressableByte, TestShadow::shadow_[i]);
93 E : }
94 E : }
95 :
96 E : TEST(ShadowTest, GetNullTerminatedArraySize) {
97 : // Reset the shadow memory.
98 E : TestShadow::Reset();
99 E : const size_t kArrayLength = 100;
100 E : const uint8 kMarkerValue = 0xAA;
101 :
102 : uint8 test_array[kArrayLength];
103 : uint8* aligned_test_array = reinterpret_cast<uint8*>(
104 : common::AlignUp(reinterpret_cast<size_t>(test_array),
105 E : Shadow::kShadowGranularity));
106 : size_t aligned_array_length = common::AlignDown(kArrayLength -
107 E : (aligned_test_array - test_array), Shadow::kShadowGranularity);
108 :
109 E : ::memset(aligned_test_array, kMarkerValue, aligned_array_length);
110 : Shadow::Poison(aligned_test_array, aligned_array_length,
111 E : Shadow::kHeapNonAccessibleByteMask);
112 :
113 E : size_t sizes_to_test[] = { 4, 7, 11, 15, 21, 87 };
114 :
115 E : for (size_t i = 0; i < arraysize(sizes_to_test); ++i) {
116 E : Shadow::Unpoison(aligned_test_array, sizes_to_test[i]);
117 E : aligned_test_array[sizes_to_test[i] - 1] = 0;
118 E : size_t size = 0;
119 : EXPECT_TRUE(Shadow::GetNullTerminatedArraySize(aligned_test_array,
120 : &size,
121 E : 0U));
122 E : EXPECT_EQ(sizes_to_test[i], size);
123 E : aligned_test_array[sizes_to_test[i] - 1] = kMarkerValue;
124 :
125 E : aligned_test_array[sizes_to_test[i]] = kMarkerValue;
126 : EXPECT_FALSE(Shadow::GetNullTerminatedArraySize(aligned_test_array,
127 : &size,
128 E : 0U));
129 E : EXPECT_EQ(sizes_to_test[i], size);
130 : EXPECT_TRUE(Shadow::GetNullTerminatedArraySize(aligned_test_array,
131 : &size,
132 E : sizes_to_test[i]));
133 :
134 : Shadow::Poison(aligned_test_array, common::AlignUp(sizes_to_test[i],
135 E : Shadow::kShadowGranularity), Shadow::kHeapNonAccessibleByteMask);
136 E : }
137 E : Shadow::Unpoison(aligned_test_array, aligned_array_length);
138 E : }
139 :
140 : } // namespace asan
141 : } // namespace agent
|