Skip to content

Commit cac29ff

Browse files
committed
fix: remove cached scratch_base_gpa from HyperlightVm
The scratch_base_gpa field was computed once at construction time but update_scratch_mapping() can be called with different-sized scratch regions. This is a correctness hazard. Revert to upstream's approach of computing the GPA inline from the scratch region's actual size. Signed-off-by: danbugs <danilochiarlone@gmail.com>
1 parent 06c246f commit cac29ff

1 file changed

Lines changed: 2 additions & 7 deletions

File tree

src/hyperlight_host/src/hypervisor/hyperlight_vm.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ pub(crate) struct HyperlightVm {
166166
// The current scratch region, used to keep it alive as long as it
167167
// is used & when unmapping
168168
scratch_memory: Option<GuestSharedMemory>,
169-
// The resolved base GPA of the scratch region
170-
scratch_base_gpa: u64,
171169

172170
mmap_regions: Vec<(u32, MemoryRegion)>, // Later mapped regions (slot number, region)
173171

@@ -474,8 +472,6 @@ impl HyperlightVm {
474472

475473
let snapshot_slot = 0u32;
476474
let scratch_slot = 1u32;
477-
let scratch_base_gpa =
478-
hyperlight_common::layout::scratch_base_gpa(scratch_mem.mem_size());
479475
#[cfg_attr(not(gdb), allow(unused_mut))]
480476
let mut ret = Self {
481477
vm,
@@ -491,7 +487,6 @@ impl HyperlightVm {
491487
snapshot_memory: None,
492488
scratch_slot,
493489
scratch_memory: None,
494-
scratch_base_gpa,
495490

496491
mmap_regions: Vec::new(),
497492

@@ -670,11 +665,11 @@ impl HyperlightVm {
670665
&mut self,
671666
scratch: GuestSharedMemory,
672667
) -> Result<(), UpdateRegionError> {
673-
let guest_base = self.scratch_base_gpa;
668+
let guest_base = hyperlight_common::layout::scratch_base_gpa(scratch.mem_size());
674669
let rgn = scratch.mapping_at(guest_base, MemoryRegionType::Scratch);
675670

676671
if let Some(old_scratch) = self.scratch_memory.replace(scratch) {
677-
let old_base = self.scratch_base_gpa;
672+
let old_base = hyperlight_common::layout::scratch_base_gpa(old_scratch.mem_size());
678673
let old_rgn = old_scratch.mapping_at(old_base, MemoryRegionType::Scratch);
679674
self.vm.unmap_memory((self.scratch_slot, &old_rgn))?;
680675
}

0 commit comments

Comments
 (0)