@@ -132,6 +132,10 @@ impl<'a> BaseGpaRegion<&'a [u8], &'a [u8]> {
132132}
133133impl < ' a > ResolvedGpa < & ' a [ u8 ] , & ' a [ u8 ] > {
134134 pub ( crate ) fn as_ref < ' b > ( & ' b self ) -> & ' a [ u8 ] {
135+ let base = self . base . as_ref ( ) ;
136+ if self . offset > base. len ( ) {
137+ return & [ ] ;
138+ }
135139 & self . base . as_ref ( ) [ self . offset ..]
136140 }
137141}
@@ -158,7 +162,7 @@ mod coherence_hack {
158162#[ cfg( any( gdb, feature = "mem_profile" ) ) ]
159163impl < T : coherence_hack:: SharedMemoryAsRefMarker > ReadableSharedMemory for T {
160164 fn copy_to_slice ( & self , slice : & mut [ u8 ] , offset : usize ) -> Result < ( ) > {
161- let ss: & [ u8 ] = & self . as_ref ( ) [ offset.. ] ;
165+ let ss: & [ u8 ] = self . as_ref ( ) ;
162166 let end = offset + slice. len ( ) ;
163167 if end > ss. len ( ) {
164168 return Err ( new_error ! (
@@ -184,6 +188,14 @@ impl<Sn: ReadableSharedMemory, Sc: ReadableSharedMemory> ResolvedGpa<Sn, Sc> {
184188 #[ allow( clippy:: useless_conversion) ]
185189 let host_region_end: usize = r. host_region . end . into ( ) ;
186190 let len = host_region_end - host_region_base;
191+ // Safety: it's a documented invariant of MemoryRegion
192+ // that the memory must remain alive as long as the
193+ // sandbox is alive, and the way this code is used,
194+ // the lifetimes of the snapshot and scratch memories
195+ // ensure that the sandbox is still alive. This could
196+ // perhaps be cleaned up/improved/made harder to
197+ // misuse significantly, but it would require a much
198+ // larger rework.
187199 let ss = std:: slice:: from_raw_parts ( host_region_base as * const u8 , len) ;
188200 let end = self . offset + slice. len ( ) ;
189201 if end > ss. len ( ) {
@@ -230,7 +242,7 @@ pub(crate) struct SandboxMemoryLayout {
230242 // The size of the scratch region in physical memory; note that
231243 // this will appear under the top of physical memory.
232244 scratch_size : usize ,
233- // The size of the scratch region in physical memory; note that
245+ // The size of the snapshot region in physical memory; note that
234246 // this will appear somewhere near the base of physical memory.
235247 snapshot_size : usize ,
236248}
@@ -653,10 +665,10 @@ impl SandboxMemoryLayout {
653665 Ok ( ( ) )
654666 }
655667
656- /// Write the finished memory layout to `shared_mem ` and return
657- /// `Ok` if successful.
668+ /// Write the finished memory layout to `mem ` and return `Ok` if
669+ /// successful.
658670 ///
659- /// Note: `shared_mem ` may have been modified, even if `Err` was returned
671+ /// Note: `mem ` may have been modified, even if `Err` was returned
660672 /// from this function.
661673 #[ instrument( err( Debug ) , skip_all, parent = Span :: current( ) , level= "Trace" ) ]
662674 pub ( crate ) fn write_peb ( & self , mem : & mut [ u8 ] ) -> Result < ( ) > {
0 commit comments