Skip to content

feat(relinker): lower SHA-256 instructions with a memory operand - #434

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
adriadam10:feat/relinker-sha256-memory
Oct 4, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
adriadam10:feat/relinker-sha256-memory

Conversation

@adriadam10

Copy link
Copy Markdown
Collaborator

What

--to-intel threw 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:

  • DecodeSha256 now decodes the memory operand (DecodeMemoryOperand in StubBodyBuilder: segment/67h prefixes, REX.X/B, ModRM, SIB, displacement) instead of throwing.
  • The stub spills a scratch xmm, loads the operand into it with movdqu, runs the existing register-form lowering with that register as the source, then restores it.
  • StubBodyBuilder keeps track of how far its spills have moved rsp. 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, not movdqa: on this AMD host a misaligned sha256msg1 xmm1, [mem] does not fault, so the aligned load would add a fault the original didn't have.
  • RIP-relative operands still throw: the stub runs at a different address and nothing relocates the displacement yet.

docs/dev/TechnicalDebt.md line 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.

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 (n/a)
  • 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

@oneandonlydean

Copy link
Copy Markdown
Collaborator

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.md says "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.

adriadam10 and others added 2 commits October 4, 2026 08:21
SHA256RNDS2/MSG1/MSG2 with a memory source made `--to-intel` fail the
whole relink. The stub now spills a scratch xmm, loads the operand into
it with MOVDQU (AMD does not fault on a misaligned SHA operand, so the
unaligned load keeps that behaviour) and runs the register-form
lowering. RSP-based operands get the spill depth added to their
displacement. RIP-relative operands still fail: the stub runs at a
different address.

Co-Authored-By: Claude <noreply@anthropic.com>
Co-Authored-By: Claude <noreply@anthropic.com>
@adriadam10
adriadam10 force-pushed the feat/relinker-sha256-memory branch from f0df7f2 to d43a548 Compare October 4, 2026 06:22
@adriadam10

Copy link
Copy Markdown
Collaborator Author

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").

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Thanks for the quick update, that covers it. I'll test it here and merge once CI is green.

@oneandonlydean
oneandonlydean merged commit dda274b into boykopovar:main Oct 4, 2026
3 checks passed
@adriadam10
adriadam10 deleted the feat/relinker-sha256-memory branch October 5, 2026 06:47
SP4C3B4R-8 pushed a commit to SP4C3B4R-8/AnyPS5 that referenced this pull request Oct 6, 2026
Takes the 13 gate-batch PRs that merged upstream (boykopovar#319 boykopovar#329 boykopovar#330 boykopovar#331 boykopovar#325
boykopovar#435 boykopovar#471 boykopovar#294 boykopovar#376 boykopovar#404 boykopovar#394 boykopovar#401 boykopovar#446) in place of astrobot's local
versions, as integ/gate-batch cab0eab had them: boykopovar#294 and boykopovar#376 in
astrobot's depth model (DepthTarget, state.depth), boykopovar#394 with the thick mip
tails, boykopovar#401 over astrobot's slab pool, boykopovar#471 keeping the settled single-lane
path. Also takes the rest of main since a818bee (boykopovar#532 boykopovar#340 boykopovar#486 boykopovar#484 boykopovar#316
boykopovar#243 boykopovar#334 boykopovar#434 boykopovar#437 boykopovar#470 boykopovar#373 boykopovar#452 boykopovar#455 and our upstreamed PRs, where
astrobot keeps its later revisions and env switches).

Resolution notes: relinker converter/matcher/tests are upstream's (boykopovar#432
decoder covers VEX RIP-relative followers); RecompileResult gains boykopovar#316's
hostSubgroupSize (set from request.target.subgroupSize); shader disk cache
format 9; boykopovar#452's scissor message; upstream's later live-stack and
memory-copy decode tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Mgp3oZvembQziVNE2xARQK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants