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/unittest_util.h"
21 :
22 : namespace instrument {
23 : namespace instrumenters {
24 :
25 : namespace {
26 :
27 : class TestAsanInstrumenter : public AsanInstrumenter {
28 : public:
29 : using AsanInstrumenter::agent_dll_;
30 : using AsanInstrumenter::input_dll_path_;
31 : using AsanInstrumenter::input_pdb_path_;
32 : using AsanInstrumenter::output_dll_path_;
33 : using AsanInstrumenter::output_pdb_path_;
34 : using AsanInstrumenter::allow_overwrite_;
35 : using AsanInstrumenter::new_decomposer_;
36 : using AsanInstrumenter::no_augment_pdb_;
37 : using AsanInstrumenter::no_parse_debug_info_;
38 : using AsanInstrumenter::no_strip_strings_;
39 : using AsanInstrumenter::filter_path_;
40 : using AsanInstrumenter::debug_friendly_;
41 : using AsanInstrumenter::use_liveness_analysis_;
42 : using AsanInstrumenter::remove_redundant_checks_;
43 : using AsanInstrumenter::kAgentDllAsan;
44 : };
45 :
46 : class AsanInstrumenterTest : public testing::PELibUnitTest {
47 : public:
48 : typedef testing::PELibUnitTest Super;
49 :
50 E : AsanInstrumenterTest()
51 : : cmd_line_(base::FilePath(L"instrument.exe")) {
52 E : }
53 :
54 E : virtual void SetUp() OVERRIDE {
55 E : testing::Test::SetUp();
56 :
57 : // Several of the tests generate progress and (deliberate) error messages
58 : // that would otherwise clutter the unittest output.
59 E : logging::SetMinLogLevel(logging::LOG_FATAL);
60 :
61 : // Setup the IO streams.
62 E : CreateTemporaryDir(&temp_dir_);
63 E : stdin_path_ = temp_dir_.Append(L"NUL");
64 E : stdout_path_ = temp_dir_.Append(L"stdout.txt");
65 E : stderr_path_ = temp_dir_.Append(L"stderr.txt");
66 E : InitStreams(stdin_path_, stdout_path_, stderr_path_);
67 :
68 : // Initialize the (potential) input and output path values.
69 E : abs_input_dll_path_ = testing::GetExeRelativePath(testing::kTestDllName);
70 E : input_dll_path_ = testing::GetRelativePath(abs_input_dll_path_);
71 E : abs_input_pdb_path_ = testing::GetExeRelativePath(testing::kTestDllPdbName);
72 E : input_pdb_path_ = testing::GetRelativePath(abs_input_pdb_path_);
73 E : output_dll_path_ = temp_dir_.Append(input_dll_path_.BaseName());
74 E : output_pdb_path_ = temp_dir_.Append(input_pdb_path_.BaseName());
75 E : }
76 :
77 E : void SetUpValidCommandLine() {
78 E : cmd_line_.AppendSwitchPath("input-dll", input_dll_path_);
79 E : cmd_line_.AppendSwitchPath("output-dll", output_dll_path_);
80 E : }
81 :
82 : protected:
83 : base::FilePath temp_dir_;
84 :
85 : // @name The redirected streams paths.
86 : // @{
87 : base::FilePath stdin_path_;
88 : base::FilePath stdout_path_;
89 : base::FilePath stderr_path_;
90 : // @}
91 :
92 : // @name Command-line and parameters.
93 : // @{
94 : CommandLine cmd_line_;
95 : base::FilePath input_dll_path_;
96 : base::FilePath input_pdb_path_;
97 : base::FilePath output_dll_path_;
98 : base::FilePath output_pdb_path_;
99 : base::FilePath test_dll_filter_path_;
100 : // @}
101 :
102 : // @name Expected final values of input parameters.
103 : // @{
104 : base::FilePath abs_input_dll_path_;
105 : base::FilePath abs_input_pdb_path_;
106 : // @}
107 :
108 : // The fake instrumenter we delegate to.
109 : TestAsanInstrumenter instrumenter_;
110 : };
111 :
112 : } // namespace
113 :
114 E : TEST_F(AsanInstrumenterTest, ParseMinimalAsan) {
115 E : SetUpValidCommandLine();
116 :
117 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
118 :
119 E : EXPECT_EQ(abs_input_dll_path_, instrumenter_.input_dll_path_);
120 E : EXPECT_EQ(output_dll_path_, instrumenter_.output_dll_path_);
121 : EXPECT_EQ(std::string(TestAsanInstrumenter::kAgentDllAsan),
122 E : instrumenter_.agent_dll_);
123 E : EXPECT_FALSE(instrumenter_.allow_overwrite_);
124 E : EXPECT_FALSE(instrumenter_.new_decomposer_);
125 E : EXPECT_FALSE(instrumenter_.no_augment_pdb_);
126 E : EXPECT_FALSE(instrumenter_.no_parse_debug_info_);
127 E : EXPECT_FALSE(instrumenter_.no_strip_strings_);
128 E : EXPECT_FALSE(instrumenter_.debug_friendly_);
129 E : EXPECT_FALSE(instrumenter_.use_liveness_analysis_);
130 E : EXPECT_FALSE(instrumenter_.remove_redundant_checks_);
131 E : }
132 :
133 E : TEST_F(AsanInstrumenterTest, ParseFullAsan) {
134 E : SetUpValidCommandLine();
135 E : cmd_line_.AppendSwitchPath("filter", test_dll_filter_path_);
136 E : cmd_line_.AppendSwitchASCII("agent", "foo.dll");
137 E : cmd_line_.AppendSwitch("debug-friendly");
138 E : cmd_line_.AppendSwitchPath("input-pdb", input_pdb_path_);
139 E : cmd_line_.AppendSwitch("new-decomposer");
140 E : cmd_line_.AppendSwitch("no-augment-pdb");
141 E : cmd_line_.AppendSwitch("no-parse-debug-info");
142 E : cmd_line_.AppendSwitch("no-strip-strings");
143 E : cmd_line_.AppendSwitchPath("output-pdb", output_pdb_path_);
144 E : cmd_line_.AppendSwitch("overwrite");
145 E : cmd_line_.AppendSwitch("use-liveness-analysis");
146 E : cmd_line_.AppendSwitch("remove-redundant-checks");
147 :
148 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
149 :
150 E : EXPECT_EQ(abs_input_dll_path_, instrumenter_.input_dll_path_);
151 E : EXPECT_EQ(output_dll_path_, instrumenter_.output_dll_path_);
152 E : EXPECT_EQ(abs_input_pdb_path_, instrumenter_.input_pdb_path_);
153 E : EXPECT_EQ(output_pdb_path_, instrumenter_.output_pdb_path_);
154 E : EXPECT_EQ(test_dll_filter_path_, instrumenter_.filter_path_);
155 E : EXPECT_EQ(std::string("foo.dll"), instrumenter_.agent_dll_);
156 E : EXPECT_TRUE(instrumenter_.allow_overwrite_);
157 E : EXPECT_TRUE(instrumenter_.new_decomposer_);
158 E : EXPECT_TRUE(instrumenter_.no_augment_pdb_);
159 E : EXPECT_TRUE(instrumenter_.no_parse_debug_info_);
160 E : EXPECT_TRUE(instrumenter_.no_strip_strings_);
161 E : EXPECT_TRUE(instrumenter_.debug_friendly_);
162 E : EXPECT_TRUE(instrumenter_.use_liveness_analysis_);
163 E : EXPECT_TRUE(instrumenter_.remove_redundant_checks_);
164 E : }
165 :
166 E : TEST_F(AsanInstrumenterTest, InstrumentImpl) {
167 E : SetUpValidCommandLine();
168 :
169 E : EXPECT_TRUE(instrumenter_.ParseCommandLine(&cmd_line_));
170 E : EXPECT_TRUE(instrumenter_.Instrument());
171 E : }
172 :
173 : } // namespace instrumenters
174 : } // namespace instrument
|