diff options
| author | Mason Reed <mason@vector35.com> | 2025-05-04 15:38:54 -0400 |
|---|---|---|
| committer | Mason Reed <35282038+emesare@users.noreply.github.com> | 2025-05-12 17:45:24 -0400 |
| commit | e180c955f9397849bdb1ea08f1e913ebac71ed5d (patch) | |
| tree | a6c8e309e3a723c04e2d5085c0449bd8f3a10db5 /rust/src | |
| parent | bb68ef5ad6c3e6a391bc884763231ed3291a5f9e (diff) | |
[Rust] More cleanup
Diffstat (limited to 'rust/src')
| -rw-r--r-- | rust/src/architecture.rs | 1 | ||||
| -rw-r--r-- | rust/src/debuginfo.rs | 15 | ||||
| -rw-r--r-- | rust/src/function.rs | 40 | ||||
| -rw-r--r-- | rust/src/interaction.rs | 2 | ||||
| -rw-r--r-- | rust/src/lib.rs | 30 | ||||
| -rw-r--r-- | rust/src/linear_view.rs | 2 | ||||
| -rw-r--r-- | rust/src/platform.rs | 2 | ||||
| -rw-r--r-- | rust/src/string.rs | 2 | ||||
| -rw-r--r-- | rust/src/type_archive.rs | 16 | ||||
| -rw-r--r-- | rust/src/type_parser.rs | 6 | ||||
| -rw-r--r-- | rust/src/type_printer.rs | 2 | ||||
| -rw-r--r-- | rust/src/types.rs | 11 | ||||
| -rw-r--r-- | rust/src/update.rs | 6 | ||||
| -rw-r--r-- | rust/src/variable.rs | 4 | ||||
| -rw-r--r-- | rust/src/websocket.rs | 2 |
15 files changed, 77 insertions, 64 deletions
diff --git a/rust/src/architecture.rs b/rust/src/architecture.rs index d2e5e0fc..ee8033a2 100644 --- a/rust/src/architecture.rs +++ b/rust/src/architecture.rs @@ -1810,6 +1810,7 @@ impl Architecture for CoreArchitecture { Ok(result) => result, Err(_) => return Err("Result buffer allocation failed".to_string()), }; + // TODO: This is actually a list of errors. let mut error_raw: *mut c_char = std::ptr::null_mut(); let res = unsafe { BNAssemble( diff --git a/rust/src/debuginfo.rs b/rust/src/debuginfo.rs index bafa9cc4..c4bc7add 100644 --- a/rust/src/debuginfo.rs +++ b/rust/src/debuginfo.rs @@ -807,15 +807,8 @@ impl DebugInfo { } for local_variable in &new_func.local_variables { - local_variables_array.push(BNVariableNameAndType { - var: local_variable.variable.into(), - 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, - }); + // NOTE: must be manually freed after call to BNAddDebugFunction is over. + local_variables_array.push(NamedVariableWithType::into_raw(local_variable.clone())); } let result = BNAddDebugFunction( @@ -841,11 +834,11 @@ impl DebugInfo { ); for i in components_array { - BNFreeString(i); + BnString::free_raw(i); } for i in &local_variables_array { - BNFreeString(i.name); + NamedVariableWithType::free_raw(*i); } result } diff --git a/rust/src/function.rs b/rust/src/function.rs index 53fec505..3eea3a3f 100644 --- a/rust/src/function.rs +++ b/rust/src/function.rs @@ -49,7 +49,7 @@ use crate::variable::{ use crate::workflow::Workflow; use std::fmt::{Debug, Formatter}; use std::ptr::NonNull; -use std::time::Duration; +use std::time::{Duration, UNIX_EPOCH}; use std::{ffi::c_char, hash::Hash, ops::Range}; /// Used to describe a location within a [`Function`]. @@ -259,7 +259,7 @@ impl FunctionViewType { } pub(crate) fn free_raw(value: BNFunctionViewType) { - let _ = unsafe { BnString::from_raw(value.name as *mut _) }; + unsafe { BnString::free_raw(value.name as *mut _) }; } } @@ -2520,23 +2520,38 @@ pub struct PerformanceInfo { pub seconds: Duration, } -impl From<BNPerformanceInfo> for PerformanceInfo { - fn from(value: BNPerformanceInfo) -> Self { +impl PerformanceInfo { + pub fn new(name: String, seconds: Duration) -> Self { Self { - name: unsafe { BnString::from_raw(value.name) }.to_string(), - seconds: Duration::from_secs_f64(value.seconds), + name: name.to_string(), + seconds, } } -} -impl From<&BNPerformanceInfo> for PerformanceInfo { - fn from(value: &BNPerformanceInfo) -> Self { + pub(crate) fn from_raw(value: &BNPerformanceInfo) -> Self { Self { - // TODO: Name will be freed by this. FIX! - name: unsafe { BnString::from_raw(value.name) }.to_string(), + name: raw_to_string(value.name as *mut _).unwrap(), seconds: Duration::from_secs_f64(value.seconds), } } + + pub(crate) fn from_owned_raw(value: BNPerformanceInfo) -> Self { + let owned = Self::from_raw(&value); + Self::free_raw(value); + owned + } + + pub(crate) fn into_raw(value: Self) -> BNPerformanceInfo { + let bn_name = BnString::new(value.name); + BNPerformanceInfo { + name: BnString::into_raw(bn_name), + seconds: value.seconds.as_secs_f64(), + } + } + + pub(crate) fn free_raw(value: BNPerformanceInfo) { + unsafe { BnString::free_raw(value.name) }; + } } impl CoreArrayProvider for PerformanceInfo { @@ -2551,8 +2566,7 @@ unsafe impl CoreArrayProviderInner for PerformanceInfo { } unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> { - // TODO: Swap this to the ref version. - Self::from(*raw) + Self::from_raw(raw) } } diff --git a/rust/src/interaction.rs b/rust/src/interaction.rs index 4a74127b..23dcaf85 100644 --- a/rust/src/interaction.rs +++ b/rust/src/interaction.rs @@ -37,7 +37,7 @@ pub fn get_text_line_input(prompt: &str, title: &str) -> Option<String> { return None; } - Some(unsafe { BnString::from_raw(value).to_string() }) + Some(unsafe { BnString::into_string(value) }) } pub fn get_integer_input(prompt: &str, title: &str) -> Option<i64> { diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 0446fe60..4d6b97b4 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -279,8 +279,8 @@ where pub fn install_directory() -> PathBuf { let install_dir_ptr: *mut c_char = unsafe { BNGetInstallDirectory() }; assert!(!install_dir_ptr.is_null()); - let bn_install_dir = unsafe { BnString::from_raw(install_dir_ptr) }; - PathBuf::from(bn_install_dir.to_string()) + let install_dir_str = unsafe { BnString::into_string(install_dir_ptr) }; + PathBuf::from(install_dir_str) } pub fn bundled_plugin_directory() -> Result<PathBuf, ()> { @@ -288,7 +288,7 @@ pub fn bundled_plugin_directory() -> Result<PathBuf, ()> { if s.is_null() { return Err(()); } - Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string())) + Ok(PathBuf::from(unsafe { BnString::into_string(s) })) } pub fn set_bundled_plugin_directory(new_dir: impl AsRef<Path>) { @@ -299,8 +299,8 @@ pub fn set_bundled_plugin_directory(new_dir: impl AsRef<Path>) { pub fn user_directory() -> PathBuf { let user_dir_ptr: *mut c_char = unsafe { BNGetUserDirectory() }; assert!(!user_dir_ptr.is_null()); - let bn_user_dir = unsafe { BnString::from_raw(user_dir_ptr) }; - PathBuf::from(bn_user_dir.to_string()) + let user_dir_str = unsafe { BnString::into_string(user_dir_ptr) }; + PathBuf::from(user_dir_str) } pub fn user_plugin_directory() -> Result<PathBuf, ()> { @@ -308,7 +308,8 @@ pub fn user_plugin_directory() -> Result<PathBuf, ()> { if s.is_null() { return Err(()); } - Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string())) + let user_plugin_dir_str = unsafe { BnString::into_string(s) }; + Ok(PathBuf::from(user_plugin_dir_str)) } pub fn repositories_directory() -> Result<PathBuf, ()> { @@ -316,14 +317,15 @@ pub fn repositories_directory() -> Result<PathBuf, ()> { if s.is_null() { return Err(()); } - Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string())) + let repo_dir_str = unsafe { BnString::into_string(s) }; + Ok(PathBuf::from(repo_dir_str)) } -pub fn settings_file_name() -> PathBuf { +pub fn settings_file_path() -> PathBuf { let settings_file_name_ptr: *mut c_char = unsafe { BNGetSettingsFileName() }; assert!(!settings_file_name_ptr.is_null()); - let bn_settings_file_name = unsafe { BnString::from_raw(settings_file_name_ptr) }; - PathBuf::from(bn_settings_file_name.to_string()) + let settings_file_path_str = unsafe { BnString::into_string(settings_file_name_ptr) }; + PathBuf::from(settings_file_path_str) } /// Write the installation directory of the currently running core instance to disk. @@ -340,7 +342,7 @@ pub fn path_relative_to_bundled_plugin_directory(path: impl AsRef<Path>) -> Resu if s.is_null() { return Err(()); } - Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string())) + Ok(PathBuf::from(unsafe { BnString::into_string(s) })) } pub fn path_relative_to_user_plugin_directory(path: impl AsRef<Path>) -> Result<PathBuf, ()> { @@ -350,7 +352,7 @@ pub fn path_relative_to_user_plugin_directory(path: impl AsRef<Path>) -> Result< if s.is_null() { return Err(()); } - Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string())) + Ok(PathBuf::from(unsafe { BnString::into_string(s) })) } pub fn path_relative_to_user_directory(path: impl AsRef<Path>) -> Result<PathBuf, ()> { @@ -360,7 +362,7 @@ pub fn path_relative_to_user_directory(path: impl AsRef<Path>) -> Result<PathBuf if s.is_null() { return Err(()); } - Ok(PathBuf::from(unsafe { BnString::from_raw(s) }.to_string())) + Ok(PathBuf::from(unsafe { BnString::into_string(s) })) } /// Returns if the running thread is the "main thread" @@ -476,7 +478,7 @@ impl VersionInfo { } pub(crate) fn free_raw(value: BNVersionInfo) { - let _ = unsafe { BnString::from_raw(value.channel) }; + unsafe { BnString::free_raw(value.channel) }; } pub fn from_string<S: BnStrCompatible>(string: S) -> Self { diff --git a/rust/src/linear_view.rs b/rust/src/linear_view.rs index ca35d525..db164278 100644 --- a/rust/src/linear_view.rs +++ b/rust/src/linear_view.rs @@ -267,7 +267,7 @@ impl LinearViewObjectIdentifier { } pub fn free_raw(value: BNLinearViewObjectIdentifier) { - let _ = unsafe { BnString::from_raw(value.name) }; + unsafe { BnString::free_raw(value.name) }; } } diff --git a/rust/src/platform.rs b/rust/src/platform.rs index 6d2b30f0..65138c19 100644 --- a/rust/src/platform.rs +++ b/rust/src/platform.rs @@ -386,7 +386,7 @@ impl Platform { assert!(!error_string.is_null()); Err(TypeParserError::new( TypeParserErrorSeverity::FatalSeverity, - unsafe { BnString::from_raw(error_string) }.to_string(), + unsafe { BnString::into_string(error_string) }, filename.to_string(), 0, 0, diff --git a/rust/src/string.rs b/rust/src/string.rs index 3cdbf384..5bd871f9 100644 --- a/rust/src/string.rs +++ b/rust/src/string.rs @@ -80,7 +80,7 @@ impl BnString { pub(crate) unsafe fn from_raw(raw: *mut c_char) -> Self { Self { raw } } - + /// Free a raw string allocated by BNAllocString. pub(crate) unsafe fn free_raw(raw: *mut c_char) { use binaryninjacore_sys::BNFreeString; diff --git a/rust/src/type_archive.rs b/rust/src/type_archive.rs index 2c8caa39..ee4bc276 100644 --- a/rust/src/type_archive.rs +++ b/rust/src/type_archive.rs @@ -112,8 +112,8 @@ impl TypeArchive { let result = unsafe { BNGetTypeArchivePath(self.handle.as_ptr()) }; match result.is_null() { false => { - let bn_res = unsafe { BnString::from_raw(result) }; - Some(PathBuf::from(bn_res.to_string())) + let path_str = unsafe { BnString::into_string(result) }; + Some(PathBuf::from(path_str)) } true => None, } @@ -136,7 +136,8 @@ impl TypeArchive { pub fn current_snapshot_id(&self) -> TypeArchiveSnapshotId { let result = unsafe { BNGetTypeArchiveCurrentSnapshotId(self.handle.as_ptr()) }; assert!(!result.is_null()); - TypeArchiveSnapshotId(unsafe { BnString::from_raw(result) }.to_string()) + let id = unsafe { BnString::into_string(result) }; + TypeArchiveSnapshotId(id) } /// Revert the type archive's current snapshot to the given snapshot @@ -651,7 +652,8 @@ impl TypeArchive { let result = unsafe { BNTypeArchiveDeserializeSnapshot(self.handle.as_ptr(), data.as_raw()) }; assert!(!result.is_null()); - TypeArchiveSnapshotId(unsafe { BnString::from_raw(result) }.to_string()) + let id = unsafe { BnString::into_string(result) }; + TypeArchiveSnapshotId(id) } /// Register a notification listener @@ -1149,21 +1151,21 @@ impl TypeArchiveMergeConflict { pub fn base_snapshot_id(&self) -> TypeArchiveSnapshotId { let value = unsafe { BNTypeArchiveMergeConflictGetBaseSnapshotId(self.handle.as_ptr()) }; assert!(!value.is_null()); - let id = unsafe { BnString::from_raw(value) }.to_string(); + let id = unsafe { BnString::into_string(value) }; TypeArchiveSnapshotId(id) } pub fn first_snapshot_id(&self) -> TypeArchiveSnapshotId { let value = unsafe { BNTypeArchiveMergeConflictGetFirstSnapshotId(self.handle.as_ptr()) }; assert!(!value.is_null()); - let id = unsafe { BnString::from_raw(value) }.to_string(); + let id = unsafe { BnString::into_string(value) }; TypeArchiveSnapshotId(id) } pub fn second_snapshot_id(&self) -> TypeArchiveSnapshotId { let value = unsafe { BNTypeArchiveMergeConflictGetSecondSnapshotId(self.handle.as_ptr()) }; assert!(!value.is_null()); - let id = unsafe { BnString::from_raw(value) }.to_string(); + let id = unsafe { BnString::into_string(value) }; TypeArchiveSnapshotId(id) } diff --git a/rust/src/type_parser.rs b/rust/src/type_parser.rs index 22c4ba76..dbf5a709 100644 --- a/rust/src/type_parser.rs +++ b/rust/src/type_parser.rs @@ -323,8 +323,8 @@ impl TypeParserError { } pub(crate) fn free_raw(value: BNTypeParserError) { - let _ = unsafe { BnString::from_raw(value.message) }; - let _ = unsafe { BnString::from_raw(value.fileName) }; + unsafe { BnString::free_raw(value.message) }; + unsafe { BnString::free_raw(value.fileName) }; } pub fn new( @@ -677,7 +677,7 @@ unsafe extern "C" fn cb_parse_type_string<T: TypeParser>( unsafe extern "C" fn cb_free_string(_ctxt: *mut c_void, string: *mut c_char) { // SAFETY: The returned string is just BnString - let _ = BnString::from_raw(string); + BnString::free_raw(string); } unsafe extern "C" fn cb_free_result(_ctxt: *mut c_void, result: *mut BNTypeParserResult) { diff --git a/rust/src/type_printer.rs b/rust/src/type_printer.rs index 124079ec..67d36570 100644 --- a/rust/src/type_printer.rs +++ b/rust/src/type_printer.rs @@ -951,7 +951,7 @@ unsafe extern "C" fn cb_print_all_types<T: TypePrinter>( unsafe extern "C" fn cb_free_string(_ctxt: *mut c_void, string: *mut c_char) { // SAFETY: The returned string is just BnString - let _ = BnString::from_raw(string); + BnString::free_raw(string); } unsafe extern "C" fn cb_free_tokens( diff --git a/rust/src/types.rs b/rust/src/types.rs index 8dbeeb48..a46dce2e 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -934,7 +934,8 @@ impl Type { pub fn generate_auto_demangled_type_id<T: Into<QualifiedName>>(name: T) -> String { let mut raw_name = QualifiedName::into_raw(name.into()); - let type_id = unsafe { BnString::into_string(BNGenerateAutoDemangledTypeId(&mut raw_name)) }; + let type_id = + unsafe { BnString::into_string(BNGenerateAutoDemangledTypeId(&mut raw_name)) }; QualifiedName::free_raw(raw_name); type_id } @@ -1109,7 +1110,7 @@ impl FunctionParameter { } pub(crate) fn free_raw(value: BNFunctionParameter) { - let _ = unsafe { BnString::from_raw(value.name) }; + unsafe { BnString::free_raw(value.name) }; let _ = unsafe { Type::ref_from_raw(value.type_) }; } @@ -1184,7 +1185,7 @@ impl EnumerationMember { } pub(crate) fn free_raw(value: BNEnumerationMember) { - let _ = unsafe { BnString::from_raw(value.name) }; + unsafe { BnString::free_raw(value.name) }; } pub fn new(name: String, value: u64, default: bool) -> Self { @@ -1723,7 +1724,7 @@ impl StructureMember { pub(crate) fn free_raw(value: BNStructureMember) { let _ = unsafe { Type::ref_from_raw(value.type_) }; - let _ = unsafe { BnString::from_raw(value.name) }; + unsafe { BnString::free_raw(value.name) }; } pub fn new( @@ -1987,7 +1988,7 @@ impl QualifiedName { } pub(crate) fn free_raw(value: BNQualifiedName) { - unsafe { BNFreeString(value.join) }; + unsafe { BnString::free_raw(value.join) }; unsafe { BNFreeStringList(value.name, value.nameCount) }; } diff --git a/rust/src/update.rs b/rust/src/update.rs index 9afb1218..aa8c7daf 100644 --- a/rust/src/update.rs +++ b/rust/src/update.rs @@ -75,9 +75,9 @@ impl UpdateChannel { } pub(crate) fn free_raw(value: BNUpdateChannel) { - let _ = unsafe { BnString::from_raw(value.name) }; - let _ = unsafe { BnString::from_raw(value.description) }; - let _ = unsafe { BnString::from_raw(value.latestVersion) }; + unsafe { BnString::free_raw(value.name) }; + unsafe { BnString::free_raw(value.description) }; + unsafe { BnString::free_raw(value.latestVersion) }; } pub fn all() -> Result<Array<UpdateChannel>, BnString> { diff --git a/rust/src/variable.rs b/rust/src/variable.rs index f6b0f522..c0cab7d5 100644 --- a/rust/src/variable.rs +++ b/rust/src/variable.rs @@ -186,7 +186,7 @@ impl NamedVariableWithType { pub(crate) fn free_raw(value: BNVariableNameAndType) { let _ = unsafe { Type::ref_from_raw(value.type_) }; - let _ = unsafe { BnString::from_raw(value.name) }; + unsafe { BnString::free_raw(value.name) }; } pub fn new(variable: Variable, ty: Conf<Ref<Type>>, name: String, auto_defined: bool) -> Self { @@ -307,7 +307,7 @@ impl StackVariableReference { pub(crate) fn free_raw(value: BNStackVariableReference) { let _ = unsafe { Type::ref_from_raw(value.type_) }; - let _ = unsafe { BnString::from_raw(value.name) }; + unsafe { BnString::free_raw(value.name) }; } } diff --git a/rust/src/websocket.rs b/rust/src/websocket.rs index cc1b5f77..4acf10a6 100644 --- a/rust/src/websocket.rs +++ b/rust/src/websocket.rs @@ -1,7 +1,7 @@ //! Interface for registering new websocket providers //! //! WARNING: Do _not_ use this for anything other than provider registration. If you need to open a -//! websocket connection use a real websocket library. +//! websocket connection, use a real websocket library. mod client; mod provider; |
