Skip to content

feat(whp): copy-on-write file mapping for snapshot load#1391

Open
danbugs wants to merge 5 commits intohyperlight-dev:mainfrom
danbugs:whp-snapshot-cow
Open

feat(whp): copy-on-write file mapping for snapshot load#1391
danbugs wants to merge 5 commits intohyperlight-dev:mainfrom
danbugs:whp-snapshot-cow

Conversation

@danbugs
Copy link
Copy Markdown
Contributor

@danbugs danbugs commented Apr 20, 2026

Summary

Stacked follow-up to #1373. The Windows path in ReadonlySharedMemory::from_file_windows is currently created with PAGE_READONLY + FILE_MAP_READ. That matches the type name (ReadonlySharedMemory) but not the semantics the from_snapshot caller needs: the host-side view has to be writable so WHP (and MSHV) can service the copy-on-write faults the guest takes on first write. A read-only mapping triggers an access violation on the host thread the moment the guest touches any page.

Switch to PAGE_WRITECOPY + FILE_MAP_COPY — the Windows equivalent of Linux's mmap(MAP_PRIVATE) that the Linux from_file path already uses. Reads still come from the backing file; writes transparently allocate private copy-on-write pages.

Relationship to #1373

Tested

On a Windows box with WHP enabled: Snapshot::to_file then MultiUseSandbox::from_snapshotrestorecall completes cleanly. Pre-fix, the same flow took an access violation on the first page fault the guest triggered after restore.

Known follow-ups (not in this PR)

None for this change specifically. Separate PR will add Registerable for MultiUseSandbox so host functions can be wired on sandboxes acquired via from_snapshot.

ludfjig and others added 5 commits April 20, 2026 22:14
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
Save snapshots to disk via Snapshot::to_file(), load them back via
Snapshot::from_file(). Create sandboxes directly from loaded snapshots via
MultiUseSandbox::from_snapshot(), bypassing ELF parsing and guest init.

Signed-off-by: Ludvig Liljenberg <4257730+ludfjig@users.noreply.github.com>
from_snapshot() was constructing a fresh MultiUseSandbox via
from_uninit(), which assigns a new monotonically-increasing sandbox
id. The loaded snapshot still carries the id of the sandbox it was
originally captured from, so the very first restore() call after
from_snapshot trips SnapshotSandboxMismatch and aborts before any
guest code runs.

Conceptually the snapshot IS this sandbox's identity when loaded
from disk — the sandbox it was captured from no longer exists.
Inherit the snapshot's sandbox_id and register the loaded snapshot
as this sandbox's active snapshot so subsequent restores match.

Signed-off-by: danbugs <danilochiarlone@gmail.com>
The Windows path in ReadonlySharedMemory::from_file_windows was
created with PAGE_READONLY + FILE_MAP_READ. That matches the name
('ReadonlySharedMemory') but not the semantics the caller needs: a
sandbox loaded from a snapshot still has to be a writable view of
the guest's memory from the host's perspective, so WHP/MSHV can
service copy-on-write faults the guest takes on first write.

A read-only mapping triggers an access violation on the host thread
the moment the guest touches any page, before the VMM can vector
the fault into the in-kernel CoW path.

Switch to PAGE_WRITECOPY + FILE_MAP_COPY — the Windows equivalent
of Linux's mmap(MAP_PRIVATE) that Linux's from_file path already
uses. Reads still come from the backing file; writes transparently
allocate private copy-on-write pages.

Follow-up to hyperlight-dev#1373; depends on that PR
landing first.

Signed-off-by: danbugs <danilochiarlone@gmail.com>
@danbugs danbugs added the kind/enhancement For PRs adding features, improving functionality, docs, tests, etc. label Apr 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement For PRs adding features, improving functionality, docs, tests, etc.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants