cpu: handle reserved XMem backing during CLZERO emulation #826

Open
github-bot wants to merge 2 commits from github-bot/gh-826 into main AGit
Owner

@⁠moffington on GitHub · 2026-11-24 14:17 UTC · original
moffington:submit/xmem-clzero-first-touch → main · +235 −15 · 5 files · 2 commits · open

Claimed in the coordination issue as: RT-CLZERO-XMEM

What and why

Emulated CLZERO raised a false write access violation on XMem pages that were untouched or that Wine still reported as reserved after a successful CPU write. The handler now checks the exact faulting subpage, commits its XMem backing when eligible, then rechecks live protection before zeroing the cache line. Initially read-only and unrelated mappings stay reserved. Read-only, no-access, and one-shot guard protections still apply.

Tested

  • The original synthetic regression failed on d0da7dade and passed with this change, including 20 repeat runs. It covers concurrent first touches and partially committed XMem pages.
  • d3d12_xmem_placed passed on an NVIDIA GeForce RTX 3060 with driver 615.78.08. Stock xmem reproduced the same five Wine failures before and after: two metadata checks, one commit-accounting check, and two protection-setup checks.
  • Omarchy, Wine 11.20, clang-cl 23.1.3, Windows SDK 10.0.26100. Native Windows and MSVC remain untested.
  • The batched CLZERO regression starts in reserved XMem and verifies that the emulation zeroes the writable prefix, reports a write fault at the exact read-only boundary, and leaves every byte from that boundary through the end of the requested span untouched.
  • The test target links the real XMem shim. The fixture runs in separate processes for SEC_RESERVE and Wine’s default SEC_COMMIT mode. Both CPU test cases passed three consecutive runs each on the rebased branch.
  • As a negative control, the new batch assertions failed as expected in SEC_RESERVE mode when built against the CPU-emulation header from either main or #2035.

AI-assisted with OpenAI Codex.

Known limitation

The protection check and the zeroing write are separate operations, so there is no atomicity guarantee against a concurrent remap or protection change.

Overlap with other open PRs

  • #2035: its region scan rejects reserved XMem pages and its header conflicts with the writable() change here. Whichever lands second needs the XMem first-touch check carried into writable_region().
  • #1499: its MSVC single-line SEH path bypasses writable(); clang-cl keeps the check. The test entry points conflict. A clang-cl/Wine control with its SEH write helper still passed the batch checks. Native MSVC is untested.
  • #1707: its XMem helper and test changes already conflict with main. If its fault-ahead commit is applied to emulated writes, the adjacent-page reservation assertion may need updating; the protected-boundary and byte-preservation assertions should still hold.

CI

CI pending on the rebased commit

**@⁠moffington** on GitHub · 2026-11-24 14:17 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/826) `moffington:submit/xmem-clzero-first-touch` → `main` · +235 −15 · 5 files · 2 commits · open Claimed in the coordination issue as: RT-CLZERO-XMEM ### What and why Emulated CLZERO raised a false write access violation on XMem pages that were untouched or that Wine still reported as reserved after a successful CPU write. The handler now checks the exact faulting subpage, commits its XMem backing when eligible, then rechecks live protection before zeroing the cache line. Initially read-only and unrelated mappings stay reserved. Read-only, no-access, and one-shot guard protections still apply. ### Tested - The original synthetic regression failed on `d0da7dade` and passed with this change, including 20 repeat runs. It covers concurrent first touches and partially committed XMem pages. - `d3d12_xmem_placed` passed on an NVIDIA GeForce RTX 3060 with driver 615.78.08. Stock `xmem` reproduced the same five Wine failures before and after: two metadata checks, one commit-accounting check, and two protection-setup checks. - Omarchy, Wine 11.20, clang-cl 23.1.3, Windows SDK 10.0.26100. Native Windows and MSVC remain untested. - The batched CLZERO regression starts in reserved XMem and verifies that the emulation zeroes the writable prefix, reports a write fault at the exact read-only boundary, and leaves every byte from that boundary through the end of the requested span untouched. - The test target links the real XMem shim. The fixture runs in separate processes for `SEC_RESERVE` and Wine’s default `SEC_COMMIT` mode. Both CPU test cases passed three consecutive runs each on the rebased branch. - As a negative control, the new batch assertions failed as expected in `SEC_RESERVE` mode when built against the CPU-emulation header from either `main` or #2035. AI-assisted with OpenAI Codex. ## Known limitation The protection check and the zeroing write are separate operations, so there is no atomicity guarantee against a concurrent remap or protection change. ## Overlap with other open PRs - #2035: its region scan rejects reserved XMem pages and its header conflicts with the `writable()` change here. Whichever lands second needs the XMem first-touch check carried into `writable_region()`. - #1499: its MSVC single-line SEH path bypasses `writable()`; clang-cl keeps the check. The test entry points conflict. A clang-cl/Wine control with its SEH write helper still passed the batch checks. Native MSVC is untested. - #1707: its XMem helper and test changes already conflict with `main`. If its fault-ahead commit is applied to emulated writes, the adjacent-page reservation assertion may need updating; the protected-boundary and byte-preservation assertions should still hold. ## CI CI pending on the rebased commit
A CLZERO emulation can encounter an XMem view whose backing is still reserved. Commit only the backing of a view that permits the attempted access, then check the faulting subpage again before writing. Keep existing XMem callers on their current commitment path.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:23:19 +00:00
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 14:23 UTC · original · approved these changes

