diff options
| author | Mason Reed <mason@vector35.com> | 2025-12-06 17:06:16 -0500 |
|---|---|---|
| committer | Mason Reed <35282038+emesare@users.noreply.github.com> | 2025-12-10 17:35:19 -0500 |
| commit | 7090d904513c3e80321bb6a8aba9c3b37d809bac (patch) | |
| tree | 0a329b0d6b01c4bd7a2ed9547f6862fce8f08fa1 /rust | |
| parent | e699fec29106d46303ef4bcb464a768e23bb28ea (diff) | |
[Rust] Remove redundant `ArchAndAddr` type
Use `Location` instead, `arch` can be a nullptr
Diffstat (limited to 'rust')
| -rw-r--r-- | rust/src/architecture.rs | 58 | ||||
| -rw-r--r-- | rust/src/binary_view.rs | 30 | ||||
| -rw-r--r-- | rust/src/function.rs | 42 |
3 files changed, 49 insertions, 81 deletions
diff --git a/rust/src/architecture.rs b/rust/src/architecture.rs index 3ea21514..2f0490f2 100644 --- a/rust/src/architecture.rs +++ b/rust/src/architecture.rs @@ -23,7 +23,7 @@ use crate::{ calling_convention::CoreCallingConvention, data_buffer::DataBuffer, disassembly::InstructionTextToken, - function::{ArchAndAddr, Function, NativeBlock}, + function::{Function, NativeBlock}, platform::Platform, rc::*, relocation::CoreRelocationHandler, @@ -47,6 +47,7 @@ use crate::relocation::{CustomRelocationHandlerHandle, RelocationHandler}; use crate::variable::IndirectBranchInfo; use crate::confidence::Conf; +use crate::function::Location; use crate::low_level_il::expression::ValueExpr; use crate::low_level_il::lifting::{ get_default_flag_cond_llil, get_default_flag_write_llil, LowLevelILFlagWriteOp, @@ -1959,7 +1960,7 @@ pub struct BasicBlockAnalysisContext { // In pub indirect_branches: Vec<IndirectBranchInfo>, - pub indirect_no_return_calls: HashSet<ArchAndAddr>, + pub indirect_no_return_calls: HashSet<Location>, pub analysis_skip_override: BNFunctionAnalysisSkipOverride, pub guided_analysis_mode: bool, pub trigger_guided_on_invalid_instruction: bool, @@ -1969,13 +1970,13 @@ pub struct BasicBlockAnalysisContext { // In/Out pub max_size_reached: bool, - contextual_returns: HashMap<ArchAndAddr, bool>, + contextual_returns: HashMap<Location, bool>, // Out - direct_code_references: HashMap<u64, ArchAndAddr>, - direct_no_return_calls: HashSet<ArchAndAddr>, - halted_disassembly_addresses: HashSet<ArchAndAddr>, - inlined_unresolved_indirect_branches: HashSet<ArchAndAddr>, + direct_code_references: HashMap<u64, Location>, + direct_no_return_calls: HashSet<Location>, + halted_disassembly_addresses: HashSet<Location>, + inlined_unresolved_indirect_branches: HashSet<Location>, } impl BasicBlockAnalysisContext { @@ -1995,7 +1996,7 @@ impl BasicBlockAnalysisContext { let indirect_no_return_calls = (0..ctx_ref.indirectNoReturnCallsCount) .map(|i| { let raw = unsafe { std::ptr::read(ctx_ref.indirectNoReturnCalls.add(i)) }; - ArchAndAddr::from(raw) + Location::from(raw) }) .collect::<HashSet<_>>(); @@ -2003,7 +2004,7 @@ impl BasicBlockAnalysisContext { .map(|i| { let loc = unsafe { let raw = std::ptr::read(ctx_ref.contextualFunctionReturnLocations.add(i)); - ArchAndAddr::from(raw) + Location::from(raw) }; let val = unsafe { *ctx_ref.contextualFunctionReturnValues.add(i) }; (loc, val) @@ -2014,7 +2015,7 @@ impl BasicBlockAnalysisContext { .map(|i| { let src = unsafe { let raw = std::ptr::read(ctx_ref.directRefSources.add(i)); - ArchAndAddr::from(raw) + Location::from(raw) }; let tgt = unsafe { *ctx_ref.directRefTargets.add(i) }; (tgt, src) @@ -2024,14 +2025,14 @@ impl BasicBlockAnalysisContext { let direct_no_return_calls = (0..ctx_ref.directNoReturnCallsCount) .map(|i| { let raw = unsafe { std::ptr::read(ctx_ref.directNoReturnCalls.add(i)) }; - ArchAndAddr::from(raw) + Location::from(raw) }) .collect::<HashSet<_>>(); let halted_disassembly_addresses = (0..ctx_ref.haltedDisassemblyAddressesCount) .map(|i| { let raw = unsafe { std::ptr::read(ctx_ref.haltedDisassemblyAddresses.add(i)) }; - ArchAndAddr::from(raw) + Location::from(raw) }) .collect::<HashSet<_>>(); @@ -2040,7 +2041,7 @@ impl BasicBlockAnalysisContext { .map(|i| { let raw = unsafe { std::ptr::read(ctx_ref.inlinedUnresolvedIndirectBranches.add(i)) }; - ArchAndAddr::from(raw) + Location::from(raw) }) .collect::<HashSet<_>>(); @@ -2064,7 +2065,8 @@ impl BasicBlockAnalysisContext { } } - pub fn add_contextual_return(&mut self, loc: ArchAndAddr, value: bool) { + pub fn add_contextual_return(&mut self, loc: impl Into<Location>, value: bool) { + let loc = loc.into(); if !self.contextual_returns.contains_key(&loc) { self.contextual_returns_dirty = true; } @@ -2072,20 +2074,22 @@ impl BasicBlockAnalysisContext { self.contextual_returns.insert(loc, value); } - pub fn add_direct_code_reference(&mut self, target: u64, src: ArchAndAddr) { - self.direct_code_references.entry(target).or_insert(src); + pub fn add_direct_code_reference(&mut self, target: u64, src: impl Into<Location>) { + self.direct_code_references + .entry(target) + .or_insert(src.into()); } - pub fn add_direct_no_return_call(&mut self, loc: ArchAndAddr) { - self.direct_no_return_calls.insert(loc); + pub fn add_direct_no_return_call(&mut self, loc: impl Into<Location>) { + self.direct_no_return_calls.insert(loc.into()); } - pub fn add_halted_disassembly_address(&mut self, loc: ArchAndAddr) { - self.halted_disassembly_addresses.insert(loc); + pub fn add_halted_disassembly_address(&mut self, loc: impl Into<Location>) { + self.halted_disassembly_addresses.insert(loc.into()); } - pub fn add_inlined_unresolved_indirect_branch(&mut self, loc: ArchAndAddr) { - self.inlined_unresolved_indirect_branches.insert(loc); + pub fn add_inlined_unresolved_indirect_branch(&mut self, loc: impl Into<Location>) { + self.inlined_unresolved_indirect_branches.insert(loc.into()); } pub fn create_basic_block( @@ -2121,7 +2125,7 @@ impl BasicBlockAnalysisContext { let mut sources: Vec<BNArchitectureAndAddress> = Vec::with_capacity(total); let mut targets: Vec<u64> = Vec::with_capacity(total); for (target, src) in &self.direct_code_references { - sources.push(src.into_raw()); + sources.push(BNArchitectureAndAddress::from(src)); targets.push(*target); } unsafe { @@ -2138,7 +2142,7 @@ impl BasicBlockAnalysisContext { let total = self.direct_no_return_calls.len(); let mut locations: Vec<BNArchitectureAndAddress> = Vec::with_capacity(total); for loc in &self.direct_no_return_calls { - locations.push(loc.into_raw()); + locations.push(BNArchitectureAndAddress::from(loc)); } unsafe { BNAnalyzeBasicBlocksContextSetDirectNoReturnCalls( @@ -2153,7 +2157,7 @@ impl BasicBlockAnalysisContext { let total = self.halted_disassembly_addresses.len(); let mut locations: Vec<BNArchitectureAndAddress> = Vec::with_capacity(total); for loc in &self.halted_disassembly_addresses { - locations.push(loc.into_raw()); + locations.push(BNArchitectureAndAddress::from(loc)); } unsafe { BNAnalyzeBasicBlocksContextSetHaltedDisassemblyAddresses( @@ -2168,7 +2172,7 @@ impl BasicBlockAnalysisContext { let total = self.inlined_unresolved_indirect_branches.len(); let mut locations: Vec<BNArchitectureAndAddress> = Vec::with_capacity(total); for loc in &self.inlined_unresolved_indirect_branches { - locations.push(loc.into_raw()); + locations.push(BNArchitectureAndAddress::from(loc)); } unsafe { BNAnalyzeBasicBlocksContextSetInlinedUnresolvedIndirectBranches( @@ -2188,7 +2192,7 @@ impl BasicBlockAnalysisContext { let mut locations: Vec<BNArchitectureAndAddress> = Vec::with_capacity(total); let mut values: Vec<bool> = Vec::with_capacity(total); for (loc, value) in &self.contextual_returns { - locations.push(loc.into_raw()); + locations.push(BNArchitectureAndAddress::from(loc)); values.push(*value); } unsafe { diff --git a/rust/src/binary_view.rs b/rust/src/binary_view.rs index 4b55e321..e1b7a5c0 100644 --- a/rust/src/binary_view.rs +++ b/rust/src/binary_view.rs @@ -38,7 +38,7 @@ use crate::external_library::{ExternalLibrary, ExternalLocation}; use crate::file_accessor::{Accessor, FileAccessor}; use crate::file_metadata::FileMetadata; use crate::flowgraph::FlowGraph; -use crate::function::{ArchAndAddr, Function, FunctionViewType, NativeBlock}; +use crate::function::{Function, FunctionViewType, Location, NativeBlock}; use crate::linear_view::{LinearDisassemblyLine, LinearViewCursor}; use crate::metadata::Metadata; use crate::platform::Platform; @@ -1445,29 +1445,23 @@ pub trait BinaryViewExt: BinaryViewBase { } } + // TODO: Should this instead be implemented on [`Function`] considering `src_func`? `Location` is local to the source function. fn should_skip_target_analysis( &self, - source: &ArchAndAddr, - srcfunc: &Function, - srcend: u64, - target: &ArchAndAddr, + src_loc: impl Into<Location>, + src_func: &Function, + src_end: u64, + target: impl Into<Location>, ) -> bool { - let mut srccopy = BNArchitectureAndAddress { - arch: source.arch.handle, - address: source.addr, - }; - let mut targetcopy = BNArchitectureAndAddress { - arch: target.arch.handle, - address: target.addr, - }; - + let src_loc = src_loc.into(); + let target = target.into(); unsafe { BNShouldSkipTargetAnalysis( self.as_ref().handle, - &mut srccopy, - srcfunc.handle, - srcend, - &mut targetcopy, + &mut src_loc.into(), + src_func.handle, + src_end, + &mut target.into(), ) } } diff --git a/rust/src/function.rs b/rust/src/function.rs index 5e4cf08a..1ef0a377 100644 --- a/rust/src/function.rs +++ b/rust/src/function.rs @@ -71,20 +71,21 @@ impl Location { arch: Some(unsafe { CoreArchitecture::from_raw(arch) }), } } + + pub fn new(arch: Option<CoreArchitecture>, addr: u64) -> Self { + Self { arch, addr } + } } impl From<u64> for Location { fn from(addr: u64) -> Self { - Location { arch: None, addr } + Location::new(None, addr) } } impl From<(CoreArchitecture, u64)> for Location { fn from(loc: (CoreArchitecture, u64)) -> Self { - Location { - arch: Some(loc.0), - addr: loc.1, - } + Location::new(Some(loc.0), loc.1) } } @@ -2998,34 +2999,3 @@ unsafe impl CoreArrayProviderInner for Comment { } } } - -#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)] -pub struct ArchAndAddr { - pub arch: CoreArchitecture, - pub addr: u64, -} - -impl ArchAndAddr { - pub fn new(arch: CoreArchitecture, addr: u64) -> Self { - Self { arch, addr } - } -} - -impl From<BNArchitectureAndAddress> for ArchAndAddr { - fn from(raw: BNArchitectureAndAddress) -> Self { - unsafe { - let arch = CoreArchitecture::from_raw(raw.arch); - let addr = raw.address; - ArchAndAddr { arch, addr } - } - } -} - -impl ArchAndAddr { - pub fn into_raw(self) -> BNArchitectureAndAddress { - BNArchitectureAndAddress { - arch: self.arch.handle, - address: self.addr, - } - } -} |
