From c3fdda9727f5507818e3f55576ad32215a22b0f9 Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Wed, 30 Apr 2025 17:38:40 -0400 Subject: [Rust] Remove Architecture trait bound on LLIL related structures --- rust/src/low_level_il/function.rs | 86 +++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 49 deletions(-) (limited to 'rust/src/low_level_il/function.rs') diff --git a/rust/src/low_level_il/function.rs b/rust/src/low_level_il/function.rs index 301c3f09..21eebc3c 100644 --- a/rust/src/low_level_il/function.rs +++ b/rust/src/low_level_il/function.rs @@ -17,7 +17,6 @@ use binaryninjacore_sys::BNGetLowLevelILOwnerFunction; use binaryninjacore_sys::BNLowLevelILFunction; use binaryninjacore_sys::BNNewLowLevelILFunctionReference; -use std::borrow::Borrow; use std::fmt::Debug; use std::hash::{Hash, Hasher}; use std::marker::PhantomData; @@ -57,51 +56,56 @@ pub trait FunctionForm: 'static + Debug {} impl FunctionForm for SSA {} impl FunctionForm for NonSSA {} -pub struct LowLevelILFunction { - pub(crate) arch_handle: A::Handle, +pub struct LowLevelILFunction { pub(crate) handle: *mut BNLowLevelILFunction, - _arch: PhantomData<*mut A>, + arch: Option, _mutability: PhantomData, _form: PhantomData, } -impl LowLevelILFunction +impl LowLevelILFunction where - A: Architecture, M: FunctionMutability, F: FunctionForm, { - pub(crate) unsafe fn from_raw( - arch_handle: A::Handle, + pub(crate) unsafe fn from_raw_with_arch( handle: *mut BNLowLevelILFunction, + arch: Option, ) -> Self { debug_assert!(!handle.is_null()); Self { - arch_handle, handle, - _arch: PhantomData, + arch, _mutability: PhantomData, _form: PhantomData, } } - pub(crate) unsafe fn ref_from_raw( - arch_handle: A::Handle, + pub(crate) unsafe fn from_raw(handle: *mut BNLowLevelILFunction) -> Self { + Self::from_raw_with_arch(handle, None) + } + + pub(crate) unsafe fn ref_from_raw_with_arch( handle: *mut BNLowLevelILFunction, + arch: Option, ) -> Ref { debug_assert!(!handle.is_null()); - Ref::new(Self::from_raw(arch_handle, handle)) + Ref::new(Self::from_raw_with_arch(handle, arch)) } - pub(crate) fn arch(&self) -> &A { - self.arch_handle.borrow() + pub(crate) unsafe fn ref_from_raw(handle: *mut BNLowLevelILFunction) -> Ref { + Self::ref_from_raw_with_arch(handle, None) } - pub fn instruction_at>( - &self, - loc: L, - ) -> Option> { + pub(crate) fn arch(&self) -> CoreArchitecture { + match self.arch { + None => self.function().arch(), + Some(arch) => arch, + } + } + + pub fn instruction_at>(&self, loc: L) -> Option> { Some(LowLevelILInstruction::new( self, self.instruction_index_at(loc)?, @@ -128,7 +132,7 @@ where pub fn instruction_from_index( &self, index: LowLevelInstructionIndex, - ) -> Option> { + ) -> Option> { if index.0 >= self.instruction_count() { None } else { @@ -161,12 +165,8 @@ where // LLIL basic blocks are not available until the function object // is finalized, so ensure we can't try requesting basic blocks // during lifting -impl LowLevelILFunction -where - A: Architecture, - F: FunctionForm, -{ - pub fn basic_blocks(&self) -> Array>> { +impl LowLevelILFunction { + pub fn basic_blocks(&self) -> Array>> { use binaryninjacore_sys::BNGetLowLevelILBasicBlockList; unsafe { @@ -179,7 +179,7 @@ where } // Allow instantiating Lifted IL functions for querying Lifted IL from Architectures -impl LowLevelILFunction> { +impl LowLevelILFunction> { // TODO: Document what happens when you pass None for `source_func`. // TODO: Doing so would construct a LowLevelILFunction with no basic blocks // TODO: Document why you would want to do that. @@ -196,7 +196,7 @@ impl LowLevelILFunction> { // BNCreateLowLevelILFunction should always return a valid object. assert!(!handle.is_null()); - unsafe { Self::ref_from_raw(arch, handle) } + unsafe { Self::ref_from_raw_with_arch(handle, Some(arch)) } } pub fn generate_ssa_form(&self) { @@ -206,9 +206,8 @@ impl LowLevelILFunction> { } } -impl ToOwned for LowLevelILFunction +impl ToOwned for LowLevelILFunction where - A: Architecture, M: FunctionMutability, F: FunctionForm, { @@ -219,17 +218,15 @@ where } } -unsafe impl RefCountable for LowLevelILFunction +unsafe impl RefCountable for LowLevelILFunction where - A: Architecture, M: FunctionMutability, F: FunctionForm, { unsafe fn inc_ref(handle: &Self) -> Ref { Ref::new(Self { - arch_handle: handle.arch_handle.clone(), handle: BNNewLowLevelILFunctionReference(handle.handle), - _arch: PhantomData, + arch: handle.arch, _mutability: PhantomData, _form: PhantomData, }) @@ -240,9 +237,8 @@ where } } -impl Debug for LowLevelILFunction +impl Debug for LowLevelILFunction where - A: Architecture + Debug, M: FunctionMutability, F: FunctionForm, { @@ -255,26 +251,18 @@ where } } -unsafe impl Send - for LowLevelILFunction -{ -} -unsafe impl Sync - for LowLevelILFunction -{ -} +unsafe impl Send for LowLevelILFunction {} +unsafe impl Sync for LowLevelILFunction {} -impl Eq for LowLevelILFunction {} +impl Eq for LowLevelILFunction {} -impl PartialEq - for LowLevelILFunction -{ +impl PartialEq for LowLevelILFunction { fn eq(&self, rhs: &Self) -> bool { self.function().eq(&rhs.function()) } } -impl Hash for LowLevelILFunction { +impl Hash for LowLevelILFunction { fn hash(&self, state: &mut H) { self.function().hash(state) } -- cgit v1.3.1