crash_handler/linux/jmp.rs
1//! FFI bindings for non-local goto
2//!
3//! ```
4//! use crash_handler::jmp;
5//!
6//! unsafe {
7//! let mut jmp_buf = std::mem::MaybeUninit::uninit();
8//!
9//! let val = jmp::sigsetjmp(jmp_buf.as_mut_ptr(), 1);
10//!
11//! if val == 0 {
12//! jmp::siglongjmp(jmp_buf.as_mut_ptr(), 22);
13//! } else {
14//! assert_eq!(val, 22);
15//! }
16//! }
17//! ```
18
19cfg_if::cfg_if! {
20 if #[cfg(target_arch = "x86_64")] {
21 #[repr(C)]
22 #[doc(hidden)]
23 #[allow(non_camel_case_types)]
24 pub struct __jmp_buf([u64; 8]);
25 } else if #[cfg(target_arch = "x86")] {
26 #[repr(C)]
27 #[doc(hidden)]
28 #[allow(non_camel_case_types)]
29 pub struct __jmp_buf([u32; 6]);
30 } else if #[cfg(target_arch = "arm")] {
31 #[repr(C)]
32 #[doc(hidden)]
33 #[allow(non_camel_case_types)]
34 pub struct __jmp_buf([u64; 32]);
35 } else if #[cfg(target_arch = "aarch64")] {
36 #[repr(C)]
37 #[doc(hidden)]
38 #[allow(non_camel_case_types)]
39 pub struct __jmp_buf([u64; 22]);
40 }
41}
42
43/// A jump buffer.
44///
45/// This is essentially the register state of a point in execution at the time
46/// of a [`sigsetjmp`] call that can be returned to by passing this buffer to
47/// [`siglongjmp`].
48#[repr(C)]
49pub struct JmpBuf {
50 /// CPU context
51 __jmp_buf: __jmp_buf,
52 /// Whether the signal mask was saved
53 __fl: u32,
54 /// Saved signal mask
55 __ss: [u32; 32],
56}
57
58unsafe extern "C" {
59 /// Set jump point for a non-local goto.
60 ///
61 /// The return value will be 0 if this is a direct invocation (ie the "first
62 /// time" `sigsetjmp` is executed), and will be the value passed to `siglongjmp`
63 /// otherwise.
64 ///
65 /// See [sigsetjmp](https://man7.org/linux/man-pages/man3/sigsetjmp.3p.html)
66 /// for more information.
67 #[cfg_attr(target_env = "gnu", link_name = "__sigsetjmp")]
68 pub fn sigsetjmp(jb: *mut JmpBuf, save_mask: i32) -> i32;
69 /// Non-local goto with signal handling
70 ///
71 /// The value passed here will be returned by `sigsetjmp` when returning
72 /// to that callsite. Note that passing a value of 0 here will be changed
73 /// to a 1.
74 ///
75 /// See [siglongjmp](https://man7.org/linux/man-pages/man3/siglongjmp.3p.html)
76 /// for more information.
77 pub fn siglongjmp(jb: *mut JmpBuf, val: i32) -> !;
78}