hooking 0.8.0

cross platform detour / hooking library in rust
Documentation
//! A trait over every kind of hook.

use std::ffi::{CStr, c_void};
use std::ptr::NonNull;

use super::{Hook, MiniHook};
use crate::error::Result;

/// Operations shared by every kind of hook, so code can work with any of
/// them.
///
/// It is dyn-compatible, so hooks of different kinds can be kept together:
///
/// ```no_run
/// # use hooking::{Hook, HookKind, MiniHook};
/// # let (a, b, hook_a, hook_b) = (std::ptr::null_mut(), std::ptr::null_mut(), std::ptr::null_mut(), std::ptr::null_mut());
/// let mut hooks: Vec<Box<dyn HookKind>> = vec![
///     Box::new(unsafe { Hook::create(a, hook_a) }.unwrap()),
///     Box::new(unsafe { MiniHook::create(b, hook_b) }.unwrap()),
/// ];
/// for hook in &mut hooks {
///     unsafe { hook.apply_hook() }.unwrap();
/// }
/// ```
pub trait HookKind {
    /// Create a hook on `target` using the default arena and patcher.
    ///
    /// # Safety
    /// As for [`Hook::create`].
    unsafe fn create(target: *mut u8, destination: *mut u8) -> Result<Self>
    where
        Self: Sized;

    /// Create a hook on `symbol` in `module`, or in any loaded module when
    /// `module` is `None`, using the default arena and patcher.
    ///
    /// # Safety
    /// As for [`Hook::by_name`].
    unsafe fn by_name(module: Option<&CStr>, symbol: &CStr, destination: *mut u8) -> Result<Self>
    where
        Self: Sized;

    /// The hooked function.
    fn target(&self) -> NonNull<c_void>;

    /// The hooked function's original code, relocated so it can still be
    /// called while the hook is applied.
    fn original_function(&self) -> NonNull<c_void>;

    /// Whether the hook is currently applied.
    fn is_applied(&self) -> bool;

    /// Patch the target so calls to it run the hook. Does nothing if the
    /// hook is already applied.
    ///
    /// # Safety
    /// The target must still be the function the hook was created for, and
    /// nothing else may patch the same bytes while this hook is applied.
    unsafe fn apply_hook(&mut self) -> Result<()>;

    /// Restore the target's original instructions. Does nothing if the hook
    /// isn't applied.
    ///
    /// # Safety
    /// The target's patched bytes must not have been changed by anything
    /// else since the hook was applied.
    unsafe fn remove_hook(&mut self) -> Result<()>;
}

// The default arena and patcher are static, so the hooks they create can be
// returned as hooks of any lifetime.
impl HookKind for Hook<'_> {
    unsafe fn create(target: *mut u8, destination: *mut u8) -> Result<Self> {
        unsafe { Hook::create(target, destination) }
    }

    unsafe fn by_name(module: Option<&CStr>, symbol: &CStr, destination: *mut u8) -> Result<Self> {
        unsafe { Hook::by_name(module, symbol, destination) }
    }

    fn target(&self) -> NonNull<c_void> {
        self.data.symbol_address
    }

    fn original_function(&self) -> NonNull<c_void> {
        Hook::original_function(self)
    }

    fn is_applied(&self) -> bool {
        Hook::is_applied(self)
    }

    unsafe fn apply_hook(&mut self) -> Result<()> {
        unsafe { Hook::apply_hook(self) }
    }

    unsafe fn remove_hook(&mut self) -> Result<()> {
        unsafe { Hook::remove_hook(self) }
    }
}

impl HookKind for MiniHook<'_> {
    unsafe fn create(target: *mut u8, destination: *mut u8) -> Result<Self> {
        unsafe { MiniHook::create(target, destination) }
    }

    unsafe fn by_name(module: Option<&CStr>, symbol: &CStr, destination: *mut u8) -> Result<Self> {
        unsafe { MiniHook::by_name(module, symbol, destination) }
    }

    fn target(&self) -> NonNull<c_void> {
        self.data.symbol_address
    }

    fn original_function(&self) -> NonNull<c_void> {
        MiniHook::original_function(self)
    }

    fn is_applied(&self) -> bool {
        MiniHook::is_applied(self)
    }

    unsafe fn apply_hook(&mut self) -> Result<()> {
        unsafe { MiniHook::apply_hook(self) }
    }

    unsafe fn remove_hook(&mut self) -> Result<()> {
        unsafe { MiniHook::remove_hook(self) }
    }
}