|
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 // stackwalker_amd64_unittest.cc: Unit tests for StackwalkerAMD64 class. |
|
33 |
|
34 #include <string.h> |
|
35 #include <string> |
|
36 #include <vector> |
|
37 |
|
38 #include "breakpad_googletest_includes.h" |
|
39 #include "common/test_assembler.h" |
|
40 #include "common/using_std_string.h" |
|
41 #include "google_breakpad/common/minidump_format.h" |
|
42 #include "google_breakpad/processor/basic_source_line_resolver.h" |
|
43 #include "google_breakpad/processor/call_stack.h" |
|
44 #include "google_breakpad/processor/code_module.h" |
|
45 #include "google_breakpad/processor/source_line_resolver_interface.h" |
|
46 #include "google_breakpad/processor/stack_frame_cpu.h" |
|
47 #include "processor/stackwalker_unittest_utils.h" |
|
48 #include "processor/stackwalker_amd64.h" |
|
49 |
|
50 using google_breakpad::BasicSourceLineResolver; |
|
51 using google_breakpad::CallStack; |
|
52 using google_breakpad::CodeModule; |
|
53 using google_breakpad::StackFrameSymbolizer; |
|
54 using google_breakpad::StackFrame; |
|
55 using google_breakpad::StackFrameAMD64; |
|
56 using google_breakpad::StackwalkerAMD64; |
|
57 using google_breakpad::SystemInfo; |
|
58 using google_breakpad::test_assembler::kLittleEndian; |
|
59 using google_breakpad::test_assembler::Label; |
|
60 using google_breakpad::test_assembler::Section; |
|
61 using std::vector; |
|
62 using testing::_; |
|
63 using testing::Return; |
|
64 using testing::SetArgumentPointee; |
|
65 using testing::Test; |
|
66 |
|
67 class StackwalkerAMD64Fixture { |
|
68 public: |
|
69 StackwalkerAMD64Fixture() |
|
70 : stack_section(kLittleEndian), |
|
71 // Give the two modules reasonable standard locations and names |
|
72 // for tests to play with. |
|
73 module1(0x40000000c0000000ULL, 0x10000, "module1", "version1"), |
|
74 module2(0x50000000b0000000ULL, 0x10000, "module2", "version2") { |
|
75 // Identify the system as a Linux system. |
|
76 system_info.os = "Linux"; |
|
77 system_info.os_short = "linux"; |
|
78 system_info.os_version = "Horrendous Hippo"; |
|
79 system_info.cpu = "x86"; |
|
80 system_info.cpu_info = ""; |
|
81 |
|
82 // Put distinctive values in the raw CPU context. |
|
83 BrandContext(&raw_context); |
|
84 |
|
85 // Create some modules with some stock debugging information. |
|
86 modules.Add(&module1); |
|
87 modules.Add(&module2); |
|
88 |
|
89 // By default, none of the modules have symbol info; call |
|
90 // SetModuleSymbols to override this. |
|
91 EXPECT_CALL(supplier, GetCStringSymbolData(_, _, _, _)) |
|
92 .WillRepeatedly(Return(MockSymbolSupplier::NOT_FOUND)); |
|
93 } |
|
94 |
|
95 // Set the Breakpad symbol information that supplier should return for |
|
96 // MODULE to INFO. |
|
97 void SetModuleSymbols(MockCodeModule *module, const string &info) { |
|
98 char *buffer = supplier.CopySymbolDataAndOwnTheCopy(info); |
|
99 EXPECT_CALL(supplier, GetCStringSymbolData(module, &system_info, _, _)) |
|
100 .WillRepeatedly(DoAll(SetArgumentPointee<3>(buffer), |
|
101 Return(MockSymbolSupplier::FOUND))); |
|
102 } |
|
103 |
|
104 // Populate stack_region with the contents of stack_section. Use |
|
105 // stack_section.start() as the region's starting address. |
|
106 void RegionFromSection() { |
|
107 string contents; |
|
108 ASSERT_TRUE(stack_section.GetContents(&contents)); |
|
109 stack_region.Init(stack_section.start().Value(), contents); |
|
110 } |
|
111 |
|
112 // Fill RAW_CONTEXT with pseudo-random data, for round-trip checking. |
|
113 void BrandContext(MDRawContextAMD64 *raw_context) { |
|
114 uint8_t x = 173; |
|
115 for (size_t i = 0; i < sizeof(*raw_context); i++) |
|
116 reinterpret_cast<uint8_t *>(raw_context)[i] = (x += 17); |
|
117 } |
|
118 |
|
119 SystemInfo system_info; |
|
120 MDRawContextAMD64 raw_context; |
|
121 Section stack_section; |
|
122 MockMemoryRegion stack_region; |
|
123 MockCodeModule module1; |
|
124 MockCodeModule module2; |
|
125 MockCodeModules modules; |
|
126 MockSymbolSupplier supplier; |
|
127 BasicSourceLineResolver resolver; |
|
128 CallStack call_stack; |
|
129 const vector<StackFrame *> *frames; |
|
130 }; |
|
131 |
|
132 class GetContextFrame: public StackwalkerAMD64Fixture, public Test { }; |
|
133 |
|
134 class SanityCheck: public StackwalkerAMD64Fixture, public Test { }; |
|
135 |
|
136 TEST_F(SanityCheck, NoResolver) { |
|
137 // There should be no references to the stack in this walk: we don't |
|
138 // provide any call frame information, so trying to reconstruct the |
|
139 // context frame's caller should fail. So there's no need for us to |
|
140 // provide stack contents. |
|
141 raw_context.rip = 0x40000000c0000200ULL; |
|
142 raw_context.rbp = 0x8000000080000000ULL; |
|
143 |
|
144 StackFrameSymbolizer frame_symbolizer(NULL, NULL); |
|
145 StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, |
|
146 &frame_symbolizer); |
|
147 // This should succeed even without a resolver or supplier. |
|
148 vector<const CodeModule*> modules_without_symbols; |
|
149 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); |
|
150 ASSERT_EQ(1U, modules_without_symbols.size()); |
|
151 ASSERT_EQ("module1", modules_without_symbols[0]->debug_file()); |
|
152 frames = call_stack.frames(); |
|
153 ASSERT_GE(1U, frames->size()); |
|
154 StackFrameAMD64 *frame = static_cast<StackFrameAMD64 *>(frames->at(0)); |
|
155 // Check that the values from the original raw context made it |
|
156 // through to the context in the stack frame. |
|
157 EXPECT_EQ(0, memcmp(&raw_context, &frame->context, sizeof(raw_context))); |
|
158 } |
|
159 |
|
160 TEST_F(GetContextFrame, Simple) { |
|
161 // There should be no references to the stack in this walk: we don't |
|
162 // provide any call frame information, so trying to reconstruct the |
|
163 // context frame's caller should fail. So there's no need for us to |
|
164 // provide stack contents. |
|
165 raw_context.rip = 0x40000000c0000200ULL; |
|
166 raw_context.rbp = 0x8000000080000000ULL; |
|
167 |
|
168 StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); |
|
169 StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, |
|
170 &frame_symbolizer); |
|
171 vector<const CodeModule*> modules_without_symbols; |
|
172 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); |
|
173 ASSERT_EQ(1U, modules_without_symbols.size()); |
|
174 ASSERT_EQ("module1", modules_without_symbols[0]->debug_file()); |
|
175 frames = call_stack.frames(); |
|
176 ASSERT_GE(1U, frames->size()); |
|
177 StackFrameAMD64 *frame = static_cast<StackFrameAMD64 *>(frames->at(0)); |
|
178 // Check that the values from the original raw context made it |
|
179 // through to the context in the stack frame. |
|
180 EXPECT_EQ(0, memcmp(&raw_context, &frame->context, sizeof(raw_context))); |
|
181 } |
|
182 |
|
183 // The stackwalker should be able to produce the context frame even |
|
184 // without stack memory present. |
|
185 TEST_F(GetContextFrame, NoStackMemory) { |
|
186 raw_context.rip = 0x40000000c0000200ULL; |
|
187 raw_context.rbp = 0x8000000080000000ULL; |
|
188 |
|
189 StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); |
|
190 StackwalkerAMD64 walker(&system_info, &raw_context, NULL, &modules, |
|
191 &frame_symbolizer); |
|
192 vector<const CodeModule*> modules_without_symbols; |
|
193 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); |
|
194 ASSERT_EQ(1U, modules_without_symbols.size()); |
|
195 ASSERT_EQ("module1", modules_without_symbols[0]->debug_file()); |
|
196 frames = call_stack.frames(); |
|
197 ASSERT_GE(1U, frames->size()); |
|
198 StackFrameAMD64 *frame = static_cast<StackFrameAMD64 *>(frames->at(0)); |
|
199 // Check that the values from the original raw context made it |
|
200 // through to the context in the stack frame. |
|
201 EXPECT_EQ(0, memcmp(&raw_context, &frame->context, sizeof(raw_context))); |
|
202 } |
|
203 |
|
204 class GetCallerFrame: public StackwalkerAMD64Fixture, public Test { }; |
|
205 |
|
206 TEST_F(GetCallerFrame, ScanWithoutSymbols) { |
|
207 // When the stack walker resorts to scanning the stack, |
|
208 // only addresses located within loaded modules are |
|
209 // considered valid return addresses. |
|
210 // Force scanning through three frames to ensure that the |
|
211 // stack pointer is set properly in scan-recovered frames. |
|
212 stack_section.start() = 0x8000000080000000ULL; |
|
213 uint64_t return_address1 = 0x50000000b0000100ULL; |
|
214 uint64_t return_address2 = 0x50000000b0000900ULL; |
|
215 Label frame1_sp, frame2_sp, frame1_rbp; |
|
216 stack_section |
|
217 // frame 0 |
|
218 .Append(16, 0) // space |
|
219 |
|
220 .D64(0x40000000b0000000ULL) // junk that's not |
|
221 .D64(0x50000000d0000000ULL) // a return address |
|
222 |
|
223 .D64(return_address1) // actual return address |
|
224 // frame 1 |
|
225 .Mark(&frame1_sp) |
|
226 .Append(16, 0) // space |
|
227 |
|
228 .D64(0x40000000b0000000ULL) // more junk |
|
229 .D64(0x50000000d0000000ULL) |
|
230 |
|
231 .Mark(&frame1_rbp) |
|
232 .D64(stack_section.start()) // This is in the right place to be |
|
233 // a saved rbp, but it's bogus, so |
|
234 // we shouldn't report it. |
|
235 |
|
236 .D64(return_address2) // actual return address |
|
237 // frame 2 |
|
238 .Mark(&frame2_sp) |
|
239 .Append(32, 0); // end of stack |
|
240 |
|
241 RegionFromSection(); |
|
242 |
|
243 raw_context.rip = 0x40000000c0000200ULL; |
|
244 raw_context.rbp = frame1_rbp.Value(); |
|
245 raw_context.rsp = stack_section.start().Value(); |
|
246 |
|
247 StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); |
|
248 StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, |
|
249 &frame_symbolizer); |
|
250 vector<const CodeModule*> modules_without_symbols; |
|
251 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); |
|
252 ASSERT_EQ(2U, modules_without_symbols.size()); |
|
253 ASSERT_EQ("module1", modules_without_symbols[0]->debug_file()); |
|
254 ASSERT_EQ("module2", modules_without_symbols[1]->debug_file()); |
|
255 frames = call_stack.frames(); |
|
256 ASSERT_EQ(3U, frames->size()); |
|
257 |
|
258 StackFrameAMD64 *frame0 = static_cast<StackFrameAMD64 *>(frames->at(0)); |
|
259 EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); |
|
260 ASSERT_EQ(StackFrameAMD64::CONTEXT_VALID_ALL, frame0->context_validity); |
|
261 EXPECT_EQ(0, memcmp(&raw_context, &frame0->context, sizeof(raw_context))); |
|
262 |
|
263 StackFrameAMD64 *frame1 = static_cast<StackFrameAMD64 *>(frames->at(1)); |
|
264 EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust); |
|
265 ASSERT_EQ((StackFrameAMD64::CONTEXT_VALID_RIP | |
|
266 StackFrameAMD64::CONTEXT_VALID_RSP | |
|
267 StackFrameAMD64::CONTEXT_VALID_RBP), |
|
268 frame1->context_validity); |
|
269 EXPECT_EQ(return_address1, frame1->context.rip); |
|
270 EXPECT_EQ(frame1_sp.Value(), frame1->context.rsp); |
|
271 EXPECT_EQ(frame1_rbp.Value(), frame1->context.rbp); |
|
272 |
|
273 StackFrameAMD64 *frame2 = static_cast<StackFrameAMD64 *>(frames->at(2)); |
|
274 EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame2->trust); |
|
275 ASSERT_EQ((StackFrameAMD64::CONTEXT_VALID_RIP | |
|
276 StackFrameAMD64::CONTEXT_VALID_RSP), |
|
277 frame2->context_validity); |
|
278 EXPECT_EQ(return_address2, frame2->context.rip); |
|
279 EXPECT_EQ(frame2_sp.Value(), frame2->context.rsp); |
|
280 } |
|
281 |
|
282 TEST_F(GetCallerFrame, ScanWithFunctionSymbols) { |
|
283 // During stack scanning, if a potential return address |
|
284 // is located within a loaded module that has symbols, |
|
285 // it is only considered a valid return address if it |
|
286 // lies within a function's bounds. |
|
287 stack_section.start() = 0x8000000080000000ULL; |
|
288 uint64_t return_address = 0x50000000b0000110ULL; |
|
289 Label frame1_sp, frame1_rbp; |
|
290 |
|
291 stack_section |
|
292 // frame 0 |
|
293 .Append(16, 0) // space |
|
294 |
|
295 .D64(0x40000000b0000000ULL) // junk that's not |
|
296 .D64(0x50000000b0000000ULL) // a return address |
|
297 |
|
298 .D64(0x40000000c0001000ULL) // a couple of plausible addresses |
|
299 .D64(0x50000000b000aaaaULL) // that are not within functions |
|
300 |
|
301 .D64(return_address) // actual return address |
|
302 // frame 1 |
|
303 .Mark(&frame1_sp) |
|
304 .Append(32, 0) // end of stack |
|
305 .Mark(&frame1_rbp); |
|
306 RegionFromSection(); |
|
307 |
|
308 raw_context.rip = 0x40000000c0000200ULL; |
|
309 raw_context.rbp = frame1_rbp.Value(); |
|
310 raw_context.rsp = stack_section.start().Value(); |
|
311 |
|
312 SetModuleSymbols(&module1, |
|
313 // The youngest frame's function. |
|
314 "FUNC 100 400 10 platypus\n"); |
|
315 SetModuleSymbols(&module2, |
|
316 // The calling frame's function. |
|
317 "FUNC 100 400 10 echidna\n"); |
|
318 |
|
319 StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); |
|
320 StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, |
|
321 &frame_symbolizer); |
|
322 vector<const CodeModule*> modules_without_symbols; |
|
323 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); |
|
324 ASSERT_EQ(0U, modules_without_symbols.size()); |
|
325 frames = call_stack.frames(); |
|
326 ASSERT_EQ(2U, frames->size()); |
|
327 |
|
328 StackFrameAMD64 *frame0 = static_cast<StackFrameAMD64 *>(frames->at(0)); |
|
329 EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); |
|
330 ASSERT_EQ(StackFrameAMD64::CONTEXT_VALID_ALL, frame0->context_validity); |
|
331 EXPECT_EQ("platypus", frame0->function_name); |
|
332 EXPECT_EQ(0x40000000c0000100ULL, frame0->function_base); |
|
333 |
|
334 StackFrameAMD64 *frame1 = static_cast<StackFrameAMD64 *>(frames->at(1)); |
|
335 EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust); |
|
336 ASSERT_EQ((StackFrameAMD64::CONTEXT_VALID_RIP | |
|
337 StackFrameAMD64::CONTEXT_VALID_RSP | |
|
338 StackFrameAMD64::CONTEXT_VALID_RBP), |
|
339 frame1->context_validity); |
|
340 EXPECT_EQ(return_address, frame1->context.rip); |
|
341 EXPECT_EQ(frame1_sp.Value(), frame1->context.rsp); |
|
342 EXPECT_EQ(frame1_rbp.Value(), frame1->context.rbp); |
|
343 EXPECT_EQ("echidna", frame1->function_name); |
|
344 EXPECT_EQ(0x50000000b0000100ULL, frame1->function_base); |
|
345 } |
|
346 |
|
347 TEST_F(GetCallerFrame, CallerPushedRBP) { |
|
348 // Functions typically push their %rbp upon entry and set %rbp pointing |
|
349 // there. If stackwalking finds a plausible address for the next frame's |
|
350 // %rbp directly below the return address, assume that it is indeed the |
|
351 // next frame's %rbp. |
|
352 stack_section.start() = 0x8000000080000000ULL; |
|
353 uint64_t return_address = 0x50000000b0000110ULL; |
|
354 Label frame0_rbp, frame1_sp, frame1_rbp; |
|
355 |
|
356 stack_section |
|
357 // frame 0 |
|
358 .Append(16, 0) // space |
|
359 |
|
360 .D64(0x40000000b0000000ULL) // junk that's not |
|
361 .D64(0x50000000b0000000ULL) // a return address |
|
362 |
|
363 .D64(0x40000000c0001000ULL) // a couple of plausible addresses |
|
364 .D64(0x50000000b000aaaaULL) // that are not within functions |
|
365 |
|
366 .Mark(&frame0_rbp) |
|
367 .D64(frame1_rbp) // caller-pushed %rbp |
|
368 .D64(return_address) // actual return address |
|
369 // frame 1 |
|
370 .Mark(&frame1_sp) |
|
371 .Append(32, 0) // body of frame1 |
|
372 .Mark(&frame1_rbp); // end of stack |
|
373 RegionFromSection(); |
|
374 |
|
375 raw_context.rip = 0x40000000c0000200ULL; |
|
376 raw_context.rbp = frame0_rbp.Value(); |
|
377 raw_context.rsp = stack_section.start().Value(); |
|
378 |
|
379 SetModuleSymbols(&module1, |
|
380 // The youngest frame's function. |
|
381 "FUNC 100 400 10 sasquatch\n"); |
|
382 SetModuleSymbols(&module2, |
|
383 // The calling frame's function. |
|
384 "FUNC 100 400 10 yeti\n"); |
|
385 |
|
386 StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); |
|
387 StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, |
|
388 &frame_symbolizer); |
|
389 vector<const CodeModule*> modules_without_symbols; |
|
390 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); |
|
391 ASSERT_EQ(0U, modules_without_symbols.size()); |
|
392 frames = call_stack.frames(); |
|
393 ASSERT_EQ(2U, frames->size()); |
|
394 |
|
395 StackFrameAMD64 *frame0 = static_cast<StackFrameAMD64 *>(frames->at(0)); |
|
396 EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); |
|
397 ASSERT_EQ(StackFrameAMD64::CONTEXT_VALID_ALL, frame0->context_validity); |
|
398 EXPECT_EQ(frame0_rbp.Value(), frame0->context.rbp); |
|
399 EXPECT_EQ("sasquatch", frame0->function_name); |
|
400 EXPECT_EQ(0x40000000c0000100ULL, frame0->function_base); |
|
401 |
|
402 StackFrameAMD64 *frame1 = static_cast<StackFrameAMD64 *>(frames->at(1)); |
|
403 EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust); |
|
404 ASSERT_EQ((StackFrameAMD64::CONTEXT_VALID_RIP | |
|
405 StackFrameAMD64::CONTEXT_VALID_RSP | |
|
406 StackFrameAMD64::CONTEXT_VALID_RBP), |
|
407 frame1->context_validity); |
|
408 EXPECT_EQ(return_address, frame1->context.rip); |
|
409 EXPECT_EQ(frame1_sp.Value(), frame1->context.rsp); |
|
410 EXPECT_EQ(frame1_rbp.Value(), frame1->context.rbp); |
|
411 EXPECT_EQ("yeti", frame1->function_name); |
|
412 EXPECT_EQ(0x50000000b0000100ULL, frame1->function_base); |
|
413 } |
|
414 |
|
415 struct CFIFixture: public StackwalkerAMD64Fixture { |
|
416 CFIFixture() { |
|
417 // Provide a bunch of STACK CFI records; we'll walk to the caller |
|
418 // from every point in this series, expecting to find the same set |
|
419 // of register values. |
|
420 SetModuleSymbols(&module1, |
|
421 // The youngest frame's function. |
|
422 "FUNC 4000 1000 10 enchiridion\n" |
|
423 // Initially, just a return address. |
|
424 "STACK CFI INIT 4000 100 .cfa: $rsp 8 + .ra: .cfa 8 - ^\n" |
|
425 // Push %rbx. |
|
426 "STACK CFI 4001 .cfa: $rsp 16 + $rbx: .cfa 16 - ^\n" |
|
427 // Save %r12 in %rbx. Weird, but permitted. |
|
428 "STACK CFI 4002 $r12: $rbx\n" |
|
429 // Allocate frame space, and save %r13. |
|
430 "STACK CFI 4003 .cfa: $rsp 40 + $r13: .cfa 32 - ^\n" |
|
431 // Put the return address in %r13. |
|
432 "STACK CFI 4005 .ra: $r13\n" |
|
433 // Save %rbp, and use it as a frame pointer. |
|
434 "STACK CFI 4006 .cfa: $rbp 16 + $rbp: .cfa 24 - ^\n" |
|
435 |
|
436 // The calling function. |
|
437 "FUNC 5000 1000 10 epictetus\n" |
|
438 // Mark it as end of stack. |
|
439 "STACK CFI INIT 5000 1000 .cfa: $rsp .ra 0\n"); |
|
440 |
|
441 // Provide some distinctive values for the caller's registers. |
|
442 expected.rsp = 0x8000000080000000ULL; |
|
443 expected.rip = 0x40000000c0005510ULL; |
|
444 expected.rbp = 0x68995b1de4700266ULL; |
|
445 expected.rbx = 0x5a5beeb38de23be8ULL; |
|
446 expected.r12 = 0xed1b02e8cc0fc79cULL; |
|
447 expected.r13 = 0x1d20ad8acacbe930ULL; |
|
448 expected.r14 = 0xe94cffc2f7adaa28ULL; |
|
449 expected.r15 = 0xb638d17d8da413b5ULL; |
|
450 |
|
451 // By default, registers are unchanged. |
|
452 raw_context = expected; |
|
453 } |
|
454 |
|
455 // Walk the stack, using stack_section as the contents of the stack |
|
456 // and raw_context as the current register values. (Set |
|
457 // raw_context.rsp to the stack's starting address.) Expect two |
|
458 // stack frames; in the older frame, expect the callee-saves |
|
459 // registers to have values matching those in 'expected'. |
|
460 void CheckWalk() { |
|
461 RegionFromSection(); |
|
462 raw_context.rsp = stack_section.start().Value(); |
|
463 |
|
464 StackFrameSymbolizer frame_symbolizer(&supplier, &resolver); |
|
465 StackwalkerAMD64 walker(&system_info, &raw_context, &stack_region, &modules, |
|
466 &frame_symbolizer); |
|
467 vector<const CodeModule*> modules_without_symbols; |
|
468 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols)); |
|
469 ASSERT_EQ(0U, modules_without_symbols.size()); |
|
470 frames = call_stack.frames(); |
|
471 ASSERT_EQ(2U, frames->size()); |
|
472 |
|
473 StackFrameAMD64 *frame0 = static_cast<StackFrameAMD64 *>(frames->at(0)); |
|
474 EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust); |
|
475 ASSERT_EQ(StackFrameAMD64::CONTEXT_VALID_ALL, frame0->context_validity); |
|
476 EXPECT_EQ("enchiridion", frame0->function_name); |
|
477 EXPECT_EQ(0x40000000c0004000ULL, frame0->function_base); |
|
478 |
|
479 StackFrameAMD64 *frame1 = static_cast<StackFrameAMD64 *>(frames->at(1)); |
|
480 EXPECT_EQ(StackFrame::FRAME_TRUST_CFI, frame1->trust); |
|
481 ASSERT_EQ((StackFrameAMD64::CONTEXT_VALID_RIP | |
|
482 StackFrameAMD64::CONTEXT_VALID_RSP | |
|
483 StackFrameAMD64::CONTEXT_VALID_RBP | |
|
484 StackFrameAMD64::CONTEXT_VALID_RBX | |
|
485 StackFrameAMD64::CONTEXT_VALID_R12 | |
|
486 StackFrameAMD64::CONTEXT_VALID_R13 | |
|
487 StackFrameAMD64::CONTEXT_VALID_R14 | |
|
488 StackFrameAMD64::CONTEXT_VALID_R15), |
|
489 frame1->context_validity); |
|
490 EXPECT_EQ(expected.rip, frame1->context.rip); |
|
491 EXPECT_EQ(expected.rsp, frame1->context.rsp); |
|
492 EXPECT_EQ(expected.rbp, frame1->context.rbp); |
|
493 EXPECT_EQ(expected.rbx, frame1->context.rbx); |
|
494 EXPECT_EQ(expected.r12, frame1->context.r12); |
|
495 EXPECT_EQ(expected.r13, frame1->context.r13); |
|
496 EXPECT_EQ(expected.r14, frame1->context.r14); |
|
497 EXPECT_EQ(expected.r15, frame1->context.r15); |
|
498 EXPECT_EQ("epictetus", frame1->function_name); |
|
499 } |
|
500 |
|
501 // The values we expect to find for the caller's registers. |
|
502 MDRawContextAMD64 expected; |
|
503 }; |
|
504 |
|
505 class CFI: public CFIFixture, public Test { }; |
|
506 |
|
507 TEST_F(CFI, At4000) { |
|
508 Label frame1_rsp = expected.rsp; |
|
509 stack_section |
|
510 .D64(0x40000000c0005510ULL) // return address |
|
511 .Mark(&frame1_rsp); // This effectively sets stack_section.start(). |
|
512 raw_context.rip = 0x40000000c0004000ULL; |
|
513 CheckWalk(); |
|
514 } |
|
515 |
|
516 TEST_F(CFI, At4001) { |
|
517 Label frame1_rsp = expected.rsp; |
|
518 stack_section |
|
519 .D64(0x5a5beeb38de23be8ULL) // saved %rbx |
|
520 .D64(0x40000000c0005510ULL) // return address |
|
521 .Mark(&frame1_rsp); // This effectively sets stack_section.start(). |
|
522 raw_context.rip = 0x40000000c0004001ULL; |
|
523 raw_context.rbx = 0xbe0487d2f9eafe29ULL; // callee's (distinct) %rbx value |
|
524 CheckWalk(); |
|
525 } |
|
526 |
|
527 TEST_F(CFI, At4002) { |
|
528 Label frame1_rsp = expected.rsp; |
|
529 stack_section |
|
530 .D64(0x5a5beeb38de23be8ULL) // saved %rbx |
|
531 .D64(0x40000000c0005510ULL) // return address |
|
532 .Mark(&frame1_rsp); // This effectively sets stack_section.start(). |
|
533 raw_context.rip = 0x40000000c0004002ULL; |
|
534 raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12 |
|
535 raw_context.r12 = 0xb0118de918a4bceaULL; // callee's (distinct) %r12 value |
|
536 CheckWalk(); |
|
537 } |
|
538 |
|
539 TEST_F(CFI, At4003) { |
|
540 Label frame1_rsp = expected.rsp; |
|
541 stack_section |
|
542 .D64(0x0e023828dffd4d81ULL) // garbage |
|
543 .D64(0x1d20ad8acacbe930ULL) // saved %r13 |
|
544 .D64(0x319e68b49e3ace0fULL) // garbage |
|
545 .D64(0x5a5beeb38de23be8ULL) // saved %rbx |
|
546 .D64(0x40000000c0005510ULL) // return address |
|
547 .Mark(&frame1_rsp); // This effectively sets stack_section.start(). |
|
548 raw_context.rip = 0x40000000c0004003ULL; |
|
549 raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12 |
|
550 raw_context.r12 = 0x89d04fa804c87a43ULL; // callee's (distinct) %r12 |
|
551 raw_context.r13 = 0x5118e02cbdb24b03ULL; // callee's (distinct) %r13 |
|
552 CheckWalk(); |
|
553 } |
|
554 |
|
555 // The results here should be the same as those at module offset 0x4003. |
|
556 TEST_F(CFI, At4004) { |
|
557 Label frame1_rsp = expected.rsp; |
|
558 stack_section |
|
559 .D64(0x0e023828dffd4d81ULL) // garbage |
|
560 .D64(0x1d20ad8acacbe930ULL) // saved %r13 |
|
561 .D64(0x319e68b49e3ace0fULL) // garbage |
|
562 .D64(0x5a5beeb38de23be8ULL) // saved %rbx |
|
563 .D64(0x40000000c0005510ULL) // return address |
|
564 .Mark(&frame1_rsp); // This effectively sets stack_section.start(). |
|
565 raw_context.rip = 0x40000000c0004004ULL; |
|
566 raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12 |
|
567 raw_context.r12 = 0x89d04fa804c87a43ULL; // callee's (distinct) %r12 |
|
568 raw_context.r13 = 0x5118e02cbdb24b03ULL; // callee's (distinct) %r13 |
|
569 CheckWalk(); |
|
570 } |
|
571 |
|
572 TEST_F(CFI, At4005) { |
|
573 Label frame1_rsp = expected.rsp; |
|
574 stack_section |
|
575 .D64(0x4b516dd035745953ULL) // garbage |
|
576 .D64(0x1d20ad8acacbe930ULL) // saved %r13 |
|
577 .D64(0xa6d445e16ae3d872ULL) // garbage |
|
578 .D64(0x5a5beeb38de23be8ULL) // saved %rbx |
|
579 .D64(0xaa95fa054aedfbaeULL) // garbage |
|
580 .Mark(&frame1_rsp); // This effectively sets stack_section.start(). |
|
581 raw_context.rip = 0x40000000c0004005ULL; |
|
582 raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12 |
|
583 raw_context.r12 = 0x46b1b8868891b34aULL; // callee's %r12 |
|
584 raw_context.r13 = 0x40000000c0005510ULL; // return address |
|
585 CheckWalk(); |
|
586 } |
|
587 |
|
588 TEST_F(CFI, At4006) { |
|
589 Label frame0_rbp; |
|
590 Label frame1_rsp = expected.rsp; |
|
591 stack_section |
|
592 .D64(0x043c6dfceb91aa34ULL) // garbage |
|
593 .D64(0x1d20ad8acacbe930ULL) // saved %r13 |
|
594 .D64(0x68995b1de4700266ULL) // saved %rbp |
|
595 .Mark(&frame0_rbp) // frame pointer points here |
|
596 .D64(0x5a5beeb38de23be8ULL) // saved %rbx |
|
597 .D64(0xf015ee516ad89eabULL) // garbage |
|
598 .Mark(&frame1_rsp); // This effectively sets stack_section.start(). |
|
599 raw_context.rip = 0x40000000c0004006ULL; |
|
600 raw_context.rbp = frame0_rbp.Value(); |
|
601 raw_context.rbx = 0xed1b02e8cc0fc79cULL; // saved %r12 |
|
602 raw_context.r12 = 0x26e007b341acfebdULL; // callee's %r12 |
|
603 raw_context.r13 = 0x40000000c0005510ULL; // return address |
|
604 CheckWalk(); |
|
605 } |