MSHookFunction

Function MSHookFunction 

Source
#[unsafe(no_mangle)]
pub unsafe extern "C" fn MSHookFunction( symbol: *mut c_void, replace: *mut c_void, result: *mut *mut c_void, )
Expand description

Hook a function at runtime by redirecting its execution to a replacement function.

This is the primary C-compatible hooking function that works across all supported architectures (x86-64, ARMv7, ARM64). It installs an inline hook by modifying the target function’s prologue to jump to your replacement function, while preserving the original instructions in a trampoline.

§Arguments

  • symbol - Pointer to the function to hook (must not be null)
  • replace - Pointer to your replacement function (must not be null)
  • result - Output pointer that receives the trampoline address to call the original function. Pass null if you don’t need to call the original function.

§Safety

This function is unsafe because it:

  • Modifies executable code at runtime
  • Requires valid function pointers
  • Changes memory protection flags
  • Can cause undefined behavior if pointers are invalid

§Examples

use substrate::MSHookFunction;
use std::os::raw::c_void;

static mut ORIGINAL: *mut c_void = std::ptr::null_mut();

unsafe extern "C" fn my_replacement() {
    println!("Hooked!");
    if !ORIGINAL.is_null() {
        let orig: extern "C" fn() = std::mem::transmute(ORIGINAL);
        orig();
    }
}

unsafe {
    let target = 0x12345678 as *mut c_void;
    MSHookFunction(target, my_replacement as *mut c_void, &mut ORIGINAL);
}