Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl rust TypeFieldReference and related #5524

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,23 @@ impl ToOwned for Type {
}
}

// NOTE ConfTypeList never exists by itself, it's always part of other struct,
// like TypeFieldReferenceTypeInfo
pub struct ConfTypeList<'a>(core::marker::PhantomData<&'a ()>);
impl<'a> CoreArrayProvider for ConfTypeList<'a> {
type Raw = BNTypeWithConfidence;
type Context = core::marker::PhantomData<&'a ()>;
type Wrapped<'b> = Conf<Ref<Type>> where 'a: 'b;
}

unsafe impl CoreArrayProviderInner for ConfTypeList<'_> {
unsafe fn free(_raw: *mut Self::Raw, _count: usize, _context: &Self::Context) {}

unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
Conf::new(Type::from_raw(raw.type_).to_owned(), raw.confidence)
}
}

///////////////////////
// FunctionParameter

Expand Down Expand Up @@ -3720,3 +3737,122 @@ unsafe impl CoreArrayProviderInner for UnresolvedIndirectBranches {
Self(*raw)
}
}

/////////////////////////
// TypeFieldReference

#[derive(Debug, Clone)]
pub struct TypeFieldReference {
pub func: Ref<Function>,
pub arch: CoreArchitecture,
pub addr: u64,
pub size: usize,
pub type_: Conf<Ref<Type>>,
}

impl TypeFieldReference {
pub fn from_raw(value: BNTypeFieldReference) -> Self {
Self {
func: unsafe { Function::from_raw(value.func) },
arch: CoreArchitecture(value.arch),
addr: value.addr,
size: value.size,
type_: Conf::new(
unsafe { Type::ref_from_raw(value.incomingType.type_) },
value.incomingType.confidence,
),
}
}
}

impl CoreArrayProvider for TypeFieldReference {
type Raw = BNTypeFieldReference;
type Context = ();
type Wrapped<'a> = ManuallyDrop<Self>;
}

unsafe impl CoreArrayProviderInner for TypeFieldReference {
unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
BNFreeTypeFieldReferences(raw, count)
}

unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
ManuallyDrop::new(TypeFieldReference::from_raw(*raw))
}
}

/////////////////////////
// TypeFieldReferenceSizeInfo

#[repr(transparent)]
pub struct TypeFieldReferenceSizeInfo(BNTypeFieldReferenceSizeInfo);

impl TypeFieldReferenceSizeInfo {
pub(crate) unsafe fn ref_from_raw(value: &BNTypeFieldReferenceSizeInfo) -> &Self {
mem::transmute(value)
}

pub fn sizes(&self) -> &[usize] {
unsafe { core::slice::from_raw_parts(self.0.sizes, self.0.count) }
}
}

impl Drop for TypeFieldReferenceSizeInfo {
fn drop(&mut self) {
unsafe { BNFreeTypeFieldReferenceSizes(self.0.sizes, self.0.count) }
}
}

impl CoreArrayProvider for TypeFieldReferenceSizeInfo {
type Raw = BNTypeFieldReferenceSizeInfo;
type Context = ();
type Wrapped<'a> = &'a TypeFieldReferenceSizeInfo;
}

unsafe impl CoreArrayProviderInner for TypeFieldReferenceSizeInfo {
unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
BNFreeTypeFieldReferenceSizeInfo(raw, count)
}

unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
TypeFieldReferenceSizeInfo::ref_from_raw(raw)
}
}

/////////////////////////
// TypeFieldReferenceTypeInfo

#[repr(transparent)]
pub struct TypeFieldReferenceTypeInfo(BNTypeFieldReferenceTypeInfo);

impl TypeFieldReferenceTypeInfo {
pub(crate) unsafe fn ref_from_raw(value: &BNTypeFieldReferenceTypeInfo) -> &Self {
mem::transmute(value)
}

pub fn types(&self) -> Array<ConfTypeList> {
unsafe { Array::new(self.0.types, self.0.count, core::marker::PhantomData) }
}
}

impl Drop for TypeFieldReferenceTypeInfo {
fn drop(&mut self) {
unsafe { BNFreeTypeFieldReferenceTypes(self.0.types, self.0.count) }
}
}

impl CoreArrayProvider for TypeFieldReferenceTypeInfo {
type Raw = BNTypeFieldReferenceTypeInfo;
type Context = ();
type Wrapped<'a> = &'a TypeFieldReferenceTypeInfo;
}

unsafe impl CoreArrayProviderInner for TypeFieldReferenceTypeInfo {
unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
BNFreeTypeFieldReferenceTypeInfo(raw, count)
}

unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
TypeFieldReferenceTypeInfo::ref_from_raw(raw)
}
}
Loading