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>
impl<T: ?Sized> ShadowVmt<T>
Sourcepub unsafe fn new(object: *mut (), vtable_size: usize) -> Result<Self>
pub unsafe fn new(object: *mut (), vtable_size: usize) -> Result<Self>
create a shadow VMT for an object
§Arguments
object- pointer to the C++ objectvtable_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)) };Sourcepub fn hook(&mut self, index: usize, detour: usize) -> Result<()>
pub fn hook(&mut self, index: usize, detour: usize) -> Result<()>
hook a virtual function by index
§Arguments
index- vtable index to hookdetour- address of the detour function
Sourcepub fn unhook_all(&mut self)
pub fn unhook_all(&mut self)
unhook all
Sourcepub fn original_vtable(&self) -> usize
pub fn original_vtable(&self) -> usize
get the original vtable address
Sourcepub fn shadow_vtable(&self) -> usize
pub fn shadow_vtable(&self) -> usize
get the shadow vtable address
Sourcepub fn vtable_size(&self) -> usize
pub fn vtable_size(&self) -> usize
get the vtable size
Sourcepub fn hook_count(&self) -> usize
pub fn hook_count(&self) -> usize
get number of active hooks
Sourcepub fn set_auto_restore(&mut self, restore: bool)
pub fn set_auto_restore(&mut self, restore: bool)
set whether to auto-restore on drop
Trait Implementations§
impl<T: ?Sized> Send for ShadowVmt<T>
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>
impl<T> UnwindSafe for ShadowVmt<T>where
T: UnwindSafe + ?Sized,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more