feat(relinker): lower SHA-256 instructions for --to-intel #200

Merged
ljkx merged 1 commit from upstream-pr/200 into main 2026-10-01 08:31:02 +00:00
ljkx commented 2026-09-30 22:25:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ljkx on 2026-09-30T22:25:29Z.


What

Addresses part of the SHA-NI item in docs/dev/TechnicalDebt.md. --to-intel passed SHA-NI instructions through unchanged, so a relinked title that executes them fails with an illegal instruction on Intel CPUs without SHA-NI (Haswell to Comet Lake, e.g. the i5-7500 and i5-7200 from the compatibility table). DecodedInstruction::IsShaNi existed but was not used, and its opcode list did not include SHA256RNDS2 (0F 38 CB).

No title is currently known to use SHA-NI; this removes the silent pass-through for SHA-256. CPUID is not intercepted, so code that checks for SHA-NI already takes its own path on the host; this matters for code that uses it unconditionally on the fixed PS5 CPU, as Demon's Souls does with INSERTQ. SHA-1 is left for a follow-up PR.

  • Lowering: SHA256RNDS2, SHA256MSG1 and SHA256MSG2 (register forms, including REX and any aliasing of destination, source and the implicit XMM0) are lowered out of line in Sha256Lowering. Only legacy SSE2 instructions are used, so the upper YMM halves, RFLAGS and the general-purpose registers are untouched. Rotations are shift pairs; each lane-0 scalar of the two rounds of SHA256RNDS2 lives in its own scratch register (8 spilled scratch registers), and SHA256MSG2 computes W16/W17 before W18/W19.
  • Short sites: these instructions are 4 or 5 bytes. MatchSequence now accepts SHA-256 next to EXTRQ/INSERTQ, so consecutive short SSE4a and SHA-256 sites share one stub; other following instructions are copied after the body as before.
  • Refactoring: the stub builder moves unchanged from Sse4aLowering.cpp to StubBodyBuilder so both lowerings use it. It gains SseImm (PSHUFD) and ShiftDwordImm (PSRLD/PSLLD). The SSE4a golden bodies are byte-identical.
  • Detection: IsShaNi now includes 0F 38 CB (it is still unused); the new IsSha256 selects the three instructions.
  • Behaviour change: SHA-256 instructions with a memory operand, and short SHA-256 sites followed by an instruction that cannot move (return, branch, RIP-relative operand), now fail the relink with the file offset instead of passing through. Before, such a relink succeeded, and the code worked on hosts with SHA-NI (Intel Ice Lake / 11th gen and newer) or when it checked CPUID and never ran on the others. Such sites now fail the relink even in code that checks CPUID.
  • Cost: stubs are 785 bytes (SHA256RNDS2), 170 (SHA256MSG1) and 231 (SHA256MSG2). SHA256RNDS2 spills 8 registers, so the stub uses 0x480 bytes below rsp (INSERTQ register form: 0x1B0). The lowering also runs on hosts that have SHA-NI, where it is slower than the native instruction.
  • TechnicalDebt.md: the item now names the remaining SHA-1 instructions and the SHA-256 memory forms.

