Skip to content

kernelx: VirtualProtect commits lazy pages in its range first - #1191

Open
emirduran707 wants to merge 1 commit into
xcl-dev:mainfrom
emirduran707:virtualprotect-lazy-pages
Open

emirduran707 wants to merge 1 commit into
xcl-dev:mainfrom
emirduran707:virtualprotect-lazy-pages

Conversation

@emirduran707

Copy link
Copy Markdown
Contributor

MapTitlePhysicalPages / XMemMapPhysicalPages map fresh pages into a title's own reservation lazily: the page stays reserved (lazy::mark) until it is first touched. A title that maps pages and calls VirtualProtect on them before touching them got FALSE with ERROR_INVALID_ADDRESS (487). Windows' VirtualProtect fails when any page in the range is not committed. On the console the pages are mapped, and XCL's non-lazy path for the same call commits them, so there the call succeeds. lazy.rs itself notes that kernel calls fail on lazy pages and that buffers passed to them are committed first (ensure). The VirtualProtect wrapper did not do that.

VirtualProtect, and VirtualProtectEx for the current process, now call lazy::ensure on the range first.

What is in it

  • crates/xcl/src/hle/kernelx.rs: the two calls; test virtual_protect_commits_lazy_pages_first. It reserves two 64 KiB pages and marks them lazy, then calls VirtualProtect(PAGE_READONLY). It checks success, that old is PAGE_READWRITE, and that the memory reads zero.

#1034 adds a different line at the top of VirtualProtect; the two changes are independent (a rebase keeps both lines).

Tested

  • AMD Radeon RX 9060 XT (driver 32.0.31044.16), Ryzen 5 5600, Windows 11 Pro 26300, Rust 1.100.0; tests debug, smoke release
  • cargo test --workspace -- --test-threads=4 on d0211db: 742 passed, 0 failed, 41 ignored; no SKIPPED GPU tests
  • the new test without the change: left: (0, 487), right: (1, 0); with it cargo test -p xcl --lib hle::kernelx passes (32 passed, 1 ignored as before)

Regression run

main:

xcl-smoke v1
commit: 47efa2ecea0a12934719ae69b81930cf02a4b1c3
gpu: AMD Radeon RX 9060 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1541 (PresentX 1541)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 19650 MB

this PR:

xcl-smoke v1
commit: d0211db87edcb1bbbc5b68463b70c0dc31f1294c
gpu: AMD Radeon RX 9060 XT
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1559 (PresentX 1559)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 19753 MB

Not tested

  • A title that protects freshly mapped pages before touching them; the smoke title's run is a regression check only.
  • VirtualProtectEx with a real handle to the own process (only the pseudo handle is treated as the current process, as VirtualFreeEx does).
  • NVIDIA and Intel GPUs.

AI use

  • Claude Code with Claude Opus 5.5: found the problem, wrote the change, the test and this description; I reviewed the diff and ran the tests and smoke runs above.

🤖 Generated with Claude Code

Physical pages mapped into the title's own reservation are left reserved
until first touched (lazy::mark in map_pages); the fault handler commits
them on a CPU access, but kernel calls do not fault, they fail. Windows'
VirtualProtect fails when any page of the range is not committed, so a
title that maps pages and changes their protection before touching them
got FALSE with ERROR_INVALID_ADDRESS (487), where the non-lazy path (and
the console) succeeds.

VirtualProtect now commits the lazy pages of the range first with
lazy::ensure, as the file and DirectStorage paths already do;
VirtualProtectEx does the same when the handle is the current process.

The new test reserves two 64 KiB pages, marks them lazy and protects them
read-only: before the fix it got (0, 487), now it succeeds, returns
PAGE_READWRITE as the old protection and the pages read as zeros.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thehollowedone

Copy link
Copy Markdown

I checked the real-handle case listed under Not tested on d0211db. Windows 11 build 26300, Ryzen 7 9800X3D, Rust 1.99.1 MSVC, debug.

A scratch test reserved a page, mapped a fresh physical page with XMemMapPhysicalPages, and confirmed MEM_RESERVE with VirtualQuery before asking VirtualProtectEx for PAGE_READONLY. The pseudo-handle succeeded. A real handle opened on the same process with only PROCESS_VM_OPERATION returned 487 and the page stayed reserved. CompareObjectHandles recognized it as the same process, while GetProcessId returned zero on that handle.

In scratch, I used CompareObjectHandles for the identity check and committed the lazy page through VirtualAllocEx on the supplied handle, removing it from the lazy set after a successful commit. Both the pseudo-handle and real VM-operation handle then succeeded, returned the previous PAGE_READWRITE protection and read back zero. A query-only handle still returned ERROR_ACCESS_DENIED and left its page reserved.

cargo test --offline --locked -p xcl --lib virtual_protect -- --nocapture --test-threads=1 passed 2/2 with that candidate. Both stock hle::kernelx::lazy::tests also passed. This was a synthetic same-process check on Windows. Foreign processes, Wine and retail were outside the test.

