Skip to content

Commit 3ccfede

Browse files
committed
Merge branch 'remove-zeroed-init' of github.com:rbran/binaryninja-api into dev
2 parents 2fb7daa + 9717866 commit 3ccfede

File tree

6 files changed

+43
-55
lines changed

6 files changed

+43
-55
lines changed

rust/src/architecture.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::{
2323
collections::HashMap,
2424
ffi::{c_char, c_int, CStr, CString},
2525
hash::Hash,
26-
mem::zeroed,
26+
mem::{zeroed, MaybeUninit},
2727
ops, ptr, slice,
2828
};
2929

@@ -1172,7 +1172,7 @@ impl Architecture for CoreArchitecture {
11721172
}
11731173
}
11741174
}
1175-
1175+
11761176
fn instruction_llil(
11771177
&self,
11781178
data: &[u8],
@@ -1689,8 +1689,8 @@ where
16891689
A: 'static + Architecture<Handle = CustomArchitectureHandle<A>> + Send + Sync,
16901690
F: FnOnce(CustomArchitectureHandle<A>, CoreArchitecture) -> A,
16911691
{
1692-
arch: A,
1693-
func: F,
1692+
arch: MaybeUninit<A>,
1693+
func: Option<F>,
16941694
}
16951695

16961696
extern "C" fn cb_init<A, F>(ctxt: *mut c_void, obj: *mut BNArchitecture)
@@ -1704,11 +1704,10 @@ where
17041704
handle: ctxt as *mut A,
17051705
};
17061706

1707-
let create = ptr::read(&custom_arch.func);
1708-
ptr::write(
1709-
&mut custom_arch.arch,
1710-
create(custom_arch_handle, CoreArchitecture(obj)),
1711-
);
1707+
let create = custom_arch.func.take().unwrap();
1708+
custom_arch
1709+
.arch
1710+
.write(create(custom_arch_handle, CoreArchitecture(obj)));
17121711
}
17131712
}
17141713

@@ -2685,13 +2684,13 @@ where
26852684
let name = name.into_bytes_with_nul();
26862685

26872686
let uninit_arch = ArchitectureBuilder {
2688-
arch: unsafe { zeroed() },
2689-
func,
2687+
arch: MaybeUninit::zeroed(),
2688+
func: Some(func),
26902689
};
26912690

26922691
let raw = Box::into_raw(Box::new(uninit_arch));
26932692
let mut custom_arch = BNCustomArchitecture {
2694-
context: raw as *mut _,
2693+
context: raw as *mut ArchitectureBuilder<_, _> as *mut _,
26952694
init: Some(cb_init::<A, F>),
26962695
getEndianness: Some(cb_endianness::<A>),
26972696
getAddressSize: Some(cb_address_size::<A>),
@@ -2776,7 +2775,7 @@ where
27762775

27772776
assert!(!res.is_null());
27782777

2779-
&(*raw).arch
2778+
(*raw).arch.assume_init_mut()
27802779
}
27812780
}
27822781

rust/src/custombinaryview.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub use binaryninjacore_sys::BNModificationStatus as ModificationStatus;
2020

2121
use std::marker::PhantomData;
2222
use std::mem;
23+
use std::mem::MaybeUninit;
2324
use std::os::raw::c_void;
2425
use std::ptr;
2526
use std::slice;
@@ -122,11 +123,10 @@ where
122123
let long_name = long_name.into_bytes_with_nul();
123124
let long_name_ptr = long_name.as_ref().as_ptr() as *mut _;
124125

125-
let ctxt = Box::new(unsafe { mem::zeroed() });
126-
let ctxt = Box::into_raw(ctxt);
126+
let ctxt = Box::leak(Box::new(MaybeUninit::zeroed()));
127127

128128
let mut bn_obj = BNCustomBinaryViewType {
129-
context: ctxt as *mut _,
129+
context: ctxt.as_mut_ptr() as *mut _,
130130
create: Some(cb_create::<T>),
131131
parse: Some(cb_parse::<T>),
132132
isValidForData: Some(cb_valid::<T>),
@@ -140,15 +140,16 @@ where
140140
if res.is_null() {
141141
// avoid leaking the space allocated for the type, but also
142142
// avoid running its Drop impl (if any -- not that there should
143-
// be one since view types live for the life of the process)
144-
mem::forget(*Box::from_raw(ctxt));
143+
// be one since view types live for the life of the process) as
144+
// MaybeUninit suppress the Drop implementation of it's inner type
145+
drop(Box::from_raw(ctxt));
145146

146147
panic!("bvt registration failed");
147148
}
148149

149-
ptr::write(ctxt, constructor(BinaryViewType(res)));
150+
ctxt.write(constructor(BinaryViewType(res)));
150151

151-
&*ctxt
152+
ctxt.assume_init_mut()
152153
}
153154
}
154155