Tested

  • Relinker test suite passes on Windows 11 (Clang 23, and MinGW-w64 GCC 16.2 with static linking) and on Ubuntu 24.04 under WSL (GCC 13.3).
  • amd64_only_windows: every destination/source pair (all 256) for each of the three instructions, and 256 random sequences of 2–3 instructions through MatchSequence, on random register images. Results are checked against a reference transcribed from the SDM, plus the existing clobber / RFLAGS / rsp / red-zone checks.
  • amd64_only_converter: operand decoding (REX, memory form rejected, prefixed 0F 38 CB and SHA-1 not taken as SHA-256), matcher names, SHA-1 still unmatched, a short SHA256RNDS2 absorbing the following SHA256MSG1, a short SHA256MSG2 absorbing a nop, the failure before a ret, and the file offset of the memory-form failure. On x86-64 Linux the stubs are executed with distinct and equal operands.
  • Mutation check: six single-token changes to the lowering (a rotation count, the second round key lane, a MSG1/MSG2 shift, AND→OR in Ch, the result lane order) each make the Windows test fail; one also checked on Linux through ctest.
  • Hardware (Ryzen 7 7800X3D, Zen 4, which has SHA-NI; harness not included in this PR): the reference model matches _mm_sha256rnds2/msg1/msg2_epu32 on 1,000,000 random inputs each. The lowered stubs match the native instructions on all 128 bits of all 16 XMM registers, the status flags and rsp: 307,200 single-instruction runs (3 instructions × 256 register pairs × 400) and 20,000 random 2–4 instruction sequences. The same six mutations each produce mismatches there.
  • End-to-end: the test_guest_intel_trampolines.py fixture with a SHA256RNDS2 + nop site, run through relinker --to-intel (Linux ELF and Windows PE output, main executable and guest module): the site jumps to a stub without the original instruction, and the stub returns after the absorbed nop.
  • No title tested.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR (SHA-1 next)
  • No comments in code except technical debt
  • Unimplemented paths throw; silent stubs are listed in TechnicalDebt (memory forms throw)
  • No notes, investigation .md files or images added to the repository
  • 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, assisted with CC.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4696696172 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/200). Originally posted by [ljkx](https://github.com/ljkx) on 2026-09-30T22:25:29Z. --- ### What Addresses part of the SHA-NI item in `docs/dev/TechnicalDebt.md`. `--to-intel` passed SHA-NI instructions through unchanged, so a relinked title that executes them fails with an illegal instruction on Intel CPUs without SHA-NI (Haswell to Comet Lake, e.g. the i5-7500 and i5-7200 from the compatibility table). `DecodedInstruction::IsShaNi` existed but was not used, and its opcode list did not include SHA256RNDS2 (`0F 38 CB`). No title is currently known to use SHA-NI; this removes the silent pass-through for SHA-256. CPUID is not intercepted, so code that checks for SHA-NI already takes its own path on the host; this matters for code that uses it unconditionally on the fixed PS5 CPU, as Demon's Souls does with INSERTQ. SHA-1 is left for a follow-up PR. - **Lowering:** SHA256RNDS2, SHA256MSG1 and SHA256MSG2 (register forms, including REX and any aliasing of destination, source and the implicit XMM0) are lowered out of line in `Sha256Lowering`. Only legacy SSE2 instructions are used, so the upper YMM halves, RFLAGS and the general-purpose registers are untouched. Rotations are shift pairs; each lane-0 scalar of the two rounds of SHA256RNDS2 lives in its own scratch register (8 spilled scratch registers), and SHA256MSG2 computes W16/W17 before W18/W19. - **Short sites:** these instructions are 4 or 5 bytes. `MatchSequence` now accepts SHA-256 next to EXTRQ/INSERTQ, so consecutive short SSE4a and SHA-256 sites share one stub; other following instructions are copied after the body as before. - **Refactoring:** the stub builder moves unchanged from `Sse4aLowering.cpp` to `StubBodyBuilder` so both lowerings use it. It gains `SseImm` (PSHUFD) and `ShiftDwordImm` (PSRLD/PSLLD). The SSE4a golden bodies are byte-identical. - **Detection:** `IsShaNi` now includes `0F 38 CB` (it is still unused); the new `IsSha256` selects the three instructions. - **Behaviour change:** SHA-256 instructions with a memory operand, and short SHA-256 sites followed by an instruction that cannot move (return, branch, RIP-relative operand), now fail the relink with the file offset instead of passing through. Before, such a relink succeeded, and the code worked on hosts with SHA-NI (Intel Ice Lake / 11th gen and newer) or when it checked CPUID and never ran on the others. Such sites now fail the relink even in code that checks CPUID. - **Cost:** stubs are 785 bytes (SHA256RNDS2), 170 (SHA256MSG1) and 231 (SHA256MSG2). SHA256RNDS2 spills 8 registers, so the stub uses 0x480 bytes below rsp (INSERTQ register form: 0x1B0). The lowering also runs on hosts that have SHA-NI, where it is slower than the native instruction. - `TechnicalDebt.md`: the item now names the remaining SHA-1 instructions and the SHA-256 memory forms. ### Tested - Relinker test suite passes on Windows 11 (Clang 23, and MinGW-w64 GCC 16.2 with static linking) and on Ubuntu 24.04 under WSL (GCC 13.3). - `amd64_only_windows`: every destination/source pair (all 256) for each of the three instructions, and 256 random sequences of 2–3 instructions through `MatchSequence`, on random register images. Results are checked against a reference transcribed from the SDM, plus the existing clobber / RFLAGS / rsp / red-zone checks. - `amd64_only_converter`: operand decoding (REX, memory form rejected, prefixed `0F 38 CB` and SHA-1 not taken as SHA-256), matcher names, SHA-1 still unmatched, a short SHA256RNDS2 absorbing the following SHA256MSG1, a short SHA256MSG2 absorbing a `nop`, the failure before a `ret`, and the file offset of the memory-form failure. On x86-64 Linux the stubs are executed with distinct and equal operands. - Mutation check: six single-token changes to the lowering (a rotation count, the second round key lane, a MSG1/MSG2 shift, AND→OR in Ch, the result lane order) each make the Windows test fail; one also checked on Linux through `ctest`. - Hardware (Ryzen 7 7800X3D, Zen 4, which has SHA-NI; harness not included in this PR): the reference model matches `_mm_sha256rnds2/msg1/msg2_epu32` on 1,000,000 random inputs each. The lowered stubs match the native instructions on all 128 bits of all 16 XMM registers, the status flags and rsp: 307,200 single-instruction runs (3 instructions × 256 register pairs × 400) and 20,000 random 2–4 instruction sequences. The same six mutations each produce mismatches there. - End-to-end: the `test_guest_intel_trampolines.py` fixture with a SHA256RNDS2 + `nop` site, run through `relinker --to-intel` (Linux ELF and Windows PE output, main executable and guest module): the site jumps to a stub without the original instruction, and the stub returns after the absorbed `nop`. - No title tested. ### 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 (SHA-1 next) - [x] No comments in code except technical debt - [x] Unimplemented paths throw; silent stubs are listed in TechnicalDebt (memory forms throw) - [x] No notes, investigation `.md` files or images added to the repository - [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, assisted with CC.
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!200
No description provided.