From 80b07dfa158bb093cc93e602363655b62ae595f8 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Fri, 22 Dec 2023 13:46:10 -0700 Subject: Support function recognizers in Rust architecture plugins --- rust/src/functionrecognizer.rs | 107 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 rust/src/functionrecognizer.rs (limited to 'rust/src/functionrecognizer.rs') diff --git a/rust/src/functionrecognizer.rs b/rust/src/functionrecognizer.rs new file mode 100644 index 00000000..ec567308 --- /dev/null +++ b/rust/src/functionrecognizer.rs @@ -0,0 +1,107 @@ +use crate::architecture::Architecture; +use crate::{ + architecture::CoreArchitecture, binaryview::BinaryView, function::Function, llil, mlil, +}; +use binaryninjacore_sys::*; +use std::os::raw::c_void; + +pub trait FunctionRecognizer { + fn recognize_low_level_il( + &self, + _bv: &BinaryView, + _func: &Function, + _llil: &llil::RegularFunction, + ) -> bool { + false + } + + fn recognize_medium_level_il( + &self, + _bv: &BinaryView, + _func: &Function, + _mlil: &mlil::MediumLevelILFunction, + ) -> bool { + false + } +} + +fn create_function_recognizer_registration(recognizer: R) -> BNFunctionRecognizer +where + R: 'static + FunctionRecognizer + Send + Sync + Sized, +{ + #[repr(C)] + struct FunctionRecognizerHandlerContext + where + R: 'static + FunctionRecognizer + Send + Sync, + { + recognizer: R, + } + + extern "C" fn cb_recognize_low_level_il( + ctxt: *mut c_void, + bv: *mut BNBinaryView, + func: *mut BNFunction, + llil: *mut BNLowLevelILFunction, + ) -> bool + where + R: 'static + FunctionRecognizer + Send + Sync, + { + let custom_handler = unsafe { &*(ctxt as *mut R) }; + let bv = unsafe { BinaryView::from_raw(BNNewViewReference(bv)) }; + let arch = unsafe { BNGetFunctionArchitecture(func) }; + let func = unsafe { Function::from_raw(BNNewFunctionReference(func)) }; + if arch.is_null() { + return false; + } + let arch = unsafe { CoreArchitecture::from_raw(arch) }; + let llil = unsafe { llil::RegularFunction::from_raw(arch, llil) }; + custom_handler.recognize_low_level_il(bv.as_ref(), func.as_ref(), &llil) + } + + extern "C" fn cb_recognize_medium_level_il( + ctxt: *mut c_void, + bv: *mut BNBinaryView, + func: *mut BNFunction, + mlil: *mut BNMediumLevelILFunction, + ) -> bool + where + R: 'static + FunctionRecognizer + Send + Sync, + { + let custom_handler = unsafe { &*(ctxt as *mut R) }; + let bv = unsafe { BinaryView::from_raw(BNNewViewReference(bv)) }; + let func = unsafe { Function::from_raw(BNNewFunctionReference(func)) }; + let mlil = unsafe { mlil::MediumLevelILFunction::from_raw(mlil) }; + custom_handler.recognize_medium_level_il(bv.as_ref(), func.as_ref(), &mlil) + } + + let recognizer = FunctionRecognizerHandlerContext { recognizer }; + let raw = Box::into_raw(Box::new(recognizer)); + BNFunctionRecognizer { + context: raw as *mut _, + recognizeLowLevelIL: Some(cb_recognize_low_level_il::), + recognizeMediumLevelIL: Some(cb_recognize_medium_level_il::), + } +} + +pub fn register_global_function_recognizer(recognizer: R) +where + R: 'static + FunctionRecognizer + Send + Sync + Sized, +{ + let mut recognizer = create_function_recognizer_registration::(recognizer); + unsafe { + BNRegisterGlobalFunctionRecognizer(&mut recognizer as *mut _); + } +} + +pub(crate) fn register_arch_function_recognizer(arch: &CoreArchitecture, recognizer: R) +where + R: 'static + FunctionRecognizer + Send + Sync + Sized, +{ + let mut recognizer = create_function_recognizer_registration::(recognizer); + unsafe { + BNRegisterArchitectureFunctionRecognizer( + arch.handle().as_ref().0, + &mut recognizer as *mut _, + ); + } +} -- cgit v1.3.1