Skip to content

fix(whp): handle unaligned files in map_file_cow#1388

Open
danbugs wants to merge 1 commit intohyperlight-dev:mainfrom
danbugs:fix-map-file-cow-unaligned
Open

fix(whp): handle unaligned files in map_file_cow#1388
danbugs wants to merge 1 commit intohyperlight-dev:mainfrom
danbugs:fix-map-file-cow-unaligned

Conversation

@danbugs
Copy link
Copy Markdown
Contributor

@danbugs danbugs commented Apr 17, 2026

Summary

  • map_file_cow silently fails on Windows for any file whose size is not a multiple of the page size (i.e. almost every real-world initrd / filesystem image).
  • CreateFileMappingW on a read-only file handle caps the section's max size at the file's on-disk size — requesting a larger size fails with ERROR_NOT_ENOUGH_MEMORY. The surrogate then asks MapViewOfFileNuma2 for a page-aligned host_size that exceeds the section, which fails with ERROR_ACCESS_DENIED.
  • Fix: for ReadOnlyFile mappings in the surrogate, pass NumberOfBytesToMap=0 (= "to end of section") instead of the caller's page-aligned host_size. MapViewOfFile always rounds the view up to a page boundary, and the OS zero-fills the tail of the final page — matching POSIX mmap semantics. Zero-copy is preserved.
  • SandboxMemory mappings still pass host_size because the guard-page bookkeeping that follows depends on knowing the exact extent.
  • map-file-cow-test now maps both an aligned (8192-byte) and an intentionally unaligned (8193-byte) file as a regression test. The previous single-file test used exactly 2 pages and coincidentally masked this bug.

Windows CreateFileMappingW with a read-only file handle caps the section
size at the file's actual size on disk (requesting max_size > file_size
fails with ERROR_NOT_ENOUGH_MEMORY). For files whose size is not a
multiple of the page size, the surrogate's MapViewOfFileNuma2 call was
passing a page-aligned host_size that exceeded the section, failing
with ERROR_ACCESS_DENIED — the map_file_cow zero-copy path was broken
for any non-page-aligned file.

Pass NumberOfBytesToMap=0 for ReadOnlyFile mappings so Windows maps the
section to its end. MapViewOfFile always returns a view whose region is
rounded up to a page boundary, and the OS zero-fills the tail of the
final page — matching POSIX mmap semantics. SandboxMemory sections are
always created with an explicit page-aligned size and still need the
exact host_size so the guard-page bookkeeping below lines up, so keep
host_size for that case.

The map-file-cow-test example now covers an intentionally 8193-byte
file to exercise the unaligned path.

Signed-off-by: danbugs <danilochiarlone@gmail.com>
@danbugs danbugs added the kind/bugfix For PRs that fix bugs label Apr 17, 2026
@danbugs danbugs enabled auto-merge (squash) April 17, 2026 23:53
danbugs added a commit to danbugs/hyperlight-unikraft that referenced this pull request Apr 18, 2026
hyperlight-dev/hyperlight#1388 is a required fix for running on Windows:
any initrd CPIO whose size isn't a multiple of the page size (nearly
all real-world initrds) fails MapViewOfFileNuma2 with ACCESS_DENIED.
Revert to hyperlight-dev/hyperlight main once the PR merges.

Signed-off-by: danbugs <danilochiarlone@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bugfix For PRs that fix bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant