PE loader: helpers that do a masked store an element at a time #1301

Open
Lucasyudi wants to merge 2 commits from upstream-pr/1311 into main
Lucasyudi commented 2026-11-24 14:36:37 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Lucasyudi on 2026-11-24T14:36:37Z.


Stacked on #1303; only the top commit (4b30c37) belongs here. Second of four PRs bringing @Ahmet-Dedeler's masked-store fix for Rosetta 2 from mac-on-main-wip (b51552b) to main. Part of #258.

Under Rosetta 2 a masked store (VMASKMOVPS / PD, VPMASKMOVD / Q to memory) that faults on any element but the first ends the process. This PR adds the code that replaces such a store: a helper that does the same store one element at a time with plain movs, which fault and resume like any store. Nothing calls it yet; PR 3 finds the sites in an image, PR 4 applies them at load under Rosetta 2.

Why this is over 800 lines: about 120 are the helper code, the rest is its proof. The exec tests are a harness (an assembly runner that loads and saves every register, the flags, YMM0–15 and the stack) that runs each helper and the real instruction from the same state and compares them. Splitting the helper from those tests would leave a PR whose code nothing checks.

What is in it

  • maskmov.rs: helper (position-independent code: address into R8 first, source and mask spilled to its stack, one guarded mov per element using CDQ / JRCXZ, so no flag or vector register is written and a masked-out element is not touched), call_site (call helper + NOPs over the store), UNWIND_INFO (shared by all helpers, so a fault in one unwinds into the title's function), and HELPER_MAX.
  • Tests: a_helper_is_the_address_two_spills_and_a_store_per_element (the bytes); exec (Windows x86-64 only): a_helper_does_what_the_instruction_does (224 stores, 4 kinds × XMM / YMM × 28 operand shapes, every mask: 32,704 runs), a_masked_out_tail_may_end_where_the_mapping_ends, a_helper_resumes_after_a_fault_on_a_later_element (a write-watched page, as Wine's write watch makes it).
  • the_instruction_itself_across_a_watched_page_boundary is #[ignore]: it runs the real instruction across a watched page boundary, which ends the test process under Rosetta 2. It is the reproduction of the bug, not a test that is skipped to pass; run it with --ignored.
  • Code and tests are Ahmet's, unchanged; the commit keeps him as author. Only the module comment changed to list what is there so far.

Tested

  • Apple M4 Pro, macOS 27.2.1, CrossOver 26.3, Rosetta 2. Cross-built for x86_64-pc-windows-gnu with mingw-w64 (GCC 16.2.0), Rust 1.100.0, debug; the lib test binary run under CrossOver's wine.
  • maskmov:: on this head: 6 passed, 0 failed, 1 ignored.
  • --exact maskmov::exec::the_instruction_itself_across_a_watched_page_boundary --ignored: prints "a fault on the first element was resumed; now one on the fourth", then rosetta error: unexpectedly need to EmulateForward on a synchronous exception … inst_index=18 and the process ends, as on Ahmet's M5 Pro.

Regression run

From @Toons29: AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, Windows 11 build 26300, release builds, scripts/smoke.ps1, the same retail title for 60 s, main 4178e88 and this head: 100 % of main's frames, 100 % of its peak commit, no crash, no device removal.

xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14831 MB
xcl-smoke v1
commit: 4b30c37ac68af66b2e1b4e88791e82debe3f373b
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1623 (PresentX 1623)
presents per 2 s (last 3): 60 60 53
device removals: 0
crashes: 0
peak commit: 14814 MB

Nothing calls this code yet, so the pair shows only that the title runs as on main.

Not tested

  • Windows on a real CPU: not run by me. CI's Windows runner will run the exec tests against the hardware's own masked stores, which is the comparison that matters most.
  • cargo test --workspace as a whole (it does not finish under CrossOver, see #258).

AI use

  • Claude Code with Claude Opus 5.5 (claude-opus-5-5): split the commit, ran the tests above and wrote this description. The code and tests are Ahmet's.
<!-- greyfinch-sync:ivall/xcl:issue:5474226775 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1311). Originally posted by [Lucasyudi](https://github.com/Lucasyudi) on 2026-11-24T14:36:37Z. --- Stacked on #1303; only the top commit (`4b30c37`) belongs here. Second of four PRs bringing @Ahmet-Dedeler's masked-store fix for Rosetta 2 from `mac-on-main-wip` (`b51552b`) to `main`. Part of #258. Under Rosetta 2 a masked store (VMASKMOVPS / PD, VPMASKMOVD / Q to memory) that faults on any element but the first ends the process. This PR adds the code that replaces such a store: a helper that does the same store one element at a time with plain `mov`s, which fault and resume like any store. Nothing calls it yet; PR 3 finds the sites in an image, PR 4 applies them at load under Rosetta 2. Why this is over 800 lines: about 120 are the helper code, the rest is its proof. The `exec` tests are a harness (an assembly runner that loads and saves every register, the flags, YMM0–15 and the stack) that runs each helper and the real instruction from the same state and compares them. Splitting the helper from those tests would leave a PR whose code nothing checks. **What is in it** - `maskmov.rs`: `helper` (position-independent code: address into R8 first, source and mask spilled to its stack, one guarded `mov` per element using CDQ / JRCXZ, so no flag or vector register is written and a masked-out element is not touched), `call_site` (`call helper` + NOPs over the store), `UNWIND_INFO` (shared by all helpers, so a fault in one unwinds into the title's function), and `HELPER_MAX`. - Tests: `a_helper_is_the_address_two_spills_and_a_store_per_element` (the bytes); `exec` (Windows x86-64 only): `a_helper_does_what_the_instruction_does` (224 stores, 4 kinds × XMM / YMM × 28 operand shapes, every mask: 32,704 runs), `a_masked_out_tail_may_end_where_the_mapping_ends`, `a_helper_resumes_after_a_fault_on_a_later_element` (a write-watched page, as Wine's write watch makes it). - `the_instruction_itself_across_a_watched_page_boundary` is `#[ignore]`: it runs the real instruction across a watched page boundary, which ends the test process under Rosetta 2. It is the reproduction of the bug, not a test that is skipped to pass; run it with `--ignored`. - Code and tests are Ahmet's, unchanged; the commit keeps him as author. Only the module comment changed to list what is there so far. **Tested** - Apple M4 Pro, macOS 27.2.1, CrossOver 26.3, Rosetta 2. Cross-built for `x86_64-pc-windows-gnu` with mingw-w64 (GCC 16.2.0), Rust 1.100.0, debug; the lib test binary run under CrossOver's `wine`. - `maskmov::` on this head: 6 passed, 0 failed, 1 ignored. - `--exact maskmov::exec::the_instruction_itself_across_a_watched_page_boundary --ignored`: prints "a fault on the first element was resumed; now one on the fourth", then `rosetta error: unexpectedly need to EmulateForward on a synchronous exception … inst_index=18` and the process ends, as on Ahmet's M5 Pro. **Regression run** From @Toons29: AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, Windows 11 build 26300, release builds, `scripts/smoke.ps1`, the same retail title for 60 s, `main` `4178e88` and this head: 100 % of `main`'s frames, 100 % of its peak commit, no crash, no device removal. ``` xcl-smoke v1 commit: 4178e888c0b5be50a2887eda45cdb832763b68a4 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1618 (PresentX 1618) presents per 2 s (last 3): 61 60 60 device removals: 0 crashes: 0 peak commit: 14831 MB ``` ``` xcl-smoke v1 commit: 4b30c37ac68af66b2e1b4e88791e82debe3f373b gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1623 (PresentX 1623) presents per 2 s (last 3): 60 60 53 device removals: 0 crashes: 0 peak commit: 14814 MB ``` Nothing calls this code yet, so the pair shows only that the title runs as on `main`. **Not tested** - Windows on a real CPU: not run by me. CI's Windows runner will run the `exec` tests against the hardware's own masked stores, which is the comparison that matters most. - `cargo test --workspace` as a whole (it does not finish under CrossOver, see #258). **AI use** - Claude Code with Claude Opus 5.5 (`claude-opus-5-5`): split the commit, ran the tests above and wrote this description. The code and tests are Ahmet's.
DavidM1911 commented 2026-11-24 16:07:10 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T15:38:42Z.


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

  • cargo test -p xcl --lib -- --test-threads=1: all passed, 0 failed, ~21 ignored. (Parallel runs intermittently abort with 0xc0000005 — same harness flake seen on unmodified main.)

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7136373116 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/1311#issuecomment-7136373116). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T15:38:42Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib -- --test-threads=1`: all passed, 0 failed, ~21 ignored. (Parallel runs intermittently abort with 0xc0000005 — same harness flake seen on unmodified `main`.) Thanks!
Toons29 commented 2026-11-25 04:10:26 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Toons29 on 2026-11-25T03:22:53Z.


Regression run of 4b30c37a on the retail title, for this PR's Regression run section. Both blocks are from scripts/smoke.ps1 as printed, not edited: main on 4178e888, this PR on its head commit with no uncommitted changes, same title and time.

xcl-smoke v1
commit: 4178e888c0b5be50a2887eda45cdb832763b68a4
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1618 (PresentX 1618)
presents per 2 s (last 3): 61 60 60
device removals: 0
crashes: 0
peak commit: 14831 MB
xcl-smoke v1
commit: 4b30c37ac68af66b2e1b4e88791e82debe3f373b
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1623 (PresentX 1623)
presents per 2 s (last 3): 60 60 53
device removals: 0
crashes: 0
peak commit: 14814 MB

Against the limits in CONTRIBUTING.md: 100 % of main's frames, 100 % of its peak commit, no crash, no device removal.

AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, cargo build --release -p xcl, 60 s, XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.

AI use: Claude Code (Claude Opus 5.5) built both trees and ran scripts/smoke.ps1; I checked the blocks before posting.

<!-- greyfinch-sync:ivall/xcl:comment:7151860802 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1311#issuecomment-7151860802). Originally posted by [Toons29](https://github.com/Toons29) on 2026-11-25T03:22:53Z. --- Regression run of `4b30c37a` on the retail title, for this PR's **Regression run** section. Both blocks are from `scripts/smoke.ps1` as printed, not edited: `main` on `4178e888`, this PR on its head commit with no uncommitted changes, same title and time. ``` xcl-smoke v1 commit: 4178e888c0b5be50a2887eda45cdb832763b68a4 gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1618 (PresentX 1618) presents per 2 s (last 3): 61 60 60 device removals: 0 crashes: 0 peak commit: 14831 MB ``` ``` xcl-smoke v1 commit: 4b30c37ac68af66b2e1b4e88791e82debe3f373b gpu: AMD Radeon RX 9070 title: game.exe seconds: 60 exit: 0x0 frames shown: 1623 (PresentX 1623) presents per 2 s (last 3): 60 60 53 device removals: 0 crashes: 0 peak commit: 14814 MB ``` Against the limits in CONTRIBUTING.md: 100 % of `main`'s frames, 100 % of its peak commit, no crash, no device removal. AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0, `cargo build --release -p xcl`, 60 s, `XCL_NETWORK=0`. This is a regression check only: it does not show whether the run reaches the code this PR changes. AI use: Claude Code (Claude Opus 5.5) built both trees and ran `scripts/smoke.ps1`; I checked the blocks before posting.
upipibi commented 2026-11-26 06:06:38 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by upipibi on 2026-11-26T05:10:11Z.


NVIDIA test report for 4b30c37: RTX 3070 8 GB (Ampere, driver 616.56), Windows 11 26100, Rust 1.100.0, debug.

  • cargo test -p xcl --lib -- --skip hle::xaudio2 --test-threads=4 (the hle::xaudio2 tests hang on this machine, on main too):
  • this PR: 704 passed, 0 failed, 22 ignored; 2 SKIPPED lines (both no wave64). The six new maskmov tests pass, including exec::a_helper_does_what_the_instruction_does and exec::a_helper_resumes_after_a_fault_on_a_later_element; the ignored one is the PR's own the_instruction_itself_across_a_watched_page_boundary.

Lib tests only; no title run.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ivall/xcl:comment:7183242120 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1311#issuecomment-7183242120). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-26T05:10:11Z. --- NVIDIA test report for `4b30c37`: RTX 3070 8 GB (Ampere, driver 616.56), Windows 11 26100, Rust 1.100.0, debug. - `cargo test -p xcl --lib -- --skip hle::xaudio2 --test-threads=4` (the `hle::xaudio2` tests hang on this machine, on `main` too): - this PR: 704 passed, 0 failed, 22 ignored; 2 SKIPPED lines (both `no wave64`). The six new `maskmov` tests pass, including `exec::a_helper_does_what_the_instruction_does` and `exec::a_helper_resumes_after_a_fault_on_a_later_element`; the ignored one is the PR's own `the_instruction_itself_across_a_watched_page_boundary`. Lib tests only; no title run. AI-assisted (Claude Code, Claude Opus 5.5).
Lucasyudi commented 2026-11-29 15:50:36 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Lucasyudi on 2026-11-29T15:50:36Z.


Thanks @upipibi for the NVIDIA run. Each part of the masked-store stack now has a run from someone other than me; it waits for a maintainer review and merges cleanly into main.

<!-- greyfinch-sync:ivall/xcl:comment:7258411574 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1311#issuecomment-7258411574). Originally posted by [Lucasyudi](https://github.com/Lucasyudi) on 2026-11-29T15:50:36Z. --- Thanks @upipibi for the NVIDIA run. Each part of the masked-store stack now has a run from someone other than me; it waits for a maintainer review and merges cleanly into `main`.
Foniass commented 2026-12-01 05:24:10 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T05:24:10Z.


Triage (#1607) – head 4b30c37

This is the second PR of a stack: it is stacked on #1303, and #1313 and #1344 sit on top of it. The shared lines are the stack, not a duplicate. No other open or merged PR adds masked-store helpers.
I read the top commit (maskmov::helper, call_site, the tests). The frame offset of 104 for rsp-based operands, the jrcxz skip lengths, and the fact that flags are preserved all check out.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7301732693 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1311#issuecomment-7301732693). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T05:24:10Z. --- **Triage (#1607)** – head 4b30c37 This is the second PR of a stack: it is stacked on #1303, and #1313 and #1344 sit on top of it. The shared lines are the stack, not a duplicate. No other open or merged PR adds masked-store helpers. I read the top commit (`maskmov::helper`, `call_site`, the tests). The frame offset of 104 for rsp-based operands, the `jrcxz` skip lengths, and the fact that flags are preserved all check out. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1311:upstream-pr/1311'
git switch 'upstream-pr/1311'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1311'
git switch 'upstream-pr/1311'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1311'
git switch 'upstream-pr/1311'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1311'
git switch 'main'
git merge --squash 'upstream-pr/1311'
git switch 'main'
git merge --ff-only 'upstream-pr/1311'
git switch 'main'
git merge 'upstream-pr/1311'
git push origin 'main'
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
xcl-dev/xcl!1301
No description provided.