xref: /aosp_15_r20/external/google-breakpad/src/common/stabs_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 // dump_stabs_unittest.cc: Unit tests for StabsToModule.
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/stabs_to_module.h"
41 
42 using google_breakpad::Module;
43 using google_breakpad::StabsToModule;
44 using std::vector;
45 
TEST(StabsToModule,SimpleCU)46 TEST(StabsToModule, SimpleCU) {
47   Module m("name", "os", "arch", "id");
48   StabsToModule h(&m);
49 
50   // Feed in a simple compilation unit that defines a function with
51   // one line.
52   EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0x9f4d1271e50db93bLL,
53                                      "build-directory"));
54   EXPECT_TRUE(h.StartFunction("function", 0xfde4abbed390c394LL));
55   EXPECT_TRUE(h.Line(0xfde4abbed390c394LL, "source-file-name", 174823314));
56   EXPECT_TRUE(h.EndFunction(0xfde4abbed390c3a4LL));
57   EXPECT_TRUE(h.EndCompilationUnit(0xfee4abbed390c3a4LL));
58   h.Finalize();
59 
60   // Now check to see what has been added to the Module.
61   Module::File *file = m.FindExistingFile("source-file-name");
62   ASSERT_TRUE(file != NULL);
63 
64   vector<Module::Function*> functions;
65   m.GetFunctions(&functions, functions.end());
66   ASSERT_EQ((size_t) 1, functions.size());
67   Module::Function *function = functions[0];
68   EXPECT_STREQ("function", function->name.str().c_str());
69   EXPECT_EQ(0xfde4abbed390c394LL, function->address);
70   EXPECT_EQ(0x10U, function->ranges[0].size);
71   EXPECT_EQ(0U, function->parameter_size);
72   ASSERT_EQ((size_t) 1, function->lines.size());
73   Module::Line *line = &function->lines[0];
74   EXPECT_EQ(0xfde4abbed390c394LL, line->address);
75   EXPECT_EQ(0x10U, line->size); // derived from EndFunction
76   EXPECT_TRUE(line->file == file);
77   EXPECT_EQ(174823314, line->number);
78 }
79 
80 #ifdef __GNUC__
81 // Function name mangling can vary by compiler, so only run mangled-name
82 // tests on GCC for simplicity's sake.
TEST(StabsToModule,Externs)83 TEST(StabsToModule, Externs) {
84   Module m("name", "os", "arch", "id");
85   StabsToModule h(&m);
86 
87   // Feed in a few Extern symbols.
88   EXPECT_TRUE(h.Extern("_foo", 0xffff));
89   EXPECT_TRUE(h.Extern("__Z21dyldGlobalLockAcquirev", 0xaaaa));
90   EXPECT_TRUE(h.Extern("_MorphTableGetNextMorphChain", 0x1111));
91   h.Finalize();
92 
93   // Now check to see what has been added to the Module.
94   vector<Module::Extern*> externs;
95   m.GetExterns(&externs, externs.end());
96   ASSERT_EQ((size_t) 3, externs.size());
97   Module::Extern *extern1 = externs[0];
98   EXPECT_STREQ("MorphTableGetNextMorphChain", extern1->name.c_str());
99   EXPECT_EQ((Module::Address)0x1111, extern1->address);
100   Module::Extern *extern2 = externs[1];
101   EXPECT_STREQ("dyldGlobalLockAcquire()", extern2->name.c_str());
102   EXPECT_EQ((Module::Address)0xaaaa, extern2->address);
103   Module::Extern *extern3 = externs[2];
104   EXPECT_STREQ("foo", extern3->name.c_str());
105   EXPECT_EQ((Module::Address)0xffff, extern3->address);
106 }
107 #endif  // __GNUC__
108 
TEST(StabsToModule,DuplicateFunctionNames)109 TEST(StabsToModule, DuplicateFunctionNames) {
110   Module m("name", "os", "arch", "id");
111   StabsToModule h(&m);
112 
113   // Compilation unit with one function, mangled name.
114   EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0xf2cfda36ecf7f46cLL,
115                                      "build-directory"));
116   EXPECT_TRUE(h.StartFunction("funcfoo",
117                               0xf2cfda36ecf7f46dLL));
118   EXPECT_TRUE(h.EndFunction(0));
119   EXPECT_TRUE(h.StartFunction("funcfoo",
120                               0xf2cfda36ecf7f46dLL));
121   EXPECT_TRUE(h.EndFunction(0));
122   EXPECT_TRUE(h.EndCompilationUnit(0));
123 
124   h.Finalize();
125 
126   // Now check to see what has been added to the Module.
127   Module::File *file = m.FindExistingFile("compilation-unit");
128   ASSERT_TRUE(file != NULL);
129 
130   vector<Module::Function*> functions;
131   m.GetFunctions(&functions, functions.end());
132   ASSERT_EQ(1U, functions.size());
133 
134   Module::Function *function = functions[0];
135   EXPECT_EQ(0xf2cfda36ecf7f46dLL, function->address);
136   EXPECT_LT(0U, function->ranges[0].size); // should have used dummy size
137   EXPECT_EQ(0U, function->parameter_size);
138   ASSERT_EQ(0U, function->lines.size());
139 }
140 
TEST(InferSizes,LineSize)141 TEST(InferSizes, LineSize) {
142   Module m("name", "os", "arch", "id");
143   StabsToModule h(&m);
144 
145   // Feed in a simple compilation unit that defines a function with
146   // one line.
147   EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0xb4513962eff94e92LL,
148                                      "build-directory"));
149   EXPECT_TRUE(h.StartFunction("function", 0xb4513962eff94e92LL));
150   EXPECT_TRUE(h.Line(0xb4513962eff94e92LL, "source-file-name-1", 77396614));
151   EXPECT_TRUE(h.Line(0xb4513963eff94e92LL, "source-file-name-2", 87660088));
152   EXPECT_TRUE(h.EndFunction(0));  // unknown function end address
153   EXPECT_TRUE(h.EndCompilationUnit(0)); // unknown CU end address
154   EXPECT_TRUE(h.StartCompilationUnit("compilation-unit-2", 0xb4523963eff94e92LL,
155                                      "build-directory-2")); // next boundary
156   EXPECT_TRUE(h.EndCompilationUnit(0));
157   h.Finalize();
158 
159   // Now check to see what has been added to the Module.
160   Module::File *file1 = m.FindExistingFile("source-file-name-1");
161   ASSERT_TRUE(file1 != NULL);
162   Module::File *file2 = m.FindExistingFile("source-file-name-2");
163   ASSERT_TRUE(file2 != NULL);
164 
165   vector<Module::Function*> functions;
166   m.GetFunctions(&functions, functions.end());
167   ASSERT_EQ((size_t) 1, functions.size());
168 
169   Module::Function *function = functions[0];
170   EXPECT_STREQ("function", function->name.str().c_str());
171   EXPECT_EQ(0xb4513962eff94e92LL, function->address);
172   EXPECT_EQ(0x1000100000000ULL, function->ranges[0].size); // inferred from CU end
173   EXPECT_EQ(0U, function->parameter_size);
174   ASSERT_EQ((size_t) 2, function->lines.size());
175 
176   Module::Line *line1 = &function->lines[0];
177   EXPECT_EQ(0xb4513962eff94e92LL, line1->address);
178   EXPECT_EQ(0x100000000ULL, line1->size); // derived from EndFunction
179   EXPECT_TRUE(line1->file == file1);
180   EXPECT_EQ(77396614, line1->number);
181 
182   Module::Line *line2 = &function->lines[1];
183   EXPECT_EQ(0xb4513963eff94e92LL, line2->address);
184   EXPECT_EQ(0x1000000000000ULL, line2->size); // derived from EndFunction
185   EXPECT_TRUE(line2->file == file2);
186   EXPECT_EQ(87660088, line2->number);
187 }
188 
189 #ifdef __GNUC__
190 // Function name mangling can vary by compiler, so only run mangled-name
191 // tests on GCC for simplicity's sake.
TEST(FunctionNames,Mangled)192 TEST(FunctionNames, Mangled) {
193   Module m("name", "os", "arch", "id");
194   StabsToModule h(&m);
195 
196   // Compilation unit with one function, mangled name.
197   EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0xf2cfda63cef7f46cLL,
198                                      "build-directory"));
199   EXPECT_TRUE(h.StartFunction("_ZNSt6vectorIySaIyEE9push_backERKy",
200                               0xf2cfda63cef7f46dLL));
201   EXPECT_TRUE(h.EndFunction(0));
202   EXPECT_TRUE(h.EndCompilationUnit(0));
203 
204   h.Finalize();
205 
206   // Now check to see what has been added to the Module.
207   Module::File *file = m.FindExistingFile("compilation-unit");
208   ASSERT_TRUE(file != NULL);
209 
210   vector<Module::Function*> functions;
211   m.GetFunctions(&functions, functions.end());
212   ASSERT_EQ(1U, functions.size());
213 
214   Module::Function *function = functions[0];
215   // This is GCC-specific, but we shouldn't be seeing STABS data anywhere
216   // but Linux.
217   EXPECT_THAT(function->name.str(), ::testing::ContainsRegex(
218     "std::vector<unsigned long long, std::allocator<unsigned long long>\\s?>::"
219     "push_back\\(unsigned long long const&\\)"));
220   EXPECT_EQ(0xf2cfda63cef7f46dLL, function->address);
221   EXPECT_LT(0U, function->ranges[0].size); // should have used dummy size
222   EXPECT_EQ(0U, function->parameter_size);
223   ASSERT_EQ(0U, function->lines.size());
224 }
225 #endif  // __GNUC__
226 
227 // The GNU toolchain can omit functions that are not used; however,
228 // when it does so, it doesn't clean up the debugging information that
229 // refers to them. In STABS, this results in compilation units whose
230 // SO addresses are zero.
TEST(Omitted,Function)231 TEST(Omitted, Function) {
232   Module m("name", "os", "arch", "id");
233   StabsToModule h(&m);
234 
235   // The StartCompilationUnit and EndCompilationUnit calls may both have an
236   // address of zero if the compilation unit has had sections removed.
237   EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0, "build-directory"));
238   EXPECT_TRUE(h.StartFunction("function", 0x2a133596));
239   EXPECT_TRUE(h.EndFunction(0));
240   EXPECT_TRUE(h.EndCompilationUnit(0));
241 }
242 
243 // TODO --- if we actually cared about STABS. Even without these we've
244 // got full coverage of non-failure source lines in dump_stabs.cc.
245 
246 // Line size from next line
247 // Line size from function end
248 // Line size from next function start
249 // line size from cu end
250 // line size from next cu start
251 // fallback size is something plausible
252 
253 // function size from function end
254 // function size from next function start
255 // function size from cu end
256 // function size from next cu start
257 // fallback size is something plausible
258 
259 // omitting functions outside the compilation unit's address range
260 // zero-line, one-line, many-line functions
261