x64_recomp: handle 0x67 address-size override in memory and moffs operands (X64-RECOMP-ADDRSIZE-67) #1392

Open
github-bot wants to merge 1 commit from github-bot/gh-1392 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-24 23:11 UTC · original
IlyassRS3:fix/x64-recomp-addrsize-67 → main · +284 −27 · 2 files · 1 commits · open

Item ID: X64-RECOMP-ADDRSIZE-67

Summary

Fixes handling of the 0x67 address-size override prefix (32-bit effective addressing in 64-bit mode) in tools/x64_recomp.py:

  • moffs (A0..A3, token Q) in _finish: reads a 4-byte unsigned moffs32 when ins.addrsize is set instead of always reading 8 bytes (which previously consumed subsequent instruction bytes or raised DecodeError("truncated imm")).
  • Memory operand metadata & EIP-relative addressing in _finish and _finish_group: tags memory operands with addrsize = 4 when ins.addrsize is set, and truncates RIP-relative effective addresses to 32 bits ((ins.addr + ins.size + disp) & 0xFFFFFFFF) per Intel SDM Vol. 2A §2.2.1.6.
  • 32-bit effective address codegen in _mem_addr: emits sf=0 (W-register) ARM64 arithmetic/moves/shifts (MOV, ADD, SUB, ADD (shifted reg), LSL) and masks immediate/absolute/disp32-only addresses to 32 bits when m.get("addrsize") == 4, wrapping modulo 2^32 and zero-extending to 64 bits (including disp == 0 with base == rd, [disp32] zero-extension vs 64-bit sign-extension, and large displacements >= 4096 / <= -4096).

