unicorn/bindings/rust
Takacs, Philipp ed9164e47a rust only add mmio callback funktion, if callback is requested
The C function uc_mmio_map() allows to add seperate callback functions
and userdata for read and write. When the callback functions are NULL
unicorn don't try to call this functions.

Previous this patch, when i.e. read_callback was None the callback was set
to mmio_read_callback_proxy and the userdata was set to NULL. On a callback
the mmio_read_callback_proxy then tried to dereference the userdata and
caused a segfault.

fixes #1762
2023-01-23 13:22:55 +01:00
..
src rust only add mmio callback funktion, if callback is requested 2023-01-23 13:22:55 +01:00
build.rs Remove use_system_unicorn and build_with_cmake 2022-05-06 14:47:15 +02:00
COPYING bindings: add Rust 2021-10-04 01:01:43 +08:00
README.md Add TriCore constants to Rust bindings 2022-05-11 17:29:03 +08:00

Unicorn-engine

Rust bindings for the Unicorn emulator with utility functions.

Checkout Unicorn2 source code at dev branch.

use unicorn_engine::{Unicorn, RegisterARM};
use unicorn_engine::unicorn_const::{Arch, Mode, Permission, SECOND_SCALE};

fn main() {
    let arm_code32: Vec<u8> = vec![0x17, 0x00, 0x40, 0xe2]; // sub r0, #23

    let mut unicorn = Unicorn::new(Arch::ARM, Mode::LITTLE_ENDIAN).expect("failed to initialize Unicorn instance");
    let emu = &mut unicorn;
    emu.mem_map(0x1000, 0x4000, Permission::ALL).expect("failed to map code page");
    emu.mem_write(0x1000, &arm_code32).expect("failed to write instructions");

    emu.reg_write(RegisterARM::R0, 123).expect("failed write R0");
    emu.reg_write(RegisterARM::R5, 1337).expect("failed write R5");

    let _ = emu.emu_start(0x1000, (0x1000 + arm_code32.len()) as u64, 10 * SECOND_SCALE, 1000);
    assert_eq!(emu.reg_read(RegisterARM::R0), Ok(100));
    assert_eq!(emu.reg_read(RegisterARM::R5), Ok(1337));
}

Further sample code can be found in tests.

Usage

Add this to your Cargo.toml:

[dependencies]
unicorn-engine = "2.0.0"

Acknowledgements

These bindings are based on Sébastien Duquette's (@ekse) unicorn-rs. We picked up the project, as it is no longer maintained. Thanks to all contributors.