cpu: add dedicated cpu_sha256_aot lifter for SHA256RNDS2, SHA256MSG1, and SHA256MSG2 #2389

Merged
Ammmae0x merged 3 commits from upstream-pr/2397 into main 2026-11-25 18:50:48 +00:00
Ammmae0x commented 2026-11-25 15:10:50 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-25T15:10:50Z.


Change

Add a dedicated, portable C++ AOT lifter tools/cpu_sha256_aot.py for the three Intel SHA-256 NI instructions from docs/coverage/x64_zen2.tsv group Crypto:

  • SHA256RNDS2 (NP 0F 38 CB /r, register and 16-byte aligned flat m128): two SHA-256 rounds using implicit XMM0[63:0] as WK1:WK0 (ignoring XMM0[127:64] and rejecting undefined implicit XMM0).
  • SHA256MSG1 (NP 0F 38 CC /r, register and 16-byte aligned flat m128): intermediate SHA-256 message schedule adding sigma0(W[i+1]) using src[31:0] for lane 3.
  • SHA256MSG2 (NP 0F 38 CD /r, register and 16-byte aligned flat m128): final SHA-256 message schedule with sequential dependency of lanes 2..3 on updated lanes 0..1.
  • Pure portable C++ uint32_t implementation (zero <immintrin.h> SHA intrinsics), movdqa (16B aligned) / movdqu transport over flat [rcx|rdx|r8] loads, [r8] stores, reg-reg copies, ret, and host_has_sha() (CPUID.07H.00H:EBX[29]).
  • Reject SHA1*, AES*/PCLMUL*, VEX/EVEX encodings, nonvolatile XMM6..15, undefined XMM reads (including implicit XMM0), unaligned/displaced/indexed/non-flat memory, stores outside [r8], prefixes, branches, and missing/trailing ret.
  • Add source-built MASM fixture tests/cpu/sha256_aot.{asm,def,_check.py} (; coverage: record cpu_aot_sha256) and register cpu_aot_sha256 in CMakeLists.txt.

Verification

