Skip to content

eh: harden is_writable for PAGE_GUARD, regions, and lazy pages (#626) - #523

Open
Grimkaaah wants to merge 1 commit into
xcl-dev:mainfrom
Grimkaaah:fix/278-is-writable-guards
Open

Grimkaaah wants to merge 1 commit into
xcl-dev:mainfrom
Grimkaaah:fix/278-is-writable-guards

Conversation

@Grimkaaah

@Grimkaaah Grimkaaah commented Nov 24, 2026 •

Copy link
Copy Markdown

Fixes #626 (item 3 of #278: is_writable guards). Items 1–2 are already on main; item 4 is #321. Complements #501 (sparse-window fast path) — this PR fixes the VirtualQuery fallback that #501 still uses.

Emulated CLZERO / ClzeroLoop asked whether the destination was writable with a single VirtualQuery. That was wrong in four ways:

  1. PAGE_GUARD. PAGE_READWRITE | PAGE_GUARD (0x104) passed & (PAGE_READWRITE | PAGE_EXECUTE_READWRITE) != 0, so the VEH would write through a guard page instead of letting the real fault happen.
  2. One region only. A ClzeroLoop can cover adjacent committed runs with different protect; one query only sees the first.
  3. Unchecked va + bytes. A wrapping end was undefined.
  4. Lazy physical pages. A page still in MEM_RESERVE (lazy map) made is_writable false → 0xC000001D, instead of committing via lazy::ensure and zeroing.

What is in it

  • eh::is_writable now matches the shape of dstorage::writable: checked_add → lazy::ensure → UMA coherent-window fast path → walk every VirtualQuery region covering [va, end), requiring a writable protect (READWRITE / WRITECOPY / EXECUTE_*) and no PAGE_GUARD.
  • Unit tests (VirtualAlloc only): guard refused then accepted after VirtualProtect; PAGE_NOACCESS and wrapping end; span across two differently-protected committed pages; reserved hole fails; lazy page commits through ensure; CLZERO on a guard page returns 0 without clearing the guard.

Tested

  • Machine: Windows 11, AMD Radeon RX 7900 XTX, MSVC, rustc from rust-toolchain.toml.
  • cargo test -p xcl --lib eh::tests -- --nocapture: 8 passed, 0 failed.
  • cargo test -p xcl --lib -- --test-threads=4: 648 passed, 0 failed, 21 ignored; 0 GPU SKIPPED.
  • Head commit of this PR: ed2f33c.

Regression run

Same title and wall time on main and on this PR's head (clean tree):

xcl-smoke v1
commit: 3db536ebe231418d76e8d6e52ccee0b47330a522
gpu: AMD Radeon RX 7900 XTX
title: 2c508222
seconds: 60
exit: 0x0
frames shown: 949 (PresentX 950)
presents per 2 s (last 3): 26 35 39
device removals: 0
crashes: 0
peak commit: 15483 MB
xcl-smoke v1
commit: ed2f33c4180389ab3d926995aff111f5c987bae2
gpu: AMD Radeon RX 7900 XTX
title: 2c508222
seconds: 60
exit: 0x0
frames shown: 1355 (PresentX 1356)
presents per 2 s (last 3): 58 56 57
device removals: 0
crashes: 0
peak commit: 15483 MB

Not tested

AI use

Implemented, tested, smoke runs, and PR description with Cursor (Composer). Plan posted on #626 / #278 before landing the complete PR.

@brothernog

Copy link
Copy Markdown

Tested 056cb61 on an RTX 3050 (driver 610.88), Windows 11 Home 26300, Rust 1.100.0, debug:

  • cargo test -p xcl --lib eh::tests: 8 passed.
  • Negative control: I swapped in main's is_writable and kept the new tests.
    • is_writable_guards_regions_overflow_and_lazy_pages fails at "PAGE_GUARD must not count as writable".
    • clzero_refuses_a_guard_page kills the test process with 0x80000001 (STATUS_GUARD_PAGE_VIOLATION): the old CLZERO wrote through the guard.
  • cargo build --workspace then cargo test --workspace --no-fail-fast -- --test-threads=4: 679 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.

The new is_writable has the same steps as dstorage::writable (checked end, lazy::ensure, coherent window, region walk without PAGE_GUARD), plus a next <= at guard.

On the interaction with #501, which the description mentions: #501 puts sparse::known_writable before this walk. On my machine its working-set check reports guarded, no-access and read-only pages as writable, details on #501. For a committed sparse-window page a title has guarded, CLZERO would then skip this PR's PAGE_GUARD check. Whichever merges second may want to keep the guard check ahead of that fast path.

This review was AI-assisted.

T-Crypt added a commit to T-Crypt/bathyal that referenced this pull request Nov 24, 2026
@eizispark

Copy link
Copy Markdown

Hardware check of 056cb61ac7506e423c307832c88e009720fd2091 (verified clean merge with main cb868b2): pass.

Tested

  • Hardware: AMD Radeon RX 9070 XT (RDNA4, driver 32.0.32015.16001), AMD Ryzen 7 9800X3D, 32 GB RAM, Windows 11, rustc 1.100.0 (0ef1bc386 2026-11-09), release + test profile
  • cargo test --workspace -- --test-threads=4: 679 passed, 0 failed, 41 ignored (GPU tests skipped on this adapter: none)
  • cargo test -p xcl --lib eh::tests: 8 passed, 0 failed

Regression run
main (9ce2070):

xcl-smoke v1
commit: 9ce2070eccf436d7bd38601c90407da447df34f3
gpu: AMD Radeon RX 9070 XT
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1213 (PresentX 1213)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 15615 MB

PR (056cb61):

xcl-smoke v1
commit: 056cb61ac7506e423c307832c88e009720fd2091
gpu: AMD Radeon RX 9070 XT
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1576 (PresentX 1576)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14759 MB

Not tested

AI use

  • Antigravity (Gemini 4 Carbon) ran the build, test suite, and scripts/smoke.ps1 harness on this machine and formatted this report.

…xcl-dev#626)

ClzeroLoop used a single VirtualQuery that treated PAGE_READWRITE|PAGE_GUARD as
writable and skipped lazy physical pages still in MEM_RESERVE. Walk every region
like dstorage::writable, reject guards, checked_add the end, and ensure lazy
pages before answering.

Assisted-by: Cursor (Composer)
@Grimkaaah
Grimkaaah force-pushed the fix/278-is-writable-guards branch from 056cb61 to ed2f33c Compare November 24, 2026 02:44
@Grimkaaah Grimkaaah changed the title eh: harden is_writable for PAGE_GUARD, regions, and lazy pages (#278) eh: harden is_writable for PAGE_GUARD, regions, and lazy pages (#626) Nov 24, 2026
@Grimkaaah

Copy link
Copy Markdown
Author

Rebased onto current main (3db536e), linked to dedicated issue #626, and filled Regression run with real xcl-smoke blocks (main + head ed2f33c, GTA6 60s, RX 7900 XTX).

@bunbeans2-arch

Copy link
Copy Markdown

Test report for ed2f33c4180389ab3d926995aff111f5c987bae2 on an Intel CPU without CLZERO (i9-9900K), where the emulated CLZERO path this PR changes is the one taken. The other runs here were on RDNA3 and on a Ryzen 7 9800X3D, which runs CLZERO itself.

Tested

  • Intel Core i9-9900K (CPUID: no CLZERO, CLWB, RDPRU, RDPID, MONITORX, SSE4a), AMD Radeon RX 9070 XT 16 GB (Adrenalin driver 32.0.32015.7029), 32 GB RAM, Windows 11 Pro 23H2 build 22631.6199, Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug.
  • This PR's head as is (base 3db536e): cargo build --workspace + cargo test --workspace -- --test-threads=4: 688 passed, 0 failed, 41 ignored, 0 SKIPPED (D3D12 backend on the RX 9070 XT); eh::tests::clzero_refuses_a_guard_page and eh::tests::is_writable_guards_regions_overflow_and_lazy_pages pass. Control, main f4ce27c: 701 passed, 0 failed, 41 ignored.
  • Real faults (a temporary test module appended to eh.rs, not part of this PR; its source is in my report on Console CPU instructions: serialize CLWB hot-patching and flush icache (#278) #321: Console CPU instructions: serialize CLWB hot-patching and flush icache (#278) #321 (comment)), on this PR's head merged onto main f4ce27c (clean merge): the instructions run for real, the CPU raises STATUS_ILLEGAL_INSTRUCTION and eh's handler takes it.
    • clzero at a line in a read-write buffer: 1 fault, that line zeroed and the bytes around it left as they were.
    • the CLZERO loop (clzero; add rax, 0x40; cmp rax, rdx; jb) over 0x400 bytes: 1 fault, the 0x400 bytes zeroed and the rest left as it was.
    • RDPRU, RDPID, MONITORX, MWAITX, EXTRQ and INSERTQ: 1 fault each, results as the test expects (the same as on main).

Not tested

  • A retail title or a scripts/smoke.ps1 run (no game files were used), a release build.
  • A real CLZERO fault on a guard page, a lazily committed page or a span across regions (only the unit tests above cover those), other Intel generations.
  • On the same merge, the multi-threaded CLWB part of that test fails as it does on main: that is the CLWB hot-patch race of Console CPU instructions: serialize CLWB hot-patching and flush icache (#278) #321, which this PR does not touch.

AI use

  • Claude Code. The real-fault test and the runs: Claude Fable 5.2; this report: Claude Opus 5.5 (the session's model was switched partway). All numbers come from the runs above on this machine.

@raylitebull

Copy link
Copy Markdown
Member

Code/CI review only (not independently runtime-tested by me): clean merge and PR description green (just awaiting CI workflow approval). Hardens eh::is_writable for PAGE_GUARD, multi-region spans, and lazy pages (#626). Recommend approving CI and merging, @ivall.

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.

eh: harden is_writable for PAGE_GUARD, multi-region spans, and lazy pages

5 participants