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

Side by Side Diff: courgette/rel32_finder_x64_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: Created 4 years, 7 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 2016 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" 5 #include "courgette/rel32_finder_x64.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
11 #include <sstream> 11 #include <sstream>
12 #include <string> 12 #include <string>
13 13
14 #include "base/macros.h" 14 #include "base/macros.h"
15 #include "courgette/base_test_unittest.h" 15 #include "courgette/base_test_unittest.h"
16 #include "courgette/image_utils.h" 16 #include "courgette/image_utils.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 Rel32FinderX64 test case.
huangs 2016/05/26 18:34:34 There's a lot of duplicate code with rel32_finder_
etiennep 2016/05/26 20:50:10 Done.
24 class Rel32FinderWin32X86TestCase { 24 class Rel32FinderX64TestCase {
25 public: 25 public:
26 Rel32FinderWin32X86TestCase(const std::string& test_data) 26 Rel32FinderX64TestCase(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 void RunTestBasic(std::string name) { 35 void RunTestBasic(std::string name) {
36 Rel32FinderWin32X86_Basic finder(relocs_start_rva_, relocs_end_rva_); 36 Rel32FinderX64 finder(relocs_start_rva_, relocs_end_rva_);
37 ASSERT_FALSE(text_data_.empty()); 37 ASSERT_FALSE(text_data_.empty());
38 finder.Find(&text_data_[0], &text_data_[0] + text_data_.size(), 38 finder.Find(&text_data_[0], &text_data_[0] + text_data_.size(),
39 text_start_rva_, text_end_rva_, abs32_locations_); 39 text_start_rva_, text_end_rva_, image_end_rva_,
40 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
46 private: 47 private:
47 RVA text_start_rva_; 48 RVA text_start_rva_;
48 RVA text_end_rva_; 49 RVA text_end_rva_;
49 RVA relocs_start_rva_; 50 RVA relocs_start_rva_;
(...skipping 26 matching lines...) Expand all
76 // Scans |iss| for the next non-empty line, and reads (hex) uint32_t into |v|. 77 // Scans |iss| for the next non-empty line, and reads (hex) uint32_t into |v|.
77 // Returns true iff successful. 78 // Returns true iff successful.
78 bool ReadHexUInt32(std::istringstream& iss, uint32_t* v) { 79 bool ReadHexUInt32(std::istringstream& iss, uint32_t* v) {
79 std::string line; 80 std::string line;
80 if (!ReadNonEmptyLine(iss, &line)) 81 if (!ReadNonEmptyLine(iss, &line))
81 return false; 82 return false;
82 return sscanf(line.c_str(), "%X", v) == 1; 83 return sscanf(line.c_str(), "%X", v) == 1;
83 } 84 }
84 85
85 // Initializes the test case by parsing the multi-line string |test_data| 86 // Initializes the test case by parsing the multi-line string |test_data|
86 // to extract Rel32FinderWin32X86 parameters, and read expected values. 87 // to extract Rel32FinderX64 parameters, and read expected values.
87 void LoadTestFromString(const std::string& test_data) { 88 void LoadTestFromString(const std::string& test_data) {
88 // The first lines (ignoring empty ones) specify RVA bounds. 89 // The first lines (ignoring empty ones) specify RVA bounds.
89 std::istringstream iss(test_data); 90 std::istringstream iss(test_data);
90 ASSERT_TRUE(ReadHexUInt32(iss, &text_start_rva_)); 91 ASSERT_TRUE(ReadHexUInt32(iss, &text_start_rva_));
91 ASSERT_TRUE(ReadHexUInt32(iss, &text_end_rva_)); 92 ASSERT_TRUE(ReadHexUInt32(iss, &text_end_rva_));
92 ASSERT_TRUE(ReadHexUInt32(iss, &relocs_start_rva_)); 93 ASSERT_TRUE(ReadHexUInt32(iss, &relocs_start_rva_));
93 ASSERT_TRUE(ReadHexUInt32(iss, &relocs_end_rva_)); 94 ASSERT_TRUE(ReadHexUInt32(iss, &relocs_end_rva_));
94 ASSERT_TRUE(ReadHexUInt32(iss, &image_end_rva_)); 95 ASSERT_TRUE(ReadHexUInt32(iss, &image_end_rva_));
95 96
96 std::string line; 97 std::string line;
97 // The Program section specifies instruction bytes. We require lines to be 98 // The Program section specifies instruction bytes. We require lines to be
98 // formatted in "DUMPBIN /DISASM" style, i.e., 99 // formatted in "DUMPBIN /DISASM" style, i.e.,
99 // "00401003: E8 00 00 00 00 call 00401008" 100 // "00401003: E8 00 00 00 00 call 00401008"
100 // ^ ^ ^ ^ ^ ^ 101 // ^ ^ ^ ^ ^ ^
101 // We extract up to 6 bytes per line. The remaining are ignored. 102 // We extract up to 7 bytes per line. The remaining are ignored.
huangs 2016/05/26 18:34:34 Keep it at 6; you can wrap instruction to next lin
etiennep 2016/05/26 20:50:10 Done.
102 const int kBytesBegin = 12; 103 const int kBytesBegin = 12;
103 const int kBytesEnd = 17; 104 const int kBytesEnd = 22;
104 ReadNonEmptyLine(iss, &line); 105 ReadNonEmptyLine(iss, &line);
105 ASSERT_EQ("Program:", line); 106 ASSERT_EQ("Program:", line);
106 while (ReadNonEmptyLine(iss, &line) && line != "Abs32:") { 107 while (ReadNonEmptyLine(iss, &line) && line != "Abs32:") {
107 std::string toks = line.substr(kBytesBegin, kBytesEnd); 108 std::string toks = line.substr(kBytesBegin, kBytesEnd);
108 uint32_t vals[6]; 109 uint32_t vals[7];
109 int num_read = sscanf(toks.c_str(), "%X %X %X %X %X %X", &vals[0], 110 int num_read =
110 &vals[1], &vals[2], &vals[3], &vals[4], &vals[5]); 111 sscanf(toks.c_str(), "%X %X %X %X %X %X %X", &vals[0], &vals[1],
112 &vals[2], &vals[3], &vals[4], &vals[5], &vals[6]);
113
111 for (int i = 0; i < num_read; ++i) 114 for (int i = 0; i < num_read; ++i)
112 text_data_.push_back(static_cast<uint8_t>(vals[i] & 0xFF)); 115 text_data_.push_back(static_cast<uint8_t>(vals[i] & 0xFF));
113 } 116 }
117
114 ASSERT_FALSE(text_data_.empty()); 118 ASSERT_FALSE(text_data_.empty());
115 119
116 // The Abs32 section specifies hex RVAs, one per line. 120 // The Abs32 section specifies hex RVAs, one per line.
117 ASSERT_EQ("Abs32:", line); 121 ASSERT_EQ("Abs32:", line);
118 while (ReadNonEmptyLine(iss, &line) && line != "Expected:") { 122 while (ReadNonEmptyLine(iss, &line) && line != "Expected:") {
119 RVA abs32_location; 123 RVA abs32_location;
120 ASSERT_EQ(1, sscanf(line.c_str(), "%X", &abs32_location)); 124 ASSERT_EQ(1, sscanf(line.c_str(), "%X", &abs32_location));
121 abs32_locations_.push_back(abs32_location); 125 abs32_locations_.push_back(abs32_location);
122 } 126 }
123 127
124 // The Expected section specifies hex Rel32 RVAs, one per line. 128 // The Expected section specifies hex Rel32 RVAs, one per line.
125 ASSERT_EQ("Expected:", line); 129 ASSERT_EQ("Expected:", line);
126 while (ReadNonEmptyLine(iss, &line)) { 130 while (ReadNonEmptyLine(iss, &line)) {
127 RVA rel32_location; 131 RVA rel32_location;
128 ASSERT_EQ(1, sscanf(line.c_str(), "%X", &rel32_location)); 132 ASSERT_EQ(1, sscanf(line.c_str(), "%X", &rel32_location));
129 expected_rel32_locations_.push_back(rel32_location); 133 expected_rel32_locations_.push_back(rel32_location);
130 } 134 }
131 } 135 }
132 }; 136 };
133 137
134 class Rel32FinderWin32X86Test : public BaseTest { 138 class Rel32FinderX64Test : public BaseTest {
135 public: 139 public:
136 void RunTest(const char* test_case_file) { 140 void RunTest(const char* test_case_file) {
137 Rel32FinderWin32X86TestCase test_case(FileContents(test_case_file)); 141 Rel32FinderX64TestCase test_case(FileContents(test_case_file));
138 test_case.RunTestBasic(test_case_file); 142 test_case.RunTestBasic(test_case_file);
139 } 143 }
140 }; 144 };
141 145
142 TEST_F(Rel32FinderWin32X86Test, TestBasic) { 146 TEST_F(Rel32FinderX64Test, TestBasic) {
143 RunTest("rel32_win32_x86_01.txt"); 147 RunTest("rel32_x64_01.txt");
144 RunTest("rel32_win32_x86_02.txt"); 148 RunTest("rel32_x64_02.txt");
145 RunTest("rel32_win32_x86_03.txt"); 149 RunTest("rel32_x64_03.txt");
146 RunTest("rel32_win32_x86_04.txt");
147 } 150 }
148 151
149 } // namespace 152 } // namespace
150 153
151 } // namespace courgette 154 } // namespace courgette
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698