cpu: reject FS/GS and LOCK prefixes in the shared lowering #189

Merged
Spaghetih merged 1 commit from upstream-pr/190 into main 2026-11-21 18:13:56 +00:00
Spaghetih commented 2026-11-21 18:10:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Spaghetih on 2026-11-21T18:10:53Z.


Change

lower() in tools/ffx_version_query.py, the integer lowering shared by the ffx_* translators, bink_heap_aot and the heap_layout and version_query fixtures, emitted flat loads and stores without checking prefixes. Before this change:

Encoding Lowered statement
65 48 8b 04 25 30 00 00 00 mov rax, gs:[0x30] rax = load<uint64_t>(48ull) (reads absolute address 0x30)
64 8b 41 08 mov eax, fs:[rcx+8] rax = load<uint32_t>(rcx + 8ull) (segment base dropped)
f0 01 01 lock add [rcx], eax plain load, add and store (atomicity dropped)

lower() now raises ValueError("unsupported instruction prefix at 0x…: …") for FS/GS segment overrides and LOCK. This matches cpu_chained_aot.py and cpu_switch_aot.py, which already reject prefixed instructions. Segment overrides that have no effect in 64-bit mode (CS/DS/ES/SS) and rep ret still lower exactly as the bare instruction. An instruction replaced through host_ops bypasses the check.

Verification

Commit 68e1bac on 345d919. Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100, Python 3.14.6, iced-x86 1.21.0, pefile 2024.8.26.

ctest --preset dev --no-tests=error -R '^cpu_lower_prefixes$' -V
  • With the change: 11 FS/GS or LOCK forms rejected; 6 neutral or unprefixed forms matched the bare lowering; host override kept. Passed.
  • Negative control, same test against the main version of tools/ffx_version_query.py: AssertionError: accepted mov rax, gs:[0x30]. Failed as expected.
ctest --preset dev --no-tests=error

82 passed, 0 failed, 3 capability skips: bvh_wave64_hardware, bvh_wave64_warp (lane range excludes 64) and wave_shuffle_warp. Hardware tests ran on an NVIDIA GeForce RTX 4070 Ti, driver 32.0.16.1088. The translations regenerated at build time (cpu_heap_layout_lifted, cpu_version_query_lifted, cpu_create_lifted, cpu_bink_heap_lifted) still pass.

Local package check. With a local copy of the loader and upscaler whose SHA-256 values match the LOADER and UPSCALER profiles:

  • ffx_version_query.py, ffx_device_init.py and ffx_create_context.py generated byte-identical C++ before and after the change (171, 1290 and 109 lines). None of the selected package intervals carries an FS/GS or LOCK prefix.
  • In a separate build configured with -DFFX_UPSCALER_IMAGE, which compiles ffx_device_slice.cpp and ffx_loop_loader.cpp into offscreen_check, ctest --test-dir out/build/ffx-device --no-tests=error -R '^ffx_' passed all 6 ffx_* tests. The full suite in that configuration gave the same 82 passes and 3 skips.

Evidence and follow-up

tests/cpu/lower_prefixes_check.py uses synthetic x64 byte sequences only, written for this repository under its MIT license. It decodes them with iced_x86 and calls lower() directly, so it needs no native fixture and no game files. The public test verifies the rejection and the unchanged lowering of neutral prefixes. The local package check verifies only that the existing package translations are unaffected.

docs/experiments.md describes this behavior after the cpu_aot_version_query paragraph and gives the reproduction command.