Verified on commit 364f8954 (rebased on upstream/main 0b31989c) across both dev (Debug) and release (/O2) presets:

  • cmake --build --preset dev --target cpu_sha256_aot_original cpu_sha256_aot_original_lifted cpu_sha256_aot_altered cpu_sha256_aot_altered_lifted -> exit 0
  • ctest --preset dev -R "^cpu_aot_sha256$" -E "cpu_native_archive_" --output-on-failure -V -> 100% tests passed, 0 tests failed out of 1 (0 skipped, 0.82s):
    • 3 SHA-256 NI forms accepted; 35 IR + 9 PE + CLI rejection controls passed
    • 1503 portable lifted + 480 /O2 cases (incl. NIST FIPS 180-4 single/multi-block & aliasing) passed; 8 mutants + 5 altered-build controls caught
    • 1493 native vs lifted SHA-NI differential cases matched bit for bit
  • cmake --build --preset release --target cpu_sha256_aot_original cpu_sha256_aot_original_lifted cpu_sha256_aot_altered cpu_sha256_aot_altered_lifted -> exit 0
  • ctest --preset release -R "^cpu_aot_sha256$" -E "cpu_native_archive_" --output-on-failure -V -> 100% tests passed, 0 tests failed out of 1 (0 skipped, 1.06s)
  • FORM_COVERAGE_RECORD verified in both out/build/dev/form_coverage/cpu_aot_sha256.txt and out/build/release/form_coverage/cpu_aot_sha256.txt: movdqa, movdqu, ret, sha256msg1, sha256msg2, sha256rnds2
  • out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py -> form coverage checks passed (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/docs_links_check.py -> Self-test passed. Checked 19 Markdown files and 464 links. (exit 0)
  • out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py -> Self-test passed. Checked 1722 tracked source, script and build/CI files: no bidirectional controls. (exit 0)

Spot mutation checks against tools/cpu_sha256_aot.py (8/8 killed by tests/cpu/sha256_aot_check.py, restored cleanly):

  • Break sequential SHA256MSG2 lane 2 dependency on w0 (/O2), read wrong SHA256MSG1 source lane (/O2), swap SHA256RNDS2 WK word (/O2), swap SHA256RNDS2 output lanes e1/e2 (/O2), drop undefined implicit XMM0 check, bypass SHA-1 rejection check, bypass 16B alignment check, and bypass r8-only store guard (all returned exit 1).

Evidence and follow-up

  • tests/cpu/sha256_aot.{asm,def,_check.py} is a clean-room, source-built MASM fixture (MIT, repo-authored; no game files, dumps, or binaries).
  • Unconditionally verifies lift, rejections, portable FIPS 180-4 single/multi-block 64-round compression, register/pointer aliasing, /O2 build, mutants, and ALTER_SHA256 before any host_has_sha() exit-77 capability skip (0 skips on this SHA-NI-capable host).

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6607613715 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2397). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-25T15:10:50Z. --- ## Change Add a dedicated, portable C++ AOT lifter `tools/cpu_sha256_aot.py` for the three Intel SHA-256 NI instructions from `docs/coverage/x64_zen2.tsv` group `Crypto`: - `SHA256RNDS2` (`NP 0F 38 CB /r`, register and 16-byte aligned flat `m128`): two SHA-256 rounds using implicit `XMM0[63:0]` as `WK1:WK0` (ignoring `XMM0[127:64]` and rejecting undefined implicit `XMM0`). - `SHA256MSG1` (`NP 0F 38 CC /r`, register and 16-byte aligned flat `m128`): intermediate SHA-256 message schedule adding `sigma0(W[i+1])` using `src[31:0]` for lane 3. - `SHA256MSG2` (`NP 0F 38 CD /r`, register and 16-byte aligned flat `m128`): final SHA-256 message schedule with sequential dependency of lanes 2..3 on updated lanes 0..1. - Pure portable C++ `uint32_t` implementation (zero `<immintrin.h>` SHA intrinsics), `movdqa` (16B aligned) / `movdqu` transport over flat `[rcx|rdx|r8]` loads, `[r8]` stores, reg-reg copies, `ret`, and `host_has_sha()` (`CPUID.07H.00H:EBX[29]`). - Reject `SHA1*`, `AES*`/`PCLMUL*`, VEX/EVEX encodings, nonvolatile `XMM6..15`, undefined XMM reads (including implicit `XMM0`), unaligned/displaced/indexed/non-flat memory, stores outside `[r8]`, prefixes, branches, and missing/trailing `ret`. - Add source-built MASM fixture `tests/cpu/sha256_aot.{asm,def,_check.py}` (`; coverage: record cpu_aot_sha256`) and register `cpu_aot_sha256` in `CMakeLists.txt`. ## Verification Verified on commit `364f8954` (rebased on `upstream/main` `0b31989c`) across both `dev` (Debug) and `release` (`/O2`) presets: - `cmake --build --preset dev --target cpu_sha256_aot_original cpu_sha256_aot_original_lifted cpu_sha256_aot_altered cpu_sha256_aot_altered_lifted` -> exit 0 - `ctest --preset dev -R "^cpu_aot_sha256$" -E "cpu_native_archive_" --output-on-failure -V` -> `100% tests passed, 0 tests failed out of 1` (0 skipped, 0.82s): - `3 SHA-256 NI forms accepted; 35 IR + 9 PE + CLI rejection controls passed` - `1503 portable lifted + 480 /O2 cases (incl. NIST FIPS 180-4 single/multi-block & aliasing) passed; 8 mutants + 5 altered-build controls caught` - `1493 native vs lifted SHA-NI differential cases matched bit for bit` - `cmake --build --preset release --target cpu_sha256_aot_original cpu_sha256_aot_original_lifted cpu_sha256_aot_altered cpu_sha256_aot_altered_lifted` -> exit 0 - `ctest --preset release -R "^cpu_aot_sha256$" -E "cpu_native_archive_" --output-on-failure -V` -> `100% tests passed, 0 tests failed out of 1` (0 skipped, 1.06s) - `FORM_COVERAGE_RECORD` verified in both `out/build/dev/form_coverage/cpu_aot_sha256.txt` and `out/build/release/form_coverage/cpu_aot_sha256.txt`: `movdqa, movdqu, ret, sha256msg1, sha256msg2, sha256rnds2` - `out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py` -> `form coverage checks passed` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/docs_links_check.py` -> `Self-test passed. Checked 19 Markdown files and 464 links.` (exit 0) - `out/pe-env/Scripts/python.exe -B tests/bidi_controls_check.py` -> `Self-test passed. Checked 1722 tracked source, script and build/CI files: no bidirectional controls.` (exit 0) Spot mutation checks against `tools/cpu_sha256_aot.py` (8/8 killed by `tests/cpu/sha256_aot_check.py`, restored cleanly): - Break sequential `SHA256MSG2` lane 2 dependency on `w0` (`/O2`), read wrong `SHA256MSG1` source lane (`/O2`), swap `SHA256RNDS2` `WK` word (`/O2`), swap `SHA256RNDS2` output lanes `e1/e2` (`/O2`), drop undefined implicit `XMM0` check, bypass SHA-1 rejection check, bypass 16B alignment check, and bypass `r8`-only store guard (all returned exit 1). ## Evidence and follow-up - `tests/cpu/sha256_aot.{asm,def,_check.py}` is a clean-room, source-built MASM fixture (MIT, repo-authored; no game files, dumps, or binaries). - Unconditionally verifies lift, rejections, portable FIPS 180-4 single/multi-block 64-round compression, register/pointer aliasing, `/O2` build, mutants, and `ALTER_SHA256` before any `host_has_sha()` exit-77 capability skip (0 skips on this SHA-NI-capable host). ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
tedin7 commented 2026-11-25 17:01:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T16:29:53Z.

Review: COMMENTED


Review of head 364f8954 (comment only).

