relinker: --to-rosetta output can mis-execute a scalar compare followed by a 128-bit load (Rosetta 2 bug) #5642

Closed
opened 2026-10-14 02:37:24 +00:00 by RINNECODER · 3 comments
RINNECODER commented 2026-10-14 02:37:24 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by RINNECODER on 2026-10-14T02:37:24Z.


Found while porting to macOS: under Rosetta 2, --to-rosetta output can mis-execute a scalar compare.

What happens

The compare must be:

  • a CMPSS/VCMPSS (and some VCMPSD);
  • with a predicate other than EQ_OQ, LT_OS or LE_OS;
  • followed in the same translated block by a full 128-bit VEX load (vmovaps/vmovups/vmovdqa) into the compare's destination register.

A later scalar FP read of that register (vucomiss, vaddss) then sees the compare mask instead of the loaded value. clang emits this shape for fmaxf followed by a reload. In a title's layout code it made 309.0 == 309.0 compare as "below", so flex items never grew.

Minimal repro

An x86-64 binary run under Rosetta 2 on Apple silicon:

vmovss      a(%rip), %xmm2       # a = 309.0
vmovaps     %xmm2, %xmm0
vcmpunordss %xmm2, %xmm2, %xmm1
vmovaps     b(%rip), %xmm1       # b = {309.0, 0, 0, 0}, 16-byte aligned
vucomiss    %xmm0, %xmm1         # expect ZF=1 (equal); Rosetta gives CF=1, ZF=0
  • qemu-user (TCG): the same sequence is correct.
  • Under Rosetta: it is also correct when a jmp puts the compare in its own block.

Where

On main, core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp, _matchRosetta lowers only RDSEED, RDPID, CLWB and CPUID. Scalar compares reach Rosetta unchanged.

Suggested fix

