From 37b51462dcf26dc40ac5d7e82acdfd4cbe754157 Mon Sep 17 00:00:00 2001 From: Rubens Brandao Date: Wed, 10 Apr 2024 17:58:05 -0300 Subject: fix the array implementation using GAT --- rust/src/types.rs | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'rust/src/types.rs') diff --git a/rust/src/types.rs b/rust/src/types.rs index 2a2a50d4..43d48b3f 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -1453,10 +1453,10 @@ unsafe impl CoreOwnedArrayProvider for NamedTypedVariable { } } -unsafe impl<'a> CoreArrayWrapper<'a> for NamedTypedVariable { - type Wrapped = ManuallyDrop; +unsafe impl CoreArrayWrapper for NamedTypedVariable { + type Wrapped<'a> = ManuallyDrop; - unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped { + unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> { ManuallyDrop::new(NamedTypedVariable { var: raw.var, ty: raw.type_, @@ -2348,10 +2348,10 @@ unsafe impl CoreOwnedArrayProvider for QualifiedName { } } -unsafe impl<'a> CoreArrayWrapper<'a> for QualifiedName { - type Wrapped = &'a QualifiedName; +unsafe impl CoreArrayWrapper for QualifiedName { + type Wrapped<'a> = &'a QualifiedName; - unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped { + unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> { mem::transmute(raw) } } @@ -2390,10 +2390,10 @@ unsafe impl CoreOwnedArrayProvider for QualifiedNameAndType { } } -unsafe impl<'a> CoreArrayWrapper<'a> for QualifiedNameAndType { - type Wrapped = &'a QualifiedNameAndType; +unsafe impl CoreArrayWrapper for QualifiedNameAndType { + type Wrapped<'a> = &'a QualifiedNameAndType; - unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped { + unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> { mem::transmute(raw) } } @@ -2436,10 +2436,10 @@ unsafe impl CoreOwnedArrayProvider for QualifiedNameTypeAndId { } } -unsafe impl<'a> CoreArrayWrapper<'a> for QualifiedNameTypeAndId { - type Wrapped = &'a QualifiedNameTypeAndId; +unsafe impl CoreArrayWrapper for QualifiedNameTypeAndId { + type Wrapped<'a> = &'a QualifiedNameTypeAndId; - unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped { + unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> { mem::transmute(raw) } } @@ -2497,10 +2497,11 @@ unsafe impl CoreOwnedArrayProvider for NameAndType { } } -unsafe impl<'a, S: 'a + BnStrCompatible> CoreArrayWrapper<'a> for NameAndType { - type Wrapped = &'a NameAndType; +unsafe impl CoreArrayWrapper for NameAndType { + type Wrapped<'a> = &'a NameAndType where S: 'a; - unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped { + unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> { + // TODO this is not always valid, because the type is not transparent mem::transmute(raw) } } @@ -2544,10 +2545,11 @@ unsafe impl CoreOwnedArrayProvider for DataVariable { } } -unsafe impl<'a> CoreArrayWrapper<'a> for DataVariable { - type Wrapped = &'a DataVariable; +unsafe impl CoreArrayWrapper for DataVariable { + type Wrapped<'a> = &'a DataVariable; - unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped { + unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> { + // TODO this is not always valid, because the type is not transparent mem::transmute(raw) } } -- cgit v1.3.1 From f3c1a3cb3cbdd25d5f556d1ab770f8cbc29fd567 Mon Sep 17 00:00:00 2001 From: Rubens Brandao Date: Tue, 16 Apr 2024 17:03:20 -0300 Subject: add guard to array with unbound return types --- rust/src/custombinaryview.rs | 6 ++---- rust/src/downloadprovider.rs | 8 +++----- rust/src/references.rs | 8 +++----- rust/src/relocation.rs | 7 +++---- rust/src/types.rs | 2 -- 5 files changed, 11 insertions(+), 20 deletions(-) (limited to 'rust/src/types.rs') diff --git a/rust/src/custombinaryview.rs b/rust/src/custombinaryview.rs index c69bad39..898102d6 100644 --- a/rust/src/custombinaryview.rs +++ b/rust/src/custombinaryview.rs @@ -298,12 +298,10 @@ unsafe impl CoreOwnedArrayProvider for BinaryViewType { } unsafe impl CoreArrayWrapper for BinaryViewType { - // TODO there is nothing blocking the returned value from out-living the - // array, change it to &_ or Guard? - type Wrapped<'a> = BinaryViewType; + type Wrapped<'a> = Guard<'a, BinaryViewType>; unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> { - BinaryViewType(*raw) + Guard::new(BinaryViewType(*raw), &()) } } diff --git a/rust/src/downloadprovider.rs b/rust/src/downloadprovider.rs index cc243722..8334e0ce 100644 --- a/rust/src/downloadprovider.rs +++ b/rust/src/downloadprovider.rs @@ -1,5 +1,5 @@ use crate::rc::{ - Array, CoreArrayProvider, CoreArrayWrapper, CoreOwnedArrayProvider, Ref, RefCountable, + Array, CoreArrayProvider, CoreArrayWrapper, CoreOwnedArrayProvider, Guard, Ref, RefCountable, }; use crate::settings::Settings; use crate::string::{BnStrCompatible, BnString}; @@ -72,12 +72,10 @@ unsafe impl CoreOwnedArrayProvider for DownloadProvider { } unsafe impl CoreArrayWrapper for DownloadProvider { - // TODO there is nothing blocking the returned value from out-living the - // array, change it to &_ or Guard? - type Wrapped<'a> = DownloadProvider; + type Wrapped<'a> = Guard<'a, DownloadProvider>; unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> { - DownloadProvider::from_raw(*raw) + Guard::new(DownloadProvider::from_raw(*raw), &()) } } diff --git a/rust/src/references.rs b/rust/src/references.rs index a30f3f5e..8b4ebb87 100644 --- a/rust/src/references.rs +++ b/rust/src/references.rs @@ -1,6 +1,6 @@ use crate::architecture::CoreArchitecture; use crate::function::Function; -use crate::rc::{CoreArrayProvider, CoreArrayWrapper, CoreOwnedArrayProvider, Ref}; +use crate::rc::{CoreArrayProvider, CoreArrayWrapper, CoreOwnedArrayProvider, Guard, Ref}; use binaryninjacore_sys::{BNFreeCodeReferences, BNFreeDataReferences, BNReferenceSource}; use std::mem::ManuallyDrop; @@ -65,12 +65,10 @@ unsafe impl CoreOwnedArrayProvider for CodeReference { } unsafe impl CoreArrayWrapper for CodeReference { - // TODO there is nothing blocking the returned value from out-living the - // array, change it to Guard? - type Wrapped<'a> = CodeReference; + type Wrapped<'a> = Guard<'a, CodeReference>; unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> { - CodeReference::new(raw) + Guard::new(CodeReference::new(raw), &()) } } diff --git a/rust/src/relocation.rs b/rust/src/relocation.rs index 47ec2ac0..455f5a5a 100644 --- a/rust/src/relocation.rs +++ b/rust/src/relocation.rs @@ -1,3 +1,4 @@ +use crate::rc::Guard; use crate::string::BnStrCompatible; use crate::{ architecture::{Architecture, CoreArchitecture}, @@ -228,11 +229,9 @@ unsafe impl CoreOwnedArrayProvider for Relocation { } unsafe impl CoreArrayWrapper for Relocation { - // TODO there is nothing blocking the returned value from out-living the - // array, change it to &_ or Guard? - type Wrapped<'a> = Relocation; + type Wrapped<'a> = Guard<'a, Relocation>; unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> { - Relocation(*raw) + Guard::new(Relocation(*raw), &()) } } diff --git a/rust/src/types.rs b/rust/src/types.rs index 43d48b3f..280d50fd 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -2501,7 +2501,6 @@ unsafe impl CoreArrayWrapper for NameAndType { type Wrapped<'a> = &'a NameAndType where S: 'a; unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> { - // TODO this is not always valid, because the type is not transparent mem::transmute(raw) } } @@ -2549,7 +2548,6 @@ unsafe impl CoreArrayWrapper for DataVariable { type Wrapped<'a> = &'a DataVariable; unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> { - // TODO this is not always valid, because the type is not transparent mem::transmute(raw) } } -- cgit v1.3.1