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