Skip to content

fix(relinker): reject LOCK-prefixed SIMD lowerings - #10340

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
Filip722:submit/lock-20261023
Oct 25, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
Filip722:submit/lock-20261023

Conversation

@Filip722

@Filip722 Filip722 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

What

LOCK-prefixed SHA-1/SHA-256 and SSE4a EXTRQ/INSERTQ instructions could be decoded and lowered after discarding LOCK, turning invalid guest bytes into executable code. Reject these encodings before generating replacements.

  • Reject LOCK in the shared SHA-1, SHA-256 and EXTRQ/INSERTQ operand decoders.
  • Cover register and memory forms and LOCK before or after another legacy prefix in the existing fixture.
  • Name the LOCK prefix in SHA diagnostics and verify conversion fails with the correct file offset for both SHA families.

MOVNTSS/MOVNTSD use a separate in-place matcher and are outside this PR's decoder fixes. Their pre-existing rejection/diagnostic gap is addressed independently in #10713; this PR does not claim complete SSE4a LOCK rejection. Neither PR depends on the other.

Tested

Linux x86-64: the regressions fail on unchanged main and each family fails independently when its guard is removed. The complete relinker suite passes. All six invalid byte sequences raise SIGILL on AMD Ryzen 5 7640HS. Windows and macOS execution not tested.

Original patch based on main a19e7f6fdc54. Manual review of conflicting #7918, #8042, #8236 and #9184 found only disjoint test changes.

Review update cfa8c10d2184: Linux x86-64, GCC 15.2.0, Release relinker-only build and the complete suite pass, including the new SHA-1 and SHA-256 converter rejection/file-offset checks. The native Linux probes described above and the native Windows validation cover the original head 1e00b460b9f2; native instruction probes and Windows/macOS execution were not rerun for this review update.

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@oneandonlydean oneandonlydean self-assigned this Oct 23, 2026
@Filip722

Copy link
Copy Markdown
Contributor Author

Additional native Windows validation of exact PR head 1e00b460b9f2606f15d401e8f67094af8a6bf06b, compared with its unchanged parent a19e7f6fdc5475c538e2a9135aa45880503ef377.

Windows 11 Pro 26H2 26300.9711, 4 vCPUs exposed from an AMD Ryzen 7 8845HS, WinLibs GCC 15.2.0 x86_64-ucrt-posix-seh r7, CMake 4.3.1, Ninja 1.13.2 and Python 3.13.7. Release relinker-only build with BUILD_TESTING=ON; two build/test jobs. Defender remained enabled.

  • All 21 focused checks passed on both the unchanged parent and this exact PR head, with no skips, including amd64_only_converter, amd64_only_windows, decoder_length and Windows trampoline/PE checks.
  • Negative control: applying only this PR's converter fixture to unchanged parent production code makes amd64_only_converter fail with LOCK EXTRQ was accepted. The exact PR passes all six new decoder rejection checks.
  • Independent native execution in the Windows VM: all six added LOCK-prefixed encodings (SHA-1, SHA-256 and SSE4a, including the two memory forms) raised 0xC000001D / STATUS_ILLEGAL_INSTRUCTION. Removing LOCK from each encoding allowed all six to execute. Each probe used a separate process and valid memory for memory operands. This corroborates rejection of these encodings, rather than dropping LOCK during lowering.

This supplements the already-passing Windows CI with exact-head WinLibs testing and negative controls. Only the 21 selected checks are claimed: an initial broader suite was stopped before completion. No title, GPU, performance or full-runtime testing was performed. AI-assisted validation.

@roboterspinne roboterspinne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed head 1e00b46 merged onto upstream/main aeeaefa (2026-10-24); the merge is clean, also onto the current main 6cd36e6, which extended Amd64OnlyConverterTests.cpp since this branch's base without conflict.

Environment: Windows 11 Home 10.0.26200, Intel i9-12900H, WinLibs GCC 15.2.0 UCRT, CMake 4.3.1, Ninja 1.13.2, Python 3.13.9, -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, Release.

Results

  • amd64_only_converter passes on the merged tree (0.3 s). Red/green per family: with only Sha1Operands.cpp reverted the test fails LOCK SHA1RNDS4 was accepted, with only Sha256Operands.cpp LOCK SHA256RNDS2 was accepted, with only Sse4aOperands.cpp LOCK EXTRQ was accepted; with all three guards it passes. That confirms the body's "each family fails independently", which a single red run cannot show because the harness stops at the first failure.
  • End to end (--to-intel --skip-syscall-check --skip-sce-module, code at 0x4000): main lowers f0 0f 38 cb ca as Intel substitution: SHA256RNDS2 at 0x4000 (5 bytes) -> stub 785 bytes and replaces the site with e9 eb 10 00 00, likewise f0 0f 3a cc ca 00 (SHA1RNDS4, 6 bytes) and f0 66 0f 78 c0 08 00 (EXTRQ, 7 bytes); this branch exits 2 with FAIL: Not a SHA-256 instruction (offset 0x4000), FAIL: Not a SHA-1 instruction (offset 0x4000) and FAIL: SSE4a instruction with a LOCK prefix (offset 0x4000). So the LOCK encodings do reach the decoders through DecodedInstruction::_skipPrefixesAndRex (which skips PrefixLock) and the matcher, and the throw fails the conversion through _atFileOffset with no catch in between; prefix-order variants (48 f0 ..., f0 48 ..., f0 f0 ..., f0 2e ...) are all refused.
  • Native probes (child process per sequence): all six fixture encodings raise STATUS_ILLEGAL_INSTRUCTION (0xc000001d); SHA256RNDS2, SHA256MSG1 [mem], SHA1RNDS4 and SHA1MSG1 [mem] without LOCK execute. EXTRQ/INSERTQ without LOCK also fault here because this Intel CPU has no SSE4a, so the SSE4a half rests on your Ryzen runs and the SDM (LOCK "can be prepended only to" the RMW list; otherwise "an undefined opcode exception will also be generated").
  • Failing the conversion matches the project's precedent for mandatory lowerings (DecodeClzero throws CLZERO with a LOCK prefix; MONITORX/MWAITX/RDPRU/MCOMMIT fail via Unsupported); only optional lowerings leave bytes in place.
  • check_conventions.py --base upstream/main passes; pr_overlap.py lists only #8042, #8236 and #9184 as not compared (same test file).

Should fix

  • MOVNTSS/MOVNTSD, the other two SSE4a lowerings, still accept LOCK. Amd64OnlyInstructionMatcher::_matchMovnts (lines 200-211) only rejects the register form, and IsMovntss/IsMovntsd go through the same _skipPrefixesAndRex, so the matcher dispatches to it before DecodeSse4a ever runs (lines 401-405). With this branch, f0 f3 0f 2b 00 c3 yields exit 0, Intel substitution: MOVNTSS at 0x4000 (5 bytes) -> in place 5 bytes, and the site becomes f0 f3 0f 11 00 = lock movss DWORD PTR [rax],xmm0, which is #UD (0xc000001d natively; MOVSS is not in the LOCK list); same for MOVNTSD (f0 f2 0f 2b 00 -> f0 f2 0f 11 00) and for LOCK after REP (f3 f0 0f 2b 00). This is pre-existing on main and milder than the SHA/EXTRQ case (the output faults like the guest instead of running working code), but it is a reported success for invalid guest bytes, the class this PR closes, and the title and body claim the SSE4a family. Suggest a LOCK scan over the prefix bytes in _matchMovnts throwing MOVNTSS/MOVNTSD with a LOCK prefix (as ClzeroOperands.cpp:29-31 does) plus two requireFailure cases next to the register-form test ({0xF0, 0xF3, 0x0F, 0x2B, 0x07}, {0xF2, 0xF0, 0x0F, 0x2B, 0x07}), or name the gap in the body as a follow-up.

Nits

  • Sha1Operands.cpp:23 / Sha256Operands.cpp:23: LOCK shares the Not a SHA-… instruction throw with 66/F2/F3; the SSE4a and CLZERO decoders name the LOCK prefix. A user converting a title would see FAIL: Not a SHA-256 instruction for bytes the matcher already identified as SHA-256.
  • Body: "the historical native instruction/regression evidence above is retained" refers to nothing above the Tested paragraph; drop it or attach the Linux logs.
  • The six checks exercise the decoders directly; a converter-level case like LOCK RDPRU was accepted (test line ~514) for one LOCK SHA site would pin the end-to-end rejection I ran by hand.

