/unicorn-rs

Rust bindings for the unicorn CPU emulator

Primary LanguageRustGNU General Public License v2.0GPL-2.0

unicorn-rs

Rust bindings for the Unicorn emulator with utility functions.

An extended version for fuzzing with AFL++ support can be found in https://github.com/aflplusplus/unicornafl.

use unicorn::RegisterARM;
use unicorn::unicorn_const::{Arch, Mode, Protection, SECOND_SCALE};

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

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

    emu.reg_write(RegisterARM::R0 as i32, 123).expect("failed write R0");
    emu.reg_write(RegisterARM::R5 as i32, 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 as i32), Ok(100));
    assert_eq!(emu.reg_read(RegisterARM::R5 as i32), Ok(1337));
}

Further sample code can be found in tests/unicorn.rs.

In addition, the bindings offer some basic utility functionalities, such as a simple heap allocator utilizing Unicorn hooks for sanitization or easily accessible debug prints. These are WIP and only tested in ARM LITTLE_ENDIAN mode.

Installation

This project has been tested on Linux, OS X and Windows.

On Windows use vcpkg

Run cp ./vcpkg/* {PATH OF VCPKG}/ports/unicorn/ OR Manual copy overwrite

Run vcpkg.exe install unicorn:x64-windows-static-md

On Linux Or Macos

Refer to the unicorn documentation to compile.

For example, run UNICORN_STATIC=yes ./make.sh to generate a static library, and then run sudo ./make.sh install to install

To Use

To use unicorn-rs, simply add it as a dependency to the Cargo.toml of your program.

[dependencies]
unicorn = { git = "https://github.com/edsky/unicorn-rs.git", version="1.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 contributers.