1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! Function hooking (detours) for Linux and Windows on x86-64 and x86.
//!
//! Patch the start of a function so calls run your code instead — the original
//! stays callable, because its overwritten instructions are relocated.
//!
//! # Ways to hook
//!
//! - **Attribute macros** — [`#[hook]`](macro@hook) / [`#[mini_hook]`](macro@mini_hook)
//! create a hook at startup and generate `enable_hook` / `disable_hook`.
//! - **[`Hook`]** — [`Hook::create`] (by address) or [`Hook::by_name`] (by
//! symbol) build a detour whose signature matches the target.
//! - **[`HookBuilder`]** — a fluent front-end: pick a target
//! ([`at`](HookBuilder::at) / [`by_name`](HookBuilder::by_name)), then a
//! detour.
//! - **Save-registers hooks** (x86-64) — hook a function *without knowing its
//! signature*: the trampoline saves the argument registers and calls a
//! dispatch that returns a [`DispatchResult`] (pass through, or override the
//! return). The detour can be a raw function ([`HookBuilder::save_registers`])
//! or a Rust closure ([`HookBuilder::save_registers_fn`]). Each hook carries
//! its own dispatch — no global state.
//!
//! # Example — a detour with [`Hook`]
//!
//! [`Hook::create`] redirects a function to a same-signature detour; the
//! original stays reachable through [`Hook::original_function`].
//!
//! ```no_run
//! use hooking::Hook;
//! # extern "C" fn target(a: u64) -> u64 { a }
//! # extern "C" fn detour(a: u64) -> u64 { a + 1 }
//! let mut hook = unsafe { Hook::create(target as *mut u8, detour as *mut u8) }.unwrap();
//! unsafe { hook.apply_hook().unwrap() }; // calls to `target` now run `detour`
//! unsafe { hook.remove_hook().unwrap() };
//! ```
//!
//! # Example — a closure detour, no signature needed
//!
//! [`HookBuilder`] is the fluent way to reach every hook kind; here a closure
//! detour that doesn't need the target's signature.
//!
//! ```no_run
//! # #[cfg(target_arch = "x86_64")] {
//! use hooking::{Dispatch, DispatchResult, HookBuilder};
//!
//! # extern "C" fn target(_: u64) -> u64 { 0 }
//! let mut hook = unsafe {
//! HookBuilder::at(target as *mut u8).save_registers_fn(|d: Dispatch| {
//! let first = d.regs.read_int(0); // read argument register 0
//! DispatchResult::OverrideInt(first + 1) // return first + 1 instead
//! })
//! }
//! .unwrap();
//! unsafe { hook.apply_hook().unwrap() };
//! # }
//! ```
//!
//! The `examples/` directory covers the rest: the attribute macros, calling the
//! original, argument rewriting, mini hooks, and custom arenas.
//!
//! # Features
//!
//! - `macros` (default) — the `#[hook]` / `#[mini_hook]` attribute macros.
//! - `reflect` — derive `facet::Facet` on [`DispatchResult`] / [`CallResult`]
//! so a reflection-based consumer (e.g. a scripting runtime) can register and
//! return them.
cfg_select!
pub use ;
pub use ;
pub const ORIGINAL_FN_CHECKSUM_MAGIC: usize = 0xDEADBEEFCAFEBABE;
/// The original function of the hook currently running, which its trampoline
/// passes in r10, with a checksum of it in r11. Returns `None` when not
/// called from a hook.
///
/// It must be called at the very start of the hook, before anything else
/// uses those registers. Only x86_64 has registers to spare for this; on any
/// architecture, [`Hook::original_function`] gives a hook's original function.