From b079d1a1e19151f5c39de84c2e1e6cc6a91abc74 Mon Sep 17 00:00:00 2001 From: Brandon Miller Date: Fri, 25 Apr 2025 13:30:45 -0400 Subject: Perform BB analysis from Architecture C++ API This commit moves AnalyzeBasicBlocks from the binary ninja core to the API and allows architecture plugins to optionally override AnalyzeBasicBlocks for a custom implementation Supply ABB inputs in BNBasicBlockAnalysisContext Register default analyze basic blocks callback This allows the nanomips and rust core architecture plugins to work again while using the C++ API DefaultAnalyzeBasicBlocks Use default ABB from Python plugins Fix bug in API ArchAndAddr operator overload Python APIs for basic block analysis --- rust/src/architecture.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'rust/src') diff --git a/rust/src/architecture.rs b/rust/src/architecture.rs index 41150840..c716bb95 100644 --- a/rust/src/architecture.rs +++ b/rust/src/architecture.rs @@ -30,6 +30,7 @@ use crate::{ string::*, types::{NameAndType, Type}, Endianness, + function::Function, }; use std::ops::Deref; use std::{ @@ -470,6 +471,19 @@ pub trait Architecture: 'static + Sized + AsRef { il: &LowLevelILMutableFunction, ) -> Option<(usize, bool)>; + fn analyze_basic_blocks( + &self, + function: &mut Function, + context: *mut BNBasicBlockAnalysisContext, + ) { + unsafe { + BNArchitectureDefaultAnalyzeBasicBlocks( + function.handle, + context, + ); + }; + } + /// Fallback flag value calculation path. This method is invoked when the core is unable to /// recover flag use semantics, and resorts to emitting instructions that explicitly set each /// observed flag to the value of an expression returned by this function. @@ -1533,6 +1547,20 @@ impl Architecture for CoreArchitecture { } } + fn analyze_basic_blocks( + &self, + function: &mut Function, + context: *mut BNBasicBlockAnalysisContext, + ) { + unsafe { + BNArchitectureAnalyzeBasicBlocks( + self.handle, + function.handle, + context, + ); + }; + } + fn flag_write_llil<'a>( &self, _flag: Self::Flag, @@ -2237,6 +2265,19 @@ where } } + extern "C" fn cb_analyze_basic_blocks( + ctxt: *mut c_void, + function: *mut BNFunction, + context: *mut BNBasicBlockAnalysisContext, + ) + where + A: 'static + Architecture> + Send + Sync, + { + let custom_arch = unsafe { &*(ctxt as *mut A) }; + let mut function = unsafe { Function::from_raw(function) }; + custom_arch.analyze_basic_blocks(&mut function, context); + } + extern "C" fn cb_reg_name(ctxt: *mut c_void, reg: u32) -> *mut c_char where A: 'static + Architecture> + Send + Sync, @@ -3155,6 +3196,7 @@ where getInstructionText: Some(cb_get_instruction_text::), freeInstructionText: Some(cb_free_instruction_text), getInstructionLowLevelIL: Some(cb_instruction_llil::), + analyzeBasicBlocks: Some(cb_analyze_basic_blocks::), getRegisterName: Some(cb_reg_name::), getFlagName: Some(cb_flag_name::), -- cgit v1.3.1