use std::ffi::c_void;
#[cfg(not(windows))]
mod sysv;
#[cfg(not(windows))]
use self::sysv as abi;
#[cfg(windows)]
mod win64;
#[cfg(windows)]
use self::win64 as abi;
use self::abi::call_original_trampoline;
pub(in crate::hooks) use self::abi::save_registers_trampoline;
pub use self::abi::{INT_ARG_REGS, SSE_ARG_REGS};
pub(in crate::hooks) type SaveRegsEntry =
extern "C" fn(usize, *mut u64, *mut [u8; 16], *mut c_void) -> DispatchResult;
pub(in crate::hooks) extern "C" fn raw_entry(
original: usize,
int_regs: *mut u64,
sse_regs: *mut [u8; 16],
data: *mut c_void,
) -> DispatchResult {
let dispatch: SavedRegsDispatch = unsafe { std::mem::transmute(data) };
dispatch(original, int_regs, sse_regs)
}
pub type SavedRegsDispatch =
extern "C" fn(original: usize, int_regs: *mut u64, sse_regs: *mut [u8; 16]) -> DispatchResult;
pub struct SavedRegs {
pub int_regs: *mut u64,
pub sse_regs: *mut [u8; 16],
}
impl SavedRegs {
pub fn read_int(&self, index: u8) -> u64 {
assert!(
(index as usize) < INT_ARG_REGS,
"integer register index out of range"
);
unsafe { *self.int_regs.add(index as usize) }
}
pub fn write_int(&self, index: u8, val: u64) {
assert!(
(index as usize) < INT_ARG_REGS,
"integer register index out of range"
);
unsafe { *self.int_regs.add(index as usize) = val }
}
pub fn read_f32(&self, index: u8) -> f32 {
assert!(
(index as usize) < SSE_ARG_REGS,
"SSE register index out of range"
);
let bytes = unsafe { &*self.sse_regs.add(index as usize) };
f32::from_le_bytes(bytes[..4].try_into().unwrap())
}
pub fn read_f64(&self, index: u8) -> f64 {
assert!(
(index as usize) < SSE_ARG_REGS,
"SSE register index out of range"
);
let bytes = unsafe { &*self.sse_regs.add(index as usize) };
f64::from_le_bytes(bytes[..8].try_into().unwrap())
}
pub fn write_f32(&self, index: u8, val: f32) {
assert!(
(index as usize) < SSE_ARG_REGS,
"SSE register index out of range"
);
let bytes = unsafe { &mut *self.sse_regs.add(index as usize) };
bytes[..4].copy_from_slice(&val.to_le_bytes());
}
pub fn write_f64(&self, index: u8, val: f64) {
assert!(
(index as usize) < SSE_ARG_REGS,
"SSE register index out of range"
);
let bytes = unsafe { &mut *self.sse_regs.add(index as usize) };
bytes[..8].copy_from_slice(&val.to_le_bytes());
}
}
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
#[repr(C, u64)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum DispatchResult {
Passthrough,
OverrideInt(u64),
OverrideFloat(f64),
}
impl DispatchResult {
pub const fn passthrough() -> Self {
Self::Passthrough
}
pub const fn override_int(val: u64) -> Self {
Self::OverrideInt(val)
}
pub const fn override_float(val: f64) -> Self {
Self::OverrideFloat(val)
}
}
extern "C" fn save_registers_dispatch_shim(
original: usize,
int_regs: *mut u64,
sse_regs: *mut [u8; 16],
) -> DispatchResult {
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let ctx = (original - 2 * std::mem::size_of::<usize>()) as *const [usize; 2];
let [entry_fn, data] = unsafe { std::ptr::read_unaligned(ctx) };
if entry_fn == 0 {
return DispatchResult::passthrough();
}
let entry: SaveRegsEntry = unsafe { std::mem::transmute(entry_fn) };
entry(original, int_regs, sse_regs, data as *mut c_void)
}));
outcome.unwrap_or(DispatchResult::passthrough())
}
#[cfg_attr(feature = "reflect", derive(facet::Facet))]
#[repr(C)]
pub struct CallResult {
pub int_val: u64,
pub float_val: f64,
}
pub unsafe fn call_with_saved_regs(
original: usize,
int_regs: *const u64,
sse_regs: *const [u8; 16],
) -> CallResult {
let mut result = CallResult {
int_val: 0,
float_val: 0.0,
};
unsafe {
call_original_trampoline(original, int_regs, sse_regs, &mut result as *mut CallResult);
}
result
}