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 : };
31 :
32 : } // namespace
33 :
34 E : TEST(ShadowTest, PoisonUnpoisonAccess) {
35 : // Reset the shadow memory.
36 E : TestShadow::Reset();
37 E : for (size_t i = 0; i < 100; ++i) {
38 : // Use a random 8-byte aligned end address.
39 E : const size_t size = base::RandInt(1, 16384);
40 : const uint8* end_addr =
41 E : reinterpret_cast<const uint8*>(base::RandInt(65536, 10*1024*1024) * 8);
42 E : const uint8* start_addr = end_addr - size;
43 :
44 E : for (size_t i = 0; i < size; ++i) {
45 E : EXPECT_TRUE(Shadow::IsAccessible(start_addr + i));
46 E : }
47 :
48 E : Shadow::Poison(start_addr, size, Shadow::kHeapNonAccessibleByteMask);
49 E : for (size_t i = 0; i < size; ++i) {
50 E : EXPECT_FALSE(Shadow::IsAccessible(start_addr + i));
51 E : }
52 E : EXPECT_TRUE(Shadow::IsAccessible(start_addr - 1));
53 E : EXPECT_TRUE(Shadow::IsAccessible(start_addr + size));
54 :
55 E : const size_t aligned_size = common::AlignUp(size, 8);
56 E : const uint8* aligned_start_addr = end_addr - aligned_size;
57 E : Shadow::Unpoison(aligned_start_addr, aligned_size);
58 E : for (size_t i = 0; i < size; ++i) {
59 E : EXPECT_TRUE(Shadow::IsAccessible(start_addr + i));
60 E : }
61 E : }
62 E : }
63 :
64 : } // namespace asan
65 : } // namespace agent
|