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

remove unecessary and crash causing zeroed call inits #5293

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
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
25 changes: 12 additions & 13 deletions rust/src/architecture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -1172,7 +1172,7 @@ impl Architecture for CoreArchitecture {
}
}
}

fn instruction_llil(
&self,
data: &[u8],
Expand Down Expand Up @@ -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)
Expand All @@ -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)));
}
}

Expand Down Expand Up @@ -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>),
Expand Down Expand Up @@ -2776,7 +2775,7 @@ where

assert!(!res.is_null());

&(*raw).arch
(*raw).arch.assume_init_mut()
}
}

Expand Down
15 changes: 8 additions & 7 deletions rust/src/custombinaryview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>),
Expand All @@ -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()
}
}

Expand Down
23 changes: 7 additions & 16 deletions rust/src/debuginfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<(), ()>>>);

Expand Down Expand Up @@ -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, ()) }
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
8 changes: 4 additions & 4 deletions rust/src/demangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
17 changes: 7 additions & 10 deletions rust/src/relocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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)]
Expand Down Expand Up @@ -501,12 +502,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>),
Expand All @@ -517,13 +515,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,
Expand Down
10 changes: 5 additions & 5 deletions rust/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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() {
Expand Down
Loading