diff options
| author | Mason Reed <mason@vector35.com> | 2025-05-07 19:22:21 -0400 |
|---|---|---|
| committer | Mason Reed <35282038+emesare@users.noreply.github.com> | 2025-05-12 17:45:24 -0400 |
| commit | 2f214f6c9935e8ce8df4732cde44a540a003258c (patch) | |
| tree | 6fe319433ef0d2ad75fcc58a50eaa632bb627ec9 /rust/src/binary_view.rs | |
| parent | b4cf0be8816182c9efca037e27e9439482f8bf36 (diff) | |
[Rust] Reduce usage of `IntoCStr` in function signatures
This is being done to reduce complexity in function signatures, specifically many of the strings we are passing ultimately should be new types themselves instead of "just strings", things such as type ids.
Another place which was confusing was dealing with filesystem related APIs, this commit turns most of those params into a stricter `Path` type.
This is bringing the rust api more inline with both python and C++, where the wrapper eagerly converts the string into the languages standard string type.
Special consideration must be made for symbols or other possible non utf-8 objects.
This commit will be followed up with one that adds the `IntoCStr` bound on API's we want to keep as invalid utf-8 so we can for example, get section by name on a section with invalid utf-8.
Diffstat (limited to 'rust/src/binary_view.rs')
| -rw-r--r-- | rust/src/binary_view.rs | 85 |
1 files changed, 40 insertions, 45 deletions
diff --git a/rust/src/binary_view.rs b/rust/src/binary_view.rs index 7259efbf..35383f0b 100644 --- a/rust/src/binary_view.rs +++ b/rust/src/binary_view.rs @@ -266,7 +266,7 @@ pub trait BinaryViewExt: BinaryViewBase { unsafe { BNGetEndOffset(self.as_ref().handle) } } - fn add_analysis_option(&self, name: impl IntoCStr) { + fn add_analysis_option(&self, name: &str) { let name = name.to_cstr(); unsafe { BNAddAnalysisOption(self.as_ref().handle, name.as_ptr()) } } @@ -399,7 +399,7 @@ pub trait BinaryViewExt: BinaryViewBase { } } - fn symbol_by_raw_name<S: IntoCStr>(&self, raw_name: S) -> Option<Ref<Symbol>> { + fn symbol_by_raw_name(&self, raw_name: impl IntoCStr) -> Option<Ref<Symbol>> { let raw_name = raw_name.to_cstr(); unsafe { @@ -424,7 +424,7 @@ pub trait BinaryViewExt: BinaryViewBase { } } - fn symbols_by_name<S: IntoCStr>(&self, name: S) -> Array<Symbol> { + fn symbols_by_name(&self, name: impl IntoCStr) -> Array<Symbol> { let raw_name = name.to_cstr(); unsafe { @@ -585,10 +585,10 @@ pub trait BinaryViewExt: BinaryViewBase { } } - fn define_auto_type<T: Into<QualifiedName>, S: IntoCStr>( + fn define_auto_type<T: Into<QualifiedName>>( &self, name: T, - source: S, + source: &str, type_obj: &Type, ) -> QualifiedName { let mut raw_name = QualifiedName::into_raw(name.into()); @@ -602,10 +602,10 @@ pub trait BinaryViewExt: BinaryViewBase { QualifiedName::from_owned_raw(name_handle) } - fn define_auto_type_with_id<T: Into<QualifiedName>, S: IntoCStr>( + fn define_auto_type_with_id<T: Into<QualifiedName>>( &self, name: T, - id: S, + id: &str, type_obj: &Type, ) -> QualifiedName { let mut raw_name = QualifiedName::into_raw(name.into()); @@ -712,7 +712,7 @@ pub trait BinaryViewExt: BinaryViewBase { } } - fn undefine_auto_type<S: IntoCStr>(&self, id: S) { + fn undefine_auto_type(&self, id: &str) { let id_str = id.to_cstr(); unsafe { BNUndefineAnalysisType(self.as_ref().handle, id_str.as_ref().as_ptr() as *const _); @@ -763,7 +763,7 @@ pub trait BinaryViewExt: BinaryViewBase { } } - fn type_by_id<S: IntoCStr>(&self, id: S) -> Option<Ref<Type>> { + fn type_by_id(&self, id: &str) -> Option<Ref<Type>> { let id_str = id.to_cstr(); unsafe { let type_handle = BNGetAnalysisTypeById(self.as_ref().handle, id_str.as_ptr()); @@ -774,7 +774,7 @@ pub trait BinaryViewExt: BinaryViewBase { } } - fn type_name_by_id<S: IntoCStr>(&self, id: S) -> Option<QualifiedName> { + fn type_name_by_id(&self, id: &str) -> Option<QualifiedName> { let id_str = id.to_cstr(); unsafe { let name_handle = BNGetAnalysisTypeNameById(self.as_ref().handle, id_str.as_ptr()); @@ -787,12 +787,12 @@ pub trait BinaryViewExt: BinaryViewBase { } } - fn type_id_by_name<T: Into<QualifiedName>>(&self, name: T) -> Option<BnString> { + fn type_id_by_name<T: Into<QualifiedName>>(&self, name: T) -> Option<String> { let mut raw_name = QualifiedName::into_raw(name.into()); unsafe { let id_cstr = BNGetAnalysisTypeId(self.as_ref().handle, &mut raw_name); QualifiedName::free_raw(raw_name); - let id = BnString::from_raw(id_cstr); + let id = BnString::into_string(id_cstr); match id.is_empty() { true => None, false => Some(id), @@ -871,7 +871,7 @@ pub trait BinaryViewExt: BinaryViewBase { section.create(self.as_ref()); } - fn remove_auto_section<S: IntoCStr>(&self, name: S) { + fn remove_auto_section(&self, name: &str) { let raw_name = name.to_cstr(); let raw_name_ptr = raw_name.as_ptr(); unsafe { @@ -879,7 +879,7 @@ pub trait BinaryViewExt: BinaryViewBase { } } - fn remove_user_section<S: IntoCStr>(&self, name: S) { + fn remove_user_section(&self, name: &str) { let raw_name = name.to_cstr(); let raw_name_ptr = raw_name.as_ptr(); unsafe { @@ -887,7 +887,7 @@ pub trait BinaryViewExt: BinaryViewBase { } } - fn section_by_name<S: IntoCStr>(&self, name: S) -> Option<Ref<Section>> { + fn section_by_name(&self, name: &str) -> Option<Ref<Section>> { unsafe { let raw_name = name.to_cstr(); let name_ptr = raw_name.as_ptr(); @@ -1103,14 +1103,14 @@ pub trait BinaryViewExt: BinaryViewBase { unsafe { BNApplyDebugInfo(self.as_ref().handle, debug_info.handle) } } - fn show_graph_report<S: IntoCStr>(&self, raw_name: S, graph: &FlowGraph) { + fn show_graph_report(&self, raw_name: &str, graph: &FlowGraph) { let raw_name = raw_name.to_cstr(); unsafe { BNShowGraphReport(self.as_ref().handle, raw_name.as_ptr(), graph.handle); } } - fn load_settings<S: IntoCStr>(&self, view_type_name: S) -> Result<Ref<Settings>> { + fn load_settings(&self, view_type_name: &str) -> Result<Ref<Settings>> { let view_type_name = view_type_name.to_cstr(); let settings_handle = unsafe { BNBinaryViewGetLoadSettings(self.as_ref().handle, view_type_name.as_ptr()) }; @@ -1122,7 +1122,7 @@ pub trait BinaryViewExt: BinaryViewBase { } } - fn set_load_settings<S: IntoCStr>(&self, view_type_name: S, settings: &Settings) { + fn set_load_settings(&self, view_type_name: &str, settings: &Settings) { let view_type_name = view_type_name.to_cstr(); unsafe { @@ -1139,7 +1139,7 @@ pub trait BinaryViewExt: BinaryViewBase { /// # Arguments /// * `name` - the name for the tag /// * `icon` - the icon (recommended 1 emoji or 2 chars) for the tag - fn create_tag_type<N: IntoCStr, I: IntoCStr>(&self, name: N, icon: I) -> Ref<TagType> { + fn create_tag_type(&self, name: &str, icon: &str) -> Ref<TagType> { let tag_type = TagType::create(self.as_ref(), name, icon); unsafe { BNAddTagType(self.as_ref().handle, tag_type.handle); @@ -1153,7 +1153,7 @@ pub trait BinaryViewExt: BinaryViewBase { } /// Get a tag type by its name. - fn tag_type_by_name<S: IntoCStr>(&self, name: S) -> Option<Ref<TagType>> { + fn tag_type_by_name(&self, name: &str) -> Option<Ref<TagType>> { let name = name.to_cstr(); unsafe { let handle = BNGetTagType(self.as_ref().handle, name.as_ptr()); @@ -1167,7 +1167,7 @@ pub trait BinaryViewExt: BinaryViewBase { /// Get a tag by its id. /// /// Note this does not tell you anything about where it is used. - fn tag_by_id<S: IntoCStr>(&self, id: S) -> Option<Ref<Tag>> { + fn tag_by_id(&self, id: &str) -> Option<Ref<Tag>> { let id = id.to_cstr(); unsafe { let handle = BNGetTag(self.as_ref().handle, id.as_ptr()); @@ -1181,7 +1181,7 @@ pub trait BinaryViewExt: BinaryViewBase { /// Creates and adds a tag to an address /// /// User tag creations will be added to the undo buffer - fn add_tag<S: IntoCStr>(&self, addr: u64, t: &TagType, data: S, user: bool) { + fn add_tag(&self, addr: u64, t: &TagType, data: &str, user: bool) { let tag = Tag::new(t, data); unsafe { BNAddTag(self.as_ref().handle, tag.handle, user) } @@ -1204,11 +1204,11 @@ pub trait BinaryViewExt: BinaryViewBase { unsafe { BNRemoveUserDataTag(self.as_ref().handle, addr, tag.handle) } } - fn comment_at(&self, addr: u64) -> Option<BnString> { + fn comment_at(&self, addr: u64) -> Option<String> { unsafe { let comment_raw = BNGetGlobalCommentForAddress(self.as_ref().handle, addr); match comment_raw.is_null() { - false => Some(BnString::from_raw(comment_raw)), + false => Some(BnString::into_string(comment_raw)), true => None, } } @@ -1218,7 +1218,7 @@ pub trait BinaryViewExt: BinaryViewBase { /// /// NOTE: This is different from setting a comment at the function-level. To set a comment in a /// function use [`Function::set_comment_at`] - fn set_comment_at(&self, addr: u64, comment: impl IntoCStr) { + fn set_comment_at(&self, addr: u64, comment: &str) { let comment_raw = comment.to_cstr(); unsafe { BNSetGlobalCommentForAddress(self.as_ref().handle, addr, comment_raw.as_ptr()) } } @@ -1271,7 +1271,7 @@ pub trait BinaryViewExt: BinaryViewBase { result } - fn query_metadata<S: IntoCStr>(&self, key: S) -> Option<Ref<Metadata>> { + fn query_metadata(&self, key: &str) -> Option<Ref<Metadata>> { let key = key.to_cstr(); let value: *mut BNMetadata = unsafe { BNBinaryViewQueryMetadata(self.as_ref().handle, key.as_ptr()) }; @@ -1282,7 +1282,7 @@ pub trait BinaryViewExt: BinaryViewBase { } } - fn get_metadata<T, S: IntoCStr>(&self, key: S) -> Option<Result<T>> + fn get_metadata<T>(&self, key: &str) -> Option<Result<T>> where T: for<'a> TryFrom<&'a Metadata>, { @@ -1290,7 +1290,7 @@ pub trait BinaryViewExt: BinaryViewBase { .map(|md| T::try_from(md.as_ref()).map_err(|_| ())) } - fn store_metadata<V, S: IntoCStr>(&self, key: S, value: V, is_auto: bool) + fn store_metadata<V>(&self, key: &str, value: V, is_auto: bool) where V: Into<Ref<Metadata>>, { @@ -1306,7 +1306,7 @@ pub trait BinaryViewExt: BinaryViewBase { }; } - fn remove_metadata<S: IntoCStr>(&self, key: S) { + fn remove_metadata(&self, key: &str) { let key = key.to_cstr(); unsafe { BNBinaryViewRemoveMetadata(self.as_ref().handle, key.as_ptr()) }; } @@ -1432,7 +1432,7 @@ pub trait BinaryViewExt: BinaryViewBase { .collect() } - fn component_by_guid<S: IntoCStr>(&self, guid: S) -> Option<Ref<Component>> { + fn component_by_guid(&self, guid: &str) -> Option<Ref<Component>> { let name = guid.to_cstr(); let result = unsafe { BNGetComponentByGuid(self.as_ref().handle, name.as_ptr()) }; NonNull::new(result).map(|h| unsafe { Component::ref_from_raw(h) }) @@ -1443,7 +1443,7 @@ pub trait BinaryViewExt: BinaryViewBase { NonNull::new(result).map(|h| unsafe { Component::ref_from_raw(h) }) } - fn component_by_path<P: IntoCStr>(&self, path: P) -> Option<Ref<Component>> { + fn component_by_path(&self, path: &str) -> Option<Ref<Component>> { let path = path.to_cstr(); let result = unsafe { BNGetComponentByPath(self.as_ref().handle, path.as_ptr()) }; NonNull::new(result).map(|h| unsafe { Component::ref_from_raw(h) }) @@ -1453,7 +1453,7 @@ pub trait BinaryViewExt: BinaryViewBase { unsafe { BNRemoveComponent(self.as_ref().handle, component.handle.as_ptr()) } } - fn remove_component_by_guid<P: IntoCStr>(&self, guid: P) -> bool { + fn remove_component_by_guid(&self, guid: &str) -> bool { let path = guid.to_cstr(); unsafe { BNRemoveComponentByGuid(self.as_ref().handle, path.as_ptr()) } } @@ -1476,7 +1476,7 @@ pub trait BinaryViewExt: BinaryViewBase { unsafe { Array::new(result, count, ()) } } - fn external_library<S: IntoCStr>(&self, name: S) -> Option<Ref<ExternalLibrary>> { + fn external_library(&self, name: &str) -> Option<Ref<ExternalLibrary>> { let name_ptr = name.to_cstr(); let result = unsafe { BNBinaryViewGetExternalLibrary(self.as_ref().handle, name_ptr.as_ptr()) }; @@ -1484,14 +1484,14 @@ pub trait BinaryViewExt: BinaryViewBase { Some(unsafe { ExternalLibrary::ref_from_raw(result_ptr) }) } - fn remove_external_library<S: IntoCStr>(&self, name: S) { + fn remove_external_library(&self, name: &str) { let name_ptr = name.to_cstr(); unsafe { BNBinaryViewRemoveExternalLibrary(self.as_ref().handle, name_ptr.as_ptr()) }; } - fn add_external_library<S: IntoCStr>( + fn add_external_library( &self, - name: S, + name: &str, backing_file: Option<&ProjectFile>, auto: bool, ) -> Option<Ref<ExternalLibrary>> { @@ -1531,11 +1531,11 @@ pub trait BinaryViewExt: BinaryViewBase { } // TODO: This is awful, rewrite this. - fn add_external_location<S: IntoCStr>( + fn add_external_location( &self, symbol: &Symbol, library: &ExternalLibrary, - target_symbol_name: S, + target_symbol_name: &str, target_address: Option<u64>, target_is_auto: bool, ) -> Option<Ref<ExternalLocation>> { @@ -1589,7 +1589,7 @@ pub trait BinaryViewExt: BinaryViewBase { unsafe { BNAddBinaryViewTypeLibrary(self.as_ref().handle, library.as_raw()) } } - fn type_library_by_name<S: IntoCStr>(&self, name: S) -> Option<TypeLibrary> { + fn type_library_by_name(&self, name: &str) -> Option<TypeLibrary> { let name = name.to_cstr(); let result = unsafe { BNGetBinaryViewTypeLibrary(self.as_ref().handle, name.as_ptr()) }; NonNull::new(result).map(|h| unsafe { TypeLibrary::from_raw(h) }) @@ -1677,12 +1677,7 @@ pub trait BinaryViewExt: BinaryViewBase { } /// Recursively imports a type interface given its GUID. - /// - /// .. note:: To support this type of lookup a type library must have - /// contain a metadata key called "type_guids" which is a map - /// Dict[string_guid, string_type_name] or - /// Dict[string_guid, Tuple[string_type_name, type_library_name]] - fn import_type_by_guid<S: IntoCStr>(&self, guid: S) -> Option<Ref<Type>> { + fn import_type_by_guid(&self, guid: &str) -> Option<Ref<Type>> { let guid = guid.to_cstr(); let result = unsafe { BNBinaryViewImportTypeLibraryTypeByGuid(self.as_ref().handle, guid.as_ptr()) }; |
