@@ -180,28 +180,10 @@ impl MultiUseSandbox {
180180 Ok ( ( ) )
181181 }
182182
183- /// Call a guest function by name, with the given return type and arguments.
184- #[ instrument( err( Debug ) , skip( self , args) , parent = Span :: current( ) ) ]
185- pub fn call_guest_function_by_name < Output : SupportedReturnType > (
186- & mut self ,
187- func_name : & str ,
188- args : impl ParameterTuple ,
189- ) -> Result < Output > {
190- maybe_time_and_emit_guest_call ( func_name, || {
191- let ret = self . call_guest_function_by_name_no_reset (
192- func_name,
193- Output :: TYPE ,
194- args. into_value ( ) ,
195- ) ;
196- self . restore_state ( ) ?;
197- Output :: from_value ( ret?)
198- } )
199- }
200-
201183 /// Call a guest function by name, with the given return type and arguments.
202184 /// The changes made to the sandbox are persisted
203185 #[ instrument( err( Debug ) , skip( self , args) , parent = Span :: current( ) ) ]
204- pub fn persist_call_guest_function_by_name < Output : SupportedReturnType > (
186+ pub fn call_guest_function_by_name < Output : SupportedReturnType + std :: fmt :: Debug > (
205187 & mut self ,
206188 func_name : & str ,
207189 args : impl ParameterTuple ,
@@ -213,7 +195,7 @@ impl MultiUseSandbox {
213195 args. into_value ( ) ,
214196 ) ;
215197 let ret = Output :: from_value ( ret?) ;
216- self . mem_mgr . unwrap_mgr_mut ( ) . push_state ( ) ? ;
198+ self . mem_mgr . unwrap_mgr_mut ( ) . push_state ( ) . unwrap ( ) ;
217199 ret
218200 } )
219201 }
@@ -298,7 +280,7 @@ impl MultiUseSandbox {
298280 ) -> Result < ReturnValue > {
299281 maybe_time_and_emit_guest_call ( func_name, || {
300282 let ret = self . call_guest_function_by_name_no_reset ( func_name, ret_type, args) ;
301- self . restore_state ( ) ?;
283+ self . mem_mgr . unwrap_mgr_mut ( ) . push_state ( ) ?;
302284 ret
303285 } )
304286 }
@@ -318,35 +300,43 @@ impl MultiUseSandbox {
318300 return_type : ReturnType ,
319301 args : Vec < ParameterValue > ,
320302 ) -> Result < ReturnValue > {
321- let fc = FunctionCall :: new (
322- function_name. to_string ( ) ,
323- Some ( args) ,
324- FunctionCallType :: Guest ,
325- return_type,
326- ) ;
327-
328- let buffer: Vec < u8 > = fc
329- . try_into ( )
330- . map_err ( |_| HyperlightError :: Error ( "Failed to serialize FunctionCall" . to_string ( ) ) ) ?;
331-
332- self . get_mgr_wrapper_mut ( )
333- . as_mut ( )
334- . write_guest_function_call ( & buffer) ?;
335-
336- self . vm . dispatch_call_from_host (
337- self . dispatch_ptr . clone ( ) ,
338- self . out_hdl . clone ( ) ,
339- self . mem_hdl . clone ( ) ,
340- #[ cfg( gdb) ]
341- self . dbg_mem_access_fn . clone ( ) ,
342- ) ?;
303+ let res = ( || {
304+ let fc = FunctionCall :: new (
305+ function_name. to_string ( ) ,
306+ Some ( args) ,
307+ FunctionCallType :: Guest ,
308+ return_type,
309+ ) ;
310+
311+ let buffer: Vec < u8 > = fc. try_into ( ) . map_err ( |_| {
312+ HyperlightError :: Error ( "Failed to serialize FunctionCall" . to_string ( ) )
313+ } ) ?;
343314
344- self . check_stack_guard ( ) ?;
345- check_for_guest_error ( self . get_mgr_wrapper_mut ( ) ) ?;
315+ self . get_mgr_wrapper_mut ( )
316+ . as_mut ( )
317+ . write_guest_function_call ( & buffer) ?;
346318
347- self . get_mgr_wrapper_mut ( )
348- . as_mut ( )
349- . get_guest_function_call_result ( )
319+ self . vm . dispatch_call_from_host (
320+ self . dispatch_ptr . clone ( ) ,
321+ self . out_hdl . clone ( ) ,
322+ self . mem_hdl . clone ( ) ,
323+ #[ cfg( gdb) ]
324+ self . dbg_mem_access_fn . clone ( ) ,
325+ ) ?;
326+
327+ self . check_stack_guard ( ) ?;
328+ check_for_guest_error ( self . get_mgr_wrapper_mut ( ) ) ?;
329+
330+ self
331+ . get_mgr_wrapper_mut ( )
332+ . as_mut ( )
333+ . get_guest_function_call_result ( )
334+ } ) ( ) ;
335+
336+ // TODO: Do we want to allow re-entrant guest function calls?
337+ self . get_mgr_wrapper_mut ( ) . as_mut ( ) . clear_io_buffers ( ) ;
338+
339+ res
350340 }
351341
352342 /// Get a handle to the interrupt handler for this sandbox,
@@ -447,13 +437,14 @@ mod tests {
447437
448438 let snapshot = sbox. snapshot ( ) . unwrap ( ) ;
449439
450- let _ = sbox. persist_call_guest_function_by_name :: < i32 > ( "AddToStatic" , 5i32 ) . unwrap ( ) ;
440+ let _ = sbox. call_guest_function_by_name :: < i32 > ( "AddToStatic" , 5i32 )
441+ . unwrap ( ) ;
451442
452- let res: i32 = sbox. persist_call_guest_function_by_name ( "GetStatic" , ( ) ) . unwrap ( ) ;
443+ let res: i32 = sbox. call_guest_function_by_name ( "GetStatic" , ( ) ) . unwrap ( ) ;
453444 assert_eq ! ( res, 5 ) ;
454445
455446 sbox. restore ( & snapshot) . unwrap ( ) ;
456- let res: i32 = sbox. persist_call_guest_function_by_name ( "GetStatic" , ( ) ) . unwrap ( ) ;
447+ let res: i32 = sbox. call_guest_function_by_name ( "GetStatic" , ( ) ) . unwrap ( ) ;
457448 assert_eq ! ( res, 0 ) ;
458449 }
459450
0 commit comments