Skip to content

fix(relinker): probe masked store elements before the store under --to-rosetta - #8142

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Kernel-Hunter:fix/rosetta-masked-stores
Oct 24, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Kernel-Hunter:fix/rosetta-masked-stores

Conversation

@Kernel-Hunter

Copy link
Copy Markdown
Contributor

What

Rosetta ends the process instead of raising SIGSEGV or SIGBUS when a masked store (VMASKMOVPS, VMASKMOVPD, VPMASKMOVD, VPMASKMOVQ) faults on any element other than an active first element:

rosetta error: unexpectedly need to EmulateForward on a synchronous exception

The macOS write watch keeps watched pages read-only and relies on that fault, so a masked store into a watched page ends the title. #4194 worked around the same failure for unaligned 256-bit stores. Zen 2 has these instructions, and clang emits them for the _mm_maskstore_* and _mm256_maskstore_* intrinsics.

With --to-rosetta, each masked store now moves to a stub that:

  1. saves RFLAGS, RAX and RCX below the 128-byte red zone,
  2. computes the operand address with LEA (RSP-based operands skip the spill frame, RIP-relative ones are relocated),
  3. reads the mask sign bits with VMOVMSKPS or VMOVMSKPD,
  4. for each active element, runs LOCK OR byte [address], 0 on its first and last byte, with the store's segment and address-size prefixes,
  5. restores the registers and runs the original store unchanged.

The locked writes fault the normal way, so the write watch handler records the page and unprotects it. They are atomic and leave memory unchanged, so a concurrent writer to the same bytes loses nothing. Once the pages are writable, the original store runs without faulting. The stub never touches inactive elements, and on hardware a masked-out element never faults either.

Not changed: masked loads (they fail the same way on an unreadable page, but nothing in the tree resolves read faults, so a fault there is fatal anyway), MASKMOVDQU and VMASKMOVDQU (they fault normally under Rosetta), the Intel target and a plain relink. The debt entry records the remaining gap: another thread making a page read-only again between the probes and the store still ends the process, and each masked store now costs up to 16 locked byte writes.

Changes:

  • MaskedStoreLowering.{hpp,cpp}: decoder for the eight store forms (VEX.66.0F38 2E/2F W0 and 8E W0/W1, L=0/1, memory operands only) and the stub emitter.
  • Amd64OnlyInstructionMatcher.cpp: the masked store case at the end of _matchRosetta and _emitOutOfLine.
  • New test masked_store_lowering:
    • decodes every form with all 16 mask and source registers, SIB with REX.X/REX.B, FS and address-size prefixes, RIP- and RSP-relative operands; loads, register forms, W1 for 2E/2F, other maps, a VEX2 form and a stray 66 prefix are not taken; trailing and truncated bytes throw;
    • checks the exact stub bytes for one VMASKMOVPD, the RSP adjustment and segment prefix on the probes, and that an overflowing stack displacement throws;
    • checks that the Intel matcher leaves masked stores alone and that the Rosetta matcher requires a trampoline;
    • on x86-64 Linux and macOS, runs 1728 stubs: 4 forms x 2 widths x 6 addressing modes ([rdi], [rsi+rbx*2+disp8], [rax+rcx*4+disp32] where the scratch registers are the base and index, [r9+r10*8], [rsp+0x40] on a stack placed next to the page, RIP-relative) x 6 offsets around a read-only page x 6 mask patterns. Each run checks the stored bytes, that nothing outside the active elements changed, that exactly one fault reaches a SIGSEGV/SIGBUS handler when an active element touches the read-only page and none otherwise, that all 16 YMM registers, the 7 GPRs used, RFLAGS and the red zone are unchanged, and that RSP is restored. The mask register also has its upper lanes set for the 128-bit forms, so a stub that reads the wrong width fails.
  • test_to_rosetta.py: a VMASKMOVPS is lowered only with --to-rosetta, for Linux and Mach-O output, and named in the output.
  • docs/dev/debt/relinker.md: new entry.