rust/src/debuginfo.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ use crate::{
7474
types::{DataVariableAndName, NameAndType, Type},
7575
};
7676

77-
use std::{hash::Hash, mem, os::raw::c_void, ptr, slice};
77+
use std::{hash::Hash, os::raw::c_void, ptr, slice};
7878

7979
struct ProgressContext(Option<Box<dyn Fn(usize, usize) -> Result<(), ()>>>);
8080

@@ -109,14 +109,14 @@ impl DebugInfoParser {
109109

110110
/// List all debug-info parsers
111111
pub fn list() -> Array<DebugInfoParser> {
112-
let mut count: usize = unsafe { mem::zeroed() };
112+
let mut count = 0;
113113
let raw_parsers = unsafe { BNGetDebugInfoParsers(&mut count as *mut _) };
114114
unsafe { Array::new(raw_parsers, count, ()) }
115115
}
116116

117117
/// Returns a list of debug-info parsers that are valid for the provided binary view
118118
pub fn parsers_for_view(bv: &BinaryView) -> Array<DebugInfoParser> {
119-
let mut count: usize = unsafe { mem::zeroed() };
119+
let mut count = 0;
120120
let raw_parsers = unsafe { BNGetDebugInfoParsersForView(bv.handle, &mut count as *mut _) };
121121
unsafe { Array::new(raw_parsers, count, ()) }
122122
}
@@ -414,10 +414,7 @@ impl DebugInfo {
414414
}
415415

416416
/// Returns a generator of all functions provided by a named DebugInfoParser
417-
pub fn functions_by_name<S: BnStrCompatible>(
418-
&self,
419-
parser_name: S,
420-
) -> Vec<DebugFunctionInfo> {
417+
pub fn functions_by_name<S: BnStrCompatible>(&self, parser_name: S) -> Vec<DebugFunctionInfo> {
421418
let parser_name = parser_name.into_bytes_with_nul();
422419

423420
let mut count: usize = 0;
@@ -758,21 +755,15 @@ impl DebugInfo {
758755
let short_name_bytes = new_func.short_name.map(|name| name.into_bytes_with_nul());
759756
let short_name = short_name_bytes
760757
.as_ref()
761-
.map_or(ptr::null_mut() as *mut _, |name| {
762-
name.as_ptr() as _
763-
});
758+
.map_or(ptr::null_mut() as *mut _, |name| name.as_ptr() as _);
764759
let full_name_bytes = new_func.full_name.map(|name| name.into_bytes_with_nul());
765760
let full_name = full_name_bytes
766761
.as_ref()
767-
.map_or(ptr::null_mut() as *mut _, |name| {
768-
name.as_ptr() as _
769-
});
762+
.map_or(ptr::null_mut() as *mut _, |name| name.as_ptr() as _);
770763
let raw_name_bytes = new_func.raw_name.map(|name| name.into_bytes_with_nul());
771764
let raw_name = raw_name_bytes
772765
.as_ref()
773-
.map_or(ptr::null_mut() as *mut _, |name| {
774-
name.as_ptr() as _
775-
});
766+
.map_or(ptr::null_mut() as *mut _, |name| name.as_ptr() as _);
776767

777768
let mut components_array: Vec<*const ::std::os::raw::c_char> =
778769
Vec::with_capacity(new_func.components.len());

rust/src/demangle.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ pub fn demangle_gnu3<S: BnStrCompatible>(
3333
) -> Result<(Option<Ref<Type>>, Vec<String>)> {
3434
let mangled_name_bwn = mangled_name.into_bytes_with_nul();
3535
let mangled_name_ptr = mangled_name_bwn.as_ref();
36-
let mut out_type: *mut BNType = unsafe { std::mem::zeroed() };
37-
let mut out_name: *mut *mut std::os::raw::c_char = unsafe { std::mem::zeroed() };
36+
let mut out_type: *mut BNType = std::ptr::null_mut();
37+
let mut out_name: *mut *mut std::os::raw::c_char = std::ptr::null_mut();
3838
let mut out_size: usize = 0;
3939
let res = unsafe {
4040
BNDemangleGNU3(
@@ -89,8 +89,8 @@ pub fn demangle_ms<S: BnStrCompatible>(
8989
let mangled_name_bwn = mangled_name.into_bytes_with_nul();
9090
let mangled_name_ptr = mangled_name_bwn.as_ref();
9191

92-
let mut out_type: *mut BNType = unsafe { std::mem::zeroed() };
93-
let mut out_name: *mut *mut std::os::raw::c_char = unsafe { std::mem::zeroed() };
92+
let mut out_type: *mut BNType = std::ptr::null_mut();
93+
let mut out_name: *mut *mut std::os::raw::c_char = std::ptr::null_mut();
9494
let mut out_size: usize = 0;
9595
let res = unsafe {
9696
BNDemangleMS(

rust/src/relocation.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::{
99
};
1010
use binaryninjacore_sys::*;
1111
use std::borrow::Borrow;
12+
use std::mem::MaybeUninit;
1213
use std::os::raw::c_void;
1314

1415
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -502,12 +503,9 @@ where
502503

503504
let name = name.into_bytes_with_nul();
504505

505-
let uninit_handler = RelocationHandlerBuilder {
506-
handler: unsafe { std::mem::zeroed() },
507-
};
508-
let raw = Box::into_raw(Box::new(uninit_handler));
506+
let raw = Box::leak(Box::new(MaybeUninit::<RelocationHandlerBuilder<_>>::zeroed()));
509507
let mut custom_handler = BNCustomRelocationHandler {
510-
context: raw as *mut _,
508+
context: raw.as_mut_ptr() as *mut _,
511509
freeObject: Some(cb_free::<R>),
512510
getRelocationInfo: Some(cb_get_relocation_info::<R>),
513511
applyRelocation: Some(cb_apply_relocation::<R>),
@@ -518,13 +516,12 @@ where
518516
assert!(!handle_raw.is_null());
519517
let handle = CoreRelocationHandler(handle_raw);
520518
let custom_handle = CustomRelocationHandlerHandle {
521-
handle: raw as *mut R,
519+
handle: raw.as_mut_ptr() as *mut R,
522520
};
523521
unsafe {
524-
core::ptr::write(
525-
&mut raw.as_mut().unwrap().handler,
526-
func(custom_handle, CoreRelocationHandler(handle.0)),
527-
);
522+
raw.write(RelocationHandlerBuilder {
523+
handler: func(custom_handle, CoreRelocationHandler(handle.0)),
524+
});
528525

529526
BNArchitectureRegisterRelocationHandler(
530527
arch.handle().as_ref().0,

rust/src/types.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ impl TypeBuilder {
422422

423423
pub fn parameters(&self) -> Result<Vec<FunctionParameter>> {
424424
unsafe {
425-
let mut count: usize = mem::zeroed();
425+
let mut count = 0;
426426
let parameters_raw = BNGetTypeBuilderParameters(self.handle, &mut count);
427427
if parameters_raw.is_null() {
428428
Err(())
@@ -793,7 +793,7 @@ impl Type {
793793

794794
pub fn parameters(&self) -> Result<Vec<FunctionParameter>> {
795795
unsafe {
796-
let mut count: usize = mem::zeroed();
796+
let mut count = 0;
797797
let parameters_raw: *mut BNFunctionParameter =
798798
BNGetTypeParameters(self.handle, &mut count);
799799
if parameters_raw.is_null() {
@@ -1549,7 +1549,7 @@ impl EnumerationBuilder {
15491549

15501550
pub fn members(&self) -> Vec<EnumerationMember> {
15511551
unsafe {
1552-
let mut count: usize = mem::zeroed();
1552+
let mut count = 0;
15531553
let members_raw = BNGetEnumerationBuilderMembers(self.handle, &mut count);
15541554
let members: &[BNEnumerationMember] = slice::from_raw_parts(members_raw, count);
15551555

@@ -1606,7 +1606,7 @@ impl Enumeration {
16061606

16071607
pub fn members(&self) -> Vec<EnumerationMember> {
16081608
unsafe {
1609-
let mut count: usize = mem::zeroed();
1609+
let mut count = 0;
16101610
let members_raw = BNGetEnumerationMembers(self.handle, &mut count);
16111611
let members: &[BNEnumerationMember] = slice::from_raw_parts(members_raw, count);
16121612

@@ -1937,7 +1937,7 @@ impl Structure {
19371937

19381938
pub fn members(&self) -> Result<Vec<StructureMember>> {
19391939
unsafe {
1940-
let mut count: usize = mem::zeroed();
1940+
let mut count = 0;
19411941
let members_raw: *mut BNStructureMember =
19421942
BNGetStructureMembers(self.handle, &mut count);
19431943
if members_raw.is_null() {

0 commit comments

Comments
 (0)