@@ -798,6 +798,60 @@ fn read_mapped_buffer(function_call: &FunctionCall) -> Result<Vec<u8>> {
798798 }
799799}
800800
801+ fn write_mapped_buffer ( function_call : & FunctionCall ) -> Result < Vec < u8 > > {
802+ if let ( ParameterValue :: ULong ( base) , ParameterValue :: ULong ( len) ) = (
803+ function_call. parameters . clone ( ) . unwrap ( ) [ 0 ] . clone ( ) ,
804+ function_call. parameters . clone ( ) . unwrap ( ) [ 1 ] . clone ( ) ,
805+ ) {
806+ let base = base as usize as * mut u8 ;
807+ let len = len as usize ;
808+
809+ unsafe {
810+ hyperlight_guest_bin:: paging:: map_region ( base as _ , base as _ , len as u64 + 4096 )
811+ } ;
812+
813+ let data = unsafe { core:: slice:: from_raw_parts_mut ( base, len) } ;
814+
815+ // should fail
816+ data[ 0 ] = 0x42 ;
817+
818+ // should never reach this
819+ Ok ( get_flatbuffer_result ( true ) )
820+ } else {
821+ Err ( HyperlightGuestError :: new (
822+ ErrorCode :: GuestFunctionParameterTypeMismatch ,
823+ "Invalid parameters passed to read_mapped_buffer" . to_string ( ) ,
824+ ) )
825+ }
826+ }
827+
828+ fn exec_mapped_buffer ( function_call : & FunctionCall ) -> Result < Vec < u8 > > {
829+ if let ( ParameterValue :: ULong ( base) , ParameterValue :: ULong ( len) ) = (
830+ function_call. parameters . clone ( ) . unwrap ( ) [ 0 ] . clone ( ) ,
831+ function_call. parameters . clone ( ) . unwrap ( ) [ 1 ] . clone ( ) ,
832+ ) {
833+ let base = base as usize as * mut u8 ;
834+ let len = len as usize ;
835+
836+ unsafe {
837+ hyperlight_guest_bin:: paging:: map_region ( base as _ , base as _ , len as u64 + 4096 )
838+ } ;
839+
840+ let data = unsafe { core:: slice:: from_raw_parts ( base, len) } ;
841+
842+ // Should be safe as long as data is something like a NOOP followed by a RET
843+ let func: fn ( ) = unsafe { core:: mem:: transmute ( data. as_ptr ( ) ) } ;
844+ func ( ) ;
845+
846+ Ok ( get_flatbuffer_result ( true ) )
847+ } else {
848+ Err ( HyperlightGuestError :: new (
849+ ErrorCode :: GuestFunctionParameterTypeMismatch ,
850+ "Invalid parameters passed to read_mapped_buffer" . to_string ( ) ,
851+ ) )
852+ }
853+ }
854+
801855#[ no_mangle]
802856pub extern "C" fn hyperlight_main ( ) {
803857 let read_from_user_memory_def = GuestFunctionDefinition :: new (
@@ -818,6 +872,24 @@ pub extern "C" fn hyperlight_main() {
818872
819873 register_function ( read_mapped_buffer_def) ;
820874
875+ let write_mapped_buffer_def = GuestFunctionDefinition :: new (
876+ "WriteMappedBuffer" . to_string ( ) ,
877+ Vec :: from ( & [ ParameterType :: ULong , ParameterType :: ULong ] ) ,
878+ ReturnType :: Bool ,
879+ write_mapped_buffer as usize ,
880+ ) ;
881+
882+ register_function ( write_mapped_buffer_def) ;
883+
884+ let exec_mapped_buffer_def = GuestFunctionDefinition :: new (
885+ "ExecMappedBuffer" . to_string ( ) ,
886+ Vec :: from ( & [ ParameterType :: ULong , ParameterType :: ULong ] ) ,
887+ ReturnType :: Bool ,
888+ exec_mapped_buffer as usize ,
889+ ) ;
890+
891+ register_function ( exec_mapped_buffer_def) ;
892+
821893 let set_static_def = GuestFunctionDefinition :: new (
822894 "SetStatic" . to_string ( ) ,
823895 Vec :: new ( ) ,
0 commit comments