feat(relinker): lower SHA-1 instructions with a memory operand #588

Merged
DotDebian merged 1 commit from upstream-pr/588 into main 2026-10-05 01:24:22 +00:00
DotDebian commented 2026-10-05 00:04:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DotDebian on 2026-10-05T00:04:35Z.


What

--to-intel failed the whole relink on SHA1RNDS4, SHA1NEXTE, SHA1MSG1 and SHA1MSG2 with a memory source ("SHA-1 instruction with a memory operand"). This lowers them the way #434 did for SHA-256.

  • DecodeSha1 decodes the ModRM/SIB/displacement into the shared MemoryOperand. The SHA1RNDS4 immediate is read after the operand, not at a fixed offset.
  • MemoryOperand gains EncodedSize (ModRM through displacement), set by DecodeMemoryOperand, so a decoder can find what follows the operand.
  • Sha1Lowering spills a scratch xmm, loads the operand with StubBodyBuilder::Load and runs the existing register-form lowering. RSP-based operands get the spill depth through Load, as for SHA-256.
  • RIP-relative operands still fail the relink, with the file offset of the site.
  • TechnicalDebt entry updated.

The load is the unaligned one #434 introduced; this PR does not change that choice.

Tested

Windows 11, MinGW-w64 GCC 15.2.0, ctest: 221/221 passed.

  • amd64_only_windows (extended): executes the lowered stubs on the host CPU and compares with a reference transcribed from the Intel SDM pseudo-code. All four instructions, every destination register, register sources and memory sources [rbx], [rbx+disp8], [rbx+disp32] and [rsp-0x10] (inside the red zone, so it only reads the right bytes if the spill depth is applied), all four SHA1RNDS4 functions, and 256 random sequences mixing register and memory forms. It also checks that no other xmm register, RFLAGS, rsp or the red zone changes. SHA-1 stubs were not executed on Windows before. Without the change the test fails with "SHA-1 instruction with a memory operand".
  • amd64_only_converter (extended): decoding of memory forms (prefixes, SIB, disp8/disp32, immediate position, truncation, RIP-relative), the stack-relative load bytes in the stub, the converter accepting a memory form and reporting the offset of a RIP-relative one.
  • The Linux-only execution block of amd64_only_converter got memory-form sites too. It is not compiled on Windows; I built and ran it locally only through a throwaway mmap shim over VirtualAlloc (not part of this PR), where it passes. It has not run on Linux.

