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_arm64_unittest.cc: Unit tests for StackwalkerARM64 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_arm64.h"
49 #include "processor/windows_frame_info.h"
50
51 using google_breakpad::BasicSourceLineResolver;
52 using google_breakpad::CallStack;
53 using google_breakpad::CodeModule;
54 using google_breakpad::StackFrameSymbolizer;
55 using google_breakpad::StackFrame;
56 using google_breakpad::StackFrameARM64;
57 using google_breakpad::Stackwalker;
58 using google_breakpad::StackwalkerARM64;
59 using google_breakpad::SystemInfo;
60 using google_breakpad::WindowsFrameInfo;
61 using google_breakpad::test_assembler::kLittleEndian;
62 using google_breakpad::test_assembler::Label;
63 using google_breakpad::test_assembler::Section;
64 using std::vector;
65 using testing::_;
66 using testing::AnyNumber;
67 using testing::DoAll;
68 using testing::Return;
69 using testing::SetArgumentPointee;
70 using testing::Test;
71
72 class StackwalkerARM64Fixture {
73 public:
StackwalkerARM64Fixture()74 StackwalkerARM64Fixture()
75 : stack_section(kLittleEndian),
76 // Give the two modules reasonable standard locations and names
77 // for tests to play with.
78 module1(0x40000000, 0x10000, "module1", "version1"),
79 module2(0x50000000, 0x10000, "module2", "version2") {
80 // Identify the system as an iOS system.
81 system_info.os = "iOS";
82 system_info.os_short = "ios";
83 system_info.cpu = "arm64";
84 system_info.cpu_info = "";
85
86 // Put distinctive values in the raw CPU context.
87 BrandContext(&raw_context);
88
89 // Create some modules with some stock debugging information.
90 modules.Add(&module1);
91 modules.Add(&module2);
92
93 // By default, none of the modules have symbol info; call
94 // SetModuleSymbols to override this.
95 EXPECT_CALL(supplier, GetCStringSymbolData(_, _, _, _, _))
96 .WillRepeatedly(Return(MockSymbolSupplier::NOT_FOUND));
97
98 // Avoid GMOCK WARNING "Uninteresting mock function call - returning
99 // directly" for FreeSymbolData().
100 EXPECT_CALL(supplier, FreeSymbolData(_)).Times(AnyNumber());
101
102 // Reset max_frames_scanned since it's static.
103 Stackwalker::set_max_frames_scanned(1024);
104 }
105
106 // Set the Breakpad symbol information that supplier should return for
107 // MODULE to INFO.
SetModuleSymbols(MockCodeModule * module,const string & info)108 void SetModuleSymbols(MockCodeModule *module, const string &info) {
109 size_t buffer_size;
110 char *buffer = supplier.CopySymbolDataAndOwnTheCopy(info, &buffer_size);
111 EXPECT_CALL(supplier, GetCStringSymbolData(module, &system_info, _, _, _))
112 .WillRepeatedly(DoAll(SetArgumentPointee<3>(buffer),
113 SetArgumentPointee<4>(buffer_size),
114 Return(MockSymbolSupplier::FOUND)));
115 }
116
117 // Populate stack_region with the contents of stack_section. Use
118 // stack_section.start() as the region's starting address.
RegionFromSection()119 void RegionFromSection() {
120 string contents;
121 ASSERT_TRUE(stack_section.GetContents(&contents));
122 stack_region.Init(stack_section.start().Value(), contents);
123 }
124
125 // Fill RAW_CONTEXT with pseudo-random data, for round-trip checking.
BrandContext(MDRawContextARM64 * raw_context)126 void BrandContext(MDRawContextARM64 *raw_context) {
127 uint8_t x = 173;
128 for (size_t i = 0; i < sizeof(*raw_context); i++)
129 reinterpret_cast<uint8_t *>(raw_context)[i] = (x += 17);
130 }
131
132 SystemInfo system_info;
133 MDRawContextARM64 raw_context;
134 Section stack_section;
135 MockMemoryRegion stack_region;
136 MockCodeModule module1;
137 MockCodeModule module2;
138 MockCodeModules modules;
139 MockSymbolSupplier supplier;
140 BasicSourceLineResolver resolver;
141 CallStack call_stack;
142 const vector<StackFrame *> *frames;
143 };
144
145 class SanityCheck: public StackwalkerARM64Fixture, public Test { };
146
TEST_F(SanityCheck,NoResolver)147 TEST_F(SanityCheck, NoResolver) {
148 // Since the context's frame pointer is garbage, the stack walk will end after
149 // the first frame.
150 StackFrameSymbolizer frame_symbolizer(NULL, NULL);
151 StackwalkerARM64 walker(&system_info, &raw_context, &stack_region, &modules,
152 &frame_symbolizer);
153 // This should succeed even without a resolver or supplier.
154 vector<const CodeModule*> modules_without_symbols;
155 vector<const CodeModule*> modules_with_corrupt_symbols;
156 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols,
157 &modules_with_corrupt_symbols));
158 ASSERT_EQ(0U, modules_without_symbols.size());
159 ASSERT_EQ(0U, modules_with_corrupt_symbols.size());
160 frames = call_stack.frames();
161 ASSERT_EQ(1U, frames->size());
162 StackFrameARM64 *frame = static_cast<StackFrameARM64 *>(frames->at(0));
163 // Check that the values from the original raw context made it
164 // through to the context in the stack frame.
165 EXPECT_EQ(0, memcmp(&raw_context, &frame->context, sizeof(raw_context)));
166 }
167
168 class GetContextFrame: public StackwalkerARM64Fixture, public Test { };
169
170 // The stackwalker should be able to produce the context frame even
171 // without stack memory present.
TEST_F(GetContextFrame,NoStackMemory)172 TEST_F(GetContextFrame, NoStackMemory) {
173 StackFrameSymbolizer frame_symbolizer(&supplier, &resolver);
174 StackwalkerARM64 walker(&system_info, &raw_context, NULL, &modules,
175 &frame_symbolizer);
176 vector<const CodeModule*> modules_without_symbols;
177 vector<const CodeModule*> modules_with_corrupt_symbols;
178 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols,
179 &modules_with_corrupt_symbols));
180 ASSERT_EQ(0U, modules_without_symbols.size());
181 ASSERT_EQ(0U, modules_with_corrupt_symbols.size());
182 frames = call_stack.frames();
183 ASSERT_EQ(1U, frames->size());
184 StackFrameARM64 *frame = static_cast<StackFrameARM64 *>(frames->at(0));
185 // Check that the values from the original raw context made it
186 // through to the context in the stack frame.
187 EXPECT_EQ(0, memcmp(&raw_context, &frame->context, sizeof(raw_context)));
188 }
189
190 class GetCallerFrame: public StackwalkerARM64Fixture, public Test { };
191
TEST_F(GetCallerFrame,ScanWithoutSymbols)192 TEST_F(GetCallerFrame, ScanWithoutSymbols) {
193 // When the stack walker resorts to scanning the stack,
194 // only addresses located within loaded modules are
195 // considered valid return addresses.
196 // Force scanning through three frames to ensure that the
197 // stack pointer is set properly in scan-recovered frames.
198 stack_section.start() = 0x80000000;
199 uint64_t return_address1 = 0x50000100;
200 uint64_t return_address2 = 0x50000900;
201 Label frame1_sp, frame2_sp;
202 stack_section
203 // frame 0
204 .Append(16, 0) // space
205
206 .D64(0x40090000) // junk that's not
207 .D64(0x60000000) // a return address
208
209 .D64(return_address1) // actual return address
210 // frame 1
211 .Mark(&frame1_sp)
212 .Append(16, 0) // space
213
214 .D64(0xF0000000) // more junk
215 .D64(0x0000000D)
216
217 .D64(return_address2) // actual return address
218 // frame 2
219 .Mark(&frame2_sp)
220 .Append(64, 0); // end of stack
221 RegionFromSection();
222
223 raw_context.iregs[MD_CONTEXT_ARM64_REG_PC] = 0x40005510;
224 raw_context.iregs[MD_CONTEXT_ARM64_REG_SP] = stack_section.start().Value();
225
226 StackFrameSymbolizer frame_symbolizer(&supplier, &resolver);
227 StackwalkerARM64 walker(&system_info, &raw_context, &stack_region, &modules,
228 &frame_symbolizer);
229 vector<const CodeModule*> modules_without_symbols;
230 vector<const CodeModule*> modules_with_corrupt_symbols;
231 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols,
232 &modules_with_corrupt_symbols));
233 ASSERT_EQ(2U, modules_without_symbols.size());
234 ASSERT_EQ("module1", modules_without_symbols[0]->debug_file());
235 ASSERT_EQ("module2", modules_without_symbols[1]->debug_file());
236 ASSERT_EQ(0U, modules_with_corrupt_symbols.size());
237 frames = call_stack.frames();
238 ASSERT_EQ(3U, frames->size());
239
240 StackFrameARM64 *frame0 = static_cast<StackFrameARM64 *>(frames->at(0));
241 EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust);
242 ASSERT_EQ(StackFrameARM64::CONTEXT_VALID_ALL,
243 frame0->context_validity);
244 EXPECT_EQ(0, memcmp(&raw_context, &frame0->context, sizeof(raw_context)));
245
246 StackFrameARM64 *frame1 = static_cast<StackFrameARM64 *>(frames->at(1));
247 EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust);
248 ASSERT_EQ((StackFrameARM64::CONTEXT_VALID_PC |
249 StackFrameARM64::CONTEXT_VALID_SP),
250 frame1->context_validity);
251 EXPECT_EQ(return_address1, frame1->context.iregs[MD_CONTEXT_ARM64_REG_PC]);
252 EXPECT_EQ(frame1_sp.Value(), frame1->context.iregs[MD_CONTEXT_ARM64_REG_SP]);
253
254 StackFrameARM64 *frame2 = static_cast<StackFrameARM64 *>(frames->at(2));
255 EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame2->trust);
256 ASSERT_EQ((StackFrameARM64::CONTEXT_VALID_PC |
257 StackFrameARM64::CONTEXT_VALID_SP),
258 frame2->context_validity);
259 EXPECT_EQ(return_address2, frame2->context.iregs[MD_CONTEXT_ARM64_REG_PC]);
260 EXPECT_EQ(frame2_sp.Value(), frame2->context.iregs[MD_CONTEXT_ARM64_REG_SP]);
261 }
262
TEST_F(GetCallerFrame,ScanWithFunctionSymbols)263 TEST_F(GetCallerFrame, ScanWithFunctionSymbols) {
264 // During stack scanning, if a potential return address
265 // is located within a loaded module that has symbols,
266 // it is only considered a valid return address if it
267 // lies within a function's bounds.
268 stack_section.start() = 0x80000000;
269 uint64_t return_address = 0x50000200;
270 Label frame1_sp;
271
272 stack_section
273 // frame 0
274 .Append(16, 0) // space
275
276 .D64(0x40090000) // junk that's not
277 .D64(0x60000000) // a return address
278
279 .D64(0x40001000) // a couple of plausible addresses
280 .D64(0x5000F000) // that are not within functions
281
282 .D64(return_address) // actual return address
283 // frame 1
284 .Mark(&frame1_sp)
285 .Append(64, 0); // end of stack
286 RegionFromSection();
287
288 raw_context.iregs[MD_CONTEXT_ARM64_REG_PC] = 0x40000200;
289 raw_context.iregs[MD_CONTEXT_ARM64_REG_SP] = stack_section.start().Value();
290
291 SetModuleSymbols(&module1,
292 // The youngest frame's function.
293 "FUNC 100 400 10 monotreme\n");
294 SetModuleSymbols(&module2,
295 // The calling frame's function.
296 "FUNC 100 400 10 marsupial\n");
297
298 StackFrameSymbolizer frame_symbolizer(&supplier, &resolver);
299 StackwalkerARM64 walker(&system_info, &raw_context, &stack_region, &modules,
300 &frame_symbolizer);
301 vector<const CodeModule*> modules_without_symbols;
302 vector<const CodeModule*> modules_with_corrupt_symbols;
303 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols,
304 &modules_with_corrupt_symbols));
305 ASSERT_EQ(0U, modules_without_symbols.size());
306 ASSERT_EQ(0U, modules_with_corrupt_symbols.size());
307 frames = call_stack.frames();
308 ASSERT_EQ(2U, frames->size());
309
310 StackFrameARM64 *frame0 = static_cast<StackFrameARM64 *>(frames->at(0));
311 EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust);
312 ASSERT_EQ(StackFrameARM64::CONTEXT_VALID_ALL,
313 frame0->context_validity);
314 EXPECT_EQ(0, memcmp(&raw_context, &frame0->context, sizeof(raw_context)));
315 EXPECT_EQ("monotreme", frame0->function_name);
316 EXPECT_EQ(0x40000100ULL, frame0->function_base);
317
318 StackFrameARM64 *frame1 = static_cast<StackFrameARM64 *>(frames->at(1));
319 EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust);
320 ASSERT_EQ((StackFrameARM64::CONTEXT_VALID_PC |
321 StackFrameARM64::CONTEXT_VALID_SP),
322 frame1->context_validity);
323 EXPECT_EQ(return_address, frame1->context.iregs[MD_CONTEXT_ARM64_REG_PC]);
324 EXPECT_EQ(frame1_sp.Value(), frame1->context.iregs[MD_CONTEXT_ARM64_REG_SP]);
325 EXPECT_EQ("marsupial", frame1->function_name);
326 EXPECT_EQ(0x50000100ULL, frame1->function_base);
327 }
328
TEST_F(GetCallerFrame,ScanFirstFrame)329 TEST_F(GetCallerFrame, ScanFirstFrame) {
330 // If the stackwalker resorts to stack scanning, it will scan much
331 // farther to find the caller of the context frame.
332 stack_section.start() = 0x80000000;
333 uint64_t return_address1 = 0x50000100;
334 uint64_t return_address2 = 0x50000900;
335 Label frame1_sp, frame2_sp;
336 stack_section
337 // frame 0
338 .Append(32, 0) // space
339
340 .D64(0x40090000) // junk that's not
341 .D64(0x60000000) // a return address
342
343 .Append(96, 0) // more space
344
345 .D64(return_address1) // actual return address
346 // frame 1
347 .Mark(&frame1_sp)
348 .Append(32, 0) // space
349
350 .D64(0xF0000000) // more junk
351 .D64(0x0000000D)
352
353 .Append(336, 0) // more space
354
355 .D64(return_address2) // actual return address
356 // (won't be found)
357 // frame 2
358 .Mark(&frame2_sp)
359 .Append(64, 0); // end of stack
360 RegionFromSection();
361
362 raw_context.iregs[MD_CONTEXT_ARM64_REG_PC] = 0x40005510;
363 raw_context.iregs[MD_CONTEXT_ARM64_REG_SP] = stack_section.start().Value();
364
365 StackFrameSymbolizer frame_symbolizer(&supplier, &resolver);
366 StackwalkerARM64 walker(&system_info, &raw_context, &stack_region, &modules,
367 &frame_symbolizer);
368 vector<const CodeModule*> modules_without_symbols;
369 vector<const CodeModule*> modules_with_corrupt_symbols;
370 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols,
371 &modules_with_corrupt_symbols));
372 ASSERT_EQ(2U, modules_without_symbols.size());
373 ASSERT_EQ("module1", modules_without_symbols[0]->debug_file());
374 ASSERT_EQ("module2", modules_without_symbols[1]->debug_file());
375 ASSERT_EQ(0U, modules_with_corrupt_symbols.size());
376 frames = call_stack.frames();
377 ASSERT_EQ(2U, frames->size());
378
379 StackFrameARM64 *frame0 = static_cast<StackFrameARM64 *>(frames->at(0));
380 EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust);
381 ASSERT_EQ(StackFrameARM64::CONTEXT_VALID_ALL,
382 frame0->context_validity);
383 EXPECT_EQ(0, memcmp(&raw_context, &frame0->context, sizeof(raw_context)));
384
385 StackFrameARM64 *frame1 = static_cast<StackFrameARM64 *>(frames->at(1));
386 EXPECT_EQ(StackFrame::FRAME_TRUST_SCAN, frame1->trust);
387 ASSERT_EQ((StackFrameARM64::CONTEXT_VALID_PC |
388 StackFrameARM64::CONTEXT_VALID_SP),
389 frame1->context_validity);
390 EXPECT_EQ(return_address1, frame1->context.iregs[MD_CONTEXT_ARM64_REG_PC]);
391 EXPECT_EQ(frame1_sp.Value(), frame1->context.iregs[MD_CONTEXT_ARM64_REG_SP]);
392 }
393
394 // Test that set_max_frames_scanned prevents using stack scanning
395 // to find caller frames.
TEST_F(GetCallerFrame,ScanningNotAllowed)396 TEST_F(GetCallerFrame, ScanningNotAllowed) {
397 // When the stack walker resorts to scanning the stack,
398 // only addresses located within loaded modules are
399 // considered valid return addresses.
400 stack_section.start() = 0x80000000;
401 uint64_t return_address1 = 0x50000100;
402 uint64_t return_address2 = 0x50000900;
403 Label frame1_sp, frame2_sp;
404 stack_section
405 // frame 0
406 .Append(16, 0) // space
407
408 .D64(0x40090000) // junk that's not
409 .D64(0x60000000) // a return address
410
411 .D64(return_address1) // actual return address
412 // frame 1
413 .Mark(&frame1_sp)
414 .Append(16, 0) // space
415
416 .D64(0xF0000000) // more junk
417 .D64(0x0000000D)
418
419 .D64(return_address2) // actual return address
420 // frame 2
421 .Mark(&frame2_sp)
422 .Append(64, 0); // end of stack
423 RegionFromSection();
424
425 raw_context.iregs[MD_CONTEXT_ARM64_REG_PC] = 0x40005510;
426 raw_context.iregs[MD_CONTEXT_ARM64_REG_SP] = stack_section.start().Value();
427
428 StackFrameSymbolizer frame_symbolizer(&supplier, &resolver);
429 StackwalkerARM64 walker(&system_info, &raw_context, &stack_region, &modules,
430 &frame_symbolizer);
431 Stackwalker::set_max_frames_scanned(0);
432
433 vector<const CodeModule*> modules_without_symbols;
434 vector<const CodeModule*> modules_with_corrupt_symbols;
435 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols,
436 &modules_with_corrupt_symbols));
437 ASSERT_EQ(1U, modules_without_symbols.size());
438 ASSERT_EQ("module1", modules_without_symbols[0]->debug_file());
439 ASSERT_EQ(0U, modules_with_corrupt_symbols.size());
440 frames = call_stack.frames();
441 ASSERT_EQ(1U, frames->size());
442
443 StackFrameARM64 *frame0 = static_cast<StackFrameARM64 *>(frames->at(0));
444 EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust);
445 ASSERT_EQ(StackFrameARM64::CONTEXT_VALID_ALL,
446 frame0->context_validity);
447 EXPECT_EQ(0, memcmp(&raw_context, &frame0->context, sizeof(raw_context)));
448 }
449
450 class GetFramesByFramePointer: public StackwalkerARM64Fixture, public Test { };
451
TEST_F(GetFramesByFramePointer,OnlyFramePointer)452 TEST_F(GetFramesByFramePointer, OnlyFramePointer) {
453 stack_section.start() = 0x80000000;
454 uint64_t return_address1 = 0x50000100;
455 uint64_t return_address2 = 0x50000900;
456 Label frame1_sp, frame2_sp;
457 Label frame1_fp, frame2_fp;
458 stack_section
459 // frame 0
460 .Append(64, 0) // Whatever values on the stack.
461 .D64(0x0000000D) // junk that's not
462 .D64(0xF0000000) // a return address.
463
464 .Mark(&frame1_fp) // Next fp will point to the next value.
465 .D64(frame2_fp) // Save current frame pointer.
466 .D64(return_address2) // Save current link register.
467 .Mark(&frame1_sp)
468
469 // frame 1
470 .Append(64, 0) // Whatever values on the stack.
471 .D64(0x0000000D) // junk that's not
472 .D64(0xF0000000) // a return address.
473
474 .Mark(&frame2_fp)
475 .D64(0)
476 .D64(0)
477 .Mark(&frame2_sp)
478
479 // frame 2
480 .Append(64, 0) // Whatever values on the stack.
481 .D64(0x0000000D) // junk that's not
482 .D64(0xF0000000); // a return address.
483 RegionFromSection();
484
485
486 raw_context.iregs[MD_CONTEXT_ARM64_REG_PC] = 0x40005510;
487 raw_context.iregs[MD_CONTEXT_ARM64_REG_LR] = return_address1;
488 raw_context.iregs[MD_CONTEXT_ARM64_REG_FP] = frame1_fp.Value();
489 raw_context.iregs[MD_CONTEXT_ARM64_REG_SP] = stack_section.start().Value();
490
491 StackFrameSymbolizer frame_symbolizer(&supplier, &resolver);
492 StackwalkerARM64 walker(&system_info, &raw_context,
493 &stack_region, &modules, &frame_symbolizer);
494
495 vector<const CodeModule*> modules_without_symbols;
496 vector<const CodeModule*> modules_with_corrupt_symbols;
497 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols,
498 &modules_with_corrupt_symbols));
499 ASSERT_EQ(2U, modules_without_symbols.size());
500 ASSERT_EQ("module1", modules_without_symbols[0]->debug_file());
501 ASSERT_EQ("module2", modules_without_symbols[1]->debug_file());
502 ASSERT_EQ(0U, modules_with_corrupt_symbols.size());
503 frames = call_stack.frames();
504 ASSERT_EQ(3U, frames->size());
505
506 StackFrameARM64 *frame0 = static_cast<StackFrameARM64 *>(frames->at(0));
507 EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust);
508 ASSERT_EQ(StackFrameARM64::CONTEXT_VALID_ALL,
509 frame0->context_validity);
510 EXPECT_EQ(0, memcmp(&raw_context, &frame0->context, sizeof(raw_context)));
511
512 StackFrameARM64 *frame1 = static_cast<StackFrameARM64 *>(frames->at(1));
513 EXPECT_EQ(StackFrame::FRAME_TRUST_FP, frame1->trust);
514 ASSERT_EQ((StackFrameARM64::CONTEXT_VALID_PC |
515 StackFrameARM64::CONTEXT_VALID_LR |
516 StackFrameARM64::CONTEXT_VALID_FP |
517 StackFrameARM64::CONTEXT_VALID_SP),
518 frame1->context_validity);
519 EXPECT_EQ(return_address1, frame1->context.iregs[MD_CONTEXT_ARM64_REG_PC]);
520 EXPECT_EQ(return_address2, frame1->context.iregs[MD_CONTEXT_ARM64_REG_LR]);
521 EXPECT_EQ(frame1_sp.Value(), frame1->context.iregs[MD_CONTEXT_ARM64_REG_SP]);
522 EXPECT_EQ(frame2_fp.Value(),
523 frame1->context.iregs[MD_CONTEXT_ARM64_REG_FP]);
524
525 StackFrameARM64 *frame2 = static_cast<StackFrameARM64 *>(frames->at(2));
526 EXPECT_EQ(StackFrame::FRAME_TRUST_FP, frame2->trust);
527 ASSERT_EQ((StackFrameARM64::CONTEXT_VALID_PC |
528 StackFrameARM64::CONTEXT_VALID_LR |
529 StackFrameARM64::CONTEXT_VALID_FP |
530 StackFrameARM64::CONTEXT_VALID_SP),
531 frame2->context_validity);
532 EXPECT_EQ(return_address2, frame2->context.iregs[MD_CONTEXT_ARM64_REG_PC]);
533 EXPECT_EQ(0U, frame2->context.iregs[MD_CONTEXT_ARM64_REG_LR]);
534 EXPECT_EQ(frame2_sp.Value(), frame2->context.iregs[MD_CONTEXT_ARM64_REG_SP]);
535 EXPECT_EQ(0U, frame2->context.iregs[MD_CONTEXT_ARM64_REG_FP]);
536 }
537
538 struct CFIFixture: public StackwalkerARM64Fixture {
CFIFixtureCFIFixture539 CFIFixture() {
540 // Provide a bunch of STACK CFI records; we'll walk to the caller
541 // from every point in this series, expecting to find the same set
542 // of register values.
543 SetModuleSymbols(&module1,
544 // The youngest frame's function.
545 "FUNC 4000 1000 10 enchiridion\n"
546 // Initially, nothing has been pushed on the stack,
547 // and the return address is still in the link
548 // register (x30).
549 "STACK CFI INIT 4000 100 .cfa: sp 0 + .ra: x30\n"
550 // Push x19, x20, the frame pointer and the link register.
551 "STACK CFI 4001 .cfa: sp 32 + .ra: .cfa -8 + ^"
552 " x19: .cfa -32 + ^ x20: .cfa -24 + ^ "
553 " x29: .cfa -16 + ^\n"
554 // Save x19..x22 in x0..x3: verify that we populate
555 // the youngest frame with all the values we have.
556 "STACK CFI 4002 x19: x0 x20: x1 x21: x2 x22: x3\n"
557 // Restore x19..x22. Save the non-callee-saves register x1.
558 "STACK CFI 4003 .cfa: sp 40 + x1: .cfa 40 - ^"
559 " x19: x19 x20: x20 x21: x21 x22: x22\n"
560 // Move the .cfa back eight bytes, to point at the return
561 // address, and restore the sp explicitly.
562 "STACK CFI 4005 .cfa: sp 32 + x1: .cfa 32 - ^"
563 " x29: .cfa 8 - ^ .ra: .cfa ^ sp: .cfa 8 +\n"
564 // Recover the PC explicitly from a new stack slot;
565 // provide garbage for the .ra.
566 "STACK CFI 4006 .cfa: sp 40 + pc: .cfa 40 - ^\n"
567
568 // The calling function.
569 "FUNC 5000 1000 10 epictetus\n"
570 // Mark it as end of stack.
571 "STACK CFI INIT 5000 1000 .cfa: 0 .ra: 0\n"
572
573 // A function whose CFI makes the stack pointer
574 // go backwards.
575 "FUNC 6000 1000 20 palinal\n"
576 "STACK CFI INIT 6000 1000 .cfa: sp 8 - .ra: x30\n"
577
578 // A function with CFI expressions that can't be
579 // evaluated.
580 "FUNC 7000 1000 20 rhetorical\n"
581 "STACK CFI INIT 7000 1000 .cfa: moot .ra: ambiguous\n");
582
583 // Provide some distinctive values for the caller's registers.
584 expected.iregs[MD_CONTEXT_ARM64_REG_PC] = 0x0000000040005510L;
585 expected.iregs[MD_CONTEXT_ARM64_REG_SP] = 0x0000000080000000L;
586 expected.iregs[19] = 0x5e68b5d5b5d55e68L;
587 expected.iregs[20] = 0x34f3ebd1ebd134f3L;
588 expected.iregs[21] = 0x74bca31ea31e74bcL;
589 expected.iregs[22] = 0x16b32dcb2dcb16b3L;
590 expected.iregs[23] = 0x21372ada2ada2137L;
591 expected.iregs[24] = 0x557dbbbbbbbb557dL;
592 expected.iregs[25] = 0x8ca748bf48bf8ca7L;
593 expected.iregs[26] = 0x21f0ab46ab4621f0L;
594 expected.iregs[27] = 0x146732b732b71467L;
595 expected.iregs[28] = 0xa673645fa673645fL;
596 expected.iregs[MD_CONTEXT_ARM64_REG_FP] = 0xe11081128112e110L;
597
598 // Expect CFI to recover all callee-saves registers. Since CFI is the
599 // only stack frame construction technique we have, aside from the
600 // context frame itself, there's no way for us to have a set of valid
601 // registers smaller than this.
602 expected_validity = (StackFrameARM64::CONTEXT_VALID_PC |
603 StackFrameARM64::CONTEXT_VALID_SP |
604 StackFrameARM64::CONTEXT_VALID_X19 |
605 StackFrameARM64::CONTEXT_VALID_X20 |
606 StackFrameARM64::CONTEXT_VALID_X21 |
607 StackFrameARM64::CONTEXT_VALID_X22 |
608 StackFrameARM64::CONTEXT_VALID_X23 |
609 StackFrameARM64::CONTEXT_VALID_X24 |
610 StackFrameARM64::CONTEXT_VALID_X25 |
611 StackFrameARM64::CONTEXT_VALID_X26 |
612 StackFrameARM64::CONTEXT_VALID_X27 |
613 StackFrameARM64::CONTEXT_VALID_X28 |
614 StackFrameARM64::CONTEXT_VALID_FP);
615
616 // By default, context frames provide all registers, as normal.
617 context_frame_validity = StackFrameARM64::CONTEXT_VALID_ALL;
618
619 // By default, registers are unchanged.
620 raw_context = expected;
621 }
622
623 // Walk the stack, using stack_section as the contents of the stack
624 // and raw_context as the current register values. (Set the stack
625 // pointer to the stack's starting address.) Expect two stack
626 // frames; in the older frame, expect the callee-saves registers to
627 // have values matching those in 'expected'.
CheckWalkCFIFixture628 void CheckWalk() {
629 RegionFromSection();
630 raw_context.iregs[MD_CONTEXT_ARM64_REG_SP] = stack_section.start().Value();
631
632 StackFrameSymbolizer frame_symbolizer(&supplier, &resolver);
633 StackwalkerARM64 walker(&system_info, &raw_context, &stack_region,
634 &modules, &frame_symbolizer);
635 walker.SetContextFrameValidity(context_frame_validity);
636 vector<const CodeModule*> modules_without_symbols;
637 vector<const CodeModule*> modules_with_corrupt_symbols;
638 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols,
639 &modules_with_corrupt_symbols));
640 ASSERT_EQ(0U, modules_without_symbols.size());
641 ASSERT_EQ(0U, modules_with_corrupt_symbols.size());
642 frames = call_stack.frames();
643 ASSERT_EQ(2U, frames->size());
644
645 StackFrameARM64 *frame0 = static_cast<StackFrameARM64 *>(frames->at(0));
646 EXPECT_EQ(StackFrame::FRAME_TRUST_CONTEXT, frame0->trust);
647 ASSERT_EQ(context_frame_validity, frame0->context_validity);
648 EXPECT_EQ("enchiridion", frame0->function_name);
649 EXPECT_EQ(0x0000000040004000UL, frame0->function_base);
650
651 StackFrameARM64 *frame1 = static_cast<StackFrameARM64 *>(frames->at(1));
652 EXPECT_EQ(StackFrame::FRAME_TRUST_CFI, frame1->trust);
653 ASSERT_EQ(expected_validity, frame1->context_validity);
654 if (expected_validity & StackFrameARM64::CONTEXT_VALID_X1)
655 EXPECT_EQ(expected.iregs[1], frame1->context.iregs[1]);
656 if (expected_validity & StackFrameARM64::CONTEXT_VALID_X19)
657 EXPECT_EQ(expected.iregs[19], frame1->context.iregs[19]);
658 if (expected_validity & StackFrameARM64::CONTEXT_VALID_X20)
659 EXPECT_EQ(expected.iregs[20], frame1->context.iregs[20]);
660 if (expected_validity & StackFrameARM64::CONTEXT_VALID_X21)
661 EXPECT_EQ(expected.iregs[21], frame1->context.iregs[21]);
662 if (expected_validity & StackFrameARM64::CONTEXT_VALID_X22)
663 EXPECT_EQ(expected.iregs[22], frame1->context.iregs[22]);
664 if (expected_validity & StackFrameARM64::CONTEXT_VALID_X23)
665 EXPECT_EQ(expected.iregs[23], frame1->context.iregs[23]);
666 if (expected_validity & StackFrameARM64::CONTEXT_VALID_X24)
667 EXPECT_EQ(expected.iregs[24], frame1->context.iregs[24]);
668 if (expected_validity & StackFrameARM64::CONTEXT_VALID_X25)
669 EXPECT_EQ(expected.iregs[25], frame1->context.iregs[25]);
670 if (expected_validity & StackFrameARM64::CONTEXT_VALID_X26)
671 EXPECT_EQ(expected.iregs[26], frame1->context.iregs[26]);
672 if (expected_validity & StackFrameARM64::CONTEXT_VALID_X27)
673 EXPECT_EQ(expected.iregs[27], frame1->context.iregs[27]);
674 if (expected_validity & StackFrameARM64::CONTEXT_VALID_X28)
675 EXPECT_EQ(expected.iregs[28], frame1->context.iregs[28]);
676 if (expected_validity & StackFrameARM64::CONTEXT_VALID_FP)
677 EXPECT_EQ(expected.iregs[MD_CONTEXT_ARM64_REG_FP],
678 frame1->context.iregs[MD_CONTEXT_ARM64_REG_FP]);
679
680 // We would never have gotten a frame in the first place if the SP
681 // and PC weren't valid or ->instruction weren't set.
682 EXPECT_EQ(expected.iregs[MD_CONTEXT_ARM64_REG_SP],
683 frame1->context.iregs[MD_CONTEXT_ARM64_REG_SP]);
684 EXPECT_EQ(expected.iregs[MD_CONTEXT_ARM64_REG_PC],
685 frame1->context.iregs[MD_CONTEXT_ARM64_REG_PC]);
686 EXPECT_EQ(expected.iregs[MD_CONTEXT_ARM64_REG_PC],
687 frame1->instruction + 4);
688 EXPECT_EQ("epictetus", frame1->function_name);
689 }
690
691 // The values we expect to find for the caller's registers.
692 MDRawContextARM64 expected;
693
694 // The validity mask for expected.
695 uint64_t expected_validity;
696
697 // The validity mask to impose on the context frame.
698 uint64_t context_frame_validity;
699 };
700
701 class CFI: public CFIFixture, public Test { };
702
TEST_F(CFI,At4000)703 TEST_F(CFI, At4000) {
704 stack_section.start() = expected.iregs[MD_CONTEXT_ARM64_REG_SP];
705 raw_context.iregs[MD_CONTEXT_ARM64_REG_PC] = 0x0000000040004000L;
706 raw_context.iregs[MD_CONTEXT_ARM64_REG_LR] = 0x0000000040005510L;
707 CheckWalk();
708 }
709
TEST_F(CFI,At4001)710 TEST_F(CFI, At4001) {
711 Label frame1_sp = expected.iregs[MD_CONTEXT_ARM64_REG_SP];
712 stack_section
713 .D64(0x5e68b5d5b5d55e68L) // saved x19
714 .D64(0x34f3ebd1ebd134f3L) // saved x20
715 .D64(0xe11081128112e110L) // saved fp
716 .D64(0x0000000040005510L) // return address
717 .Mark(&frame1_sp); // This effectively sets stack_section.start().
718 raw_context.iregs[MD_CONTEXT_ARM64_REG_PC] = 0x0000000040004001L;
719 // distinct callee x19, x20 and fp
720 raw_context.iregs[19] = 0xadc9f635a635adc9L;
721 raw_context.iregs[20] = 0x623135ac35ac6231L;
722 raw_context.iregs[MD_CONTEXT_ARM64_REG_FP] = 0x5fc4be14be145fc4L;
723 CheckWalk();
724 }
725
726 // As above, but unwind from a context that has only the PC and SP.
TEST_F(CFI,At4001LimitedValidity)727 TEST_F(CFI, At4001LimitedValidity) {
728 Label frame1_sp = expected.iregs[MD_CONTEXT_ARM64_REG_SP];
729 stack_section
730 .D64(0x5e68b5d5b5d55e68L) // saved x19
731 .D64(0x34f3ebd1ebd134f3L) // saved x20
732 .D64(0xe11081128112e110L) // saved fp
733 .D64(0x0000000040005510L) // return address
734 .Mark(&frame1_sp); // This effectively sets stack_section.start().
735 context_frame_validity =
736 StackFrameARM64::CONTEXT_VALID_PC | StackFrameARM64::CONTEXT_VALID_SP;
737 raw_context.iregs[MD_CONTEXT_ARM64_REG_PC] = 0x0000000040004001L;
738 raw_context.iregs[MD_CONTEXT_ARM64_REG_FP] = 0x5fc4be14be145fc4L;
739
740 expected_validity = (StackFrameARM64::CONTEXT_VALID_PC
741 | StackFrameARM64::CONTEXT_VALID_SP
742 | StackFrameARM64::CONTEXT_VALID_FP
743 | StackFrameARM64::CONTEXT_VALID_X19
744 | StackFrameARM64::CONTEXT_VALID_X20);
745 CheckWalk();
746 }
747
TEST_F(CFI,At4002)748 TEST_F(CFI, At4002) {
749 Label frame1_sp = expected.iregs[MD_CONTEXT_ARM64_REG_SP];
750 stack_section
751 .D64(0xff3dfb81fb81ff3dL) // no longer saved x19
752 .D64(0x34f3ebd1ebd134f3L) // no longer saved x20
753 .D64(0xe11081128112e110L) // saved fp
754 .D64(0x0000000040005510L) // return address
755 .Mark(&frame1_sp); // This effectively sets stack_section.start().
756 raw_context.iregs[MD_CONTEXT_ARM64_REG_PC] = 0x0000000040004002L;
757 raw_context.iregs[0] = 0x5e68b5d5b5d55e68L; // saved x19
758 raw_context.iregs[1] = 0x34f3ebd1ebd134f3L; // saved x20
759 raw_context.iregs[2] = 0x74bca31ea31e74bcL; // saved x21
760 raw_context.iregs[3] = 0x16b32dcb2dcb16b3L; // saved x22
761 raw_context.iregs[19] = 0xadc9f635a635adc9L; // distinct callee x19
762 raw_context.iregs[20] = 0x623135ac35ac6231L; // distinct callee x20
763 raw_context.iregs[21] = 0xac4543564356ac45L; // distinct callee x21
764 raw_context.iregs[22] = 0x2561562f562f2561L; // distinct callee x22
765 // distinct callee fp
766 raw_context.iregs[MD_CONTEXT_ARM64_REG_FP] = 0x5fc4be14be145fc4L;
767 CheckWalk();
768 }
769
TEST_F(CFI,At4003)770 TEST_F(CFI, At4003) {
771 Label frame1_sp = expected.iregs[MD_CONTEXT_ARM64_REG_SP];
772 stack_section
773 .D64(0xdd5a48c848c8dd5aL) // saved x1 (even though it's not callee-saves)
774 .D64(0xff3dfb81fb81ff3dL) // no longer saved x19
775 .D64(0x34f3ebd1ebd134f3L) // no longer saved x20
776 .D64(0xe11081128112e110L) // saved fp
777 .D64(0x0000000040005510L) // return address
778 .Mark(&frame1_sp); // This effectively sets stack_section.start().
779 raw_context.iregs[MD_CONTEXT_ARM64_REG_PC] = 0x0000000040004003L;
780 // distinct callee x1 and fp
781 raw_context.iregs[1] = 0xfb756319fb756319L;
782 raw_context.iregs[MD_CONTEXT_ARM64_REG_FP] = 0x5fc4be14be145fc4L;
783 // caller's x1
784 expected.iregs[1] = 0xdd5a48c848c8dd5aL;
785 expected_validity |= StackFrameARM64::CONTEXT_VALID_X1;
786 CheckWalk();
787 }
788
789 // We have no new rule at module offset 0x4004, so the results here should
790 // be the same as those at module offset 0x4003.
TEST_F(CFI,At4004)791 TEST_F(CFI, At4004) {
792 Label frame1_sp = expected.iregs[MD_CONTEXT_ARM64_REG_SP];
793 stack_section
794 .D64(0xdd5a48c848c8dd5aL) // saved x1 (even though it's not callee-saves)
795 .D64(0xff3dfb81fb81ff3dL) // no longer saved x19
796 .D64(0x34f3ebd1ebd134f3L) // no longer saved x20
797 .D64(0xe11081128112e110L) // saved fp
798 .D64(0x0000000040005510L) // return address
799 .Mark(&frame1_sp); // This effectively sets stack_section.start().
800 raw_context.iregs[MD_CONTEXT_ARM64_REG_PC] = 0x0000000040004004L;
801 // distinct callee x1 and fp
802 raw_context.iregs[1] = 0xfb756319fb756319L;
803 raw_context.iregs[MD_CONTEXT_ARM64_REG_FP] = 0x5fc4be14be145fc4L;
804 // caller's x1
805 expected.iregs[1] = 0xdd5a48c848c8dd5aL;
806 expected_validity |= StackFrameARM64::CONTEXT_VALID_X1;
807 CheckWalk();
808 }
809
810 // Here we move the .cfa, but provide an explicit rule to recover the SP,
811 // so again there should be no change in the registers recovered.
TEST_F(CFI,At4005)812 TEST_F(CFI, At4005) {
813 Label frame1_sp = expected.iregs[MD_CONTEXT_ARM64_REG_SP];
814 stack_section
815 .D64(0xdd5a48c848c8dd5aL) // saved x1 (even though it's not callee-saves)
816 .D64(0xff3dfb81fb81ff3dL) // no longer saved x19
817 .D64(0x34f3ebd1ebd134f3L) // no longer saved x20
818 .D64(0xe11081128112e110L) // saved fp
819 .D64(0x0000000040005510L) // return address
820 .Mark(&frame1_sp); // This effectively sets stack_section.start().
821 raw_context.iregs[MD_CONTEXT_ARM64_REG_PC] = 0x0000000040004005L;
822 raw_context.iregs[1] = 0xfb756319fb756319L; // distinct callee x1
823 expected.iregs[1] = 0xdd5a48c848c8dd5aL; // caller's x1
824 expected_validity |= StackFrameARM64::CONTEXT_VALID_X1;
825 CheckWalk();
826 }
827
828 // Here we provide an explicit rule for the PC, and have the saved .ra be
829 // bogus.
TEST_F(CFI,At4006)830 TEST_F(CFI, At4006) {
831 Label frame1_sp = expected.iregs[MD_CONTEXT_ARM64_REG_SP];
832 stack_section
833 .D64(0x0000000040005510L) // saved pc
834 .D64(0xdd5a48c848c8dd5aL) // saved x1 (even though it's not callee-saves)
835 .D64(0xff3dfb81fb81ff3dL) // no longer saved x19
836 .D64(0x34f3ebd1ebd134f3L) // no longer saved x20
837 .D64(0xe11081128112e110L) // saved fp
838 .D64(0xf8d157835783f8d1L) // .ra rule recovers this, which is garbage
839 .Mark(&frame1_sp); // This effectively sets stack_section.start().
840 raw_context.iregs[MD_CONTEXT_ARM64_REG_PC] = 0x0000000040004006L;
841 raw_context.iregs[1] = 0xfb756319fb756319L; // distinct callee x1
842 expected.iregs[1] = 0xdd5a48c848c8dd5aL; // caller's x1
843 expected_validity |= StackFrameARM64::CONTEXT_VALID_X1;
844 CheckWalk();
845 }
846
847 // Check that we reject rules that would cause the stack pointer to
848 // move in the wrong direction.
TEST_F(CFI,RejectBackwards)849 TEST_F(CFI, RejectBackwards) {
850 raw_context.iregs[MD_CONTEXT_ARM64_REG_PC] = 0x0000000040006000L;
851 raw_context.iregs[MD_CONTEXT_ARM64_REG_SP] = 0x0000000080000000L;
852 raw_context.iregs[MD_CONTEXT_ARM64_REG_LR] = 0x0000000040005510L;
853 StackFrameSymbolizer frame_symbolizer(&supplier, &resolver);
854 StackwalkerARM64 walker(&system_info, &raw_context, &stack_region, &modules,
855 &frame_symbolizer);
856 vector<const CodeModule*> modules_without_symbols;
857 vector<const CodeModule*> modules_with_corrupt_symbols;
858 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols,
859 &modules_with_corrupt_symbols));
860 ASSERT_EQ(0U, modules_without_symbols.size());
861 ASSERT_EQ(0U, modules_with_corrupt_symbols.size());
862 frames = call_stack.frames();
863 ASSERT_EQ(1U, frames->size());
864 }
865
866 // Check that we reject rules whose expressions' evaluation fails.
TEST_F(CFI,RejectBadExpressions)867 TEST_F(CFI, RejectBadExpressions) {
868 raw_context.iregs[MD_CONTEXT_ARM64_REG_PC] = 0x0000000040007000L;
869 raw_context.iregs[MD_CONTEXT_ARM64_REG_SP] = 0x0000000080000000L;
870 StackFrameSymbolizer frame_symbolizer(&supplier, &resolver);
871 StackwalkerARM64 walker(&system_info, &raw_context, &stack_region, &modules,
872 &frame_symbolizer);
873 vector<const CodeModule*> modules_without_symbols;
874 vector<const CodeModule*> modules_with_corrupt_symbols;
875 ASSERT_TRUE(walker.Walk(&call_stack, &modules_without_symbols,
876 &modules_with_corrupt_symbols));
877 ASSERT_EQ(0U, modules_without_symbols.size());
878 ASSERT_EQ(0U, modules_with_corrupt_symbols.size());
879 frames = call_stack.frames();
880 ASSERT_EQ(1U, frames->size());
881 }
882