VFS: ReadFileEx, WriteFile and scatter/gather commit lazy pages first #1345
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1355"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by emirduran707 on 2026-11-24T15:09:01Z.
Pages a title maps from physical memory into its own reservation stay reserved until first touched (
lazy::mark). Kernel calls do not fault them in, they fail, so XCL commits the buffers of the file calls it routes first (lazy.rs). OnlyReadFiledid that.On Windows,
ReadFileEx,ReadFileScatter,WriteFile,WriteFileExandWriteFileGathergo to their ownNt*calls and never reach theReadFilehook (measured on Windows 11 26300). On a buffer that is only reserved:ReadFileExfails withERROR_NOACCESS(998), and its completion routine never runs;WriteFilefails withERROR_INVALID_USER_BUFFER(1784).So a title that reads with
ReadFileExinto, or writes from, freshly mapped memory got an error where the console succeeds.These five calls are now hooked in
kernelbase, next toReadFile. They commit the lazy pages of their buffers before calling the original. For scatter/gather that means eachFILE_SEGMENT_ELEMENTpage, with each array entry read fault-safely viaReadProcessMemory(GetCurrentProcess(), ...)(afterlazy::ensureon the entry address) so an invalid or unreadable segment array pointer never faults in the hook and is left for the kernel API to reject with its normal error. A committed lazy page holds zeros, which is what it held, so writes stay correct.lazy::ensurereturns at once for an empty range and takes a shared lock otherwise.What is in it
crates/xcl/src/vfs_hooks.rs: the five hooks and fault-safeensure_segments.crates/xcl/src/hle/kernelx/lazy.rs:markispubfor the integration test.crates/xcl/tests/lazy_file_io.rs(new):ReadFileExinto a lazy page,WriteFileandWriteFileExfrom one,ReadFileScatter/WriteFileGatherwith segments in two lazy pages, andscatter_and_gather_reject_invalid_segment_array_without_crashing(testing both an unmapped pointer0x1000and aPAGE_NOACCESSpage).Tested
cargo test --workspace --no-fail-fast -- --test-threads=4one5c2bc6(rebased ontomaine1d84b7):1426 passed, 2 failed, 46 ignored; 8 SKIPPED(6 inlower_dxil::tests::{f16,f32}_programs_on_the_gpu;heap_window_out_of_memory_falls_back_to_the_switch_form;pso_measurement_compiles_an_exact_synthetic_record)tests/lazy_file_io.rspass (includingscatter_and_gather_reject_invalid_segment_array_without_crashing).gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1seed 722 andfuzz_validate_and_gpu_3seed 1599) fail identically on unmodifiedmainon this GPU.Regression run
main (
08db8020):this PR (
e5c2bc6):Not tested
Nt*file calls (not hooked, as before); Wine; NVIDIA and Intel GPUs.AI use
maine1d84b7, madeensure_segmentsfault-safe withReadProcessMemoryand addedscatter_and_gather_reject_invalid_segment_array_without_crashing, reran the full workspace suite and 60 s smoke test, and updated this description.Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-24T15:16:38Z.
Full-suite run, since this PR has no test report yet. Tested exact head
b5d3ae8(on its base930e8b4).Tested
cargo build --workspace, thenXCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 755 passed, 0 failed, 41 ignored. SKIPPED (no wave64):gpu_geometry_wave32_prefix_count_over_a_mask,gpu_wave32_program_in_wave64_hardware.read_file_ex_into_a_lazy_page_reads_the_data,write_file_from_a_lazy_page_writes_zeros,write_file_ex_from_a_lazy_page_writes_zeros,scatter_and_gather_with_lazy_segments_move_the_data.Not tested
AI use
Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T16:28:11Z.
Verification on RTX 3060 Ti (Windows 11), branch merged with current
main:cargo test -p xcl --lib: all passed, 0 failed, ~21 ignored.Thanks!
upstream-sync referenced this pull request2026-11-24 19:06:45 +00:00
Imported from ivall/xcl. Originally posted by thehollowedone on 2026-11-25T09:20:57Z.
Independent Windows run on b5d3ae8. Windows 11 build 26300, Ryzen 7 9800X3D, RTX 3080 Ti (621.35), Rust 1.99.1 MSVC, DXC 1.8.2502.11.
The clean
cargo build --workspace --offline --locked -j 4passed.cargo test --workspace --offline --locked --no-fail-fast -j 4 -- --nocapture --test-threads=4finished with 755 passed, 0 failed, 41 ignored. Cargo's pass count includes two native wave64 runtime skips. All four stock lazy I/O tests passed.A scratch extension of
lazy_file_ioverified 16,384 bytes in each direction at a 4 KiB file offset. The scatter read put four distinct payloads into nonadjacent lazy pages in segment-array order. The gather write preserved the new order, read zeros from a fresh lazy page, and left the file's prefix and suffix intact. An unreferenced page stayed reserved. In a fresh process without the file hooks, the same scatter request completed zero bytes with Windows error 998. The focused test filter waslocal_scatter_gather_reordered_segments_and_nonzero_offsets.Synthetic valid requests only. Retail streaming was outside the test scope. AI use: Codex (GPT-6, exact variant unspecified) prepared the scratch fixture, ran the tests and drafted this report.
Imported from ivall/xcl. Originally posted by oni-swr on 2026-11-25T18:54:57Z.
AMD RDNA3 full-suite run on exact head
b5d3ae8(b5d3ae8f5c318d60223dfacb6652268059003dfa), merged ontomain3f27770(clean merge, local detached merge commit, nothing pushed).Tested
cargo test --workspace --no-fail-fast -- --test-threads=4on the merged tree: 1359 passed, 2 failed, 46 ignored. the newhle::kernelx::lazytests passed.maintoo:gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1andgpu::recomp::lower_dxil::tests::a_bridged_mask_write_where_one_half_is_elsewhere_meets_no_barrier.main3f27770alone, same command and machine: 1352 passed, 5 failed, 46 ignored. The 3 extra baseline failures (title_textures::placed_title_textures_share_vram_and_preserve_pixels,title_textures::transitions_apply_when_their_list_is_submitted,vram::d32s8_targets_take_their_own_size_of_video_memory) passed in this run; I did not rerun the baseline, so I can't say if they are flaky or load-dependent.main):f16_programs_on_the_gpuandf32_programs_on_the_gpu(refusals for unsupported V_*_F16 forms),heap_window_out_of_memory_falls_back_to_the_switch_form(no heap windows).Not tested
scripts/smoke.ps1/ the regression run, any retail title, a release build, other GPUs. A single run, not repeated.AI use
Imported from ivall/xcl. Originally posted by bmjask on 2026-11-25T21:46:13Z.
Review: COMMENTED
P1: ensure_segments raw-dereferences FILE_SEGMENT_ELEMENT array before original ReadFileScatter/WriteFileGather. Invalid/unreadable segment pointer can crash XCL instead of letting kernel API return error. Use fault-safe read/validate each segment-array entry and add invalid-array regression; valid lazy commits/ordinary async behavior otherwise look sound.
Imported from ivall/xcl. Originally posted by emirduran707 on 2026-11-25T23:03:50Z.
Thanks for the catch @bmjask! Fixed in
e5c2bc6(rebased onto currentmaine1d84b7):ensure_segmentsnow checksn == 0/ pointer arithmetic overflow and reads eachFILE_SEGMENT_ELEMENTentry throughReadProcessMemory(GetCurrentProcess(), ...)(afterlazy::ensureon the entry address) instead of raw-dereferencingsegs, so an invalid or unreadable segment array pointer returns cleanly to the originalReadFileScatter/WriteFileGatherAPI instead of crashing.scatter_and_gather_reject_invalid_segment_array_without_crashingincrates/xcl/tests/lazy_file_io.rscovering both an unmapped pointer (0x1000) and aPAGE_NOACCESSpage for bothReadFileScatterandWriteFileGather(all 5 integration tests pass; full workspace suite1426 passed, 2 pre-existing RX 9060 XT fuzz failed, 46 ignored; 60 s smoke1577 frames, 13962 MB peak, 0 device removals, 0 crashes).Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T05:03:16Z.
Re-verification on RTX 3060 Ti (Windows 11), new head merged with current
main:cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.Thanks!
— Devin
Imported from ivall/xcl. Originally posted by LocranKing on 2026-11-30T04:31:15Z.
merge-check claim
head:
e5c2bc6e23Imported from ivall/xcl. Originally posted by LocranKing on 2026-11-30T04:43:23Z.
merge-check
base:
f3bb664968head:
e5c2bc6e23merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2864 passed, 1 failed, 51 ignored (28 SKIPPED: 22 no wave64, 2 no d3d12SDKLayers.dll, 2 no heap windows, 1 no D3D12 debug layer, 1 Windows Developer Mode required to disable driver cache); failing: gpu::dxil::msaa_tests::gpu_direct_image_get_resinfo_d16 (also fails identically on base
f3bb6649alone with left (0, 0) vs right (3735928559, 3405691582), introduced on main betweenf97120dcandf3bb6649). All 5 new integration tests in tests/lazy_file_io.rs passed.review: none blocking. Read the whole diff across all 3 files (lazy.rs, vfs_hooks.rs, tests/lazy_file_io.rs) and checked the merge result on
f3bb6649. ReadFileEx, WriteFile and WriteFileEx guard against null buffers and call lazy::ensure(buf, n) like hk_read_file; ensure_segments checks overflow, commits lazy segment-array pages before reading each 8-byte FILE_SEGMENT_ELEMENT fault-safely via ReadProcessMemory(GetCurrentProcess(), ...), stops at a null terminator or unreadable entry so the kernel API returns its normal error, and commits each 4 KiB segment page. LAZY lock operations do not log or call WriteFile, so hooking WriteFile cannot re-enter LAZY. No duplicates in #1607 or open/merged PRs. Follow-up note (out of scope here): hk_read_file on main now also notifies d3d12x::file_reads::returned for DMA reads into graphics memory, which a separate PR could extend to ReadFileEx/ReadFileScatter if needed.performance: reasoned: no frame-time or extra memory cost. ReadFileEx/WriteFile/WriteFileEx add one shared LAZY RwLock read + BTreeSet range lookup (same as hk_read_file) ahead of an Nt* file syscall; scatter/gather does one in-process ReadProcessMemory per 4 KiB segment; only lazy pages explicitly passed to file I/O are committed, matching the author's matched smoke pair (1577 vs 1553 frames, 13962 vs 13966 MB peak commit).
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 3060 Ti 8 GB (driver 621.40 / 32.0.16.2140, wave32), AMD Ryzen 5 5500, Windows 11 build 26300.9974, rustc 1.100.0 (debug), DXC 1.9.2602.17 via XCL_DXC_DIR; Antigravity (Gemini 4 Carbon (High)) ran the merge, build, test suite, base failure verification, duplicate search, and diff review.
Imported from ivall/xcl. Originally posted by thehollowedone on 2026-11-30T04:53:32Z.
Follow-up on exact head
e5c2bc6e23664e79579fabb31cbbe1cb386c8b44: theReadFileExandWriteFileExtests stop after at most fiftySleepEx(100, TRUE)calls. If the request is still pending then, theWriteFileExtest lets its stackOVERLAPPEDleave scope, and the later assertion failure unwinds the lazy buffer. Windows requires both theOVERLAPPEDand data buffer to remain valid until completion (ReadFileEx, WriteFileEx). I confirmed this from the test paths and API contract; I did not reproduce a delayed completion.A safe timeout path is to request cancellation for that specific
OVERLAPPEDwithCancelIoEx, then keep the handle, structure and buffer alive and continue alertable waits until the completion routine records completion before asserting the timeout. Microsoft’s cancellation contract says cancellation does not itself wait for I/O completion.AI use: Codex (GPT-6.2 Sol) checked the test lifetime paths and drafted this follow-up.
Imported from ivall/xcl. Originally posted by Elytes on 2026-11-30T08:34:53Z.
merge-check
base:
d52ff48caahead:
e5c2bc6e23merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2881 passed, 1 failed, 51 ignored; SKIPPED: 28 (22 no wave64, 2 no heap windows, 3 no D3D12 debug layer / d3d12SDKLayers.dll, 1 driver cache needs Developer Mode); failing: gpu::dxil::msaa_tests::gpu_direct_image_get_resinfo_d16, which also fails on the base alone (base lib 2733 passed / 1 failed, same as the merge; this PR's tests are in tests/lazy_file_io.rs: 5 passed)
review: no blocking finding after reading the whole diff (3 files, +341/-1). The five hooks follow hk_read_file's pattern (lazy::ensure on the buffer, then the original). With vfs_hooks.rs reverted to the base and the new tests kept, 4 of the 5 tests fail exactly as the description says (ReadFileEx / ReadFileScatter 998, WriteFile / WriteFileEx 1784) and pass with the hooks; the fifth (invalid segment array) passes either way, as expected for a no-crash test. ensure_segments reads each FILE_SEGMENT_ELEMENT through ReadProcessMemory after committing the array's own lazy page, so a bad array pointer is left to the kernel. Note, not blocking: hk_read_file also reports the read to d3d12x::file_reads::returned (unbuffered reads into graphics memory become known writes); the new ReadFileEx / ReadFileScatter hooks do not. Such reads were not tracked before either, so nothing regresses, but now that they are hooked they could be. No duplicate found (open and merged PRs searched for these calls and for lazy pages in file I/O; #2359 and #1191 commit lazy pages on other paths). thehollowedone's note on the OVERLAPPED / buffer lifetime in the ReadFileEx / WriteFileEx tests' timeout path is valid; test-only, and only when a completion takes over 5 s: not blocking, but worth a follow-up.
performance: WriteFile is now hooked process-wide, so every write (XCL's own logs too) takes the lazy set's read lock; ensure returns at once when the set is empty, else a BTreeSet range lookup (reasoned, not measured). No memory cost beyond committing the lazy pages a call reads or writes.
verdict: safe to merge
hardware / tool and model: NVIDIA GeForce RTX 5090 (driver 32.0.16.1742, wave32), Intel Core i9-14900KS, 192 GB, Windows 11 build 26300.10075, rustc 1.100.1, debug / Claude Code (Claude Opus 5.5) ran the merge, build, suite, the base run and the run without the hooks, searched for duplicates and reviewed the diff.