Follow-up I can open separately: lower() also ignores the 67 address-size prefix. lea rax, [ecx+edx] lowers to the 64-bit sum rcx + rdx, while the hardware zero-extends the 32-bit sum. With ECX = 0xFFFFFFFF and EDX = 1, the hardware gives 0 and the lowering gives 0x100000000. That fix changes semantics rather than rejecting input, so I would pair it with a native MASM fixture.

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 (Claude Code). Every result above was run locally on the configuration listed.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6532569233 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/190). Originally posted by [Spaghetih](https://github.com/Spaghetih) on 2026-11-21T18:10:53Z. --- ## Change `lower()` in `tools/ffx_version_query.py`, the integer lowering shared by the `ffx_*` translators, `bink_heap_aot` and the `heap_layout` and `version_query` fixtures, emitted flat loads and stores without checking prefixes. Before this change: | Encoding | Lowered statement | |---|---| | `65 48 8b 04 25 30 00 00 00` `mov rax, gs:[0x30]` | `rax = load<uint64_t>(48ull)` (reads absolute address 0x30) | | `64 8b 41 08` `mov eax, fs:[rcx+8]` | `rax = load<uint32_t>(rcx + 8ull)` (segment base dropped) | | `f0 01 01` `lock add [rcx], eax` | plain load, add and store (atomicity dropped) | `lower()` now raises `ValueError("unsupported instruction prefix at 0x…: …")` for FS/GS segment overrides and LOCK. This matches `cpu_chained_aot.py` and `cpu_switch_aot.py`, which already reject prefixed instructions. Segment overrides that have no effect in 64-bit mode (CS/DS/ES/SS) and `rep ret` still lower exactly as the bare instruction. An instruction replaced through `host_ops` bypasses the check. ## Verification Commit `68e1bac` on `345d919`. Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100, Python 3.14.6, `iced-x86` 1.21.0, `pefile` 2024.8.26. ```powershell ctest --preset dev --no-tests=error -R '^cpu_lower_prefixes$' -V ``` - With the change: `11 FS/GS or LOCK forms rejected; 6 neutral or unprefixed forms matched the bare lowering; host override kept`. Passed. - Negative control, same test against the `main` version of `tools/ffx_version_query.py`: `AssertionError: accepted mov rax, gs:[0x30]`. Failed as expected. ```powershell ctest --preset dev --no-tests=error ``` 82 passed, 0 failed, 3 capability skips: `bvh_wave64_hardware`, `bvh_wave64_warp` (lane range excludes 64) and `wave_shuffle_warp`. Hardware tests ran on an NVIDIA GeForce RTX 4070 Ti, driver 32.0.16.1088. The translations regenerated at build time (`cpu_heap_layout_lifted`, `cpu_version_query_lifted`, `cpu_create_lifted`, `cpu_bink_heap_lifted`) still pass. **Local package check.** With a local copy of the loader and upscaler whose SHA-256 values match the `LOADER` and `UPSCALER` profiles: - `ffx_version_query.py`, `ffx_device_init.py` and `ffx_create_context.py` generated byte-identical C++ before and after the change (171, 1290 and 109 lines). None of the selected package intervals carries an FS/GS or LOCK prefix. - In a separate build configured with `-DFFX_UPSCALER_IMAGE`, which compiles `ffx_device_slice.cpp` and `ffx_loop_loader.cpp` into `offscreen_check`, `ctest --test-dir out/build/ffx-device --no-tests=error -R '^ffx_'` passed all 6 `ffx_*` tests. The full suite in that configuration gave the same 82 passes and 3 skips. ## Evidence and follow-up `tests/cpu/lower_prefixes_check.py` uses synthetic x64 byte sequences only, written for this repository under its MIT license. It decodes them with `iced_x86` and calls `lower()` directly, so it needs no native fixture and no game files. The public test verifies the rejection and the unchanged lowering of neutral prefixes. The local package check verifies only that the existing package translations are unaffected. `docs/experiments.md` describes this behavior after the `cpu_aot_version_query` paragraph and gives the reproduction command. Follow-up I can open separately: `lower()` also ignores the `67` address-size prefix. `lea rax, [ecx+edx]` lowers to the 64-bit sum `rcx + rdx`, while the hardware zero-extends the 32-bit sum. With ECX = 0xFFFFFFFF and EDX = 1, the hardware gives 0 and the lowering gives 0x100000000. That fix changes semantics rather than rejecting input, so I would pair it with a native MASM fixture. ## 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 (Claude Code). Every result above was run locally on the configuration listed._
upstream-sync closed this pull request 2026-11-24 06:41:45 +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!189
No description provided.