Repository navigation
Add support for Rust architecture hook plugins #8659
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
Merged
+1,556
−180
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dff1722
Add support for Rust architecture hook plugins
zznop 3e427c9
Fix flag-write forwarding and intrinsic-class overrides in Rust hooks
zznop a59e361
Preserve address mutation throughout the Rust architecture API
zznop 2de70ae
Honor architecture-level intrinsic class overrides in Rust
zznop 0cd93e5
Expand Rust flag-write operations and unify callback dispatch
zznop 21c1272
Preserve function-context destruction through stacked Rust hooks
zznop 24d3085
Preserve decoded length when Rust LLIL lifting fails
zznop 2288202
Preserve instruction-token metadata through Rust architecture hooks
zznop 2084452
Rust architecture hook plugin example
zznop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| [package] | ||
| name = "example_primask_write_hook" | ||
| version = "0.1.0" | ||
| edition = "2021" | ||
| license = "Apache-2.0" | ||
| publish = false | ||
|
|
||
| [lib] | ||
| crate-type = ["cdylib"] | ||
|
|
||
| [dependencies] | ||
| binaryninjacore-sys = { path = "../../binaryninjacore-sys" } | ||
| binaryninja = { path = "../.." } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # PRIMASK Write Hook Example | ||
|
|
||
| This example registers a Rust architecture hook on `thumb2` that appends ` ; Toggling interrupts` to the disassembly | ||
| of `msr primask` instructions. | ||
|
|
||
| ## Building | ||
|
|
||
| ```sh | ||
| # Build from the root directory (binaryninja-api) | ||
| cargo build -p example_primask_write_hook | ||
| # Link binary on macOS | ||
| ln -sf "$PWD/target/debug/libexample_primask_write_hook.dylib" ~/Library/Application\ Support/Binary\ Ninja/plugins/ | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| fn main() { | ||
| let link_path = std::env::var_os("DEP_BINARYNINJACORE_PATH") | ||
| .expect("DEP_BINARYNINJACORE_PATH not specified"); | ||
|
|
||
| println!("cargo::rustc-link-lib=dylib=binaryninjacore"); | ||
| println!("cargo::rustc-link-search={}", link_path.to_str().unwrap()); | ||
|
|
||
| #[cfg(target_os = "linux")] | ||
| { | ||
| println!( | ||
| "cargo::rustc-link-arg=-Wl,-rpath,{0},-L{0}", | ||
| link_path.to_string_lossy() | ||
| ); | ||
| } | ||
|
|
||
| #[cfg(target_os = "macos")] | ||
| { | ||
| let crate_name = std::env::var("CARGO_PKG_NAME").expect("CARGO_PKG_NAME not set"); | ||
| let lib_name = crate_name.replace('-', "_"); | ||
| println!( | ||
| "cargo::rustc-link-arg=-Wl,-install_name,@rpath/lib{}.dylib", | ||
| lib_name | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| //! Rust architecture hook plugin that annotates ARM/thumb writes to PRIMASK | ||
|
|
||
| use binaryninja::architecture::{ | ||
| register_architecture_hook, Architecture, ArchitectureHook, CoreArchitecture, | ||
| }; | ||
| use binaryninja::disassembly::{InstructionTextToken, InstructionTextTokenKind}; | ||
| use binaryninja::logger::{bn_log, BnLogLevel}; | ||
| use std::ffi::c_void; | ||
| use std::ptr::NonNull; | ||
|
|
||
| struct InterruptHook; | ||
|
|
||
| impl ArchitectureHook for InterruptHook { | ||
| fn instruction_text_with_context( | ||
| &self, | ||
| base: &CoreArchitecture, | ||
| data: &[u8], | ||
| addr: u64, | ||
| _context: Option<NonNull<c_void>>, | ||
| ) -> Option<(usize, Vec<InstructionTextToken>)> { | ||
| let (length, mut tokens) = base.instruction_text(data, addr)?; | ||
| if tokens.first().is_some_and(|token| token.text == "msr") | ||
| && tokens.get(2).is_some_and(|token| token.text == "primask") | ||
| { | ||
| tokens.push(InstructionTextToken::new( | ||
| " ; Toggling interrupts", | ||
| InstructionTextTokenKind::Annotation, | ||
| )); | ||
| } | ||
| Some((length, tokens)) | ||
| } | ||
| } | ||
|
|
||
| #[allow(non_snake_case)] | ||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn CorePluginDependencies() { | ||
| binaryninja::add_required_plugin_dependency("arch_armv7"); | ||
| } | ||
|
|
||
| #[allow(non_snake_case)] | ||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn CorePluginInit() -> bool { | ||
| let Some(thumb2) = CoreArchitecture::by_name("thumb2") else { | ||
| bn_log( | ||
| "interrupts_arch_hook", | ||
| BnLogLevel::ErrorLog, | ||
| "thumb2 architecture unavailable", | ||
| ); | ||
| return false; | ||
| }; | ||
| register_architecture_hook(thumb2, InterruptHook); | ||
| bn_log( | ||
| "interrupts_arch_hook", | ||
| BnLogLevel::InfoLog, | ||
| "Registered Rust interrupt annotation hook on thumb2", | ||
| ); | ||
| true | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.