1 : // Copyright 2014 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/block_utils.h"
16 :
17 : #include "gtest/gtest.h"
18 : #include "syzygy/agent/asan/asan_logger.h"
19 : #include "syzygy/agent/asan/unittest_util.h"
20 :
21 : namespace agent {
22 : namespace asan {
23 :
24 : namespace {
25 :
26 : typedef testing::TestWithAsanRuntime BlockUtilTest;
27 :
28 : } // namespace
29 :
30 E : TEST_F(BlockUtilTest, IsBlockCorruptInvalidMagicNumber) {
31 E : const size_t kAllocSize = 100;
32 E : testing::FakeAsanBlock fake_block(kShadowRatioLog, runtime_->stack_cache());
33 E : EXPECT_TRUE(fake_block.InitializeBlock(kAllocSize));
34 :
35 E : fake_block.block_info.header->magic = ~kBlockHeaderMagic;
36 E : EXPECT_TRUE(IsBlockCorrupt(fake_block.block_info.block, NULL));
37 E : fake_block.block_info.header->magic = kBlockHeaderMagic;
38 E : EXPECT_FALSE(IsBlockCorrupt(fake_block.block_info.block, NULL));
39 E : }
40 :
41 E : TEST_F(BlockUtilTest, IsBlockCorruptInvalidChecksum) {
42 E : const size_t kAllocSize = 100;
43 : static const size_t kChecksumRepeatCount = 10;
44 :
45 : // This can fail because of a checksum collision. However, we run it a
46 : // handful of times to keep the chances as small as possible.
47 E : for (size_t i = 0; i < kChecksumRepeatCount; ++i) {
48 E : testing::FakeAsanBlock fake_block(kShadowRatioLog, runtime_->stack_cache());
49 E : EXPECT_TRUE(fake_block.InitializeBlock(kAllocSize));
50 E : EXPECT_TRUE(fake_block.MarkBlockAsQuarantined());
51 :
52 : // Change some of the block content and verify that the block is now being
53 : // seen as corrupt.
54 E : size_t original_checksum = fake_block.block_info.header->checksum;
55 E : uint8 original_value = fake_block.block_info.body[0];
56 E : fake_block.block_info.body[0]++;
57 :
58 : // Try again for all but the last attempt if this appears to have failed.
59 : if (!IsBlockCorrupt(fake_block.block_info.body, NULL) &&
60 E : i + 1 < kChecksumRepeatCount) {
61 i : continue;
62 : }
63 :
64 E : ASSERT_TRUE(IsBlockCorrupt(fake_block.block_info.body, NULL));
65 E : fake_block.block_info.body[0] = original_value;
66 E : ASSERT_FALSE(IsBlockCorrupt(fake_block.block_info.body, NULL));
67 E : break;
68 i : }
69 E : }
70 :
71 : } // namespace asan
72 : } // namespace agent
|