xref: /aosp_15_r20/external/google-breakpad/src/common/dwarf_line_to_module_unittest.cc (revision 9712c20fc9bbfbac4935993a2ca0b3958c5adad2)
1 // Copyright 2010 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 //     * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //     * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 //     * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 // Original author: Jim Blandy <[email protected]> <[email protected]>
30 
31 // dwarf_line_to_module.cc: Unit tests for google_breakpad::DwarfLineToModule.
32 
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>  // Must come first
35 #endif
36 
37 #include <vector>
38 
39 #include "breakpad_googletest_includes.h"
40 #include "common/dwarf_line_to_module.h"
41 
42 using std::vector;
43 
44 using google_breakpad::DwarfLineToModule;
45 using google_breakpad::Module;
46 using google_breakpad::Module;
47 
TEST(SimpleModule,One)48 TEST(SimpleModule, One) {
49   Module m("name", "os", "architecture", "id");
50   vector<Module::Line> lines;
51   std::map<uint32_t, Module::File*> cu_files;
52   DwarfLineToModule h(&m, "/", &lines, &cu_files);
53 
54   h.DefineFile("file1", 0x30bf0f27, 0, 0, 0);
55   h.AddLine(0x6fd126fbf74f2680LL, 0x63c9a14cf556712bLL, 0x30bf0f27,
56             0x4c090cbf, 0x1cf9fe0d);
57 
58   vector<Module::File*> files;
59   m.GetFiles(&files);
60   EXPECT_EQ(1U, files.size());
61   EXPECT_STREQ("/file1", files[0]->name.c_str());
62 
63   EXPECT_EQ(1U, lines.size());
64   EXPECT_EQ(0x6fd126fbf74f2680ULL, lines[0].address);
65   EXPECT_EQ(0x63c9a14cf556712bULL, lines[0].size);
66   EXPECT_TRUE(lines[0].file == files[0]);
67   EXPECT_EQ(0x4c090cbf, lines[0].number);
68 }
69 
TEST(SimpleModule,Many)70 TEST(SimpleModule, Many) {
71   Module m("name", "os", "architecture", "id");
72   vector<Module::Line> lines;
73   std::map<uint32_t, Module::File*> cu_files;
74   DwarfLineToModule h(&m, "/", &lines, &cu_files);
75 
76   h.DefineDir("directory1", 0x838299ab);
77   h.DefineDir("directory2", 0xf85de023);
78   h.DefineFile("file1", 0x2b80377a, 0x838299ab, 0, 0);
79   h.DefineFile("file1", 0x63beb4a4, 0xf85de023, 0, 0);
80   h.DefineFile("file2", 0x1d161d56, 0x838299ab, 0, 0);
81   h.DefineFile("file2", 0x1e7a667c, 0xf85de023, 0, 0);
82   h.AddLine(0x69900c5d553b7274ULL, 0x90fded183f0d0d3cULL, 0x2b80377a,
83             0x15b0f0a9U, 0x3ff5abd6U);
84   h.AddLine(0x45811219a39b7101ULL, 0x25a5e6a924afc41fULL, 0x63beb4a4,
85             0x4d259ce9U, 0x41c5ee32U);
86   h.AddLine(0xfa90514c1dc9704bULL, 0x0063efeabc02f313ULL, 0x1d161d56,
87             0x1ee9fa4fU, 0xbf70e46aU);
88   h.AddLine(0x556b55fb6a647b10ULL, 0x3f3089ca2bfd80f5ULL, 0x1e7a667c,
89             0x77fc280eU, 0x2c4a728cU);
90   h.DefineFile("file3", -1, 0, 0, 0);
91   h.AddLine(0xe2d72a37f8d9403aULL, 0x034dfab5b0d4d236ULL, 0x63beb4a5,
92             0x75047044U, 0xb6a0016cU);
93 
94   vector<Module::File*> files;
95   m.GetFiles(&files);
96   ASSERT_EQ(5U, files.size());
97   EXPECT_STREQ("/directory1/file1", files[0]->name.c_str());
98   EXPECT_STREQ("/directory1/file2", files[1]->name.c_str());
99   EXPECT_STREQ("/directory2/file1", files[2]->name.c_str());
100   EXPECT_STREQ("/directory2/file2", files[3]->name.c_str());
101   EXPECT_STREQ("/file3",            files[4]->name.c_str());
102 
103   ASSERT_EQ(5U, lines.size());
104 
105   EXPECT_EQ(0x69900c5d553b7274ULL, lines[0].address);
106   EXPECT_EQ(0x90fded183f0d0d3cULL, lines[0].size);
107   EXPECT_TRUE(lines[0].file == files[0]);
108   EXPECT_EQ(0x15b0f0a9, lines[0].number);
109 
110   EXPECT_EQ(0x45811219a39b7101ULL, lines[1].address);
111   EXPECT_EQ(0x25a5e6a924afc41fULL, lines[1].size);
112   EXPECT_TRUE(lines[1].file == files[2]);
113   EXPECT_EQ(0x4d259ce9, lines[1].number);
114 
115   EXPECT_EQ(0xfa90514c1dc9704bULL, lines[2].address);
116   EXPECT_EQ(0x0063efeabc02f313ULL, lines[2].size);
117   EXPECT_TRUE(lines[2].file == files[1]);
118   EXPECT_EQ(0x1ee9fa4f, lines[2].number);
119 
120   EXPECT_EQ(0x556b55fb6a647b10ULL, lines[3].address);
121   EXPECT_EQ(0x3f3089ca2bfd80f5ULL, lines[3].size);
122   EXPECT_TRUE(lines[3].file == files[3]);
123   EXPECT_EQ(0x77fc280e, lines[3].number);
124 
125   EXPECT_EQ(0xe2d72a37f8d9403aULL, lines[4].address);
126   EXPECT_EQ(0x034dfab5b0d4d236ULL, lines[4].size);
127   EXPECT_TRUE(lines[4].file == files[4]);
128   EXPECT_EQ(0x75047044, lines[4].number);
129 }
130 
TEST(Filenames,Absolute)131 TEST(Filenames, Absolute) {
132   Module m("name", "os", "architecture", "id");
133   vector<Module::Line> lines;
134   std::map<uint32_t, Module::File*> cu_files;
135   DwarfLineToModule h(&m, "/", &lines, &cu_files);
136 
137   h.DefineDir("directory1", 1);
138   h.DefineFile("/absolute", 1, 1, 0, 0);
139 
140   h.AddLine(1, 1, 1, 0, 0);
141 
142   vector<Module::File*> files;
143   m.GetFiles(&files);
144   ASSERT_EQ(1U, files.size());
145   EXPECT_STREQ("/absolute", files[0]->name.c_str());
146   ASSERT_EQ(1U, lines.size());
147   EXPECT_TRUE(lines[0].file == files[0]);
148 }
149 
TEST(Filenames,Relative)150 TEST(Filenames, Relative) {
151   Module m("name", "os", "architecture", "id");
152   vector<Module::Line> lines;
153   std::map<uint32_t, Module::File*> cu_files;
154   DwarfLineToModule h(&m, "/", &lines, &cu_files);
155 
156   h.DefineDir("directory1", 1);
157   h.DefineFile("relative", 1, 1, 0, 0);
158 
159   h.AddLine(1, 1, 1, 0, 0);
160 
161   vector<Module::File*> files;
162   m.GetFiles(&files);
163   ASSERT_EQ(1U, files.size());
164   EXPECT_STREQ("/directory1/relative", files[0]->name.c_str());
165   ASSERT_EQ(1U, lines.size());
166   EXPECT_TRUE(lines[0].file == files[0]);
167 }
168 
TEST(Filenames,StrangeFile)169 TEST(Filenames, StrangeFile) {
170   Module m("name", "os", "architecture", "id");
171   vector<Module::Line> lines;
172   std::map<uint32_t, Module::File*> cu_files;
173   DwarfLineToModule h(&m, "/", &lines, &cu_files);
174 
175   h.DefineDir("directory1", 1);
176   h.DefineFile("", 1, 1, 0, 0);
177   h.AddLine(1, 1, 1, 0, 0);
178 
179   ASSERT_EQ(1U, lines.size());
180   EXPECT_STREQ("/directory1/", lines[0].file->name.c_str());
181 }
182 
TEST(Filenames,StrangeDirectory)183 TEST(Filenames, StrangeDirectory) {
184   Module m("name", "os", "architecture", "id");
185   vector<Module::Line> lines;
186   std::map<uint32_t, Module::File*> cu_files;
187   DwarfLineToModule h(&m, "/", &lines, &cu_files);
188 
189   h.DefineDir("", 1);
190   h.DefineFile("file1", 1, 1, 0, 0);
191   h.AddLine(1, 1, 1, 0, 0);
192 
193   ASSERT_EQ(1U, lines.size());
194   EXPECT_STREQ("/file1", lines[0].file->name.c_str());
195 }
196 
TEST(Filenames,StrangeDirectoryAndFile)197 TEST(Filenames, StrangeDirectoryAndFile) {
198   Module m("name", "os", "architecture", "id");
199   vector<Module::Line> lines;
200   std::map<uint32_t, Module::File*> cu_files;
201   DwarfLineToModule h(&m, "/", &lines, &cu_files);
202 
203   h.DefineDir("", 1);
204   h.DefineFile("", 1, 1, 0, 0);
205   h.AddLine(1, 1, 1, 0, 0);
206 
207   ASSERT_EQ(1U, lines.size());
208   EXPECT_STREQ("/", lines[0].file->name.c_str());
209 }
210 
211 // We should use the compilation directory when encountering a file for
212 // directory number zero.
TEST(Filenames,DirectoryZeroFileIsRelativeToCompilationDir)213 TEST(Filenames, DirectoryZeroFileIsRelativeToCompilationDir) {
214   Module m("name", "os", "architecture", "id");
215   vector<Module::Line> lines;
216   std::map<uint32_t, Module::File*> cu_files;
217   DwarfLineToModule h(&m, "src/build", &lines, &cu_files);
218 
219   h.DefineDir("Dir", 1);
220   h.DefineFile("File", 1, 0, 0, 0);
221 
222   h.AddLine(1, 1, 1, 0, 0);
223 
224   ASSERT_EQ(1U, lines.size());
225   EXPECT_STREQ("src/build/File", lines[0].file->name.c_str());
226 }
227 
228 // We should treat non-absolute directories as relative to the compilation
229 // directory.
TEST(Filenames,IncludeDirectoryRelativeToDirectoryZero)230 TEST(Filenames, IncludeDirectoryRelativeToDirectoryZero) {
231   Module m("name", "os", "architecture", "id");
232   vector<Module::Line> lines;
233   std::map<uint32_t, Module::File*> cu_files;
234   DwarfLineToModule h(&m, "src/build", &lines, &cu_files);
235 
236   h.DefineDir("Dir", 1);
237   h.DefineFile("File", 1, 1, 0, 0);
238 
239   h.AddLine(1, 1, 1, 0, 0);
240 
241   ASSERT_EQ(1U, lines.size());
242   EXPECT_STREQ("src/build/Dir/File", lines[0].file->name.c_str());
243 }
244 
245 // We should treat absolute directories as absolute, and not relative to
246 // the compilation dir.
TEST(Filenames,IncludeDirectoryAbsolute)247 TEST(Filenames, IncludeDirectoryAbsolute) {
248   Module m("name", "os", "architecture", "id");
249   vector<Module::Line> lines;
250   std::map<uint32_t, Module::File*> cu_files;
251   DwarfLineToModule h(&m, "src/build", &lines, &cu_files);
252 
253   h.DefineDir("/Dir", 1);
254   h.DefineFile("File", 1, 1, 0, 0);
255 
256   h.AddLine(1, 1, 1, 0, 0);
257 
258   ASSERT_EQ(1U, lines.size());
259   EXPECT_STREQ("/Dir/File", lines[0].file->name.c_str());
260 }
261 
262 // We should silently ignore attempts to define directory number zero,
263 // since that is always the compilation directory.
TEST(ModuleErrors,DirectoryZero)264 TEST(ModuleErrors, DirectoryZero) {
265   Module m("name", "os", "architecture", "id");
266   vector<Module::Line> lines;
267   std::map<uint32_t, Module::File*> cu_files;
268   DwarfLineToModule h(&m, "/", &lines, &cu_files);
269 
270   h.DefineDir("directory0", 0); // should be ignored
271   h.DefineFile("relative", 1, 0, 0, 0);
272 
273   h.AddLine(1, 1, 1, 0, 0);
274 
275   ASSERT_EQ(1U, lines.size());
276   EXPECT_STREQ("/relative", lines[0].file->name.c_str());
277 }
278 
279 // We should refuse to add lines with bogus file numbers. We should
280 // produce only one warning, however.
TEST(ModuleErrors,BadFileNumber)281 TEST(ModuleErrors, BadFileNumber) {
282   Module m("name", "os", "architecture", "id");
283   vector<Module::Line> lines;
284   std::map<uint32_t, Module::File*> cu_files;
285   DwarfLineToModule h(&m, "/", &lines, &cu_files);
286 
287   h.DefineFile("relative", 1, 0, 0, 0);
288   h.AddLine(1, 1, 2, 0, 0); // bad file number
289   h.AddLine(2, 1, 2, 0, 0); // bad file number (no duplicate warning)
290 
291   EXPECT_EQ(0U, lines.size());
292 }
293 
294 // We should treat files with bogus directory numbers as relative to
295 // the compilation unit.
TEST(ModuleErrors,BadDirectoryNumber)296 TEST(ModuleErrors, BadDirectoryNumber) {
297   Module m("name", "os", "architecture", "id");
298   vector<Module::Line> lines;
299   std::map<uint32_t, Module::File*> cu_files;
300   DwarfLineToModule h(&m, "/", &lines, &cu_files);
301 
302   h.DefineDir("directory1", 1);
303   h.DefineFile("baddirnumber1", 1, 2, 0, 0); // bad directory number
304   h.DefineFile("baddirnumber2", 2, 2, 0, 0); // bad dir number (no warning)
305   h.AddLine(1, 1, 1, 0, 0);
306 
307   ASSERT_EQ(1U, lines.size());
308   EXPECT_STREQ("baddirnumber1", lines[0].file->name.c_str());
309 }
310 
311 // We promise not to report empty lines.
TEST(ModuleErrors,EmptyLine)312 TEST(ModuleErrors, EmptyLine) {
313   Module m("name", "os", "architecture", "id");
314   vector<Module::Line> lines;
315   std::map<uint32_t, Module::File*> cu_files;
316   DwarfLineToModule h(&m, "/", &lines, &cu_files);
317 
318   h.DefineFile("filename1", 1, 0, 0, 0);
319   h.AddLine(1, 0, 1, 0, 0);
320 
321   ASSERT_EQ(0U, lines.size());
322 }
323 
324 // We are supposed to clip lines that extend beyond the end of the
325 // address space.
TEST(ModuleErrors,BigLine)326 TEST(ModuleErrors, BigLine) {
327   Module m("name", "os", "architecture", "id");
328   vector<Module::Line> lines;
329   std::map<uint32_t, Module::File*> cu_files;
330   DwarfLineToModule h(&m, "/", &lines, &cu_files);
331 
332   h.DefineFile("filename1", 1, 0, 0, 0);
333   h.AddLine(0xffffffffffffffffULL, 2, 1, 0, 0);
334 
335   ASSERT_EQ(1U, lines.size());
336   EXPECT_EQ(1U, lines[0].size);
337 }
338 
339 // The 'Omitted' tests verify that we correctly omit line information
340 // for code in sections that the linker has dropped. See "GNU
341 // toolchain omitted sections support" at the top of the
342 // DwarfLineToModule class.
343 
TEST(Omitted,DroppedThenGood)344 TEST(Omitted, DroppedThenGood) {
345   Module m("name", "os", "architecture", "id");
346   vector<Module::Line> lines;
347   std::map<uint32_t, Module::File*> cu_files;
348   DwarfLineToModule h(&m, "/", &lines, &cu_files);
349 
350   h.DefineFile("filename1", 1, 0, 0, 0);
351   h.AddLine(0,  10, 1, 83816211, 0);   // should be omitted
352   h.AddLine(20, 10, 1, 13059195, 0);   // should be recorded
353 
354   ASSERT_EQ(1U, lines.size());
355   EXPECT_EQ(13059195, lines[0].number);
356 }
357 
TEST(Omitted,GoodThenDropped)358 TEST(Omitted, GoodThenDropped) {
359   Module m("name", "os", "architecture", "id");
360   vector<Module::Line> lines;
361   std::map<uint32_t, Module::File*> cu_files;
362   DwarfLineToModule h(&m, "/", &lines, &cu_files);
363 
364   h.DefineFile("filename1", 1, 0, 0, 0);
365   h.AddLine(0x9dd6a372, 10, 1, 41454594, 0);   // should be recorded
366   h.AddLine(0,  10, 1, 44793413, 0);           // should be omitted
367 
368   ASSERT_EQ(1U, lines.size());
369   EXPECT_EQ(41454594, lines[0].number);
370 }
371 
TEST(Omitted,Mix1)372 TEST(Omitted, Mix1) {
373   Module m("name", "os", "architecture", "id");
374   vector<Module::Line> lines;
375   std::map<uint32_t, Module::File*> cu_files;
376   DwarfLineToModule h(&m, "/", &lines, &cu_files);
377 
378   h.DefineFile("filename1", 1, 0, 0, 0);
379   h.AddLine(0x679ed72f,  10,   1, 58932642, 0);   // should be recorded
380   h.AddLine(0xdfb5a72d,  10,   1, 39847385, 0);   // should be recorded
381   h.AddLine(0,           0x78, 1, 23053829, 0);   // should be omitted
382   h.AddLine(0x78,        0x6a, 1, 65317783, 0);   // should be omitted
383   h.AddLine(0x78 + 0x6a, 0x2a, 1, 77601423, 0);   // should be omitted
384   h.AddLine(0x9fe0cea5,  10,   1, 91806582, 0);   // should be recorded
385   h.AddLine(0x7e41a109,  10,   1, 56169221, 0);   // should be recorded
386 
387   ASSERT_EQ(4U, lines.size());
388   EXPECT_EQ(58932642, lines[0].number);
389   EXPECT_EQ(39847385, lines[1].number);
390   EXPECT_EQ(91806582, lines[2].number);
391   EXPECT_EQ(56169221, lines[3].number);
392 }
393 
TEST(Omitted,Mix2)394 TEST(Omitted, Mix2) {
395   Module m("name", "os", "architecture", "id");
396   vector<Module::Line> lines;
397   std::map<uint32_t, Module::File*> cu_files;
398   DwarfLineToModule h(&m, "/", &lines, &cu_files);
399 
400   h.DefineFile("filename1", 1, 0, 0, 0);
401   h.AddLine(0,           0xf2, 1, 58802211, 0);   // should be omitted
402   h.AddLine(0xf2,        0xb9, 1, 78958222, 0);   // should be omitted
403   h.AddLine(0xf2 + 0xb9, 0xf7, 1, 64861892, 0);   // should be omitted
404   h.AddLine(0x4e4d271e,  9,    1, 67355743, 0);   // should be recorded
405   h.AddLine(0xdfb5a72d,  30,   1, 23365776, 0);   // should be recorded
406   h.AddLine(0,           0x64, 1, 76196762, 0);   // should be omitted
407   h.AddLine(0x64,        0x33, 1, 71066611, 0);   // should be omitted
408   h.AddLine(0x64 + 0x33, 0xe3, 1, 61749337, 0);   // should be omitted
409 
410   ASSERT_EQ(2U, lines.size());
411   EXPECT_EQ(67355743, lines[0].number);
412   EXPECT_EQ(23365776, lines[1].number);
413 }
414