cpu: lower LAR, LSL, VERR, and VERW segment inspection instructions #2035

Merged
Ammmae0x merged 2 commits from upstream-pr/2042 into main 2026-11-24 23:52:12 +00:00
Ammmae0x commented 2026-11-24 23:33:34 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T23:33:34Z.


Change

Add a bounded Windows x64 AOT lifter (tools/cpu_seg_inspect_aot.py), source-built MASM fixture (tests/cpu/seg_inspect.asm, tests/cpu/seg_inspect.def), differential verification harness (tests/cpu/seg_inspect_check.py), and CTest registration (cpu_aot_seg_inspect in CMakeLists.txt) covering the Integer segment inspection mnemonics from docs/coverage/x64_zen2.tsv:

  • lar r16/r32/r64, r/m16 (load access rights byte)
  • lsl r16/r32/r64, r/m16 (load segment limit)
  • verr r/m16 (verify segment for reading)
  • verw r/m16 (verify segment for writing)

Key architectural semantics modeled for Windows x64 user-mode (CPL=3) GDT descriptors:

  • LAR/LSL: on ZF=1, r16 updates [15:0] preserving [63:16], while r32/r64 zero-extend the 32-bit result to 64 bits; on ZF=0, all 64 bits of the destination remain untouched. Only ZF is updated; CF, PF, SF, and OF are preserved. Architecturally undefined LAR bits [19:16] (00FXFF00H) are masked (0x00F0FF00u) only on 32/64-bit LAR with ZF=1.
  • VERR/VERW: sets ZF=1 only for readable/writable user-mode GDT selectors (TI=0, non-null, present, S=1, DPL >= max(CPL, RPL) unless conforming code, and readable/writable type bits), leaving all other flags and GPRs untouched.
  • m16 selector loads execute unconditionally via volatile load_always<std::uint16_t>() so /O2 never eliminates faulting reads even when the result is discarded.
  • 32-bit CMOVE/CMOVNE zero-extends dst[63:32] even when the condition is false.
  • Rejects VEX/EVEX/XOP encodings, segment/LOCK/REP/REPNE prefixes, RIP-relative and SIB-indexed memory operands, clobbered pointer bases, nonvolatile and high-byte GPRs, non-leaf branches/calls, memory destinations for XOR/OR/ADD, and undefined GPR/flag reads.

Verification

Tested on commit 88767ebc (Windows x64, MSVC 14.50, dev Debug and release /O2):

  1. cmake --build --preset dev --target cpu_seg_inspect_original cpu_seg_inspect_original_lifted cpu_seg_inspect_altered cpu_seg_inspect_altered_lifted
    • Result: succeeded with 0 errors, 0 warnings.
  2. cmake --build --preset release --target cpu_seg_inspect_original cpu_seg_inspect_original_lifted cpu_seg_inspect_altered cpu_seg_inspect_altered_lifted
    • Result: succeeded with 0 errors, 0 warnings.
  3. ctest --preset dev -R "^cpu_aot_seg_inspect$" --output-on-failure -V
    • Result: 1/1 Test #648: cpu_aot_seg_inspect .............. Passed (unit rejections (6 raw + 21 IR), 7 PE-patched rejections, and CLI non-traceback rejections passed; seg_inspect passed: 2628 native, 2628 lifted, 2628 /O2 cases across 20 exports; ALTER_IMM diverged on {'verr_reg': 8, 'verw_reg': 8, 'verr_mem_disp': 8, 'verw_mem_disp': 8, 'seg_imm_select': 23, 'seg_flags_preserve': 23}). Capability skips: none (exits 77 on non-standard segment layouts).
  4. ctest --preset release -R "^cpu_aot_seg_inspect$" --output-on-failure
    • Result: 1/1 Test #648: cpu_aot_seg_inspect .............. Passed.
  5. FORM_COVERAGE_RECORD output (out/build/dev/form_coverage/cpu_aot_seg_inspect.txt and release):
    • Emits: add, cmove, lar, lsl, mov, ret, setb, sete, seto, setp, sets, verr, verw.
  6. .\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py
    • Result: form coverage checks passed (exit 0).
  7. .\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py
    • Result: Self-test passed. Checked 18 Markdown files and 391 links. (exit 0).
  8. .\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py
    • Result: Self-test passed. Checked 1404 tracked source, script and build/CI files: no bidirectional controls. (exit 0).