AI-assisted. Codex (GPT-6, exact variant unavailable) wrote and ran the scratch checks and drafted this report.

@DavidM1911

Copy link
Copy Markdown
Contributor

Independent verification on current main 384f7f7 (branch already contains it — clean).

Machine: Windows 11, Ryzen 9 7900X + RTX 3060 Ti; Rust stable MSVC, debug.

  • cargo test -p xcl --lib kernelx: 33 passed, 0 failed, 1 ignored — the VirtualProtect lazy-commit change keeps the kernelx suite green.

AI use: test runs by Devin (Cognition, SWE-2); no code changes.

@fromthemoonx

Copy link
Copy Markdown

Full-suite run, to go with the kernelx run above.

Tested

  • NVIDIA GeForce RTX 3060 12 GB, driver 32.0.16.1088; AMD Ryzen 7 5700X; native Windows 11 Pro 26H2 (build 26300.9974); rustc 1.100.0 stable-x86_64-pc-windows-msvc; debug
  • Exact head d0211db87edcb1bbbc5b68463b70c0dc31f1294c, which is on current main 47efa2e: cargo build --workspace, then cargo test --workspace --no-fail-fast -- --test-threads=4: 742 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • Fails without the change: with kernelx.rs as on main plus only virtual_protect_commits_lazy_pages_first, the test fails with VirtualProtect on lazy pages, left: (0, 487), right: (1, 0): FALSE with ERROR_INVALID_ADDRESS, as the description says.
  • hle::kernelx tests (lazy pages included), 10 runs: 10 of 10 passed (32 passed, 1 ignored each)
  • e2e, 3 runs: 3 of 3 passed

Not tested

  • A retail title calling VirtualProtect on freshly mapped pages; release build.

AI use

  • Claude Code (Claude Opus 5.5) ran the build and tests on the machine above, including the check without the change, and wrote this report from the output.

@JoaoPauluu

Copy link
Copy Markdown

A run on Linux under Wine.

Tested

  • RTX 3060 12 GB (NVIDIA 620.70.06, 32-lane waves), i5-10400F, 16 GB, CachyOS (kernel 7.3.3), Proton Experimental 11.0-20261112 with the patched vkd3d-proton 3.1 from Linux: docs/linux.md, scripts/linux and Wine start-up checks for vkd3d-proton and DXVK (#154, #241) #257, DXC 1.9.2609.5. Release, cross-built for x86_64-pc-windows-gnu. d0211db as it is (based on 47efa2e), no uncommitted changes. The lib test binary runs under Wine, one test per process (--exact). VirtualProtect is Wine's here, so this checks the lazy-page commit against Wine's memory manager.
  • hle::kernelx::tests::virtual_protect_commits_lazy_pages_first: ok, no SKIPPED.

Not tested

  • whether the new tests fail without the change on this stack; the rest of the suite on this head; a retail title; native Windows.

AI use

  • Claude Code (Claude Opus 5.5) built the head, ran the tests and wrote this report. The account owner asked for test reports to be posted and has not reviewed this one.

gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 24, 2026
…vall#1142, ivall#1150, ivall#1156, ivall#1182, ivall#1191, ivall#1195, ivall#1221

Integrate 10 high-impact open upstream PRs on top of upstream/main (930e8b4):
- ivall#1182: share video memory across render targets aliased onto one physical page (~1.85-2.1 GB VRAM saved)
- ivall#1142: declare tiled texture pools texture-only (D3D12_HEAP_FLAG_DENY_BUFFERS) for vkd3d-proton
- ivall#1097: report process memory under Wine via PagefileUsage fallback
- ivall#1078: bound BCPack record scratch memory to 2 KiB stack batches
- ivall#1150: convert console clear-value formats in CreatePlacedResource
- ivall#1156: release the interface returned by QueryInterface in class_for
- ivall#1139: check pointer alignment in is_adopted before dereferencing
- ivall#1195: skip ExecuteIndirect dispatch of untranslated console compute pipelines
- ivall#1191: VirtualProtect commits lazy pages in its range first
- ivall#1221: unmark lazy away mappings in FreeTitlePhysicalPages

Assisted-by: Antigravity (Google DeepMind)
gustavsi added a commit to gustavsi/xcl that referenced this pull request Nov 24, 2026
…ll#1221, and updated Linux PRs

Update fork README for upstream/main 930e8b4 (BCPack BC1-BC7, XCL_PSO_WAIT_MS, low-VA ASLR < 2^40, alloc_pool, sparse tile parking/pool reuse, direct page moves, write-watch merge), newly integrated PRs (ivall#1078, ivall#1097, ivall#1139, ivall#1142, ivall#1150, ivall#1156, ivall#1182, ivall#1191, ivall#1195, ivall#1221), and updated Linux PRs (ivall#867, ivall#871, ivall#877).

Assisted-by: Antigravity (Google DeepMind)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants