feat(relinker): lower SHA-256 instructions with a memory operand #434

Merged
adriadam10 merged 2 commits from upstream-pr/434 into main 2026-10-04 06:42:11 +00:00
adriadam10 commented 2026-10-03 06:32:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-03T06:32:44Z.


What

--to-intel threw on SHA256RNDS2/MSG1/MSG2 with a memory source ("SHA-256 instruction with a memory operand"), so one such instruction failed the whole relink. Compilers fold loads into these instructions (sha256msg1 xmm1, [rsp+0x20]), so real code can contain them.

Fix:

  • DecodeSha256 now decodes the memory operand (DecodeMemoryOperand in StubBodyBuilder: segment/67h prefixes, REX.X/B, ModRM, SIB, displacement) instead of throwing.
  • The stub spills a scratch xmm, loads the operand into it with movdqu, runs the existing register-form lowering with that register as the source, then restores it.
  • StubBodyBuilder keeps track of how far its spills have moved rsp. An RSP-based operand gets that depth added to its displacement, and is always re-encoded with a disp32. R12 (base 100 with REX.B) is not treated as RSP.
  • movdqu, not movdqa: on this AMD host a misaligned sha256msg1 xmm1, [mem] does not fault, so the aligned load would add a fault the original didn't have.
  • RIP-relative operands still throw: the stub runs at a different address and nothing relocates the displacement yet.

docs/dev/TechnicalDebt.md line 55 still says SHA-256 memory forms fail the relink. #430 rewrites that same line. I left it alone so the two PRs don't conflict. Once both are merged, it should say that only RIP-relative SHA-1/SHA-256 operands fail. I can do that in a follow-up, or you can fold it into #430.

Tested

Linux x86-64 (Ryzen 5 7535HS, Zen 3+, has SHA-NI), no title.

  • Amd64OnlyConverterTests.cpp:
    • sha256Operands: decodes a GS + 67h + REX.R + SIB + disp8 form, R12 base vs RSP base, SIB without a base (disp32), and rejects RIP-relative and truncated operands.
    • sha256Execution: runs each op through the stub with [rcx], [rsp+0x18], [rsp+rax*1+0x08] (rax = 16), cs: [r12] and [rcx+disp32], compared against the reference. The harness now also checks that xmm1 survives (it is the scratch picked for these forms).
    • converterSha256: the memory form now becomes a trampoline; the RIP-relative form still fails at its file offset.
    • On main the new execution and converter checks fail with "SHA-256 instruction with a memory operand" (the decode checks don't compile there because Memory doesn't exist yet).
    • Negative checks, each one makes the suite fail: no spill-depth adjustment, R12 treated as RSP, no disp8 to disp32 widening, no spill of the scratch, SIB index dropped.
  • Differential against the native instruction (local harness, not committed): all 128 bits of the 16 XMM registers, flags, rax, rcx and rsp. Five addressing modes ([rsp+d8], [rsp+d32], [r12+d8], [r8+d8], [rsp+r9*2+d8]) × 16 destinations × 17 displacements per op, plus 3000 random 2-4 instruction sequences through MatchSequence that mix them with the SSE4a and SHA-256 register forms. 0 mismatches for the memory forms (37 200 runs per op). On the full set, the only mismatches are the sequences where the #433 bug affects SSE4a. With #433 applied too: 432 720 runs, 0 mismatches. The harness catches a missing spill-depth adjustment (22 140 mismatches) and R12 treated as RSP (7 560).
  • Full Release build + ctest: 125/125.
  • git merge-tree against every open relinker PR (#298 #299 #340 #350 #353 #356 #418 #430 #431 #432 #433): clean.

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 in TechnicalDebt (n/a)
  • 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 (n/a)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4724637549 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/434). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-03T06:32:44Z. --- ### What `--to-intel` threw on SHA256RNDS2/MSG1/MSG2 with a memory source ("SHA-256 instruction with a memory operand"), so one such instruction failed the whole relink. Compilers fold loads into these instructions (`sha256msg1 xmm1, [rsp+0x20]`), so real code can contain them. Fix: - `DecodeSha256` now decodes the memory operand (`DecodeMemoryOperand` in `StubBodyBuilder`: segment/67h prefixes, REX.X/B, ModRM, SIB, displacement) instead of throwing. - The stub spills a scratch xmm, loads the operand into it with `movdqu`, runs the existing register-form lowering with that register as the source, then restores it. - `StubBodyBuilder` keeps track of how far its spills have moved `rsp`. An RSP-based operand gets that depth added to its displacement, and is always re-encoded with a disp32. R12 (base 100 with REX.B) is not treated as RSP. - `movdqu`, not `movdqa`: on this AMD host a misaligned `sha256msg1 xmm1, [mem]` does not fault, so the aligned load would add a fault the original didn't have. - RIP-relative operands still throw: the stub runs at a different address and nothing relocates the displacement yet. `docs/dev/TechnicalDebt.md` line 55 still says SHA-256 memory forms fail the relink. #430 rewrites that same line. I left it alone so the two PRs don't conflict. Once both are merged, it should say that only RIP-relative SHA-1/SHA-256 operands fail. I can do that in a follow-up, or you can fold it into #430. ### Tested Linux x86-64 (Ryzen 5 7535HS, Zen 3+, has SHA-NI), no title. - `Amd64OnlyConverterTests.cpp`: - `sha256Operands`: decodes a GS + 67h + REX.R + SIB + disp8 form, R12 base vs RSP base, SIB without a base (disp32), and rejects RIP-relative and truncated operands. - `sha256Execution`: runs each op through the stub with `[rcx]`, `[rsp+0x18]`, `[rsp+rax*1+0x08]` (rax = 16), `cs: [r12]` and `[rcx+disp32]`, compared against the reference. The harness now also checks that xmm1 survives (it is the scratch picked for these forms). - `converterSha256`: the memory form now becomes a trampoline; the RIP-relative form still fails at its file offset. - On `main` the new execution and converter checks fail with "SHA-256 instruction with a memory operand" (the decode checks don't compile there because `Memory` doesn't exist yet). - Negative checks, each one makes the suite fail: no spill-depth adjustment, R12 treated as RSP, no disp8 to disp32 widening, no spill of the scratch, SIB index dropped. - Differential against the native instruction (local harness, not committed): all 128 bits of the 16 XMM registers, flags, rax, rcx and rsp. Five addressing modes ([rsp+d8], [rsp+d32], [r12+d8], [r8+d8], [rsp+r9*2+d8]) × 16 destinations × 17 displacements per op, plus 3000 random 2-4 instruction sequences through `MatchSequence` that mix them with the SSE4a and SHA-256 register forms. 0 mismatches for the memory forms (37 200 runs per op). On the full set, the only mismatches are the sequences where the #433 bug affects SSE4a. With #433 applied too: 432 720 runs, 0 mismatches. The harness catches a missing spill-depth adjustment (22 140 mismatches) and R12 treated as RSP (7 560). - Full Release build + `ctest`: 125/125. - `git merge-tree` against every open relinker PR (#298 #299 #340 #350 #353 #356 #418 #430 #431 #432 #433): clean. ### 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 paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt (n/a) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source (n/a) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-04 05:44:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T05:44:38Z.