Tests & Verification

  • AI-assisted: yes
  • Host / hardware: macOS (Apple M5, arm64)
  • Standalone checks on this branch (upstream/main 28a32e190):
    • python3 -m compileall -q make_game.py tools tests — OK
    • python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_addrsize67.py — OK
    • python3 -I tests/test_x64_recomp_addrsize67.py -v — 3 static decode/codegen tests OK (3 exec tests skip cleanly without #1174 runner)
    • python3 -I tests/test_x64_recomp.py — 6 static OK (26 exec skipped)
    • python3 -I tests/test_text_write_line_endings.py — 3/3 OK
  • Live Apple M5 mutation proof (tests/test_x64_recomp_addrsize67.py on unpatched upstream/main + #1174): 19 failures (FAILED (failures=19)).
  • Native Apple M5 execution (20-way scratch merge with #1174 + all 18 other open x64_recomp PR branches):
    • 0 merge conflicts across all 20 branches
    • tests/test_x64_recomp_addrsize67.py: 6/6 OK (0 skipped) on native Apple M5 arm64
    • All 19 tests/test_x64_recomp_*.py suites combined: 138/138 OK (0 skipped)
**@⁠IlyassRS3** on GitHub · 2026-11-24 23:11 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1392) `IlyassRS3:fix/x64-recomp-addrsize-67` → `main` · +284 −27 · 2 files · 1 commits · open Item ID: `X64-RECOMP-ADDRSIZE-67` ## Summary Fixes handling of the `0x67` address-size override prefix (32-bit effective addressing in 64-bit mode) in `tools/x64_recomp.py`: - **`moffs` (`A0`..`A3`, token `Q`) in `_finish`**: reads a 4-byte unsigned `moffs32` when `ins.addrsize` is set instead of always reading 8 bytes (which previously consumed subsequent instruction bytes or raised `DecodeError("truncated imm")`). - **Memory operand metadata & EIP-relative addressing in `_finish` and `_finish_group`**: tags memory operands with `addrsize = 4` when `ins.addrsize` is set, and truncates RIP-relative effective addresses to 32 bits (`(ins.addr + ins.size + disp) & 0xFFFFFFFF`) per Intel SDM Vol. 2A §2.2.1.6. - **32-bit effective address codegen in `_mem_addr`**: emits `sf=0` (`W`-register) ARM64 arithmetic/moves/shifts (`MOV`, `ADD`, `SUB`, `ADD (shifted reg)`, `LSL`) and masks immediate/absolute/disp32-only addresses to 32 bits when `m.get("addrsize") == 4`, wrapping modulo `2^32` and zero-extending to 64 bits (including `disp == 0` with `base == rd`, `[disp32]` zero-extension vs 64-bit sign-extension, and large displacements `>= 4096` / `<= -4096`). ## Tests & Verification - **AI-assisted**: yes - **Host / hardware**: macOS (Apple M5, arm64) - Standalone checks on this branch (`upstream/main` `28a32e190`): - `python3 -m compileall -q make_game.py tools tests` — OK - `python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp_addrsize67.py` — OK - `python3 -I tests/test_x64_recomp_addrsize67.py -v` — 3 static decode/codegen tests OK (3 exec tests skip cleanly without `#1174` runner) - `python3 -I tests/test_x64_recomp.py` — 6 static OK (26 exec skipped) - `python3 -I tests/test_text_write_line_endings.py` — 3/3 OK - **Live Apple M5 mutation proof** (`tests/test_x64_recomp_addrsize67.py` on unpatched `upstream/main` + `#1174`): **19 failures** (`FAILED (failures=19)`). - **Native Apple M5 execution (20-way scratch merge with `#1174` + all 18 other open `x64_recomp` PR branches)**: - 0 merge conflicts across all 20 branches - `tests/test_x64_recomp_addrsize67.py`: **6/6 OK (0 skipped)** on native Apple M5 arm64 - All 19 `tests/test_x64_recomp_*.py` suites combined: **138/138 OK (0 skipped)**
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:49:47 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 23:11 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-24 23:11 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1392#pullrequestreview-6048886013) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Author
Owner

@⁠kevincommits on GitHub · 2026-11-26 04:29 UTC · original

Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged main at a73a57e13 into the branch locally (913 commits behind, no conflicts). Head tested: 442ef0a66.

Against a reference model this branch is right in all 432 checks. main gets 167 wrong, every one a 67-prefixed form; the un-prefixed half is identical on both, so the _mem_addr rewrite changes nothing for ordinary 64-bit addressing.

  • Test file. As shipped under QEMU, python -I tests/test_x64_recomp_addrsize67.py is 6 run, 16 errors, from the wiring bug in #1409 (xr.run_kernel where xr.run_result is needed; here the raw bytes reach hi & 0xFFFFFFFF). With RUNNER swapped for xr.run_result in memory (no file changed): 6/6.

  • Beyond the file, generated cases under QEMU against a small Python model. Each address form is run twice, as lea rax, [...] with and without the 67 prefix, and the full 64-bit rax is compared (the effective address, or its low 32 bits zero-extended):

    • [rcx + disp] with no displacement, disp8 and disp32 encodings, 14 displacements from -0x80000000 to 0x7fffffff, four base values including ones that wrap at 32 bits and ones with junk above bit 31;
    • [rax] and [rax+1] where the destination is also the base;
    • [rcx + rdx*s], [rcx + rdx*s + disp8], [rcx + rdx*s + disp32] for all four scales;
    • [rdx*s + disp32] for all four scales, and [disp32] alone;
    • two 67-prefixed memory reads (mov eax, [ecx], mov eax, [ecx+4]) with junk in the upper half of rcx.
    tool checks wrong what is wrong
    this branch (measured with #2143 merged in locally) 432 0
    main 432 167 all 67 forms: the prefix is ignored, so the address is computed at 64 bits (67 lea rax,[rcx+1] with rcx=0xffffffff gives 0x100000000, want 0); the two memory reads time out
  • Counts. tests/test_x64_recomp.py 44/44 and tests/test_recomp_census.py 11/11, the same as main. Full standalone suite: 514 run, 512 pass; the 2 that fail are main's test_mod_pack_output_alias and the new file, for the wiring reason.

Not tested: moffs forms (67 a0–a3) and EIP-relative addressing beyond the file's own cases, 67 on stores and on string instructions, and segment-prefixed operands. No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-26 04:29 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1392#issuecomment-7182642634) Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged `main` at `a73a57e13` into the branch locally (913 commits behind, no conflicts). Head tested: `442ef0a66`. **Against a reference model this branch is right in all 432 checks. `main` gets 167 wrong, every one a `67`-prefixed form; the un-prefixed half is identical on both, so the `_mem_addr` rewrite changes nothing for ordinary 64-bit addressing.** - **Test file.** As shipped under QEMU, `python -I tests/test_x64_recomp_addrsize67.py` is 6 run, 16 errors, from the wiring bug in #1409 (`xr.run_kernel` where `xr.run_result` is needed; here the raw bytes reach `hi & 0xFFFFFFFF`). With `RUNNER` swapped for `xr.run_result` in memory (no file changed): **6/6**. - Beyond the file, generated cases under QEMU against a small Python model. Each address form is run twice, as `lea rax, [...]` with and without the `67` prefix, and the full 64-bit `rax` is compared (the effective address, or its low 32 bits zero-extended): - `[rcx + disp]` with no displacement, `disp8` and `disp32` encodings, 14 displacements from `-0x80000000` to `0x7fffffff`, four base values including ones that wrap at 32 bits and ones with junk above bit 31; - `[rax]` and `[rax+1]` where the destination is also the base; - `[rcx + rdx*s]`, `[rcx + rdx*s + disp8]`, `[rcx + rdx*s + disp32]` for all four scales; - `[rdx*s + disp32]` for all four scales, and `[disp32]` alone; - two `67`-prefixed memory reads (`mov eax, [ecx]`, `mov eax, [ecx+4]`) with junk in the upper half of `rcx`. | tool | checks | wrong | what is wrong | |---|---|---|---| | this branch (measured with #2143 merged in locally) | 432 | 0 | | | `main` | 432 | 167 | all `67` forms: the prefix is ignored, so the address is computed at 64 bits (`67 lea rax,[rcx+1]` with `rcx=0xffffffff` gives `0x100000000`, want 0); the two memory reads time out | - **Counts.** `tests/test_x64_recomp.py` 44/44 and `tests/test_recomp_census.py` 11/11, the same as `main`. Full standalone suite: 514 run, 512 pass; the 2 that fail are `main`'s `test_mod_pack_output_alias` and the new file, for the wiring reason. Not tested: `moffs` forms (`67 a0`–`a3`) and EIP-relative addressing beyond the file's own cases, `67` on stores and on string instructions, and segment-prefixed operands. No game files used. AI-assisted.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1392/head:github-bot/gh-1392'
git switch 'github-bot/gh-1392'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1392'
git switch 'github-bot/gh-1392'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1392'
git switch 'github-bot/gh-1392'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1392'
git switch 'main'
git merge --squash 'github-bot/gh-1392'
git switch 'main'
git merge --ff-only 'github-bot/gh-1392'
git switch 'main'
git merge 'github-bot/gh-1392'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.