x64_recomp: in/out immediate ports need their imm8 byte (X64-RECOMP-IN-IMM8) #1488

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

@⁠kett1m on GitHub · 2026-11-25 01:36 UTC · original
kett1m:x64-recomp-in-imm8 → main · +28 −1 · 3 files · 1 commits · open

The primary table in tools/x64_recomp.py gives E4, E5, E6 and E7 the operand shape
sys, so the decoder consumed only the opcode and the instruction came out one byte long. These
are the immediate-port forms of in and out:

  • E4 ib = in al, imm8
  • E5 ib = in eax, imm8
  • E6 ib = out imm8, al
  • E7 ib = out imm8, eax

Each takes one immediate byte, so the instruction is two bytes; the walk read the port number as
the next opcode and the rest of the function came out at the wrong offset. The EC-EF DX-port
forms take no immediate and are unchanged.

Measurement

The image is game_scarlett_master.exe, SHA-256
e95020b2fa4a721db3df59fdecf75621730ac559d8835252f6c3b658d5a08993. REA measured the section
ranges with gnu.objdump section_headers, evidence
ev_e29cbdffd0dc7c46889b2a1f955011ce9f2795017233c97107ded73f9186dd90: .text VMA
0x140001000, file 0x400, size 0x5f3f966.

A per-instruction differential against GNU objdump over .text: objdump's linear walk and the
decoder's walk read the same bytes and the instruction lengths are compared (26,674,180 objdump
instructions). On main (b4b7756) it reports 52,638 mismatching instructions; the form is used
twice, at RVA 0x55f2ed4 (in $0x3d,%al) and RVA 0x564652c (in $0x73,%al), each one byte
short of objdump's two. With this change the count is 52,636, both of those are gone, and no new
mismatch signature appears. Both sites sit inside .pdata functions (RVAs 0x55f2ed0 and
0x5646250). out with an immediate port does not occur anywhere in .text, so it is covered by
the unit test rather than by a retail count.

Test

tests/test_x64_recomp.py::DecodeTests::test_in_out_immediate_port_consumes_imm8: E4 3d, E5 73,
E6 40 and E7 40 are two bytes with the operand shape in al, imm8 and its three siblings, and
EC/ED/EE/EF stay one byte. Against the unfixed decoder the first case decodes to size 1.

python -I tests/test_x64_recomp.py: 33 tests OK, 26 skipped (this host has no
qemu-system-aarch64). python -I tests/test_recomp_census.py: 11 tests OK.
python3 -m compileall -q make_game.py tools tests exits 0. python -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp.py docs/mobile.md exits
0 with no output.

The per-function .pdata walk is noisy for this change, because the other length defects still on
main leave most functions misaligned, so it is not quoted as an impact figure.

docs/mobile.md records the measurement, the two offsets and the evidence ID. No game files, no
GPU, nothing online. Tested on Linux (openSUSE Tumbleweed, Python 3.13.18, binutils 2.47), 12
cores.

AI-assisted (DeepSeek V4.1 Flash).

**@⁠kett1m** on GitHub · 2026-11-25 01:36 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1488) `kett1m:x64-recomp-in-imm8` → `main` · +28 −1 · 3 files · 1 commits · open The primary table in `tools/x64_recomp.py` gives `E4`, `E5`, `E6` and `E7` the operand shape `sys`, so the decoder consumed only the opcode and the instruction came out one byte long. These are the immediate-port forms of `in` and `out`: - `E4 ib` = `in al, imm8` - `E5 ib` = `in eax, imm8` - `E6 ib` = `out imm8, al` - `E7 ib` = `out imm8, eax` Each takes one immediate byte, so the instruction is two bytes; the walk read the port number as the next opcode and the rest of the function came out at the wrong offset. The `EC`-`EF` DX-port forms take no immediate and are unchanged. ## Measurement The image is `game_scarlett_master.exe`, SHA-256 `e95020b2fa4a721db3df59fdecf75621730ac559d8835252f6c3b658d5a08993`. REA measured the section ranges with `gnu.objdump` `section_headers`, evidence `ev_e29cbdffd0dc7c46889b2a1f955011ce9f2795017233c97107ded73f9186dd90`: `.text` VMA `0x140001000`, file `0x400`, size `0x5f3f966`. A per-instruction differential against GNU objdump over `.text`: objdump's linear walk and the decoder's walk read the same bytes and the instruction lengths are compared (26,674,180 objdump instructions). On `main` (`b4b7756`) it reports 52,638 mismatching instructions; the form is used twice, at RVA `0x55f2ed4` (`in $0x3d,%al`) and RVA `0x564652c` (`in $0x73,%al`), each one byte short of objdump's two. With this change the count is 52,636, both of those are gone, and no new mismatch signature appears. Both sites sit inside `.pdata` functions (RVAs `0x55f2ed0` and `0x5646250`). `out` with an immediate port does not occur anywhere in `.text`, so it is covered by the unit test rather than by a retail count. ## Test `tests/test_x64_recomp.py::DecodeTests::test_in_out_immediate_port_consumes_imm8`: `E4 3d`, `E5 73`, `E6 40` and `E7 40` are two bytes with the operand shape `in al, imm8` and its three siblings, and `EC`/`ED`/`EE`/`EF` stay one byte. Against the unfixed decoder the first case decodes to size 1. `python -I tests/test_x64_recomp.py`: 33 tests OK, 26 skipped (this host has no qemu-system-aarch64). `python -I tests/test_recomp_census.py`: 11 tests OK. `python3 -m compileall -q make_game.py tools tests` exits 0. `python -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp.py docs/mobile.md` exits 0 with no output. The per-function `.pdata` walk is noisy for this change, because the other length defects still on `main` leave most functions misaligned, so it is not quoted as an impact figure. `docs/mobile.md` records the measurement, the two offsets and the evidence ID. No game files, no GPU, nothing online. Tested on Linux (openSUSE Tumbleweed, Python 3.13.18, binutils 2.47), 12 cores. AI-assisted (DeepSeek V4.1 Flash).
The primary table gives `E4`, `E5`, `E6` and `E7` the operand shape `sys`, so the decoder
consumed only the opcode and reported one byte. These are the immediate-port forms of `in`
and `out` (`E4 ib` = in al,imm8, `E5 ib` = in eax,imm8, `E6 ib` = out imm8,al, `E7 ib` =
out imm8,eax): each takes one immediate byte, so the instruction is two bytes and the walk
was one byte short, reading the port number as the next opcode. The `EC`-`EF` DX-port forms
do take no immediate and are left alone.

