diff options
| author | Ryan Snyder <ryan@vector35.com> | 2024-05-01 08:33:43 -0400 |
|---|---|---|
| committer | Ryan Snyder <ryan@vector35.com> | 2024-05-01 08:33:43 -0400 |
| commit | 3ccfedef84b8743f7045c50cdeb3f78b530ada8d (patch) | |
| tree | cc4850291d5cff7005de9e68b43b98c322ab2fb2 /rust | |
| parent | 2fb7daaf3210c4984f20444560145e776b586601 (diff) | |
| parent | 9717866665a42356881b062fcd80bc09492ee8b8 (diff) | |
Merge branch 'remove-zeroed-init' of github.com:rbran/binaryninja-api into dev
Diffstat (limited to 'rust')
| -rw-r--r-- | rust/src/architecture.rs | 25 | ||||
| -rw-r--r-- | rust/src/custombinaryview.rs | 15 | ||||
| -rw-r--r-- | rust/src/debuginfo.rs | 23 | ||||
| -rw-r--r-- | rust/src/demangle.rs | 8 | ||||
| -rw-r--r-- | rust/src/relocation.rs | 17 | ||||
| -rw-r--r-- | rust/src/types.rs | 10 |
6 files changed, 43 insertions, 55 deletions
diff --git a/rust/src/architecture.rs b/rust/src/architecture.rs index 9a7b3e72..a03f28d0 100644 --- a/rust/src/architecture.rs +++ b/rust/src/architecture.rs @@ -23,7 +23,7 @@ use std::{ collections::HashMap, ffi::{c_char, c_int, CStr, CString}, hash::Hash, - mem::zeroed, + mem::{zeroed, MaybeUninit}, ops, ptr, slice, }; @@ -1172,7 +1172,7 @@ impl Architecture for CoreArchitecture { } } } - + fn instruction_llil( &self, data: &[u8], @@ -1689,8 +1689,8 @@ where A: 'static + Architecture<Handle = CustomArchitectureHandle<A>> + Send + Sync, F: FnOnce(CustomArchitectureHandle<A>, CoreArchitecture) -> A, { - arch: A, - func: F, + arch: MaybeUninit<A>, + func: Option<F>, } extern "C" fn cb_init<A, F>(ctxt: *mut c_void, obj: *mut BNArchitecture) @@ -1704,11 +1704,10 @@ where handle: ctxt as *mut A, }; - let create = ptr::read(&custom_arch.func); - ptr::write( - &mut custom_arch.arch, - create(custom_arch_handle, CoreArchitecture(obj)), - ); + let create = custom_arch.func.take().unwrap(); + custom_arch + .arch + .write(create(custom_arch_handle, CoreArchitecture(obj))); } } @@ -2685,13 +2684,13 @@ where let name = name.into_bytes_with_nul(); let uninit_arch = ArchitectureBuilder { - arch: unsafe { zeroed() }, - func, + arch: MaybeUninit::zeroed(), + func: Some(func), }; let raw = Box::into_raw(Box::new(uninit_arch)); let mut custom_arch = BNCustomArchitecture { - context: raw as *mut _, + context: raw as *mut ArchitectureBuilder<_, _> as *mut _, init: Some(cb_init::<A, F>), getEndianness: Some(cb_endianness::<A>), getAddressSize: Some(cb_address_size::<A>), @@ -2776,7 +2775,7 @@ where assert!(!res.is_null()); - &(*raw).arch + (*raw).arch.assume_init_mut() } } diff --git a/rust/src/custombinaryview.rs b/rust/src/custombinaryview.rs index 898102d6..4c645ffd 100644 --- a/rust/src/custombinaryview.rs +++ b/rust/src/custombinaryview.rs @@ -20,6 +20,7 @@ pub use binaryninjacore_sys::BNModificationStatus as ModificationStatus; use std::marker::PhantomData; use std::mem; +use std::mem::MaybeUninit; use std::os::raw::c_void; use std::ptr; use std::slice; @@ -122,11 +123,10 @@ where let long_name = long_name.into_bytes_with_nul(); let long_name_ptr = long_name.as_ref().as_ptr() as *mut _; - let ctxt = Box::new(unsafe { mem::zeroed() }); - let ctxt = Box::into_raw(ctxt); + let ctxt = Box::leak(Box::new(MaybeUninit::zeroed())); let mut bn_obj = BNCustomBinaryViewType { - context: ctxt as *mut _, + context: ctxt.as_mut_ptr() as *mut _, create: Some(cb_create::<T>), parse: Some(cb_parse::<T>), isValidForData: Some(cb_valid::<T>), @@ -140,15 +140,16 @@ where if res.is_null() { // avoid leaking the space allocated for the type, but also // avoid running its Drop impl (if any -- not that there should - // be one since view types live for the life of the process) - mem::forget(*Box::from_raw(ctxt)); + // be one since view types live for the life of the process) as + // MaybeUninit suppress the Drop implementation of it's inner type + drop(Box::from_raw(ctxt)); panic!("bvt registration failed"); } - ptr::write(ctxt, constructor(BinaryViewType(res))); + ctxt.write(constructor(BinaryViewType(res))); - &*ctxt + ctxt.assume_init_mut() } } diff --git a/rust/src/debuginfo.rs b/rust/src/debuginfo.rs index ab4f8f6b..08ba2517 100644 --- a/rust/src/debuginfo.rs +++ b/rust/src/debuginfo.rs @@ -74,7 +74,7 @@ use crate::{ types::{DataVariableAndName, NameAndType, Type}, }; -use std::{hash::Hash, mem, os::raw::c_void, ptr, slice}; +use std::{hash::Hash, os::raw::c_void, ptr, slice}; struct ProgressContext(Option<Box<dyn Fn(usize, usize) -> Result<(), ()>>>); @@ -109,14 +109,14 @@ impl DebugInfoParser { /// List all debug-info parsers pub fn list() -> Array<DebugInfoParser> { - let mut count: usize = unsafe { mem::zeroed() }; + let mut count = 0; let raw_parsers = unsafe { BNGetDebugInfoParsers(&mut count as *mut _) }; unsafe { Array::new(raw_parsers, count, ()) } } /// Returns a list of debug-info parsers that are valid for the provided binary view pub fn parsers_for_view(bv: &BinaryView) -> Array<DebugInfoParser> { - let mut count: usize = unsafe { mem::zeroed() }; + let mut count = 0; let raw_parsers = unsafe { BNGetDebugInfoParsersForView(bv.handle, &mut count as *mut _) }; unsafe { Array::new(raw_parsers, count, ()) } } @@ -414,10 +414,7 @@ impl DebugInfo { } /// Returns a generator of all functions provided by a named DebugInfoParser - pub fn functions_by_name<S: BnStrCompatible>( - &self, - parser_name: S, - ) -> Vec<DebugFunctionInfo> { + pub fn functions_by_name<S: BnStrCompatible>(&self, parser_name: S) -> Vec<DebugFunctionInfo> { let parser_name = parser_name.into_bytes_with_nul(); let mut count: usize = 0; @@ -758,21 +755,15 @@ impl DebugInfo { let short_name_bytes = new_func.short_name.map(|name| name.into_bytes_with_nul()); let short_name = short_name_bytes .as_ref() - .map_or(ptr::null_mut() as *mut _, |name| { - name.as_ptr() as _ - }); + .map_or(ptr::null_mut() as *mut _, |name| name.as_ptr() as _); let full_name_bytes = new_func.full_name.map(|name| name.into_bytes_with_nul()); let full_name = full_name_bytes .as_ref() - .map_or(ptr::null_mut() as *mut _, |name| { - name.as_ptr() as _ - }); + .map_or(ptr::null_mut() as *mut _, |name| name.as_ptr() as _); let raw_name_bytes = new_func.raw_name.map(|name| name.into_bytes_with_nul()); let raw_name = raw_name_bytes .as_ref() - .map_or(ptr::null_mut() as *mut _, |name| { - name.as_ptr() as _ - }); + .map_or(ptr::null_mut() as *mut _, |name| name.as_ptr() as _); let mut components_array: Vec<*const ::std::os::raw::c_char> = Vec::with_capacity(new_func.components.len()); diff --git a/rust/src/demangle.rs b/rust/src/demangle.rs index 19eb085c..3756ea06 100644 --- a/rust/src/demangle.rs +++ b/rust/src/demangle.rs @@ -33,8 +33,8 @@ pub fn demangle_gnu3<S: BnStrCompatible>( ) -> Result<(Option<Ref<Type>>, Vec<String>)> { let mangled_name_bwn = mangled_name.into_bytes_with_nul(); let mangled_name_ptr = mangled_name_bwn.as_ref(); - let mut out_type: *mut BNType = unsafe { std::mem::zeroed() }; - let mut out_name: *mut *mut std::os::raw::c_char = unsafe { std::mem::zeroed() }; + let mut out_type: *mut BNType = std::ptr::null_mut(); + let mut out_name: *mut *mut std::os::raw::c_char = std::ptr::null_mut(); let mut out_size: usize = 0; let res = unsafe { BNDemangleGNU3( @@ -89,8 +89,8 @@ pub fn demangle_ms<S: BnStrCompatible>( let mangled_name_bwn = mangled_name.into_bytes_with_nul(); let mangled_name_ptr = mangled_name_bwn.as_ref(); - let mut out_type: *mut BNType = unsafe { std::mem::zeroed() }; - let mut out_name: *mut *mut std::os::raw::c_char = unsafe { std::mem::zeroed() }; + let mut out_type: *mut BNType = std::ptr::null_mut(); + let mut out_name: *mut *mut std::os::raw::c_char = std::ptr::null_mut(); let mut out_size: usize = 0; let res = unsafe { BNDemangleMS( diff --git a/rust/src/relocation.rs b/rust/src/relocation.rs index 455f5a5a..17fd5958 100644 --- a/rust/src/relocation.rs +++ b/rust/src/relocation.rs @@ -9,6 +9,7 @@ use crate::{ }; use binaryninjacore_sys::*; use std::borrow::Borrow; +use std::mem::MaybeUninit; use std::os::raw::c_void; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] @@ -502,12 +503,9 @@ where let name = name.into_bytes_with_nul(); - let uninit_handler = RelocationHandlerBuilder { - handler: unsafe { std::mem::zeroed() }, - }; - let raw = Box::into_raw(Box::new(uninit_handler)); + let raw = Box::leak(Box::new(MaybeUninit::<RelocationHandlerBuilder<_>>::zeroed())); let mut custom_handler = BNCustomRelocationHandler { - context: raw as *mut _, + context: raw.as_mut_ptr() as *mut _, freeObject: Some(cb_free::<R>), getRelocationInfo: Some(cb_get_relocation_info::<R>), applyRelocation: Some(cb_apply_relocation::<R>), @@ -518,13 +516,12 @@ where assert!(!handle_raw.is_null()); let handle = CoreRelocationHandler(handle_raw); let custom_handle = CustomRelocationHandlerHandle { - handle: raw as *mut R, + handle: raw.as_mut_ptr() as *mut R, }; unsafe { - core::ptr::write( - &mut raw.as_mut().unwrap().handler, - func(custom_handle, CoreRelocationHandler(handle.0)), - ); + raw.write(RelocationHandlerBuilder { + handler: func(custom_handle, CoreRelocationHandler(handle.0)), + }); BNArchitectureRegisterRelocationHandler( arch.handle().as_ref().0, diff --git a/rust/src/types.rs b/rust/src/types.rs index 426b0487..85405b5e 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -422,7 +422,7 @@ impl TypeBuilder { pub fn parameters(&self) -> Result<Vec<FunctionParameter>> { unsafe { - let mut count: usize = mem::zeroed(); + let mut count = 0; let parameters_raw = BNGetTypeBuilderParameters(self.handle, &mut count); if parameters_raw.is_null() { Err(()) @@ -793,7 +793,7 @@ impl Type { pub fn parameters(&self) -> Result<Vec<FunctionParameter>> { unsafe { - let mut count: usize = mem::zeroed(); + let mut count = 0; let parameters_raw: *mut BNFunctionParameter = BNGetTypeParameters(self.handle, &mut count); if parameters_raw.is_null() { @@ -1549,7 +1549,7 @@ impl EnumerationBuilder { pub fn members(&self) -> Vec<EnumerationMember> { unsafe { - let mut count: usize = mem::zeroed(); + let mut count = 0; let members_raw = BNGetEnumerationBuilderMembers(self.handle, &mut count); let members: &[BNEnumerationMember] = slice::from_raw_parts(members_raw, count); @@ -1606,7 +1606,7 @@ impl Enumeration { pub fn members(&self) -> Vec<EnumerationMember> { unsafe { - let mut count: usize = mem::zeroed(); + let mut count = 0; let members_raw = BNGetEnumerationMembers(self.handle, &mut count); let members: &[BNEnumerationMember] = slice::from_raw_parts(members_raw, count); @@ -1937,7 +1937,7 @@ impl Structure { pub fn members(&self) -> Result<Vec<StructureMember>> { unsafe { - let mut count: usize = mem::zeroed(); + let mut count = 0; let members_raw: *mut BNStructureMember = BNGetStructureMembers(self.handle, &mut count); if members_raw.is_null() { |
