cpu: wrap 32-bit effective addresses in the jump-table translator #243

Merged
Spaghetih merged 2 commits from upstream-pr/246 into main 2026-11-21 19:56:01 +00:00
Spaghetih commented 2026-11-21 19:12:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Spaghetih on 2026-11-21T19:12:44Z.


## Change

Independent problem: the same 32-bit effective-address mistranslation that #205 fixed in lower() also exists in the jump-table translator, tools/cpu_switch_aot.py (Lifter.address). That translator builds addresses itself. With a 67h prefix it summed the 32-bit base and the zero-extended 32-bit displacement in 64 bits, without wrapping:

Native fixture Input RCX Hardware Before this change
lea rax, [ecx+1] 0xffffffff 0x0 0x100000000
lea rax, [ecx-8] 0x10 0x8 0x100000008
lea rax, [ecx-8] 0xffffffff 0xfffffff7 0x1fffffff7

Lifter.address now wraps the sum at 32 bits and zero-extends it when the base is a 32-bit register. A 32-bit index was already rejected, because the index must be a 64-bit GPR. EIP- and ESP-based operands are now rejected alongside RIP and RSP. Previously they reached reg(), which has no binding for them.

Shared interface: Lifter.address in cpu_switch_aot.py. #173 also edits this file (SETcc/CMOVcc), but not address(), and it edits switch.asm/switch_check.py. This PR adds a separate fixture so the two do not touch the same lines. Neither depends on the other.

The second commit adds switch_addr32 → cpu_aot_switch_addr32 to CPU_FIXTURES in tools/form_coverage.py. Without it the coverage report lists the new fixture as unclassified. #252 classifies the fixtures already on main at another position in the table. All three branches merge cleanly in sequence, and form_coverage_check passes on the combination.

Verification

Commit on f85723e. 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_aot_switch_addr32$' -V
  • With this change: 120 native and translated 32-bit address cases matched the reference, then the EIP/ESP rejection check. Passed.
  • Negative control, the same test with tools/cpu_switch_aot.py from f85723e: AssertionError: lifted lea_wrap(0xffffffff) = 0x100000000, expected 0x0. Failed as expected.
  • translate() on cpu_switch_original.dll and cpu_switch_swapped.dll (select_rva,select_rel,run_ops,shift_narrow) produced byte-identical C++ before and after this change (189 lines each). The existing jump-table output is unaffected.
ctest --preset dev --no-tests=error
./out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py

ctest gave 114 passed, 0 failed and 3 capability skips: bvh_wave64_hardware, bvh_wave64_warp (lane range excludes 64) and wave_shuffle_warp. The hardware tests ran on an NVIDIA GeForce RTX 4070 Ti, driver 32.0.16.1088. form_coverage_check printed form coverage checks passed.

Evidence and follow-up

tests/cpu/switch_addr32.asm, .def and switch_addr32_check.py are original source written for this repository under its MIT license. They need no game files. The native image runs against the same Python reference as the translation, so the expected values come from the hardware. The inputs combine 32-bit wraparound with upper-half noise (0, 0xdeadbeef00000000, 0xffffffff00000000). The load and store use a page committed below 4 GiB, and each store must leave the rest of the page unchanged. docs/experiments.md describes the behavior after the jump-table section and gives the command.

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:6533238948 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/246). Originally posted by [Spaghetih](https://github.com/Spaghetih) on 2026-11-21T19:12:44Z. --- ## Change Independent problem: the same 32-bit effective-address mistranslation that #205 fixed in `lower()` also exists in the jump-table translator, `tools/cpu_switch_aot.py` (`Lifter.address`). That translator builds addresses itself. With a 67h prefix it summed the 32-bit base and the zero-extended 32-bit displacement in 64 bits, without wrapping: | Native fixture | Input RCX | Hardware | Before this change | |---|---|---|---| | `lea rax, [ecx+1]` | `0xffffffff` | `0x0` | `0x100000000` | | `lea rax, [ecx-8]` | `0x10` | `0x8` | `0x100000008` | | `lea rax, [ecx-8]` | `0xffffffff` | `0xfffffff7` | `0x1fffffff7` | `Lifter.address` now wraps the sum at 32 bits and zero-extends it when the base is a 32-bit register. A 32-bit index was already rejected, because the index must be a 64-bit GPR. EIP- and ESP-based operands are now rejected alongside RIP and RSP. Previously they reached `reg()`, which has no binding for them. Shared interface: `Lifter.address` in `cpu_switch_aot.py`. #173 also edits this file (SETcc/CMOVcc), but not `address()`, and it edits `switch.asm`/`switch_check.py`. This PR adds a separate fixture so the two do not touch the same lines. Neither depends on the other. The second commit adds `switch_addr32` → `cpu_aot_switch_addr32` to `CPU_FIXTURES` in `tools/form_coverage.py`. Without it the coverage report lists the new fixture as unclassified. #252 classifies the fixtures already on `main` at another position in the table. All three branches merge cleanly in sequence, and `form_coverage_check` passes on the combination. ## Verification Commit on `f85723e`. 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_aot_switch_addr32$' -V ``` - With this change: `120 native and translated 32-bit address cases matched the reference`, then the EIP/ESP rejection check. Passed. - Negative control, the same test with `tools/cpu_switch_aot.py` from `f85723e`: `AssertionError: lifted lea_wrap(0xffffffff) = 0x100000000, expected 0x0`. Failed as expected. - `translate()` on `cpu_switch_original.dll` and `cpu_switch_swapped.dll` (`select_rva,select_rel,run_ops,shift_narrow`) produced byte-identical C++ before and after this change (189 lines each). The existing jump-table output is unaffected. ```powershell ctest --preset dev --no-tests=error ./out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py ``` `ctest` gave 114 passed, 0 failed and 3 capability skips: `bvh_wave64_hardware`, `bvh_wave64_warp` (lane range excludes 64) and `wave_shuffle_warp`. The hardware tests ran on an NVIDIA GeForce RTX 4070 Ti, driver 32.0.16.1088. `form_coverage_check` printed `form coverage checks passed`. ## Evidence and follow-up `tests/cpu/switch_addr32.asm`, `.def` and `switch_addr32_check.py` are original source written for this repository under its MIT license. They need no game files. The native image runs against the same Python reference as the translation, so the expected values come from the hardware. The inputs combine 32-bit wraparound with upper-half noise (`0`, `0xdeadbeef00000000`, `0xffffffff00000000`). The load and store use a page committed below 4 GiB, and each store must leave the rest of the page unchanged. `docs/experiments.md` describes the behavior after the jump-table section and gives the command. ## 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:42:00 +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!243
No description provided.