Repository navigation
fix(relinker): probe masked store elements before the store under --to-rosetta - #8142
Conversation
…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.
|
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.
|
|
Verdict: confirm. These are results from x86-64 Linux, native on Zen 5, and from Windows, which the PR doesn't list. Tested at
AI-assisted: yes (Claude Code); results above were run on this machine. |
|
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. |
|
Windows run of 3cae921 merged onto Built |
jotitadev
left a comment
There was a problem hiding this comment.
[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_loweringandto_rosettapass on both. - Under Rosetta the C++
masked_store_loweringruns its 1728-stubexecution()(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) makesto_rosettafail 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.
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:
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:LOCK OR byte [address], 0on its first and last byte, with the store's segment and address-size prefixes,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_matchRosettaand_emitOutOfLine.masked_store_lowering:[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_64program under Rosetta, with a SIGSEGV/SIGBUS handler that unprotects the page, stores to a read-write page followed by a read-only page:ctest, 210/210 pass.-DCMAKE_OSX_ARCHITECTURES=x86_64), where the execution test runs: 210/210 pass, includingmasked_store_lowering(1728 executions, about 1 s),amd64_only_converter,andn_loweringandto_rosetta.masked_store_loweringends with the samerosetta error.python3 tools/check_conventions.py --base origin/mainandgit diff --checkpass.python3 tools/pr_overlap.py --branch fix/rosetta-masked-storesreports no overlapping pull request. It could not compare feat(aot): consolidate native ARM64 compiler, runtime and consumers #5021, test: organize library tests and label CTest suites #6044, fix(relinker): skip jump tables placed after a function's code #6618, feat(wasm): run relinked titles in the browser with WebGPU #6879, feat: run PS4 games on the AGC driver — Limbo (CUSA01664) in game (integration branch) #6977 and feat: macOS on Apple Silicon host support (Rosetta 2 and MoltenVK) #7332 because they conflict withmain. None of them changes how--to-rosettahandles masked stores. feat: macOS on Apple Silicon host support (Rosetta 2 and MoltenVK) #7332 carries the ADCX/ADOX matcher change from fix(relinker): lower ADCX and ADOX for Rosetta #7173, which merges cleanly with this branch, and feat(aot): consolidate native ARM64 compiler, runtime and consumers #5021 and feat(wasm): run relinked titles in the browser with WebGPU #6879 translate masked moves in their own AOT and WebAssembly backends.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
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)