OLD | NEW |
(Empty) | |
| 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/block_graph/basic_block_assembler.h" |
| 16 #include "syzygy/protect/protect_lib/code_randomizer.h" |
| 17 |
| 18 #include <algorithm> |
| 19 #include <vector> |
| 20 #include "base/strings/utf_string_conversions.h" |
| 21 #include "gtest/gtest.h" |
| 22 #include "gmock/gmock.h" |
| 23 #include "syzygy/block_graph/basic_block_subgraph.h" |
| 24 #include "syzygy/core/unittest_util.h" |
| 25 #include "syzygy/pe/unittest_util.h" |
| 26 #include "syzygy/application/application.h" |
| 27 |
| 28 namespace protect { |
| 29 |
| 30 namespace { |
| 31 |
| 32 class CodeRandomizerTest : public testing::Test, public CodeRandomizer { |
| 33 public: |
| 34 typedef block_graph::BlockGraph BlockGraph; |
| 35 typedef block_graph::BasicBlockSubGraph BasicBlockSubGraph; |
| 36 typedef block_graph::BasicCodeBlock BasicCodeBlock; |
| 37 typedef block_graph::BasicBlock BasicBlock; |
| 38 typedef block_graph::BasicBlockAssembler BasicBlockAssembler; |
| 39 typedef BlockGraph::RelativeAddress RelativeAddress; |
| 40 typedef BlockGraph::Block::SourceRange SourceRange; |
| 41 |
| 42 CodeRandomizerTest(); |
| 43 |
| 44 void SetUp() override {} |
| 45 |
| 46 void TearDown() override {} |
| 47 |
| 48 protected: |
| 49 struct Ref { |
| 50 size_t offset; |
| 51 block_graph::BasicBlockReference::ReferredType type; |
| 52 const void* reference; |
| 53 }; |
| 54 |
| 55 BlockGraph block_graph_; |
| 56 BlockGraph::Block* test_block_; |
| 57 block_graph::BasicBlockSubGraph subgraph_; |
| 58 BasicCodeBlock* test_bb_; |
| 59 BasicBlock::Instructions instructions_; |
| 60 BasicBlockAssembler asm_; |
| 61 }; |
| 62 |
| 63 } |
| 64 CodeRandomizerTest::CodeRandomizerTest() |
| 65 : test_block_(NULL), |
| 66 test_bb_(NULL), |
| 67 asm_(instructions_.end(), &instructions_) { |
| 68 std::srand(std::time(0)); |
| 69 test_block_ = block_graph_.AddBlock(BlockGraph::CODE_BLOCK, 10, "test block"); |
| 70 test_bb_ = subgraph_.AddBasicCodeBlock("foo"); |
| 71 } |
| 72 |
| 73 TEST_F(CodeRandomizerTest, FindSafeRegister) { |
| 74 RegState test_state; |
| 75 std::vector<const assm::Register32> possible_regs; |
| 76 possible_regs.push_back(assm::eax); |
| 77 possible_regs.push_back(assm::ebx); |
| 78 possible_regs.push_back(assm::ecx); |
| 79 possible_regs.push_back(assm::edx); |
| 80 possible_regs.push_back(assm::esi); |
| 81 possible_regs.push_back(assm::edi); |
| 82 |
| 83 std::random_shuffle(possible_regs.begin(), possible_regs.end()); |
| 84 |
| 85 // Empty state |
| 86 bool save_reg = true; |
| 87 assm::Register32 reg = CodeRandomizerTest::FindSafeRegister(asm_, test_state,
save_reg); |
| 88 |
| 89 EXPECT_TRUE(possible_regs.end() != find(possible_regs.begin(), possible_regs.e
nd(), reg)); |
| 90 EXPECT_FALSE(save_reg); |
| 91 |
| 92 while (possible_regs.size() > 1) { |
| 93 test_state.Add(possible_regs[0].id()); |
| 94 possible_regs.erase(possible_regs.begin()); |
| 95 save_reg = true; |
| 96 |
| 97 reg = CodeRandomizerTest::FindSafeRegister(asm_, test_state, save_reg); |
| 98 |
| 99 EXPECT_TRUE(possible_regs.end() != find(possible_regs.begin(), possible_regs
.end(), reg)); |
| 100 EXPECT_FALSE(save_reg); |
| 101 } |
| 102 |
| 103 // Full State, save_reg should be true |
| 104 save_reg = false; |
| 105 test_state.Add(possible_regs[0].id()); |
| 106 reg = CodeRandomizerTest::FindSafeRegister(asm_, test_state, save_reg); |
| 107 EXPECT_TRUE(save_reg); |
| 108 } |
| 109 |
| 110 TEST_F(CodeRandomizerTest, RandModifyEsp) { |
| 111 BasicBlock::Instructions instructions; |
| 112 BasicBlockAssembler assm(instructions.end(), &instructions); |
| 113 Instruction instr; |
| 114 RegState state; |
| 115 |
| 116 int repeat_times = 10; |
| 117 int prev_size = 0; |
| 118 |
| 119 while (repeat_times) { |
| 120 state.extra_stack = 0; |
| 121 state.instruction_count = 0; |
| 122 |
| 123 RandModifyESP(assm, state); |
| 124 |
| 125 if (instructions.size() == prev_size) { |
| 126 EXPECT_EQ(0, state.extra_stack); |
| 127 EXPECT_EQ(0, state.instruction_count); |
| 128 |
| 129 } else { |
| 130 EXPECT_EQ(1, instructions.size() - prev_size); |
| 131 EXPECT_EQ(1, state.instruction_count); |
| 132 |
| 133 instr = instructions.back(); |
| 134 //std::cout << instr.representation().ops[0]. << "\n"; |
| 135 //instr. |
| 136 } |
| 137 } |
| 138 |
| 139 } |
| 140 */ |
| 141 } |
OLD | NEW |