From 168a3fd34824adc9c6a606cd144219701f15cccf Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Wed, 17 Dec 2025 21:23:46 -0500 Subject: [Rust] Replace `log` with `tracing` - Added more documentation - Replaced global named logger for plugins, fixing the issue when the CU has multiple (e.g. statically linked demo) - Simplified some misc code This is a breaking change, but I believe there is no better time to make it, we cannot continue to use the `log` crate, it is too limited for our needs. --- .../src/activities/objc_msg_send_calls.rs | 3 ++- .../objc_msg_send_calls/rewrite_to_direct_call.rs | 3 ++- .../src/activities/remove_memory_management.rs | 3 ++- plugins/workflow_objc/src/activities/super_init.rs | 19 +++++++++++-------- plugins/workflow_objc/src/lib.rs | 10 +++------- plugins/workflow_objc/src/metadata/global_state.rs | 13 +++++++------ plugins/workflow_objc/src/workflow.rs | 3 ++- 7 files changed, 29 insertions(+), 25 deletions(-) (limited to 'plugins/workflow_objc/src') diff --git a/plugins/workflow_objc/src/activities/objc_msg_send_calls.rs b/plugins/workflow_objc/src/activities/objc_msg_send_calls.rs index b39c843c..86024047 100644 --- a/plugins/workflow_objc/src/activities/objc_msg_send_calls.rs +++ b/plugins/workflow_objc/src/activities/objc_msg_send_calls.rs @@ -7,6 +7,7 @@ use binaryninja::{ instruction::{InstructionHandler as _, LowLevelILInstruction, LowLevelILInstructionKind}, operation::{CallSsa, Operation}, }, + tracing, variable::PossibleValueSet, workflow::AnalysisContext, }; @@ -53,7 +54,7 @@ pub fn process(ac: &AnalysisContext) -> Result<(), Error> { Ok(true) => function_changed = true, Ok(_) => {} Err(err) => { - log::error!( + tracing::error!( "Error processing instruction at {:#x}: {}", insn.address(), err diff --git a/plugins/workflow_objc/src/activities/objc_msg_send_calls/rewrite_to_direct_call.rs b/plugins/workflow_objc/src/activities/objc_msg_send_calls/rewrite_to_direct_call.rs index 0ddf2945..56c2aa52 100644 --- a/plugins/workflow_objc/src/activities/objc_msg_send_calls/rewrite_to_direct_call.rs +++ b/plugins/workflow_objc/src/activities/objc_msg_send_calls/rewrite_to_direct_call.rs @@ -4,6 +4,7 @@ use binaryninja::{ function::{LowLevelILFunction, Mutable, NonSSA, SSA}, instruction::{InstructionHandler as _, LowLevelILInstruction, LowLevelILInstructionKind}, }, + tracing, }; use super::MessageSendType; @@ -48,7 +49,7 @@ pub fn process_call( Ok(()) } _ => { - log::error!( + tracing::error!( "Unexpected LLIL operation for objc_msgSend call at {:#x}", insn.address() ); diff --git a/plugins/workflow_objc/src/activities/remove_memory_management.rs b/plugins/workflow_objc/src/activities/remove_memory_management.rs index 3990f010..fdd76650 100644 --- a/plugins/workflow_objc/src/activities/remove_memory_management.rs +++ b/plugins/workflow_objc/src/activities/remove_memory_management.rs @@ -11,6 +11,7 @@ use binaryninja::{ lifting::LowLevelILLabel, LowLevelILRegisterKind, }, + tracing, variable::PossibleValueSet, workflow::AnalysisContext, }; @@ -176,7 +177,7 @@ pub fn process(ac: &AnalysisContext) -> Result<(), Error> { Ok(true) => function_changed = true, Ok(_) => {} Err(err) => { - log::error!( + tracing::error!( "Error processing instruction at {:#x}: {}", insn.address(), err diff --git a/plugins/workflow_objc/src/activities/super_init.rs b/plugins/workflow_objc/src/activities/super_init.rs index cfd3ecdc..1cdf8825 100644 --- a/plugins/workflow_objc/src/activities/super_init.rs +++ b/plugins/workflow_objc/src/activities/super_init.rs @@ -10,6 +10,7 @@ use binaryninja::{ MediumLevelILFunction, MediumLevelILLiftedInstruction, MediumLevelILLiftedInstructionKind, }, rc::Ref, + tracing, types::Type, variable::{RegisterValueType, SSAVariable}, workflow::AnalysisContext, @@ -148,7 +149,7 @@ fn return_type_for_super_init(call: &Call, view: &BinaryView) -> Option Option Option src, _ => { // The Swift compiler generates code that conditionally assigns to the receiver field of `objc_super`. - log::debug!( + tracing::debug!( "Unexpected variable definition kind at {:#0x} {:#x?}", super_param_def.address, super_param_def @@ -184,7 +185,7 @@ fn return_type_for_super_init(call: &Call, view: &BinaryView) -> Option src_var, _ => { // The Swift compiler generates code that initializes the `objc_super` variable in more varied ways. - log::debug!( + tracing::debug!( " found non-address-of variable definition of `objc_super` variable at {:#0x} {:?}", super_param_def.address, super_param_def @@ -223,7 +224,7 @@ fn return_type_for_super_init(call: &Call, view: &BinaryView) -> Option Option Option bool { - Logger::new("Plugin.Objective-C") - .with_level(LevelFilter::Debug) - .init(); + binaryninja::tracing_init!("Plugin.Objective-C"); if workflow::register_activities().is_err() { - log::warn!("Failed to register Objective-C workflow"); + tracing::warn!("Failed to register Objective-C workflow"); return false; }; diff --git a/plugins/workflow_objc/src/metadata/global_state.rs b/plugins/workflow_objc/src/metadata/global_state.rs index 61104451..c6682a5f 100644 --- a/plugins/workflow_objc/src/metadata/global_state.rs +++ b/plugins/workflow_objc/src/metadata/global_state.rs @@ -1,10 +1,11 @@ +use binaryninja::file_metadata::SessionId; use binaryninja::{ binary_view::{BinaryView, BinaryViewBase, BinaryViewExt}, file_metadata::FileMetadata, metadata::Metadata, rc::Ref, settings::{QueryOptions, Settings}, - ObjectDestructor, + tracing, ObjectDestructor, }; use dashmap::DashMap; use once_cell::sync::Lazy; @@ -31,8 +32,8 @@ struct SelectorImplementations { sel_to_impl: HashMap>, } -static VIEW_INFOS: Lazy>> = Lazy::new(DashMap::new); -static IGNORED_VIEWS: Lazy> = Lazy::new(DashMap::new); +static VIEW_INFOS: Lazy>> = Lazy::new(DashMap::new); +static IGNORED_VIEWS: Lazy> = Lazy::new(DashMap::new); struct ObjectLifetimeObserver; @@ -69,7 +70,7 @@ impl GlobalState { observer.register(); } - fn id(bv: &BinaryView) -> usize { + fn id(bv: &BinaryView) -> SessionId { bv.file().session_id() } @@ -159,7 +160,7 @@ impl AnalysisInfo { }; let version_meta = meta.get("version")?; if version_meta.get_unsigned_integer()? != 1 { - log::error!( + tracing::error!( "workflow_objc: Unexpected Objective-C metadata version. Expected 1, got {}.", version_meta.get_unsigned_integer()? ); @@ -192,7 +193,7 @@ impl AnalysisInfo { for item in &array { let item = item.get_array()?; if item.len() != 2 { - log::warn!( + tracing::warn!( "Expected selector implementation metadata to have 2 items, found {}", item.len() ); diff --git a/plugins/workflow_objc/src/workflow.rs b/plugins/workflow_objc/src/workflow.rs index 08a39042..13e3a71e 100644 --- a/plugins/workflow_objc/src/workflow.rs +++ b/plugins/workflow_objc/src/workflow.rs @@ -1,3 +1,4 @@ +use binaryninja::tracing; use binaryninja::workflow::{activity, Activity, AnalysisContext, Workflow}; use crate::{activities, error::WorkflowRegistrationError}; @@ -15,7 +16,7 @@ fn run( ) -> impl Fn(&AnalysisContext) { move |ac| { if let Err(err) = func(ac) { - log::debug!("Error occurred while running activity: {err:#x?}"); + tracing::debug!("Error occurred while running activity: {err:#x?}"); } } } -- cgit v1.3.1