Skip to content

Commit

Permalink
rust bindings for uc_mem_read_virtual
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippTakacs committed Feb 28, 2025
1 parent 596cf40 commit 5d62f1f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bindings/rust/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ extern "C" {
bytes: *mut u8,
size: libc::size_t,
) -> uc_error;
pub fn uc_mem_read_virtual(
engine: uc_handle,
address: u64,
prot: Permission,
bytes: *mut u8,
size: libc::size_t,
) -> uc_error;
pub fn uc_mem_map(engine: uc_handle, address: u64, size: libc::size_t, perms: u32) -> uc_error;
pub fn uc_mem_map_ptr(
engine: uc_handle,
Expand Down
11 changes: 11 additions & 0 deletions bindings/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,17 @@ impl<'a, D> Unicorn<'a, D> {
unsafe { ffi::uc_mem_read(self.get_handle(), address, buf.as_mut_ptr(), size) }.and(Ok(buf))
}

/// Read a range of bytes from memory at the specified emulated virtual address.
pub fn mem_read_virtual(&self, address: u64, prot: Permission, buf: &mut [u8]) -> Result<(), uc_error> {
unsafe { ffi::uc_mem_read_virtual(self.get_handle(), address, prot, buf.as_mut_ptr(), buf.len()) }.into()
}

/// Return a range of bytes from memory at the specified emulated virtual address as vector.
pub fn mem_read_virtual_as_vec(&self, address: u64, prot: Permission, buf: &mut [u8]) -> Result<Vec<u8>, uc_error> {
let mut buf = vec![0; size];
unsafe { ffi::uc_mem_read_virtual(self.get_handle(), address, prot, buf.as_mut_ptr(), buf.len()) }.and(Ok(buf))
}

/// Write the data in `bytes` to the emulated physical address `address`
pub fn mem_write(&mut self, address: u64, bytes: &[u8]) -> Result<(), uc_error> {
unsafe { ffi::uc_mem_write(self.get_handle(), address, bytes.as_ptr(), bytes.len()) }.into()
Expand Down

0 comments on commit 5d62f1f

Please sign in to comment.