-
Notifications
You must be signed in to change notification settings - Fork 1
/
setjmp_longjmp.rs
71 lines (65 loc) · 2.56 KB
/
setjmp_longjmp.rs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#[cfg(all(unix, target_arch = "x86_64"))]
pub use linux_x86_64::{longjmp, setjmp, JumpBuf};
#[cfg(all(unix, target_arch = "x86_64"))]
mod linux_x86_64 {
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct JumpBuf([usize; 8]);
impl JumpBuf {
pub const fn new() -> Self {
Self([0; 8])
}
}
core::arch::global_asm!(
r#"
.global setjmp
setjmp:
mov [rdi], rbx ; // Store caller saved registers
mov [rdi+8], rbp ; // ^
mov [rdi+16], r12 ; // ^
mov [rdi+24], r13 ; // ^
mov [rdi+32], r14 ; // ^
mov [rdi+40], r15 ; // ^
lea rdx, [rsp+8] ; // go one value up (as if setjmp wasn't called)
mov [rdi+48], rdx ; // Store the new rsp pointer in env[7]
mov rdx, [rsp] ; // go one value up (as if setjmp wasn't called)
mov [rdi+56], rdx ; // Store the address we will resume at in env[8]
xor eax, eax ; // Always return 0
ret
"#
);
extern "C-unwind" {
#[link_name = "setjmp"]
pub fn setjmp(env: *mut JumpBuf) -> u32;
}
core::arch::global_asm!(
r#"
.global longjmp
longjmp:
xor eax, eax ; // set eax to 0
cmp esi, 1 ; // CF = val ? 0 : 1
adc eax, esi ; // eax = val + !val ; These two lines add one to ret if equals 0
mov rbx, [rdi] ; // Load in caller saved registers
mov rbp, [rdi+8] ; // ^
mov r12, [rdi+16] ; // ^
mov r13, [rdi+24] ; // ^
mov r14, [rdi+32] ; // ^
mov r15, [rdi+40] ; // ^
mov rsp, [rdi+48] ; // Value of rsp before setjmp call
jmp [rdi+56] ; // goto saved address without altering rsp
"#
);
extern "C-unwind" {
/// Performs transfer of execution to a location dynamically established by [`crate::setjmp`].
///
/// Loads information from the [`crate::JumpBuf`] env. Returns the `ret` value to the caller of
/// [`crate::setjmp`]. If `ret` was mistakenly given as 0, it is incremented to 1.
///
/// Safety:
///
/// Can only jump over any frames that are "Plain Old Frames," aka frames that can be trivially
/// deallocated. POF frames do not contain any pending destructors (live `Drop` objects) or
/// `catch_unwind` calls. (see c-unwind [RFC](https://github.com/rust-lang/rfcs/blob/master/text/2945-c-unwind-abi.md#plain-old-frames))
pub fn longjmp(env: *const JumpBuf, ret: u32) -> !;
}
}