@@ -235,12 +235,21 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
235235 }
236236
237237 /// Wraps ExclusiveSharedMemory::build
238+ // Morally, this should not have to be a Result: this operation is
239+ // infallible. The source of the Result is
240+ // update_scratch_bookkeeping(), which calls functions that can
241+ // fail due to bounds checks (which are statically known to be ok
242+ // in this situation) or due to failing to take the scratch shared
243+ // memory lock, but the scratch shared memory is built in this
244+ // function, its lock does not escape before the end of the
245+ // function, and the lock is taken by no other code path, so we
246+ // know it is not contended.
238247 pub fn build (
239248 self ,
240- ) -> (
249+ ) -> Result < (
241250 SandboxMemoryManager < HostSharedMemory > ,
242251 SandboxMemoryManager < GuestSharedMemory > ,
243- ) {
252+ ) > {
244253 let ( hshm, gshm) = self . shared_mem . build ( ) ;
245254 let ( hscratch, gscratch) = self . scratch_mem . build ( ) ;
246255 let mut host_mgr = SandboxMemoryManager {
@@ -265,8 +274,8 @@ impl SandboxMemoryManager<ExclusiveSharedMemory> {
265274 } ;
266275 host_mgr. update_scratch_bookkeeping (
267276 ( SandboxMemoryLayout :: BASE_ADDRESS + self . layout . get_pt_offset ( ) ) as u64 ,
268- ) ;
269- ( host_mgr, guest_mgr)
277+ ) ? ;
278+ Ok ( ( host_mgr, guest_mgr) )
270279 }
271280}
272281
@@ -424,39 +433,30 @@ impl SandboxMemoryManager<HostSharedMemory> {
424433
425434 Some ( gscratch)
426435 } ;
427- self . update_scratch_bookkeeping ( snapshot. root_pt_gpa ( ) ) ;
436+ self . update_scratch_bookkeeping ( snapshot. root_pt_gpa ( ) ) ? ;
428437 Ok ( ( gsnapshot, gscratch) )
429438 }
430439
431- fn update_scratch_bookkeeping ( & mut self , snapshot_pt_base_gpa : u64 ) {
440+ fn update_scratch_bookkeeping ( & mut self , snapshot_pt_base_gpa : u64 ) -> Result < ( ) > {
432441 let scratch_size = self . scratch_mem . mem_size ( ) ;
442+
433443 let size_offset =
434444 scratch_size - hyperlight_common:: layout:: SCRATCH_TOP_SIZE_OFFSET as usize ;
435- // The only way that write can fail is if the offset is
436- // outside of the memory, which would be sufficiently much of
437- // an invariant violation that panicking is probably
438- // sensible...
439- #[ allow( clippy:: unwrap_used) ]
440445 self . scratch_mem
441- . write :: < u64 > ( size_offset, scratch_size as u64 )
442- . unwrap ( ) ;
446+ . write :: < u64 > ( size_offset, scratch_size as u64 ) ? ;
447+
443448 let alloc_offset =
444449 scratch_size - hyperlight_common:: layout:: SCRATCH_TOP_ALLOCATOR_OFFSET as usize ;
445- // See above comment about unwrap() on write
446- #[ allow( clippy:: unwrap_used) ]
447- self . scratch_mem
448- . write :: < u64 > (
449- alloc_offset,
450- hyperlight_common:: layout:: scratch_base_gpa ( scratch_size) ,
451- )
452- . unwrap ( ) ;
450+ self . scratch_mem . write :: < u64 > (
451+ alloc_offset,
452+ hyperlight_common:: layout:: scratch_base_gpa ( scratch_size) ,
453+ ) ?;
454+
453455 let snapshot_pt_base_gpa_offset = scratch_size
454456 - hyperlight_common:: layout:: SCRATCH_TOP_SNAPSHOT_PT_GPA_BASE_OFFSET as usize ;
455- // See above comment about unwrap() on write
456- #[ allow( clippy:: unwrap_used) ]
457457 self . scratch_mem
458- . write :: < u64 > ( snapshot_pt_base_gpa_offset, snapshot_pt_base_gpa)
459- . unwrap ( ) ;
458+ . write :: < u64 > ( snapshot_pt_base_gpa_offset, snapshot_pt_base_gpa) ? ;
459+ Ok ( ( ) )
460460 }
461461}
462462
0 commit comments