1 : // Copyright 2011 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/reorder/random_order_generator.h"
16 :
17 : #include "gtest/gtest.h"
18 : #include "syzygy/pe/pe_utils.h"
19 : #include "syzygy/reorder/order_generator_test.h"
20 :
21 : namespace reorder {
22 :
23 : namespace {
24 :
25 : typedef Reorderer::Order::BlockSpec BlockSpec;
26 : typedef Reorderer::Order::SectionSpecVector SectionSpecVector;
27 :
28 : class RandomOrderGeneratorTest : public testing::OrderGeneratorTest {
29 : protected:
30 E : RandomOrderGeneratorTest() : order_generator_(1234) {}
31 :
32 : RandomOrderGenerator order_generator_;
33 : };
34 :
35 : } // namespace
36 :
37 E : TEST_F(RandomOrderGeneratorTest, DoNotReorder) {
38 : EXPECT_TRUE(order_generator_.CalculateReordering(input_dll_,
39 : image_layout_,
40 : false,
41 : false,
42 E : &order_));
43 :
44 E : ExpectNoDuplicateBlocks();
45 :
46 : // Verify that the order found in order_ matches the original decomposed
47 : // image.
48 E : for (size_t i = 0; i != order_.sections.size(); ++i) {
49 E : const IMAGE_SECTION_HEADER* section = input_dll_.section_header(i);
50 E : ExpectSameOrder(section, order_.sections[i].blocks);
51 E : }
52 E : }
53 :
54 E : TEST_F(RandomOrderGeneratorTest, ReorderCode) {
55 : EXPECT_TRUE(order_generator_.CalculateReordering(input_dll_,
56 : image_layout_,
57 : true,
58 : false,
59 E : &order_));
60 :
61 E : ExpectNoDuplicateBlocks();
62 :
63 : // Verify that code blocks have been reordered and that data blocks have not.
64 E : for (size_t i = 0; i != order_.sections.size(); ++i) {
65 E : const IMAGE_SECTION_HEADER* section = input_dll_.section_header(i);
66 E : if ((section->Characteristics & IMAGE_SCN_CNT_CODE) != 0) {
67 E : ExpectDifferentOrder(section, order_.sections[i].blocks);
68 E : } else {
69 E : ExpectSameOrder(section, order_.sections[i].blocks);
70 : }
71 E : }
72 E : }
73 :
74 E : TEST_F(RandomOrderGeneratorTest, ReorderData) {
75 : EXPECT_TRUE(order_generator_.CalculateReordering(input_dll_,
76 : image_layout_,
77 : false,
78 : true,
79 E : &order_));
80 :
81 E : ExpectNoDuplicateBlocks();
82 :
83 : // Verify that data blocks have been reordered and that code blocks have not.
84 E : for (size_t i = 0; i != order_.sections.size(); ++i) {
85 E : const IMAGE_SECTION_HEADER* section = input_dll_.section_header(i);
86 E : if ((section->Characteristics & IMAGE_SCN_CNT_CODE) == 0) {
87 E : std::string name = input_dll_.GetSectionName(*section);
88 : // .tls and .rsrc only have one block.
89 E : if (name == ".tls" || name == ".rsrc") {
90 E : EXPECT_EQ(1U, order_.sections[i].blocks.size());
91 E : } else {
92 : // In VS2013 the .reloc section may contain only one block.
93 E : if (order_.sections[i].blocks.size() > 1)
94 E : ExpectDifferentOrder(section, order_.sections[i].blocks);
95 : }
96 E : } else {
97 E : ExpectSameOrder(section, order_.sections[i].blocks);
98 : }
99 E : }
100 E : }
101 :
102 : } // namespace reorder
|