Evidence and follow-up

  • Fixture tests/cpu/seg_inspect.asm and tests/cpu/seg_inspect.def are original, source-built synthetic MASM test fixtures under the repository MIT license; no external binaries or game files are used.
  • Negative controls include 6 raw-byte excluded-encoding checks, 21 IR-level rejection checks, 7 PE-patched rejection checks, CLI non-traceback / zero-leftover-file checks, 6 ALTER_IMM mutant exports verifying divergence from the original, and a NULL-pointer faulting test for discarded m16 reads under /O2.
  • Assumption: user-mode inspection models the standard Windows x64 static user GDT descriptors (0x20..0x23, 0x28..0x2B, 0x30..0x33); hosts with non-standard segment layouts skip cleanly with exit code 77.

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:6593581524 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2042). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T23:33:34Z. --- ## Change Add a bounded Windows x64 AOT lifter (`tools/cpu_seg_inspect_aot.py`), source-built MASM fixture (`tests/cpu/seg_inspect.asm`, `tests/cpu/seg_inspect.def`), differential verification harness (`tests/cpu/seg_inspect_check.py`), and CTest registration (`cpu_aot_seg_inspect` in `CMakeLists.txt`) covering the `Integer` segment inspection mnemonics from `docs/coverage/x64_zen2.tsv`: - `lar r16/r32/r64, r/m16` (load access rights byte) - `lsl r16/r32/r64, r/m16` (load segment limit) - `verr r/m16` (verify segment for reading) - `verw r/m16` (verify segment for writing) Key architectural semantics modeled for Windows x64 user-mode (`CPL=3`) GDT descriptors: - `LAR`/`LSL`: on `ZF=1`, `r16` updates `[15:0]` preserving `[63:16]`, while `r32`/`r64` zero-extend the 32-bit result to 64 bits; on `ZF=0`, all 64 bits of the destination remain untouched. Only `ZF` is updated; `CF`, `PF`, `SF`, and `OF` are preserved. Architecturally undefined `LAR` bits `[19:16]` (`00FXFF00H`) are masked (`0x00F0FF00u`) only on 32/64-bit `LAR` with `ZF=1`. - `VERR`/`VERW`: sets `ZF=1` only for readable/writable user-mode GDT selectors (`TI=0`, non-null, present, `S=1`, `DPL >= max(CPL, RPL)` unless conforming code, and readable/writable type bits), leaving all other flags and GPRs untouched. - `m16` selector loads execute unconditionally via `volatile` `load_always<std::uint16_t>()` so `/O2` never eliminates faulting reads even when the result is discarded. - 32-bit `CMOVE`/`CMOVNE` zero-extends `dst[63:32]` even when the condition is false. - Rejects VEX/EVEX/XOP encodings, segment/LOCK/REP/REPNE prefixes, RIP-relative and SIB-indexed memory operands, clobbered pointer bases, nonvolatile and high-byte GPRs, non-leaf branches/calls, memory destinations for `XOR`/`OR`/`ADD`, and undefined GPR/flag reads. ## Verification Tested on commit `88767ebc` (Windows x64, MSVC 14.50, `dev` Debug and `release` `/O2`): 1. `cmake --build --preset dev --target cpu_seg_inspect_original cpu_seg_inspect_original_lifted cpu_seg_inspect_altered cpu_seg_inspect_altered_lifted` - Result: succeeded with 0 errors, 0 warnings. 2. `cmake --build --preset release --target cpu_seg_inspect_original cpu_seg_inspect_original_lifted cpu_seg_inspect_altered cpu_seg_inspect_altered_lifted` - Result: succeeded with 0 errors, 0 warnings. 3. `ctest --preset dev -R "^cpu_aot_seg_inspect$" --output-on-failure -V` - Result: `1/1 Test #648: cpu_aot_seg_inspect .............. Passed` (`unit rejections (6 raw + 21 IR), 7 PE-patched rejections, and CLI non-traceback rejections passed`; `seg_inspect passed: 2628 native, 2628 lifted, 2628 /O2 cases across 20 exports; ALTER_IMM diverged on {'verr_reg': 8, 'verw_reg': 8, 'verr_mem_disp': 8, 'verw_mem_disp': 8, 'seg_imm_select': 23, 'seg_flags_preserve': 23}`). Capability skips: none (exits `77` on non-standard segment layouts). 4. `ctest --preset release -R "^cpu_aot_seg_inspect$" --output-on-failure` - Result: `1/1 Test #648: cpu_aot_seg_inspect .............. Passed`. 5. `FORM_COVERAGE_RECORD` output (`out/build/dev/form_coverage/cpu_aot_seg_inspect.txt` and `release`): - Emits: `add, cmove, lar, lsl, mov, ret, setb, sete, seto, setp, sets, verr, verw`. 6. `.\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py` - Result: `form coverage checks passed` (exit 0). 7. `.\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py` - Result: `Self-test passed. Checked 18 Markdown files and 391 links.` (exit 0). 8. `.\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py` - Result: `Self-test passed. Checked 1404 tracked source, script and build/CI files: no bidirectional controls.` (exit 0). ## Evidence and follow-up - Fixture `tests/cpu/seg_inspect.asm` and `tests/cpu/seg_inspect.def` are original, source-built synthetic MASM test fixtures under the repository MIT license; no external binaries or game files are used. - Negative controls include 6 raw-byte excluded-encoding checks, 21 IR-level rejection checks, 7 PE-patched rejection checks, CLI non-traceback / zero-leftover-file checks, 6 `ALTER_IMM` mutant exports verifying divergence from the original, and a `NULL`-pointer faulting test for discarded `m16` reads under `/O2`. - Assumption: user-mode inspection models the standard Windows x64 static user GDT descriptors (`0x20..0x23`, `0x28..0x2B`, `0x30..0x33`); hosts with non-standard segment layouts skip cleanly with exit code `77`. ## 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.
upstream-sync closed this pull request 2026-11-25 00:05:06 +00:00
R0Y00 commented 2026-11-25 01:01:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T23:47:16Z.


I added the fixed GDT profile and reproduction command to the experiment documentation and CLI help. The Windows Release retest exercised 2,628 cases per native, lifted and /O2 variant, including selector mutations and rejection controls. I also corrected compiler-output decoding under a non-English locale. No skips in the local test.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7147794132 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2042#issuecomment-7147794132). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T23:47:16Z. --- I added the fixed GDT profile and reproduction command to the experiment documentation and CLI help. The Windows Release retest exercised 2,628 cases per native, lifted and /O2 variant, including selector mutations and rejection controls. I also corrected compiler-output decoding under a non-English locale. No skips in the local test.
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!2035
No description provided.