Last active
July 22, 2020 15:27
-
-
Save bkolobara/bc3ac7296c302cf3dcbd3c17aa83328f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub unsafe fn swap(arg: usize, new_sp: *mut u8) -> (usize, *mut u8) { | |
let ret_val: usize; | |
let ret_sp: *mut u8; | |
asm!( | |
"lea {tmp}, [rip + 10]", | |
"push {tmp}", | |
"mov rsi, rsp", | |
"mov rsp, {new_sp}", | |
"pop {tmp}", | |
"jmp {tmp}", | |
tmp = out(reg) _, | |
new_sp = in(reg) new_sp, | |
out("rsi") ret_sp, | |
inout("rdi") arg => ret_val, | |
out("rax") _, out("rcx") _, out("rdx") _, | |
out("r8") _, out("r9") _, out("r10") _, out("r11") _, | |
out("xmm0") _, out("xmm1") _, out("xmm2") _, out("xmm3") _, | |
out("xmm4") _, out("xmm5") _, out("xmm6") _, out("xmm7") _, | |
out("xmm8") _, out("xmm9") _, out("xmm10") _, out("xmm11") _, | |
out("xmm12") _, out("xmm13") _, out("xmm14") _, out("xmm15") _, | |
); | |
(ret_val, ret_sp) | |
} | |
pub fn resume(&mut self, input: Input) -> Option<Output> { | |
let (data_out, stack_ptr) = unsafe { arch::swap(&input as *const Input as usize, self.stack_ptr) }; | |
self.stack_ptr = stack_ptr; | |
std::mem::forget(input); | |
unsafe { std::ptr::read(data_out as *const Option<Output>) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment