@@ -46,7 +46,7 @@ use hyperlight_guest::exit::{abort_with_code, abort_with_code_and_message};
4646use  hyperlight_guest_bin:: guest_function:: definition:: GuestFunctionDefinition ; 
4747use  hyperlight_guest_bin:: guest_function:: register:: register_function; 
4848use  hyperlight_guest_bin:: host_comm:: { 
49-     call_host_function,  call_host_function_without_returning_result, 
49+     call_host_function,  call_host_function_without_returning_result,  read_n_bytes_from_user_memory , 
5050} ; 
5151use  hyperlight_guest_bin:: memory:: malloc; 
5252use  hyperlight_guest_bin:: { MIN_STACK_ADDRESS ,  guest_logger} ; 
@@ -750,8 +750,42 @@ fn large_parameters(function_call: &FunctionCall) -> Result<Vec<u8>> {
750750    } 
751751} 
752752
753+ fn  read_from_user_memory ( function_call :  & FunctionCall )  -> Result < Vec < u8 > >  { 
754+     if  let  ( ParameterValue :: ULong ( num) ,  ParameterValue :: VecBytes ( expected) )  = ( 
755+         function_call. parameters . clone ( ) . unwrap ( ) [ 0 ] . clone ( ) , 
756+         function_call. parameters . clone ( ) . unwrap ( ) [ 1 ] . clone ( ) , 
757+     )  { 
758+         let  bytes = read_n_bytes_from_user_memory ( num) . expect ( "Failed to read from user memory" ) ; 
759+ 
760+         // verify that the user memory contains the expected data 
761+         if  bytes != expected { 
762+             error ! ( "User memory does not contain the expected data" ) ; 
763+             return  Err ( HyperlightGuestError :: new ( 
764+                 ErrorCode :: GuestError , 
765+                 "User memory does not contain the expected data" . to_string ( ) , 
766+             ) ) ; 
767+         } 
768+ 
769+         Ok ( get_flatbuffer_result ( & * bytes) ) 
770+     }  else  { 
771+         Err ( HyperlightGuestError :: new ( 
772+             ErrorCode :: GuestFunctionParameterTypeMismatch , 
773+             "Invalid parameters passed to read_from_user_memory" . to_string ( ) , 
774+         ) ) 
775+     } 
776+ } 
777+ 
753778#[ no_mangle]  
754779pub  extern  "C"  fn  hyperlight_main ( )  { 
780+     let  read_from_user_memory_def = GuestFunctionDefinition :: new ( 
781+         "ReadFromUserMemory" . to_string ( ) , 
782+         Vec :: from ( & [ ParameterType :: ULong ,  ParameterType :: VecBytes ] ) , 
783+         ReturnType :: VecBytes , 
784+         read_from_user_memory as  usize , 
785+     ) ; 
786+ 
787+     register_function ( read_from_user_memory_def) ; 
788+ 
755789    let  set_static_def = GuestFunctionDefinition :: new ( 
756790        "SetStatic" . to_string ( ) , 
757791        Vec :: new ( ) , 
0 commit comments