1 : // Copyright 2012 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/grinder/line_info.h"
16 :
17 : #include "base/win/scoped_com_initializer.h"
18 : #include "gmock/gmock.h"
19 : #include "gtest/gtest.h"
20 : #include "syzygy/core/unittest_util.h"
21 : #include "syzygy/pe/unittest_util.h"
22 :
23 : namespace grinder {
24 :
25 : namespace {
26 :
27 : class TestLineInfo : public LineInfo {
28 : public:
29 : using LineInfo::source_files_;
30 : using LineInfo::source_lines_;
31 :
32 E : void ResetVisitedLines() {
33 E : for (size_t i = 0; i < source_lines_.size(); ++i) {
34 E : source_lines_[i].visit_count = 0;
35 E : }
36 E : }
37 :
38 E : void GetVisitedLines(std::vector<size_t>* visited_lines) const {
39 E : DCHECK(visited_lines != NULL);
40 E : visited_lines->clear();
41 E : for (size_t i = 0; i < source_lines_.size(); ++i) {
42 E : if (source_lines_[i].visit_count > 0)
43 E : visited_lines->push_back(source_lines_[i].line_number);
44 E : }
45 E : }
46 : };
47 :
48 : class LineInfoTest : public testing::Test {
49 : public:
50 E : virtual void SetUp() override {
51 E : testing::Test::SetUp();
52 :
53 E : pdb_path_ = testing::GetExeTestDataRelativePath(
54 : testing::kCoverageInstrumentedTestDllPdbName);
55 :
56 :
57 E : std::wstring static_pdb_path(
58 : L"syzygy/grinder/test_data/coverage_instrumented_test_dll.pdb");
59 E : static_pdb_path_ = testing::GetSrcRelativePath(static_pdb_path.c_str());
60 E : }
61 :
62 : // Ensures that COM is initialized for tests in this fixture.
63 : base::win::ScopedCOMInitializer com_initializer_;
64 :
65 : base::FilePath pdb_path_;
66 : base::FilePath static_pdb_path_;
67 : };
68 :
69 : void PushBackSourceLine(TestLineInfo* line_info,
70 : const std::string* source_file_name,
71 : size_t line_number,
72 : uint32_t address,
73 E : size_t size) {
74 E : DCHECK(line_info != NULL);
75 E : line_info->source_lines_.push_back(LineInfo::SourceLine(
76 : source_file_name,
77 : line_number,
78 : core::RelativeAddress(address),
79 : size));
80 E : }
81 :
82 : #define EXPECT_LINES_VISITED(line_info, ...) \
83 : { \
84 : const size_t kLineNumbers[] = { __VA_ARGS__ }; \
85 : std::vector<size_t> visited, expected; \
86 : expected.assign(kLineNumbers, kLineNumbers + arraysize(kLineNumbers)); \
87 : line_info.GetVisitedLines(&visited); \
88 : std::sort(expected.begin(), expected.end()); \
89 : std::sort(visited.begin(), visited.end()); \
90 : EXPECT_THAT(expected, ::testing::ContainerEq(visited)); \
91 : }
92 :
93 : #define EXPECT_NO_LINES_VISITED(line_info) \
94 : { \
95 : std::vector<size_t> visited; \
96 : line_info.GetVisitedLines(&visited); \
97 : EXPECT_EQ(0u, visited.size()); \
98 : }
99 :
100 : } // namespace
101 :
102 E : TEST_F(LineInfoTest, InitDynamicPdb) {
103 E : TestLineInfo line_info;
104 E : EXPECT_TRUE(line_info.Init(pdb_path_));
105 E : }
106 :
107 E : TEST_F(LineInfoTest, InitStaticPdb) {
108 E : TestLineInfo line_info;
109 E : EXPECT_TRUE(line_info.Init(static_pdb_path_));
110 :
111 : // The expected values were taken by running "pdb_dumper --dump-modules
112 : // syzygy/grinder/test_data/coverage_instrumented_test_dll.pdb" and running
113 : // through the following filters:
114 : // grep "line at" | sed 's/(.*$//' | uniq | sort | uniq | wc -l
115 E : EXPECT_EQ(138u, line_info.source_files().size());
116 : // grep "line at" | wc -l
117 E : EXPECT_EQ(8379u, line_info.source_lines().size());
118 E : }
119 :
120 E : TEST_F(LineInfoTest, Visit) {
121 E : TestLineInfo line_info;
122 :
123 : // Create a single dummy source file.
124 E : std::string source_file("foo.cc");
125 :
126 : // The first two entries have identical ranges, and map multiple lines to
127 : // those ranges.
128 E : PushBackSourceLine(&line_info, &source_file, 1, 4096, 2);
129 E : PushBackSourceLine(&line_info, &source_file, 2, 4096, 2);
130 E : PushBackSourceLine(&line_info, &source_file, 3, 4098, 2);
131 E : PushBackSourceLine(&line_info, &source_file, 5, 4100, 2);
132 : // Leave a gap between these two entries.
133 E : PushBackSourceLine(&line_info, &source_file, 6, 4104, 6);
134 E : PushBackSourceLine(&line_info, &source_file, 7, 4110, 2);
135 :
136 : // So, our line info looks like this:
137 : // 1,2 3 5 6 7 <-- line numbers
138 : // +----+----+----+----+----+----+
139 : // |0,1 | 2 | 3 |gap | 4 | 5 | <-- source_lines_ indices
140 : // +----+----+----+----+----+----+
141 : // 4096 4098 4100 4102 4104 4110 4112 <-- address ranges
142 :
143 : // Visit a repeated BB (multiple lines).
144 E : EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4096), 2, 1));
145 E : EXPECT_LINES_VISITED(line_info, 1, 2);
146 :
147 : // Visit a range spanning multiple BBs (we don't reset the previously
148 : // visited lines to ensure that stats are kept correctly across multiple
149 : // calls to LineInfo::Visit).
150 E : EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4098), 4, 1));
151 E : EXPECT_LINES_VISITED(line_info, 1, 2, 3, 5);
152 :
153 : // Visit a gap and no blocks.
154 E : line_info.ResetVisitedLines();
155 E : EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4102), 2, 1));
156 E : EXPECT_NO_LINES_VISITED(line_info);
157 :
158 : // Visit a range spanning a gap (at the left) and a BB.
159 E : line_info.ResetVisitedLines();
160 E : EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4102), 8, 1));
161 E : EXPECT_LINES_VISITED(line_info, 6);
162 :
163 : // Visit a range spanning a gap (at the right) and a BB.
164 E : line_info.ResetVisitedLines();
165 E : EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4100), 4, 1));
166 E : EXPECT_LINES_VISITED(line_info, 5);
167 :
168 : // Visit a range spanning 2 BBs with a gap in the middle.
169 E : line_info.ResetVisitedLines();
170 E : EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4100), 10, 1));
171 E : EXPECT_LINES_VISITED(line_info, 5, 6);
172 :
173 : // Visit a range only partially spanning a single BB.
174 E : line_info.ResetVisitedLines();
175 E : EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4100), 1, 1));
176 E : EXPECT_LINES_VISITED(line_info, 5);
177 :
178 : // Visit a range partially spanning a BB on the left.
179 E : line_info.ResetVisitedLines();
180 E : EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4108), 4, 1));
181 E : EXPECT_LINES_VISITED(line_info, 6, 7);
182 :
183 : // Visit a range partially spanning a BB on the right.
184 E : line_info.ResetVisitedLines();
185 E : EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4104), 7, 1));
186 E : EXPECT_LINES_VISITED(line_info, 6, 7);
187 E : }
188 :
189 E : TEST_F(LineInfoTest, VisitCounterWorks) {
190 E : TestLineInfo line_info;
191 :
192 : // Create a single dummy source file.
193 E : std::string source_file("foo.cc");
194 :
195 : // Add a source line.
196 E : PushBackSourceLine(&line_info, &source_file, 1, 4096, 2);
197 : LineInfo::SourceLines::const_iterator line_it =
198 E : line_info.source_lines().begin();
199 E : EXPECT_EQ(0u, line_it->visit_count);
200 :
201 E : EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4096), 2, 1));
202 E : EXPECT_EQ(1u, line_it->visit_count);
203 :
204 E : EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4096), 2, 2));
205 E : EXPECT_EQ(3u, line_it->visit_count);
206 :
207 : // Ensure our saturation addition works by trying to overflow.
208 E : EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4096), 2, 0xffffffff));
209 E : EXPECT_EQ(0xffffffff, line_it->visit_count);
210 E : EXPECT_TRUE(line_info.Visit(core::RelativeAddress(4096), 2, 10));
211 E : EXPECT_EQ(0xffffffff, line_it->visit_count);
212 E : }
213 :
214 : } // namespace grinder
|