summaryrefslogtreecommitdiff
path: root/rust/examples
diff options
context:
space:
mode:
authorRubens Brandao <git@rubens.io>2023-11-18 15:58:33 -0300
committerKyle Martin <krm504@nyu.edu>2023-11-21 15:18:50 -0500
commit8c9cdd38c3302280087c9e6d94f7f57083885edd (patch)
tree8bccf380b4470e0de8ac11c23b164e0acf6ffbb0 /rust/examples
parentb040fcfce48db861600eeb122cd0e2ff802fac96 (diff)
add mlil to rust
Diffstat (limited to 'rust/examples')
-rw-r--r--rust/examples/mlil_lifter/Cargo.lock355
-rw-r--r--rust/examples/mlil_lifter/Cargo.toml16
-rw-r--r--rust/examples/mlil_lifter/build.rs68
-rw-r--r--rust/examples/mlil_lifter/src/main.rs52
-rw-r--r--rust/examples/mlil_visitor/Cargo.lock355
-rw-r--r--rust/examples/mlil_visitor/Cargo.toml16
-rw-r--r--rust/examples/mlil_visitor/build.rs68
-rw-r--r--rust/examples/mlil_visitor/src/main.rs267
8 files changed, 1197 insertions, 0 deletions
diff --git a/rust/examples/mlil_lifter/Cargo.lock b/rust/examples/mlil_lifter/Cargo.lock
new file mode 100644
index 00000000..57f01d55
--- /dev/null
+++ b/rust/examples/mlil_lifter/Cargo.lock
@@ -0,0 +1,355 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "aho-corasick"
+version = "0.7.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
+dependencies = [
+ "memchr",
+]
+
+[[package]]
+name = "ansi_term"
+version = "0.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
+name = "atty"
+version = "0.2.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
+dependencies = [
+ "hermit-abi",
+ "libc",
+ "winapi",
+]
+
+[[package]]
+name = "binaryninja"
+version = "0.1.0"
+source = "git+https://github.com/Vector35/binaryninja-api.git?branch=dev#83d7a7800ac4a970d618ad125cdbb18e3b29b53e"
+dependencies = [
+ "binaryninjacore-sys",
+ "libc",
+ "log",
+]
+
+[[package]]
+name = "binaryninjacore-sys"
+version = "0.1.0"
+source = "git+https://github.com/Vector35/binaryninja-api.git?branch=dev#83d7a7800ac4a970d618ad125cdbb18e3b29b53e"
+dependencies = [
+ "bindgen",
+]
+
+[[package]]
+name = "bindgen"
+version = "0.58.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0f8523b410d7187a43085e7e064416ea32ded16bd0a4e6fc025e21616d01258f"
+dependencies = [
+ "bitflags",
+ "cexpr",
+ "clang-sys",
+ "clap",
+ "env_logger",
+ "lazy_static",
+ "lazycell",
+ "log",
+ "peeking_take_while",
+ "proc-macro2",
+ "quote",
+ "regex",
+ "rustc-hash",
+ "shlex",
+ "which",
+]
+
+[[package]]
+name = "bitflags"
+version = "1.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
+
+[[package]]
+name = "cexpr"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f4aedb84272dbe89af497cf81375129abda4fc0a9e7c5d317498c15cc30c0d27"
+dependencies = [
+ "nom",
+]
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "clang-sys"
+version = "1.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fa66045b9cb23c2e9c1520732030608b02ee07e5cfaa5a521ec15ded7fa24c90"
+dependencies = [
+ "glob",
+ "libc",
+ "libloading",
+]
+
+[[package]]
+name = "clap"
+version = "2.34.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
+dependencies = [
+ "ansi_term",
+ "atty",
+ "bitflags",
+ "strsim",
+ "textwrap",
+ "unicode-width",
+ "vec_map",
+]
+
+[[package]]
+name = "env_logger"
+version = "0.8.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3"
+dependencies = [
+ "atty",
+ "humantime",
+ "log",
+ "regex",
+ "termcolor",
+]
+
+[[package]]
+name = "glob"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
+
+[[package]]
+name = "hermit-abi"
+version = "0.1.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "humantime"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
+
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
+name = "lazycell"
+version = "1.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
+
+[[package]]
+name = "libc"
+version = "0.2.117"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c"
+
+[[package]]
+name = "libloading"
+version = "0.7.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "efbc0f03f9a775e9f6aed295c6a1ba2253c5757a9e03d55c6caa46a681abcddd"
+dependencies = [
+ "cfg-if",
+ "winapi",
+]
+
+[[package]]
+name = "log"
+version = "0.4.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
+name = "memchr"
+version = "2.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
+
+[[package]]
+name = "nom"
+version = "5.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af"
+dependencies = [
+ "memchr",
+ "version_check",
+]
+
+[[package]]
+name = "peeking_take_while"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.36"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
+dependencies = [
+ "unicode-xid",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "regex"
+version = "1.5.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.6.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
+
+[[package]]
+name = "rustc-hash"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
+
+[[package]]
+name = "shlex"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
+
+[[package]]
+name = "strsim"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
+
+[[package]]
+name = "template"
+version = "0.1.0"
+dependencies = [
+ "binaryninja",
+]
+
+[[package]]
+name = "termcolor"
+version = "1.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
+dependencies = [
+ "winapi-util",
+]
+
+[[package]]
+name = "textwrap"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
+dependencies = [
+ "unicode-width",
+]
+
+[[package]]
+name = "unicode-width"
+version = "0.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
+
+[[package]]
+name = "unicode-xid"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
+
+[[package]]
+name = "vec_map"
+version = "0.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
+
+[[package]]
+name = "version_check"
+version = "0.9.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
+
+[[package]]
+name = "which"
+version = "3.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "winapi"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-util"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
diff --git a/rust/examples/mlil_lifter/Cargo.toml b/rust/examples/mlil_lifter/Cargo.toml
new file mode 100644
index 00000000..4ea158fc
--- /dev/null
+++ b/rust/examples/mlil_lifter/Cargo.toml
@@ -0,0 +1,16 @@
+[package]
+name = "mlil_lifter"
+version = "0.1.0"
+edition = "2021"
+
+# Uncomment this if you're writing a plugin (plugins are shared objects loaded by the core):
+# [lib]
+# crate-type = ["cdylib"]
+
+# You can point at the BinaryNinja dependency in one of two ways, via path:
+[dependencies]
+binaryninja = {path="../../"}
+
+# Or directly at the git repo:
+# [dependencies]
+# binaryninja = {git = "https://github.com/Vector35/binaryninja-api.git", branch = "dev"}
diff --git a/rust/examples/mlil_lifter/build.rs b/rust/examples/mlil_lifter/build.rs
new file mode 100644
index 00000000..5ba9bcde
--- /dev/null
+++ b/rust/examples/mlil_lifter/build.rs
@@ -0,0 +1,68 @@
+use std::env;
+use std::fs::File;
+use std::io::BufReader;
+use std::path::PathBuf;
+
+#[cfg(target_os = "macos")]
+static LASTRUN_PATH: (&str, &str) = ("HOME", "Library/Application Support/Binary Ninja/lastrun");
+
+#[cfg(target_os = "linux")]
+static LASTRUN_PATH: (&str, &str) = ("HOME", ".binaryninja/lastrun");
+
+#[cfg(windows)]
+static LASTRUN_PATH: (&str, &str) = ("APPDATA", "Binary Ninja\\lastrun");
+
+// Check last run location for path to BinaryNinja; Otherwise check the default install locations
+fn link_path() -> PathBuf {
+ use std::io::prelude::*;
+
+ let home = PathBuf::from(env::var(LASTRUN_PATH.0).unwrap());
+ let lastrun = PathBuf::from(&home).join(LASTRUN_PATH.1);
+
+ File::open(lastrun)
+ .and_then(|f| {
+ let mut binja_path = String::new();
+ let mut reader = BufReader::new(f);
+
+ reader.read_line(&mut binja_path)?;
+ Ok(PathBuf::from(binja_path.trim()))
+ })
+ .unwrap_or_else(|_| {
+ #[cfg(target_os = "macos")]
+ return PathBuf::from("/Applications/Binary Ninja.app/Contents/MacOS");
+
+ #[cfg(target_os = "linux")]
+ return home.join("binaryninja");
+
+ #[cfg(windows)]
+ return PathBuf::from(env::var("PROGRAMFILES").unwrap())
+ .join("Vector35\\BinaryNinja\\");
+ })
+}
+
+fn main() {
+ // Use BINARYNINJADIR first for custom BN builds/configurations (BN devs/build server), fallback on defaults
+ let install_path = env::var("BINARYNINJADIR")
+ .map(PathBuf::from)
+ .unwrap_or_else(|_| link_path());
+
+ #[cfg(target_os = "linux")]
+ println!(
+ "cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
+ install_path.to_str().unwrap(),
+ install_path.to_str().unwrap(),
+ );
+
+ #[cfg(target_os = "macos")]
+ println!(
+ "cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
+ install_path.to_str().unwrap(),
+ install_path.to_str().unwrap(),
+ );
+
+ #[cfg(target_os = "windows")]
+ {
+ println!("cargo:rustc-link-lib=binaryninjacore");
+ println!("cargo:rustc-link-search={}", install_path.to_str().unwrap());
+ }
+}
diff --git a/rust/examples/mlil_lifter/src/main.rs b/rust/examples/mlil_lifter/src/main.rs
new file mode 100644
index 00000000..3ba25630
--- /dev/null
+++ b/rust/examples/mlil_lifter/src/main.rs
@@ -0,0 +1,52 @@
+use std::env;
+
+use binaryninja::binaryview::BinaryViewExt;
+
+// Standalone executables need to provide a main function for rustc
+// Plugins should refer to `binaryninja::command::*` for the various registration callbacks.
+fn main() {
+ let mut args = env::args();
+ let _ = args.next().unwrap();
+ let Some(filename) = args.next() else {
+ panic!("Expected input filename\n");
+ };
+
+ // This loads all the core architecture, platform, etc plugins
+ // Standalone executables probably need to call this, but plugins do not
+ println!("Loading plugins...");
+ binaryninja::headless::init();
+
+ // Your code here...
+ println!("Loading binary...");
+ let bv = binaryninja::load(filename).expect("Couldn't open binary file");
+
+ // Go through all functions in the binary
+ for func in bv.functions().iter() {
+ let sym = func.symbol();
+ println!("Function {}:", sym.full_name());
+
+ let Ok(il) = func.medium_level_il() else {
+ println!(" Does not have MLIL\n");
+ continue;
+ };
+ // Get the SSA form for this function
+ let il = il.ssa_form();
+
+ // Loop through all blocks in the function
+ for block in il.basic_blocks().iter() {
+ // Loop though each instruction in the block
+ for instr in block.iter() {
+ // Uplift the instruction into a native rust format
+ let lifted = instr.lift();
+ let address = instr.address();
+
+ // print the lifted instruction
+ println!("{address:08x}: {lifted:x?}");
+ }
+ }
+ println!();
+ }
+
+ // Important! Standalone executables need to call shutdown or they will hang forever
+ binaryninja::headless::shutdown();
+}
diff --git a/rust/examples/mlil_visitor/Cargo.lock b/rust/examples/mlil_visitor/Cargo.lock
new file mode 100644
index 00000000..57f01d55
--- /dev/null
+++ b/rust/examples/mlil_visitor/Cargo.lock
@@ -0,0 +1,355 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "aho-corasick"
+version = "0.7.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
+dependencies = [
+ "memchr",
+]
+
+[[package]]
+name = "ansi_term"
+version = "0.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
+name = "atty"
+version = "0.2.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
+dependencies = [
+ "hermit-abi",
+ "libc",
+ "winapi",
+]
+
+[[package]]
+name = "binaryninja"
+version = "0.1.0"
+source = "git+https://github.com/Vector35/binaryninja-api.git?branch=dev#83d7a7800ac4a970d618ad125cdbb18e3b29b53e"
+dependencies = [
+ "binaryninjacore-sys",
+ "libc",
+ "log",
+]
+
+[[package]]
+name = "binaryninjacore-sys"
+version = "0.1.0"
+source = "git+https://github.com/Vector35/binaryninja-api.git?branch=dev#83d7a7800ac4a970d618ad125cdbb18e3b29b53e"
+dependencies = [
+ "bindgen",
+]
+
+[[package]]
+name = "bindgen"
+version = "0.58.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0f8523b410d7187a43085e7e064416ea32ded16bd0a4e6fc025e21616d01258f"
+dependencies = [
+ "bitflags",
+ "cexpr",
+ "clang-sys",
+ "clap",
+ "env_logger",
+ "lazy_static",
+ "lazycell",
+ "log",
+ "peeking_take_while",
+ "proc-macro2",
+ "quote",
+ "regex",
+ "rustc-hash",
+ "shlex",
+ "which",
+]
+
+[[package]]
+name = "bitflags"
+version = "1.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
+
+[[package]]
+name = "cexpr"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f4aedb84272dbe89af497cf81375129abda4fc0a9e7c5d317498c15cc30c0d27"
+dependencies = [
+ "nom",
+]
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "clang-sys"
+version = "1.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fa66045b9cb23c2e9c1520732030608b02ee07e5cfaa5a521ec15ded7fa24c90"
+dependencies = [
+ "glob",
+ "libc",
+ "libloading",
+]
+
+[[package]]
+name = "clap"
+version = "2.34.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
+dependencies = [
+ "ansi_term",
+ "atty",
+ "bitflags",
+ "strsim",
+ "textwrap",
+ "unicode-width",
+ "vec_map",
+]
+
+[[package]]
+name = "env_logger"
+version = "0.8.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3"
+dependencies = [
+ "atty",
+ "humantime",
+ "log",
+ "regex",
+ "termcolor",
+]
+
+[[package]]
+name = "glob"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
+
+[[package]]
+name = "hermit-abi"
+version = "0.1.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "humantime"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
+
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
+name = "lazycell"
+version = "1.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
+
+[[package]]
+name = "libc"
+version = "0.2.117"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c"
+
+[[package]]
+name = "libloading"
+version = "0.7.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "efbc0f03f9a775e9f6aed295c6a1ba2253c5757a9e03d55c6caa46a681abcddd"
+dependencies = [
+ "cfg-if",
+ "winapi",
+]
+
+[[package]]
+name = "log"
+version = "0.4.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
+name = "memchr"
+version = "2.4.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
+
+[[package]]
+name = "nom"
+version = "5.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af"
+dependencies = [
+ "memchr",
+ "version_check",
+]
+
+[[package]]
+name = "peeking_take_while"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.36"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
+dependencies = [
+ "unicode-xid",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145"
+dependencies = [
+ "proc-macro2",
+]
+
+[[package]]
+name = "regex"
+version = "1.5.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.6.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
+
+[[package]]
+name = "rustc-hash"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
+
+[[package]]
+name = "shlex"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3"
+
+[[package]]
+name = "strsim"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
+
+[[package]]
+name = "template"
+version = "0.1.0"
+dependencies = [
+ "binaryninja",
+]
+
+[[package]]
+name = "termcolor"
+version = "1.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
+dependencies = [
+ "winapi-util",
+]
+
+[[package]]
+name = "textwrap"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
+dependencies = [
+ "unicode-width",
+]
+
+[[package]]
+name = "unicode-width"
+version = "0.1.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
+
+[[package]]
+name = "unicode-xid"
+version = "0.2.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
+
+[[package]]
+name = "vec_map"
+version = "0.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
+
+[[package]]
+name = "version_check"
+version = "0.9.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
+
+[[package]]
+name = "which"
+version = "3.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "winapi"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-util"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
diff --git a/rust/examples/mlil_visitor/Cargo.toml b/rust/examples/mlil_visitor/Cargo.toml
new file mode 100644
index 00000000..76ca1222
--- /dev/null
+++ b/rust/examples/mlil_visitor/Cargo.toml
@@ -0,0 +1,16 @@
+[package]
+name = "mlil_visitor"
+version = "0.1.0"
+edition = "2021"
+
+# Uncomment this if you're writing a plugin (plugins are shared objects loaded by the core):
+# [lib]
+# crate-type = ["cdylib"]
+
+# You can point at the BinaryNinja dependency in one of two ways, via path:
+[dependencies]
+binaryninja = {path="../../"}
+
+# Or directly at the git repo:
+# [dependencies]
+# binaryninja = {git = "https://github.com/Vector35/binaryninja-api.git", branch = "dev"}
diff --git a/rust/examples/mlil_visitor/build.rs b/rust/examples/mlil_visitor/build.rs
new file mode 100644
index 00000000..5ba9bcde
--- /dev/null
+++ b/rust/examples/mlil_visitor/build.rs
@@ -0,0 +1,68 @@
+use std::env;
+use std::fs::File;
+use std::io::BufReader;
+use std::path::PathBuf;
+
+#[cfg(target_os = "macos")]
+static LASTRUN_PATH: (&str, &str) = ("HOME", "Library/Application Support/Binary Ninja/lastrun");
+
+#[cfg(target_os = "linux")]
+static LASTRUN_PATH: (&str, &str) = ("HOME", ".binaryninja/lastrun");
+
+#[cfg(windows)]
+static LASTRUN_PATH: (&str, &str) = ("APPDATA", "Binary Ninja\\lastrun");
+
+// Check last run location for path to BinaryNinja; Otherwise check the default install locations
+fn link_path() -> PathBuf {
+ use std::io::prelude::*;
+
+ let home = PathBuf::from(env::var(LASTRUN_PATH.0).unwrap());
+ let lastrun = PathBuf::from(&home).join(LASTRUN_PATH.1);
+
+ File::open(lastrun)
+ .and_then(|f| {
+ let mut binja_path = String::new();
+ let mut reader = BufReader::new(f);
+
+ reader.read_line(&mut binja_path)?;
+ Ok(PathBuf::from(binja_path.trim()))
+ })
+ .unwrap_or_else(|_| {
+ #[cfg(target_os = "macos")]
+ return PathBuf::from("/Applications/Binary Ninja.app/Contents/MacOS");
+
+ #[cfg(target_os = "linux")]
+ return home.join("binaryninja");
+
+ #[cfg(windows)]
+ return PathBuf::from(env::var("PROGRAMFILES").unwrap())
+ .join("Vector35\\BinaryNinja\\");
+ })
+}
+
+fn main() {
+ // Use BINARYNINJADIR first for custom BN builds/configurations (BN devs/build server), fallback on defaults
+ let install_path = env::var("BINARYNINJADIR")
+ .map(PathBuf::from)
+ .unwrap_or_else(|_| link_path());
+
+ #[cfg(target_os = "linux")]
+ println!(
+ "cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-l:libbinaryninjacore.so.1",
+ install_path.to_str().unwrap(),
+ install_path.to_str().unwrap(),
+ );
+
+ #[cfg(target_os = "macos")]
+ println!(
+ "cargo:rustc-link-arg=-Wl,-rpath,{},-L{},-lbinaryninjacore",
+ install_path.to_str().unwrap(),
+ install_path.to_str().unwrap(),
+ );
+
+ #[cfg(target_os = "windows")]
+ {
+ println!("cargo:rustc-link-lib=binaryninjacore");
+ println!("cargo:rustc-link-search={}", install_path.to_str().unwrap());
+ }
+}
diff --git a/rust/examples/mlil_visitor/src/main.rs b/rust/examples/mlil_visitor/src/main.rs
new file mode 100644
index 00000000..3f75708c
--- /dev/null
+++ b/rust/examples/mlil_visitor/src/main.rs
@@ -0,0 +1,267 @@
+use std::env;
+
+use binaryninja::binaryview::BinaryViewExt;
+use binaryninja::mlil::operation::MediumLevelILOperand;
+use binaryninja::mlil::{MediumLevelILFunction, MediumLevelILInstruction, MediumLevelILOperation};
+use binaryninja::types::Variable;
+
+fn print_indent(indent: usize) {
+ print!("{:<indent$}", "")
+}
+
+fn print_operation(operation: &MediumLevelILOperation) {
+ use MediumLevelILOperation::*;
+ match operation {
+ Nop(_) => print!("Nop"),
+ Noret(_) => print!("Noret"),
+ Bp(_) => print!("Bp"),
+ Undef(_) => print!("Undef"),
+ Unimpl(_) => print!("Unimpl"),
+ If(_) => print!("If"),
+ FloatConst(_) => print!("FloatConst"),
+ Const(_) => print!("Const"),
+ ConstPtr(_) => print!("ConstPtr"),
+ Import(_) => print!("Import"),
+ ExternPtr(_) => print!("ExternPtr"),
+ ConstData(_) => print!("ConstData"),
+ Jump(_) => print!("Jump"),
+ RetHint(_) => print!("RetHint"),
+ StoreSsa(_) => print!("StoreSsa"),
+ StoreStructSsa(_) => print!("StoreStructSsa"),
+ StoreStruct(_) => print!("StoreStruct"),
+ Store(_) => print!("Store"),
+ JumpTo(_) => print!("JumpTo"),
+ Goto(_) => print!("Goto"),
+ FreeVarSlot(_) => print!("FreeVarSlot"),
+ SetVarField(_) => print!("SetVarField"),
+ SetVar(_) => print!("SetVar"),
+ FreeVarSlotSsa(_) => print!("FreeVarSlotSsa"),
+ SetVarSsaField(_) => print!("SetVarSsaField"),
+ SetVarAliasedField(_) => print!("SetVarAliasedField"),
+ SetVarAliased(_) => print!("SetVarAliased"),
+ SetVarSsa(_) => print!("SetVarSsa"),
+ VarPhi(_) => print!("VarPhi"),
+ MemPhi(_) => print!("MemPhi"),
+ VarSplit(_) => print!("VarSplit"),
+ SetVarSplit(_) => print!("SetVarSplit"),
+ VarSplitSsa(_) => print!("VarSplitSsa"),
+ SetVarSplitSsa(_) => print!("SetVarSplitSsa"),
+ Add(_) => print!("Add"),
+ Sub(_) => print!("Sub"),
+ And(_) => print!("And"),
+ Or(_) => print!("Or"),
+ Xor(_) => print!("Xor"),
+ Lsl(_) => print!("Lsl"),
+ Lsr(_) => print!("Lsr"),
+ Asr(_) => print!("Asr"),
+ Rol(_) => print!("Rol"),
+ Ror(_) => print!("Ror"),
+ Mul(_) => print!("Mul"),
+ MuluDp(_) => print!("MuluDp"),
+ MulsDp(_) => print!("MulsDp"),
+ Divu(_) => print!("Divu"),
+ DivuDp(_) => print!("DivuDp"),
+ Divs(_) => print!("Divs"),
+ DivsDp(_) => print!("DivsDp"),
+ Modu(_) => print!("Modu"),
+ ModuDp(_) => print!("ModuDp"),
+ Mods(_) => print!("Mods"),
+ ModsDp(_) => print!("ModsDp"),
+ CmpE(_) => print!("CmpE"),
+ CmpNe(_) => print!("CmpNe"),
+ CmpSlt(_) => print!("CmpSlt"),
+ CmpUlt(_) => print!("CmpUlt"),
+ CmpSle(_) => print!("CmpSle"),
+ CmpUle(_) => print!("CmpUle"),
+ CmpSge(_) => print!("CmpSge"),
+ CmpUge(_) => print!("CmpUge"),
+ CmpSgt(_) => print!("CmpSgt"),
+ CmpUgt(_) => print!("CmpUgt"),
+ TestBit(_) => print!("TestBit"),
+ AddOverflow(_) => print!("AddOverflow"),
+ FcmpE(_) => print!("FcmpE"),
+ FcmpNe(_) => print!("FcmpNe"),
+ FcmpLt(_) => print!("FcmpLt"),
+ FcmpLe(_) => print!("FcmpLe"),
+ FcmpGe(_) => print!("FcmpGe"),
+ FcmpGt(_) => print!("FcmpGt"),
+ FcmpO(_) => print!("FcmpO"),
+ FcmpUo(_) => print!("FcmpUo"),
+ Fadd(_) => print!("Fadd"),
+ Fsub(_) => print!("Fsub"),
+ Fmul(_) => print!("Fmul"),
+ Fdiv(_) => print!("Fdiv"),
+ Adc(_) => print!("Adc"),
+ Sbb(_) => print!("Sbb"),
+ Rlc(_) => print!("Rlc"),
+ Rrc(_) => print!("Rrc"),
+ Call(_) => print!("Call"),
+ Tailcall(_) => print!("Tailcall"),
+ Syscall(_) => print!("Syscall"),
+ Intrinsic(_) => print!("Intrinsic"),
+ IntrinsicSsa(_) => print!("IntrinsicSsa"),
+ CallSsa(_) => print!("CallSsa"),
+ TailcallSsa(_) => print!("TailcallSsa"),
+ CallUntypedSsa(_) => print!("CallUntypedSsa"),
+ TailcallUntypedSsa(_) => print!("TailcallUntypedSsa"),
+ SyscallSsa(_) => print!("SyscallSsa"),
+ SyscallUntypedSsa(_) => print!("SyscallUntypedSsa"),
+ CallUntyped(_) => print!("CallUntyped"),
+ TailcallUntyped(_) => print!("TailcallUntyped"),
+ SyscallUntyped(_) => print!("SyscallUntyped"),
+ Neg(_) => print!("Neg"),
+ Not(_) => print!("Not"),
+ Sx(_) => print!("Sx"),
+ Zx(_) => print!("Zx"),
+ LowPart(_) => print!("LowPart"),
+ BoolToInt(_) => print!("BoolToInt"),
+ UnimplMem(_) => print!("UnimplMem"),
+ Fsqrt(_) => print!("Fsqrt"),
+ Fneg(_) => print!("Fneg"),
+ Fabs(_) => print!("Fabs"),
+ FloatToInt(_) => print!("FloatToInt"),
+ IntToFloat(_) => print!("IntToFloat"),
+ FloatConv(_) => print!("FloatConv"),
+ RoundToInt(_) => print!("RoundToInt"),
+ Floor(_) => print!("Floor"),
+ Ceil(_) => print!("Ceil"),
+ Ftrunc(_) => print!("Ftrunc"),
+ Load(_) => print!("Load"),
+ LoadStruct(_) => print!("LoadStruct"),
+ LoadStructSsa(_) => print!("LoadStructSsa"),
+ LoadSsa(_) => print!("LoadSsa"),
+ Ret(_) => print!("Ret"),
+ Var(_) => print!("Var"),
+ AddressOf(_) => print!("AddressOf"),
+ VarField(_) => print!("VarField"),
+ AddressOfField(_) => print!("AddressOfField"),
+ VarSsa(_) => print!("VarSsa"),
+ VarAliased(_) => print!("VarAliased"),
+ VarSsaField(_) => print!("VarSsaField"),
+ VarAliasedField(_) => print!("VarAliasedField"),
+ Trap(_) => print!("Trap"),
+ }
+}
+
+fn print_variable(func: &MediumLevelILFunction, var: &Variable) {
+ print!("{}", func.get_function().get_variable_name(var));
+}
+
+fn print_il_expr(instr: &MediumLevelILInstruction, mut indent: usize) {
+ print_indent(indent);
+ print_operation(instr.operation());
+ println!("");
+
+ indent += 1;
+
+ use MediumLevelILOperand::*;
+ for (_name, operand) in instr.operands() {
+ match operand {
+ Int(int) => {
+ print_indent(indent);
+ println!("int 0x{:x}", int);
+ }
+ Float(float) => {
+ print_indent(indent);
+ println!("int {:e}", float);
+ }
+ Expr(expr) => print_il_expr(&expr, indent),
+ Var(var) => {
+ print_indent(indent);
+ print!("var ");
+ print_variable(instr.function(), &var);
+ println!();
+ }
+ VarSsa(var) => {
+ print_indent(indent);
+ print!("ssa var ");
+ print_variable(instr.function(), &var.variable);
+ println!("#{}", var.version);
+ }
+ IntList(list) => {
+ print_indent(indent);
+ print!("index list ");
+ for i in list {
+ print!("{i} ");
+ }
+ println!();
+ }
+ VarList(list) => {
+ print_indent(indent);
+ print!("var list ");
+ for i in list {
+ print_variable(instr.function(), &i);
+ print!(" ");
+ }
+ println!();
+ }
+ VarSsaList(list) => {
+ print_indent(indent);
+ print!("ssa var list ");
+ for i in list {
+ print_variable(instr.function(), &i.variable);
+ print!("#{} ", i.version);
+ }
+ println!();
+ }
+ ExprList(list) => {
+ print_indent(indent);
+ println!("expr list");
+ for i in list {
+ print_il_expr(&i, indent + 1);
+ }
+ }
+ TargetMap(list) => {
+ print_indent(indent);
+ print!("target map ");
+ for (i, f) in list {
+ print!("({i}, {f}) ");
+ }
+ println!();
+ }
+ }
+ }
+}
+
+// Standalone executables need to provide a main function for rustc
+// Plugins should refer to `binaryninja::command::*` for the various registration callbacks.
+fn main() {
+ let mut args = env::args();
+ let _ = args.next().unwrap();
+ let Some(filename) = args.next() else {
+ panic!("Expected input filename\n");
+ };
+
+ // This loads all the core architecture, platform, etc plugins
+ // Standalone executables probably need to call this, but plugins do not
+ println!("Loading plugins...");
+ binaryninja::headless::init();
+
+ // Your code here...
+ println!("Loading binary...");
+ let bv = binaryninja::load(filename).expect("Couldn't open binary file");
+
+ // Go through all functions in the binary
+ for func in bv.functions().iter() {
+ let sym = func.symbol();
+ println!("Function {}:", sym.full_name());
+
+ let Ok(il) = func.medium_level_il() else {
+ println!(" Does not have MLIL\n");
+ continue;
+ };
+
+ // Loop through all blocks in the function
+ for block in il.basic_blocks().iter() {
+ // Loop though each instruction in the block
+ for instr in block.iter() {
+ // Generically parse the IL tree and display the parts
+ print_il_expr(&instr, 2);
+ }
+ }
+ println!();
+ }
+
+ // Important! Standalone executables need to call shutdown or they will hang forever
+ binaryninja::headless::shutdown();
+}