Not checked: AMD hardware (your Ryzen runs cover the SSE4a-without-LOCK side); the full suite on an idle machine; Rosetta-target paths; whether real titles carry such bytes in scanned code.

I found no blocking issue: the three decoders now refuse what the hardware refuses, the counter-check holds per family, and the conversion fails fast where main produced working code for invalid bytes. The one thing I would settle before merge is MOVNTSS/MOVNTSD, in this PR or as a declared follow-up. For disclosure: I have open relinker PRs (#10564, #10402, #10401, #10403, #10390, #10391); none touches codegen/.

Written with AI assistance (Claude Code). I ran the builds, tests, end-to-end conversions and native probes above on my Windows 11 machine.

continue;
}
if (b == PrefixOperandSize || b == PrefixRepne || b == PrefixRep) {
if (b == PrefixLock || b == PrefixOperandSize || b == PrefixRepne || b == PrefixRep) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: for f0 the matcher has already identified a SHA-256 opcode, so FAIL: Not a SHA-256 instruction (offset 0x4000) hides the cause; the SSE4a and CLZERO decoders say "... with a LOCK prefix". A separate if (b == PrefixLock) throw CodegenException("SHA-256 instruction with a LOCK prefix"); (and the SHA-1 twin) would match them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in cfa8c10: both SHA decoders now report SHA-1 instruction with a LOCK prefix / SHA-256 instruction with a LOCK prefix, matching the explicit SSE4a/CLZERO diagnostics. Linux Release relinker-only build and all 240 checks pass. AI-assisted.

pos += 1;
continue;
}
if (b == PrefixLock) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This never runs for the other two SSE4a lowerings: MOVNTSS/MOVNTSD are dispatched to _matchMovnts (Amd64OnlyInstructionMatcher.cpp:401-405) before DecodeSse4a, and that path only rejects the register form, so f0 f3 0f 2b 00 still converts with Intel substitution: MOVNTSS ... in place and the site becomes f0 f3 0f 11 00 (lock movss, #UD, 0xc000001d natively); same for MOVNTSD and for f3 f0 .... Pre-existing on main, but the title claims the SSE4a family. A LOCK scan in _matchMovnts throwing MOVNTSS/MOVNTSD with a LOCK prefix (as ClzeroOperands.cpp:29-31) plus two requireFailure cases would close it; otherwise please name it as a follow-up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept the decoder fixes scoped here as agreed with @oneandonlydean. The description now explicitly excludes MOVNTSS/MOVNTSD and names independent follow-up #10713. That PR rejects LOCK in the in-place matcher, covers both prefix orders for both MOVNT forms at converter level, and checks the failure offset. The new regression fails on unchanged main and passes with the guard; native AMD probes fault for all four LOCK encodings while all four unlocked controls execute. AI-assisted.

}

