feat(relinker): lower SHA-1 instructions with a memory operand #588
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/588"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by DotDebian on 2026-10-05T00:04:35Z.
What
--to-intelfailed 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.DecodeSha1decodes the ModRM/SIB/displacement into the sharedMemoryOperand. The SHA1RNDS4 immediate is read after the operand, not at a fixed offset.MemoryOperandgainsEncodedSize(ModRM through displacement), set byDecodeMemoryOperand, so a decoder can find what follows the operand.Sha1Loweringspills a scratch xmm, loads the operand withStubBodyBuilder::Loadand runs the existing register-form lowering. RSP-based operands get the spill depth throughLoad, as for SHA-256.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.amd64_only_convertergot memory-form sites too. It is not compiled on Windows; I built and ran it locally only through a throwawaymmapshim overVirtualAlloc(not part of this PR), where it passes. It has not run on Linux.No title was run.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
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.
main)docs/dev/TechnicalDebt.mdL77-78docs/dev/TechnicalDebt.mdL77-78Imported 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
EncodedSizeis the right fix. I disassembled the test encodings with llvm-mc and each one is the form the test expects (for examplesha1rnds4 xmm10, xmmword ptr gs:[esp+4*ecx-16], 254andsha1msg1 xmm1, [rsp+24], with the stub loading from[rsp+168]).You mentioned the Linux execution block hadn't run on Linux:
amd64_only_converterpasses here on Linux, and on main's code it doesn't build (Sha1Operandshas noMemory).Optional, and the same as #434: a legacy SSE SHA instruction faults on a memory operand that isn't 16-byte aligned, while the
movdquload 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.