2021-10-17 04:26:40 +03:00
|
|
|
# Unicorn-engine
|
2021-10-03 20:01:43 +03:00
|
|
|
|
|
|
|
Rust bindings for the [Unicorn](http://www.unicorn-engine.org/) emulator with utility functions.
|
|
|
|
|
2021-10-17 04:26:40 +03:00
|
|
|
Checkout Unicorn2 source code at [dev branch](https://github.com/unicorn-engine/unicorn/tree/dev).
|
2021-10-03 20:01:43 +03:00
|
|
|
|
|
|
|
```rust
|
2021-11-09 18:04:59 +03:00
|
|
|
use unicorn_engine::{Unicorn, RegisterARM};
|
2021-10-17 04:26:40 +03:00
|
|
|
use unicorn_engine::unicorn_const::{Arch, Mode, Permission, SECOND_SCALE};
|
2021-10-03 20:01:43 +03:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let arm_code32: Vec<u8> = vec![0x17, 0x00, 0x40, 0xe2]; // sub r0, #23
|
|
|
|
|
2021-11-09 18:04:59 +03:00
|
|
|
let mut unicorn = Unicorn::new(Arch::ARM, Mode::LITTLE_ENDIAN).expect("failed to initialize Unicorn instance");
|
2022-03-15 09:50:39 +03:00
|
|
|
let emu = &mut unicorn;
|
2021-10-03 20:01:43 +03:00
|
|
|
emu.mem_map(0x1000, 0x4000, Permission::ALL).expect("failed to map code page");
|
|
|
|
emu.mem_write(0x1000, &arm_code32).expect("failed to write instructions");
|
|
|
|
|
2021-11-09 18:04:59 +03:00
|
|
|
emu.reg_write(RegisterARM::R0, 123).expect("failed write R0");
|
|
|
|
emu.reg_write(RegisterARM::R5, 1337).expect("failed write R5");
|
2021-10-03 20:01:43 +03:00
|
|
|
|
|
|
|
let _ = emu.emu_start(0x1000, (0x1000 + arm_code32.len()) as u64, 10 * SECOND_SCALE, 1000);
|
2022-03-15 09:50:39 +03:00
|
|
|
assert_eq!(emu.reg_read(RegisterARM::R0), Ok(100));
|
|
|
|
assert_eq!(emu.reg_read(RegisterARM::R5), Ok(1337));
|
2021-10-03 20:01:43 +03:00
|
|
|
}
|
|
|
|
```
|
2022-05-10 14:24:59 +03:00
|
|
|
Further sample code can be found in [tests](../../tests/rust-tests/main.rs).
|
2021-10-03 20:01:43 +03:00
|
|
|
|
2021-10-17 04:26:40 +03:00
|
|
|
## Usage
|
2021-10-03 20:01:43 +03:00
|
|
|
|
2021-10-17 04:26:40 +03:00
|
|
|
Add this to your `Cargo.toml`:
|
2021-10-03 20:01:43 +03:00
|
|
|
|
|
|
|
```
|
|
|
|
[dependencies]
|
2022-05-04 16:31:53 +03:00
|
|
|
unicorn-engine = "2.0.0"
|
2021-10-03 20:01:43 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
## Acknowledgements
|
|
|
|
|
|
|
|
These bindings are based on Sébastien Duquette's (@ekse) [unicorn-rs](https://github.com/unicorn-rs/unicorn-rs).
|
|
|
|
We picked up the project, as it is no longer maintained.
|
2021-10-17 04:26:40 +03:00
|
|
|
Thanks to all contributors.
|
2021-10-03 20:01:43 +03:00
|
|
|
|