Skip to content

Commit 6f7f55e

Browse files
authored
Remove Snapshot's empty regions field (#1509)
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
1 parent 53d0aa5 commit 6f7f55e

3 files changed

Lines changed: 4 additions & 92 deletions

File tree

src/hyperlight_host/src/mem/mgr.rs

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -864,24 +864,20 @@ impl SandboxMemoryManager<HostSharedMemory> {
864864
#[cfg(test)]
865865
#[cfg(all(not(feature = "i686-guest"), target_arch = "x86_64"))]
866866
mod tests {
867-
use hyperlight_common::vmem::{MappingKind, PAGE_TABLE_SIZE};
868867
use hyperlight_testing::sandbox_sizes::{LARGE_HEAP_SIZE, MEDIUM_HEAP_SIZE, SMALL_HEAP_SIZE};
869868
use hyperlight_testing::simple_guest_as_string;
870869

871870
use crate::GuestBinary;
872-
use crate::mem::memory_region::MemoryRegionFlags;
873871
use crate::sandbox::SandboxConfiguration;
874872
use crate::sandbox::snapshot::Snapshot;
875873

876-
/// Verify page tables for a given configuration.
877-
/// Creates a Snapshot and verifies every page in every region has correct PTEs.
874+
/// Build a Snapshot for the given configuration and verify the
875+
/// NULL page is not mapped in its page tables.
878876
fn verify_page_tables(name: &str, config: SandboxConfiguration) {
879877
let path = simple_guest_as_string().expect("failed to get simple guest path");
880878
let snapshot = Snapshot::from_env(GuestBinary::FilePath(path), config)
881879
.unwrap_or_else(|e| panic!("{}: failed to create snapshot: {}", name, e));
882880

883-
let regions = snapshot.regions();
884-
885881
// Verify NULL page (0x0) is NOT mapped
886882
assert!(
887883
unsafe { hyperlight_common::vmem::virt_to_phys(&snapshot, 0, 1) }
@@ -890,57 +886,6 @@ mod tests {
890886
"{}: NULL page (0x0) should NOT be mapped",
891887
name
892888
);
893-
894-
// Verify every page in every region
895-
for region in regions {
896-
let mut addr = region.guest_region.start as u64;
897-
898-
while addr < region.guest_region.end as u64 {
899-
let mapping = unsafe { hyperlight_common::vmem::virt_to_phys(&snapshot, addr, 1) }
900-
.next()
901-
.unwrap_or_else(|| {
902-
panic!(
903-
"{}: {:?} region: address 0x{:x} is not mapped",
904-
name, region.region_type, addr
905-
)
906-
});
907-
908-
// Verify identity mapping (phys == virt for low memory)
909-
assert_eq!(
910-
mapping.phys_base, addr,
911-
"{}: {:?} region: address 0x{:x} should identity map, got phys 0x{:x}",
912-
name, region.region_type, addr, mapping.phys_base
913-
);
914-
915-
// Verify kind is Basic
916-
let MappingKind::Basic(bm) = mapping.kind else {
917-
panic!(
918-
"{}: {:?} region: address 0x{:x} should be kind basic, got {:?}",
919-
name, region.region_type, addr, mapping.kind
920-
);
921-
};
922-
923-
// Verify writable
924-
let actual = bm.writable;
925-
let expected = region.flags.contains(MemoryRegionFlags::WRITE);
926-
assert_eq!(
927-
actual, expected,
928-
"{}: {:?} region: address 0x{:x} has writable {}, expected {} (region flags: {:?})",
929-
name, region.region_type, addr, actual, expected, region.flags
930-
);
931-
932-
// Verify executable
933-
let actual = bm.executable;
934-
let expected = region.flags.contains(MemoryRegionFlags::EXECUTE);
935-
assert_eq!(
936-
actual, expected,
937-
"{}: {:?} region: address 0x{:x} has executable {}, expected {} (region flags: {:?})",
938-
name, region.region_type, addr, actual, expected, region.flags
939-
);
940-
941-
addr += PAGE_TABLE_SIZE as u64;
942-
}
943-
}
944889
}
945890

946891
#[test]

src/hyperlight_host/src/sandbox/initialized_multi_use.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
use std::collections::HashSet;
1817
use std::path::Path;
1918
use std::sync::{Arc, Mutex};
2019

@@ -534,24 +533,13 @@ impl MultiUseSandbox {
534533
self.vm.set_stack_top(snapshot.stack_top_gva());
535534
self.vm.set_entrypoint(snapshot.entrypoint());
536535

537-
let current_regions: HashSet<_> = self.vm.get_mapped_regions().cloned().collect();
538-
let snapshot_regions: HashSet<_> = snapshot.regions().iter().cloned().collect();
539-
540-
let regions_to_unmap = current_regions.difference(&snapshot_regions);
541-
let regions_to_map = snapshot_regions.difference(&current_regions);
542-
543-
for region in regions_to_unmap {
536+
let current_regions: Vec<MemoryRegion> = self.vm.get_mapped_regions().cloned().collect();
537+
for region in &current_regions {
544538
self.vm
545539
.unmap_region(region)
546540
.map_err(HyperlightVmError::UnmapRegion)?;
547541
}
548542

549-
for region in regions_to_map {
550-
// Safety: The region has been mapped before, and at that point the caller promised that the memory region is valid
551-
// in their call to `MultiUseSandbox::map_region`
552-
unsafe { self.vm.map_region(region) }.map_err(HyperlightVmError::MapRegion)?;
553-
}
554-
555543
// The restored snapshot is now our most current snapshot
556544
self.snapshot = Some(snapshot.clone());
557545

src/hyperlight_host/src/sandbox/snapshot/mod.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ pub struct Snapshot {
6868
layout: crate::mem::layout::SandboxMemoryLayout,
6969
/// Memory of the sandbox at the time this snapshot was taken
7070
memory: ReadonlySharedMemory,
71-
/// The memory regions that were mapped when this snapshot was
72-
/// taken (excluding initial sandbox regions)
73-
regions: Vec<MemoryRegion>,
7471
/// Extra debug information about the binary in this snapshot,
7572
/// from when the binary was first loaded into the snapshot.
7673
///
@@ -364,12 +361,9 @@ impl Snapshot {
364361
- hyperlight_common::layout::SCRATCH_TOP_EXN_STACK_OFFSET
365362
+ 1;
366363

367-
let extra_regions = Vec::new();
368-
369364
Ok(Self {
370365
memory: ReadonlySharedMemory::from_bytes(&memory, layout.snapshot_size)?,
371366
layout,
372-
regions: extra_regions,
373367
load_info,
374368
stack_top_gva: exn_stack_top_gva,
375369
sregs: None,
@@ -547,19 +541,9 @@ impl Snapshot {
547541
debug_assert!(guest_visible_size.is_multiple_of(PAGE_SIZE));
548542
layout.set_snapshot_size(guest_visible_size);
549543

550-
// Drop the embedder-provided regions: post-compaction every
551-
// VA that used to map into a `map_file_cow` region has been
552-
// rewritten to point at the new copy inside the snapshot blob
553-
// (see the `guest_page` walk above). Re-mapping the originals
554-
// on restore is unnecessary for the translation to work and
555-
// actively risks corrupting the snapshot if the new snapshot
556-
// PAs overlap the old region PAs.
557-
let regions: Vec<MemoryRegion> = Vec::new();
558-
559544
Ok(Self {
560545
layout,
561546
memory: ReadonlySharedMemory::from_bytes(&memory, guest_visible_size)?,
562-
regions,
563547
load_info,
564548
stack_top_gva,
565549
sregs: Some(sregs),
@@ -574,11 +558,6 @@ impl Snapshot {
574558
self.snapshot_generation
575559
}
576560

577-
/// Get the mapped regions from this snapshot
578-
pub(crate) fn regions(&self) -> &[MemoryRegion] {
579-
&self.regions
580-
}
581-
582561
/// Return the main memory contents of the snapshot
583562
#[instrument(skip_all, parent = Span::current(), level= "Trace")]
584563
pub(crate) fn memory(&self) -> &ReadonlySharedMemory {

0 commit comments

Comments
 (0)