use std::sync::atomic::AtomicUsize;
#[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(super) static SAVE_REGISTERS_DISPATCH: AtomicUsize = AtomicUsize::new(0);
pub type SavedRegsDispatch = extern "C" fn(
original: usize,
int_regs: *mut u64,
sse_regs: *mut [u8; 16],
result: *mut 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());
}
}
#[derive(Debug, Clone, Copy)]
pub enum HookAction {
Passthrough,
OverrideInt(u64),
OverrideFloat(f64),
}
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct DispatchResult {
action: u64,
int_val: u64,
float_val: f64,
}
impl DispatchResult {
pub fn passthrough() -> Self {
Self {
action: 0,
int_val: 0,
float_val: 0.0,
}
}
pub fn override_int(val: u64) -> Self {
Self {
action: 1,
int_val: val,
float_val: 0.0,
}
}
pub fn override_float(val: f64) -> Self {
Self {
action: 2,
int_val: 0,
float_val: val,
}
}
}
impl From<HookAction> for DispatchResult {
fn from(action: HookAction) -> Self {
match action {
HookAction::Passthrough => Self::passthrough(),
HookAction::OverrideInt(v) => Self::override_int(v),
HookAction::OverrideFloat(v) => Self::override_float(v),
}
}
}
extern "C" fn save_registers_dispatch_shim(
original: usize,
int_regs: *mut u64,
sse_regs: *mut [u8; 16],
result: *mut DispatchResult,
) {
let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
let dispatch_addr = SAVE_REGISTERS_DISPATCH.load(std::sync::atomic::Ordering::Acquire);
if dispatch_addr == 0 {
unsafe { *result = DispatchResult::passthrough() };
return;
}
let dispatch: SavedRegsDispatch = unsafe { std::mem::transmute(dispatch_addr) };
dispatch(original, int_regs, sse_regs, result);
}));
if outcome.is_err() {
unsafe { *result = DispatchResult::passthrough() };
}
}
#[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
}