fix(relinker): isolate Rosetta scalar compare hazards #5683
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5683"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T06:22:07Z.
Fixes #5642.
What
Rosetta 2 can miscompile certain VEX scalar compare instructions when a full 128-bit load follows in the same translated block. Move only the measured hazardous VEX scalar compare forms (predicates other than EQ_OQ, LT_OS, and LE_OS) out of line through the existing trampoline path. The original instruction executes in its own translated block, with RIP-relative memory operands relocated. Other predicates and packed, legacy, and EVEX forms remain unchanged.
The commit carries Leandro Soares’s tested implementation with his original commit authorship preserved.
Tested
amd64_only_converterpassed, including new out-of-line cases.progress_historyfails because this image has Python 3.11, whosetarfile.extractalldoes not accept thefilterargument used by that test.tools/check_conventions.py --base upstream/mainpassed.Checklist
main; no other open PR implements the same functionsImported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T10:51:59Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/relinker/CMakeLists.txtL52, L169, L352core/relinker/codegen/tests/Amd64OnlyConverterTests.cppL1674Some 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.
core/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtcore/relinker/CMakeLists.txtImported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-14T14:50:14Z.
Validated on Apple silicon under Rosetta 2: M2 Max, macOS 27.2 (26B5091g).
Relinker-only build of this branch (
8b400afc5)AppleClang, Ninja, Release,
ANYPS5_RELINKER_ONLY=ON,BUILD_TESTING=ON: 149/149 tests pass.Reproduction
The repro from #5642 fails as reported:
vucomisssees309.0 == 309.0as not equal. Ajmpbetween the compare and the 128-bit load makes it pass.I ran all 32 predicates with both a true and a false result. Pattern:
vcmpss→vmovaps(128-bit load into the compare destination) →vucomiss. Results are deterministic across runs:VCMPSSVCMPSD<= LE_OSexclusion inDecodeScalarCompareis correct.Out-of-line form
I wrote by hand the shape this PR emits: the compare in a stub followed by a
jmpback, with the load after the return. All predicates then give the correct result.I did not run the relinker's own output on macOS: it produces Linux ELF / Windows PE. The out-of-line check above uses hand-written code with the same shape, not converted code.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by 011010 on 2026-10-14T18:31:36Z.
Verified on macOS 27.0.1 (26A434), Apple M4, x86-64 under Rosetta 2, Apple clang 21: the relinker-only build passes
ctest149/149 with this change applied to currentmain.Imported from boykopovar/AnyPS5. Originally posted by JoaoPauloCMarra on 2026-10-15T10:05:32Z.
ran the relinker output end to end under Rosetta, since the earlier runs only checked the hand-written shape and the converter tests
M4 Pro, macOS 27.0.1 (26A434), Apple clang 21, PR head
8b400afc5, relinker-only buildctest 149/149 on arm64 and 149/149 on an x86_64 build under Rosetta (--timeout 30)
the check: a synthetic ELF with 40 sites (vcmpss predicates 0-31 and legacy cmpss 0-7). each site compares into xmm1, then a vmovaps loads 309.0 into xmm1, then a vucomiss checks the loaded value. I relinked it with --skip-sce-module, mapped the PT_LOAD segments and called each site from an x86_64 harness under Rosetta
main (with or without --to-rosetta) and this branch without it: 15/32 vcmpss and 3/8 cmpss lose the load
this branch with --to-rosetta: 29 vcmpss sites moved out of line (all but 0-2), 0/32 lose the load. legacy cmpss still 3/8, which #5762 covers
same result on two runs
the sites that fail are exactly the predicates that give a false result for 309.0 vs itself. here that includes 11 and 27 (FALSE_OQ, FALSE_OS), so on this machine they have to move out of line, it isnt just harmless
AI-assisted (Claude Code), I ran all of the above on my machine
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T11:58:26Z.
Checked the decoder (VEX2/VEX3, F3/F2 prefixes, predicates > LE_OS, RIP-relative relocation), the tests and the relinker ctest (133/133 green on the PR merged onto main). Independent Rosetta 2 runs on M2 Max / M4 / M4 Pro reproduce #5642 and show the out-of-line form fixes it end to end; legacy CMPSS is left to #5762. Merging.