diff options
| author | Rubens Brandao <git@rubens.io> | 2024-04-17 08:14:00 -0300 |
|---|---|---|
| committer | Rubens Brandao <git@rubens.io> | 2024-04-17 08:14:00 -0300 |
| commit | 58673b20ef858ae5d4d7b1ff0dec1460a0f36e5f (patch) | |
| tree | bdfd5dff219829438d2069fc433cae280002c1c6 | |
| parent | 90040508478f4551eb303a5056026fd59bb0cdc6 (diff) | |
use Ref to own types
| -rw-r--r-- | rust/src/architecture.rs | 10 | ||||
| -rw-r--r-- | rust/src/binaryview.rs | 4 | ||||
| -rw-r--r-- | rust/src/debuginfo.rs | 8 | ||||
| -rw-r--r-- | rust/src/types.rs | 63 |
4 files changed, 63 insertions, 22 deletions
diff --git a/rust/src/architecture.rs b/rust/src/architecture.rs index 82df7441..9b5b36b4 100644 --- a/rust/src/architecture.rs +++ b/rust/src/architecture.rs @@ -313,7 +313,7 @@ pub trait Intrinsic: Sized + Clone + Copy { fn id(&self) -> u32; /// Reeturns the list of the input names and types for this intrinsic. - fn inputs(&self) -> Vec<NameAndType>; + fn inputs(&self) -> Vec<Ref<NameAndType>>; /// Returns the list of the output types for this intrinsic. fn outputs(&self) -> Vec<Conf<Ref<Type>>>; @@ -650,7 +650,7 @@ impl Intrinsic for UnusedIntrinsic { fn id(&self) -> u32 { unreachable!() } - fn inputs(&self) -> Vec<NameAndType> { + fn inputs(&self) -> Vec<Ref<NameAndType>> { unreachable!() } fn outputs(&self) -> Vec<Conf<Ref<Type>>> { @@ -992,7 +992,7 @@ impl Intrinsic for crate::architecture::CoreIntrinsic { self.1 } - fn inputs(&self) -> Vec<NameAndType> { + fn inputs(&self) -> Vec<Ref<NameAndType>> { let mut count: usize = 0; unsafe { @@ -1172,7 +1172,7 @@ impl Architecture for CoreArchitecture { } } } - + fn instruction_llil( &self, data: &[u8], @@ -2424,7 +2424,7 @@ where let inputs = intrinsic.inputs(); let mut res = Vec::with_capacity(inputs.len()); for input in inputs { - res.push(input.into_raw()); + res.push(unsafe { Ref::into_raw(input) }.into_raw()); } unsafe { diff --git a/rust/src/binaryview.rs b/rust/src/binaryview.rs index bcb3f57f..ecf75384 100644 --- a/rust/src/binaryview.rs +++ b/rust/src/binaryview.rs @@ -574,7 +574,7 @@ pub trait BinaryViewExt: BinaryViewBase { } } - fn define_auto_data_var(&self, dv: DataVariable) { + fn define_auto_data_var(&self, dv: Ref<DataVariable>) { unsafe { BNDefineDataVariable( self.as_ref().handle, @@ -585,7 +585,7 @@ pub trait BinaryViewExt: BinaryViewBase { } /// You likely would also like to call [`Self::define_user_symbol`] to bind this data variable with a name - fn define_user_data_var(&self, dv: DataVariable) { + fn define_user_data_var(&self, dv: Ref<DataVariable>) { unsafe { BNDefineUserDataVariable( self.as_ref().handle, diff --git a/rust/src/debuginfo.rs b/rust/src/debuginfo.rs index 7bb5e36f..32db7ebb 100644 --- a/rust/src/debuginfo.rs +++ b/rust/src/debuginfo.rs @@ -376,7 +376,7 @@ impl DebugInfo { } /// Returns a generator of all types provided by a named DebugInfoParser - pub fn types_by_name<S: BnStrCompatible>(&self, parser_name: S) -> Vec<NameAndType> { + pub fn types_by_name<S: BnStrCompatible>(&self, parser_name: S) -> Vec<Ref<NameAndType>> { let parser_name = parser_name.into_bytes_with_nul(); let mut count: usize = 0; @@ -387,7 +387,7 @@ impl DebugInfo { &mut count, ) }; - let result: Vec<NameAndType> = unsafe { + let result: Vec<Ref<NameAndType>> = unsafe { slice::from_raw_parts_mut(debug_types_ptr, count) .iter() .map(NameAndType::from_raw) @@ -399,10 +399,10 @@ impl DebugInfo { } /// A generator of all types provided by DebugInfoParsers - pub fn types(&self) -> Vec<NameAndType> { + pub fn types(&self) -> Vec<Ref<NameAndType>> { let mut count: usize = 0; let debug_types_ptr = unsafe { BNGetDebugTypes(self.handle, ptr::null_mut(), &mut count) }; - let result: Vec<NameAndType> = unsafe { + let result: Vec<Ref<NameAndType>> = unsafe { slice::from_raw_parts_mut(debug_types_ptr, count) .iter() .map(NameAndType::from_raw) diff --git a/rust/src/types.rs b/rust/src/types.rs index eb629503..05cad7e6 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -2454,7 +2454,7 @@ unsafe impl<'a> CoreArrayWrapper<'a> for QualifiedNameTypeAndId { pub struct NameAndType(pub(crate) BNNameAndType); impl NameAndType { - pub(crate) fn from_raw(raw: &BNNameAndType) -> Self { + pub(crate) fn from_raw(raw: &BNNameAndType) -> Ref<Self> { Self::new( raw_to_string(raw.name).unwrap(), unsafe { &Type::ref_from_raw(raw.type_) }, @@ -2464,12 +2464,14 @@ impl NameAndType { } impl NameAndType { - pub fn new<S: BnStrCompatible>(name: S, t: &Ref<Type>, confidence: u8) -> Self { - Self(BNNameAndType { - name: unsafe { BNAllocString(name.into_bytes_with_nul().as_ref().as_ptr() as *mut _) }, - type_: unsafe { Ref::into_raw(t.to_owned()).handle }, - typeConfidence: confidence, - }) + pub fn new<S: BnStrCompatible>(name: S, t: &Type, confidence: u8) -> Ref<Self> { + unsafe { + Ref::new(Self(BNNameAndType { + name: BNAllocString(name.into_bytes_with_nul().as_ref().as_ptr() as *mut _), + type_: Ref::into_raw(t.to_owned()).handle, + typeConfidence: confidence, + })) + } } pub(crate) fn into_raw(self) -> BNNameAndType { @@ -2491,11 +2493,27 @@ impl NameAndType { } } -impl Drop for NameAndType { - fn drop(&mut self) { +impl ToOwned for NameAndType { + type Owned = Ref<Self>; + + fn to_owned(&self) -> Self::Owned { + unsafe { RefCountable::inc_ref(self) } + } +} + +unsafe impl RefCountable for NameAndType { + unsafe fn inc_ref(handle: &Self) -> Ref<Self> { + Self::new( + CStr::from_ptr(handle.0.name), + handle.t(), + handle.type_with_confidence().confidence, + ) + } + + unsafe fn dec_ref(handle: &Self) { unsafe { - BNFreeString(self.0.name); - BNFreeType(self.0.type_); + BNFreeString(handle.0.name); + RefCountable::dec_ref(handle.t()); } } } @@ -2559,6 +2577,29 @@ impl DataVariable { } } +impl ToOwned for DataVariable { + type Owned = Ref<Self>; + + fn to_owned(&self) -> Self::Owned { + unsafe { RefCountable::inc_ref(self) } + } +} + +unsafe impl RefCountable for DataVariable { + unsafe fn inc_ref(handle: &Self) -> Ref<Self> { + unsafe { + Ref::new(Self(BNDataVariable { + type_: Ref::into_raw(handle.t().to_owned()).handle, + ..handle.0 + })) + } + } + + unsafe fn dec_ref(handle: &Self) { + unsafe { BNFreeType(handle.0.type_) } + } +} + impl CoreArrayProvider for DataVariable { type Raw = BNDataVariable; type Context = (); |
