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
50
use libc::{c_void, ucontext_t, REG_RIP};

#[derive(Clone, Copy, Debug)]
pub struct UContextPtr(*const ucontext_t);

impl UContextPtr {
    #[inline]
    pub fn new(ptr: *const c_void) -> Self {
        assert!(!ptr.is_null(), "non-null context");
        UContextPtr(ptr as *const ucontext_t)
    }

    #[inline]
    pub fn get_ip(self) -> *const c_void {
        let mcontext = &unsafe { *(self.0) }.uc_mcontext;
        mcontext.gregs[REG_RIP as usize] as *const _
    }
}

#[repr(C)]
#[derive(Clone, Copy)]
pub struct UContext {
    context: ucontext_t,
}

impl UContext {
    #[inline]
    pub fn new(ptr: *const c_void) -> Self {
        UContext {
            context: *unsafe {
                (ptr as *const ucontext_t)
                    .as_ref()
                    .expect("non-null context")
            },
        }
    }

    pub fn as_ptr(&mut self) -> UContextPtr {
        UContextPtr::new(&self.context as *const _ as *const _)
    }
}

impl Into<UContext> for UContextPtr {
    #[inline]
    fn into(self) -> UContext {
        UContext {
            context: unsafe { *(self.0) },
        }
    }
}