The memory operand handling looks right to me (the spill depth for RSP bases, R12 not treated as RSP, xmm0 kept out of the scratch). The only thing left is the TechnicalDebt line: now that #430 has landed, main's docs/dev/TechnicalDebt.md says "SHA-1 and SHA-256 instructions with a memory operand fail the relink", which is no longer true for SHA-256 after this PR. Could you change it here to say that SHA-1 instructions with a memory operand and RIP-relative SHA-256 operands fail the relink? Then it's ready from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977032798 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/434#issuecomment-5977032798). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T05:44:38Z. --- The memory operand handling looks right to me (the spill depth for RSP bases, R12 not treated as RSP, xmm0 kept out of the scratch). The only thing left is the TechnicalDebt line: now that #430 has landed, main's `docs/dev/TechnicalDebt.md` says "SHA-1 and SHA-256 instructions with a memory operand fail the relink", which is no longer true for SHA-256 after this PR. Could you change it here to say that SHA-1 instructions with a memory operand and RIP-relative SHA-256 operands fail the relink? Then it's ready from my side.
adriadam10 commented 2026-10-04 06:22:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T06:22:06Z.


Thanks! Rebased onto main (with #430) and updated the TechnicalDebt line: it now says SHA-1 instructions with a memory operand and SHA-256 instructions with a RIP-relative operand fail the relink. The latter still throw in DecodeMemoryOperand ("RIP-relative memory operand cannot move into a stub").

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977273033 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/434#issuecomment-5977273033). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T06:22:06Z. --- Thanks! Rebased onto main (with #430) and updated the TechnicalDebt line: it now says SHA-1 instructions with a memory operand and SHA-256 instructions with a RIP-relative operand fail the relink. The latter still throw in `DecodeMemoryOperand` ("RIP-relative memory operand cannot move into a stub").
oneandonlydean commented 2026-10-04 06:29:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T06:29:23Z.


Thanks for the quick update, that covers it. I'll test it here and merge once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5977319866 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/434#issuecomment-5977319866). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T06:29:23Z. --- Thanks for the quick update, that covers it. I'll test it here and merge once CI is green.
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!434
No description provided.