@@ -14,53 +14,33 @@ See the License for the specific language governing permissions and
1414limitations under the License.
1515*/
1616
17+ use std:: collections:: HashMap ;
1718use std:: io:: { IsTerminal , Write } ;
1819
1920use hyperlight_common:: flatbuffer_wrappers:: function_types:: { ParameterValue , ReturnValue } ;
2021use termcolor:: { Color , ColorChoice , ColorSpec , StandardStream , WriteColor } ;
2122use tracing:: { instrument, Span } ;
2223
23- use super :: { ExtraAllowedSyscall , FunctionsMap } ;
24- use crate :: func:: host_functions:: HostFunctionDefinition ;
24+ use super :: ExtraAllowedSyscall ;
2525use crate :: func:: HyperlightFunction ;
2626use crate :: HyperlightError :: HostFunctionNotFound ;
2727use crate :: { new_error, Result } ;
2828
29- type HostFunctionDetails = Option < Vec < HostFunctionDefinition > > ;
30-
3129#[ derive( Default , Clone ) ]
3230/// A Wrapper around details of functions exposed by the Host
3331pub struct HostFuncsWrapper {
34- functions_map : FunctionsMap ,
35- function_details : HostFunctionDetails ,
32+ functions_map : HashMap < String , ( HyperlightFunction , Option < Vec < ExtraAllowedSyscall > > ) > ,
3633}
3734
3835impl HostFuncsWrapper {
39- #[ instrument( skip_all, parent = Span :: current( ) , level = "Trace" ) ]
40- fn get_host_funcs ( & self ) -> & FunctionsMap {
41- & self . functions_map
42- }
43- #[ instrument( skip_all, parent = Span :: current( ) , level = "Trace" ) ]
44- fn get_host_funcs_mut ( & mut self ) -> & mut FunctionsMap {
45- & mut self . functions_map
46- }
47- #[ instrument( skip_all, parent = Span :: current( ) , level = "Trace" ) ]
48- fn get_host_func_details ( & self ) -> & HostFunctionDetails {
49- & self . function_details
50- }
51- #[ instrument( skip_all, parent = Span :: current( ) , level = "Trace" ) ]
52- fn get_host_func_details_mut ( & mut self ) -> & mut HostFunctionDetails {
53- & mut self . function_details
54- }
55-
5636 /// Register a host function with the sandbox.
5737 #[ instrument( err( Debug ) , skip_all, parent = Span :: current( ) , level = "Trace" ) ]
5838 pub ( crate ) fn register_host_function (
5939 & mut self ,
60- hfd : & HostFunctionDefinition ,
40+ name : String ,
6141 func : HyperlightFunction ,
6242 ) -> Result < ( ) > {
63- register_host_function_helper ( self , hfd , func, None )
43+ register_host_function_helper ( self , name , func, None )
6444 }
6545
6646 /// Register a host function with the sandbox, with a list of extra syscalls
@@ -69,11 +49,11 @@ impl HostFuncsWrapper {
6949 #[ cfg( all( feature = "seccomp" , target_os = "linux" ) ) ]
7050 pub ( crate ) fn register_host_function_with_syscalls (
7151 & mut self ,
72- hfd : & HostFunctionDefinition ,
52+ name : String ,
7353 func : HyperlightFunction ,
7454 extra_allowed_syscalls : Vec < ExtraAllowedSyscall > ,
7555 ) -> Result < ( ) > {
76- register_host_function_helper ( self , hfd , func, Some ( extra_allowed_syscalls) )
56+ register_host_function_helper ( self , name , func, Some ( extra_allowed_syscalls) )
7757 }
7858
7959 /// Assuming a host function called `"HostPrint"` exists, and takes a
@@ -84,7 +64,7 @@ impl HostFuncsWrapper {
8464 #[ instrument( err( Debug ) , skip_all, parent = Span :: current( ) , level = "Trace" ) ]
8565 pub ( super ) fn host_print ( & mut self , msg : String ) -> Result < i32 > {
8666 let res = call_host_func_impl (
87- self . get_host_funcs ( ) ,
67+ & self . functions_map ,
8868 "HostPrint" ,
8969 vec ! [ ParameterValue :: String ( msg) ] ,
9070 ) ?;
@@ -104,56 +84,40 @@ impl HostFuncsWrapper {
10484 name : & str ,
10585 args : Vec < ParameterValue > ,
10686 ) -> Result < ReturnValue > {
107- call_host_func_impl ( self . get_host_funcs ( ) , name, args)
108- }
109-
110- /// Insert a host function into the list of registered host functions.
111- pub ( super ) fn insert_host_function ( & mut self , host_function : HostFunctionDefinition ) {
112- match & mut self . function_details {
113- Some ( host_functions) => host_functions. push ( host_function) ,
114- None => {
115- let host_functions = Vec :: from ( & [ host_function] ) ;
116- self . function_details = Some ( host_functions) ;
117- }
118- }
87+ call_host_func_impl ( & self . functions_map , name, args)
11988 }
12089}
12190
12291fn register_host_function_helper (
12392 self_ : & mut HostFuncsWrapper ,
124- hfd : & HostFunctionDefinition ,
93+ name : String ,
12594 func : HyperlightFunction ,
12695 extra_allowed_syscalls : Option < Vec < ExtraAllowedSyscall > > ,
12796) -> Result < ( ) > {
12897 if let Some ( _syscalls) = extra_allowed_syscalls {
12998 #[ cfg( all( feature = "seccomp" , target_os = "linux" ) ) ]
130- self_
131- . get_host_funcs_mut ( )
132- . insert ( hfd. function_name . to_string ( ) , func, Some ( _syscalls) ) ;
99+ self_. functions_map . insert ( name, ( func, Some ( _syscalls) ) ) ;
133100
134101 #[ cfg( not( all( feature = "seccomp" , target_os = "linux" ) ) ) ]
135102 return Err ( new_error ! (
136103 "Extra syscalls are only supported on Linux with seccomp"
137104 ) ) ;
138105 } else {
139- self_
140- . get_host_funcs_mut ( )
141- . insert ( hfd. function_name . to_string ( ) , func, None ) ;
106+ self_. functions_map . insert ( name, ( func, None ) ) ;
142107 }
143- self_. insert_host_function ( hfd. clone ( ) ) ;
144108
145109 Ok ( ( ) )
146110}
147111
148112#[ instrument( err( Debug ) , skip_all, parent = Span :: current( ) , level = "Trace" ) ]
149113fn call_host_func_impl (
150- host_funcs : & FunctionsMap ,
114+ host_funcs : & HashMap < String , ( HyperlightFunction , Option < Vec < ExtraAllowedSyscall > > ) > ,
151115 name : & str ,
152116 args : Vec < ParameterValue > ,
153117) -> Result < ReturnValue > {
154118 // Inner function containing the common logic
155119 fn call_func (
156- host_funcs : & FunctionsMap ,
120+ host_funcs : & HashMap < String , ( HyperlightFunction , Option < Vec < ExtraAllowedSyscall > > ) > ,
157121 name : & str ,
158122 args : Vec < ParameterValue > ,
159123 ) -> Result < ReturnValue > {
0 commit comments