@@ -22,6 +22,7 @@ use std::mem::{align_of, size_of};
2222use std:: ptr:: null_mut;
2323use std:: sync:: { Arc , RwLock } ;
2424
25+ use bytemuck:: Pod ;
2526use hyperlight_common:: mem:: PAGE_SIZE_USIZE ;
2627use tracing:: { Span , instrument} ;
2728#[ cfg( target_os = "windows" ) ]
@@ -387,25 +388,6 @@ impl Drop for Placeholder {
387388 }
388389}
389390
390- /// An unsafe marker trait for types for which all bit patterns are valid.
391- /// This is required in order for it to be safe to read a value of a particular
392- /// type out of the sandbox from the HostSharedMemory.
393- ///
394- /// # Safety
395- /// This must only be implemented for types for which all bit patterns
396- /// are valid. It requires that any (non-undef/poison) value of the
397- /// correct size can be transmuted to the type.
398- pub unsafe trait AllValid { }
399- unsafe impl AllValid for u8 { }
400- unsafe impl AllValid for u16 { }
401- unsafe impl AllValid for u32 { }
402- unsafe impl AllValid for u64 { }
403- unsafe impl AllValid for i8 { }
404- unsafe impl AllValid for i16 { }
405- unsafe impl AllValid for i32 { }
406- unsafe impl AllValid for i64 { }
407- unsafe impl AllValid for [ u8 ; 16 ] { }
408-
409391/// A trait that abstracts over the particular kind of SharedMemory,
410392/// used when invoking operations from Rust that absolutely must have
411393/// exclusive control over the shared memory for correctness +
@@ -1162,37 +1144,20 @@ pub struct HostSharedMemory {
11621144unsafe impl Send for HostSharedMemory { }
11631145
11641146impl HostSharedMemory {
1165- /// Read a value of type T, whose representation is the same
1166- /// between the sandbox and the host, and which has no invalid bit
1167- /// patterns
1168- pub fn read < T : AllValid > ( & self , offset : usize ) -> Result < T > {
1147+ /// Read a [`Pod`] value of type `T`, whose representation is the same
1148+ /// between the sandbox and the host.
1149+ pub fn read < T : Pod > ( & self , offset : usize ) -> Result < T > {
11691150 bounds_check ! ( offset, std:: mem:: size_of:: <T >( ) , self . mem_size( ) ) ;
1170- unsafe {
1171- let mut ret: core:: mem:: MaybeUninit < T > = core:: mem:: MaybeUninit :: uninit ( ) ;
1172- {
1173- let slice: & mut [ u8 ] = core:: slice:: from_raw_parts_mut (
1174- ret. as_mut_ptr ( ) as * mut u8 ,
1175- std:: mem:: size_of :: < T > ( ) ,
1176- ) ;
1177- self . copy_to_slice ( slice, offset) ?;
1178- }
1179- Ok ( ret. assume_init ( ) )
1180- }
1151+ let mut ret = T :: zeroed ( ) ;
1152+ self . copy_to_slice ( bytemuck:: bytes_of_mut ( & mut ret) , offset) ?;
1153+ Ok ( ret)
11811154 }
11821155
1183- /// Write a value of type T, whose representation is the same
1184- /// between the sandbox and the host, and which has no invalid bit
1185- /// patterns
1186- pub fn write < T : AllValid > ( & self , offset : usize , data : T ) -> Result < ( ) > {
1156+ /// Write a [`Pod`] value of type `T`, whose representation is the same
1157+ /// between the sandbox and the host.
1158+ pub fn write < T : Pod > ( & self , offset : usize , data : T ) -> Result < ( ) > {
11871159 bounds_check ! ( offset, std:: mem:: size_of:: <T >( ) , self . mem_size( ) ) ;
1188- unsafe {
1189- let slice: & [ u8 ] = core:: slice:: from_raw_parts (
1190- core:: ptr:: addr_of!( data) as * const u8 ,
1191- std:: mem:: size_of :: < T > ( ) ,
1192- ) ;
1193- self . copy_from_slice ( slice, offset) ?;
1194- }
1195- Ok ( ( ) )
1160+ self . copy_from_slice ( bytemuck:: bytes_of ( & data) , offset)
11961161 }
11971162
11981163 /// Copy the contents of the slice into the sandbox at the
0 commit comments