Skip to content

Commit

Permalink
Merge pull request #1574 from nviennot/dev
Browse files Browse the repository at this point in the history
Rust bindings for insn_invalid_hook
  • Loading branch information
wtdcode authored Apr 3, 2022
2 parents ec808c9 + af8a409 commit ca81d46
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
12 changes: 12 additions & 0 deletions bindings/rust/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,18 @@ pub extern "C" fn insn_in_hook_proxy<D, F>(
(user_data.callback)(&mut user_data.uc, port, size);
}

pub extern "C" fn insn_invalid_hook_proxy<D, F>(
uc: uc_handle,
user_data: *mut UcHook<D, F>,
) -> bool
where
F: FnMut(&mut crate::Unicorn<D>) -> bool,
{
let user_data = unsafe { &mut *user_data };
debug_assert_eq!(uc, user_data.uc.get_handle());
(user_data.callback)(&mut user_data.uc)
}

pub extern "C" fn insn_out_hook_proxy<D, F>(
uc: uc_handle,
port: u32,
Expand Down
33 changes: 33 additions & 0 deletions bindings/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,39 @@ impl<'a, D> Unicorn<'a, D> {
}
}

/// Add hook for invalid instructions
pub fn add_insn_invalid_hook<F: 'a>(&mut self, callback: F) -> Result<ffi::uc_hook, uc_error>
where
F: FnMut(&mut Unicorn<D>) -> bool,
{
let mut hook_ptr = core::ptr::null_mut();
let mut user_data = Box::new(ffi::UcHook {
callback,
uc: Unicorn {
inner: self.inner.clone(),
},
});

let err = unsafe {
ffi::uc_hook_add(
self.get_handle(),
&mut hook_ptr,
HookType::INSN_INVALID,
ffi::insn_invalid_hook_proxy::<D, F> as _,
user_data.as_mut() as *mut _ as _,
0,
0,
)
};
if err == uc_error::OK {
self.inner_mut().hooks.push((hook_ptr, user_data));

Ok(hook_ptr)
} else {
Err(err)
}
}

/// Add hook for x86 IN instruction.
pub fn add_insn_in_hook<F: 'a>(&mut self, callback: F) -> Result<ffi::uc_hook, uc_error>
where
Expand Down

0 comments on commit ca81d46

Please sign in to comment.