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 i = 0; i < 100; ++i) {
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 E : const size_t aligned_size = common::AlignUp(size, 8);
57 E : const uint8* aligned_start_addr = end_addr - aligned_size;
58 E : Shadow::Unpoison(aligned_start_addr, aligned_size);
59 E : for (size_t i = 0; i < size; ++i) {
60 E : EXPECT_TRUE(Shadow::IsAccessible(start_addr + i));
61 E : }
62 E : }
63 E : }
64 :
65 E : TEST(ShadowTest, SetUpAndTearDown) {
66 : // Reset the shadow memory.
67 E : TestShadow::Reset();
68 :
69 : // Don't check all the shadow bytes otherwise this test will take too much
70 : // time.
71 E : const size_t kShadowLookupInterval = 25;
72 :
73 E : intptr_t shadow_array_start = reinterpret_cast<intptr_t>(TestShadow::shadow_);
74 E : size_t shadow_start = shadow_array_start >> 3;
75 E : size_t shadow_end = shadow_start + (TestShadow::kShadowSize >> 3);
76 :
77 E : Shadow::SetUp();
78 E : for (size_t i = shadow_start; i < shadow_end; i += kShadowLookupInterval) {
79 E : ASSERT_EQ(Shadow::kAsanMemoryByte, TestShadow::shadow_[i]);
80 E : }
81 E : Shadow::TearDown();
82 E : for (size_t i = shadow_start; i < shadow_end; i += kShadowLookupInterval) {
83 E : ASSERT_EQ(Shadow::kHeapAddressableByte, TestShadow::shadow_[i]);
84 E : }
85 E : }
86 :
87 : } // namespace asan
88 : } // namespace agent
|