Skip to content

Commit 013877e

Browse files
committed
rust bindings for uc_mem_read_virtual and uc_virtual_to_physical
1 parent 1bc4ccf commit 013877e

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

bindings/rust/src/ffi.rs

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use crate::{Unicorn, UnicornInner};
55

6-
use super::unicorn_const::{uc_error, Arch, HookType, MemRegion, MemType, Mode, Query, TlbEntry};
6+
use super::unicorn_const::{uc_error, Arch, HookType, MemRegion, MemType, Mode, Query, TlbEntry, Permission};
77
use alloc::rc::Weak;
88
use core::cell::UnsafeCell;
99
use core::ffi::c_void;
@@ -48,6 +48,19 @@ extern "C" {
4848
bytes: *mut u8,
4949
size: libc::size_t,
5050
) -> uc_error;
51+
pub fn uc_mem_read_virtual(
52+
engine: uc_handle,
53+
address: u64,
54+
prot: Permission,
55+
bytes: *mut u8,
56+
size: libc::size_t,
57+
) -> uc_error;
58+
pub fn uc_virtual_to_physical(
59+
engine: uc_handle,
60+
address: u64,
61+
prot: Permission,
62+
paddr: *mut u64,
63+
) -> uc_error;
5164
pub fn uc_mem_map(engine: uc_handle, address: u64, size: libc::size_t, perms: u32) -> uc_error;
5265
pub fn uc_mem_map_ptr(
5366
engine: uc_handle,

bindings/rust/src/lib.rs

+21
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,32 @@ impl<'a, D> Unicorn<'a, D> {
336336
unsafe { ffi::uc_mem_read(self.get_handle(), address, buf.as_mut_ptr(), size) }.and(Ok(buf))
337337
}
338338

339+
/// Read a range of bytes from memory at the specified emulated virtual address.
340+
pub fn mem_read_virtual(&self, address: u64, prot: Permission, buf: &mut [u8]) -> Result<(), uc_error> {
341+
unsafe { ffi::uc_mem_read_virtual(self.get_handle(), address, prot, buf.as_mut_ptr(), buf.len()) }.into()
342+
}
343+
344+
/// Return a range of bytes from memory at the specified emulated virtual address as vector.
345+
pub fn mem_read_virtual_as_vec(&self, address: u64, prot: Permission, size: usize) -> Result<Vec<u8>, uc_error> {
346+
let mut buf = vec![0; size];
347+
unsafe { ffi::uc_mem_read_virtual(self.get_handle(), address, prot, buf.as_mut_ptr(), buf.len()) }.and(Ok(buf))
348+
}
349+
339350
/// Write the data in `bytes` to the emulated physical address `address`
340351
pub fn mem_write(&mut self, address: u64, bytes: &[u8]) -> Result<(), uc_error> {
341352
unsafe { ffi::uc_mem_write(self.get_handle(), address, bytes.as_ptr(), bytes.len()) }.into()
342353
}
343354

355+
/// translate virtual to physical address
356+
pub fn virtual_to_physical(&mut self, address: u64, prot: Permission) -> Result<u64, uc_error> {
357+
let mut physical: u64 = 0;
358+
let err = unsafe { ffi::uc_virtual_to_physical(self.get_handle(), address, prot, &mut physical) };
359+
if err != uc_error::OK {
360+
return Err(err);
361+
}
362+
return Ok(physical);
363+
}
364+
344365
/// Map an existing memory region in the emulator at the specified address.
345366
///
346367
/// # Safety

0 commit comments

Comments
 (0)