Skip to content

Commit 1a4fb60

Browse files
committed
[Rust] Remove redundant ArchAndAddr type
Use `Location` instead, `arch` can be a nullptr
1 parent dc4fe7b commit 1a4fb60

File tree

3 files changed

+49
-81
lines changed

3 files changed

+49
-81
lines changed

rust/src/architecture.rs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{
2323
calling_convention::CoreCallingConvention,
2424
data_buffer::DataBuffer,
2525
disassembly::InstructionTextToken,
26-
function::{ArchAndAddr, Function, NativeBlock},
26+
function::{Function, NativeBlock},
2727
platform::Platform,
2828
rc::*,
2929
relocation::CoreRelocationHandler,
@@ -47,6 +47,7 @@ use crate::relocation::{CustomRelocationHandlerHandle, RelocationHandler};
4747
use crate::variable::IndirectBranchInfo;
4848

4949
use crate::confidence::Conf;
50+
use crate::function::Location;
5051
use crate::low_level_il::expression::ValueExpr;
5152
use crate::low_level_il::lifting::{
5253
get_default_flag_cond_llil, get_default_flag_write_llil, LowLevelILFlagWriteOp,
@@ -1959,7 +1960,7 @@ pub struct BasicBlockAnalysisContext {
19591960

19601961
// In
19611962
pub indirect_branches: Vec<IndirectBranchInfo>,
1962-
pub indirect_no_return_calls: HashSet<ArchAndAddr>,
1963+
pub indirect_no_return_calls: HashSet<Location>,
19631964
pub analysis_skip_override: BNFunctionAnalysisSkipOverride,
19641965
pub guided_analysis_mode: bool,
19651966
pub trigger_guided_on_invalid_instruction: bool,
@@ -1969,13 +1970,13 @@ pub struct BasicBlockAnalysisContext {
19691970

19701971
// In/Out
19711972
pub max_size_reached: bool,
1972-
contextual_returns: HashMap<ArchAndAddr, bool>,
1973+
contextual_returns: HashMap<Location, bool>,
19731974

19741975
// Out
1975-
direct_code_references: HashMap<u64, ArchAndAddr>,
1976-
direct_no_return_calls: HashSet<ArchAndAddr>,
1977-
halted_disassembly_addresses: HashSet<ArchAndAddr>,
1978-
inlined_unresolved_indirect_branches: HashSet<ArchAndAddr>,
1976+
direct_code_references: HashMap<u64, Location>,
1977+
direct_no_return_calls: HashSet<Location>,
1978+
halted_disassembly_addresses: HashSet<Location>,
1979+
inlined_unresolved_indirect_branches: HashSet<Location>,
19791980
}
19801981

19811982
impl BasicBlockAnalysisContext {
@@ -1995,15 +1996,15 @@ impl BasicBlockAnalysisContext {
19951996
let indirect_no_return_calls = (0..ctx_ref.indirectNoReturnCallsCount)
19961997
.map(|i| {
19971998
let raw = unsafe { std::ptr::read(ctx_ref.indirectNoReturnCalls.add(i)) };
1998-
ArchAndAddr::from(raw)
1999+
Location::from(raw)
19992000
})
20002001
.collect::<HashSet<_>>();
20012002

20022003
let contextual_returns = (0..ctx_ref.contextualFunctionReturnCount)
20032004
.map(|i| {
20042005
let loc = unsafe {
20052006
let raw = std::ptr::read(ctx_ref.contextualFunctionReturnLocations.add(i));
2006-
ArchAndAddr::from(raw)
2007+
Location::from(raw)
20072008
};
20082009
let val = unsafe { *ctx_ref.contextualFunctionReturnValues.add(i) };
20092010
(loc, val)
@@ -2014,7 +2015,7 @@ impl BasicBlockAnalysisContext {
20142015
.map(|i| {
20152016
let src = unsafe {
20162017
let raw = std::ptr::read(ctx_ref.directRefSources.add(i));
2017-
ArchAndAddr::from(raw)
2018+
Location::from(raw)
20182019
};
20192020
let tgt = unsafe { *ctx_ref.directRefTargets.add(i) };
20202021
(tgt, src)
@@ -2024,14 +2025,14 @@ impl BasicBlockAnalysisContext {
20242025
let direct_no_return_calls = (0..ctx_ref.directNoReturnCallsCount)
20252026
.map(|i| {
20262027
let raw = unsafe { std::ptr::read(ctx_ref.directNoReturnCalls.add(i)) };
2027-
ArchAndAddr::from(raw)
2028+
Location::from(raw)
20282029
})
20292030
.collect::<HashSet<_>>();
20302031

20312032
let halted_disassembly_addresses = (0..ctx_ref.haltedDisassemblyAddressesCount)
20322033
.map(|i| {
20332034
let raw = unsafe { std::ptr::read(ctx_ref.haltedDisassemblyAddresses.add(i)) };
2034-
ArchAndAddr::from(raw)
2035+
Location::from(raw)
20352036
})
20362037
.collect::<HashSet<_>>();
20372038

@@ -2040,7 +2041,7 @@ impl BasicBlockAnalysisContext {
20402041
.map(|i| {
20412042
let raw =
20422043
unsafe { std::ptr::read(ctx_ref.inlinedUnresolvedIndirectBranches.add(i)) };
2043-
ArchAndAddr::from(raw)
2044+
Location::from(raw)
20442045
})
20452046
.collect::<HashSet<_>>();
20462047

@@ -2064,28 +2065,31 @@ impl BasicBlockAnalysisContext {
20642065
}
20652066
}
20662067

2067-
pub fn add_contextual_return(&mut self, loc: ArchAndAddr, value: bool) {
2068+
pub fn add_contextual_return(&mut self, loc: impl Into<Location>, value: bool) {
2069+
let loc = loc.into();
20682070
if !self.contextual_returns.contains_key(&loc) {
20692071
self.contextual_returns_dirty = true;
20702072
}
20712073

20722074
self.contextual_returns.insert(loc, value);
20732075
}
20742076

2075-
pub fn add_direct_code_reference(&mut self, target: u64, src: ArchAndAddr) {
2076-
self.direct_code_references.entry(target).or_insert(src);
2077+
pub fn add_direct_code_reference(&mut self, target: u64, src: impl Into<Location>) {
2078+
self.direct_code_references
2079+
.entry(target)
2080+
.or_insert(src.into());
20772081
}
20782082

2079-
pub fn add_direct_no_return_call(&mut self, loc: ArchAndAddr) {
2080-
self.direct_no_return_calls.insert(loc);
2083+
pub fn add_direct_no_return_call(&mut self, loc: impl Into<Location>) {
2084+
self.direct_no_return_calls.insert(loc.into());
20812085
}
20822086

2083-
pub fn add_halted_disassembly_address(&mut self, loc: ArchAndAddr) {
2084-
self.halted_disassembly_addresses.insert(loc);
2087+
pub fn add_halted_disassembly_address(&mut self, loc: impl Into<Location>) {
2088+
self.halted_disassembly_addresses.insert(loc.into());
20852089
}
20862090

2087-
pub fn add_inlined_unresolved_indirect_branch(&mut self, loc: ArchAndAddr) {
2088-
self.inlined_unresolved_indirect_branches.insert(loc);
2091+
pub fn add_inlined_unresolved_indirect_branch(&mut self, loc: impl Into<Location>) {
2092+
self.inlined_unresolved_indirect_branches.insert(loc.into());
20892093
}
20902094

20912095
pub fn create_basic_block(
@@ -2121,7 +2125,7 @@ impl BasicBlockAnalysisContext {
21212125
let mut sources: Vec<BNArchitectureAndAddress> = Vec::with_capacity(total);
21222126
let mut targets: Vec<u64> = Vec::with_capacity(total);
21232127
for (target, src) in &self.direct_code_references {
2124-
sources.push(src.into_raw());
2128+
sources.push(BNArchitectureAndAddress::from(src));
21252129
targets.push(*target);
21262130
}
21272131
unsafe {
@@ -2138,7 +2142,7 @@ impl BasicBlockAnalysisContext {
21382142
let total = self.direct_no_return_calls.len();
21392143
let mut locations: Vec<BNArchitectureAndAddress> = Vec::with_capacity(total);
21402144
for loc in &self.direct_no_return_calls {
2141-
locations.push(loc.into_raw());
2145+
locations.push(BNArchitectureAndAddress::from(loc));
21422146
}
21432147
unsafe {
21442148
BNAnalyzeBasicBlocksContextSetDirectNoReturnCalls(
@@ -2153,7 +2157,7 @@ impl BasicBlockAnalysisContext {
21532157
let total = self.halted_disassembly_addresses.len();
21542158
let mut locations: Vec<BNArchitectureAndAddress> = Vec::with_capacity(total);
21552159
for loc in &self.halted_disassembly_addresses {
2156-
locations.push(loc.into_raw());
2160+
locations.push(BNArchitectureAndAddress::from(loc));
21572161
}
21582162
unsafe {
21592163
BNAnalyzeBasicBlocksContextSetHaltedDisassemblyAddresses(
@@ -2168,7 +2172,7 @@ impl BasicBlockAnalysisContext {
21682172
let total = self.inlined_unresolved_indirect_branches.len();
21692173
let mut locations: Vec<BNArchitectureAndAddress> = Vec::with_capacity(total);
21702174
for loc in &self.inlined_unresolved_indirect_branches {
2171-
locations.push(loc.into_raw());
2175+
locations.push(BNArchitectureAndAddress::from(loc));
21722176
}
21732177
unsafe {
21742178
BNAnalyzeBasicBlocksContextSetInlinedUnresolvedIndirectBranches(
@@ -2188,7 +2192,7 @@ impl BasicBlockAnalysisContext {
21882192
let mut locations: Vec<BNArchitectureAndAddress> = Vec::with_capacity(total);
21892193
let mut values: Vec<bool> = Vec::with_capacity(total);
21902194
for (loc, value) in &self.contextual_returns {
2191-
locations.push(loc.into_raw());
2195+
locations.push(BNArchitectureAndAddress::from(loc));
21922196
values.push(*value);
21932197
}
21942198
unsafe {

rust/src/binary_view.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::external_library::{ExternalLibrary, ExternalLocation};
3838
use crate::file_accessor::{Accessor, FileAccessor};
3939
use crate::file_metadata::FileMetadata;
4040
use crate::flowgraph::FlowGraph;
41-
use crate::function::{ArchAndAddr, Function, FunctionViewType, NativeBlock};
41+
use crate::function::{Function, FunctionViewType, Location, NativeBlock};
4242
use crate::linear_view::{LinearDisassemblyLine, LinearViewCursor};
4343
use crate::metadata::Metadata;
4444
use crate::platform::Platform;
@@ -1445,29 +1445,23 @@ pub trait BinaryViewExt: BinaryViewBase {
14451445
}
14461446
}
14471447

1448+
// TODO: Should this instead be implemented on [`Function`] considering `src_func`? `Location` is local to the source function.
14481449
fn should_skip_target_analysis(
14491450
&self,
1450-
source: &ArchAndAddr,
1451-
srcfunc: &Function,
1452-
srcend: u64,
1453-
target: &ArchAndAddr,
1451+
src_loc: impl Into<Location>,
1452+
src_func: &Function,
1453+
src_end: u64,
1454+
target: impl Into<Location>,
14541455
) -> bool {
1455-
let mut srccopy = BNArchitectureAndAddress {
1456-
arch: source.arch.handle,
1457-
address: source.addr,
1458-
};
1459-
let mut targetcopy = BNArchitectureAndAddress {
1460-
arch: target.arch.handle,
1461-
address: target.addr,
1462-
};
1463-
1456+
let src_loc = src_loc.into();
1457+
let target = target.into();
14641458
unsafe {
14651459
BNShouldSkipTargetAnalysis(
14661460
self.as_ref().handle,
1467-
&mut srccopy,
1468-
srcfunc.handle,
1469-
srcend,
1470-
&mut targetcopy,
1461+
&mut src_loc.into(),
1462+
src_func.handle,
1463+
src_end,
1464+
&mut target.into(),
14711465
)
14721466
}
14731467
}

rust/src/function.rs

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,21 @@ impl Location {
7171
arch: Some(unsafe { CoreArchitecture::from_raw(arch) }),
7272
}
7373
}
74+
75+
pub fn new(arch: Option<CoreArchitecture>, addr: u64) -> Self {
76+
Self { arch, addr }
77+
}
7478
}
7579

7680
impl From<u64> for Location {
7781
fn from(addr: u64) -> Self {
78-
Location { arch: None, addr }
82+
Location::new(None, addr)
7983
}
8084
}
8185

8286
impl From<(CoreArchitecture, u64)> for Location {
8387
fn from(loc: (CoreArchitecture, u64)) -> Self {
84-
Location {
85-
arch: Some(loc.0),
86-
addr: loc.1,
87-
}
88+
Location::new(Some(loc.0), loc.1)
8889
}
8990
}
9091

@@ -2998,34 +2999,3 @@ unsafe impl CoreArrayProviderInner for Comment {
29982999
}
29993000
}
30003001
}
3001-
3002-
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
3003-
pub struct ArchAndAddr {
3004-
pub arch: CoreArchitecture,
3005-
pub addr: u64,
3006-
}
3007-
3008-
impl ArchAndAddr {
3009-
pub fn new(arch: CoreArchitecture, addr: u64) -> Self {
3010-
Self { arch, addr }
3011-
}
3012-
}
3013-
3014-
impl From<BNArchitectureAndAddress> for ArchAndAddr {
3015-
fn from(raw: BNArchitectureAndAddress) -> Self {
3016-
unsafe {
3017-
let arch = CoreArchitecture::from_raw(raw.arch);
3018-
let addr = raw.address;
3019-
ArchAndAddr { arch, addr }
3020-
}
3021-
}
3022-
}
3023-
3024-
impl ArchAndAddr {
3025-
pub fn into_raw(self) -> BNArchitectureAndAddress {
3026-
BNArchitectureAndAddress {
3027-
arch: self.arch.handle,
3028-
address: self.addr,
3029-
}
3030-
}
3031-
}

0 commit comments

Comments
 (0)