aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriximeow <me@iximeow.net>2020-01-12 15:03:09 -0800
committeriximeow <me@iximeow.net>2020-01-12 16:10:14 -0800
commit825eabe868163189d371901ecfe27b4a3d0c9e97 (patch)
treef6240281710fe5971a7b28de3f3277402bd6eb0f
parentb7b781db7a2ba94c816ff5138a713114c39d4034 (diff)
add *extremely* poor ffi bindings for x86 decoders
this is specifically to support a disas-bench integration, for now
-rw-r--r--ffi/.gitignore2
-rw-r--r--ffi/Cargo.toml19
-rw-r--r--ffi/src/lib.rs43
3 files changed, 64 insertions, 0 deletions
diff --git a/ffi/.gitignore b/ffi/.gitignore
new file mode 100644
index 0000000..2c96eb1
--- /dev/null
+++ b/ffi/.gitignore
@@ -0,0 +1,2 @@
+target/
+Cargo.lock
diff --git a/ffi/Cargo.toml b/ffi/Cargo.toml
new file mode 100644
index 0000000..ff1a4c0
--- /dev/null
+++ b/ffi/Cargo.toml
@@ -0,0 +1,19 @@
+[package]
+name = "yaxpeax_x86_ffi"
+version = "0.0.1"
+authors = ["iximeow <me@iximeow.net>"]
+edition = "2018"
+
+[dependencies]
+yaxpeax-x86 = { path = "../" }
+yaxpeax-arch = { path = "../../../yaxpeax-arch" }
+
+[lib]
+name = "yaxpeax_x86_ffi"
+path = "src/lib.rs"
+crate-type = ["staticlib"]
+
+[profile.release]
+opt-level = 3
+lto = "fat"
+
diff --git a/ffi/src/lib.rs b/ffi/src/lib.rs
new file mode 100644
index 0000000..12e077b
--- /dev/null
+++ b/ffi/src/lib.rs
@@ -0,0 +1,43 @@
+use yaxpeax_arch::{Arch, Decoder, LengthedInstruction, Address};
+use yaxpeax_x86::x86_64;
+
+use std::fmt::Write;
+
+#[no_mangle]
+pub unsafe extern "C" fn yaxpeax_decode_x86_64_optimistic(data: *const std::os::raw::c_char, length: std::os::raw::c_longlong, inst: *mut yaxpeax_x86::Instruction) -> bool {
+ let inst: &mut yaxpeax_x86::Instruction = std::mem::transmute(inst);
+ <x86_64 as Arch>::Decoder::default().decode_into(inst, std::slice::from_raw_parts(data as *const u8, length as usize).iter().cloned()).is_err()
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn yaxpeax_instr_length_x86_64(inst: *mut yaxpeax_x86::Instruction) -> usize {
+ let inst: &mut yaxpeax_x86::Instruction = std::mem::transmute(inst);
+ inst.len().to_linear()
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn yaxpeax_instr_fmt(inst: *mut yaxpeax_x86::Instruction, size: *mut u64, capacity: *mut u64) -> *mut u8 {
+ let inst: &mut yaxpeax_x86::Instruction = std::mem::transmute(inst);
+ let mut res = String::with_capacity(128);
+
+ write!(res, "{}", inst).unwrap();
+
+ let mut bytes = res.into_bytes();
+ let ptr = bytes.as_mut_ptr();
+ *capacity = bytes.capacity() as u64;
+ *size = bytes.len() as u64;
+ std::mem::forget(bytes);
+ ptr
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn yaxpeax_instr_x86_64_blank() -> *mut yaxpeax_x86::Instruction {
+ Box::leak(Box::new(yaxpeax_x86::Instruction::invalid()))
+}
+
+#[no_mangle]
+pub unsafe extern "C" fn yaxpeax_str_drop(data: *mut u8, size: u64, capacity: u64) {
+ std::mem::drop(
+ Vec::from_raw_parts(data, size as usize, capacity as usize)
+ );
+}