lucet_runtime_internals/sysdeps/
linux.rs

1use libc::{c_void, ucontext_t, REG_RIP};
2
3#[derive(Clone, Copy, Debug)]
4pub struct UContextPtr(*const ucontext_t);
5
6impl UContextPtr {
7    #[inline]
8    pub fn new(ptr: *const c_void) -> Self {
9        assert!(!ptr.is_null(), "non-null context");
10        UContextPtr(ptr as *const ucontext_t)
11    }
12
13    #[inline]
14    pub fn get_ip(self) -> *const c_void {
15        let mcontext = &unsafe { *(self.0) }.uc_mcontext;
16        mcontext.gregs[REG_RIP as usize] as *const _
17    }
18}
19
20#[repr(C)]
21#[derive(Clone, Copy)]
22pub struct UContext {
23    context: ucontext_t,
24}
25
26impl UContext {
27    #[inline]
28    pub fn new(ptr: *const c_void) -> Self {
29        UContext {
30            context: *unsafe {
31                (ptr as *const ucontext_t)
32                    .as_ref()
33                    .expect("non-null context")
34            },
35        }
36    }
37
38    pub fn as_ptr(&mut self) -> UContextPtr {
39        UContextPtr::new(&self.context as *const _ as *const _)
40    }
41}
42
43impl Into<UContext> for UContextPtr {
44    #[inline]
45    fn into(self) -> UContext {
46        UContext {
47            context: unsafe { *(self.0) },
48        }
49    }
50}