feat(relinker): lower SHA-1 instructions for --to-intel #430

Merged
adriadam10 merged 1 commit from upstream-pr/430 into main 2026-10-04 01:14:09 +00:00
adriadam10 commented 2026-10-03 05:40:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-03T05:40:53Z.


What

Addresses the SHA-1 part of the --to-intel item in docs/dev/TechnicalDebt.md. SHA1RNDS4, SHA1NEXTE, SHA1MSG1 and SHA1MSG2 were passed through unchanged, so a relinked title that executes them dies with an illegal instruction on Intel CPUs without SHA-NI (Haswell to Comet Lake). This is the follow-up announced in #200. No title is currently known to use them.

  • Lowering: the register forms (any REX, any aliasing of destination and source, every SHA1RNDS4 function imm8[1:0]) are lowered out of line in Sha1Lowering, next to Sha256Lowering, through the shared StubBodyBuilder. Only SSE2 is used (pshufd, shufpd, dword/byte shifts, paddd with a RIP-relative round constant), so upper YMM halves, RFLAGS and the general-purpose registers are untouched.
  • SHA1RNDS4 runs the four rounds with each lane-0 scalar of A..E in its own scratch register (7 spilled registers, 0x3F0 bytes below rsp); E starts at zero as in the SDM. SHA1MSG2 computes W16..W18 lane-wise and folds rol(W16, 1) into lane 0 for W19.
  • Short sites: SHA1NEXTE/MSG1/MSG2 are 4 bytes, so MatchSequence now accepts SHA-1 next to SSE4a, SHA-256 and CLZERO; other following instructions are copied after the body as before.
  • Detection: new DecodedInstruction::IsSha1 (0F 38 C8/C9/CA, 0F 3A CC, no 66/F2/F3 prefix) and DecodeSha1.
  • Behaviour change: as for SHA-256 in #200, SHA-1 instructions with a memory operand, and short SHA-1 sites followed by an instruction that cannot move, now fail the relink with the file offset instead of passing through.
  • Cost: stubs are 640-704 bytes (SHA1RNDS4, depending on the function), 97 (SHA1NEXTE), 44 (SHA1MSG1) and 119 (SHA1MSG2). The lowering also runs on hosts that have SHA-NI, where it is slower than the native instruction.
  • TechnicalDebt.md: the item now names only the SHA-1/SHA-256 memory forms and RDPRU/MCOMMIT.

Tested

  • Linux (CachyOS, GCC 16, Release, same flags as CI): full build, libs, ctest 125/125.
  • amd64_only_converter:
    • Operand decoding: REX on either side, the immediate's low two bits, memory forms rejected (also for SHA1RNDS4 before its immediate is read), missing immediate, 66-prefixed forms and SHA-256 opcodes not taken as SHA-1. Decoder lengths for register and memory forms.
    • Matcher names for the four instructions.
    • Converter: SHA1RNDS4 as a 5-byte site, SHA1NEXTE absorbing the following SHA1MSG1 into one stub, SHA1MSG2 absorbing a nop, failure before a ret, and the file offset of the memory-form failure.
    • Execution (x86-64 Linux): 16 sites (all four SHA1RNDS4 functions, equal operands, xmm8-xmm15) on 64 random register images each, checked against a reference transcribed from the SDM; every other xmm register, rax, rcx and RFLAGS must be unchanged.
  • Mutation check: each of these makes the test fail: rol 5→4, rol 30→31, a wrong K2, AND→OR in Ch, Maj changed, word order reversed, E not zeroed, SHA1NEXTE lane shift, SHA1MSG1 shufpd immediate, SHA1MSG2 without the W19 fold, SHA1MSG2 shift, and a missing restore (crash).
  • Hardware (Ryzen 5 7535HS, Zen 3+, which has SHA-NI; harness not included in this PR):
    • The reference model matches _mm_sha1rnds4_epu32 (all four functions), _mm_sha1nexte_epu32, _mm_sha1msg1_epu32 and _mm_sha1msg2_epu32 on 100,000 random inputs each (700,000 results).
    • Stubs against the native instructions on all 128 bits of all 16 XMM registers, rax, rcx, the status flags and rsp: every destination/source pair of the seven forms × 100 random images, plus 2,000 random sequences of 2-4 instructions through MatchSequence × 100 images: 379,200 runs, 0 mismatches. A wrong K2 gives 3,050 mismatches in the same harness.
    • Nothing was run on Intel hardware.
  • Windows: not run locally; amd64_only_windows is not extended here.
  • No title tested.