Checked:

  • git diff --stat origin/main...HEAD: 5 files, new lifter, asm fixture, check script and a 24-line CMake block. Same shape as the sibling cpu_*_aot lifters, so it is small and additive.
  • No other open or merged PR covers SHA256RNDS2/MSG1/MSG2 (gh pr list --state all --search "SHA256RNDS2 sha256").
  • Test strength: tests/cpu/sha256_aot_check.py runs compiled original vs lifted executables against Python references (ref_sha256rnds2, ref_sha256msg1, ref_sha256msg2) on edge, carry-wrap, lane-isolation and random inputs. It also checks that the upper 64 bits of XMM0 are ignored, aliasing, memory-source forms, mutants, an altered binary and rejection cases.
  • It also drives a full 64-round compression through the lifted exports (line ~509) and compares the digest with hashlib, which is an independent end-to-end reference.
  • Python compiles with python3 -I -m py_compile.

No blocking findings. Win2025 WARP dev+release will be run next.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6061187237 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2397#pullrequestreview-6061187237). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T16:29:53Z. **Review:** COMMENTED --- Review of head 364f8954 (comment only). Checked: - `git diff --stat origin/main...HEAD`: 5 files, new lifter, asm fixture, check script and a 24-line CMake block. Same shape as the sibling `cpu_*_aot` lifters, so it is small and additive. - No other open or merged PR covers SHA256RNDS2/MSG1/MSG2 (`gh pr list --state all --search "SHA256RNDS2 sha256"`). - Test strength: `tests/cpu/sha256_aot_check.py` runs compiled original vs lifted executables against Python references (`ref_sha256rnds2`, `ref_sha256msg1`, `ref_sha256msg2`) on edge, carry-wrap, lane-isolation and random inputs. It also checks that the upper 64 bits of XMM0 are ignored, aliasing, memory-source forms, mutants, an altered binary and rejection cases. - It also drives a full 64-round compression through the lifted exports (line ~509) and compares the digest with `hashlib`, which is an independent end-to-end reference. - Python compiles with `python3 -I -m py_compile`. No blocking findings. Win2025 WARP dev+release will be run next. AI-assisted; commands listed were executed locally.
KelboLive commented 2026-11-25 17:01:56 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by KelboLive on 2026-11-25T16:07:10Z.


APPROVED. Dedicated cpu_sha256_aot lifter for SHA256 ops.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7167609442 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2397#issuecomment-7167609442). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-25T16:07:10Z. --- APPROVED. Dedicated cpu_sha256_aot lifter for SHA256 ops.
tedin7 commented 2026-11-25 17:01:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T16:33:44Z.


Win2025 WARP receipt for head 8f0c8fb7 after updating with main.

Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit 8f0c8fb7cf581d47e179c838fcc11fb09efb1eed:

  • Debug (--preset dev):
    • Build (cmake --build --preset dev --target cpu_sha256_aot_altered cpu_sha256_aot_altered_lifted cpu_sha256_aot_original cpu_sha256_aot_original_lifted -- -j 2): 3.58s (configure 1.66s)
    • Test (ctest --preset dev -R 'cpu_aot_sha256' --parallel 6 --no-tests=error --output-on-failure): 1/1 passed (1.02s: cpu_aot_sha256 0.91s)
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release --target cpu_sha256_aot_altered cpu_sha256_aot_altered_lifted cpu_sha256_aot_original cpu_sha256_aot_original_lifted -- -j 2): 1.02s (configure 1.66s)
    • Test (ctest --preset release -R 'cpu_aot_sha256' --parallel 6 --no-tests=error --output-on-failure): 1/1 passed (0.89s: cpu_aot_sha256 0.80s)
  • sccache: 0/4 hits; wall 15.3s

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7168177627 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2397#issuecomment-7168177627). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T16:33:44Z. --- Win2025 WARP receipt for head 8f0c8fb7 after updating with main. Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `8f0c8fb7cf581d47e179c838fcc11fb09efb1eed`: - **Debug (`--preset dev`)**: - Build (`cmake --build --preset dev --target cpu_sha256_aot_altered cpu_sha256_aot_altered_lifted cpu_sha256_aot_original cpu_sha256_aot_original_lifted -- -j 2`): `3.58s` (configure `1.66s`) - Test (`ctest --preset dev -R 'cpu_aot_sha256' --parallel 6 --no-tests=error --output-on-failure`): **1/1 passed** (`1.02s`: `cpu_aot_sha256` `0.91s`) - **Release `/O2` (`--preset release`)**: - Build (`cmake --build --preset release --target cpu_sha256_aot_altered cpu_sha256_aot_altered_lifted cpu_sha256_aot_original cpu_sha256_aot_original_lifted -- -j 2`): `1.02s` (configure `1.66s`) - Test (`ctest --preset release -R 'cpu_aot_sha256' --parallel 6 --no-tests=error --output-on-failure`): **1/1 passed** (`0.89s`: `cpu_aot_sha256` `0.80s`) - `sccache`: 0/4 hits; wall `15.3s` AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-25 19:03:35 +00:00
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
R0Y00/Xbox2Windows!2389
No description provided.