xur_

> module

std::stub::hooking

low-level inline function hooking

use std::stub::hooking; feature hooking native only

std::stub::hooking provides low-level inline function hooking by address or symbol name. For Il2Cpp method hooking, prefer Method.hook() or Method.hook_void_block() from std::stub::il2cpp, which handle ABI marshalling automatically. This module is for hooking arbitrary native functions where you manage the calling convention yourself.

> functions

# by_name(module_or_symbol: str, symbol_or_dest: str | i64, dest?: i64) -> Hook

Create a hook by looking up a symbol: by_name(symbol, dest) searches every loaded library, by_name(module, symbol, dest) only module. Returns a bare Hook (not a Result) and raises a runtime error if the symbol isn't found or the arguments don't fit either form.

# create(target: i64, dest: i64) -> Result<Hook, HookError>

Create a hook that redirects calls from the target address to the dest address. The hook is not active until apply() is called. Fails with HookError::CreateFailed if the target can't be hooked.

> types

┌ type Hook

These return a Result whose error is a HookError (LockPoisoned, CreateFailed, ApplyFailed or RemoveFailed, each but the first carrying a message) — thread them with ?.

# hook.apply() -> Result<(), HookError>

Activate the hook. Calls to the target are redirected to the destination.

# hook.original() -> Result<i64, HookError>

Get the address of the trampoline that calls the original function.

# hook.remove() -> Result<(), HookError>

Remove the hook and restore the original code.

# hook.target() -> Result<i64, HookError>

Get the target address being hooked.

┌ enum HookError
HookError::LockPoisoned
HookError::CreateFailed(str)
HookError::ApplyFailed(str)
HookError::RemoveFailed(str)
┌ type Skip

An opaque value; see the functions that return it.

> examples

use std::stub::hooking;

// `detour` is the address of your replacement function.
fn install(detour: i64) -> Result<()> {
    // Hook a function by address
    let hook = hooking::create(0x7f0001000, detour)?;
    hook.apply()?;
    let orig = hook.original()?;

    // Hook by symbol name
    // (by_name raises instead of returning a Result)
    let hook = hooking::by_name("libgame.so", "GameUpdate", detour);
    hook.apply()?;

    // Remove when done
    hook.remove()?;
    Ok(())
}