Skip to main content

forge/
hook.rs

1/// Installs a hook generated by `#[forge::hook]`.
2///
3/// The first argument is the base address (e.g. from `forge::mem::text_addr()`),
4/// and the second is the hook module created by `#[forge::hook]`. The macro
5/// adds the hook's `OFFSET` constant to the base to compute the final target.
6///
7/// An optional third argument passes a context pointer that can be retrieved
8/// inside the hook body with `context!()` or `context!(T)`.
9///
10/// # Examples
11/// ```ignore
12/// // No context
13/// forge::install_hook!(forge::mem::text_addr(), my_hook);
14///
15/// // With context
16/// let mut num: u32 = 3;
17/// forge::install_hook!(forge::mem::text_addr(), my_hook, &mut num);
18/// ```
19/// Note: `num` MUST outlive the hook
20#[macro_export]
21macro_rules! install_hook {
22    ($base:expr, $hook:ident) => {
23        unsafe { $hook::__install($base as u32) }
24    };
25    ($base:expr, $hook:ident, $ctx:expr) => {
26        unsafe {
27            let __ctx = $ctx;
28            $hook::__install_with_ctx($base as u32, __ctx as *const _ as *const ::core::ffi::c_void)
29        }
30    };
31}
32
33/// Updates the context for an already-installed hook.
34///
35/// # Example
36/// ```ignore
37/// forge::update_hook_ctx!(my_hook, &mut new_value);
38/// ```
39#[macro_export]
40macro_rules! update_hook_ctx {
41    ($hook:ident, $ctx:expr) => {
42        unsafe {
43            let __ctx = $ctx;
44            $hook::__update_ctx(__ctx as *const _ as *const ::core::ffi::c_void)
45        }
46    };
47}