Skip to content

PE loader: find a title's masked stores by walking its functions - #1313

Open
Lucasyudi wants to merge 3 commits into
xcl-dev:mainfrom
Lucasyudi:cpu/maskmov-plan
Open

Lucasyudi wants to merge 3 commits into
xcl-dev:mainfrom
Lucasyudi:cpu/maskmov-plan

Conversation

@Lucasyudi

@Lucasyudi Lucasyudi commented Nov 24, 2026 •

Copy link
Copy Markdown

Stacked on #1311 (which is stacked on #1303); only the top commit (0f1fdf6) belongs here. Third of four PRs bringing @Ahmet-Dedeler's masked-store fix for Rosetta 2 from mac-on-main-wip (b51552b) to main. Part of #258.

plan finds the masked stores in a mapped image and writes their helpers (#1311). It does not trust a byte pattern: it walks the instructions from the start of the function that the unwind data names and takes the pattern only if the walk lands exactly on it, so bytes inside another instruction or in data are left alone. A store in a leaf function (no unwind data) is walked to from the end of the function before it. Nothing calls plan yet; PR 4 calls it from the loader under Rosetta 2.

What is in it

  • maskmov.rs: plan, Plan, Site, Skip (why a pattern was left: no function, an instruction the walk does not know, not at an instruction boundary, a form the helper does not take) and GAP_MAX (how far behind a function a leaf function's store may be, 64 KiB).
  • Test a_site_is_a_pattern_the_walk_from_its_function_arrives_at: a made-up image with a store in a function, two in a row sharing a helper, one in a leaf function, the pattern inside a mov rax, imm64, behind an EVEX instruction, and far from any function; plus a section that ends inside a pattern and one with no function.
  • Code and test are Ahmet's, unchanged (the module is now the whole of b51552b's maskmov.rs); only the module comment's last line says the loader step comes later. The commit keeps him as author.

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: 7 passed, 0 failed, 1 ignored (the reproduction described in PE loader: helpers that do a masked store an element at a time #1311).

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: 101 % 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: 0f1fdf640f0bc1a294c438201c8ced8cf286c025
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1631 (PresentX 1631)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14815 MB

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

Not tested

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 test are Ahmet's.

Ahmet-Dedeler and others added 3 commits November 24, 2026 11:31
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 (xcl-dev#258).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The second part of the masked-store replacement for Rosetta 2.
`maskmov::helper` writes position-independent code that does what one
store does: it takes the address into R8 while every register still holds
the title's value, spills source and mask to its stack, and stores each
selected element with a plain `mov` (CDQ / JRCXZ on the mask's sign: no
flag and no vector register written, a masked-out element not touched).
A plain `mov` faults and resumes like any store, element by element.
`call_site` is the `call` + NOPs that replaces a store; `UNWIND_INFO` is
the unwind data all helpers share, so a fault in a helper unwinds into the
title's function like one at the store.

Tests: the helper's bytes, and under `exec` (Windows x86-64 only) each of
224 stores (4 kinds x XMM / YMM x 28 operand shapes) run against the
instruction itself for every mask - registers, flags, YMM0-15, stack and
memory identical; a masked-out tail at the end of a mapping; a fault on a
later element in a write-watched page resumed. The ignored test runs the
instruction itself across a watched page boundary: under Rosetta 2 it
ends the process, which is the finding.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The third part of the masked-store replacement for Rosetta 2.
`maskmov::plan` finds the stores in the executable sections of a mapped
image and writes their helpers. A byte pattern alone is not taken: the
instructions are walked with `insn_len` from the start of the function the
unwind data names, and the walk has to arrive exactly at the pattern. A
pattern outside every function is walked to from the end of the function
before it (a leaf function has no unwind data), within 64 KiB. Stores with
the same bytes share a helper (a RIP-relative one gets its own). Every
pattern left alone is recorded with why.

Test: a made-up image with a store in a function, two in a row, one in a
leaf function, the pattern inside another instruction's immediate, behind
an EVEX instruction the walk does not know, and far from any function.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@DavidM1911

Copy link
Copy Markdown
Contributor

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

Toons29 commented Nov 25, 2026

Copy link
Copy Markdown

Regression run of 0f1fdf64 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: 0f1fdf640f0bc1a294c438201c8ced8cf286c025
gpu: AMD Radeon RX 9070
title: game.exe
seconds: 60
exit: 0x0
frames shown: 1631 (PresentX 1631)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14815 MB

Against the limits in CONTRIBUTING.md: 101 % 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

upipibi commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

NVIDIA test report for 0f1fdf6: 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: 705 passed, 0 failed, 22 ignored; 2 SKIPPED lines (both no wave64). The seven maskmov tests pass, including the new a_site_is_a_pattern_the_walk_from_its_function_arrives_at; 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

Copy link
Copy Markdown
Author

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

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head 0f1fdf6

This is the third PR of a stack: it is stacked on #1311 (on #1303), and #1344 sits on top of it. The shared lines are the stack, not a duplicate. No other open or merged PR finds masked-store sites.
I read the top commit (maskmov::plan and its test) and found no problems. Note that nothing calls plan until #1344.
Recommendation: keep open.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants