feat(relinker): lower SHA-256 instructions with a memory operand #434
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/434"
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 adriadam10 on 2026-10-03T06:32:44Z.
What
--to-intelthrew 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:
DecodeSha256now decodes the memory operand (DecodeMemoryOperandinStubBodyBuilder: segment/67h prefixes, REX.X/B, ModRM, SIB, displacement) instead of throwing.movdqu, runs the existing register-form lowering with that register as the source, then restores it.StubBodyBuilderkeeps track of how far its spills have movedrsp. 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, notmovdqa: on this AMD host a misalignedsha256msg1 xmm1, [mem]does not fault, so the aligned load would add a fault the original didn't have.docs/dev/TechnicalDebt.mdline 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.mainthe new execution and converter checks fail with "SHA-256 instruction with a memory operand" (the decode checks don't compile there becauseMemorydoesn't exist yet).MatchSequencethat 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).ctest: 125/125.git merge-treeagainst every open relinker PR (#298 #299 #340 #350 #353 #356 #418 #430 #431 #432 #433): clean.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt (n/a).mdfiles or images added to the repository (attach them to this PR)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.mdsays "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.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").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.