Skip to main content

Injection

Struct Injection 

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

A code injection, and the RAII guard that owns it.

Reaching the target runs the injected work, then the displaced original instructions, then the rest of the original code. The work is machine code (code, asm) or a register-context callback (inspect, modify). Build one with Injection::at.

§Threads, batches and dropping

These work as for Hook: patching suspends other threads, batch queues changes, and dropping reverts the patch and frees the stub and callback, or leaks them if the patch can’t be reverted. A callback may run on several threads at once, each with its own Context, so keep its own state in atomics or locks.

Implementations§

Source§

impl Injection

Source

pub fn at(target: *const ()) -> InjectionBuilder

Starts building an injection at target.

Nothing is patched until install.

Source

pub fn scan(pattern: &str) -> InjectionScan

Starts building an injection where a byte pattern matches.

The pattern is searched for in the main module’s code when the injection is installed, and must match exactly once; see scan for the syntax. Use offset to land on an instruction inside the match.

Source

pub fn enable(&self) -> Result<()>

Enables the injection.

Does nothing if already enabled. Suspends other threads while patching; see Injection for threads and batches.

§Errors

Returns an error naming the target if patching fails, such as Error::TargetModified when a third party changed its code.

Source

pub fn disable(&self) -> Result<()>

Disables the injection, restoring the original code.

Does nothing if already disabled. Suspends other threads while patching; see Injection for threads and batches.

§Errors

As for enable.

Source

pub unsafe fn enable_unsuspended(&self) -> Result<()>

Enables the injection without suspending other threads.

Faster than enable, and applied at once even inside a batch; see Hook.

§Errors

As for enable.

§Safety

No other thread may run the target’s first bytes while they are written, or it may execute a half-written instruction.

Source

pub unsafe fn disable_unsuspended(&self) -> Result<()>

Disables the injection without suspending other threads.

Applied at once like enable_unsuspended.

§Errors

As for enable.

§Safety

No other thread may run the target’s first bytes while the original ones are written back.

Source

pub fn is_enabled(&self) -> bool

Returns whether the injection is enabled.

Source

pub fn leak(self)

Keeps the injection installed for the rest of the process.

It stays in its current state, so enable it first if it should stay active. The target address stays reserved.

Trait Implementations§

Source§

impl Debug for Injection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Injection

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for Injection

Source§

impl Sync for Injection

Auto Trait Implementations§

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.