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

Open
github-bot wants to merge 1 commit from github-bot/gh-523 into main AGit
Owner

@⁠Grimkaaah on GitHub · 2026-11-24 01:16 UTC · original
Grimkaaah:fix/278-is-writable-guards → main · +117 −7 · 1 files · 1 commits · open

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

  • Interaction with #501's sparse::known_writable fast path (not on main yet); merge order should be fine — both can sit above the VirtualQuery walk.
  • NVIDIA / wave32-only GPUs (smoke and suite were on RDNA3).

AI use

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

**@⁠Grimkaaah** on GitHub · 2026-11-24 01:16 UTC · [original](https://github.com/xcl-dev/xcl/pull/523) `Grimkaaah:fix/278-is-writable-guards` → `main` · +117 −7 · 1 files · 1 commits · open 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** - Interaction with #501's `sparse::known_writable` fast path (not on `main` yet); merge order should be fine — both can sit above the VirtualQuery walk. - NVIDIA / wave32-only GPUs (smoke and suite were on RDNA3). **AI use** Implemented, tested, smoke runs, and PR description with Cursor (Composer). Plan posted on #626 / #278 before landing the complete PR.
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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:24:29 +00:00
Author
Owner

@⁠brothernog on GitHub · 2026-11-24 01:42 UTC · original

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.

**@⁠brothernog** on GitHub · 2026-11-24 01:42 UTC · [original](https://github.com/xcl-dev/xcl/pull/523#issuecomment-7119372963) 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.
Author
Owner

@⁠eizispark on GitHub · 2026-11-24 02:30 UTC · original

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

  • wave32-only GPUs (NVIDIA / Intel); gameplay past the menus; combined merge order with #501

AI use

  • Antigravity (Gemini 4 Carbon) ran the build, test suite, and scripts/smoke.ps1 harness on this machine and formatted this report.
**@⁠eizispark** on GitHub · 2026-11-24 02:30 UTC · [original](https://github.com/xcl-dev/xcl/pull/523#issuecomment-7120209224) 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** - wave32-only GPUs (NVIDIA / Intel); gameplay past the menus; combined merge order with #501 **AI use** - Antigravity (Gemini 4 Carbon) ran the build, test suite, and `scripts/smoke.ps1` harness on this machine and formatted this report.
Author
Owner

@⁠Grimkaaah on GitHub · 2026-11-24 02:44 UTC · original

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).

**@⁠Grimkaaah** on GitHub · 2026-11-24 02:44 UTC · [original](https://github.com/xcl-dev/xcl/pull/523#issuecomment-7120430599) 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).
Author
Owner

@⁠bunbeans2-arch on GitHub · 2026-11-24 04:43 UTC · original

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 #321: https://github.com/ivall/xcl/pull/321#issuecomment-7122269978), 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 #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.
**@⁠bunbeans2-arch** on GitHub · 2026-11-24 04:43 UTC · [original](https://github.com/xcl-dev/xcl/pull/523#issuecomment-7122283949) 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 #321: https://github.com/ivall/xcl/pull/321#issuecomment-7122269978), 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 #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.
Author
Owner

@⁠raylitebull on GitHub · 2026-11-24 10:40 UTC · original

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.

**@⁠raylitebull** on GitHub · 2026-11-24 10:40 UTC · [original](https://github.com/xcl-dev/xcl/pull/523#issuecomment-7129091830) 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.
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 13:43 UTC · original

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.)

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-25 13:43 UTC · [original](https://github.com/xcl-dev/xcl/pull/523#issuecomment-7164194562) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.) Thanks!
Author
Owner

@⁠upipibi on GitHub · 2026-11-25 20:25 UTC · original

NVIDIA test report for ed2f33c (head as is, base 3db536e), for #1607's "one more test report" list. RTX 3070 8 GB (Ampere, 32-lane waves), driver 616.56, Windows 11, debug, after cargo build --workspace:

  • cargo test --workspace --no-fail-fast -- --test-threads=4 --skip hle::xaudio2: 679 passed, 0 failed, 41 ignored. The end-to-end title tests pass.
  • Skipped hle::xaudio2, because its tests fail or hang on this machine on main too. The PR doesn't touch it.
  • SKIPPED lines: 2, both no wave64 on this card.

AI-assisted (Claude Code, Claude Opus 5.5). Synthetic tests only; no title run.

**@⁠upipibi** on GitHub · 2026-11-25 20:25 UTC · [original](https://github.com/xcl-dev/xcl/pull/523#issuecomment-7173657782) NVIDIA test report for `ed2f33c` (head as is, base `3db536e`), for #1607's "one more test report" list. RTX 3070 8 GB (Ampere, 32-lane waves), driver 616.56, Windows 11, debug, after `cargo build --workspace`: - `cargo test --workspace --no-fail-fast -- --test-threads=4 --skip hle::xaudio2`: **679 passed, 0 failed, 41 ignored**. The end-to-end title tests pass. - Skipped `hle::xaudio2`, because its tests fail or hang on this machine on `main` too. The PR doesn't touch it. - SKIPPED lines: 2, both `no wave64` on this card. AI-assisted (Claude Code, Claude Opus 5.5). Synthetic tests only; no title run.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/523/head:github-bot/gh-523'
git switch 'github-bot/gh-523'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-523'
git switch 'github-bot/gh-523'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-523'
git switch 'github-bot/gh-523'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-523'
git switch 'main'
git merge --squash 'github-bot/gh-523'
git switch 'main'
git merge --ff-only 'github-bot/gh-523'
git switch 'main'
git merge 'github-bot/gh-523'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.