PE loader: decode the AVX masked stores a Rosetta 2 fault cannot resume #1303

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

@⁠Lucasyudi on GitHub · 2026-11-24 14:31 UTC · original
Lucasyudi:cpu/maskmov-decode → main · +390 −0 · 2 files · 1 commits · open

First of four PRs that bring @⁠Ahmet-Dedeler's masked-store fix for Rosetta 2 from mac-on-main-wip (b51552b, 04e9301) to main, split by the size rule. Part of #258.

Rosetta 2 runs VMASKMOVPS / PD and VPMASKMOVD / Q to memory an element at a time and resumes a fault only on the first element; a fault on a later one ends the process (rosetta error: unexpectedly need to EmulateForward on a synchronous exception). Under Wine on macOS the write watch keeps pages protected, so a title's masked store that runs into a watched page ends the run. The fix replaces each such store at image load with a call to a helper that stores one element at a time. This PR is only the decoding; nothing calls it yet.

Plan for the rest (each its own PR, stacked): 2. the helpers, call_site and their unwind data, with the tests that run each helper against the instruction itself; 3. plan, which finds the sites of an image by walking from the start of each function; 4. the loader step (XCL_MASKED_STORES, on only under Rosetta 2), the one runtime change.

What is in it

  • maskmov.rs (new): insn_len (length of an x86-64 instruction, None for what a walk must not guess at), decode / is_candidate (the four stores with any memory operand), and two unit tests, instruction_lengths and decodes_the_masked_stores. Code and tests are Ahmet's from b51552b, unchanged; only the module comment now lists what this PR has. The commit keeps him as its author.
  • lib.rs: pub mod maskmov;.

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: 2 passed, 0 failed.
  • The whole b51552b module on main 930e8b4 (cherry-picked as is): maskmov:: 7 passed, 1 ignored. The ignored one (the_instruction_itself_across_a_watched_page_boundary), run with --ignored, ends the process here as on Ahmet's M5 Pro: "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.

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: b558ec333047102add1ba3c5ec38c594cd25c087
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 60
device removals: 0
crashes: 0
peak commit: 14813 MB

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

Not tested

  • Windows: not built with MSVC, not run. The tests are pure byte decoding and do not depend on the host.
  • cargo test --workspace as a whole (it does not finish under CrossOver, see #258).
  • Ahmet checked insn_len against Capstone on 22.4 million instructions of a retail image (in his HANDOFF notes on mac-on-main-wip); I did not repeat that.

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.
**@⁠Lucasyudi** on GitHub · 2026-11-24 14:31 UTC · [original](https://github.com/xcl-dev/xcl/pull/1303) `Lucasyudi:cpu/maskmov-decode` → `main` · +390 −0 · 2 files · 1 commits · open First of four PRs that bring @⁠Ahmet-Dedeler's masked-store fix for Rosetta 2 from `mac-on-main-wip` (`b51552b`, `04e9301`) to `main`, split by the size rule. Part of #258. Rosetta 2 runs VMASKMOVPS / PD and VPMASKMOVD / Q to memory an element at a time and resumes a fault only on the first element; a fault on a later one ends the process (`rosetta error: unexpectedly need to EmulateForward on a synchronous exception`). Under Wine on macOS the write watch keeps pages protected, so a title's masked store that runs into a watched page ends the run. The fix replaces each such store at image load with a call to a helper that stores one element at a time. This PR is only the decoding; nothing calls it yet. Plan for the rest (each its own PR, stacked): 2. the helpers, `call_site` and their unwind data, with the tests that run each helper against the instruction itself; 3. `plan`, which finds the sites of an image by walking from the start of each function; 4. the loader step (`XCL_MASKED_STORES`, on only under Rosetta 2), the one runtime change. **What is in it** - `maskmov.rs` (new): `insn_len` (length of an x86-64 instruction, `None` for what a walk must not guess at), `decode` / `is_candidate` (the four stores with any memory operand), and two unit tests, `instruction_lengths` and `decodes_the_masked_stores`. Code and tests are Ahmet's from `b51552b`, unchanged; only the module comment now lists what this PR has. The commit keeps him as its author. - `lib.rs`: `pub mod maskmov;`. **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: 2 passed, 0 failed. - The whole `b51552b` module on `main` `930e8b4` (cherry-picked as is): `maskmov::` 7 passed, 1 ignored. The ignored one (`the_instruction_itself_across_a_watched_page_boundary`), run with `--ignored`, ends the process here as on Ahmet's M5 Pro: "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`. **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: b558ec333047102add1ba3c5ec38c594cd25c087 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 60 device removals: 0 crashes: 0 peak commit: 14813 MB ``` Nothing calls this code yet, so the pair shows only that the title runs as on `main`. **Not tested** - Windows: not built with MSVC, not run. The tests are pure byte decoding and do not depend on the host. - `cargo test --workspace` as a whole (it does not finish under CrossOver, see #258). - Ahmet checked `insn_len` against Capstone on 22.4 million instructions of a retail image (in his HANDOFF notes on `mac-on-main-wip`); I did not repeat that. **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.
Rosetta 2 runs VMASKMOVPS / PD and VPMASKMOVD / Q to memory an element at
a time and resumes a fault only on the first element; a fault on a later
one ends the process ("rosetta error: unexpectedly need to EmulateForward
on a synchronous exception"). Under Wine on macOS the write watch keeps
pages protected, so a title's masked store that crosses into a watched
page ends the run. The remedy (later commits) replaces each such store at
image load with a call to a helper that stores one element at a time.

This commit is the first part: `maskmov::insn_len`, the length of an
x86-64 instruction (to walk a function from its start to a candidate
site), and `maskmov::decode`, the four stores with any memory operand.
Nothing calls them yet.

Split out of mac-on-main-wip b51552b (#258).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:50:04 +00:00
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-24 15:47 UTC · original

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!

**@⁠DavidM1911** on GitHub · 2026-11-24 15:47 UTC · [original](https://github.com/xcl-dev/xcl/pull/1303#issuecomment-7136604374) 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!
Author
Owner

@⁠Toons29 on GitHub · 2026-11-25 03:22 UTC · original

Regression run of b558ec33 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: b558ec333047102add1ba3c5ec38c594cd25c087
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 60
device removals: 0
crashes: 0
peak commit: 14813 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.

**@⁠Toons29** on GitHub · 2026-11-25 03:22 UTC · [original](https://github.com/xcl-dev/xcl/pull/1303#issuecomment-7151848370) Regression run of `b558ec33` 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: b558ec333047102add1ba3c5ec38c594cd25c087 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 60 device removals: 0 crashes: 0 peak commit: 14813 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.
Author
Owner

@⁠upipibi on GitHub · 2026-11-26 04:41 UTC · original

NVIDIA test report for b558ec3: 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: 700 passed, 0 failed, 21 ignored; 2 SKIPPED lines (both no wave64). The new maskmov::tests::instruction_lengths and decodes_the_masked_stores pass.

Lib tests only; no title run.

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

**@⁠upipibi** on GitHub · 2026-11-26 04:41 UTC · [original](https://github.com/xcl-dev/xcl/pull/1303#issuecomment-7182816944) NVIDIA test report for `b558ec3`: 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: 700 passed, 0 failed, 21 ignored; 2 SKIPPED lines (both `no wave64`). The new `maskmov::tests::instruction_lengths` and `decodes_the_masked_stores` pass. Lib tests only; no title run. AI-assisted (Claude Code, Claude Opus 5.5).
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/1303/head:github-bot/gh-1303'
git switch 'github-bot/gh-1303'

Merge

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