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/asan_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/image_filter.h"
21 : #include "syzygy/pe/unittest_util.h"
22 :
23 : namespace instrument {
24 : namespace instrumenters {
25 :
26 : namespace {
27 :
28 : class TestAsanInstrumenter : public AsanInstrumenter {
29 : public:
30 : using AsanInstrumenter::agent_dll_;
31 : using AsanInstrumenter::input_image_path_;
32 : using AsanInstrumenter::input_pdb_path_;
33 : using AsanInstrumenter::use_interceptors_;
34 : using AsanInstrumenter::output_image_path_;
35 : using AsanInstrumenter::output_pdb_path_;
36 : using AsanInstrumenter::allow_overwrite_;
37 : using AsanInstrumenter::old_decomposer_;
38 : using AsanInstrumenter::no_augment_pdb_;
39 : using AsanInstrumenter::no_strip_strings_;
40 : using AsanInstrumenter::filter_path_;
41 : using AsanInstrumenter::debug_friendly_;
42 : using AsanInstrumenter::use_liveness_analysis_;
43 : using AsanInstrumenter::remove_redundant_checks_;
44 : using AsanInstrumenter::kAgentDllAsan;
45 : using AsanInstrumenter::InstrumentImpl;
46 : using InstrumenterWithAgent::CreateRelinker;
47 :
48 E : TestAsanInstrumenter() {
49 : // Call the GetPERelinker function to initialize it.
50 E : EXPECT_TRUE(GetPERelinker() != NULL);
51 E : }
52 : };
53 :
54 : class AsanInstrumenterTest : public testing::PELibUnitTest {
55 : public:
56 : typedef testing::PELibUnitTest Super;
57 :
58 E : AsanInstrumenterTest()
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 : test_dll_filter_path_ = temp_dir_.Append(L"test_dll_filter.json");
84 E : dummy_filter_path_ = temp_dir_.Append(L"dummy_filter.json");
85 E : }
86 :
87 E : void SetUpValidCommandLine() {
88 E : cmd_line_.AppendSwitchPath("input-image", input_image_path_);
89 E : cmd_line_.AppendSwitchPath("output-image", output_image_path_);
90 E : }
91 :
92 : protected:
93 E : void MakeFilters() {
94 : // Create a valid test_dll filter. Just so it's not empty we mark the NT
95 : // headers as non-instrumentable.
96 E : pe::ImageFilter filter;
97 E : ASSERT_TRUE(filter.Init(abs_input_image_path_));
98 : filter.filter.Mark(pe::ImageFilter::RelativeAddressFilter::Range(
99 E : core::RelativeAddress(0), 4096));
100 E : ASSERT_TRUE(filter.SaveToJSON(false, test_dll_filter_path_));
101 :
102 : // Muck up the time date stamp and create an invalid filter.
103 E : filter.signature.module_time_date_stamp ^= 0x0F00BA55;
104 E : ASSERT_TRUE(filter.SaveToJSON(true, dummy_filter_path_));
105 E : }
106 :
107 : base::FilePath temp_dir_;
108 :
109 : // @name The redirected streams paths.
110 : // @{
111 : base::FilePath stdin_path_;
112 : base::FilePath stdout_path_;
113 : base::FilePath stderr_path_;
114 : // @}
115 :
116 : // @name Command-line and parameters.
117 : // @{
118 : CommandLine cmd_line_;
119 : base::FilePath input_image_path_;
120 : base::FilePath input_pdb_path_;
121 : base::FilePath output_image_path_;
122 : base::FilePath output_pdb_path_;
123 : base::FilePath test_dll_filter_path_;
124 : base::FilePath dummy_filter_path_;
125 : // @}
126 :
127 : // @name Expected final values of input parameters.
128 : // @{
129 : base::FilePath abs_input_image_path_;
130 : base::FilePath abs_input_pdb_path_;
131 : // @}
132 :
133 : // The fake instrumenter we delegate to.
134 : TestAsanInstrumenter instrumenter_;
135 : };
136 :
137 : } // namespace
138 :
139 E : TEST_F(AsanInstrumenterTest, ParseMinimalAsan) {
140 E : SetUpValidCommandLine();
141 :
142 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
143 :
144 E : EXPECT_EQ(abs_input_image_path_, instrumenter_.input_image_path_);
145 E : EXPECT_EQ(output_image_path_, instrumenter_.output_image_path_);
146 : EXPECT_EQ(std::string(TestAsanInstrumenter::kAgentDllAsan),
147 E : instrumenter_.agent_dll_);
148 E : EXPECT_FALSE(instrumenter_.allow_overwrite_);
149 E : EXPECT_FALSE(instrumenter_.old_decomposer_);
150 E : EXPECT_FALSE(instrumenter_.no_augment_pdb_);
151 E : EXPECT_FALSE(instrumenter_.no_strip_strings_);
152 E : EXPECT_FALSE(instrumenter_.debug_friendly_);
153 E : EXPECT_TRUE(instrumenter_.use_interceptors_);
154 E : EXPECT_TRUE(instrumenter_.use_liveness_analysis_);
155 E : EXPECT_TRUE(instrumenter_.remove_redundant_checks_);
156 E : }
157 :
158 E : TEST_F(AsanInstrumenterTest, ParseFullAsan) {
159 E : SetUpValidCommandLine();
160 E : cmd_line_.AppendSwitchPath("filter", test_dll_filter_path_);
161 E : cmd_line_.AppendSwitchASCII("agent", "foo.dll");
162 E : cmd_line_.AppendSwitch("debug-friendly");
163 E : cmd_line_.AppendSwitchPath("input-pdb", input_pdb_path_);
164 E : cmd_line_.AppendSwitch("old-decomposer");
165 E : cmd_line_.AppendSwitch("no-augment-pdb");
166 E : cmd_line_.AppendSwitch("no-interceptors");
167 E : cmd_line_.AppendSwitch("no-strip-strings");
168 E : cmd_line_.AppendSwitchPath("output-pdb", output_pdb_path_);
169 E : cmd_line_.AppendSwitch("overwrite");
170 E : cmd_line_.AppendSwitch("no-liveness-analysis");
171 E : cmd_line_.AppendSwitch("no-redundancy-analysis");
172 :
173 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
174 :
175 E : EXPECT_EQ(abs_input_image_path_, instrumenter_.input_image_path_);
176 E : EXPECT_EQ(output_image_path_, instrumenter_.output_image_path_);
177 E : EXPECT_EQ(abs_input_pdb_path_, instrumenter_.input_pdb_path_);
178 E : EXPECT_EQ(output_pdb_path_, instrumenter_.output_pdb_path_);
179 E : EXPECT_EQ(test_dll_filter_path_, instrumenter_.filter_path_);
180 E : EXPECT_EQ(std::string("foo.dll"), instrumenter_.agent_dll_);
181 E : EXPECT_TRUE(instrumenter_.allow_overwrite_);
182 E : EXPECT_TRUE(instrumenter_.old_decomposer_);
183 E : EXPECT_TRUE(instrumenter_.no_augment_pdb_);
184 E : EXPECT_TRUE(instrumenter_.no_strip_strings_);
185 E : EXPECT_TRUE(instrumenter_.debug_friendly_);
186 E : EXPECT_FALSE(instrumenter_.use_interceptors_);
187 E : EXPECT_FALSE(instrumenter_.use_liveness_analysis_);
188 E : EXPECT_FALSE(instrumenter_.remove_redundant_checks_);
189 E : }
190 :
191 E : TEST_F(AsanInstrumenterTest, InstrumentImpl) {
192 E : SetUpValidCommandLine();
193 :
194 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
195 E : EXPECT_TRUE(instrumenter_.CreateRelinker());
196 E : EXPECT_TRUE(instrumenter_.InstrumentImpl());
197 E : }
198 :
199 E : TEST_F(AsanInstrumenterTest, FailsWithInvalidFilter) {
200 E : cmd_line_.AppendSwitchPath("input-image", input_image_path_);
201 E : cmd_line_.AppendSwitchPath("output-image", output_image_path_);
202 E : cmd_line_.AppendSwitchPath("filter", dummy_filter_path_);
203 :
204 : // We don't expect the relinker to be called at all, as before we get that far
205 : // the filter will be identified as being for the wrong module.
206 :
207 E : MakeFilters();
208 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
209 E : EXPECT_TRUE(instrumenter_.CreateRelinker());
210 E : EXPECT_FALSE(instrumenter_.InstrumentImpl());
211 E : }
212 :
213 E : TEST_F(AsanInstrumenterTest, SucceedsWithValidFilter) {
214 E : cmd_line_.AppendSwitchPath("input-image", input_image_path_);
215 E : cmd_line_.AppendSwitchPath("output-image", output_image_path_);
216 E : cmd_line_.AppendSwitchPath("filter", test_dll_filter_path_);
217 :
218 E : MakeFilters();
219 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
220 E : EXPECT_TRUE(instrumenter_.CreateRelinker());
221 E : EXPECT_TRUE(instrumenter_.InstrumentImpl());
222 E : }
223 :
224 : } // namespace instrumenters
225 : } // namespace instrument
|