pub struct Unicorn<'a, D: 'a> { /* private fields */ }
Expand description
A Unicorn emulator instance.
You could clone this instance cheaply, since it has an Rc
inside.
Implementations§
Source§impl<'a> Unicorn<'a, ()>
impl<'a> Unicorn<'a, ()>
Source§impl<'a, D> Unicorn<'a, D>where
D: 'a,
impl<'a, D> Unicorn<'a, D>where
D: 'a,
Sourcepub fn new_with_data(
arch: Arch,
mode: Mode,
data: D,
) -> Result<Unicorn<'a, D>, uc_error>
pub fn new_with_data( arch: Arch, mode: Mode, data: D, ) -> Result<Unicorn<'a, D>, uc_error>
Create a new instance of the unicorn engine for the specified architecture and hardware mode.
Source§impl<'a, D> Unicorn<'a, D>
impl<'a, D> Unicorn<'a, D>
Sourcepub fn get_data(&self) -> &D
pub fn get_data(&self) -> &D
Return whatever data was passed during initialization.
For an example, have a look at utils::init_emu_with_heap
where
a struct is passed which is used for a custom allocator.
Sourcepub fn get_data_mut(&mut self) -> &mut D
pub fn get_data_mut(&mut self) -> &mut D
Return a mutable reference to whatever data was passed during initialization.
Sourcepub fn get_handle(&self) -> *mut uc_engine
pub fn get_handle(&self) -> *mut uc_engine
Return the handle of the current emulator.
Sourcepub fn mem_regions(&self) -> Result<Vec<MemRegion>, uc_error>
pub fn mem_regions(&self) -> Result<Vec<MemRegion>, uc_error>
Returns a vector with the memory regions that are mapped in the emulator.
Sourcepub fn mem_read(&self, address: u64, buf: &mut [u8]) -> Result<(), uc_error>
pub fn mem_read(&self, address: u64, buf: &mut [u8]) -> Result<(), uc_error>
Read a range of bytes from memory at the specified emulated physical address.
Sourcepub fn mem_read_as_vec(
&self,
address: u64,
size: usize,
) -> Result<Vec<u8>, uc_error>
pub fn mem_read_as_vec( &self, address: u64, size: usize, ) -> Result<Vec<u8>, uc_error>
Return a range of bytes from memory at the specified emulated physical address as vector.
Sourcepub fn vmem_read(
&self,
address: u64,
prot: Prot,
buf: &mut [u8],
) -> Result<(), uc_error>
pub fn vmem_read( &self, address: u64, prot: Prot, buf: &mut [u8], ) -> Result<(), uc_error>
Read a range of bytes from memory at the specified emulated virtual address.
Sourcepub fn vmem_read_as_vec(
&self,
address: u64,
prot: Prot,
size: usize,
) -> Result<Vec<u8>, uc_error>
pub fn vmem_read_as_vec( &self, address: u64, prot: Prot, size: usize, ) -> Result<Vec<u8>, uc_error>
Return a range of bytes from memory at the specified emulated virtual address as vector.
Sourcepub fn mem_write(&mut self, address: u64, bytes: &[u8]) -> Result<(), uc_error>
pub fn mem_write(&mut self, address: u64, bytes: &[u8]) -> Result<(), uc_error>
Write the data in bytes
to the emulated physical address address
Sourcepub fn vmem_translate(
&mut self,
address: u64,
prot: Prot,
) -> Result<u64, uc_error>
pub fn vmem_translate( &mut self, address: u64, prot: Prot, ) -> Result<u64, uc_error>
translate virtual to physical address
Sourcepub unsafe fn mem_map_ptr(
&mut self,
address: u64,
size: u64,
perms: Prot,
ptr: *mut c_void,
) -> Result<(), uc_error>
pub unsafe fn mem_map_ptr( &mut self, address: u64, size: u64, perms: Prot, ptr: *mut c_void, ) -> Result<(), uc_error>
Map an existing memory region in the emulator at the specified address.
§Safety
This function is marked unsafe because it is the responsibility of the caller to
ensure that size
matches the size of the passed buffer, an invalid size
value will
likely cause a crash in unicorn.
address
must be aligned to 4kb or this will return Error::ARG
.
size
must be a multiple of 4kb or this will return Error::ARG
.
ptr
is a pointer to the provided memory region that will be used by the emulator.
Sourcepub fn mem_map(
&mut self,
address: u64,
size: u64,
perms: Prot,
) -> Result<(), uc_error>
pub fn mem_map( &mut self, address: u64, size: u64, perms: Prot, ) -> Result<(), uc_error>
Map a memory region in the emulator at the specified address.
address
must be aligned to 4kb or this will return Error::ARG
.
size
must be a multiple of 4kb or this will return Error::ARG
.
Sourcepub fn mmio_map<R, W>(
&mut self,
address: u64,
size: u64,
read_callback: Option<R>,
write_callback: Option<W>,
) -> Result<(), uc_error>
pub fn mmio_map<R, W>( &mut self, address: u64, size: u64, read_callback: Option<R>, write_callback: Option<W>, ) -> Result<(), uc_error>
Map in am MMIO region backed by callbacks.
address
must be aligned to 4kb or this will return Error::ARG
.
size
must be a multiple of 4kb or this will return Error::ARG
.
Sourcepub fn mmio_map_ro<F>(
&mut self,
address: u64,
size: u64,
callback: F,
) -> Result<(), uc_error>
pub fn mmio_map_ro<F>( &mut self, address: u64, size: u64, callback: F, ) -> Result<(), uc_error>
Map in a read-only MMIO region backed by a callback.
address
must be aligned to 4kb or this will return Error::ARG
.
size
must be a multiple of 4kb or this will return Error::ARG
.
Sourcepub fn mmio_map_wo<F>(
&mut self,
address: u64,
size: u64,
callback: F,
) -> Result<(), uc_error>
pub fn mmio_map_wo<F>( &mut self, address: u64, size: u64, callback: F, ) -> Result<(), uc_error>
Map in a write-only MMIO region backed by a callback.
address
must be aligned to 4kb or this will return Error::ARG
.
size
must be a multiple of 4kb or this will return Error::ARG
.
Sourcepub fn mem_unmap(&mut self, address: u64, size: u64) -> Result<(), uc_error>
pub fn mem_unmap(&mut self, address: u64, size: u64) -> Result<(), uc_error>
Unmap a memory region.
address
must be aligned to 4kb or this will return Error::ARG
.
size
must be a multiple of 4kb or this will return Error::ARG
.
Sourcepub fn mem_protect(
&mut self,
address: u64,
size: u64,
perms: Prot,
) -> Result<(), uc_error>
pub fn mem_protect( &mut self, address: u64, size: u64, perms: Prot, ) -> Result<(), uc_error>
Set the memory permissions for an existing memory region.
address
must be aligned to 4kb or this will return Error::ARG
.
size
must be a multiple of 4kb or this will return Error::ARG
.
Sourcepub fn reg_write<T: Into<i32>>(
&mut self,
regid: T,
value: u64,
) -> Result<(), uc_error>
pub fn reg_write<T: Into<i32>>( &mut self, regid: T, value: u64, ) -> Result<(), uc_error>
Write an unsigned value from a register.
Sourcepub fn reg_write_batch<T>(
&self,
regids: &[T],
values: &[u64],
count: i32,
) -> Result<(), uc_error>
pub fn reg_write_batch<T>( &self, regids: &[T], values: &[u64], count: i32, ) -> Result<(), uc_error>
Write values into batch of registers
Sourcepub fn reg_write_long<T: Into<i32>>(
&self,
regid: T,
value: &[u8],
) -> Result<(), uc_error>
pub fn reg_write_long<T: Into<i32>>( &self, regid: T, value: &[u8], ) -> Result<(), uc_error>
Write variable sized values into registers.
The user has to make sure that the buffer length matches the register size. This adds support for registers >64 bit (GDTR/IDTR, XMM, YMM, ZMM (x86); Q, V (arm64)).
Sourcepub fn reg_read<T: Into<i32>>(&self, regid: T) -> Result<u64, uc_error>
pub fn reg_read<T: Into<i32>>(&self, regid: T) -> Result<u64, uc_error>
Read an unsigned value from a register.
Not to be used with registers larger than 64 bit.
Sourcepub fn reg_read_batch<T>(
&self,
regids: &mut [T],
count: i32,
) -> Result<Vec<u64>, uc_error>
pub fn reg_read_batch<T>( &self, regids: &mut [T], count: i32, ) -> Result<Vec<u64>, uc_error>
Read batch of registers
Not to be used with registers larger than 64 bit
Sourcepub fn reg_read_long<T: Into<i32>>(
&self,
regid: T,
) -> Result<Box<[u8]>, uc_error>
pub fn reg_read_long<T: Into<i32>>( &self, regid: T, ) -> Result<Box<[u8]>, uc_error>
Read 128, 256 or 512 bit register value into heap allocated byte array.
This adds safe support for registers >64 bit (GDTR/IDTR, XMM, YMM, ZMM, ST (x86); Q, V (arm64)).
Sourcepub fn reg_read_arm_coproc(
&self,
reg: &mut RegisterARMCP,
) -> Result<(), uc_error>
pub fn reg_read_arm_coproc( &self, reg: &mut RegisterARMCP, ) -> Result<(), uc_error>
Read ARM Coprocessor register
Sourcepub fn reg_write_arm_coproc(
&mut self,
reg: &RegisterARMCP,
) -> Result<(), uc_error>
pub fn reg_write_arm_coproc( &mut self, reg: &RegisterARMCP, ) -> Result<(), uc_error>
Write ARM Coprocessor register
Sourcepub fn reg_read_arm64_coproc(
&self,
reg: &mut RegisterARM64CP,
) -> Result<(), uc_error>
pub fn reg_read_arm64_coproc( &self, reg: &mut RegisterARM64CP, ) -> Result<(), uc_error>
Read ARM64 Coprocessor register
Sourcepub fn reg_write_arm64_coproc(
&mut self,
reg: &RegisterARM64CP,
) -> Result<(), uc_error>
pub fn reg_write_arm64_coproc( &mut self, reg: &RegisterARM64CP, ) -> Result<(), uc_error>
Write ARM64 Coprocessor register
Sourcepub fn reg_read_i32<T: Into<i32>>(&self, regid: T) -> Result<i32, uc_error>
pub fn reg_read_i32<T: Into<i32>>(&self, regid: T) -> Result<i32, uc_error>
Read a signed 32-bit value from a register.
Sourcepub fn add_code_hook<F>(
&mut self,
begin: u64,
end: u64,
callback: F,
) -> Result<UcHookId, uc_error>
pub fn add_code_hook<F>( &mut self, begin: u64, end: u64, callback: F, ) -> Result<UcHookId, uc_error>
Add a code hook.
Sourcepub fn add_block_hook<F>(
&mut self,
begin: u64,
end: u64,
callback: F,
) -> Result<UcHookId, uc_error>
pub fn add_block_hook<F>( &mut self, begin: u64, end: u64, callback: F, ) -> Result<UcHookId, uc_error>
Add a block hook.
Sourcepub fn add_mem_hook<F>(
&mut self,
hook_type: HookType,
begin: u64,
end: u64,
callback: F,
) -> Result<UcHookId, uc_error>
pub fn add_mem_hook<F>( &mut self, hook_type: HookType, begin: u64, end: u64, callback: F, ) -> Result<UcHookId, uc_error>
Add a memory hook.
Sourcepub fn add_intr_hook<F>(&mut self, callback: F) -> Result<UcHookId, uc_error>
pub fn add_intr_hook<F>(&mut self, callback: F) -> Result<UcHookId, uc_error>
Add an interrupt hook.
Sourcepub fn add_insn_invalid_hook<F>(
&mut self,
callback: F,
) -> Result<UcHookId, uc_error>
pub fn add_insn_invalid_hook<F>( &mut self, callback: F, ) -> Result<UcHookId, uc_error>
Add hook for invalid instructions
Sourcepub fn add_insn_in_hook<F>(&mut self, callback: F) -> Result<UcHookId, uc_error>
pub fn add_insn_in_hook<F>(&mut self, callback: F) -> Result<UcHookId, uc_error>
Add hook for x86 IN instruction.
Sourcepub fn add_insn_out_hook<F>(
&mut self,
callback: F,
) -> Result<UcHookId, uc_error>
pub fn add_insn_out_hook<F>( &mut self, callback: F, ) -> Result<UcHookId, uc_error>
Add hook for x86 OUT instruction.
Sourcepub fn add_insn_sys_hook<F>(
&mut self,
insn_type: X86Insn,
begin: u64,
end: u64,
callback: F,
) -> Result<UcHookId, uc_error>
pub fn add_insn_sys_hook<F>( &mut self, insn_type: X86Insn, begin: u64, end: u64, callback: F, ) -> Result<UcHookId, uc_error>
Add hook for x86 SYSCALL or SYSENTER.
Sourcepub fn add_insn_sys_hook_arm64<F>(
&mut self,
insn_type: Arm64Insn,
begin: u64,
end: u64,
callback: F,
) -> Result<UcHookId, uc_error>
pub fn add_insn_sys_hook_arm64<F>( &mut self, insn_type: Arm64Insn, begin: u64, end: u64, callback: F, ) -> Result<UcHookId, uc_error>
Add hook for ARM MRS/MSR/SYS/SYSL instructions.
If the callback returns true, the read/write to system registers would be skipped (even though that may cause exceptions!). Note one callback per instruction is allowed.
pub fn add_tlb_hook<F>( &mut self, begin: u64, end: u64, callback: F, ) -> Result<UcHookId, uc_error>
pub fn add_tcg_hook<F>( &mut self, code: TcgOpCode, flag: TcgOpFlag, begin: u64, end: u64, callback: F, ) -> Result<UcHookId, uc_error>
Sourcepub fn add_edge_gen_hook<F>(
&mut self,
begin: u64,
end: u64,
callback: F,
) -> Result<UcHookId, uc_error>
pub fn add_edge_gen_hook<F>( &mut self, begin: u64, end: u64, callback: F, ) -> Result<UcHookId, uc_error>
Add hook for edge generated event.
Callback parameters: (uc, cur_tb, prev_tb)
Sourcepub fn remove_hook(&mut self, hook_id: UcHookId) -> Result<(), uc_error>
pub fn remove_hook(&mut self, hook_id: UcHookId) -> Result<(), uc_error>
Remove a hook.
hook_id
is the value returned by add_*_hook
functions.
Sourcepub fn context_alloc(&self) -> Result<Context, uc_error>
pub fn context_alloc(&self) -> Result<Context, uc_error>
Allocate and return an empty Unicorn context.
To be populated via context_save
.
Sourcepub fn context_save(&self, context: &mut Context) -> Result<(), uc_error>
pub fn context_save(&self, context: &mut Context) -> Result<(), uc_error>
Save current Unicorn context to previously allocated Context struct.
Sourcepub fn context_init(&self) -> Result<Context, uc_error>
pub fn context_init(&self) -> Result<Context, uc_error>
Allocate and return a Context struct initialized with the current CPU context.
This can be used for fast rollbacks with context_restore
.
In case of many non-concurrent context saves, use context_alloc
and *_save
individually to avoid unnecessary allocations.
Sourcepub fn context_restore(&self, context: &Context) -> Result<(), uc_error>
pub fn context_restore(&self, context: &Context) -> Result<(), uc_error>
Restore a previously saved Unicorn context.
Perform a quick rollback of the CPU context, including registers and some internal metadata. Contexts may not be shared across engine instances with differing arches or modes. Memory has to be restored manually, if needed.
Sourcepub fn emu_start(
&mut self,
begin: u64,
until: u64,
timeout: u64,
count: usize,
) -> Result<(), uc_error>
pub fn emu_start( &mut self, begin: u64, until: u64, timeout: u64, count: usize, ) -> Result<(), uc_error>
Emulate machine code for a specified duration.
begin
is the address where to start the emulation. The emulation stops if until
is hit. timeout
specifies a duration in microseconds after which the emulation is
stopped (infinite execution if set to 0). count
is the maximum number of instructions
to emulate (emulate all the available instructions if set to 0).
Sourcepub fn emu_stop(&mut self) -> Result<(), uc_error>
pub fn emu_stop(&mut self) -> Result<(), uc_error>
Stop the emulation.
This is usually called from callback function in hooks. NOTE: For now, this will stop the execution only after the current block.
Sourcepub fn query(&self, query: Query) -> Result<usize, uc_error>
pub fn query(&self, query: Query) -> Result<usize, uc_error>
Query the internal status of the engine.
supported: MODE
, PAGE_SIZE
, ARCH
Sourcepub fn pc_read(&self) -> Result<u64, uc_error>
pub fn pc_read(&self) -> Result<u64, uc_error>
Gets the current program counter for this unicorn
instance.
Sourcepub fn set_pc(&mut self, value: u64) -> Result<(), uc_error>
pub fn set_pc(&mut self, value: u64) -> Result<(), uc_error>
Sets the program counter for this unicorn
instance.