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