# `detour-rs` [![Azure build Status][azure-shield]][azure] [![crates.io version][crate-shield]][crate] [![Documentation][docs-shield]][docs] [![Language (Rust)][rust-shield]][rust]

This is a cross-platform detour library developed in Rust. Beyond the basic functionality, this library handles branch redirects, RIP-relative instructions, hot-patching, NOP-padded functions, and allows the original function to be called using a trampoline whilst hooked.

This is one of few cross-platform detour libraries that exists, and to maintain this feature, not all desired functionality can be supported due to lack of cross-platform APIs. Therefore EIP relocation is not supported.

NOTE: Nightly is currently required for static_detour! and is enabled by default.

Platforms

This library provides CI for these targets:

Installation

Add this to your Cargo.toml:

toml [dependencies] detour = "0.8.0"

Example

```rust use std::error::Error; use detour3::static_detour;

static_detour! { static Test: /* extern "X" */ fn(i32) -> i32; }

fn add5(val: i32) -> i32 { val + 5 }

fn add10(val: i32) -> i32 { val + 10 }

fn main() -> Result<(), Box> { // Reroute the 'add5' function to 'add10' (can also be a closure) unsafe { Test.initialize(add5, add10)? };

asserteq!(add5(1), 6); asserteq!(Test.call(1), 6);

// Hooks must be enabled to take effect unsafe { Test.enable()? };

// The original function is detoured to 'add10' assert_eq!(add5(1), 11);

// The original function can still be invoked using 'call' assert_eq!(Test.call(1), 6);

// It is also possible to change the detour whilst hooked Test.setdetour(|val| val - 5); asserteq!(add5(5), 0);

unsafe { Test.disable()? };

assert_eq!(add5(1), 6); Ok(()) } ```

Mentions

Part of the library's external user interface was inspired by minhook-rs, created by Jascha-N, and it contains derivative code of his work.

Appendix