@@ -46,7 +46,7 @@ use hyperlight_guest::exit::{abort_with_code, abort_with_code_and_message};
46
46
use hyperlight_guest_bin:: guest_function:: definition:: GuestFunctionDefinition ;
47
47
use hyperlight_guest_bin:: guest_function:: register:: register_function;
48
48
use 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 ,
50
50
} ;
51
51
use hyperlight_guest_bin:: memory:: malloc;
52
52
use hyperlight_guest_bin:: { MIN_STACK_ADDRESS , guest_logger} ;
@@ -750,8 +750,42 @@ fn large_parameters(function_call: &FunctionCall) -> Result<Vec<u8>> {
750
750
}
751
751
}
752
752
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
+
753
778
#[ no_mangle]
754
779
pub 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
+
755
789
let set_static_def = GuestFunctionDefinition :: new (
756
790
"SetStatic" . to_string ( ) ,
757
791
Vec :: new ( ) ,
0 commit comments