hook_function

Function hook_function 

Source
pub unsafe fn hook_function<T>(
    symbol: *mut T,
    replace: *mut T,
) -> Result<*mut T>
Expand description

Type-safe Rust wrapper for hooking functions.

This is a generic wrapper around the C API that provides type safety and Result-based error handling. It’s the recommended way to use the hooking functionality from Rust code.

§Type Parameters

  • T - The function type to hook (typically a function pointer)

§Arguments

  • symbol - Pointer to the function to hook
  • replace - Pointer to your replacement function

§Returns

Ok(*mut T) containing the trampoline pointer to call the original function. Err(SubstrateError) if the hook installation fails.

§Safety

This function is unsafe because it modifies executable code at runtime. Both pointers must be valid function pointers of the correct type.

§Examples

use substrate::hook_function;

extern "C" fn original_func(x: i32) -> i32 { x }
extern "C" fn hooked_func(x: i32) -> i32 { x + 1 }

unsafe {
    let trampoline = hook_function(
        original_func as *mut _,
        hooked_func as *mut _
    ).expect("Hook failed");
}