fix(relinker): isolate Rosetta scalar compare hazards #5683

Merged
MarcZX10 merged 1 commit from upstream-pr/5683 into main 2026-10-15 12:57:15 +00:00
MarcZX10 commented 2026-10-14 06:22:07 +00:00 (Migrated from github.com)

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

  • Linux, GCC 12.2, Release, relinker-only build: build succeeded and amd64_only_converter passed, including new out-of-line cases.
  • Full CTest: 157/158 passed. progress_history fails because this image has Python 3.11, whose tarfile.extractall does not accept the filter argument used by that test.
  • tools/check_conventions.py --base upstream/main passed.
  • macOS Rosetta was not available locally; the Rosetta-specific reproduction and predicate/addressing-mode matrix are from the issue reporter.

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-path policy: not applicable; no new unimplemented path
  • No notes, investigation files, or images added
  • No third-party code added
  • Generic behavior, not title-specific
  • Depends on: none
  • Unblocks: no title ID identified in #5642
  • GPUs tested: n/a (relinker-only)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4853843623 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5683). Originally posted by [MarcZX10](https://github.com/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 - Linux, GCC 12.2, Release, relinker-only build: build succeeded and `amd64_only_converter` passed, including new out-of-line cases. - Full CTest: 157/158 passed. `progress_history` fails because this image has Python 3.11, whose `tarfile.extractall` does not accept the `filter` argument used by that test. - `tools/check_conventions.py --base upstream/main` passed. - macOS Rosetta was not available locally; the Rosetta-specific reproduction and predicate/addressing-mode matrix are from the issue reporter. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented-path policy: not applicable; no new unimplemented path - [x] No notes, investigation files, or images added - [x] No third-party code added - [x] Generic behavior, not title-specific - [x] Depends on: none - [x] Unblocks: no title ID identified in #5642 - [x] GPUs tested: n/a (relinker-only) - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-14 10:51:59 +00:00 (Migrated from github.com)

Imported 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.

Pull request Conflicting files (lines on main) Same exports Same new files
#5784 core/relinker/CMakeLists.txt L52, L169, L352
#6111 core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp L1674

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
#3891 core/relinker/CMakeLists.txt
#4156 core/relinker/CMakeLists.txt
#4396 core/relinker/CMakeLists.txt
#4397 core/relinker/CMakeLists.txt
#4422 core/relinker/CMakeLists.txt
#4423 core/relinker/CMakeLists.txt
#4424 core/relinker/CMakeLists.txt
#4520 core/relinker/CMakeLists.txt
#4521 core/relinker/CMakeLists.txt
#4522 core/relinker/CMakeLists.txt
#4523 core/relinker/CMakeLists.txt
#4566 core/relinker/CMakeLists.txt
#4599 core/relinker/CMakeLists.txt
#4718 core/relinker/CMakeLists.txt
#4740 core/relinker/CMakeLists.txt
#4749 core/relinker/CMakeLists.txt
#4760 core/relinker/CMakeLists.txt
#4813 core/relinker/CMakeLists.txt
#5021 core/relinker/CMakeLists.txt
#6044 core/relinker/CMakeLists.txt
#6112 core/relinker/CMakeLists.txt
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6166694339 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5683#issuecomment-6166694339). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-14T10:51:59Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #5784 | `core/relinker/CMakeLists.txt` [L52](https://github.com/boykopovar/AnyPS5/blob/71bd2876e56a06e462e269b9d13616e07053ccad/core/relinker/CMakeLists.txt#L52), [L169](https://github.com/boykopovar/AnyPS5/blob/71bd2876e56a06e462e269b9d13616e07053ccad/core/relinker/CMakeLists.txt#L169), [L352](https://github.com/boykopovar/AnyPS5/blob/71bd2876e56a06e462e269b9d13616e07053ccad/core/relinker/CMakeLists.txt#L352) | | | | #6111 | `core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp` [L1674](https://github.com/boykopovar/AnyPS5/blob/71bd2876e56a06e462e269b9d13616e07053ccad/core/relinker/codegen/tests/Amd64OnlyConverterTests.cpp#L1674) | | | 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 | |---|---| | #3891 | `core/relinker/CMakeLists.txt` | | #4156 | `core/relinker/CMakeLists.txt` | | #4396 | `core/relinker/CMakeLists.txt` | | #4397 | `core/relinker/CMakeLists.txt` | | #4422 | `core/relinker/CMakeLists.txt` | | #4423 | `core/relinker/CMakeLists.txt` | | #4424 | `core/relinker/CMakeLists.txt` | | #4520 | `core/relinker/CMakeLists.txt` | | #4521 | `core/relinker/CMakeLists.txt` | | #4522 | `core/relinker/CMakeLists.txt` | | #4523 | `core/relinker/CMakeLists.txt` | | #4566 | `core/relinker/CMakeLists.txt` | | #4599 | `core/relinker/CMakeLists.txt` | | #4718 | `core/relinker/CMakeLists.txt` | | #4740 | `core/relinker/CMakeLists.txt` | | #4749 | `core/relinker/CMakeLists.txt` | | #4760 | `core/relinker/CMakeLists.txt` | | #4813 | `core/relinker/CMakeLists.txt` | | #5021 | `core/relinker/CMakeLists.txt` | | #6044 | `core/relinker/CMakeLists.txt` | | #6112 | `core/relinker/CMakeLists.txt` |
jotitadev commented 2026-10-14 14:50:14 +00:00 (Migrated from github.com)

Imported 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: vucomiss sees 309.0 == 309.0 as not equal. A jmp between 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:

failing cases
VCMPSS 24/64
VCMPSD 4/64 (predicates 13, 14, 16)
  • A case fails only when the compare result is false (zero mask). No case with an all-ones mask fails.
  • Predicates 0, 1 and 2 never fail, so the <= LE_OS exclusion in DecodeScalarCompare is correct.
  • Predicates 11 and 27 (FALSE_OQ, FALSE_OS) never fail either. Moving them out of line is harmless.

Out-of-line form

I wrote by hand the shape this PR emits: the compare in a stub followed by a jmp back, 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6171160733 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5683#issuecomment-6171160733). Originally posted by [jotitadev](https://github.com/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: `vucomiss` sees `309.0 == 309.0` as not equal. A `jmp` between 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: | | failing cases | |---|---| | `VCMPSS` | 24/64 | | `VCMPSD` | 4/64 (predicates 13, 14, 16) | - A case fails only when the compare result is false (zero mask). No case with an all-ones mask fails. - Predicates 0, 1 and 2 never fail, so the `<= LE_OS` exclusion in `DecodeScalarCompare` is correct. - Predicates 11 and 27 (FALSE_OQ, FALSE_OS) never fail either. Moving them out of line is harmless. ### Out-of-line form I wrote by hand the shape this PR emits: the compare in a stub followed by a `jmp` back, 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.
011010 commented 2026-10-14 18:31:36 +00:00 (Migrated from github.com)

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 ctest 149/149 with this change applied to current main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6175484368 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5683#issuecomment-6175484368). Originally posted by [011010](https://github.com/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 `ctest` 149/149 with this change applied to current `main`.
JoaoPauloCMarra commented 2026-10-15 10:05:32 +00:00 (Migrated from github.com)

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 build
ctest 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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6189552177 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5683#issuecomment-6189552177). Originally posted by [JoaoPauloCMarra](https://github.com/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 build ctest 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
adriadam10 commented 2026-10-15 11:58:26 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6191502276 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5683#issuecomment-6191502276). Originally posted by [adriadam10](https://github.com/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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!5683
No description provided.