Skip to content

Commit 757629a

Browse files
committed
[Rust] WIP: Misc cleanup surrounding custom basic block analysis
1 parent 3c47579 commit 757629a

File tree

10 files changed

+570
-518
lines changed

10 files changed

+570
-518
lines changed

arch/riscv/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ use binaryninja::relocation::{Relocation, RelocationHandlerExt};
99
use binaryninja::{
1010
add_optional_plugin_dependency, architecture,
1111
architecture::{
12-
llvm_assemble, Architecture, ArchitectureExt, CoreArchitecture, CustomArchitectureHandle,
13-
ImplicitRegisterExtend, InstructionInfo, LlvmServicesCodeModel, LlvmServicesDialect,
14-
LlvmServicesRelocMode, Register as Reg, RegisterInfo, UnusedFlag, UnusedRegisterStack,
15-
UnusedRegisterStackInfo,
12+
Architecture, ArchitectureExt, CoreArchitecture, CustomArchitectureHandle,
13+
ImplicitRegisterExtend, InstructionInfo, Register as Reg, RegisterInfo, UnusedFlag,
14+
UnusedRegisterStack, UnusedRegisterStackInfo,
1615
},
1716
binary_view::{BinaryView, BinaryViewExt},
1817
calling_convention::{register_calling_convention, CallingConvention, ConventionBuilder},
1918
custom_binary_view::{BinaryViewType, BinaryViewTypeExt},
2019
disassembly::{InstructionTextToken, InstructionTextTokenKind},
2120
function::Function,
2221
function_recognizer::FunctionRecognizer,
22+
llvm::{llvm_assemble, LlvmServicesCodeModel, LlvmServicesDialect, LlvmServicesRelocMode},
2323
rc::Ref,
2424
relocation::{
2525
CoreRelocationHandler, CustomRelocationHandlerHandle, RelocationHandler, RelocationInfo,

rust/src/architecture.rs

Lines changed: 7 additions & 224 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@
1616
1717
use std::ops::Deref;
1818
use std::{
19-
borrow::{Borrow, Cow},
20-
collections::HashMap,
21-
ffi::{c_char, c_int, c_void, CStr, CString},
22-
fmt::{Debug, Display, Formatter},
19+
borrow::Borrow,
20+
ffi::{c_char, c_void, CString},
21+
fmt::{Debug, Formatter},
2322
hash::Hash,
2423
mem::MaybeUninit,
2524
};
@@ -38,29 +37,29 @@ use crate::{
3837
calling_convention::CoreCallingConvention,
3938
data_buffer::DataBuffer,
4039
disassembly::InstructionTextToken,
41-
function::{ArchAndAddr, Function, NativeBlock},
40+
function::Function,
4241
platform::Platform,
4342
rc::*,
4443
relocation::CoreRelocationHandler,
4544
string::{IntoCStr, *},
4645
types::{NameAndType, Type},
47-
BranchType, Endianness,
46+
Endianness,
4847
};
4948

5049
pub mod basic_block;
50+
pub mod branches;
5151
pub mod flag;
5252
pub mod instruction;
5353
pub mod intrinsic;
54-
pub mod llvm;
5554
pub mod register;
5655

5756
// Re-export all the submodules to keep from breaking everyone's code.
5857
// We split these out just to clarify each part, not necessarily to enforce an extra namespace.
5958
pub use basic_block::*;
59+
pub use branches::*;
6060
pub use flag::*;
6161
pub use instruction::*;
6262
pub use intrinsic::*;
63-
pub use llvm::*;
6463
pub use register::*;
6564

6665
#[macro_export]
@@ -861,222 +860,6 @@ impl Architecture for CoreArchitecture {
861860
}
862861
}
863862

864-
pub struct BasicBlockAnalysisContext {
865-
pub(crate) handle: *mut BNBasicBlockAnalysisContext,
866-
contextual_returns_dirty: bool,
867-
868-
// In
869-
pub indirect_branches: Vec<IndirectBranchInfo>,
870-
pub indirect_no_return_calls: HashSet<ArchAndAddr>,
871-
pub analysis_skip_override: BNFunctionAnalysisSkipOverride,
872-
pub translate_tail_calls: bool,
873-
pub disallow_branch_to_string: bool,
874-
pub max_function_size: u64,
875-
pub halt_on_invalid_instruction: bool,
876-
pub max_size_reached: bool,
877-
878-
// In/Out
879-
contextual_returns: HashMap<ArchAndAddr, bool>,
880-
881-
// Out
882-
direct_code_references: HashMap<u64, ArchAndAddr>,
883-
direct_no_return_calls: HashSet<ArchAndAddr>,
884-
halted_disassembly_addresses: HashSet<ArchAndAddr>,
885-
}
886-
887-
impl BasicBlockAnalysisContext {
888-
pub unsafe fn from_raw(handle: *mut BNBasicBlockAnalysisContext) -> Self {
889-
debug_assert!(!handle.is_null());
890-
891-
let ctx_ref = &*handle;
892-
893-
let indirect_branches = (0..ctx_ref.indirectBranchesCount)
894-
.map(|i| {
895-
let raw: BNIndirectBranchInfo =
896-
unsafe { std::ptr::read(ctx_ref.indirectBranches.add(i)) };
897-
IndirectBranchInfo::from(raw)
898-
})
899-
.collect::<Vec<_>>();
900-
901-
let indirect_no_return_calls = (0..ctx_ref.indirectNoReturnCallsCount)
902-
.map(|i| {
903-
let raw = unsafe { std::ptr::read(ctx_ref.indirectNoReturnCalls.add(i)) };
904-
ArchAndAddr::from(raw)
905-
})
906-
.collect::<HashSet<_>>();
907-
908-
let contextual_returns = (0..ctx_ref.contextualFunctionReturnCount)
909-
.map(|i| {
910-
let loc = unsafe {
911-
let raw = std::ptr::read(ctx_ref.contextualFunctionReturnLocations.add(i));
912-
ArchAndAddr::from(raw)
913-
};
914-
let val = unsafe { *ctx_ref.contextualFunctionReturnValues.add(i) };
915-
(loc, val)
916-
})
917-
.collect::<HashMap<_, _>>();
918-
919-
let direct_code_references = (0..ctx_ref.directRefCount)
920-
.map(|i| {
921-
let src = unsafe {
922-
let raw = std::ptr::read(ctx_ref.directRefSources.add(i));
923-
ArchAndAddr::from(raw)
924-
};
925-
let tgt = unsafe { *ctx_ref.directRefTargets.add(i) };
926-
(tgt, src)
927-
})
928-
.collect::<HashMap<_, _>>();
929-
930-
let direct_no_return_calls = (0..ctx_ref.directNoReturnCallsCount)
931-
.map(|i| {
932-
let raw = unsafe { std::ptr::read(ctx_ref.directNoReturnCalls.add(i)) };
933-
ArchAndAddr::from(raw)
934-
})
935-
.collect::<HashSet<_>>();
936-
937-
let halted_disassembly_addresses = (0..ctx_ref.haltedDisassemblyAddressesCount)
938-
.map(|i| {
939-
let raw = unsafe { std::ptr::read(ctx_ref.haltedDisassemblyAddresses.add(i)) };
940-
ArchAndAddr::from(raw)
941-
})
942-
.collect::<HashSet<_>>();
943-
944-
BasicBlockAnalysisContext {
945-
handle,
946-
contextual_returns_dirty: false,
947-
indirect_branches,
948-
indirect_no_return_calls,
949-
analysis_skip_override: ctx_ref.analysisSkipOverride,
950-
translate_tail_calls: ctx_ref.translateTailCalls,
951-
disallow_branch_to_string: ctx_ref.disallowBranchToString,
952-
max_function_size: ctx_ref.maxFunctionSize,
953-
halt_on_invalid_instruction: ctx_ref.haltOnInvalidInstructions,
954-
max_size_reached: ctx_ref.maxSizeReached,
955-
contextual_returns,
956-
direct_code_references,
957-
direct_no_return_calls,
958-
halted_disassembly_addresses,
959-
}
960-
}
961-
962-
pub fn add_contextual_return(&mut self, loc: ArchAndAddr, value: bool) {
963-
if !self.contextual_returns.contains_key(&loc) {
964-
self.contextual_returns_dirty = true;
965-
}
966-
967-
self.contextual_returns.insert(loc, value);
968-
}
969-
970-
pub fn add_direct_code_reference(&mut self, target: u64, src: ArchAndAddr) {
971-
self.direct_code_references.entry(target).or_insert(src);
972-
}
973-
974-
pub fn add_direct_no_return_call(&mut self, loc: ArchAndAddr) {
975-
self.direct_no_return_calls.insert(loc);
976-
}
977-
978-
pub fn add_halted_disassembly_address(&mut self, loc: ArchAndAddr) {
979-
self.halted_disassembly_addresses.insert(loc);
980-
}
981-
982-
pub fn create_basic_block(
983-
&self,
984-
arch: CoreArchitecture,
985-
start: u64,
986-
) -> Option<Ref<BasicBlock<NativeBlock>>> {
987-
let raw_block =
988-
unsafe { BNAnalyzeBasicBlocksContextCreateBasicBlock(self.handle, arch.handle, start) };
989-
990-
if raw_block.is_null() {
991-
return None;
992-
}
993-
994-
unsafe { Some(BasicBlock::ref_from_raw(raw_block, NativeBlock::new())) }
995-
}
996-
997-
pub fn add_basic_block(&self, block: Ref<BasicBlock<NativeBlock>>) {
998-
unsafe {
999-
BNAnalyzeBasicBlocksContextAddBasicBlockToFunction(self.handle, block.handle);
1000-
}
1001-
}
1002-
1003-
pub fn add_temp_outgoing_reference(&self, target: &Function) {
1004-
unsafe {
1005-
BNAnalyzeBasicBlocksContextAddTempReference(self.handle, target.handle);
1006-
}
1007-
}
1008-
1009-
pub fn finalize(&mut self) {
1010-
if !self.direct_code_references.is_empty() {
1011-
let total = self.direct_code_references.len();
1012-
let mut sources: Vec<BNArchitectureAndAddress> = Vec::with_capacity(total);
1013-
let mut targets: Vec<u64> = Vec::with_capacity(total);
1014-
for (target, src) in &self.direct_code_references {
1015-
sources.push(src.into_raw());
1016-
targets.push(*target);
1017-
}
1018-
unsafe {
1019-
BNAnalyzeBasicBlocksContextSetDirectCodeReferences(
1020-
self.handle,
1021-
sources.as_mut_ptr(),
1022-
targets.as_mut_ptr(),
1023-
total,
1024-
);
1025-
}
1026-
}
1027-
1028-
if !self.direct_no_return_calls.is_empty() {
1029-
let total = self.direct_no_return_calls.len();
1030-
let mut locations: Vec<BNArchitectureAndAddress> = Vec::with_capacity(total);
1031-
for loc in &self.direct_no_return_calls {
1032-
locations.push(loc.into_raw());
1033-
}
1034-
unsafe {
1035-
BNAnalyzeBasicBlocksContextSetDirectNoReturnCalls(
1036-
self.handle,
1037-
locations.as_mut_ptr(),
1038-
total,
1039-
);
1040-
}
1041-
}
1042-
1043-
if !self.halted_disassembly_addresses.is_empty() {
1044-
let total = self.halted_disassembly_addresses.len();
1045-
let mut locations: Vec<BNArchitectureAndAddress> = Vec::with_capacity(total);
1046-
for loc in &self.halted_disassembly_addresses {
1047-
locations.push(loc.into_raw());
1048-
}
1049-
unsafe {
1050-
BNAnalyzeBasicBlocksContextSetHaltedDisassemblyAddresses(
1051-
self.handle,
1052-
locations.as_mut_ptr(),
1053-
total,
1054-
);
1055-
}
1056-
}
1057-
1058-
if self.contextual_returns_dirty {
1059-
let total = self.contextual_returns.len();
1060-
let mut locations: Vec<BNArchitectureAndAddress> = Vec::with_capacity(total);
1061-
let mut values: Vec<bool> = Vec::with_capacity(total);
1062-
for (loc, value) in &self.contextual_returns {
1063-
locations.push(loc.into_raw());
1064-
values.push(*value);
1065-
}
1066-
unsafe {
1067-
BNAnalyzeBasicBlocksContextSetContextualFunctionReturns(
1068-
self.handle,
1069-
locations.as_mut_ptr(),
1070-
values.as_mut_ptr(),
1071-
total,
1072-
);
1073-
}
1074-
}
1075-
1076-
unsafe { BNAnalyzeBasicBlocksContextFinalize(self.handle) };
1077-
}
1078-
}
1079-
1080863
impl Debug for CoreArchitecture {
1081864
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
1082865
f.debug_struct("CoreArchitecture")

0 commit comments

Comments
 (0)