hooking 0.7.0

cross platform detour / hooking library in rust
Documentation
//! The trampolines for the Windows x64 ABI.
//!
//! The first four arguments are passed in `rcx, rdx, r8, r9`, or in
//! `xmm0-xmm3` for floats: the Nth argument uses slot N of whichever set
//! matches its type. Every call needs 32 bytes of shadow space below the
//! return address, and `rsi`, `rdi` and `xmm6-xmm15` are callee-saved.

use super::CallResult;
use crate::ORIGINAL_FN_CHECKSUM_MAGIC;

/// Integer argument registers: `[rcx, rdx, r8, r9]`.
pub const INT_ARG_REGS: usize = 4;
/// SSE argument registers: `[xmm0, ..., xmm3]`.
pub const SSE_ARG_REGS: usize = 4;

/// Naked trampoline for [`Hook::create_save_registers`](crate::Hook::create_save_registers).
/// Saves the Windows x64 argument registers, calls the dispatch function,
/// and either restores them for a tail-call to the original or returns an
/// override value.
#[unsafe(naked)]
pub(in crate::hooks) unsafe extern "C" fn save_registers_trampoline() {
    core::arch::naked_asm!(
        // Jumped to from the hook's trampoline, so rsp is 8 past a 16-byte
        // boundary, as at the start of any function.
        "push rbx",
        "push rbp",
        "mov rbp, rsp",
        // Verify the r10/r11 checksum from hooking's trampoline.
        "mov rbx, r10",
        "movabs rax, {magic}",
        "xor rax, r10",
        "cmp rax, r11",
        "jne 9f",
        // Save integer argument registers: [rcx, rdx, r8, r9], with rsp
        // pointing at rcx (index 0) after the pushes.
        "push r9",
        "push r8",
        "push rdx",
        "push rcx",
        // Save SSE argument registers (4 x 16 = 64 bytes).
        "sub rsp, 64",
        "movdqu [rsp+0x00], xmm0",
        "movdqu [rsp+0x10], xmm1",
        "movdqu [rsp+0x20], xmm2",
        "movdqu [rsp+0x30], xmm3",
        // Allocate DispatchResult (24 bytes, rounded to 32).
        "sub rsp, 32",
        // Call dispatch(original, &int_regs, &sse_regs, &result).
        "mov rcx, rbx",            // original stub address
        "lea rdx, [rsp + 32 + 64]", // int_regs: past result + sse
        "lea r8, [rsp + 32]",      // sse_regs: past result
        "mov r9, rsp",             // result: bottom of stack
        // Stack: ret(8) + rbx(8) + rbp(8) + ints(32) + sse(64) + result(32)
        // = 152, which is 8 past a 16-byte boundary. 32 bytes of shadow
        // space plus 8 aligns it for the call.
        "sub rsp, 40",
        "call {dispatch}",
        "add rsp, 40",
        // Read result.action
        "mov rax, [rsp]",
        "test rax, rax",
        "jnz 2f",
        // action=0: Passthrough - restore all regs and tail-call original.
        "add rsp, 32",
        "movdqu xmm0, [rsp+0x00]",
        "movdqu xmm1, [rsp+0x10]",
        "movdqu xmm2, [rsp+0x20]",
        "movdqu xmm3, [rsp+0x30]",
        "add rsp, 64",
        "pop rcx",
        "pop rdx",
        "pop r8",
        "pop r9",
        "mov rax, rbx",
        "mov rsp, rbp",
        "pop rbp",
        "pop rbx",
        "jmp rax",
        "2:",
        // action=1 or 2: override return value.
        "cmp rax, 2",
        "je 3f",
        // action=1: int override - load int_val into rax.
        "mov rax, [rsp + 8]",
        "jmp 4f",
        "3:",
        // action=2: float override - load float_val into xmm0.
        "movsd xmm0, [rsp + 16]",
        "xor eax, eax",
        "4:",
        // Clean up frame and return with the override.
        "mov rsp, rbp",
        "pop rbp",
        "pop rbx",
        "ret",
        "9:",
        // r10/r11 checksum mismatch - not called from a hook, bail.
        "pop rbp",
        "pop rbx",
        "ret",
        magic = const ORIGINAL_FN_CHECKSUM_MAGIC,
        dispatch = sym super::save_registers_dispatch_shim,
    );
}

/// Naked trampoline that loads the Windows x64 argument registers from the
/// saved arrays, calls the original function, and writes rax + xmm0 to the
/// output struct.
///
/// Args (Windows x64 ABI):
///   rcx = original fn ptr
///   rdx = int_regs ptr
///   r8  = sse_regs ptr
///   r9  = result ptr (CallResult)
#[unsafe(naked)]
pub(super) unsafe extern "C" fn call_original_trampoline(
    _original: usize,
    _int_regs: *const u64,
    _sse_regs: *const [u8; 16],
    _result: *mut CallResult,
) {
    core::arch::naked_asm!(
        // Called with rsp 8 past a 16-byte boundary, so after these two
        // pushes rbp is 8 past one too.
        "push rbx",
        "push rbp",
        "mov rbp, rsp",
        // Save the original fn ptr and result ptr, which loading the
        // argument registers would overwrite. Pushing the result ptr
        // leaves rsp 16-byte aligned.
        "mov rbx, rcx", // original
        "push r9",      // result ptr at [rbp-8]
        // Load SSE registers from r8.
        "movdqu xmm0, [r8+0x00]",
        "movdqu xmm1, [r8+0x10]",
        "movdqu xmm2, [r8+0x20]",
        "movdqu xmm3, [r8+0x30]",
        // Load integer registers from rdx (last, as it overwrites rdx and
        // r8, which hold our own arguments).
        "mov rax, rdx",
        "mov rcx, [rax+0]",
        "mov rdx, [rax+8]",
        "mov r8,  [rax+16]",
        "mov r9,  [rax+24]",
        // Shadow space for the callee; rsp stays 16-byte aligned.
        "sub rsp, 32",
        "call rbx",
        "add rsp, 32",
        // Write return values to CallResult.
        "mov rcx, [rbp-8]",    // result ptr
        "mov [rcx], rax",      // int_val = rax
        "movsd [rcx+8], xmm0", // float_val = xmm0
        "mov rsp, rbp",
        "pop rbp",
        "pop rbx",
        "ret",
    );
}