OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 | |
16 #include "syzygy/instrument/transforms/security_cookie_check_hook_transform.h" | |
17 | |
18 #include "gtest/gtest.h" | |
19 #include "syzygy/block_graph/basic_block.h" | |
20 #include "syzygy/block_graph/basic_block_decomposer.h" | |
21 #include "syzygy/block_graph/basic_block_subgraph.h" | |
22 #include "syzygy/block_graph/block_graph.h" | |
23 #include "syzygy/core/unittest_util.h" | |
24 #include "syzygy/instrument/transforms/unittest_util.h" | |
25 #include "syzygy/pe/unittest_util.h" | |
26 | |
27 #include "mnemonics.h" // NOLINT | |
28 | |
29 namespace instrument { | |
30 namespace transforms { | |
31 namespace { | |
32 | |
33 using block_graph::BasicBlock; | |
34 using block_graph::BasicBlockDecomposer; | |
35 using block_graph::BasicBlockSubGraph; | |
36 using block_graph::BasicCodeBlock; | |
37 using block_graph::BlockGraph; | |
38 using block_graph::Instruction; | |
39 | |
40 class TestSecurityCookieCheckHookTransform | |
41 : public SecurityCookieCheckHookTransform { | |
chrisha
2017/05/09 19:18:40
You've no need to expose anything via this inherit
| |
42 | |
43 }; | |
44 | |
45 class SecurityCookieCheckHookTransformTest | |
46 : public testing::TestDllTransformTest { | |
47 protected: | |
48 void CheckBasicBlockInstrumentation(); | |
49 | |
50 TestSecurityCookieCheckHookTransform security_cookie_check_hook_; | |
51 }; | |
52 | |
53 void SecurityCookieCheckHookTransformTest::CheckBasicBlockInstrumentation() { | |
54 bool hit = false; | |
55 | |
56 BlockGraph::BlockMap::const_iterator block_iter = | |
57 block_graph_.blocks().begin(); | |
58 for (; block_iter != block_graph_.blocks().end(); ++block_iter) { | |
59 const BlockGraph::Block& block = block_iter->second; | |
60 | |
61 // Skip everything but __my_report_gsfailure. | |
62 if (block.name() != "__my_report_gsfailure") | |
chrisha
2017/05/09 19:18:40
Expose the string as a class static member in the
| |
63 continue; | |
64 | |
65 hit = true; | |
66 | |
67 // Decompose the block to basic-blocks. | |
68 BasicBlockSubGraph subgraph; | |
69 BasicBlockDecomposer bb_decomposer(&block, &subgraph); | |
70 ASSERT_TRUE(bb_decomposer.Decompose()); | |
71 | |
72 // Retrieve the first basic block. | |
73 ASSERT_EQ(1, subgraph.block_descriptions().size()); | |
74 const BasicBlockSubGraph::BasicBlockOrdering& original_order = | |
75 subgraph.block_descriptions().front().basic_block_order; | |
76 BasicCodeBlock* first_bb = BasicCodeBlock::Cast(*original_order.begin()); | |
77 ASSERT_NE(first_bb, nullptr); | |
78 | |
79 // Check if the stub is a 'mov [deadbeef], eax' instruction | |
chrisha
2017/05/09 19:18:40
Missing period on comment.
| |
80 BasicBlockSubGraph::BBCollection::const_iterator bb_iter = | |
81 subgraph.basic_blocks().begin(); | |
82 for (; bb_iter != subgraph.basic_blocks().end(); ++bb_iter) { | |
83 const BasicCodeBlock* bb = BasicCodeBlock::Cast(*bb_iter); | |
84 if (bb == nullptr || bb->is_padding()) | |
85 continue; | |
86 | |
87 BasicBlock::Instructions::const_iterator | |
88 inst_iter = bb->instructions().begin(), | |
89 end_iter = bb->instructions().end(); | |
90 ASSERT_NE(inst_iter, end_iter); | |
91 // mov [deadbeef], eax | |
92 const Instruction& inst = *inst_iter; | |
93 EXPECT_EQ(I_MOV, inst.representation().opcode); | |
94 } | |
chrisha
2017/05/09 19:18:40
Instead of using the disassembler and everything,
| |
95 } | |
96 | |
97 EXPECT_TRUE(hit); | |
98 } | |
99 | |
100 } // namespace | |
101 | |
102 TEST_F(SecurityCookieCheckHookTransformTest, ApplyTranform) { | |
103 ASSERT_NO_FATAL_FAILURE(DecomposeTestDll()); | |
104 | |
105 ASSERT_TRUE(block_graph::ApplyBlockGraphTransform( | |
106 &security_cookie_check_hook_, policy_, | |
107 &block_graph_, header_block_ | |
chrisha
2017/05/09 19:18:40
Indent these lines another 2 spaces.
| |
108 )); | |
chrisha
2017/05/09 19:18:40
Move to end of previous line.
| |
109 | |
110 ASSERT_NO_FATAL_FAILURE(CheckBasicBlockInstrumentation()); | |
111 } | |
112 | |
113 } // namespace transforms | |
114 } // namespace instrument | |
OLD | NEW |