ShadowVmt

Struct ShadowVmt 

Source
pub struct ShadowVmt<T: ?Sized = ()> { /* private fields */ }
Expand description

shadow VMT for instance-specific hooking

creates a copy of the original vtable and replaces the object’s vptr to point to the shadow copy. this allows hooking specific instances without affecting other objects of the same class.

Implementations§

Source§

impl<T: ?Sized> ShadowVmt<T>

Source

pub unsafe fn new(object: *mut (), vtable_size: usize) -> Result<Self>

create a shadow VMT for an object

§Arguments
  • object - pointer to the C++ object
  • vtable_size - number of entries in the vtable
§Safety

The object must be a valid C++ object with a vtable. vtable_size must be accurate (too small = missing functions, too large = garbage).

§Example
// create shadow for an object with 10 virtual functions
let mut shadow = unsafe { ShadowVmt::new(object_ptr, 10)? };

// hook the 3rd virtual function
shadow.hook(2, my_detour as usize)?;

// get original to call
let original: fn() = unsafe { std::mem::transmute(shadow.original(2)) };
Source

pub fn hook(&mut self, index: usize, detour: usize) -> Result<()>

hook a virtual function by index

§Arguments
  • index - vtable index to hook
  • detour - address of the detour function
Source

pub fn unhook(&mut self, index: usize) -> Result<()>

unhook a specific index

Source

pub fn unhook_all(&mut self)

unhook all

Source

pub fn original(&self, index: usize) -> Option<usize>

get the original function at an index

Source

pub fn original_vtable(&self) -> usize

get the original vtable address

Source

pub fn shadow_vtable(&self) -> usize

get the shadow vtable address

Source

pub fn vtable_size(&self) -> usize

get the vtable size

Source

pub fn is_hooked(&self, index: usize) -> bool

check if an index is hooked

Source

pub fn hook_count(&self) -> usize

get number of active hooks

Source

pub fn set_auto_restore(&mut self, restore: bool)

set whether to auto-restore on drop

Source

pub fn restore(self) -> Result<()>

restore and consume

Trait Implementations§

Source§

impl<T: ?Sized> Drop for ShadowVmt<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: ?Sized> Send for ShadowVmt<T>

Source§

impl<T: ?Sized> Sync for ShadowVmt<T>

Auto Trait Implementations§

§

impl<T> Freeze for ShadowVmt<T>
where T: ?Sized,

§

impl<T> RefUnwindSafe for ShadowVmt<T>
where T: RefUnwindSafe + ?Sized,

§

impl<T> Unpin for ShadowVmt<T>
where T: Unpin + ?Sized,

§

impl<T> UnwindSafe for ShadowVmt<T>
where T: UnwindSafe + ?Sized,

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.