Skip to content

Commit

Permalink
Feature - JIT enable host stack frames (#31)
Browse files Browse the repository at this point in the history
* Adds cfg feature "jit-enable-host-stack-frames".

* Adds stmt_expr_attribute_asm!().
  • Loading branch information
Lichtso authored Feb 13, 2025
1 parent 7362297 commit 4b59104
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ libc = { version = "0.2", optional = true }
[features]
default = ["jit"]
jit = ["libc", "winapi"]
jit-enable-host-stack-frames = ["jit"]
fuzzer-not-safe-for-production = ["arbitrary"]
debugger = ["gdbstub"]
shuttle-test = ["dep:shuttle"]
Expand Down
25 changes: 21 additions & 4 deletions src/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,30 @@ impl JitProgram {
registers: [u64; 12],
) {
unsafe {
std::arch::asm!(
let runtime_environment = std::ptr::addr_of_mut!(*vm)
.cast::<u64>()
.offset(get_runtime_environment_key() as isize);
let instruction_meter =
(vm.previous_instruction_meter as i64).wrapping_add(registers[11] as i64);
let entrypoint = &self.text_section
[self.pc_section[registers[11] as usize] as usize & (i32::MAX as u32 as usize)]
as *const u8;
macro_rules! stmt_expr_attribute_asm {
($($prologue:literal,)+ cfg(not(feature = $feature:literal)), $guarded:tt, $($epilogue:tt)+) => {
#[cfg(feature = $feature)]
std::arch::asm!($($prologue,)+ $($epilogue)+);
#[cfg(not(feature = $feature))]
std::arch::asm!($($prologue,)+ $guarded, $($epilogue)+);
}
}
stmt_expr_attribute_asm!(
// RBP and RBX must be saved and restored manually in the current version of rustc and llvm.
"push rbx",
"push rbp",
"mov [{host_stack_pointer}], rsp",
"add QWORD PTR [{host_stack_pointer}], -8",
// RBP is zeroed out in order not to compromise the runtime environment (RDI) encryption.
cfg(not(feature = "jit-enable-host-stack-frames")),
"xor rbp, rbp",
"mov [rsp-8], rax",
"mov rax, [r11 + 0x00]",
Expand All @@ -146,9 +163,9 @@ impl JitProgram {
"pop rbp",
"pop rbx",
host_stack_pointer = in(reg) &mut vm.host_stack_pointer,
inlateout("rdi") std::ptr::addr_of_mut!(*vm).cast::<u64>().offset(get_runtime_environment_key() as isize) => _,
inlateout("r10") (vm.previous_instruction_meter as i64).wrapping_add(registers[11] as i64) => _,
inlateout("rax") &self.text_section[self.pc_section[registers[11] as usize] as usize & (i32::MAX as u32 as usize)] as *const u8 => _,
inlateout("rdi") runtime_environment => _,
inlateout("r10") instruction_meter => _,
inlateout("rax") entrypoint => _,
inlateout("r11") &registers => _,
lateout("rsi") _, lateout("rdx") _, lateout("rcx") _, lateout("r8") _,
lateout("r9") _, lateout("r12") _, lateout("r13") _, lateout("r14") _, lateout("r15") _,
Expand Down

0 comments on commit 4b59104

Please sign in to comment.