void sse4aOperands() {
requireFailure([] { const Bytes bytes = {0xF0, 0x66, 0x0F, 0x78, 0xC0, 0x08, 0x00}; (void)Codegen::DecodeSse4a(bytes.data(), bytes.size()); }, "LOCK EXTRQ was accepted");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, optional: these exercise the decoders directly; one converter-level case in the style of LOCK RDPRU was accepted (line ~514) for a LOCK SHA site would also pin that the conversion fails rather than lowering, which I checked by hand (FAIL: Not a SHA-256 instruction (offset 0x4000) on this branch, Intel substitution: SHA256RNDS2 ... -> stub 785 bytes on main).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added converter-level rejection cases for both SHA-256 and SHA-1 in cfa8c10. They prepend LOCK to the existing valid segment fixtures and require conversion to throw with file offset 0x200; the unchanged valid fixtures still lower successfully. All 240 Linux relinker-only checks pass. AI-assisted.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

@Filip722 @roboterspinne I checked the dispatch order and agree that LOCK MOVNTSS/MOVNTSD bypass DecodeSse4a: _matchMovnts preserves F0 while replacing 0F 2B with 0F 11. The resulting lock movss/movsd is still invalid. I’m comfortable keeping the three decoder fixes scoped here and handling MOVNTSS/MOVNTSD in a declared follow-up: this is a pre-existing rejection/diagnostic gap, and the output still faults rather than becoming working code for invalid guest bytes. Please make that scope explicit in the description and name the follow-up before landing, so this is not presented as complete SSE4a LOCK rejection. A prefix-order regression for each MOVNT form and a converter-level SHA case would make good coverage for that work.

@Filip722

Copy link
Copy Markdown
Contributor Author

@roboterspinne @oneandonlydean addressed the review in cfa8c10d2184: explicit SHA LOCK diagnostics and converter-level rejection/file-offset coverage for both SHA families. I also removed the confusing historical-evidence sentence from the description and clarified which checks cover the earlier head.

The separate MOVNTSS/MOVNTSD follow-up is #10713. It rejects LOCK in the in-place matcher and covers LOCK before/after the mandatory prefix for each form. The description here now limits this PR to SHA-1, SHA-256, EXTRQ and INSERTQ; neither PR depends on the other.

Local validation: the complete Linux Release relinker-only suites pass (240 checks here; 248 for #10713). The MOVNT regression fails on unchanged main, and all four native AMD LOCK probes raise SIGILL while unlocked controls execute. Convention and whitespace checks pass. Hosted checks for the new commits are still running. AI-assisted implementation and validation.

@roboterspinne

Copy link
Copy Markdown
Contributor

@Filip722 Thanks, checked cfa8c10 merged onto upstream/main 2fade7e (2026-10-24) on Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0 UCRT, CMake 4.3.1, Ninja 1.13.2, -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, Release; the merge is clean and the update changes only the two SHA decoders and the converter test.

  • amd64_only_converter, amd64_only_windows and decoder_length pass on the merged tree, so the two new converter-level cases (LOCK inserted at 0x200, failure offset 0x200, the unchanged fixtures still lowered through stubs) hold on Windows too.
  • Counter-check: with main's Sha1Operands.cpp and Sha256Operands.cpp under this head's test file, amd64_only_converter fails at its first LOCK SHA case (LOCK SHA256RNDS2 was accepted). The harness stops at the first failure, so the converter-level cases were not reached in that run; on main the same site lowers in place (checked by hand in my review), so they would fail as well.
  • Full relinker-only suite on the merged tree: 244 tests, 243 pass, long_paths skipped; the one failure is patch_advisor_unit, which fails on main whenever the temp path contains a space (test(tools): compare quoted and resolved paths in the patch advisor test #10667 fixes it) and is unrelated to this PR.
  • The diagnostics now read SHA-1 instruction with a LOCK prefix and SHA-256 instruction with a LOCK prefix, matching the SSE4a and CLZERO messages. The description's scope and fix(relinker): reject LOCK-prefixed MOVNTSS and MOVNTSD #10713 (two production lines in _matchMovnts, converter-level cases for both prefix orders of both MOVNT forms) cover what I asked for in the Sse4aOperands.cpp comment; the two heads merge cleanly with each other (different test functions), so the order does not matter.

All three of my comments are addressed from my side. Hosted CI on cfa8c10 was green on ubuntu, windows and ASan when I looked; the macOS jobs were still running.

Written with AI assistance (Claude Code). I ran the build and tests above on my Windows 11 machine.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head cfa8c10d2184210f0eaf6aa79e9441f72c1f28a4. The SHA-1/SHA-256 decoders and SSE4a decoder now reject LOCK explicitly, and the tests cover register and memory encodings plus converter-level failure offsets for both SHA families. The stated scope excludes LOCK-prefixed MOVNTSS/MOVNTSD, which is tracked separately in #10713.

I configured a Release relinker-only build, compiled amd64_only_converter_tests, and ran ctest -R '^amd64_only_converter$' (1/1 passed). tools/check_conventions.py --base upstream/main --head upstream/pr-10340 and git diff --check also pass. Hosted eligibility/conventions/labels, Ubuntu/Windows/macOS relinker and ASan checks are green; product jobs are skipped by policy. The author additionally reports all 240 Linux relinker-only checks passing. I found no blocker.

@oneandonlydean
oneandonlydean merged commit a512780 into boykopovar:main Oct 25, 2026
14 checks passed
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.

4 participants