cpu: truncate 32-bit effective addresses in ffx_version_query #202

Merged
Vonsek merged 1 commit from upstream-pr/205 into main 2026-11-21 18:42:30 +00:00
Vonsek commented 2026-11-21 18:22:07 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T18:22:07Z.


Change

Truncate 32-bit effective addresses (x64 67h address-size override, where the memory base or index register is 32-bit) to uint32_t in tools/ffx_version_query.py (lower / address).

Previously, address() mapped 32-bit base and index registers (ECX, EDX, etc.) to their full 64-bit host variables without truncating the resulting address to 32 bits. Whenever the upper 32 bits of the base/index register were nonzero or the 32-bit sum wrapped around (for example lea rax, [ecx + edx*2 + 4] or lea r8, [ecx + 1]), the lifted code produced a 64-bit sum instead of the zero-extended 32-bit x64 effective address.

address() now checks RegisterExt.size(...) == 4 on memory_base and memory_index and wraps the expression in static_cast<uint32_t>(...).

Adds a source-built MASM function addr32_probe to tests/cpu/version_query.asm (exported in tests/cpu/version_query.def), lifts it in tests/cpu/version_query_fixture.py, and compares native Windows execution against the lifted DLL across four inputs including nonzero upper 32-bit halves and 32-bit wraparound.

Verification

Tested on Windows 11 x64, VS 2022 Build Tools 17.14 (MSVC 19.44.35230.0, ML64 14.44.35230.0), Windows SDK 10.0.26100, Python 3.12.10 (pefile==2024.8.26, iced-x86==1.21.0), at commit cccd89c:

  • Before the fix in tools/ffx_version_query.py, native and lifted outputs diverge on the first case:
    ctest --preset dev --no-tests=error -R '^cpu_aot_version_query$' -V
    AssertionError: addr32_probe(0x12345678ffffffff, 0xdeadbeef00000002): native=0x7 lifted=0xddbb822e00000007
    1/1 Test #66: cpu_aot_version_query ............***Failed    0.15 sec
    
  • After the fix:
    cmake --build --preset dev --target cpu_version_query cpu_version_query_lifted cpu_create_lifted cpu_heap_layout_lifted
    ctest --preset dev --no-tests=error -R '^cpu_aot_(version_query|context_lifecycle|heap_layout)$' -V
    1/3 Test #65: cpu_aot_heap_layout ..............   Passed    0.16 sec
    2/3 Test #66: cpu_aot_version_query ............   Passed    0.16 sec
    3/3 Test #68: cpu_aot_context_lifecycle ........   Passed    0.09 sec
    100% tests passed, 0 tests failed out of 3
    
    (cpu_aot_version_query output includes addr32_probe: 4 native/lifted cases matched)

Evidence and follow-up

The regression fixture is built from original MASM source in tests/cpu/version_query.asm and verified differentially against native x64 execution on Windows.

