diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 23078ec88..48efcf228 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -102,7 +102,6 @@ version = "0.1.0" dependencies = [ "binaryninjacore-sys", "lazy_static", - "libc", "log", "rayon", ] diff --git a/rust/Cargo.toml b/rust/Cargo.toml index b827b9bb5..4f30f8392 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -10,7 +10,6 @@ noexports = [] [dependencies] lazy_static = "1.4.0" log = "0.4" -libc = "0.2" rayon = { version = "1.8", optional = true } binaryninjacore-sys = { path = "binaryninjacore-sys" } diff --git a/rust/src/architecture.rs b/rust/src/architecture.rs index cf5d32d64..91426fb9c 100644 --- a/rust/src/architecture.rs +++ b/rust/src/architecture.rs @@ -19,6 +19,7 @@ use binaryninjacore_sys::*; use std::{ + alloc, borrow::{Borrow, Cow}, collections::HashMap, ffi::{c_char, c_int, CStr, CString}, @@ -1696,8 +1697,8 @@ where A: 'static + Architecture> + Send + Sync + Sized, F: FnOnce(CustomArchitectureHandle, CoreArchitecture) -> A, { + use std::ffi::{c_char, c_void}; use std::mem; - use std::os::raw::{c_char, c_void}; #[repr(C)] struct ArchitectureBuilder @@ -2096,30 +2097,44 @@ where { let custom_arch = unsafe { &*(ctxt as *mut A) }; - if let Some(group) = custom_arch.flag_group_from_id(group) { - let flag_conditions = group.flag_conditions(); + let Some(group) = custom_arch.flag_group_from_id(group) else { + unsafe { *count = 0 }; + return ptr::null_mut(); + }; - unsafe { - let allocation_size = - mem::size_of::() * flag_conditions.len(); - let result = libc::malloc(allocation_size) as *mut BNFlagConditionForSemanticClass; - let out_slice = slice::from_raw_parts_mut(result, flag_conditions.len()); + let flag_conditions = group.flag_conditions(); - for (i, (class, cond)) in flag_conditions.iter().enumerate() { - let out = out_slice.get_unchecked_mut(i); + let conds: Box<[_]> = flag_conditions + .iter() + .map(|(class, cond)| BNFlagConditionForSemanticClass { + semanticClass: class.id(), + condition: *cond, + }) + .collect(); - out.semanticClass = class.id(); - out.condition = *cond; - } + // Extremely gross. We want to smuggle the length through FFI so that we can free the array + // later, so we construct a custom Layout containing one `usize` followed by all the + // conditions, then allocate according to this layout. + let len = flag_conditions.len(); + let (layout, offset) = alloc::Layout::new::() + .extend(alloc::Layout::array::(len).unwrap()) + .map(|(layout, offset)| (layout.pad_to_align(), offset)) + .unwrap(); - *count = flag_conditions.len(); - result - } - } else { - unsafe { - *count = 0; + unsafe { + let raw: *mut u8 = alloc::alloc(layout); + if raw.is_null() { + return ptr::null_mut(); } - ptr::null_mut() + + // Smuggle the length + raw.cast::().write(len); + // Offset past the length, now we're just pointing at the conditions + let raw = raw.add(offset).cast::(); + raw.copy_from(conds.as_ptr(), len); + + count.write(len); + raw } } @@ -2129,8 +2144,27 @@ where ) where A: 'static + Architecture> + Send + Sync, { + if conds.is_null() { + return; + } + + // We don't know the length yet, and the alignment of an array is the same as its elements, + // so we just mock [T; N] with T in order to obtain the offset we want. + let offset = alloc::Layout::new::() + .extend(alloc::Layout::new::()) + .unwrap() + .1; + unsafe { - libc::free(conds as *mut _); + // Read out the smuggled length, then recalculate the *actual* layout of the data given + // the length, and finally free the memory. + let base = conds.cast::().sub(offset); + let len = base.cast::().read(); + let layout = alloc::Layout::new::() + .extend(alloc::Layout::array::(len).unwrap()) + .unwrap() + .0; + alloc::dealloc(base, layout); } } diff --git a/rust/src/binaryview.rs b/rust/src/binaryview.rs index b8bac14ac..bd83b8742 100644 --- a/rust/src/binaryview.rs +++ b/rust/src/binaryview.rs @@ -268,15 +268,11 @@ pub trait BinaryViewExt: BinaryViewBase { } fn original_base(&self) -> u64 { - unsafe { - BNGetOriginalBase(self.as_ref().handle) - } + unsafe { BNGetOriginalBase(self.as_ref().handle) } } fn set_original_base(&self, base: u64) { - unsafe { - BNSetOriginalBase(self.as_ref().handle, base) - } + unsafe { BNSetOriginalBase(self.as_ref().handle, base) } } fn end(&self) -> u64 { @@ -1411,10 +1407,7 @@ pub trait BinaryViewExt: BinaryViewBase { unsafe { BNRemoveComponentByGuid(self.as_ref().handle, path.as_ptr()) } } - fn data_variable_parent_components( - &self, - data_variable: &DataVariable, - ) -> Array { + fn data_variable_parent_components(&self, data_variable: &DataVariable) -> Array { let mut count = 0; let result = unsafe { BNGetDataVariableParentComponents( @@ -1835,7 +1828,7 @@ where Handler: BinaryViewEventHandler, { unsafe extern "C" fn on_event( - ctx: *mut ::std::os::raw::c_void, + ctx: *mut c_void, view: *mut BNBinaryView, ) { ffi_wrap!("EventHandler::on_event", { @@ -1848,10 +1841,6 @@ where let raw = Box::into_raw(boxed); unsafe { - BNRegisterBinaryViewEvent( - event_type, - Some(on_event::), - raw as *mut ::std::os::raw::c_void, - ); + BNRegisterBinaryViewEvent(event_type, Some(on_event::), raw as *mut c_void); } } diff --git a/rust/src/callingconvention.rs b/rust/src/callingconvention.rs index a009b439e..24b509c65 100644 --- a/rust/src/callingconvention.rs +++ b/rust/src/callingconvention.rs @@ -15,19 +15,17 @@ //! Contains and provides information about different systems' calling conventions to analysis. use std::borrow::Borrow; +use std::ffi::c_void; use std::fmt::{Debug, Formatter}; use std::marker::PhantomData; use std::mem; -use std::os::raw::c_void; use std::ptr; use std::slice; use binaryninjacore_sys::*; use crate::architecture::{Architecture, ArchitectureExt, CoreArchitecture, Register}; -use crate::rc::{ - CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, RefCountable, -}; +use crate::rc::{CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, RefCountable}; use crate::string::*; // TODO diff --git a/rust/src/command.rs b/rust/src/command.rs index e682baeef..7a0af93f1 100644 --- a/rust/src/command.rs +++ b/rust/src/command.rs @@ -37,8 +37,8 @@ use binaryninjacore_sys::{ BNRegisterPluginCommandForFunction, BNRegisterPluginCommandForRange, }; +use std::ffi::c_void; use std::ops::Range; -use std::os::raw::c_void; use crate::binaryview::BinaryView; use crate::function::Function; diff --git a/rust/src/custombinaryview.rs b/rust/src/custombinaryview.rs index 05f1acb8d..c01d45e49 100644 --- a/rust/src/custombinaryview.rs +++ b/rust/src/custombinaryview.rs @@ -18,10 +18,10 @@ use binaryninjacore_sys::*; pub use binaryninjacore_sys::BNModificationStatus as ModificationStatus; +use std::ffi::c_void; use std::marker::PhantomData; use std::mem; use std::mem::MaybeUninit; -use std::os::raw::c_void; use std::ptr; use std::slice; diff --git a/rust/src/debuginfo.rs b/rust/src/debuginfo.rs index 2b43c9cc7..9f8949ee6 100644 --- a/rust/src/debuginfo.rs +++ b/rust/src/debuginfo.rs @@ -70,15 +70,16 @@ use binaryninjacore_sys::*; -use crate::{ - binaryview::BinaryView, - platform::Platform, - rc::*, - string::{raw_to_string, BnStrCompatible, BnString}, - types::{DataVariableAndName, NameAndType, NamedTypedVariable, Type}, -}; +use crate::binaryview::BinaryView; +use crate::platform::Platform; +use crate::rc::*; +use crate::string::{raw_to_string, BnStrCompatible, BnString}; +use crate::types::{DataVariableAndName, NameAndType, NamedTypedVariable, Type}; -use std::{hash::Hash, os::raw::c_void, ptr, slice}; +use std::ffi::{c_char, c_void}; +use std::hash::Hash; +use std::ptr; +use std::slice; struct ProgressContext(Option Result<(), ()>>>); @@ -311,14 +312,11 @@ impl From<&BNDebugFunctionInfo> for DebugFunctionInfo { .map(|component| raw_to_string(*component as *const _).unwrap()) .collect(); - let local_variables: Vec = unsafe { slice::from_raw_parts(raw.localVariables, raw.localVariableN) } - .iter() - .map(|local_variable| { - unsafe { - NamedTypedVariable::from_raw(local_variable) - } - }) - .collect(); + let local_variables: Vec = + unsafe { slice::from_raw_parts(raw.localVariables, raw.localVariableN) } + .iter() + .map(|local_variable| unsafe { NamedTypedVariable::from_raw(local_variable) }) + .collect(); Self { short_name: raw_to_string(raw.shortName), @@ -433,10 +431,7 @@ impl DebugInfo { } /// Returns a generator of all functions provided by a named DebugInfoParser - pub fn functions_by_name( - &self, - parser_name: S - ) -> Vec { + pub fn functions_by_name(&self, parser_name: S) -> Vec { let parser_name = parser_name.into_bytes_with_nul(); let mut count: usize = 0; @@ -754,8 +749,7 @@ impl DebugInfo { new_type: &Type, components: &[&str], ) -> bool { - let mut components_array: Vec<*const ::std::os::raw::c_char> = - Vec::with_capacity(components.len()); + let mut components_array: Vec<*const c_char> = Vec::with_capacity(components.len()); for component in components { components_array.push(component.as_ptr() as _); } @@ -787,28 +781,28 @@ impl DebugInfo { .as_ref() .map_or(ptr::null_mut() as *mut _, |name| name.as_ptr() as _); - let mut components_array: Vec<*mut ::std::os::raw::c_char> = - Vec::with_capacity(new_func.components.len()); - + let mut components_array: Vec<*mut c_char> = Vec::with_capacity(new_func.components.len()); let mut local_variables_array: Vec = Vec::with_capacity(new_func.local_variables.len()); unsafe { for component in &new_func.components { - components_array.push(BNAllocString(component.clone().into_bytes_with_nul().as_ptr() as _)); + components_array.push(BNAllocString( + component.clone().into_bytes_with_nul().as_ptr() as _, + )); } for local_variable in &new_func.local_variables { - local_variables_array.push( - BNVariableNameAndType { - var: local_variable.var.raw(), - autoDefined: local_variable.auto_defined, - typeConfidence: local_variable.ty.confidence, - name: BNAllocString(local_variable.name.clone().into_bytes_with_nul().as_ptr() as _), - type_: local_variable.ty.contents.handle, - } - ); + local_variables_array.push(BNVariableNameAndType { + var: local_variable.var.raw(), + autoDefined: local_variable.auto_defined, + typeConfidence: local_variable.ty.confidence, + name: BNAllocString( + local_variable.name.clone().into_bytes_with_nul().as_ptr() as _ + ), + type_: local_variable.ty.contents.handle, + }); } let result = BNAddDebugFunction( @@ -852,8 +846,7 @@ impl DebugInfo { name: Option, components: &[&str], ) -> bool { - let mut components_array: Vec<*const ::std::os::raw::c_char> = - Vec::with_capacity(components.len()); + let mut components_array: Vec<*const c_char> = Vec::with_capacity(components.len()); for component in components { components_array.push(component.as_ptr() as _); } diff --git a/rust/src/demangle.rs b/rust/src/demangle.rs index 1b940ff91..3f6ed50f8 100644 --- a/rust/src/demangle.rs +++ b/rust/src/demangle.rs @@ -15,8 +15,7 @@ //! Interfaces for demangling and simplifying mangled names in binaries. use binaryninjacore_sys::*; -use std::os::raw::c_char; -use std::{ffi::CStr, result}; +use std::ffi::{c_char, CStr}; use crate::architecture::CoreArchitecture; use crate::string::{BnStrCompatible, BnString}; @@ -24,15 +23,12 @@ use crate::types::Type; use crate::rc::*; -pub type Result = result::Result; +pub type Result = std::result::Result; -pub fn demangle_llvm( - mangled_name: S, - simplify: bool, -) -> Result> { +pub fn demangle_llvm(mangled_name: S, simplify: bool) -> Result> { let mangled_name_bwn = mangled_name.into_bytes_with_nul(); let mangled_name_ptr = mangled_name_bwn.as_ref(); - let mut out_name: *mut *mut std::os::raw::c_char = unsafe { std::mem::zeroed() }; + let mut out_name: *mut *mut c_char = unsafe { std::mem::zeroed() }; let mut out_size: usize = 0; let res = unsafe { BNDemangleLLVM( @@ -77,7 +73,7 @@ pub fn demangle_gnu3( 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 = std::ptr::null_mut(); - let mut out_name: *mut *mut std::os::raw::c_char = std::ptr::null_mut(); + let mut out_name: *mut *mut c_char = std::ptr::null_mut(); let mut out_size: usize = 0; let res = unsafe { BNDemangleGNU3( @@ -133,7 +129,7 @@ pub fn demangle_ms( let mangled_name_ptr = mangled_name_bwn.as_ref(); 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_name: *mut *mut c_char = std::ptr::null_mut(); let mut out_size: usize = 0; let res = unsafe { BNDemangleMS( diff --git a/rust/src/downloadprovider.rs b/rust/src/downloadprovider.rs index 0d388486e..607f89c11 100644 --- a/rust/src/downloadprovider.rs +++ b/rust/src/downloadprovider.rs @@ -1,10 +1,9 @@ -use crate::rc::{Array, CoreArrayProvider, Guard, CoreArrayProviderInner, Ref, RefCountable}; +use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, RefCountable}; use crate::settings::Settings; use crate::string::{BnStrCompatible, BnString}; use binaryninjacore_sys::*; use std::collections::HashMap; -use std::ffi::{c_void, CStr}; -use std::os::raw::c_char; +use std::ffi::{c_char, c_void, CStr}; use std::ptr::null_mut; use std::slice; diff --git a/rust/src/fileaccessor.rs b/rust/src/fileaccessor.rs index cf94e902b..3ff1a5175 100644 --- a/rust/src/fileaccessor.rs +++ b/rust/src/fileaccessor.rs @@ -27,7 +27,7 @@ impl<'a> FileAccessor<'a> { where F: 'a + Read + Write + Seek + Sized, { - use std::os::raw::c_void; + use std::ffi::c_void; extern "C" fn cb_get_length(ctxt: *mut c_void) -> u64 where diff --git a/rust/src/filemetadata.rs b/rust/src/filemetadata.rs index 7d385a4c8..b1726c051 100644 --- a/rust/src/filemetadata.rs +++ b/rust/src/filemetadata.rs @@ -49,6 +49,7 @@ use crate::database::Database; use crate::rc::*; use crate::string::*; +use std::ffi::c_void; use std::ptr; #[derive(PartialEq, Eq, Hash)] @@ -224,7 +225,7 @@ impl FileMetadata { BNCreateDatabaseWithProgress( handle, filename_ptr, - func as *mut libc::c_void, + func as *mut c_void, Some(cb_progress_func), ptr::null_mut(), ) @@ -273,7 +274,7 @@ impl FileMetadata { BNOpenExistingDatabaseWithProgress( self.handle, filename_ptr, - func as *mut libc::c_void, + func as *mut c_void, Some(cb_progress_func), ) }, @@ -313,11 +314,7 @@ unsafe impl RefCountable for FileMetadata { } } -unsafe extern "C" fn cb_progress_func( - ctxt: *mut ::std::os::raw::c_void, - progress: usize, - total: usize, -) -> bool { +unsafe extern "C" fn cb_progress_func(ctxt: *mut c_void, progress: usize, total: usize) -> bool { let func: fn(usize, usize) -> bool = core::mem::transmute(ctxt); func(progress, total) } diff --git a/rust/src/functionrecognizer.rs b/rust/src/functionrecognizer.rs index c63edcab0..63f732897 100644 --- a/rust/src/functionrecognizer.rs +++ b/rust/src/functionrecognizer.rs @@ -3,7 +3,7 @@ use crate::{ architecture::CoreArchitecture, binaryview::BinaryView, function::Function, llil, mlil, }; use binaryninjacore_sys::*; -use std::os::raw::c_void; +use std::ffi::c_void; pub trait FunctionRecognizer { fn recognize_low_level_il( diff --git a/rust/src/headless.rs b/rust/src/headless.rs index d4d8892ab..18a63bf98 100644 --- a/rust/src/headless.rs +++ b/rust/src/headless.rs @@ -13,8 +13,7 @@ // limitations under the License. use crate::{ - binaryview, - rc, + binaryview, rc, string::{BnStrCompatible, IntoJson}, }; @@ -23,17 +22,16 @@ use std::path::PathBuf; #[cfg(not(target_os = "windows"))] fn binja_path() -> PathBuf { - use std::ffi::{CStr, OsStr}; + use std::ffi::{c_char, c_void, CStr, OsStr}; use std::mem; - use std::os::raw; use std::os::unix::ffi::OsStrExt; #[repr(C)] struct DlInfo { - dli_fname: *const raw::c_char, - dli_fbase: *mut raw::c_void, - dli_sname: *const raw::c_char, - dli_saddr: *mut raw::c_void, + dli_fname: *const c_char, + dli_fbase: *mut c_void, + dli_sname: *const c_char, + dli_saddr: *mut c_void, } if let Ok(p) = env::var("BINJA_DIR") { @@ -41,7 +39,7 @@ fn binja_path() -> PathBuf { } extern "C" { - fn dladdr(addr: *mut raw::c_void, info: *mut DlInfo) -> raw::c_int; + fn dladdr(addr: *mut c_void, info: *mut DlInfo) -> i32; } unsafe { diff --git a/rust/src/interaction.rs b/rust/src/interaction.rs index 7d10bbe4a..9d66730de 100644 --- a/rust/src/interaction.rs +++ b/rust/src/interaction.rs @@ -16,8 +16,7 @@ use binaryninjacore_sys::*; -use std::ffi::CStr; -use std::os::raw::{c_char, c_void}; +use std::ffi::{c_char, c_void, CStr}; use std::path::PathBuf; use crate::binaryview::BinaryView; @@ -25,7 +24,7 @@ use crate::rc::Ref; use crate::string::{BnStrCompatible, BnString}; pub fn get_text_line_input(prompt: &str, title: &str) -> Option { - let mut value: *mut libc::c_char = std::ptr::null_mut(); + let mut value: *mut c_char = std::ptr::null_mut(); let result = unsafe { BNGetTextLineInput( @@ -80,7 +79,7 @@ pub fn get_address_input(prompt: &str, title: &str) -> Option { } pub fn get_open_filename_input(prompt: &str, extension: &str) -> Option { - let mut value: *mut libc::c_char = std::ptr::null_mut(); + let mut value: *mut c_char = std::ptr::null_mut(); let result = unsafe { BNGetOpenFileNameInput( @@ -98,7 +97,7 @@ pub fn get_open_filename_input(prompt: &str, extension: &str) -> Option } pub fn get_save_filename_input(prompt: &str, title: &str, default_name: &str) -> Option { - let mut value: *mut libc::c_char = std::ptr::null_mut(); + let mut value: *mut c_char = std::ptr::null_mut(); let result = unsafe { BNGetSaveFileNameInput( @@ -117,7 +116,7 @@ pub fn get_save_filename_input(prompt: &str, title: &str, default_name: &str) -> } pub fn get_directory_name_input(prompt: &str, default_name: &str) -> Option { - let mut value: *mut libc::c_char = std::ptr::null_mut(); + let mut value: *mut c_char = std::ptr::null_mut(); let result = unsafe { BNGetDirectoryNameInput( diff --git a/rust/src/lib.rs b/rust/src/lib.rs index c086478d9..acfb6d809 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -106,7 +106,6 @@ extern crate log; #[doc(hidden)] pub extern crate binaryninjacore_sys; -extern crate libc; #[cfg(feature = "rayon")] extern crate rayon; @@ -176,10 +175,10 @@ use std::path::PathBuf; pub use binaryninjacore_sys::BNBranchType as BranchType; pub use binaryninjacore_sys::BNEndianness as Endianness; use binaryview::BinaryView; -use metadata::Metadata; -use metadata::MetadataType; -use string::BnStrCompatible; -use string::IntoJson; +use metadata::{Metadata, MetadataType}; +use string::{BnStrCompatible, BnString, IntoJson}; + +use std::ffi::c_char; // Commented out to suppress unused warnings // const BN_MAX_INSTRUCTION_LENGTH: u64 = 256; @@ -210,7 +209,7 @@ pub fn load(filename: S) -> Option( binaryninjacore_sys::BNLoadFilename( filename.as_ref().as_ptr() as *mut _, update_analysis_and_wait, - options_or_default.as_ptr() as *mut core::ffi::c_char, + options_or_default.as_ptr() as *mut c_char, None, ) }; @@ -299,7 +298,7 @@ pub fn load_view( binaryninjacore_sys::BNLoadBinaryView( bv.handle as *mut _, update_analysis_and_wait, - options_or_default.as_ptr() as *mut core::ffi::c_char, + options_or_default.as_ptr() as *mut c_char, None, ) }; @@ -312,72 +311,59 @@ pub fn load_view( } pub fn install_directory() -> Result { - let s: *mut std::os::raw::c_char = unsafe { binaryninjacore_sys::BNGetInstallDirectory() }; + let s = unsafe { binaryninjacore_sys::BNGetInstallDirectory() }; if s.is_null() { return Err(()); } - Ok(PathBuf::from( - unsafe { string::BnString::from_raw(s) }.to_string(), - )) + Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string())) } pub fn bundled_plugin_directory() -> Result { - let s: *mut std::os::raw::c_char = - unsafe { binaryninjacore_sys::BNGetBundledPluginDirectory() }; + let s = unsafe { binaryninjacore_sys::BNGetBundledPluginDirectory() }; if s.is_null() { return Err(()); } - Ok(PathBuf::from( - unsafe { string::BnString::from_raw(s) }.to_string(), - )) + Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string())) } pub fn set_bundled_plugin_directory(new_dir: S) { unsafe { binaryninjacore_sys::BNSetBundledPluginDirectory( - new_dir.into_bytes_with_nul().as_ref().as_ptr() as *const std::os::raw::c_char, + new_dir.into_bytes_with_nul().as_ref().as_ptr() as *const c_char, ) }; } pub fn user_directory() -> Result { - let s: *mut std::os::raw::c_char = unsafe { binaryninjacore_sys::BNGetUserDirectory() }; + let s = unsafe { binaryninjacore_sys::BNGetUserDirectory() }; if s.is_null() { return Err(()); } - Ok(PathBuf::from( - unsafe { string::BnString::from_raw(s) }.to_string(), - )) + Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string())) } pub fn user_plugin_directory() -> Result { - let s: *mut std::os::raw::c_char = unsafe { binaryninjacore_sys::BNGetUserPluginDirectory() }; + let s = unsafe { binaryninjacore_sys::BNGetUserPluginDirectory() }; if s.is_null() { return Err(()); } - Ok(PathBuf::from( - unsafe { string::BnString::from_raw(s) }.to_string(), - )) + Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string())) } pub fn repositories_directory() -> Result { - let s: *mut std::os::raw::c_char = unsafe { binaryninjacore_sys::BNGetRepositoriesDirectory() }; + let s = unsafe { binaryninjacore_sys::BNGetRepositoriesDirectory() }; if s.is_null() { return Err(()); } - Ok(PathBuf::from( - unsafe { string::BnString::from_raw(s) }.to_string(), - )) + Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string())) } pub fn settings_file_name() -> Result { - let s: *mut std::os::raw::c_char = unsafe { binaryninjacore_sys::BNGetSettingsFileName() }; + let s = unsafe { binaryninjacore_sys::BNGetSettingsFileName() }; if s.is_null() { return Err(()); } - Ok(PathBuf::from( - unsafe { string::BnString::from_raw(s) }.to_string(), - )) + Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string())) } pub fn save_last_run() { @@ -387,51 +373,45 @@ pub fn save_last_run() { pub fn path_relative_to_bundled_plugin_directory( path: S, ) -> Result { - let s: *mut std::os::raw::c_char = unsafe { + let s = unsafe { binaryninjacore_sys::BNGetPathRelativeToBundledPluginDirectory( - path.into_bytes_with_nul().as_ref().as_ptr() as *const std::os::raw::c_char, + path.into_bytes_with_nul().as_ref().as_ptr() as *const c_char, ) }; if s.is_null() { return Err(()); } - Ok(PathBuf::from( - unsafe { string::BnString::from_raw(s) }.to_string(), - )) + Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string())) } pub fn path_relative_to_user_plugin_directory( path: S, ) -> Result { - let s: *mut std::os::raw::c_char = unsafe { + let s = unsafe { binaryninjacore_sys::BNGetPathRelativeToUserPluginDirectory( - path.into_bytes_with_nul().as_ref().as_ptr() as *const std::os::raw::c_char, + path.into_bytes_with_nul().as_ref().as_ptr() as *const c_char, ) }; if s.is_null() { return Err(()); } - Ok(PathBuf::from( - unsafe { string::BnString::from_raw(s) }.to_string(), - )) + Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string())) } pub fn path_relative_to_user_directory(path: S) -> Result { - let s: *mut std::os::raw::c_char = unsafe { + let s = unsafe { binaryninjacore_sys::BNGetPathRelativeToUserDirectory( - path.into_bytes_with_nul().as_ref().as_ptr() as *const std::os::raw::c_char, + path.into_bytes_with_nul().as_ref().as_ptr() as *const c_char, ) }; if s.is_null() { return Err(()); } - Ok(PathBuf::from( - unsafe { string::BnString::from_raw(s) }.to_string(), - )) + Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string())) } -pub fn version() -> string::BnString { - unsafe { string::BnString::from_raw(binaryninjacore_sys::BNGetVersionString()) } +pub fn version() -> BnString { + unsafe { BnString::from_raw(binaryninjacore_sys::BNGetVersionString()) } } pub fn plugin_abi_version() -> u32 { @@ -461,7 +441,7 @@ pub fn plugin_ui_abi_minimum_version() -> u32 { pub fn add_required_plugin_dependency(name: S) { unsafe { binaryninjacore_sys::BNAddRequiredPluginDependency( - name.into_bytes_with_nul().as_ref().as_ptr() as *const std::os::raw::c_char, + name.into_bytes_with_nul().as_ref().as_ptr() as *const c_char, ) }; } @@ -469,7 +449,7 @@ pub fn add_required_plugin_dependency(name: S) { pub fn add_optional_plugin_dependency(name: S) { unsafe { binaryninjacore_sys::BNAddOptionalPluginDependency( - name.into_bytes_with_nul().as_ref().as_ptr() as *const std::os::raw::c_char, + name.into_bytes_with_nul().as_ref().as_ptr() as *const c_char, ) }; } diff --git a/rust/src/logger.rs b/rust/src/logger.rs index 6e0b18870..faca50664 100644 --- a/rust/src/logger.rs +++ b/rust/src/logger.rs @@ -33,8 +33,7 @@ pub use binaryninjacore_sys::BNLogLevel as Level; use binaryninjacore_sys::{BNLogListener, BNUpdateLogListeners}; use log; -use std::ffi::CStr; -use std::os::raw::{c_char, c_void}; +use std::ffi::{c_char, c_void, CStr}; struct Logger; static LOGGER: Logger = Logger; diff --git a/rust/src/metadata.rs b/rust/src/metadata.rs index 9eeff8fad..d4e89988a 100644 --- a/rust/src/metadata.rs +++ b/rust/src/metadata.rs @@ -2,7 +2,7 @@ use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Guard, Ref, Re use crate::string::{BnStrCompatible, BnString, IntoJson}; use binaryninjacore_sys::*; use std::collections::HashMap; -use std::os::raw::c_char; +use std::ffi::c_char; use std::slice; pub type MetadataType = BNMetadataType; diff --git a/rust/src/relocation.rs b/rust/src/relocation.rs index f9ece0435..a4068aa20 100644 --- a/rust/src/relocation.rs +++ b/rust/src/relocation.rs @@ -9,8 +9,8 @@ use crate::{ }; use binaryninjacore_sys::*; use std::borrow::Borrow; +use std::ffi::c_void; use std::mem::MaybeUninit; -use std::os::raw::c_void; #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] pub enum RelocationType { @@ -500,7 +500,9 @@ where let name = name.into_bytes_with_nul(); - let raw = Box::leak(Box::new(MaybeUninit::>::zeroed())); + let raw = Box::leak(Box::new( + MaybeUninit::>::zeroed(), + )); let mut custom_handler = BNCustomRelocationHandler { context: raw.as_mut_ptr() as *mut _, freeObject: Some(cb_free::), diff --git a/rust/src/settings.rs b/rust/src/settings.rs index 3ec3e4063..f8297ce59 100644 --- a/rust/src/settings.rs +++ b/rust/src/settings.rs @@ -16,8 +16,8 @@ pub use binaryninjacore_sys::BNSettingsScope as SettingsScope; use binaryninjacore_sys::*; +use std::ffi::c_char; use std::iter::FromIterator; -use std::os::raw::c_char; use crate::binaryview::BinaryView; use crate::rc::*; diff --git a/rust/src/string.rs b/rust/src/string.rs index 936e30334..d4ad0225f 100644 --- a/rust/src/string.rs +++ b/rust/src/string.rs @@ -15,17 +15,16 @@ //! String wrappers for core-owned strings and strings being passed to the core use std::borrow::Cow; -use std::ffi::{CStr, CString}; +use std::ffi::{c_char, CStr, CString}; use std::fmt; use std::hash::{Hash, Hasher}; use std::mem; use std::ops::Deref; -use std::os::raw; use crate::rc::*; use crate::types::QualifiedName; -pub(crate) fn raw_to_string(ptr: *const raw::c_char) -> Option { +pub(crate) fn raw_to_string(ptr: *const c_char) -> Option { if ptr.is_null() { None } else { @@ -37,7 +36,7 @@ pub(crate) fn raw_to_string(ptr: *const raw::c_char) -> Option { /// functions provided by binaryninja_sys. #[repr(transparent)] pub struct BnString { - raw: *mut raw::c_char, + raw: *mut c_char, } /// A nul-terminated C string allocated by the core. @@ -65,11 +64,11 @@ impl BnString { } /// Construct a BnString from an owned const char* allocated by BNAllocString - pub(crate) unsafe fn from_raw(raw: *mut raw::c_char) -> Self { + pub(crate) unsafe fn from_raw(raw: *mut c_char) -> Self { Self { raw } } - pub(crate) fn into_raw(self) -> *mut raw::c_char { + pub(crate) fn into_raw(self) -> *mut c_char { let res = self.raw; // we're surrendering ownership over the *mut c_char to @@ -162,7 +161,7 @@ impl fmt::Debug for BnString { } impl CoreArrayProvider for BnString { - type Raw = *mut raw::c_char; + type Raw = *mut c_char; type Context = (); type Wrapped<'a> = &'a str; } diff --git a/rust/src/types.rs b/rust/src/types.rs index d5be0e864..2f6704b43 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -31,17 +31,15 @@ use crate::{ }; use lazy_static::lazy_static; -use std::ptr::null_mut; use std::{ borrow::{Borrow, Cow}, collections::{HashMap, HashSet}, - ffi::CStr, + ffi::{c_char, CStr}, fmt::{self, Debug, Display, Formatter}, hash::{Hash, Hasher}, iter::{zip, IntoIterator}, mem::{self, ManuallyDrop}, ops::Range, - os::raw::c_char, ptr, result, slice, sync::Mutex, }; @@ -1252,7 +1250,7 @@ impl fmt::Debug for Type { let mut name = QualifiedName::from(""); - let mut lines: *mut BNTypeDefinitionLine = null_mut(); + let mut lines: *mut BNTypeDefinitionLine = ptr::null_mut(); let mut count: usize = 0; unsafe {