Skip to main content

Hook

Struct Hook 

Source
pub struct Hook { /* private fields */ }
Expand description

An installed inline hook that redirects a target function to a detour.

The hook overwrites the first few bytes of the target function with a jump to the detour. A trampoline is allocated nearby containing the original instructions and a jump back, allowing the detour to call the original.

The hook is automatically removed when dropped.

Implementations§

Source§

impl Hook

Source

pub unsafe fn install(target: *const u8, detour: *const u8) -> Result<Self>

Install an inline hook at target, redirecting calls to detour.

Returns a Hook whose trampoline can be used to call the original function.

§Safety
  • target must point to the start of a callable function in executable memory.
  • detour must be a function with the same calling convention and signature.
  • No thread may be executing the first 14 bytes of target during this call.
Source

pub fn trampoline(&self) -> *const u8

Returns a pointer to the trampoline that calls the original function.

Transmute this to the original function’s type to call it:

let original: extern "C" fn(i32) -> i32 = std::mem::transmute(hook.trampoline());
Source

pub unsafe fn unhook(&mut self) -> Result<()>

Remove the hook, restoring the original function bytes.

§Safety

No thread may be executing the trampoline or the patched region of the target.

Trait Implementations§

Source§

impl Drop for Hook

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Hook

Source§

impl Sync for Hook

Auto Trait Implementations§

§

impl Freeze for Hook

§

impl RefUnwindSafe for Hook

§

impl Unpin for Hook

§

impl UnsafeUnpin for Hook

§

impl UnwindSafe for Hook

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.