Because address() is shared by LEA and memory operands in ffx_version_query.lower, this fixes 32-bit effective address computation across all lowered memory and LEA instructions.

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.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6532684091 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/205). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T18:22:07Z. --- ## Change Truncate 32-bit effective addresses (x64 `67h` address-size override, where the memory base or index register is 32-bit) to `uint32_t` in `tools/ffx_version_query.py` (`lower` / `address`). Previously, `address()` mapped 32-bit base and index registers (`ECX`, `EDX`, etc.) to their full 64-bit host variables without truncating the resulting address to 32 bits. Whenever the upper 32 bits of the base/index register were nonzero or the 32-bit sum wrapped around (for example `lea rax, [ecx + edx*2 + 4]` or `lea r8, [ecx + 1]`), the lifted code produced a 64-bit sum instead of the zero-extended 32-bit x64 effective address. `address()` now checks `RegisterExt.size(...) == 4` on `memory_base` and `memory_index` and wraps the expression in `static_cast<uint32_t>(...)`. Adds a source-built MASM function `addr32_probe` to `tests/cpu/version_query.asm` (exported in `tests/cpu/version_query.def`), lifts it in `tests/cpu/version_query_fixture.py`, and compares native Windows execution against the lifted DLL across four inputs including nonzero upper 32-bit halves and 32-bit wraparound. ## Verification Tested on Windows 11 x64, VS 2022 Build Tools 17.14 (MSVC 19.44.35230.0, ML64 14.44.35230.0), Windows SDK 10.0.26100, Python 3.12.10 (`pefile==2024.8.26`, `iced-x86==1.21.0`), at commit `cccd89c`: - Before the fix in `tools/ffx_version_query.py`, native and lifted outputs diverge on the first case: ```text ctest --preset dev --no-tests=error -R '^cpu_aot_version_query$' -V AssertionError: addr32_probe(0x12345678ffffffff, 0xdeadbeef00000002): native=0x7 lifted=0xddbb822e00000007 1/1 Test #66: cpu_aot_version_query ............***Failed 0.15 sec ``` - After the fix: ```text cmake --build --preset dev --target cpu_version_query cpu_version_query_lifted cpu_create_lifted cpu_heap_layout_lifted ctest --preset dev --no-tests=error -R '^cpu_aot_(version_query|context_lifecycle|heap_layout)$' -V 1/3 Test #65: cpu_aot_heap_layout .............. Passed 0.16 sec 2/3 Test #66: cpu_aot_version_query ............ Passed 0.16 sec 3/3 Test #68: cpu_aot_context_lifecycle ........ Passed 0.09 sec 100% tests passed, 0 tests failed out of 3 ``` (`cpu_aot_version_query` output includes `addr32_probe: 4 native/lifted cases matched`) ## Evidence and follow-up The regression fixture is built from original MASM source in `tests/cpu/version_query.asm` and verified differentially against native x64 execution on Windows. Because `address()` is shared by `LEA` and memory operands in `ffx_version_query.lower`, this fixes 32-bit effective address computation across all lowered memory and `LEA` instructions. ## 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.
upstream-sync closed this pull request 2026-11-24 06:41:49 +00:00
chatgpt-codex-connector[bot commented 2026-11-24 06:47:49 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-21T18:24:25Z.


Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T18:26:17.957710Z cccd89c Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067230991 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/205#issuecomment-7067230991). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T18:24:25Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"cccd89c3b03ffae70990a65278d7adf17983fea5","has_any_findings":false,"has_unresolved_findings":false,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-21T18:26:17.957710Z">2026-11-21T18:26:17.957710Z</relative-time> | `cccd89c` | Draft marked ready | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@codex review" or "@codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
Spaghetih commented 2026-11-24 06:47:51 +00:00 (Migrated from github.com)

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


Thanks for picking this up. I had flagged the 67h case as a follow-up in #190 and had a local branch for it, so I ran my fixture against your head instead of opening a duplicate.

Your head cccd89c with an extra fixture applied locally (Windows 11 26200.9974, MSVC 14.44.35207, Python 3.14.6, iced-x86 1.21.0):

The extra fixture is a separate MASM image with four 67h-prefixed functions, compared native vs. translated against a 32-bit reference:

Function Encoding Cases
lea_sum32 67 48 8d 04 11 lea rax,[ecx+edx] 36
lea_scaled32 67 48 8d 44 d1 f8 lea rax,[ecx+edx*8-8] 36
load32 67 8b 04 11 mov eax,[ecx+edx] 21
store32 67 44 89 04 11 mov [ecx+edx],r8d 21

The inputs combine 32-bit wraparound with upper-half noise (0, 0xdeadbeef00000000, 0xffffffff00000000) on both registers. The load and store cases use a page committed below 4 GiB, including a base + 0xffffff14 index that wraps back into the page. Each store also checks that no other word in the page changed. The -8 case matters because iced reports a 32-bit-mode displacement as 0xfffffff8, so without the truncation even a zero upper half is off by 4 GiB.

Result on your head: 72 lea and 42 load/store cases matched the 32-bit reference on the native and translated images. cpu_aot_version_query (including addr32_probe) also passed. On main the same fixture fails at the first case: lifted lea_sum32(0xffffffff, 0x1) = 0x100000000, expected 0x0.

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 generate byte-identical C++ on your head and on main. None of the selected package intervals uses 32-bit addressing.

One remaining edge, low priority. A 67h EIP-relative operand (67 8b 05 10 00 00 00, mov eax,[eip+0x10]) takes the is_ip_rel_memory_operand branch before the new truncation. It ends in a bare KeyError from globals_, or binds to a global if one happens to sit at the truncated RVA, while the hardware uses the low 32 bits of the runtime RIP. Compilers do not emit this form. An explicit ValueError would make it a clear rejection.

Once this merges, I can open a small follow-up with the MASM load/store fixture and the EIP-relative rejection, unless you would rather fold either into this PR.

AI-assisted (Claude Code). The results above were run locally on the configuration listed.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067501098 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/205#issuecomment-7067501098). Originally posted by [Spaghetih](https://github.com/Spaghetih) on 2026-11-21T18:46:21Z. --- Thanks for picking this up. I had flagged the 67h case as a follow-up in #190 and had a local branch for it, so I ran my fixture against your head instead of opening a duplicate. **Your head `cccd89c` with an extra fixture applied locally** (Windows 11 26200.9974, MSVC 14.44.35207, Python 3.14.6, `iced-x86` 1.21.0): The extra fixture is a separate MASM image with four 67h-prefixed functions, compared native vs. translated against a 32-bit reference: | Function | Encoding | Cases | |---|---|---| | `lea_sum32` | `67 48 8d 04 11` `lea rax,[ecx+edx]` | 36 | | `lea_scaled32` | `67 48 8d 44 d1 f8` `lea rax,[ecx+edx*8-8]` | 36 | | `load32` | `67 8b 04 11` `mov eax,[ecx+edx]` | 21 | | `store32` | `67 44 89 04 11` `mov [ecx+edx],r8d` | 21 | The inputs combine 32-bit wraparound with upper-half noise (`0`, `0xdeadbeef00000000`, `0xffffffff00000000`) on both registers. The load and store cases use a page committed below 4 GiB, including a base + `0xffffff14` index that wraps back into the page. Each store also checks that no other word in the page changed. The `-8` case matters because iced reports a 32-bit-mode displacement as `0xfffffff8`, so without the truncation even a zero upper half is off by 4 GiB. Result on your head: `72 lea and 42 load/store cases matched the 32-bit reference on the native and translated images`. `cpu_aot_version_query` (including `addr32_probe`) also passed. On `main` the same fixture fails at the first case: `lifted lea_sum32(0xffffffff, 0x1) = 0x100000000, expected 0x0`. **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` generate byte-identical C++ on your head and on `main`. None of the selected package intervals uses 32-bit addressing. **One remaining edge, low priority.** A 67h EIP-relative operand (`67 8b 05 10 00 00 00`, `mov eax,[eip+0x10]`) takes the `is_ip_rel_memory_operand` branch before the new truncation. It ends in a bare `KeyError` from `globals_`, or binds to a global if one happens to sit at the truncated RVA, while the hardware uses the low 32 bits of the runtime RIP. Compilers do not emit this form. An explicit `ValueError` would make it a clear rejection. Once this merges, I can open a small follow-up with the MASM load/store fixture and the EIP-relative rejection, unless you would rather fold either into this PR. _AI-assisted (Claude Code). The results above were run locally on the configuration listed._
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!202
No description provided.