cpu: handle reserved XMem backing during CLZERO emulation #826

Closed
moffington wants to merge 2 commits from upstream-pr/826 into main
moffington commented 2026-11-24 14:17:43 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by moffington on 2026-11-24T14:17:43Z.


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.

Windows: pending a tester, Linux/Wine only so far

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5473884924 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/826). Originally posted by [moffington](https://github.com/moffington) on 2026-11-24T14:17:43Z. --- 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. Windows: pending a tester, Linux/Wine only so far
abnalstlll-byte commented 2026-11-24 14:23:00 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by abnalstlll-byte on 2026-11-24T14:23:00Z.

Review: APPROVED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6040942173 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/826#pullrequestreview-6040942173). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-24T14:23:00Z. **Review:** APPROVED --- ### 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 commented 2026-11-24 14:26:13 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by abnalstlll-byte on 2026-11-24T14:26:13Z.

Review: APPROVED


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6041006749 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/826#pullrequestreview-6041006749). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-24T14:26:13Z. **Review:** APPROVED --- 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.
moffington commented 2026-11-24 16:24:07 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by moffington on 2026-11-24T16:24:07Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7137607765 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/826#issuecomment-7137607765). Originally posted by [moffington](https://github.com/moffington) on 2026-11-24T16:24:07Z. --- 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.
moffington commented 2026-11-25 13:57:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by moffington on 2026-11-25T13:57:48Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7164545782 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/826#issuecomment-7164545782). Originally posted by [moffington](https://github.com/moffington) on 2026-11-25T13:57:48Z. --- 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 commented 2026-11-25 20:13:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by moffington on 2026-11-25T20:13:30Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7173359541 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/826#issuecomment-7173359541). Originally posted by [moffington](https://github.com/moffington) on 2026-11-25T20:13:30Z. --- 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 commented 2026-11-26 02:55:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by moffington on 2026-11-26T02:55:19Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7181236316 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/826#issuecomment-7181236316). Originally posted by [moffington](https://github.com/moffington) on 2026-11-26T02:55:19Z. --- 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.
kurrutako commented 2026-12-01 08:51:08 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kurrutako on 2026-12-01T08:51:08Z.


Tested on Windows 11, Ryzen 7 7800X3D (no CLZERO here, so the runtime emulates it), MSVC 2022, Release.

  • Head d62aa5b75: cpu_emulation passes. cpu_emulation_xmem_sec_commit is skipped (exit 77, not under Wine).

  • On the base a73a57e13 without the fix, the new tests give 5 failures, so they cover the bug.

  • xmem_test fails one check ("the reader of the views' last pages never faults or misreads"). It fails the same way on this PR, on its base and on current main, so the PR did not cause it.

  • Conflicts with current main:

    • xmem.cpp: efc192990 added commit_bits to commit_mapped_page.
    • cpu_emulation.h: #2035 replaced writable() with writable_region(), and try_clzero_loop now steps by VirtualQuery regions.
    • cpu_emulation_test.cpp: the CLWB rewrite.
  • None of the failing CI checks are in this PR's files:

    • linux-gpu-tools: the vk_null_descriptor_test build fails.
    • macos: cs_imgatomic_ext fails on MoltenVK.
    • translator-tests: llvm-mc rejects a DPP variant.
    • macos-python: an export test fails.
    • windows: cancelled during Build, so the PR's own tests never ran in CI.

    A rebase and a CI re-run are needed.

  • The title's CLZERO loops already go through clzero_patch by default, so this path matters for sites it does not match or with LEONIDA_CLZERO_PATCH=0.

AI-assisted (Claude Code)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7305869622 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/826#issuecomment-7305869622). Originally posted by [kurrutako](https://github.com/kurrutako) on 2026-12-01T08:51:08Z. --- Tested on Windows 11, Ryzen 7 7800X3D (no CLZERO here, so the runtime emulates it), MSVC 2022, Release. - Head d62aa5b75: `cpu_emulation` passes. `cpu_emulation_xmem_sec_commit` is skipped (exit 77, not under Wine). - On the base a73a57e13 without the fix, the new tests give 5 failures, so they cover the bug. - `xmem_test` fails one check ("the reader of the views' last pages never faults or misreads"). It fails the same way on this PR, on its base and on current main, so the PR did not cause it. - Conflicts with current main: - `xmem.cpp`: efc192990 added commit_bits to `commit_mapped_page`. - `cpu_emulation.h`: #2035 replaced `writable()` with `writable_region()`, and `try_clzero_loop` now steps by `VirtualQuery` regions. - `cpu_emulation_test.cpp`: the CLWB rewrite. - None of the failing CI checks are in this PR's files: - `linux-gpu-tools`: the `vk_null_descriptor_test` build fails. - `macos`: `cs_imgatomic_ext` fails on MoltenVK. - `translator-tests`: llvm-mc rejects a DPP variant. - `macos-python`: an export test fails. - `windows`: cancelled during Build, so the PR's own tests never ran in CI. A rebase and a CI re-run are needed. - The title's CLZERO loops already go through `clzero_patch` by default, so this path matters for sites it does not match or with `LEONIDA_CLZERO_PATCH=0`. AI-assisted (Claude Code)
aslan2142 commented 2026-12-01 16:32:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:32:10Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7317767880 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/826#issuecomment-7317767880). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:32:10Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!826
No description provided.