1 : // Copyright 2013 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/instrument/instrumenters/entry_thunk_instrumenter.h"
16 :
17 : #include "base/command_line.h"
18 : #include "gtest/gtest.h"
19 : #include "syzygy/core/unittest_util.h"
20 : #include "syzygy/pe/unittest_util.h"
21 :
22 : namespace instrument {
23 : namespace instrumenters {
24 :
25 : namespace {
26 :
27 : class TestEntryThunkInstrumenter : public EntryThunkInstrumenter {
28 : public:
29 : using EntryThunkInstrumenter::agent_dll_;
30 : using EntryThunkInstrumenter::input_image_path_;
31 : using EntryThunkInstrumenter::input_pdb_path_;
32 : using EntryThunkInstrumenter::output_image_path_;
33 : using EntryThunkInstrumenter::output_pdb_path_;
34 : using EntryThunkInstrumenter::allow_overwrite_;
35 : using EntryThunkInstrumenter::no_augment_pdb_;
36 : using EntryThunkInstrumenter::no_strip_strings_;
37 : using EntryThunkInstrumenter::instrument_unsafe_references_;
38 : using EntryThunkInstrumenter::module_entry_only_;
39 : using EntryThunkInstrumenter::thunk_imports_;
40 : using EntryThunkInstrumenter::debug_friendly_;
41 : using EntryThunkInstrumenter::instrumentation_mode_;
42 : using EntryThunkInstrumenter::kAgentDllProfile;
43 : using EntryThunkInstrumenter::kAgentDllRpc;
44 : using EntryThunkInstrumenter::InstrumentPrepare;
45 : using EntryThunkInstrumenter::InstrumentImpl;
46 : using InstrumenterWithAgent::CreateRelinker;
47 :
48 E : explicit TestEntryThunkInstrumenter(Mode instrumentation_mode)
49 : : EntryThunkInstrumenter(instrumentation_mode) {
50 E : }
51 : };
52 :
53 : class EntryThunkInstrumenterTest : public testing::PELibUnitTest {
54 : public:
55 : typedef testing::PELibUnitTest Super;
56 :
57 E : EntryThunkInstrumenterTest()
58 : : cmd_line_(base::FilePath(L"instrument.exe")) {
59 E : }
60 :
61 E : void SetUp() override {
62 E : testing::Test::SetUp();
63 :
64 : // Several of the tests generate progress and (deliberate) error messages
65 : // that would otherwise clutter the unittest output.
66 E : logging::SetMinLogLevel(logging::LOG_FATAL);
67 :
68 : // Setup the IO streams.
69 E : CreateTemporaryDir(&temp_dir_);
70 E : stdin_path_ = temp_dir_.Append(L"NUL");
71 E : stdout_path_ = temp_dir_.Append(L"stdout.txt");
72 E : stderr_path_ = temp_dir_.Append(L"stderr.txt");
73 E : InitStreams(stdin_path_, stdout_path_, stderr_path_);
74 :
75 : // Initialize the (potential) input and output path values.
76 E : abs_input_image_path_ = testing::GetExeRelativePath(testing::kTestDllName);
77 E : input_image_path_ = testing::GetRelativePath(abs_input_image_path_);
78 E : abs_input_pdb_path_ = testing::GetExeRelativePath(testing::kTestDllPdbName);
79 E : input_pdb_path_ = testing::GetRelativePath(abs_input_pdb_path_);
80 E : output_image_path_ = temp_dir_.Append(input_image_path_.BaseName());
81 E : output_pdb_path_ = temp_dir_.Append(input_pdb_path_.BaseName());
82 E : }
83 :
84 E : void SetUpValidCommandLine() {
85 E : cmd_line_.AppendSwitchPath("input-image", input_image_path_);
86 E : cmd_line_.AppendSwitchPath("output-image", output_image_path_);
87 E : }
88 :
89 : protected:
90 : base::FilePath temp_dir_;
91 :
92 : // @name The redirected streams paths.
93 : // @{
94 : base::FilePath stdin_path_;
95 : base::FilePath stdout_path_;
96 : base::FilePath stderr_path_;
97 : // @}
98 :
99 : // @name Command-line and parameters.
100 : // @{
101 : base::CommandLine cmd_line_;
102 : base::FilePath input_image_path_;
103 : base::FilePath input_pdb_path_;
104 : base::FilePath output_image_path_;
105 : base::FilePath output_pdb_path_;
106 : // @}
107 :
108 : // @name Expected final values of input parameters.
109 : // @{
110 : base::FilePath abs_input_image_path_;
111 : base::FilePath abs_input_pdb_path_;
112 : // @}
113 :
114 : // The fake instrumenter we delegate to.
115 : scoped_ptr<TestEntryThunkInstrumenter> instrumenter_;
116 : };
117 :
118 : } // namespace
119 :
120 E : TEST_F(EntryThunkInstrumenterTest, ParseMinimalCallTrace) {
121 E : SetUpValidCommandLine();
122 : instrumenter_.reset(
123 E : new TestEntryThunkInstrumenter(EntryThunkInstrumenter::CALL_TRACE));
124 :
125 E : EXPECT_TRUE(instrumenter_->ParseCommandLine(&cmd_line_));
126 :
127 : EXPECT_EQ(EntryThunkInstrumenter::CALL_TRACE,
128 E : instrumenter_->instrumentation_mode_);
129 E : EXPECT_EQ(abs_input_image_path_, instrumenter_->input_image_path_);
130 E : EXPECT_EQ(output_image_path_, instrumenter_->output_image_path_);
131 : EXPECT_EQ(std::string(TestEntryThunkInstrumenter::kAgentDllRpc),
132 E : instrumenter_->agent_dll_);
133 E : EXPECT_FALSE(instrumenter_->allow_overwrite_);
134 E : EXPECT_FALSE(instrumenter_->no_augment_pdb_);
135 E : EXPECT_FALSE(instrumenter_->no_strip_strings_);
136 E : EXPECT_FALSE(instrumenter_->debug_friendly_);
137 E : EXPECT_FALSE(instrumenter_->thunk_imports_);
138 E : EXPECT_TRUE(instrumenter_->instrument_unsafe_references_);
139 E : EXPECT_FALSE(instrumenter_->module_entry_only_);
140 E : }
141 :
142 E : TEST_F(EntryThunkInstrumenterTest, ParseFullCallTrace) {
143 E : SetUpValidCommandLine();
144 : instrumenter_.reset(
145 E : new TestEntryThunkInstrumenter(EntryThunkInstrumenter::CALL_TRACE));
146 :
147 E : cmd_line_.AppendSwitchASCII("agent", "foo.dll");
148 E : cmd_line_.AppendSwitch("debug-friendly");
149 E : cmd_line_.AppendSwitchPath("input-pdb", input_pdb_path_);
150 E : cmd_line_.AppendSwitch("no-augment-pdb");
151 E : cmd_line_.AppendSwitch("no-strip-strings");
152 E : cmd_line_.AppendSwitchPath("output-pdb", output_pdb_path_);
153 E : cmd_line_.AppendSwitch("overwrite");
154 E : cmd_line_.AppendSwitch("instrument-imports");
155 E : cmd_line_.AppendSwitch("module-entry-only");
156 E : cmd_line_.AppendSwitch("no-unsafe-refs");
157 :
158 E : EXPECT_TRUE(instrumenter_->ParseCommandLine(&cmd_line_));
159 :
160 : EXPECT_EQ(EntryThunkInstrumenter::CALL_TRACE,
161 E : instrumenter_->instrumentation_mode_);
162 E : EXPECT_EQ(abs_input_image_path_, instrumenter_->input_image_path_);
163 E : EXPECT_EQ(output_image_path_, instrumenter_->output_image_path_);
164 E : EXPECT_EQ(abs_input_pdb_path_, instrumenter_->input_pdb_path_);
165 E : EXPECT_EQ(output_pdb_path_, instrumenter_->output_pdb_path_);
166 E : EXPECT_EQ(std::string("foo.dll"), instrumenter_->agent_dll_);
167 E : EXPECT_TRUE(instrumenter_->allow_overwrite_);
168 E : EXPECT_TRUE(instrumenter_->no_augment_pdb_);
169 E : EXPECT_TRUE(instrumenter_->no_strip_strings_);
170 E : EXPECT_TRUE(instrumenter_->debug_friendly_);
171 E : EXPECT_TRUE(instrumenter_->thunk_imports_);
172 E : EXPECT_FALSE(instrumenter_->instrument_unsafe_references_);
173 E : EXPECT_TRUE(instrumenter_->module_entry_only_);
174 E : }
175 :
176 E : TEST_F(EntryThunkInstrumenterTest, ParseMinimalProfile) {
177 E : SetUpValidCommandLine();
178 : instrumenter_.reset(
179 E : new TestEntryThunkInstrumenter(EntryThunkInstrumenter::PROFILE));
180 :
181 E : EXPECT_TRUE(instrumenter_->ParseCommandLine(&cmd_line_));
182 :
183 : EXPECT_EQ(EntryThunkInstrumenter::PROFILE,
184 E : instrumenter_->instrumentation_mode_);
185 E : EXPECT_EQ(abs_input_image_path_, instrumenter_->input_image_path_);
186 E : EXPECT_EQ(output_image_path_, instrumenter_->output_image_path_);
187 :
188 : EXPECT_EQ(std::string(TestEntryThunkInstrumenter::kAgentDllProfile),
189 E : instrumenter_->agent_dll_);
190 :
191 E : EXPECT_FALSE(instrumenter_->allow_overwrite_);
192 E : EXPECT_FALSE(instrumenter_->no_augment_pdb_);
193 E : EXPECT_FALSE(instrumenter_->no_strip_strings_);
194 E : EXPECT_FALSE(instrumenter_->debug_friendly_);
195 E : EXPECT_FALSE(instrumenter_->thunk_imports_);
196 E : EXPECT_FALSE(instrumenter_->instrument_unsafe_references_);
197 E : EXPECT_FALSE(instrumenter_->module_entry_only_);
198 E : }
199 :
200 E : TEST_F(EntryThunkInstrumenterTest, ParseFullProfile) {
201 E : SetUpValidCommandLine();
202 : instrumenter_.reset(
203 E : new TestEntryThunkInstrumenter(EntryThunkInstrumenter::PROFILE));
204 E : cmd_line_.AppendSwitchASCII("agent", "foo.dll");
205 E : cmd_line_.AppendSwitch("debug-friendly");
206 E : cmd_line_.AppendSwitchPath("input-pdb", input_pdb_path_);
207 E : cmd_line_.AppendSwitch("no-augment-pdb");
208 E : cmd_line_.AppendSwitch("no-strip-strings");
209 E : cmd_line_.AppendSwitchPath("output-pdb", output_pdb_path_);
210 E : cmd_line_.AppendSwitch("overwrite");
211 E : cmd_line_.AppendSwitch("instrument-imports");
212 :
213 E : EXPECT_TRUE(instrumenter_->ParseCommandLine(&cmd_line_));
214 :
215 : EXPECT_EQ(EntryThunkInstrumenter::PROFILE,
216 E : instrumenter_->instrumentation_mode_);
217 E : EXPECT_EQ(abs_input_image_path_, instrumenter_->input_image_path_);
218 E : EXPECT_EQ(output_image_path_, instrumenter_->output_image_path_);
219 E : EXPECT_EQ(abs_input_pdb_path_, instrumenter_->input_pdb_path_);
220 E : EXPECT_EQ(output_pdb_path_, instrumenter_->output_pdb_path_);
221 E : EXPECT_EQ(std::string("foo.dll"), instrumenter_->agent_dll_);
222 E : EXPECT_TRUE(instrumenter_->allow_overwrite_);
223 E : EXPECT_TRUE(instrumenter_->no_augment_pdb_);
224 E : EXPECT_TRUE(instrumenter_->no_strip_strings_);
225 E : EXPECT_TRUE(instrumenter_->debug_friendly_);
226 E : EXPECT_TRUE(instrumenter_->thunk_imports_);
227 E : }
228 :
229 E : TEST_F(EntryThunkInstrumenterTest, InstrumentImplCallTrace) {
230 E : SetUpValidCommandLine();
231 : instrumenter_.reset(
232 E : new TestEntryThunkInstrumenter(EntryThunkInstrumenter::CALL_TRACE));
233 :
234 E : EXPECT_TRUE(instrumenter_->ParseCommandLine(&cmd_line_));
235 E : EXPECT_TRUE(instrumenter_->InstrumentPrepare());
236 E : EXPECT_TRUE(instrumenter_->CreateRelinker());
237 E : EXPECT_TRUE(instrumenter_->InstrumentImpl());
238 E : }
239 :
240 E : TEST_F(EntryThunkInstrumenterTest, InstrumentImplProfile) {
241 E : SetUpValidCommandLine();
242 : instrumenter_.reset(
243 E : new TestEntryThunkInstrumenter(EntryThunkInstrumenter::PROFILE));
244 :
245 E : EXPECT_TRUE(instrumenter_->ParseCommandLine(&cmd_line_));
246 E : EXPECT_TRUE(instrumenter_->InstrumentPrepare());
247 E : EXPECT_TRUE(instrumenter_->CreateRelinker());
248 E : EXPECT_TRUE(instrumenter_->InstrumentImpl());
249 E : }
250 :
251 : } // namespace instrumenters
252 : } // namespace instrument
|