A per-instruction differential against objdump over the retail image's `.text`
(`game_scarlett_master.exe`, SHA-256
`e95020b2fa4a721db3df59fdecf75621730ac559d8835252f6c3b658d5a08993`, 26,674,180
instructions) finds the form used twice, at RVAs `0x55f2ed4` and `0x564652c`; both were one
byte short of objdump's two. The fix takes the mismatch count from 52,638 to 52,636 and adds
no signature. `docs/mobile.md` records the measurement, the two offsets and the REA
`gnu.objdump` section-header evidence.

Test: tests/test_x64_recomp.py::DecodeTests::test_in_out_immediate_port_consumes_imm8 fails
before the fix (the first case decodes to size 1) and passes after, covering `E4`-`E7` and
the one-byte `EC`-`EF` forms.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:53:45 +00:00
Author
Owner

@⁠ytsodacan on GitHub · 2026-11-25 03:36 UTC · original · edited 2026-11-25 03:43 UTC

[KEEP]

  • The change looks correct and well scoped: tools/x64_recomp.py E4-E7 go from sys to Ab Ib/Av Ib/Ib Ab/Ib Av, with a decode test covering all four plus the unchanged EC-EF forms.
  • CI has not finished: change-scope and no-game-data are still QUEUED, so there is no green result yet.
  • Likely textual conflicts with #1485 and #1483, which also add tests at the same spot (tests/test_x64_recomp.py near test_prefixes) and docs in docs/mobile.md. A maintainer should pick a merge order, then rebase the rest.
  • Suggest merging once CI is green and this is rebased after its siblings.
**@⁠ytsodacan** on GitHub · 2026-11-25 03:36 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1488#issuecomment-7152064861) · edited 2026-11-25 03:43 UTC **[KEEP]** - The change looks correct and well scoped: `tools/x64_recomp.py` E4-E7 go from `sys` to `Ab Ib`/`Av Ib`/`Ib Ab`/`Ib Av`, with a decode test covering all four plus the unchanged EC-EF forms. - CI has not finished: `change-scope` and `no-game-data` are still QUEUED, so there is no green result yet. - Likely textual conflicts with #1485 and #1483, which also add tests at the same spot (`tests/test_x64_recomp.py` near `test_prefixes`) and docs in `docs/mobile.md`. A maintainer should pick a merge order, then rebase the rest. - Suggest merging once CI is green and this is rebased after its siblings.
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-25 03:52 UTC · original · approved these changes

Approving. Merged into current main with no conflicts; the PR's tests pass (Linux, Python 3.13). E4/E5 are in AL/eAX, imm8 and E6/E7 out imm8, AL/eAX, so each carries an imm8 port byte; treating them as bare sys ended the instruction one byte early.

AI-assisted (Claude).

**@⁠tiguan26** on GitHub · 2026-11-25 03:52 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1488#pullrequestreview-6051616466) · **approved these changes** Approving. Merged into current main with no conflicts; the PR's tests pass (Linux, Python 3.13). E4/E5 are `in AL/eAX, imm8` and E6/E7 `out imm8, AL/eAX`, so each carries an imm8 port byte; treating them as bare `sys` ended the instruction one byte early. AI-assisted (Claude).
Author
Owner

@⁠kevincommits on GitHub · 2026-11-25 22:04 UTC · original

Independent run. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew, so the 26 exec tests ran rather than skipped. I merged main at 6b843a4ad into the branch locally (633 commits behind, no conflicts).

  • main alone decodes e4 3d as ('in', size 1); this branch gives ('in', size 2).
  • tests/test_x64_recomp.py: 34/34 (33 on main), so the new test fails on main and passes here. tests/test_recomp_census.py: 11/11.
  • Every standalone tests/test_*.py under python -I: 510 run, 506 pass; the 4 that fail are the same 4 that fail on plain main.

I only ran the synthetic byte fixtures. I did not reproduce the retail-image counts in the docs/mobile.md paragraph. No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-25 22:04 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1488#issuecomment-7175902208) Independent run. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew, so the 26 exec tests ran rather than skipped. I merged `main` at `6b843a4ad` into the branch locally (633 commits behind, no conflicts). - `main` alone decodes `e4 3d` as `('in', size 1)`; this branch gives `('in', size 2)`. - `tests/test_x64_recomp.py`: 34/34 (33 on `main`), so the new test fails on `main` and passes here. `tests/test_recomp_census.py`: 11/11. - Every standalone `tests/test_*.py` under `python -I`: 510 run, 506 pass; the 4 that fail are the same 4 that fail on plain `main`. I only ran the synthetic byte fixtures. I did not reproduce the retail-image counts in the `docs/mobile.md` paragraph. 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/1488/head:github-bot/gh-1488'
git switch 'github-bot/gh-1488'

Merge

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