Overlap: no open PR touches SHA-1. The TechnicalDebt.md line sits next to the executable-icon line that #350 removes, so whichever lands second needs a trivial rebase.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR (memory forms)
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt (memory forms throw)
  • 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:4724395793 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/430). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-03T05:40:53Z. --- ### What Addresses the SHA-1 part of the `--to-intel` item in `docs/dev/TechnicalDebt.md`. SHA1RNDS4, SHA1NEXTE, SHA1MSG1 and SHA1MSG2 were passed through unchanged, so a relinked title that executes them dies with an illegal instruction on Intel CPUs without SHA-NI (Haswell to Comet Lake). This is the follow-up announced in #200. No title is currently known to use them. - **Lowering:** the register forms (any REX, any aliasing of destination and source, every SHA1RNDS4 function `imm8[1:0]`) are lowered out of line in `Sha1Lowering`, next to `Sha256Lowering`, through the shared `StubBodyBuilder`. Only SSE2 is used (`pshufd`, `shufpd`, dword/byte shifts, `paddd` with a RIP-relative round constant), so upper YMM halves, RFLAGS and the general-purpose registers are untouched. - **SHA1RNDS4** runs the four rounds with each lane-0 scalar of A..E in its own scratch register (7 spilled registers, 0x3F0 bytes below rsp); E starts at zero as in the SDM. **SHA1MSG2** computes W16..W18 lane-wise and folds `rol(W16, 1)` into lane 0 for W19. - **Short sites:** SHA1NEXTE/MSG1/MSG2 are 4 bytes, so `MatchSequence` now accepts SHA-1 next to SSE4a, SHA-256 and CLZERO; other following instructions are copied after the body as before. - **Detection:** new `DecodedInstruction::IsSha1` (`0F 38 C8/C9/CA`, `0F 3A CC`, no 66/F2/F3 prefix) and `DecodeSha1`. - **Behaviour change:** as for SHA-256 in #200, SHA-1 instructions with a memory operand, and short SHA-1 sites followed by an instruction that cannot move, now fail the relink with the file offset instead of passing through. - **Cost:** stubs are 640-704 bytes (SHA1RNDS4, depending on the function), 97 (SHA1NEXTE), 44 (SHA1MSG1) and 119 (SHA1MSG2). The lowering also runs on hosts that have SHA-NI, where it is slower than the native instruction. - `TechnicalDebt.md`: the item now names only the SHA-1/SHA-256 memory forms and RDPRU/MCOMMIT. ### Tested - Linux (CachyOS, GCC 16, Release, same flags as CI): full build, `libs`, `ctest` 125/125. - `amd64_only_converter`: - Operand decoding: REX on either side, the immediate's low two bits, memory forms rejected (also for SHA1RNDS4 before its immediate is read), missing immediate, 66-prefixed forms and SHA-256 opcodes not taken as SHA-1. Decoder lengths for register and memory forms. - Matcher names for the four instructions. - Converter: SHA1RNDS4 as a 5-byte site, SHA1NEXTE absorbing the following SHA1MSG1 into one stub, SHA1MSG2 absorbing a `nop`, failure before a `ret`, and the file offset of the memory-form failure. - Execution (x86-64 Linux): 16 sites (all four SHA1RNDS4 functions, equal operands, xmm8-xmm15) on 64 random register images each, checked against a reference transcribed from the SDM; every other xmm register, rax, rcx and RFLAGS must be unchanged. - Mutation check: each of these makes the test fail: rol 5→4, rol 30→31, a wrong K2, AND→OR in Ch, Maj changed, word order reversed, E not zeroed, SHA1NEXTE lane shift, SHA1MSG1 `shufpd` immediate, SHA1MSG2 without the W19 fold, SHA1MSG2 shift, and a missing restore (crash). - Hardware (Ryzen 5 7535HS, Zen 3+, which has SHA-NI; harness not included in this PR): - The reference model matches `_mm_sha1rnds4_epu32` (all four functions), `_mm_sha1nexte_epu32`, `_mm_sha1msg1_epu32` and `_mm_sha1msg2_epu32` on 100,000 random inputs each (700,000 results). - Stubs against the native instructions on all 128 bits of all 16 XMM registers, rax, rcx, the status flags and rsp: every destination/source pair of the seven forms × 100 random images, plus 2,000 random sequences of 2-4 instructions through `MatchSequence` × 100 images: 379,200 runs, 0 mismatches. A wrong K2 gives 3,050 mismatches in the same harness. - Nothing was run on Intel hardware. - Windows: not run locally; `amd64_only_windows` is not extended here. - No title tested. Overlap: no open PR touches SHA-1. The `TechnicalDebt.md` line sits next to the executable-icon line that #350 removes, so whichever lands second needs a trivial rebase. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 (memory forms) - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) (memory forms throw) - [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
github-actions[bot] commented 2026-10-03 06:35:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-03T06:35:23Z.


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
#350 docs/dev/TechnicalDebt.md L57-58
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5966407679 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/430#issuecomment-5966407679). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-03T06:35:23Z. --- <!-- 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 | |---|---|---|---| | #350 | `docs/dev/TechnicalDebt.md` [L57-58](https://github.com/boykopovar/AnyPS5/blob/e6505d8a932213e7069e38cdbde9f832e33eed18/docs/dev/TechnicalDebt.md#L57-L58) | | |
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!430
No description provided.