summaryrefslogtreecommitdiffstats
path: root/third_party/rust/crash-context/tests/capture_context.rs
blob: 641e1dd7069c4478d3a085774522df460ed77377 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#[test]
fn captures() {
    fn one() {
        two();
    }

    fn two() {
        three();
    }

    #[allow(unsafe_code)]
    fn three() {
        cfg_if::cfg_if! {
            if #[cfg(target_os = "windows")] {
                let ctx = unsafe {
                    let mut ctx = std::mem::MaybeUninit::zeroed();
                    crash_context::capture_context(ctx.as_mut_ptr());

                    ctx.assume_init()
                };

                cfg_if::cfg_if! {
                    if #[cfg(target_arch = "x86_64")] {
                        assert!(ctx.Rbp != 0);
                        assert!(ctx.Rsp != 0);
                        assert!(ctx.Rip != 0);
                    } else if #[cfg(target_arch = "x86")] {
                        assert!(ctx.Ebp != 0);
                        assert!(ctx.Esp != 0);
                        assert!(ctx.Eip != 0);
                    }
                }
            } else if #[cfg(all(target_os = "linux", target_arch = "x86_64"))] {
                let ctx = unsafe {
                    let mut ctx = std::mem::MaybeUninit::zeroed();
                    assert_eq!(crash_context::crash_context_getcontext(ctx.as_mut_ptr()), 0);
                    ctx.assume_init()
                };

                let gregs = &ctx.uc_mcontext.gregs;
                assert!(gregs[libc::REG_RBP as usize] != 0);
                assert!(gregs[libc::REG_RSP as usize] != 0);
                assert!(gregs[libc::REG_RIP as usize] != 0);
            }
        }
    }

    one();
}