Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust bindings improvements #1480

Merged
merged 12 commits into from
Nov 10, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bindings/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ include = [
"/Cargo.toml",
"/README.md",
"/src/*",
"build.rs"
"build.rs",
]
license = "GPL-2.0"
readme = "README.md"
Expand Down
5 changes: 3 additions & 2 deletions bindings/rust/build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use bytes::Buf;
use flate2::read::GzDecoder;
use reqwest::header::USER_AGENT;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::{env, process::Command};
use tar::Archive;

fn find_unicorn(unicorn_dir: &PathBuf) -> Option<PathBuf> {
fn find_unicorn(unicorn_dir: &Path) -> Option<PathBuf> {
for entry in std::fs::read_dir(unicorn_dir).ok()? {
let entry = entry.unwrap();
let path = entry.path();
Expand Down Expand Up @@ -49,6 +49,7 @@ fn download_unicorn() -> Option<String> {
}
}

#[allow(clippy::branches_sharing_code)]
fn main() {
let profile = env::var("PROFILE").unwrap();

Expand Down
6 changes: 6 additions & 0 deletions bindings/rust/src/arm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,9 @@ impl RegisterARM {
pub const FP: RegisterARM = RegisterARM::R11;
pub const IP: RegisterARM = RegisterARM::R12;
}

impl From<RegisterARM> for i32 {
fn from(r: RegisterARM) -> Self {
r as i32
}
}
10 changes: 7 additions & 3 deletions bindings/rust/src/arm64.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#![allow(non_camel_case_types)]
// For Unicorn Engine. AUTO-GENERATED FILE, DO NOT EDIT

// ARM64 registers
#[repr(C)]
#[derive(PartialEq, Debug, Clone, Copy)]
#[allow(non_camel_case_types)]
pub enum RegisterARM64 {
INVALID = 0,
X29 = 1,
Expand Down Expand Up @@ -319,3 +317,9 @@ impl RegisterARM64 {
pub const FP: RegisterARM64 = RegisterARM64::X29;
pub const LR: RegisterARM64 = RegisterARM64::X30;
}

impl From<RegisterARM64> for i32 {
fn from(r: RegisterARM64) -> Self {
r as i32
}
}
185 changes: 75 additions & 110 deletions bindings/rust/src/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#![allow(non_camel_case_types)]
#![allow(dead_code)]

use super::unicorn_const::*;
use crate::Unicorn;

use super::unicorn_const::{uc_error, Arch, HookType, MemRegion, MemType, Mode, Query};
use libc::{c_char, c_int};
use std::ffi::c_void;
use std::pin::Pin;
use std::{ffi::c_void, marker::PhantomData};

pub type uc_handle = *mut c_void;
pub type uc_hook = *mut c_void;
Expand Down Expand Up @@ -75,156 +76,120 @@ extern "C" {
pub fn uc_context_alloc(engine: uc_handle, context: *mut uc_context) -> uc_error;
pub fn uc_context_save(engine: uc_handle, context: uc_context) -> uc_error;
pub fn uc_context_restore(engine: uc_handle, context: uc_context) -> uc_error;
pub fn uc_set_data_ptr(engine: uc_handle, ptr: *mut c_void) -> uc_error;
pub fn uc_get_data_ptr(engine: uc_handle) -> *mut c_void;
}

pub struct CodeHook {
pub unicorn: *mut crate::UnicornInner,
pub callback: Box<dyn FnMut(crate::UnicornHandle, u64, u32)>,
pub struct UcHook<'a, D: 'a, F: 'a> {
pub callback: F,
pub phantom: PhantomData<&'a D>,
}

pub struct BlockHook {
pub unicorn: *mut crate::UnicornInner,
pub callback: Box<dyn FnMut(crate::UnicornHandle, u64, u32)>,
}
pub trait IsUcHook<'a> {}

pub struct MemHook {
pub unicorn: *mut crate::UnicornInner,
pub callback: Box<dyn FnMut(crate::UnicornHandle, MemType, u64, usize, i64)>,
}

pub struct InterruptHook {
pub unicorn: *mut crate::UnicornInner,
pub callback: Box<dyn FnMut(crate::UnicornHandle, u32)>,
}

pub struct InstructionInHook {
pub unicorn: *mut crate::UnicornInner,
pub callback: Box<dyn FnMut(crate::UnicornHandle, u32, usize)>,
}

pub struct InstructionOutHook {
pub unicorn: *mut crate::UnicornInner,
pub callback: Box<dyn FnMut(crate::UnicornHandle, u32, usize, u32)>,
}
impl<'a, D, F> IsUcHook<'a> for UcHook<'a, D, F> {}

pub struct InstructionSysHook {
pub unicorn: *mut crate::UnicornInner,
pub callback: Box<dyn FnMut(crate::UnicornHandle)>,
fn read_uc_from_uc_handle<'a, D>(uc: uc_handle) -> &'a mut crate::Unicorn<'a, D>
where
D: 'a,
{
unsafe {
(uc_get_data_ptr(uc) as *mut Unicorn<'a, D>)
.as_mut()
.unwrap()
}
}

pub extern "C" fn code_hook_proxy(
pub extern "C" fn code_hook_proxy<D, F>(
uc: uc_handle,
address: u64,
size: u32,
user_data: *mut CodeHook,
) {
let unicorn = unsafe { &mut *(*user_data).unicorn };
let callback = &mut unsafe { &mut *(*user_data).callback };
user_data: *mut UcHook<D, F>,
) where
F: FnMut(&mut crate::Unicorn<D>, u64, u32),
{
let unicorn = read_uc_from_uc_handle(uc);
let callback = unsafe { &mut (*user_data).callback };
assert_eq!(uc, unicorn.uc);
callback(
crate::UnicornHandle {
inner: unsafe { Pin::new_unchecked(unicorn) },
},
address,
size,
);
callback(unicorn, address, size);
}

pub extern "C" fn block_hook_proxy(
pub extern "C" fn block_hook_proxy<D, F>(
uc: uc_handle,
address: u64,
size: u32,
user_data: *mut BlockHook,
) {
let unicorn = unsafe { &mut *(*user_data).unicorn };
let callback = &mut unsafe { &mut *(*user_data).callback };
user_data: *mut UcHook<D, F>,
) where
F: FnMut(&mut crate::Unicorn<D>, u64, u32),
{
let unicorn = read_uc_from_uc_handle(uc);
let callback = unsafe { &mut (*user_data).callback };
assert_eq!(uc, unicorn.uc);
callback(
crate::UnicornHandle {
inner: unsafe { Pin::new_unchecked(unicorn) },
},
address,
size,
);
callback(unicorn, address, size);
}

pub extern "C" fn mem_hook_proxy(
pub extern "C" fn mem_hook_proxy<D, F>(
uc: uc_handle,
mem_type: MemType,
address: u64,
size: u32,
value: i64,
user_data: *mut MemHook,
) {
let unicorn = unsafe { &mut *(*user_data).unicorn };
let callback = &mut unsafe { &mut *(*user_data).callback };
user_data: *mut UcHook<D, F>,
) -> bool
where
F: FnMut(&mut crate::Unicorn<D>, MemType, u64, usize, i64) -> bool,
{
let unicorn = read_uc_from_uc_handle(uc);
let callback = unsafe { &mut (*user_data).callback };
assert_eq!(uc, unicorn.uc);
callback(
crate::UnicornHandle {
inner: unsafe { Pin::new_unchecked(unicorn) },
},
mem_type,
address,
size as usize,
value,
);
callback(unicorn, mem_type, address, size as usize, value)
}

pub extern "C" fn intr_hook_proxy(uc: uc_handle, value: u32, user_data: *mut InterruptHook) {
let unicorn = unsafe { &mut *(*user_data).unicorn };
let callback = &mut unsafe { &mut *(*user_data).callback };
pub extern "C" fn intr_hook_proxy<D, F>(uc: uc_handle, value: u32, user_data: *mut UcHook<D, F>)
where
F: FnMut(&mut crate::Unicorn<D>, u32),
{
let unicorn = read_uc_from_uc_handle(uc);
let callback = unsafe { &mut (*user_data).callback };
assert_eq!(uc, unicorn.uc);
callback(
crate::UnicornHandle {
inner: unsafe { Pin::new_unchecked(unicorn) },
},
value,
);
callback(unicorn, value);
}

pub extern "C" fn insn_in_hook_proxy(
pub extern "C" fn insn_in_hook_proxy<D, F>(
uc: uc_handle,
port: u32,
size: usize,
user_data: *mut InstructionInHook,
) {
let unicorn = unsafe { &mut *(*user_data).unicorn };
let callback = &mut unsafe { &mut *(*user_data).callback };
user_data: *mut UcHook<D, F>,
) where
F: FnMut(&mut crate::Unicorn<D>, u32, usize),
{
let unicorn = read_uc_from_uc_handle(uc);
let callback = unsafe { &mut (*user_data).callback };
assert_eq!(uc, unicorn.uc);
callback(
crate::UnicornHandle {
inner: unsafe { Pin::new_unchecked(unicorn) },
},
port,
size,
);
callback(unicorn, port, size);
}

pub extern "C" fn insn_out_hook_proxy(
pub extern "C" fn insn_out_hook_proxy<D, F>(
uc: uc_handle,
port: u32,
size: usize,
value: u32,
user_data: *mut InstructionOutHook,
) {
let unicorn = unsafe { &mut *(*user_data).unicorn };
let callback = &mut unsafe { &mut *(*user_data).callback };
user_data: *mut UcHook<D, F>,
) where
F: FnMut(&mut crate::Unicorn<D>, u32, usize, u32),
{
let unicorn = read_uc_from_uc_handle(uc);
let callback = unsafe { &mut (*user_data).callback };
assert_eq!(uc, unicorn.uc);
callback(
crate::UnicornHandle {
inner: unsafe { Pin::new_unchecked(unicorn) },
},
port,
size,
value,
);
callback(unicorn, port, size, value);
}

pub extern "C" fn insn_sys_hook_proxy(uc: uc_handle, user_data: *mut InstructionSysHook) {
let unicorn = unsafe { &mut *(*user_data).unicorn };
let callback = &mut unsafe { &mut *(*user_data).callback };
pub extern "C" fn insn_sys_hook_proxy<D, F>(uc: uc_handle, user_data: *mut UcHook<D, F>)
where
F: FnMut(&mut crate::Unicorn<D>),
{
let unicorn = read_uc_from_uc_handle(uc);
assert_eq!(uc, unicorn.uc);
callback(crate::UnicornHandle {
inner: unsafe { Pin::new_unchecked(unicorn) },
});
let callback = unsafe { &mut (*user_data).callback };
callback(unicorn);
}
Loading