Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(506)

Side by Side Diff: courgette/rel32_finder_unittest.cc

Issue 2008253004: Refactor rel32 searching process for x64 to make it more similar to x86. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Improve rel32_finder doc Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "courgette/rel32_finder_win32_x86.h"
6
7 #include <stddef.h> 5 #include <stddef.h>
8 #include <stdint.h> 6 #include <stdint.h>
9 7
10 #include <algorithm> 8 #include <algorithm>
11 #include <sstream> 9 #include <sstream>
12 #include <string> 10 #include <string>
13 11
14 #include "base/macros.h" 12 #include "base/macros.h"
15 #include "courgette/base_test_unittest.h" 13 #include "courgette/base_test_unittest.h"
16 #include "courgette/image_utils.h" 14 #include "courgette/image_utils.h"
15 #include "courgette/rel32_finder_x64.h"
16 #include "courgette/rel32_finder_x86.h"
17 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
18 18
19 namespace courgette { 19 namespace courgette {
20 20
21 namespace { 21 namespace {
22 22
23 // Helper class to load and execute a Rel32FinderWin32X86 test case. 23 // Helper class to load and execute a Rel32Finder test case.
24 class Rel32FinderWin32X86TestCase { 24 class Rel32FinderTestCase {
25 public: 25 public:
26 Rel32FinderWin32X86TestCase(const std::string& test_data) 26 Rel32FinderTestCase(const std::string& test_data)
27 : text_start_rva_(0), 27 : text_start_rva_(0),
28 text_end_rva_(0), 28 text_end_rva_(0),
29 relocs_start_rva_(0), 29 relocs_start_rva_(0),
30 relocs_end_rva_(0), 30 relocs_end_rva_(0),
31 image_end_rva_(0) { 31 image_end_rva_(0) {
32 LoadTestFromString(test_data); 32 LoadTestFromString(test_data);
33 } 33 }
34 34
35 template <class Finder>
huangs 2016/05/30 05:48:34 Pretty cool use of template. :) In this instance
etiennep 2016/05/30 17:07:30 Done.
35 void RunTestBasic(std::string name) { 36 void RunTestBasic(std::string name) {
36 Rel32FinderWin32X86_Basic finder(relocs_start_rva_, relocs_end_rva_); 37 Finder finder = CreateFinder<Finder>();
37 ASSERT_FALSE(text_data_.empty()); 38 ASSERT_FALSE(text_data_.empty());
38 finder.Find(&text_data_[0], &text_data_[0] + text_data_.size(), 39 finder.Find(&text_data_[0], &text_data_[0] + text_data_.size(),
39 text_start_rva_, text_end_rva_, abs32_locations_); 40 text_start_rva_, text_end_rva_, abs32_locations_);
40 std::vector<RVA> rel32_locations; 41 std::vector<RVA> rel32_locations;
41 finder.SwapRel32Locations(&rel32_locations); 42 finder.SwapRel32Locations(&rel32_locations);
42 EXPECT_EQ(expected_rel32_locations_, rel32_locations) 43 EXPECT_EQ(expected_rel32_locations_, rel32_locations)
43 << "From test case " << name << " (addresses are in hex)"; 44 << "From test case " << name << " (addresses are in hex)";
44 } 45 }
45 46
47 template <class Finder>
48 Finder CreateFinder();
49
50 template <>
51 Rel32FinderX86 CreateFinder<Rel32FinderX86>() {
52 return Rel32FinderX86(relocs_start_rva_, relocs_end_rva_);
53 }
54 template <>
55 Rel32FinderX64 CreateFinder<Rel32FinderX64>() {
56 return Rel32FinderX64(relocs_start_rva_, relocs_end_rva_, image_end_rva_);
57 }
58
46 private: 59 private:
47 RVA text_start_rva_; 60 RVA text_start_rva_;
48 RVA text_end_rva_; 61 RVA text_end_rva_;
49 RVA relocs_start_rva_; 62 RVA relocs_start_rva_;
50 RVA relocs_end_rva_; 63 RVA relocs_end_rva_;
51 RVA image_end_rva_; 64 RVA image_end_rva_;
52 std::vector<uint8_t> text_data_; 65 std::vector<uint8_t> text_data_;
53 std::vector<RVA> abs32_locations_; 66 std::vector<RVA> abs32_locations_;
54 std::vector<RVA> expected_rel32_locations_; 67 std::vector<RVA> expected_rel32_locations_;
55 68
(...skipping 20 matching lines...) Expand all
76 // Scans |iss| for the next non-empty line, and reads (hex) uint32_t into |v|. 89 // Scans |iss| for the next non-empty line, and reads (hex) uint32_t into |v|.
77 // Returns true iff successful. 90 // Returns true iff successful.
78 bool ReadHexUInt32(std::istringstream& iss, uint32_t* v) { 91 bool ReadHexUInt32(std::istringstream& iss, uint32_t* v) {
79 std::string line; 92 std::string line;
80 if (!ReadNonEmptyLine(iss, &line)) 93 if (!ReadNonEmptyLine(iss, &line))
81 return false; 94 return false;
82 return sscanf(line.c_str(), "%X", v) == 1; 95 return sscanf(line.c_str(), "%X", v) == 1;
83 } 96 }
84 97
85 // Initializes the test case by parsing the multi-line string |test_data| 98 // Initializes the test case by parsing the multi-line string |test_data|
86 // to extract Rel32FinderWin32X86 parameters, and read expected values. 99 // to extract Rel32Finder parameters, and read expected values.
87 void LoadTestFromString(const std::string& test_data) { 100 void LoadTestFromString(const std::string& test_data) {
88 // The first lines (ignoring empty ones) specify RVA bounds. 101 // The first lines (ignoring empty ones) specify RVA bounds.
89 std::istringstream iss(test_data); 102 std::istringstream iss(test_data);
90 ASSERT_TRUE(ReadHexUInt32(iss, &text_start_rva_)); 103 ASSERT_TRUE(ReadHexUInt32(iss, &text_start_rva_));
91 ASSERT_TRUE(ReadHexUInt32(iss, &text_end_rva_)); 104 ASSERT_TRUE(ReadHexUInt32(iss, &text_end_rva_));
92 ASSERT_TRUE(ReadHexUInt32(iss, &relocs_start_rva_)); 105 ASSERT_TRUE(ReadHexUInt32(iss, &relocs_start_rva_));
93 ASSERT_TRUE(ReadHexUInt32(iss, &relocs_end_rva_)); 106 ASSERT_TRUE(ReadHexUInt32(iss, &relocs_end_rva_));
94 ASSERT_TRUE(ReadHexUInt32(iss, &image_end_rva_)); 107 ASSERT_TRUE(ReadHexUInt32(iss, &image_end_rva_));
95 108
96 std::string line; 109 std::string line;
97 // The Program section specifies instruction bytes. We require lines to be 110 // The Program section specifies instruction bytes. We require lines to be
98 // formatted in "DUMPBIN /DISASM" style, i.e., 111 // formatted in "DUMPBIN /DISASM" style, i.e.,
99 // "00401003: E8 00 00 00 00 call 00401008" 112 // "00401003: E8 00 00 00 00 call 00401008"
100 // ^ ^ ^ ^ ^ ^ 113 // ^ ^ ^ ^ ^ ^
101 // We extract up to 6 bytes per line. The remaining are ignored. 114 // We extract up to 6 bytes per line. The remaining are ignored.
102 const int kBytesBegin = 12; 115 const int kBytesBegin = 12;
103 const int kBytesEnd = 17; 116 const int kBytesEnd = 17;
104 ReadNonEmptyLine(iss, &line); 117 ReadNonEmptyLine(iss, &line);
105 ASSERT_EQ("Program:", line); 118 ASSERT_EQ("Program:", line);
106 while (ReadNonEmptyLine(iss, &line) && line != "Abs32:") { 119 while (ReadNonEmptyLine(iss, &line) && line != "Abs32:") {
107 std::string toks = line.substr(kBytesBegin, kBytesEnd); 120 std::string toks = line.substr(kBytesBegin, kBytesEnd);
108 uint32_t vals[6]; 121 uint32_t vals[6];
109 int num_read = sscanf(toks.c_str(), "%X %X %X %X %X %X", &vals[0], 122 int num_read = sscanf(toks.c_str(), "%X %X %X %X %X %X", &vals[0],
110 &vals[1], &vals[2], &vals[3], &vals[4], &vals[5]); 123 &vals[1], &vals[2], &vals[3], &vals[4], &vals[5]);
111 for (int i = 0; i < num_read; ++i) 124 for (int i = 0; i < num_read; ++i)
112 text_data_.push_back(static_cast<uint8_t>(vals[i] & 0xFF)); 125 text_data_.push_back(static_cast<uint8_t>(vals[i] & 0xFF));
113 } 126 }
114 ASSERT_FALSE(text_data_.empty()); 127 ASSERT_FALSE(text_data_.empty());
115 128
116 // The Abs32 section specifies hex RVAs, one per line. 129 // The Abs32 section specifies hex RVAs, one per line.
117 ASSERT_EQ("Abs32:", line); 130 ASSERT_EQ("Abs32:", line);
118 while (ReadNonEmptyLine(iss, &line) && line != "Expected:") { 131 while (ReadNonEmptyLine(iss, &line) && line != "Expected:") {
119 RVA abs32_location; 132 RVA abs32_location;
120 ASSERT_EQ(1, sscanf(line.c_str(), "%X", &abs32_location)); 133 ASSERT_EQ(1, sscanf(line.c_str(), "%X", &abs32_location));
121 abs32_locations_.push_back(abs32_location); 134 abs32_locations_.push_back(abs32_location);
122 } 135 }
123 136
124 // The Expected section specifies hex Rel32 RVAs, one per line. 137 // The Expected section specifies hex Rel32 RVAs, one per line.
125 ASSERT_EQ("Expected:", line); 138 ASSERT_EQ("Expected:", line);
126 while (ReadNonEmptyLine(iss, &line)) { 139 while (ReadNonEmptyLine(iss, &line)) {
127 RVA rel32_location; 140 RVA rel32_location;
128 ASSERT_EQ(1, sscanf(line.c_str(), "%X", &rel32_location)); 141 ASSERT_EQ(1, sscanf(line.c_str(), "%X", &rel32_location));
129 expected_rel32_locations_.push_back(rel32_location); 142 expected_rel32_locations_.push_back(rel32_location);
130 } 143 }
131 } 144 }
132 }; 145 };
133 146
134 class Rel32FinderWin32X86Test : public BaseTest { 147 class Rel32FinderTest : public BaseTest {
135 public: 148 public:
149 template <class Finder>
136 void RunTest(const char* test_case_file) { 150 void RunTest(const char* test_case_file) {
137 Rel32FinderWin32X86TestCase test_case(FileContents(test_case_file)); 151 Rel32FinderTestCase test_case(FileContents(test_case_file));
138 test_case.RunTestBasic(test_case_file); 152 test_case.RunTestBasic<Finder>(test_case_file);
139 } 153 }
140 }; 154 };
141 155
142 TEST_F(Rel32FinderWin32X86Test, TestBasic) { 156 TEST_F(Rel32FinderTest, TestBasic) {
143 RunTest("rel32_win32_x86_01.txt"); 157 RunTest<Rel32FinderX86>("rel32_x86_01.txt");
144 RunTest("rel32_win32_x86_02.txt"); 158 RunTest<Rel32FinderX86>("rel32_x86_02.txt");
145 RunTest("rel32_win32_x86_03.txt"); 159 RunTest<Rel32FinderX86>("rel32_x86_03.txt");
146 RunTest("rel32_win32_x86_04.txt"); 160 RunTest<Rel32FinderX86>("rel32_x86_04.txt");
161
162 RunTest<Rel32FinderX64>("rel32_x64_01.txt");
163 RunTest<Rel32FinderX64>("rel32_x64_02.txt");
164 RunTest<Rel32FinderX64>("rel32_x64_03.txt");
147 } 165 }
huangs 2016/05/30 05:48:34 Curious why there's no rel32_x64_04.txt?
etiennep 2016/05/30 17:07:30 I don't think testing false positive is relevant.
148 166
149 } // namespace 167 } // namespace
150 168
151 } // namespace courgette 169 } // namespace courgette
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698