Tested

Apple M5, macOS 27.0.1 (26A434), Apple clang, relinker-only Release build.

A standalone clang -arch x86_64 program under Rosetta, with a SIGSEGV/SIGBUS handler that unprotects the page, stores to a read-write page followed by a read-only page:

vmaskmovps ymm, all lanes active, crossing into the read-only page    rosetta error, exit 134
vmaskmovps ymm, lane 0 inactive, entirely inside the read-only page   rosetta error, exit 134
vmaskmovps ymm, lane 0 active, at the start of the read-only page     SIGBUS delivered, handler resumes
vmaskmovps ymm, active lanes all in the read-write page                no fault
vmaskmovps/pd, vpmaskmovd/q, xmm and ymm, element 1 in the read-only page   rosetta error, all 8 forms
vmaskmovps ymm load, all lanes, crossing into a PROT_NONE page        rosetta error, exit 134
maskmovdqu and vmaskmovdqu, crossing into the read-only page          fault delivered, handler resumes
vmovups ymm store, crossing into the read-only page                   fault delivered, handler resumes
lock or byte [read-only page], 0                                      fault delivered, memory unchanged

Not tested on Linux or Windows. The execution test is also built for x86-64 Linux, so the Linux CI job runs it natively. No title was tested, and I don't know which titles use masked stores on watched memory.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes. The tests above were run on the hardware listed.

…o-rosetta

