Debian Codegraph @

third_party/rust/minidump-unwind/src/amd64_unittest.rs

raw
1 // Copyright 2015 Ted Mielczarek. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3
4 use crate::*;
5 use minidump::format::CONTEXT_AMD64;
6 use minidump::system_info::{Cpu, Os};
7 use std::collections::HashMap;
8 use test_assembler::*;
9
10 struct TestFixture {
11 pub raw: CONTEXT_AMD64,
12 pub modules: MinidumpModuleList,
13 pub system_info: SystemInfo,
14 pub symbols: HashMap<String, String>,
15 }
16
17 impl TestFixture {
18 pub fn new() -> TestFixture {
19 TestFixture {
20 raw: CONTEXT_AMD64::default(),
21 // Give the two modules reasonable standard locations and names
22 // for tests to play with.
23 modules: MinidumpModuleList::from_modules(vec![
24 MinidumpModule::new(0x00007400c0000000, 0x10000, "module1"),
25 MinidumpModule::new(0x00007500b0000000, 0x10000, "module2"),
26 ]),
27 system_info: SystemInfo {
28 os: Os::Linux,
29 os_version: None,
30 os_build: None,
31 cpu: Cpu::X86_64,
32 cpu_info: None,
33 cpu_microcode_version: None,
34 cpu_count: 1,
35 },
36 symbols: HashMap::new(),
37 }
38 }
39
40 pub async fn walk_stack(&self, stack: Section) -> CallStack {
41 let context = MinidumpContext {
42 raw: MinidumpRawContext::Amd64(self.raw.clone()),
43 valid: MinidumpContextValidity::All,
44 };
45 let base = stack.start().value().unwrap();
46 let size = stack.size();
47 let stack = stack.get_contents().unwrap();
48 let stack_memory = &MinidumpMemory {
49 desc: Default::default(),
50 base_address: base,
51 size,
52 bytes: &stack,
53 endian: scroll::LE,
54 };
55 let symbolizer = Symbolizer::new(string_symbol_supplier(self.symbols.clone()));
56 let mut stack = CallStack::with_context(context);
57
58 walk_stack(
59 0,
60 (),
61 &mut stack,
62 Some(UnifiedMemory::Memory(stack_memory)),
63 &self.modules,
64 &self.system_info,
65 &symbolizer,
66 )
67 .await;
68
69 stack
70 }
71
72 pub fn add_symbols(&mut self, name: String, symbols: String) {
73 self.symbols.insert(name, symbols);
74 }
75 }
76
77 #[tokio::test]
78 async fn test_simple() {
79 let mut f = TestFixture::new();
80 let stack = Section::new();
81 stack.start().set_const(0x80000000);
82 // There should be no references to the stack in this walk: we don't
83 // provide any call frame information, so trying to reconstruct the
84 // context frame's caller should fail. So there's no need for us to
85 // provide stack contents.
86 f.raw.rip = 0x00007400c0000200;
87 f.raw.rbp = 0x8000000080000000;
88
89 let s = f.walk_stack(stack).await;
90 assert_eq!(s.frames.len(), 1);
91 let f = &s.frames[0];
92 let m = f.module.as_ref().unwrap();
93 assert_eq!(m.code_file(), "module1");
94 }
95
96 #[tokio::test]
97 async fn test_caller_pushed_rbp() {
98 // Functions typically push their %rbp upon entry and set %rbp pointing
99 // there. If stackwalking finds a plausible address for the next frame's
100 // %rbp directly below the return address, assume that it is indeed the
101 // next frame's %rbp.
102 let mut f = TestFixture::new();
103 let mut stack = Section::new();
104 let stack_start = 0x8000000080000000;
105 let return_address = 0x00007500b0000110;
106 stack.start().set_const(stack_start);
107
108 let frame0_rbp = Label::new();
109 let frame1_sp = Label::new();
110 let frame1_rbp = Label::new();
111
112 stack = stack
113 // frame 0
114 .append_repeated(0, 16) // space
115 .D64(0x00007400b0000000) // junk that's not
116 .D64(0x00007500b0000000) // a return address
117 .D64(0x00007400c0001000) // a couple of plausible addresses
118 .D64(0x00007500b000aaaa) // that are not within functions
119 .mark(&frame0_rbp)
120 .D64(&frame1_rbp) // caller-pushed %rbp
121 .D64(return_address) // actual return address
122 // frame 1
123 .mark(&frame1_sp)
124 .append_repeated(0, 32) // body of frame1
125 .mark(&frame1_rbp) // end of stack
126 .D64(0);
127
128 f.raw.rip = 0x00007400c0000200;
129 f.raw.rbp = frame0_rbp.value().unwrap();
130 f.raw.rsp = stack.start().value().unwrap();
131
132 let s = f.walk_stack(stack).await;
133 assert_eq!(s.frames.len(), 2);
134
135 {
136 // To avoid reusing locals by mistake
137 let f0 = &s.frames[0];
138 assert_eq!(f0.trust, FrameTrust::Context);
139 assert_eq!(f0.context.valid, MinidumpContextValidity::All);
140 if let MinidumpRawContext::Amd64(ctx) = &f0.context.raw {
141 assert_eq!(ctx.rbp, frame0_rbp.value().unwrap());
142 } else {
143 unreachable!();
144 }
145 }
146
147 {
148 // To avoid reusing locals by mistake
149 let f1 = &s.frames[1];
150 assert_eq!(f1.trust, FrameTrust::FramePointer);
151 if let MinidumpContextValidity::Some(ref which) = f1.context.valid {
152 assert!(which.contains("rip"));
153 assert!(which.contains("rsp"));
154 assert!(which.contains("rbp"));
155 } else {
156 unreachable!();
157 }
158 if let MinidumpRawContext::Amd64(ctx) = &f1.context.raw {
159 assert_eq!(ctx.rip, return_address);
160 assert_eq!(ctx.rsp, frame1_sp.value().unwrap());
161 assert_eq!(ctx.rbp, frame1_rbp.value().unwrap());
162 } else {
163 unreachable!();
164 }
165 }
166 }
167
168 #[tokio::test]
169 async fn test_windows_rbp_scan() {
170 let mut f = TestFixture::new();
171 f.system_info.os = Os::Windows;
172
173 let mut stack = Section::new();
174 let stack_start = 0x8000000080000000;
175 let return_address = 0x00007500b0000110;
176 stack.start().set_const(stack_start);
177
178 let frame0_rbp = Label::new();
179 let frame1_sp = Label::new();
180 let frame1_rbp = Label::new();
181
182 stack = stack
183 // frame 0
184 .append_repeated(0, 16) // space
185 .D64(0x00000000b0000000) // junk that's not
186 .D64(0x00000000b0000000) // a return address
187 .mark(&frame0_rbp) // the FP can point to the middle of the stack on Windows
188 .D64(0x00000000c0001000)
189 .D64(0x00000000b000aaaa)
190 .D64(&frame1_rbp) // caller-pushed %rbp
191 .D64(return_address) // actual return address
192 // frame 1
193 .mark(&frame1_sp)
194 .append_repeated(0, 32) // body of frame1
195 .mark(&frame1_rbp) // end of stack
196 .D64(0);
197
198 f.raw.rip = 0x00007400c0000200;
199 f.raw.rbp = frame0_rbp.value().unwrap();
200 f.raw.rsp = stack.start().value().unwrap();
201
202 let s = f.walk_stack(stack).await;
203 assert_eq!(s.frames.len(), 2);
204
205 {
206 // To avoid reusing locals by mistake
207 let f0 = &s.frames[0];
208 assert_eq!(f0.trust, FrameTrust::Context);
209 assert_eq!(f0.context.valid, MinidumpContextValidity::All);
210 if let MinidumpRawContext::Amd64(ctx) = &f0.context.raw {
211 assert_eq!(ctx.rbp, frame0_rbp.value().unwrap());
212 } else {
213 unreachable!();
214 }
215 }
216
217 {
218 // To avoid reusing locals by mistake
219 let f1 = &s.frames[1];
220 assert_eq!(f1.trust, FrameTrust::Scan);
221 if let MinidumpContextValidity::Some(ref which) = f1.context.valid {
222 assert!(which.contains("rip"));
223 assert!(which.contains("rsp"));
224 } else {
225 unreachable!();
226 }
227 if let MinidumpRawContext::Amd64(ctx) = &f1.context.raw {
228 assert_eq!(ctx.rip, return_address);
229 assert_eq!(ctx.rsp, frame1_sp.value().unwrap());
230 } else {
231 unreachable!();
232 }
233 }
234 }
235
236 #[tokio::test]
237 async fn test_scan_without_symbols() {
238 // When the stack walker resorts to scanning the stack,
239 // only addresses located within loaded modules are
240 // considered valid return addresses.
241 // Force scanning through three frames to ensure that the
242 // stack pointer is set properly in scan-recovered frames.
243 let mut f = TestFixture::new();
244 let mut stack = Section::new();
245 let stack_start = 0x8000000080000000;
246 stack.start().set_const(stack_start);
247
248 let return_address1 = 0x00007500b0000100;
249 let return_address2 = 0x00007500b0000900;
250
251 let frame1_sp = Label::new();
252 let frame2_sp = Label::new();
253 let frame1_rbp = Label::new();
254 stack = stack
255 // frame 0
256 .append_repeated(0, 16) // space
257 .D64(0x00007400b0000000) // junk that's not
258 .D64(0x00007500d0000000) // a return address
259 .D64(return_address1) // actual return address
260 // frame 1
261 .mark(&frame1_sp)
262 .append_repeated(0, 16) // space
263 .D64(0x00007400b0000000) // more junk
264 .D64(0x00007500d0000000)
265 .mark(&frame1_rbp)
266 .D64(stack_start) // This is in the right place to be
267 // a saved rbp, but it's bogus, so
268 // we shouldn't report it.
269 .D64(return_address2) // actual return address
270 // frame 2
271 .mark(&frame2_sp)
272 .append_repeated(0, 32); // end of stack
273
274 f.raw.rip = 0x00007400c0000200;
275 f.raw.rbp = frame1_rbp.value().unwrap();
276 f.raw.rsp = stack.start().value().unwrap();
277
278 let s = f.walk_stack(stack).await;
279 assert_eq!(s.frames.len(), 3);
280
281 {
282 // To avoid reusing locals by mistake
283 let f0 = &s.frames[0];
284 assert_eq!(f0.trust, FrameTrust::Context);
285 assert_eq!(f0.context.valid, MinidumpContextValidity::All);
286 }
287
288 {
289 // To avoid reusing locals by mistake
290 let f1 = &s.frames[1];
291 assert_eq!(f1.trust, FrameTrust::Scan);
292 if let MinidumpContextValidity::Some(ref which) = f1.context.valid {
293 assert!(which.contains("rip"));
294 assert!(which.contains("rsp"));
295 assert!(which.contains("rbp"));
296 } else {
297 unreachable!();
298 }
299
300 if let MinidumpRawContext::Amd64(ctx) = &f1.context.raw {
301 assert_eq!(ctx.rip, return_address1);
302 assert_eq!(ctx.rsp, frame1_sp.value().unwrap());
303 assert_eq!(ctx.rbp, frame1_rbp.value().unwrap());
304 } else {
305 unreachable!();
306 }
307 }
308
309 {
310 // To avoid reusing locals by mistake
311 let f2 = &s.frames[2];
312 assert_eq!(f2.trust, FrameTrust::Scan);
313 if let MinidumpContextValidity::Some(ref which) = f2.context.valid {
314 assert!(which.contains("rip"));
315 assert!(which.contains("rsp"));
316 } else {
317 unreachable!();
318 }
319
320 if let MinidumpRawContext::Amd64(ctx) = &f2.context.raw {
321 assert_eq!(ctx.rip, return_address2);
322 assert_eq!(ctx.rsp, frame2_sp.value().unwrap());
323 } else {
324 unreachable!();
325 }
326 }
327 }
328
329 #[tokio::test]
330 async fn test_scan_with_symbols() {
331 // Test that we can refine our scanning using symbols. Specifically we
332 // should be able to reject pointers that are in modules but don't map to
333 // any FUNC/PUBLIC record.
334 let mut f = TestFixture::new();
335 let mut stack = Section::new();
336 let stack_start = 0x8000000080000000u64;
337 stack.start().set_const(stack_start);
338
339 let return_address = 0x00007500b0000110u64;
340
341 let frame1_rsp = Label::new();
342 let frame1_rbp = Label::new();
343 stack = stack
344 // frame 0
345 .append_repeated(0, 16) // space
346 .D64(0x00007400b0000000u64) // junk that's not
347 .D64(0x00007500b0000000u64) // a return address
348 .D64(0x00007400c0001000u64) // a couple of plausible addresses
349 .D64(0x00007500b000aaaau64) // that are not within functions
350 .D64(return_address) // actual return address
351 // frame 1
352 .mark(&frame1_rsp)
353 .append_repeated(0, 32)
354 .mark(&frame1_rbp); // end of stack
355
356 f.raw.rip = 0x00007400c0000200;
357 f.raw.rbp = frame1_rbp.value().unwrap();
358 f.raw.rsp = stack.start().value().unwrap();
359
360 f.add_symbols(
361 String::from("module1"),
362 // The youngest frame's function.
363 String::from("FUNC 100 400 10 monotreme\n"),
364 );
365 f.add_symbols(
366 String::from("module2"),
367 // The calling frame's function.
368 String::from("FUNC 100 400 10 marsupial\n"),
369 );
370
371 let s = f.walk_stack(stack).await;
372 assert_eq!(s.frames.len(), 2);
373
374 {
375 // Frame 0
376 let frame = &s.frames[0];
377 assert_eq!(frame.trust, FrameTrust::Context);
378 assert_eq!(frame.context.valid, MinidumpContextValidity::All);
379 }
380
381 {
382 // Frame 1
383 let frame = &s.frames[1];
384 let valid = &frame.context.valid;
385 assert_eq!(frame.trust, FrameTrust::Scan);
386 if let MinidumpContextValidity::Some(ref which) = valid {
387 assert_eq!(which.len(), 3);
388 } else {
389 unreachable!();
390 }
391
392 if let MinidumpRawContext::Amd64(ctx) = &frame.context.raw {
393 assert_eq!(ctx.get_register("rip", valid).unwrap(), return_address);
394 assert_eq!(
395 ctx.get_register("rsp", valid).unwrap(),
396 frame1_rsp.value().unwrap()
397 );
398 assert_eq!(
399 ctx.get_register("rbp", valid).unwrap(),
400 frame1_rbp.value().unwrap()
401 );
402 } else {
403 unreachable!();
404 }
405 }
406 }
407
408 const CALLEE_SAVE_REGS: &[&str] = &["rip", "rbx", "rbp", "rsp", "r12", "r13", "r14", "r15"];
409
410 fn init_cfi_state() -> (TestFixture, Section, CONTEXT_AMD64, MinidumpContextValidity) {
411 let mut f = TestFixture::new();
412 let symbols = [
413 // The youngest frame's function.
414 "FUNC 4000 1000 10 enchiridion\n",
415 // Initially, just a return address.
416 "STACK CFI INIT 4000 100 .cfa: $rsp 8 + .ra: .cfa 8 - ^\n",
417 // Push %rbx.
418 "STACK CFI 4001 .cfa: $rsp 16 + $rbx: .cfa 16 - ^\n",
419 // Save %r12 in %rbx. Weird, but permitted.
420 "STACK CFI 4002 $r12: $rbx\n",
421 // Allocate frame space, and save %r13.
422 "STACK CFI 4003 .cfa: $rsp 40 + $r13: .cfa 32 - ^\n",
423 // Put the return address in %r13.
424 "STACK CFI 4005 .ra: $r13\n",
425 // Save %rbp, and use it as a frame pointer.
426 "STACK CFI 4006 .cfa: $rbp 16 + $rbp: .cfa 24 - ^\n",
427 // The calling function.
428 "FUNC 5000 1000 10 epictetus\n",
429 // Mark it as end of stack.
430 "STACK CFI INIT 5000 1000 .cfa: $rsp .ra 0\n",
431 ];
432 f.add_symbols(String::from("module1"), symbols.concat());
433
434 f.raw.set_register("rsp", 0x8000000080000000);
435 f.raw.set_register("rip", 0x00007400c0005510);
436 f.raw.set_register("rbp", 0x68995b1de4700266);
437 f.raw.set_register("rbx", 0x5a5beeb38de23be8);
438 f.raw.set_register("r12", 0xed1b02e8cc0fc79c);
439 f.raw.set_register("r13", 0x1d20ad8acacbe930);
440 f.raw.set_register("r14", 0xe94cffc2f7adaa28);
441 f.raw.set_register("r15", 0xb638d17d8da413b5);
442
443 let raw_valid = MinidumpContextValidity::All;
444
445 let expected = f.raw.clone();
446 let expected_regs = CALLEE_SAVE_REGS;
447 let expected_valid = MinidumpContextValidity::Some(expected_regs.iter().copied().collect());
448
449 let stack = Section::new();
450 stack
451 .start()
452 .set_const(f.raw.get_register("rsp", &raw_valid).unwrap());
453
454 (f, stack, expected, expected_valid)
455 }
456
457 async fn check_cfi(
458 f: TestFixture,
459 stack: Section,
460 expected: CONTEXT_AMD64,
461 expected_valid: MinidumpContextValidity,
462 ) {
463 let s = f.walk_stack(stack).await;
464 assert_eq!(s.frames.len(), 2);
465
466 {
467 // Frame 0
468 let frame = &s.frames[0];
469 assert_eq!(frame.trust, FrameTrust::Context);
470 assert_eq!(frame.context.valid, MinidumpContextValidity::All);
471 }
472
473 {
474 // Frame 1
475 if let MinidumpContextValidity::Some(ref expected_regs) = expected_valid {
476 let frame = &s.frames[1];
477 let valid = &frame.context.valid;
478 assert_eq!(frame.trust, FrameTrust::CallFrameInfo);
479 if let MinidumpContextValidity::Some(ref which) = valid {
480 assert_eq!(which.len(), expected_regs.len());
481 } else {
482 unreachable!();
483 }
484
485 if let MinidumpRawContext::Amd64(ctx) = &frame.context.raw {
486 for reg in expected_regs {
487 assert_eq!(
488 ctx.get_register(reg, valid),
489 expected.get_register(reg, &expected_valid),
490 "{reg} registers didn't match!"
491 );
492 }
493 return;
494 }
495 }
496 }
497 unreachable!();
498 }
499
500 #[tokio::test]
501 async fn test_cfi_at_4000() {
502 let (mut f, mut stack, mut expected, expected_valid) = init_cfi_state();
503
504 let frame1_rsp = Label::new();
505 stack = stack
506 .D64(0x00007400c0005510)
507 .mark(&frame1_rsp)
508 .append_repeated(0, 1000);
509
510 expected.set_register("rsp", frame1_rsp.value().unwrap());
511 f.raw.set_register("rip", 0x00007400c0004000);
512
513 check_cfi(f, stack, expected, expected_valid).await;
514 }
515
516 #[tokio::test]
517 async fn test_cfi_at_4001() {
518 let (mut f, mut stack, mut expected, expected_valid) = init_cfi_state();
519
520 let frame1_rsp = Label::new();
521 stack = stack
522 .D64(0x5a5beeb38de23be8) // saved %rbx
523 .D64(0x00007400c0005510) // return address
524 .mark(&frame1_rsp)
525 .append_repeated(0, 1000);
526
527 expected.set_register("rsp", frame1_rsp.value().unwrap());
528 f.raw.set_register("rip", 0x00007400c0004001);
529 f.raw.set_register("rbx", 0xbe0487d2f9eafe29);
530
531 check_cfi(f, stack, expected, expected_valid).await;
532 }
533
534 #[tokio::test]
535 async fn test_cfi_at_4002() {
536 let (mut f, mut stack, mut expected, expected_valid) = init_cfi_state();
537
538 let frame1_rsp = Label::new();
539 stack = stack
540 .D64(0x5a5beeb38de23be8) // saved %rbx
541 .D64(0x00007400c0005510) // return address
542 .mark(&frame1_rsp)
543 .append_repeated(0, 1000);
544
545 expected.set_register("rsp", frame1_rsp.value().unwrap());
546 f.raw.set_register("rip", 0x00007400c0004002);
547 f.raw.set_register("rbx", 0xed1b02e8cc0fc79c); // saved %r12
548 f.raw.set_register("r12", 0xb0118de918a4bcea); // callee's (distinct) %r12 value
549
550 check_cfi(f, stack, expected, expected_valid).await;
551 }
552
553 #[tokio::test]
554 async fn test_cfi_at_4003() {
555 let (mut f, mut stack, mut expected, expected_valid) = init_cfi_state();
556
557 let frame1_rsp = Label::new();
558 stack = stack
559 .D64(0x0e023828dffd4d81) // garbage
560 .D64(0x1d20ad8acacbe930) // saved %r13
561 .D64(0x319e68b49e3ace0f) // garbage
562 .D64(0x5a5beeb38de23be8) // saved %rbx
563 .D64(0x00007400c0005510) // return address
564 .mark(&frame1_rsp)
565 .append_repeated(0, 1000);
566
567 expected.set_register("rsp", frame1_rsp.value().unwrap());
568 f.raw.set_register("rip", 0x00007400c0004003);
569 f.raw.set_register("rbx", 0xed1b02e8cc0fc79c); // saved %r12
570 f.raw.set_register("r12", 0x89d04fa804c87a43); // callee's (distinct) %r12
571 f.raw.set_register("r13", 0x5118e02cbdb24b03); // callee's (distinct) %r13
572
573 check_cfi(f, stack, expected, expected_valid).await;
574 }
575
576 #[tokio::test]
577 async fn test_cfi_at_4004() {
578 let (mut f, mut stack, mut expected, expected_valid) = init_cfi_state();
579
580 let frame1_rsp = Label::new();
581 stack = stack
582 .D64(0x0e023828dffd4d81) // garbage
583 .D64(0x1d20ad8acacbe930) // saved %r13
584 .D64(0x319e68b49e3ace0f) // garbage
585 .D64(0x5a5beeb38de23be8) // saved %rbx
586 .D64(0x00007400c0005510) // return address
587 .mark(&frame1_rsp)
588 .append_repeated(0, 1000);
589
590 expected.set_register("rsp", frame1_rsp.value().unwrap());
591 f.raw.set_register("rip", 0x00007400c0004004);
592 f.raw.set_register("rbx", 0xed1b02e8cc0fc79c); // saved %r12
593 f.raw.set_register("r12", 0x46b1b8868891b34a); // callee's (distinct) %r12
594 f.raw.set_register("r13", 0x5118e02cbdb24b03); // callee's (distinct) %r13
595
596 check_cfi(f, stack, expected, expected_valid).await;
597 }
598
599 #[tokio::test]
600 async fn test_cfi_at_4005() {
601 let (mut f, mut stack, mut expected, expected_valid) = init_cfi_state();
602
603 let frame1_rsp = Label::new();
604 stack = stack
605 .D64(0x4b516dd035745953) // garbage
606 .D64(0x1d20ad8acacbe930) // saved %r13
607 .D64(0xa6d445e16ae3d872) // garbage
608 .D64(0x5a5beeb38de23be8) // saved %rbx
609 .D64(0xaa95fa054aedfbae) // garbage
610 .mark(&frame1_rsp)
611 .append_repeated(0, 1000);
612
613 expected.set_register("rsp", frame1_rsp.value().unwrap());
614 f.raw.set_register("rip", 0x00007400c0004005);
615 f.raw.set_register("rbx", 0xed1b02e8cc0fc79c); // saved %r12
616 f.raw.set_register("r12", 0x46b1b8868891b34a); // callee's %r12
617 f.raw.set_register("r13", 0x00007400c0005510); // return address
618
619 check_cfi(f, stack, expected, expected_valid).await;
620 }
621
622 #[tokio::test]
623 async fn test_cfi_at_4006() {
624 let (mut f, mut stack, mut expected, expected_valid) = init_cfi_state();
625
626 let frame0_rbp = Label::new();
627 let frame1_rsp = Label::new();
628 stack = stack
629 .D64(0x043c6dfceb91aa34) // garbage
630 .D64(0x1d20ad8acacbe930) // saved %r13
631 .D64(0x68995b1de4700266) // saved %rbp
632 .mark(&frame0_rbp) // frame pointer points here
633 .D64(0x5a5beeb38de23be8) // saved %rbx
634 .D64(0xf015ee516ad89eab) // garbage
635 .mark(&frame1_rsp)
636 .append_repeated(0, 1000);
637
638 expected.set_register("rsp", frame1_rsp.value().unwrap());
639 f.raw.set_register("rip", 0x00007400c0004006);
640 f.raw.set_register("rbp", frame0_rbp.value().unwrap());
641 f.raw.set_register("rbx", 0xed1b02e8cc0fc79c); // saved %r12
642 f.raw.set_register("r12", 0x26e007b341acfebd); // callee's %r12
643 f.raw.set_register("r13", 0x00007400c0005510); // return address
644
645 check_cfi(f, stack, expected, expected_valid).await;
646 }
647
648 #[tokio::test]
649 async fn test_frame_pointer_overflow() {
650 // Make sure we don't explode when trying frame pointer analysis on a value
651 // that will overflow.
652
653 type Pointer = u64;
654 let stack_max: Pointer = Pointer::MAX;
655 let stack_size: Pointer = 1000;
656 let bad_frame_ptr: Pointer = stack_max;
657
658 let mut f = TestFixture::new();
659 let mut stack = Section::new();
660 let stack_start: Pointer = stack_max - stack_size;
661 stack.start().set_const(stack_start);
662
663 stack = stack
664 // frame 0
665 .append_repeated(0, stack_size as usize); // junk, not important to the test
666
667 f.raw.rip = 0x00007400c0000200;
668 f.raw.rbp = bad_frame_ptr;
669 f.raw.rsp = stack.start().value().unwrap() as Pointer;
670
671 let s = f.walk_stack(stack).await;
672 assert_eq!(s.frames.len(), 1);
673
674 // As long as we don't panic, we're good!
675 }
676
677 #[tokio::test]
678 async fn test_frame_pointer_barely_no_overflow() {
679 // This is test_caller_pushed_rbp but with the all the values pushed
680 // as close to the upper memory boundary as possible, to confirm that
681 // our code doesn't randomly overflow *AND* isn't overzealous in
682 // its overflow guards.
683
684 let mut f = TestFixture::new();
685 let mut stack = Section::new();
686
687 type Pointer = u64;
688 let stack_max: Pointer = Pointer::MAX;
689 let pointer_size: Pointer = std::mem::size_of::<Pointer>() as Pointer;
690 let stack_size: Pointer = pointer_size * 3;
691
692 let stack_start: Pointer = stack_max - stack_size;
693 let return_address: Pointer = 0x00007500b0000110;
694 stack.start().set_const(stack_start);
695
696 let frame0_fp = Label::new();
697 let frame1_sp = Label::new();
698 let frame1_fp = Label::new();
699
700 stack = stack
701 // frame 0
702 .mark(&frame0_fp)
703 .D64(&frame1_fp) // caller-pushed %rbp
704 .D64(return_address) // actual return address
705 // frame 1
706 .mark(&frame1_sp)
707 .mark(&frame1_fp) // end of stack
708 .D64(0);
709
710 f.raw.rip = 0x00007400c0000200;
711 f.raw.rbp = frame0_fp.value().unwrap() as Pointer;
712 f.raw.rsp = stack.start().value().unwrap();
713
714 let s = f.walk_stack(stack).await;
715 assert_eq!(s.frames.len(), 2);
716
717 {
718 // To avoid reusing locals by mistake
719 let f0 = &s.frames[0];
720 assert_eq!(f0.trust, FrameTrust::Context);
721 assert_eq!(f0.context.valid, MinidumpContextValidity::All);
722 if let MinidumpRawContext::Amd64(ctx) = &f0.context.raw {
723 assert_eq!(ctx.rbp, frame0_fp.value().unwrap() as Pointer);
724 } else {
725 unreachable!();
726 }
727 }
728
729 {
730 // To avoid reusing locals by mistake
731 let f1 = &s.frames[1];
732 assert_eq!(f1.trust, FrameTrust::FramePointer);
733 if let MinidumpContextValidity::Some(ref which) = f1.context.valid {
734 assert!(which.contains("rip"));
735 assert!(which.contains("rsp"));
736 assert!(which.contains("rbp"));
737 } else {
738 unreachable!();
739 }
740 if let MinidumpRawContext::Amd64(ctx) = &f1.context.raw {
741 assert_eq!(ctx.rip, return_address);
742 assert_eq!(ctx.rsp, frame1_sp.value().unwrap() as Pointer);
743 assert_eq!(ctx.rbp, frame1_fp.value().unwrap() as Pointer);
744 } else {
745 unreachable!();
746 }
747 }
748 }
749
750 #[tokio::test]
751 async fn test_scan_walk_overflow() {
752 // There's a possible overflow when address_of_ip starts out at 0.
753 //
754 // To avoid this, we only try to recover rbp when we're scanning at least
755 // 1 pointer width away from the start of the stack.
756 let mut f = TestFixture::new();
757 let mut stack = Section::new();
758 let stack_start = 0;
759 stack.start().set_const(stack_start);
760
761 let return_address1 = 0x00007500b0000100_u64;
762
763 let frame1_sp = Label::new();
764 let frame1_rbp = Label::new();
765
766 stack = stack
767 // frame 0
768 .D64(return_address1) // actual return address
769 // frame 1
770 .mark(&frame1_sp)
771 .append_repeated(0, 16) // space
772 .D64(0x00007400b0000000) // more junk
773 .D64(0x00007500d0000000)
774 .mark(&frame1_rbp);
775
776 f.raw.rip = 0x00007400c0000200;
777 f.raw.rbp = frame1_rbp.value().unwrap();
778 f.raw.rsp = stack.start().value().unwrap();
779
780 let s = f.walk_stack(stack).await;
781 assert_eq!(s.frames.len(), 2);
782
783 {
784 // To avoid reusing locals by mistake
785 let f0 = &s.frames[0];
786 assert_eq!(f0.trust, FrameTrust::Context);
787 assert_eq!(f0.context.valid, MinidumpContextValidity::All);
788 }
789
790 {
791 // To avoid reusing locals by mistake
792 let f1 = &s.frames[1];
793 assert_eq!(f1.trust, FrameTrust::Scan);
794 if let MinidumpContextValidity::Some(ref which) = f1.context.valid {
795 assert!(which.contains("rip"));
796 assert!(which.contains("rsp"));
797 } else {
798 unreachable!();
799 }
800
801 if let MinidumpRawContext::Amd64(ctx) = &f1.context.raw {
802 assert_eq!(ctx.rip, return_address1);
803 assert_eq!(ctx.rsp, frame1_sp.value().unwrap());
804 // We were unable to recover rbp, so it defaulted to 0.
805 assert_eq!(ctx.rbp, 0);
806 } else {
807 unreachable!();
808 }
809 }
810 }