Clean-Room Peer Review for PR #826 (cpu: handle reserved XMem backing during CLZERO emulation)

High-impact fix for emulated Zen 2 memory semantics on Intel/non-AMD host CPUs:

  • First-Touch Backing Commitment:
    • Implements commit_physical_page_for_write(address) in
      untime/shims/xmem.cpp to dynamically commit reserved XMem scratch backing when targeted by emulated CLZERO.
    • Respects page protection boundaries: safely verifies AllocationProtect when State == MEM_RESERVE and delivers PAGE_GUARD violations accurately before testing writability.
  • Defensive Error Handling:
    • Rejects read-only views, unmapped placeholders, and inaccessible subpages with STATUS_ACCESS_VIOLATION.
    • Accurately reports STATUS_GUARD_PAGE_VIOLATION on first touch of guard pages before resetting to standard write mode.
  • Concurrency & Unit Test Coverage:

untime/test/cpu_emulation_test.cpp adds est_xmem_clzero testing concurrent multi-threaded first touches on one XMem page via std::latch(2).

  • Tests exact 64-byte line clearing invariance (std::memcmp against zero array).
  • Clean-room verification: Zero game data, strict C++20 compliance, builds and passes on MSVC with /OPT:NOREF.

LGTM! Outstanding systems-level fix. Approved.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 14:23 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/826#pullrequestreview-6040942173) · **approved these changes** ### Clean-Room Peer Review for PR #826 (cpu: handle reserved XMem backing during CLZERO emulation) High-impact fix for emulated Zen 2 memory semantics on Intel/non-AMD host CPUs: - **First-Touch Backing Commitment**: - Implements commit_physical_page_for_write(address) in untime/shims/xmem.cpp to dynamically commit reserved XMem scratch backing when targeted by emulated CLZERO. - Respects page protection boundaries: safely verifies AllocationProtect when State == MEM_RESERVE and delivers PAGE_GUARD violations accurately before testing writability. - **Defensive Error Handling**: - Rejects read-only views, unmapped placeholders, and inaccessible subpages with STATUS_ACCESS_VIOLATION. - Accurately reports STATUS_GUARD_PAGE_VIOLATION on first touch of guard pages before resetting to standard write mode. - **Concurrency & Unit Test Coverage**: - untime/test/cpu_emulation_test.cpp adds est_xmem_clzero testing concurrent multi-threaded first touches on one XMem page via std::latch(2). - Tests exact 64-byte line clearing invariance (std::memcmp against zero array). - **Clean-room verification**: Zero game data, strict C++20 compliance, builds and passes on MSVC with /OPT:NOREF. LGTM! Outstanding systems-level fix. Approved.
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 14:26 UTC · original · approved these changes

Clean-room verified (zero game data). Crucial fix for Zen 2 CLZERO instruction emulation on Intel hosts touching lazily committed XMem physical memory: introduces commit_physical_page_for_write while strictly honoring PAGE_GUARD/PAGE_READONLY. Exhaustive concurrency and subpage tests in cpu_emulation_test.cpp.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 14:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/826#pullrequestreview-6041006749) · **approved these changes** Clean-room verified (zero game data). Crucial fix for Zen 2 CLZERO instruction emulation on Intel hosts touching lazily committed XMem physical memory: introduces commit_physical_page_for_write while strictly honoring PAGE_GUARD/PAGE_READONLY. Exhaustive concurrency and subpage tests in cpu_emulation_test.cpp.
Author
Owner