No title was run.

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
  • 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
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4738609299 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/588). Originally posted by [DotDebian](https://github.com/DotDebian) on 2026-10-05T00:04:35Z. --- ### What `--to-intel` failed the whole relink on SHA1RNDS4, SHA1NEXTE, SHA1MSG1 and SHA1MSG2 with a memory source ("SHA-1 instruction with a memory operand"). This lowers them the way #434 did for SHA-256. - `DecodeSha1` decodes the ModRM/SIB/displacement into the shared `MemoryOperand`. The SHA1RNDS4 immediate is read after the operand, not at a fixed offset. - `MemoryOperand` gains `EncodedSize` (ModRM through displacement), set by `DecodeMemoryOperand`, so a decoder can find what follows the operand. - `Sha1Lowering` spills a scratch xmm, loads the operand with `StubBodyBuilder::Load` and runs the existing register-form lowering. RSP-based operands get the spill depth through `Load`, as for SHA-256. - RIP-relative operands still fail the relink, with the file offset of the site. - TechnicalDebt entry updated. The load is the unaligned one #434 introduced; this PR does not change that choice. ### Tested Windows 11, MinGW-w64 GCC 15.2.0, `ctest`: 221/221 passed. - `amd64_only_windows` (extended): executes the lowered stubs on the host CPU and compares with a reference transcribed from the Intel SDM pseudo-code. All four instructions, every destination register, register sources and memory sources `[rbx]`, `[rbx+disp8]`, `[rbx+disp32]` and `[rsp-0x10]` (inside the red zone, so it only reads the right bytes if the spill depth is applied), all four SHA1RNDS4 functions, and 256 random sequences mixing register and memory forms. It also checks that no other xmm register, RFLAGS, rsp or the red zone changes. SHA-1 stubs were not executed on Windows before. Without the change the test fails with "SHA-1 instruction with a memory operand". - `amd64_only_converter` (extended): decoding of memory forms (prefixes, SIB, disp8/disp32, immediate position, truncation, RIP-relative), the stack-relative load bytes in the stub, the converter accepting a memory form and reporting the offset of a RIP-relative one. - The Linux-only execution block of `amd64_only_converter` got memory-form sites too. It is not compiled on Windows; I built and ran it locally only through a throwaway `mmap` shim over `VirtualAlloc` (not part of this PR), where it passes. It has not run on Linux. No title was run. ### 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](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) - [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 - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-05 00:22:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-05T00:22:28Z.


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
#485 docs/dev/TechnicalDebt.md L77-78
#537 docs/dev/TechnicalDebt.md L77-78
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5986009668 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/588#issuecomment-5986009668). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-05T00:22:28Z. --- <!-- 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 | |---|---|---|---| | #485 | `docs/dev/TechnicalDebt.md` [L77-78](https://github.com/boykopovar/AnyPS5/blob/4ae4f53ffb4a5fcb6a42a166b0acb6f654a6711e/docs/dev/TechnicalDebt.md#L77-L78) | | | | #537 | `docs/dev/TechnicalDebt.md` [L77-78](https://github.com/boykopovar/AnyPS5/blob/4ae4f53ffb4a5fcb6a42a166b0acb6f654a6711e/docs/dev/TechnicalDebt.md#L77-L78) | | |
oneandonlydean commented 2026-10-05 01:18:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T01:18:33Z.


Thanks, this follows the SHA-256 memory path from #434 closely: the scratch register is chosen away from the destination, the stack displacement is adjusted by the spill depth, and segment and address-size prefixes carry over to the load. Reading the SHA1RNDS4 immediate after the operand through EncodedSize is the right fix. I disassembled the test encodings with llvm-mc and each one is the form the test expects (for example sha1rnds4 xmm10, xmmword ptr gs:[esp+4*ecx-16], 254 and sha1msg1 xmm1, [rsp+24], with the stub loading from [rsp+168]).

You mentioned the Linux execution block hadn't run on Linux: amd64_only_converter passes here on Linux, and on main's code it doesn't build (Sha1Operands has no Memory).

Optional, and the same as #434: a legacy SSE SHA instruction faults on a memory operand that isn't 16-byte aligned, while the movdqu load doesn't. That only matters for a program that would crash on the console anyway.

PPSA21564 has no SHA instructions, so this doesn't change its relink. Ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5986482891 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/588#issuecomment-5986482891). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T01:18:33Z. --- Thanks, this follows the SHA-256 memory path from #434 closely: the scratch register is chosen away from the destination, the stack displacement is adjusted by the spill depth, and segment and address-size prefixes carry over to the load. Reading the SHA1RNDS4 immediate after the operand through `EncodedSize` is the right fix. I disassembled the test encodings with llvm-mc and each one is the form the test expects (for example `sha1rnds4 xmm10, xmmword ptr gs:[esp+4*ecx-16], 254` and `sha1msg1 xmm1, [rsp+24]`, with the stub loading from `[rsp+168]`). You mentioned the Linux execution block hadn't run on Linux: `amd64_only_converter` passes here on Linux, and on main's code it doesn't build (`Sha1Operands` has no `Memory`). Optional, and the same as #434: a legacy SSE SHA instruction faults on a memory operand that isn't 16-byte aligned, while the `movdqu` load doesn't. That only matters for a program that would crash on the console anyway. PPSA21564 has no SHA instructions, so this doesn't change its relink. Ready to merge from my side.
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!588
No description provided.