Rosetta ends the process instead of raising SIGSEGV or SIGBUS when a
VMASKMOVPS, VMASKMOVPD, VPMASKMOVD or VPMASKMOVQ store faults on any
element other than an active first element ("rosetta error: unexpectedly
need to EmulateForward on a synchronous exception"). Measured on an M5
with macOS 27.0.1 for all eight 128- and 256-bit store forms. A masked
store into a page the macOS write watch keeps read-only therefore ends
the title.

--to-rosetta now moves each masked store to a stub that saves RAX, RCX
and RFLAGS below the red zone, computes the operand address with LEA,
reads the mask sign bits with VMOVMSKPS or VMOVMSKPD, and writes to the
first and last byte of every active element with LOCK OR of 0. Those
writes fault normally, so the write watch sees the page and unprotects
it, and they leave memory unchanged. The stub then restores the
registers and runs the original store. Segment and address-size
prefixes are kept on the probes, RSP-based operands skip the spill
frame, and RIP-relative operands are relocated. Masked loads, the
Intel target and a plain relink are unchanged.

masked_store_lowering decodes every form, register and extension,
checks the stub bytes, and on x86-64 Linux and macOS runs 1728 stubs
across all forms, six addressing modes, six offsets around a read-only
page and six mask patterns. It checks the stored bytes, that exactly one
fault reaches the handler when an active element touches the page,
that no register, flag or red-zone byte changes, and that RSP is
restored. Without the probes the x86-64 build aborts under Rosetta.
test_to_rosetta.py checks that only --to-rosetta lowers a VMASKMOVPS.
@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/relinker/CMakeLists.txt
docs/dev/debt/relinker.md
PR 6044 core/relinker/CMakeLists.txt
PR 7173 core/relinker/CMakeLists.txt
core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp
docs/dev/debt/relinker.md
PR 8076 docs/dev/debt/relinker.md
PR 9228 docs/dev/debt/relinker.md
PR 9408 core/relinker/CMakeLists.txt
docs/dev/debt/relinker.md

@wachi35676

Copy link
Copy Markdown
Contributor

Verdict: confirm. These are results from x86-64 Linux, native on Zen 5, and from Windows, which the PR doesn't list.

Tested at 3cae921a6, merged onto main 02fb1ce53. Relinker-only Release builds on Windows 11 Home 10.0.26300 (Ryzen 5 9600X, WinLibs GCC 15.2.0 r7) and WSL Ubuntu 24.04 (GCC 13.3).

  • ctest: Windows 222/222, WSL 227/227 (main: 221/221 and 226/226).

  • WSL runs the execution part natively on the 9600X: 1728 masked store executions, Masked store lowering checks passed. On Windows the execution part is compiled out, as the test intends.

  • Negative controls (WSL):

    • with only the two probes.insert(...) lines removed from EmitMaskedStore: VMASKMOVPD stub does not probe its elements before the store, rc 1;
    • with Amd64OnlyInstructionMatcher.cpp reverted to main: Rosetta mode did not require a masked store trampoline, rc 1.

    So the test catches a stub without probes even on hardware where the store faults normally.

  • The PS5 homebrew I have contain masked stores, so --macos --to-rosetta output changes:

    title stores lowered (from the relinker's log) outputs changed
    PPSA99005 7 VMASKMOVPS, 38 VPMASKMOVD app.macho
    PPSA47402 57 VPMASKMOVD app.macho
    PPSA99581 33 VMASKMOVPS, 62 VPMASKMOVD, 20 VPMASKMOVQ app.macho
    PPSA99995 1 VPMASKMOVD in the executable app.macho and the Wine modules ntdll, win32u, wowprospero and libvulkan

    All four relink with rc 0. The --windows --to-intel and Linux --to-intel outputs of all four are byte-identical to main (8 of 8 sets), so only --to-rosetta is affected. I have no Mac, so I can't run the lowered titles under Rosetta.

AI-assisted: yes (Claude Code); results above were run on this machine.

@Kernel-Hunter

Copy link
Copy Markdown
Contributor Author

Thanks for running this on Zen 5 and Windows, and for the negative controls. Good to see the four titles relink cleanly and that the Intel outputs stay byte-identical.

@ycbuzi

ycbuzi commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Windows run of 3cae921 merged onto main 1fd6718 (7f30b073ef, clean), one of the hosts the description lists as not tested. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, BUILD_TESTING=ON.

Built relinker and masked_store_lowering_tests; masked_store_lowering passes (0.19 s) and to_rosetta passes (0.45 s; 0.39 s on main 1fd6718 built the same way, for comparison). Not run: the lowered code itself, since there is no Rosetta here.

@jotitadev jotitadev left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[OPTIONAL — the author already reports an Apple M5 run; this is an independent second Apple chip (M2 Max) plus an in-tree red/green. Publish only if a second Apple data point is wanted.]

Apple M2 Max, macOS 27.2 (26B5091g), Apple clang, CMake 4.4.4, Ninja, relinker-only Release build. Head 3cae921a6 merged onto current main 0c6a5bd59 (clean). Built arm64 and x86-64 (-DCMAKE_OSX_ARCHITECTURES=x86_64 -DPython3_EXECUTABLE=/usr/bin/python3, Python 3.9.6).

  • arm64: full relinker suite 230/230. x86-64 under Rosetta: full suite 230/230 (Python 3.9.6). masked_store_lowering and to_rosetta pass on both.
  • Under Rosetta the C++ masked_store_lowering runs its 1728-stub execution() (4 forms x 2 widths x 6 addressing modes x 6 page offsets x 6 masks) against a read-only page with a SIGSEGV/SIGBUS handler: exactly one fault per active element that touches the protected page and none otherwise, with the YMM/GPR/flags/red zone preserved. That is the write-watch scenario the PR targets, confirmed on an Apple host under Rosetta.
  • Red/green: neutralizing just the two masked-store hooks in _matchRosetta/_emitOutOfLine (keeping the test) makes to_rosetta fail with "did not lower VMASKMOVPS"; the full PR passes.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

@oneandonlydean oneandonlydean self-assigned this Oct 23, 2026
@oneandonlydean
oneandonlydean merged commit a011db7 into boykopovar:main Oct 24, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants