@@ -924,31 +924,40 @@ pub trait BinaryViewExt: BinaryViewBase {
924924 MemoryMap :: new ( self . as_ref ( ) . to_owned ( ) )
925925 }
926926
927- fn add_auto_function ( & self , plat : & Platform , addr : u64 ) -> Option < Ref < Function > > {
928- unsafe {
929- let handle = BNAddFunctionForAnalysis (
930- self . as_ref ( ) . handle ,
931- plat. handle ,
932- addr,
933- false ,
934- std:: ptr:: null_mut ( ) ,
935- ) ;
936-
937- if handle. is_null ( ) {
938- return None ;
939- }
927+ /// Add an auto function at the given `address` with the views default platform.
928+ ///
929+ /// NOTE: The default platform **must** be set for this view!
930+ fn add_auto_function ( & self , address : u64 ) -> Option < Ref < Function > > {
931+ let platform = self . default_platform ( ) ?;
932+ self . add_auto_function_with_platform ( address, & platform)
933+ }
940934
941- Some ( Function :: ref_from_raw ( handle) )
942- }
935+ /// Add an auto function at the given `address` with the `platform`.
936+ ///
937+ /// NOTE: If the view's default platform is not set, this will set it to `platform`.
938+ fn add_auto_function_with_platform (
939+ & self ,
940+ address : u64 ,
941+ platform : & Platform ,
942+ ) -> Option < Ref < Function > > {
943+ self . add_auto_function_ext ( address, platform, None )
943944 }
944945
945- fn add_function_with_type (
946+ /// Add an auto function at the given `address` with the `platform` and function type.
947+ ///
948+ /// NOTE: If the view's default platform is not set, this will set it to `platform`.
949+ fn add_auto_function_ext (
946950 & self ,
947- plat : & Platform ,
948- addr : u64 ,
949- auto_discovered : bool ,
951+ address : u64 ,
952+ platform : & Platform ,
950953 func_type : Option < & Type > ,
951954 ) -> Option < Ref < Function > > {
955+ // If the view does not have a default platform, we must set it.
956+ // The core expects there to be one before it can create a function.
957+ if self . default_platform ( ) . is_none ( ) {
958+ self . set_default_platform ( platform) ;
959+ }
960+
952961 unsafe {
953962 let func_type = match func_type {
954963 Some ( func_type) => func_type. handle ,
@@ -957,9 +966,9 @@ pub trait BinaryViewExt: BinaryViewBase {
957966
958967 let handle = BNAddFunctionForAnalysis (
959968 self . as_ref ( ) . handle ,
960- plat . handle ,
961- addr ,
962- auto_discovered ,
969+ platform . handle ,
970+ address ,
971+ true ,
963972 func_type,
964973 ) ;
965974
@@ -971,28 +980,85 @@ pub trait BinaryViewExt: BinaryViewBase {
971980 }
972981 }
973982
974- fn add_entry_point ( & self , plat : & Platform , addr : u64 ) {
983+ /// Remove an auto function from the view.
984+ ///
985+ /// Pass `true` for `update_refs` to update all references of the function.
986+ ///
987+ /// NOTE: Unlike [`BinaryViewExt::remove_user_function`], this will NOT prohibit the function from
988+ /// being re-added in the future, use [`BinaryViewExt::remove_user_function`] to blacklist the
989+ /// function from being automatically created.
990+ fn remove_auto_function ( & self , func : & Function , update_refs : bool ) {
975991 unsafe {
976- BNAddEntryPointForAnalysis ( self . as_ref ( ) . handle , plat . handle , addr ) ;
992+ BNRemoveAnalysisFunction ( self . as_ref ( ) . handle , func . handle , update_refs ) ;
977993 }
978994 }
979995
980- fn create_user_function ( & self , plat : & Platform , addr : u64 ) -> Result < Ref < Function > > {
981- unsafe {
982- let func = BNCreateUserFunction ( self . as_ref ( ) . handle , plat. handle , addr) ;
996+ /// Add a user function at the given `address` with the views default platform.
997+ ///
998+ /// NOTE: The default platform **must** be set for this view!
999+ fn add_user_function ( & self , addr : u64 ) -> Option < Ref < Function > > {
1000+ let platform = self . default_platform ( ) ?;
1001+ self . add_user_function_with_platform ( addr, & platform)
1002+ }
9831003
1004+ /// Add an auto function at the given `address` with the `platform`.
1005+ ///
1006+ /// NOTE: If the view's default platform is not set, this will set it to `platform`.
1007+ fn add_user_function_with_platform (
1008+ & self ,
1009+ addr : u64 ,
1010+ platform : & Platform ,
1011+ ) -> Option < Ref < Function > > {
1012+ // If the view does not have a default platform, we must set it.
1013+ // The core expects there to be one before it can create a function.
1014+ if self . default_platform ( ) . is_none ( ) {
1015+ self . set_default_platform ( platform) ;
1016+ }
1017+
1018+ unsafe {
1019+ let func = BNCreateUserFunction ( self . as_ref ( ) . handle , platform. handle , addr) ;
9841020 if func. is_null ( ) {
985- return Err ( ( ) ) ;
1021+ return None ;
9861022 }
987-
988- Ok ( Function :: ref_from_raw ( func) )
1023+ Some ( Function :: ref_from_raw ( func) )
9891024 }
9901025 }
9911026
1027+ /// Removes the function from the view and blacklists it from being created automatically.
1028+ ///
1029+ /// NOTE: If you call [`BinaryViewExt::add_user_function`], it will override the blacklist.
1030+ fn remove_user_function ( & self , func : & Function ) {
1031+ unsafe { BNRemoveUserFunction ( self . as_ref ( ) . handle , func. handle ) }
1032+ }
1033+
9921034 fn has_functions ( & self ) -> bool {
9931035 unsafe { BNHasFunctions ( self . as_ref ( ) . handle ) }
9941036 }
9951037
1038+ /// Add an entry point at the given `address` with the view's default platform.
1039+ ///
1040+ /// NOTE: The default platform **must** be set for this view!
1041+ fn add_entry_point ( & self , addr : u64 ) {
1042+ if let Some ( platform) = self . default_platform ( ) {
1043+ self . add_entry_point_with_platform ( addr, & platform) ;
1044+ }
1045+ }
1046+
1047+ /// Add an entry point at the given `address` with the `platform`.
1048+ ///
1049+ /// NOTE: If the view's default platform is not set, this will set it to `platform`.
1050+ fn add_entry_point_with_platform ( & self , addr : u64 , platform : & Platform ) {
1051+ // If the view does not have a default platform, we must set it.
1052+ // The core expects there to be one before it can create a function.
1053+ if self . default_platform ( ) . is_none ( ) {
1054+ self . set_default_platform ( platform) ;
1055+ }
1056+
1057+ unsafe {
1058+ BNAddEntryPointForAnalysis ( self . as_ref ( ) . handle , platform. handle , addr) ;
1059+ }
1060+ }
1061+
9961062 fn entry_point_function ( & self ) -> Option < Ref < Function > > {
9971063 unsafe {
9981064 let raw_func_ptr = BNGetAnalysisEntryPoint ( self . as_ref ( ) . handle ) ;
0 commit comments