diff options
| author | Mason Reed <mason@vector35.com> | 2024-10-28 21:17:48 -0400 |
|---|---|---|
| committer | Mason Reed <mason@vector35.com> | 2024-10-28 21:21:55 -0400 |
| commit | 2f17bf7439e2339b4ead7470cafd033875f9837c (patch) | |
| tree | ac0cc0b7a1f17234f5e517cf8f73dcbd9a949fa8 /rust/src/lib.rs | |
| parent | 690c8b92d5605332f2d7a6b5d138b1eccc69fbce (diff) | |
Rust: Add object destructor API
Diffstat (limited to 'rust/src/lib.rs')
| -rw-r--r-- | rust/src/lib.rs | 58 |
1 files changed, 55 insertions, 3 deletions
diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 0c1022c0..e6323c07 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -174,10 +174,10 @@ pub mod update; pub mod workflow; use std::collections::HashMap; -use std::ffi::CStr; +use std::ffi::{c_void, CStr}; use std::path::PathBuf; use std::ptr; - +use binaryninjacore_sys::{BNBinaryView, BNFileMetadata, BNFunction, BNObjectDestructionCallbacks, BNRegisterObjectDestructionCallbacks, BNUnregisterObjectDestructionCallbacks}; pub use binaryninjacore_sys::BNBranchType as BranchType; pub use binaryninjacore_sys::BNEndianness as Endianness; use binaryview::BinaryView; @@ -185,7 +185,8 @@ use metadata::Metadata; use metadata::MetadataType; use string::BnStrCompatible; use string::IntoJson; - +use crate::filemetadata::FileMetadata; +use crate::function::Function; // Commented out to suppress unused warnings // const BN_MAX_INSTRUCTION_LENGTH: u64 = 256; // const BN_DEFAULT_INSTRUCTION_LENGTH: u64 = 16; @@ -613,6 +614,57 @@ pub fn memory_info() -> HashMap<String, u64> { usage } +/// The trait required for receiving core object destruction callbacks. +pub trait ObjectDestructor: 'static + Sync + Sized { + fn destruct_view(&self, _view: &BinaryView) {} + fn destruct_file_metadata(&self, _metadata: &FileMetadata) {} + fn destruct_function(&self, _func: &Function) {} + + unsafe extern "C" fn cb_destruct_binary_view(ctxt: *mut c_void, view: *mut BNBinaryView) + { + ffi_wrap!("ObjectDestructor::destruct_view", { + let view_type = &*(ctxt as *mut Self); + let view = BinaryView::from_raw(view); + view_type.destruct_view(&view); + }) + } + + unsafe extern "C" fn cb_destruct_file_metadata(ctxt: *mut c_void, file: *mut BNFileMetadata) + { + ffi_wrap!("ObjectDestructor::destruct_file_metadata", { + let view_type = &*(ctxt as *mut Self); + let file = FileMetadata::from_raw(file); + view_type.destruct_file_metadata(&file); + }) + } + + unsafe extern "C" fn cb_destruct_function(ctxt: *mut c_void, func: *mut BNFunction) + { + ffi_wrap!("ObjectDestructor::destruct_function", { + let view_type = &*(ctxt as *mut Self); + let func = Function::from_raw(func); + view_type.destruct_function(&func); + }) + } + + unsafe fn as_callbacks(&'static mut self) -> BNObjectDestructionCallbacks { + BNObjectDestructionCallbacks { + context: std::mem::transmute(&self), + destructBinaryView: Some(Self::cb_destruct_binary_view), + destructFileMetadata: Some(Self::cb_destruct_file_metadata), + destructFunction: Some(Self::cb_destruct_function), + } + } + + fn register(&'static mut self) { + unsafe { BNRegisterObjectDestructionCallbacks(&mut self.as_callbacks()) }; + } + + fn unregister(&'static mut self) { + unsafe { BNUnregisterObjectDestructionCallbacks(&mut self.as_callbacks()) }; + } +} + pub fn version() -> string::BnString { unsafe { string::BnString::from_raw(binaryninjacore_sys::BNGetVersionString()) } } |