@⁠moffington on GitHub · 2026-11-24 16:24 UTC · original

Investigated both failed CI checks and confirmed identical failures at the PR's base (d0da7dade):

  • Windows: the same three unresolved synchronization imports from the Rust XMA2 decoder. Addressed by #877.
  • Linux GPU tools: the same cs_imgload_d16_tfe and cs_imgload_tfe failures tracked in #31.

[Baseline CI run](https://github.com/ubiquitarianism/leonida/actions/runs/45406612415)

Neither failure appears to be introduced by this PR.

**@⁠moffington** on GitHub · 2026-11-24 16:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/826#issuecomment-7137607765) Investigated both failed CI checks and confirmed identical failures at the PR's base (`d0da7dade`): - Windows: the same three unresolved synchronization imports from the Rust XMA2 decoder. Addressed by #877. - Linux GPU tools: the same `cs_imgload_d16_tfe` and `cs_imgload_tfe` failures tracked in #31. [[Baseline CI run](https://github.com/ubiquitarianism/leonida/actions/runs/45406612415)](https://github.com/ubiquitarianism/leonida/actions/runs/45406612415) Neither failure appears to be introduced by this PR.
Author
Owner

@⁠moffington on GitHub · 2026-11-25 13:57 UTC · original

Rebased #826 onto 77baf9062 and published ffdb44446. git merge-tree also passed against refreshed main at 71a769d7d. The rebase preserves upstream’s Wine SEC_COMMIT fast path and keeps the CLZERO fallback for subpages that still report MEM_RESERVE.

On Wine 11.20, 8/8 focused tests passed, including d3d12_xmem_placed on an RTX 3060. The CLZERO regression passed 20/20 runs. The same fixture reproduced four CLZERO failures on upstream main at 77baf9062.

#1499 overlaps in cpu_emulation_test.cpp and routes MSVC single-line CLZERO through an SEH-protected store that bypasses #826’s writable() check. The combined MSVC path remains unverified.

**@⁠moffington** on GitHub · 2026-11-25 13:57 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/826#issuecomment-7164545782) Rebased #826 onto `77baf9062` and published `ffdb44446`. `git merge-tree` also passed against refreshed `main` at `71a769d7d`. The rebase preserves upstream’s Wine `SEC_COMMIT` fast path and keeps the CLZERO fallback for subpages that still report `MEM_RESERVE`. On Wine 11.20, 8/8 focused tests passed, including `d3d12_xmem_placed` on an RTX 3060. The CLZERO regression passed 20/20 runs. The same fixture reproduced four CLZERO failures on upstream `main` at `77baf9062`. #1499 overlaps in `cpu_emulation_test.cpp` and routes MSVC single-line CLZERO through an SEH-protected store that bypasses #826’s `writable()` check. The combined MSVC path remains unverified.
Author
Owner

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

Rechecked mergeability against upstream main (db1a22d) with current HEAD ffdb444. The merge succeeds cleanly, so the needs rebase label appears stale. CI is currently queued.

I also checked the overlap with #1499. While its MSVC SEH approach addresses first-touch backing, #826 also covers the non-MSVC path. A trial merge of both PRs produces a conflict in runtime/test/cpu_emulation_test.cpp.

Flagging this for merge-order coordination so we preserve the correctness guarantees and regression coverage of both changes.

**@⁠moffington** on GitHub · 2026-11-25 20:13 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/826#issuecomment-7173359541) Rechecked mergeability against upstream `main` (`db1a22d`) with current HEAD `ffdb444`. The merge succeeds cleanly, so the `needs rebase` label appears stale. CI is currently queued. I also checked the overlap with #1499. While its MSVC SEH approach addresses first-touch backing, #826 also covers the non-MSVC path. A trial merge of both PRs produces a conflict in `runtime/test/cpu_emulation_test.cpp`. Flagging this for merge-order coordination so we preserve the correctness guarantees and regression coverage of both changes.
Author
Owner

@⁠moffington on GitHub · 2026-11-26 02:55 UTC · original

Investigated the Runtime workflow #1655 failures on ffdb444 (run 45628868378). None of the failures are introduced by this PR:

  • windows (job): 420/421 tests passed (the earlier XMA2 unresolved imports from #877 are gone). Only 419 - vk_xdxr_postbuild_readback failed because runtime/test/vk_xdxr_postbuild_readback_test.cpp returns 77 when no bufferDeviceAddress Vulkan device is present, but runtime/CMakeLists.txt registers the test without SKIP_RETURN_CODE 77 (upstream dfadf4ce5; also failing on main run #1288).
  • linux-gpu-tools (job): Build fails in runtime/test/vk_null_descriptor_test.cpp:97 (‘LEONIDA_TEST_NULL_DESCRIPTORS_SPV’ was not declared in this scope). linux-gpu-tools does not install glslangValidator, so CMake omits -DLEONIDA_TEST_NULL_DESCRIPTORS_SPV=...; main() is guarded on #ifndef LEONIDA_TEST_NULL_DESCRIPTORS_SPV, but run() at line 97 is not (upstream ba626820e, reproduced locally).
  • macos (job): Runtime tests on MoltenVK passed; GPU tests on MoltenVK failed on cs_imgatomic_ext (6 of 64 sint words differ; first word 32: got 0x0badf00d, expected 0x00000101), matching main run #1288 (upstream 367d91ef2).
  • wine (job): Skipped by .github/workflows/runtime.yml (github.event_name != 'pull_request'), not due to the windows failure.

Mergeability check: GitHub reports mergeable: true / rebaseable: true and git merge-tree against main (7f3d0095a) is clean, so the needs rebase label remains stale.

**@⁠moffington** on GitHub · 2026-11-26 02:55 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/826#issuecomment-7181236316) Investigated the Runtime workflow #1655 failures on `ffdb444` ([run 45628868378](https://github.com/ubiquitarianism/leonida/actions/runs/45628868378)). None of the failures are introduced by this PR: - **`windows` ([job](https://github.com/ubiquitarianism/leonida/actions/runs/45628868378/job/139256335205))**: 420/421 tests passed (the earlier XMA2 unresolved imports from #877 are gone). Only `419 - vk_xdxr_postbuild_readback` failed because `runtime/test/vk_xdxr_postbuild_readback_test.cpp` returns `77` when no `bufferDeviceAddress` Vulkan device is present, but `runtime/CMakeLists.txt` registers the test without `SKIP_RETURN_CODE 77` (upstream `dfadf4ce5`; also failing on `main` run [#1288](https://github.com/ubiquitarianism/leonida/actions/runs/45534464235)). - **`linux-gpu-tools` ([job](https://github.com/ubiquitarianism/leonida/actions/runs/45628868378/job/139256335959))**: Build fails in `runtime/test/vk_null_descriptor_test.cpp:97` (`‘LEONIDA_TEST_NULL_DESCRIPTORS_SPV’ was not declared in this scope`). `linux-gpu-tools` does not install `glslangValidator`, so CMake omits `-DLEONIDA_TEST_NULL_DESCRIPTORS_SPV=...`; `main()` is guarded on `#ifndef LEONIDA_TEST_NULL_DESCRIPTORS_SPV`, but `run()` at line 97 is not (upstream `ba626820e`, reproduced locally). - **`macos` ([job](https://github.com/ubiquitarianism/leonida/actions/runs/45628868378/job/139256334694))**: `Runtime tests on MoltenVK` passed; `GPU tests on MoltenVK` failed on `cs_imgatomic_ext` (`6 of 64 sint words differ; first word 32: got 0x0badf00d, expected 0x00000101`), matching `main` run [#1288](https://github.com/ubiquitarianism/leonida/actions/runs/45534464235) (upstream `367d91ef2`). - **`wine` ([job](https://github.com/ubiquitarianism/leonida/actions/runs/45628868378/job/139582653739))**: Skipped by `.github/workflows/runtime.yml` (`github.event_name != 'pull_request'`), not due to the `windows` failure. Mergeability check: GitHub reports `mergeable: true` / `rebaseable: true` and `git merge-tree` against `main` (`7f3d0095a`) is clean, so the `needs rebase` label remains stale.
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/826/head:github-bot/gh-826'
git switch 'github-bot/gh-826'

Merge

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