1 // Copyright (c) 2010 Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 // Original author: Jim Blandy <jimb@mozilla.com> <jimb@red-bean.com>
31 
32 // dump_stabs_unittest.cc: Unit tests for StabsToModule.
33 
34 #include <vector>
35 
36 #include "breakpad_googletest_includes.h"
37 #include "common/stabs_to_module.h"
38 
39 using google_breakpad::Module;
40 using google_breakpad::StabsToModule;
41 using std::vector;
42 
TEST(StabsToModule,SimpleCU)43 TEST(StabsToModule, SimpleCU) {
44   Module m("name", "os", "arch", "id");
45   StabsToModule h(&m);
46 
47   // Feed in a simple compilation unit that defines a function with
48   // one line.
49   EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0x9f4d1271e50db93bLL,
50                                      "build-directory"));
51   EXPECT_TRUE(h.StartFunction("function", 0xfde4abbed390c394LL));
52   EXPECT_TRUE(h.Line(0xfde4abbed390c394LL, "source-file-name", 174823314));
53   EXPECT_TRUE(h.EndFunction(0xfde4abbed390c3a4LL));
54   EXPECT_TRUE(h.EndCompilationUnit(0xfee4abbed390c3a4LL));
55   h.Finalize();
56 
57   // Now check to see what has been added to the Module.
58   Module::File *file = m.FindExistingFile("source-file-name");
59   ASSERT_TRUE(file != NULL);
60 
61   vector<Module::Function *> functions;
62   m.GetFunctions(&functions, functions.end());
63   ASSERT_EQ((size_t) 1, functions.size());
64   Module::Function *function = functions[0];
65   EXPECT_STREQ("function", function->name.c_str());
66   EXPECT_EQ(0xfde4abbed390c394LL, function->address);
67   EXPECT_EQ(0x10U, function->size);
68   EXPECT_EQ(0U, function->parameter_size);
69   ASSERT_EQ((size_t) 1, function->lines.size());
70   Module::Line *line = &function->lines[0];
71   EXPECT_EQ(0xfde4abbed390c394LL, line->address);
72   EXPECT_EQ(0x10U, line->size); // derived from EndFunction
73   EXPECT_TRUE(line->file == file);
74   EXPECT_EQ(174823314, line->number);
75 }
76 
77 #ifdef __GNUC__
78 // Function name mangling can vary by compiler, so only run mangled-name
79 // tests on GCC for simplicity's sake.
TEST(StabsToModule,Externs)80 TEST(StabsToModule, Externs) {
81   Module m("name", "os", "arch", "id");
82   StabsToModule h(&m);
83 
84   // Feed in a few Extern symbols.
85   EXPECT_TRUE(h.Extern("_foo", 0xffff));
86   EXPECT_TRUE(h.Extern("__Z21dyldGlobalLockAcquirev", 0xaaaa));
87   EXPECT_TRUE(h.Extern("_MorphTableGetNextMorphChain", 0x1111));
88   h.Finalize();
89 
90   // Now check to see what has been added to the Module.
91   vector<Module::Extern *> externs;
92   m.GetExterns(&externs, externs.end());
93   ASSERT_EQ((size_t) 3, externs.size());
94   Module::Extern *extern1 = externs[0];
95   EXPECT_STREQ("MorphTableGetNextMorphChain", extern1->name.c_str());
96   EXPECT_EQ((Module::Address)0x1111, extern1->address);
97   Module::Extern *extern2 = externs[1];
98   EXPECT_STREQ("dyldGlobalLockAcquire()", extern2->name.c_str());
99   EXPECT_EQ((Module::Address)0xaaaa, extern2->address);
100   Module::Extern *extern3 = externs[2];
101   EXPECT_STREQ("foo", extern3->name.c_str());
102   EXPECT_EQ((Module::Address)0xffff, extern3->address);
103 }
104 #endif  // __GNUC__
105 
TEST(StabsToModule,DuplicateFunctionNames)106 TEST(StabsToModule, DuplicateFunctionNames) {
107   Module m("name", "os", "arch", "id");
108   StabsToModule h(&m);
109 
110   // Compilation unit with one function, mangled name.
111   EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0xf2cfda36ecf7f46cLL,
112                                      "build-directory"));
113   EXPECT_TRUE(h.StartFunction("funcfoo",
114                               0xf2cfda36ecf7f46dLL));
115   EXPECT_TRUE(h.EndFunction(0));
116   EXPECT_TRUE(h.StartFunction("funcfoo",
117                               0xf2cfda36ecf7f46dLL));
118   EXPECT_TRUE(h.EndFunction(0));
119   EXPECT_TRUE(h.EndCompilationUnit(0));
120 
121   h.Finalize();
122 
123   // Now check to see what has been added to the Module.
124   Module::File *file = m.FindExistingFile("compilation-unit");
125   ASSERT_TRUE(file != NULL);
126 
127   vector<Module::Function *> functions;
128   m.GetFunctions(&functions, functions.end());
129   ASSERT_EQ(1U, functions.size());
130 
131   Module::Function *function = functions[0];
132   EXPECT_EQ(0xf2cfda36ecf7f46dLL, function->address);
133   EXPECT_LT(0U, function->size);  // should have used dummy size
134   EXPECT_EQ(0U, function->parameter_size);
135   ASSERT_EQ(0U, function->lines.size());
136 }
137 
TEST(InferSizes,LineSize)138 TEST(InferSizes, LineSize) {
139   Module m("name", "os", "arch", "id");
140   StabsToModule h(&m);
141 
142   // Feed in a simple compilation unit that defines a function with
143   // one line.
144   EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0xb4513962eff94e92LL,
145                                      "build-directory"));
146   EXPECT_TRUE(h.StartFunction("function", 0xb4513962eff94e92LL));
147   EXPECT_TRUE(h.Line(0xb4513962eff94e92LL, "source-file-name-1", 77396614));
148   EXPECT_TRUE(h.Line(0xb4513963eff94e92LL, "source-file-name-2", 87660088));
149   EXPECT_TRUE(h.EndFunction(0));  // unknown function end address
150   EXPECT_TRUE(h.EndCompilationUnit(0)); // unknown CU end address
151   EXPECT_TRUE(h.StartCompilationUnit("compilation-unit-2", 0xb4523963eff94e92LL,
152                                      "build-directory-2")); // next boundary
153   EXPECT_TRUE(h.EndCompilationUnit(0));
154   h.Finalize();
155 
156   // Now check to see what has been added to the Module.
157   Module::File *file1 = m.FindExistingFile("source-file-name-1");
158   ASSERT_TRUE(file1 != NULL);
159   Module::File *file2 = m.FindExistingFile("source-file-name-2");
160   ASSERT_TRUE(file2 != NULL);
161 
162   vector<Module::Function *> functions;
163   m.GetFunctions(&functions, functions.end());
164   ASSERT_EQ((size_t) 1, functions.size());
165 
166   Module::Function *function = functions[0];
167   EXPECT_STREQ("function", function->name.c_str());
168   EXPECT_EQ(0xb4513962eff94e92LL, function->address);
169   EXPECT_EQ(0x1000100000000ULL, function->size); // inferred from CU end
170   EXPECT_EQ(0U, function->parameter_size);
171   ASSERT_EQ((size_t) 2, function->lines.size());
172 
173   Module::Line *line1 = &function->lines[0];
174   EXPECT_EQ(0xb4513962eff94e92LL, line1->address);
175   EXPECT_EQ(0x100000000ULL, line1->size); // derived from EndFunction
176   EXPECT_TRUE(line1->file == file1);
177   EXPECT_EQ(77396614, line1->number);
178 
179   Module::Line *line2 = &function->lines[1];
180   EXPECT_EQ(0xb4513963eff94e92LL, line2->address);
181   EXPECT_EQ(0x1000000000000ULL, line2->size); // derived from EndFunction
182   EXPECT_TRUE(line2->file == file2);
183   EXPECT_EQ(87660088, line2->number);
184 }
185 
186 #ifdef __GNUC__
187 // Function name mangling can vary by compiler, so only run mangled-name
188 // tests on GCC for simplicity's sake.
TEST(FunctionNames,Mangled)189 TEST(FunctionNames, Mangled) {
190   Module m("name", "os", "arch", "id");
191   StabsToModule h(&m);
192 
193   // Compilation unit with one function, mangled name.
194   EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0xf2cfda63cef7f46cLL,
195                                      "build-directory"));
196   EXPECT_TRUE(h.StartFunction("_ZNSt6vectorIySaIyEE9push_backERKy",
197                               0xf2cfda63cef7f46dLL));
198   EXPECT_TRUE(h.EndFunction(0));
199   EXPECT_TRUE(h.EndCompilationUnit(0));
200 
201   h.Finalize();
202 
203   // Now check to see what has been added to the Module.
204   Module::File *file = m.FindExistingFile("compilation-unit");
205   ASSERT_TRUE(file != NULL);
206 
207   vector<Module::Function *> functions;
208   m.GetFunctions(&functions, functions.end());
209   ASSERT_EQ(1U, functions.size());
210 
211   Module::Function *function = functions[0];
212   // This is GCC-specific, but we shouldn't be seeing STABS data anywhere
213   // but Linux.
214   EXPECT_STREQ("std::vector<unsigned long long, "
215                "std::allocator<unsigned long long> >::"
216                "push_back(unsigned long long const&)",
217                function->name.c_str());
218   EXPECT_EQ(0xf2cfda63cef7f46dLL, function->address);
219   EXPECT_LT(0U, function->size); // should have used dummy size
220   EXPECT_EQ(0U, function->parameter_size);
221   ASSERT_EQ(0U, function->lines.size());
222 }
223 #endif  // __GNUC__
224 
225 // The GNU toolchain can omit functions that are not used; however,
226 // when it does so, it doesn't clean up the debugging information that
227 // refers to them. In STABS, this results in compilation units whose
228 // SO addresses are zero.
TEST(Omitted,Function)229 TEST(Omitted, Function) {
230   Module m("name", "os", "arch", "id");
231   StabsToModule h(&m);
232 
233   // The StartCompilationUnit and EndCompilationUnit calls may both have an
234   // address of zero if the compilation unit has had sections removed.
235   EXPECT_TRUE(h.StartCompilationUnit("compilation-unit", 0, "build-directory"));
236   EXPECT_TRUE(h.StartFunction("function", 0x2a133596));
237   EXPECT_TRUE(h.EndFunction(0));
238   EXPECT_TRUE(h.EndCompilationUnit(0));
239 }
240 
241 // TODO --- if we actually cared about STABS. Even without these we've
242 // got full coverage of non-failure source lines in dump_stabs.cc.
243 
244 // Line size from next line
245 // Line size from function end
246 // Line size from next function start
247 // line size from cu end
248 // line size from next cu start
249 // fallback size is something plausible
250 
251 // function size from function end
252 // function size from next function start
253 // function size from cu end
254 // function size from next cu start
255 // fallback size is something plausible
256 
257 // omitting functions outside the compilation unit's address range
258 // zero-line, one-line, many-line functions
259