For the Rosetta target, move each such compare out of line, unchanged, through the existing trampoline path (stub = the instruction + a jump back). The compare then runs in its own translated block, with exact semantics.

  • We tested every predicate of every form (legacy and VEX, ss and sd; register, memory and RIP-relative operands) under Rosetta. As written, 68 of 80 sequences lose the load. Split out of line, none do.
  • Splitting every predicate except EQ_OQ/LT_OS/LE_OS covers all failing forms we measured.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5840968343 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5642). Originally posted by [RINNECODER](https://github.com/RINNECODER) on 2026-10-14T02:37:24Z. --- Found while porting to macOS: under Rosetta 2, `--to-rosetta` output can mis-execute a scalar compare. ### What happens The compare must be: - a `CMPSS`/`VCMPSS` (and some `VCMPSD`); - with a predicate other than EQ_OQ, LT_OS or LE_OS; - followed in the same translated block by a full 128-bit VEX load (`vmovaps`/`vmovups`/`vmovdqa`) into the compare's destination register. A later scalar FP read of that register (`vucomiss`, `vaddss`) then sees the compare mask instead of the loaded value. clang emits this shape for `fmaxf` followed by a reload. In a title's layout code it made `309.0 == 309.0` compare as "below", so flex items never grew. ### Minimal repro An x86-64 binary run under Rosetta 2 on Apple silicon: ```asm vmovss a(%rip), %xmm2 # a = 309.0 vmovaps %xmm2, %xmm0 vcmpunordss %xmm2, %xmm2, %xmm1 vmovaps b(%rip), %xmm1 # b = {309.0, 0, 0, 0}, 16-byte aligned vucomiss %xmm0, %xmm1 # expect ZF=1 (equal); Rosetta gives CF=1, ZF=0 ``` - **qemu-user (TCG):** the same sequence is correct. - **Under Rosetta:** it is also correct when a `jmp` puts the compare in its own block. ### Where On `main`, `core/relinker/codegen/src/x86/Amd64OnlyInstructionMatcher.cpp`, `_matchRosetta` lowers only RDSEED, RDPID, CLWB and CPUID. Scalar compares reach Rosetta unchanged. ### Suggested fix For the Rosetta target, move each such compare out of line, unchanged, through the existing trampoline path (stub = the instruction + a jump back). The compare then runs in its own translated block, with exact semantics. - We tested every predicate of every form (legacy and VEX, `ss` and `sd`; register, memory and RIP-relative operands) under Rosetta. As written, 68 of 80 sequences lose the load. Split out of line, none do. - Splitting every predicate except EQ_OQ/LT_OS/LE_OS covers all failing forms we measured.
LeandroSSB commented 2026-10-14 03:15:41 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by LeandroSSB on 2026-10-14T03:15:41Z.


Taken: LeandroSSB/AnyPS5:fix/relinker-rosetta-scalar-compare (one commit, a76950dd5, on current main). Your approach applied: VEX scalar compares (VCMPSS/VCMPSD, VEX2/VEX3 map 0F) with predicate not in {EQ_OQ, LT_OS, LE_OS} are moved out-of-line through the existing trampoline path (stub = instruction + return jump, RIP-relative sources relocated), as Optional substitutions so unplaceable sites are kept. Predicates 0/1/2 and all packed/legacy/EVEX forms stay inline.

One scope note: legacy (non-VEX) CMPSS/CMPSD are not covered — X64InstructionDecoder has a pre-existing gap (two-byte 0F C2 missing from the ModRM+imm8 tables), so legacy compares never reach the matcher intact. The repro is VEX-only; I recommend a separate issue for the decoder gap if it matters.

Validated on Linux (GCC 13.3, relinker-only build, BUILD_TESTING=ON): 156/156 tests pass, including the new Rosetta out-of-line test cases; tools/check_conventions.py --base origin/main passes. Not opened as a PR because of the intake pause; a maintainer is welcome to carry the commit.

AI-assisted (Claude Code); I ran the validation above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6160073156 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5642#issuecomment-6160073156). Originally posted by [LeandroSSB](https://github.com/LeandroSSB) on 2026-10-14T03:15:41Z. --- Taken: [`LeandroSSB/AnyPS5:fix/relinker-rosetta-scalar-compare`](https://github.com/LeandroSSB/AnyPS5/tree/fix/relinker-rosetta-scalar-compare) (one commit, `a76950dd5`, on current `main`). Your approach applied: VEX scalar compares (VCMPSS/VCMPSD, VEX2/VEX3 map 0F) with predicate not in {EQ_OQ, LT_OS, LE_OS} are moved out-of-line through the existing trampoline path (stub = instruction + return jump, RIP-relative sources relocated), as `Optional` substitutions so unplaceable sites are kept. Predicates 0/1/2 and all packed/legacy/EVEX forms stay inline. One scope note: legacy (non-VEX) CMPSS/CMPSD are not covered — `X64InstructionDecoder` has a pre-existing gap (two-byte `0F C2` missing from the ModRM+imm8 tables), so legacy compares never reach the matcher intact. The repro is VEX-only; I recommend a separate issue for the decoder gap if it matters. **Validated** on Linux (GCC 13.3, relinker-only build, `BUILD_TESTING=ON`): **156/156 tests pass**, including the new Rosetta out-of-line test cases; `tools/check_conventions.py --base origin/main` passes. Not opened as a PR because of the intake pause; a maintainer is welcome to carry the commit. AI-assisted (Claude Code); I ran the validation above on my machine.
MarcZX10 commented 2026-10-14 06:22:12 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T06:22:12Z.


Opened #5683 with Leandro Soares' tested fix, preserving his commit authorship. The Linux relinker-only build succeeded; amd64_only_converter passes. Full CTest is 157/158: the only failure is the unrelated progress_history Python 3.11 tarfile.extractall(filter=...) compatibility error. Conventions pass. I could not run macOS Rosetta locally, so the Rosetta reproduction remains supported by the reporter's tested matrix.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162268021 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5642#issuecomment-6162268021). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-14T06:22:12Z. --- Opened #5683 with Leandro Soares' tested fix, preserving his commit authorship. The Linux relinker-only build succeeded; `amd64_only_converter` passes. Full CTest is 157/158: the only failure is the unrelated `progress_history` Python 3.11 `tarfile.extractall(filter=...)` compatibility error. Conventions pass. I could not run macOS Rosetta locally, so the Rosetta reproduction remains supported by the reporter's tested matrix.
MarcZX10 commented 2026-10-14 18:16:02 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T18:16:02Z.


Independent Apple Silicon validation has now been added to PR #5683: on an M2 Max under Rosetta 2, the reported compare/load sequence fails as described for 24/64 VCMPSS cases and 4/64 VCMPSD cases; only false-result masks fail, and predicates 0, 1 and 2 did not fail. The hand-written out-of-line form passed the predicate checks. The reviewer did not execute the relinker's converted output on macOS, so this is confirmation of the Rosetta behavior and trampoline shape, not an end-to-end converted-title run.

The PR's latest hosted conventions, eligibility, intake, and Ubuntu/Windows/macOS relinker checks pass. Its overlap with #5784 is only in relinker test registration; the two PRs cover independent instructions and no functional dependency is declared.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6175192756 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5642#issuecomment-6175192756). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-14T18:16:02Z. --- Independent Apple Silicon validation has now been added to [PR #5683](https://github.com/boykopovar/AnyPS5/pull/5683): on an M2 Max under Rosetta 2, the reported compare/load sequence fails as described for 24/64 VCMPSS cases and 4/64 VCMPSD cases; only false-result masks fail, and predicates 0, 1 and 2 did not fail. The hand-written out-of-line form passed the predicate checks. The reviewer did not execute the relinker's converted output on macOS, so this is confirmation of the Rosetta behavior and trampoline shape, not an end-to-end converted-title run. The PR's latest hosted conventions, eligibility, intake, and Ubuntu/Windows/macOS relinker checks pass. Its overlap with #5784 is only in relinker test registration; the two PRs cover independent instructions and no functional dependency is declared.
Sign in to join this conversation.
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#5642
No description provided.