Commit 6a44de0
authored
Map file cow windows (#1296)
* feat: add SurrogateMapping enum for Windows map_file_cow support
Introduce SurrogateMapping enum (SandboxMemory/ReadOnlyFile) to
HostRegionBase on Windows. This allows the surrogate process pipeline
to distinguish between standard sandbox memory (guard pages, PAGE_READWRITE)
and file-backed read-only mappings (no guard pages, PAGE_READONLY).
- Add SurrogateMapping enum to memory_region.rs
- Add surrogate_mapping field to HostRegionBase
- Update Hash, MemoryRegionKind::add impls to include new field
- Update host_region_base() in shared_mem.rs to set SandboxMemory
- Update crashdump test to include surrogate_mapping field
- Add unit tests for SurrogateMapping enum behaviour
- Fix Justfile fmt-apply/fmt-check/witguest-wit for Windows (PowerShell)
Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
* feat: propagate SurrogateMapping through surrogate pipeline
Pass SurrogateMapping through SurrogateProcess::map and WhpVm::map_memory
so that file-backed read-only mappings (ReadOnlyFile) use PAGE_READONLY
and skip guard pages, while sandbox memory (SandboxMemory) retains the
existing PAGE_READWRITE + guard page behaviour.
- Add mapping parameter to SurrogateProcess::map
- Derive page protection and guard page logic from SurrogateMapping variant
- Update WhpVm::map_memory to extract and forward surrogate_mapping
- Update existing test call site in surrogate_process_manager
- Add test: readonly_file_mapping_skips_guard_pages
- Add test: surrogate_map_ref_counting
Signed-off-by: Simon Davies <simon.davies@microsoft.com>
Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
* feat: add OwnedFileMapping RAII struct for Windows handle cleanup
Introduce OwnedFileMapping to track host-side file mapping resources
(MapViewOfFile view + CreateFileMappingW handle) with RAII cleanup.
Add file_mappings Vec to MultiUseSandbox (Windows-only, positioned
after vm field for correct drop order).
- OwnedFileMapping::Drop calls UnmapViewOfFile then CloseHandle
- unsafe impl Send + Sync (raw pointer only used during Drop)
- Add test: owned_file_mapping_drop_releases_handles
Signed-off-by: Simon Davies <simon.davies@microsoft.com>
Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
* feat: implement map_file_cow on Windows
Replace the Windows stub with a full implementation that maps a file
into the guest address space via the surrogate process pipeline:
CreateFileMappingW -> MapViewOfFile -> SurrogateProcess::map ->
WHvMapGpaRange2.
Key details:
- Opens file read-only, creates PAGE_READONLY mapping
- Uses SurrogateMapping::ReadOnlyFile (no guard pages, PAGE_READONLY)
- Guest gets READ|EXECUTE access via WHvMapGpaRange2 flags
- Tracks host resources in OwnedFileMapping for RAII cleanup
- Error path cleans up MapViewOfFile + CloseHandle on failure
- CreateFileMappingW uses 0,0 for size (file's actual size, Windows
rounds to page boundaries internally)
Tests (264 passed, 0 failed):
- test_map_file_cow_basic: map file, read from guest, verify content
- test_map_file_cow_read_only_enforcement: write triggers violation
- test_map_file_cow_poisoned: PoisonedSandbox check + restore
- test_map_file_cow_multi_vm_same_file: two sandboxes, same file
- test_map_file_cow_multi_vm_threaded: 5 threads, concurrent mapping
- test_map_file_cow_cleanup_no_handle_leak: file deletable after drop
Signed-off-by: Simon Davies <simon.davies@microsoft.com>
Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
* feat: wire map_file_cow cleanup into restore()
Clean up host-side file mapping resources (OwnedFileMapping) when
restore() unmaps regions. For each unmapped region, remove the
corresponding OwnedFileMapping entry, whose Drop impl calls
UnmapViewOfFile + CloseHandle.
Also remove the dead_code allow now that guest_base is read.
Tests (266 passed, 0 failed):
- test_map_file_cow_snapshot_restore: map, snapshot, restore, verify
data readable from snapshot memory
- test_map_file_cow_snapshot_remapping_cycle: snapshot1 (empty) ->
map file -> snapshot2 -> restore1 (unmapped) -> restore2 (folded)
Signed-off-by: Simon Davies <simon.davies@microsoft.com>
Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
* fix: address code review findings
H1: Add null check after MapViewOfFileNuma2 in SurrogateProcess::map
H2: Store SurrogateMapping in HandleMapping, add debug_assert_eq on reuse
M1: Clean up surrogate mapping on VirtualProtectEx failure (pre-existing)
M2: Use checked_sub for ref count, log error on underflow
M5: Early return with clear error for empty (0-byte) files
L1: Fix incorrect Send/Sync safety comment on OwnedFileMapping
L3: Rename _fp/_guest_base to file_path/guest_base
L4: Use usize::try_from(file_size) instead of silent truncation
N2: Use page_size::get() in test helper instead of magic 4096
N3: Change SurrogateMapping and surrogate_mapping field to pub(crate)
N4: Replace low-value derive trait test with meaningful variant test
Also: Change MemoryRegionType::Heap to Code for file mappings,
add tracing::error in release-mode vacant unmap path.
All 266 tests pass, 0 failures.
Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
* fix: add MappedFile region type, downgrade MemoryRegionType to pub(crate), fix clippy
- Add MemoryRegionType::MappedFile variant for map_file_cow regions
- Downgrade MemoryRegionType from pub to pub(crate) (not part of public API)
- Use MappedFile consistently on both Windows and Linux in map_file_cow
- Replace disallowed debug_assert_eq! with tracing::warn! in surrogate_process
- Fix Justfile merge conflict
Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
* Fix clippy
Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
* Review Feedback
Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
* Remove OwnedFileMapping
Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
---------
Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
Signed-off-by: Simon Davies <simon.davies@microsoft.com>1 parent 3a28293 commit 6a44de0
File tree
6 files changed
+798
-50
lines changed- src/hyperlight_host/src
- hypervisor
- virtual_machine
- mem
- sandbox
6 files changed
+798
-50
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| |||
287 | 287 | | |
288 | 288 | | |
289 | 289 | | |
290 | | - | |
291 | | - | |
| 290 | + | |
292 | 291 | | |
293 | 292 | | |
294 | 293 | | |
295 | 294 | | |
296 | 295 | | |
297 | 296 | | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
298 | 301 | | |
299 | 302 | | |
300 | 303 | | |
301 | | - | |
302 | | - | |
| 304 | + | |
303 | 305 | | |
304 | 306 | | |
305 | 307 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| 33 | + | |
33 | 34 | | |
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
39 | 43 | | |
40 | 44 | | |
41 | 45 | | |
| |||
57 | 61 | | |
58 | 62 | | |
59 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
60 | 76 | | |
61 | 77 | | |
62 | 78 | | |
63 | 79 | | |
64 | 80 | | |
| 81 | + | |
65 | 82 | | |
66 | 83 | | |
67 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
68 | 93 | | |
69 | 94 | | |
70 | 95 | | |
71 | 96 | | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
72 | 103 | | |
73 | 104 | | |
74 | 105 | | |
| |||
80 | 111 | | |
81 | 112 | | |
82 | 113 | | |
83 | | - | |
| 114 | + | |
84 | 115 | | |
85 | 116 | | |
86 | 117 | | |
87 | | - | |
88 | 118 | | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
94 | | - | |
95 | | - | |
96 | | - | |
97 | | - | |
98 | | - | |
99 | | - | |
100 | | - | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
101 | 124 | | |
102 | 125 | | |
103 | | - | |
104 | | - | |
105 | | - | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
110 | | - | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
116 | 162 | | |
| 163 | + | |
117 | 164 | | |
118 | 165 | | |
119 | 166 | | |
| 167 | + | |
120 | 168 | | |
121 | 169 | | |
122 | 170 | | |
| |||
126 | 174 | | |
127 | 175 | | |
128 | 176 | | |
129 | | - | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
130 | 184 | | |
131 | 185 | | |
132 | 186 | | |
133 | 187 | | |
134 | 188 | | |
135 | 189 | | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
136 | 194 | | |
137 | | - | |
| 195 | + | |
138 | 196 | | |
139 | 197 | | |
140 | 198 | | |
| |||
Lines changed: 124 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
459 | 459 | | |
460 | 460 | | |
461 | 461 | | |
| 462 | + | |
462 | 463 | | |
463 | 464 | | |
464 | 465 | | |
| |||
498 | 499 | | |
499 | 500 | | |
500 | 501 | | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
501 | 625 | | |
0 commit comments