x64_recomp: 0F 01 /r needs its ModRM byte (X64-RECOMP-GROUP7) #1483

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

@⁠kett1m on GitHub · 2026-11-25 01:19 UTC · original
kett1m:x64-recomp-grp7 → main · +59 −5 · 3 files · 1 commits · open

tools/x64_recomp.py's 0F table has no entry for opcode 0x01, so the whole
system-instruction group decoded as the unknown op0f_01 of two bytes. Every form of
0F 01 /r carries a ModRM byte: the memory forms are SGDT/SIDT/LGDT/LIDT/SMSW/LMSW/INVLPG, and
the register forms are whole ModRM bytes (MONITOR, MWAIT, XGETBV, XSETBV, CLZERO, MONITORX,
MWAITX, RDTSCP, SWAPGS, the VMX/SVM instructions). The walk stopped after the two opcode bytes,
so every instruction after it in the function was read at the wrong offset.

Measurement

A per-instruction differential against GNU objdump over the retail image's .text: objdump's
linear walk and the decoder's walk read the same bytes and the instruction lengths are compared
(objdump -d --start-address/--stop-address over the section, 26,674,180 objdump instructions).
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; .pdata VMA 0x14abcf000, file 0x6e03a00.

The differential flags 16 0F 01 boundaries, all one byte short before the fix:

  • 10 clzero, RVAs 0x4e89a70 to 0x4e89bb4, in two neighbouring .pdata functions
    (0x4e899c0 and 0x4e89b80). The first sits at the head of the title's 64-byte zeroing loop
    (clzero; add $0x40,%rax; cmp %r10,%rax; jb back to it), so these run, they are not
    stray data.
  • 2 xgetbv, RVAs 0x21ae7b5 and 0x55e8e92.
  • 2 monitorx, RVAs 0x4fafbd7 and 0x4fafd21.
  • 2 mwaitx, RVAs 0x4fafc00 and 0x4fafd52.

Over the whole .pdata corpus (recomp_census.py's walk, 299,257 functions), the fix lets 9
functions decode further, 638 instructions in total, and 7 of them reach the end of the function
with no unknown instruction left.

Test

tests/test_x64_recomp.py::DecodeTests::test_group7_0f01_carries_modrm pins the register forms
(CLZERO, XGETBV, MONITORX, MWAITX, MONITOR, RDTSCP, SWAPGS, SMSW) at three bytes and the memory
forms (sgdt [rax], sgdt [rsp+0x10], invlpg [rsp+0x10]) at their full
ModRM/SIB/displacement length. Against the unfixed decoder it fails: 0f 01 fc decodes to size
2.

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.

docs/mobile.md records the measurement and the evidence ID. The differential is local analysis
of the shipped file; nothing was executed. 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:19 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1483) `kett1m:x64-recomp-grp7` → `main` · +59 −5 · 3 files · 1 commits · open `tools/x64_recomp.py`'s `0F` table has no entry for opcode `0x01`, so the whole system-instruction group decoded as the unknown `op0f_01` of two bytes. Every form of `0F 01 /r` carries a ModRM byte: the memory forms are SGDT/SIDT/LGDT/LIDT/SMSW/LMSW/INVLPG, and the register forms are whole ModRM bytes (MONITOR, MWAIT, XGETBV, XSETBV, CLZERO, MONITORX, MWAITX, RDTSCP, SWAPGS, the VMX/SVM instructions). The walk stopped after the two opcode bytes, so every instruction after it in the function was read at the wrong offset. ## Measurement A per-instruction differential against GNU objdump over the retail image's `.text`: objdump's linear walk and the decoder's walk read the same bytes and the instruction lengths are compared (`objdump -d --start-address/--stop-address` over the section, 26,674,180 objdump instructions). 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`; `.pdata` VMA `0x14abcf000`, file `0x6e03a00`. The differential flags 16 `0F 01` boundaries, all one byte short before the fix: - 10 `clzero`, RVAs `0x4e89a70` to `0x4e89bb4`, in two neighbouring `.pdata` functions (`0x4e899c0` and `0x4e89b80`). The first sits at the head of the title's 64-byte zeroing loop (`clzero`; `add $0x40,%rax`; `cmp %r10,%rax`; `jb` back to it), so these run, they are not stray data. - 2 `xgetbv`, RVAs `0x21ae7b5` and `0x55e8e92`. - 2 `monitorx`, RVAs `0x4fafbd7` and `0x4fafd21`. - 2 `mwaitx`, RVAs `0x4fafc00` and `0x4fafd52`. Over the whole `.pdata` corpus (`recomp_census.py`'s walk, 299,257 functions), the fix lets 9 functions decode further, 638 instructions in total, and 7 of them reach the end of the function with no unknown instruction left. ## Test `tests/test_x64_recomp.py::DecodeTests::test_group7_0f01_carries_modrm` pins the register forms (CLZERO, XGETBV, MONITORX, MWAITX, MONITOR, RDTSCP, SWAPGS, SMSW) at three bytes and the memory forms (`sgdt [rax]`, `sgdt [rsp+0x10]`, `invlpg [rsp+0x10]`) at their full ModRM/SIB/displacement length. Against the unfixed decoder it fails: `0f 01 fc` decodes to size 2. `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. `docs/mobile.md` records the measurement and the evidence ID. The differential is local analysis of the shipped file; nothing was executed. 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 `0F` table had no entry for opcode `0x01`, so the whole system-instruction
group decoded as the unknown `op0f_01` and its two opcode bytes. Every form of
`0F 01 /r` carries a ModRM byte: the memory forms are SGDT/SIDT/LGDT/LIDT/SMSW/
LMSW/INVLPG, and the register forms are whole ModRM bytes (MONITOR, MWAIT,
XGETBV, XSETBV, CLZERO, MONITORX, MWAITX, RDTSCP, SWAPGS, the VMX/SVM
instructions). Stopping after the two opcode bytes made every following
instruction in the function the wrong one.

A per-instruction differential against objdump over the retail image's `.text`
(26,674,180 instructions) shows the group decoded one byte short at all 16 sites
the image uses (10 clzero, 2 xgetbv, 2 monitorx, 2 mwaitx); with the group back,
9 `.pdata` functions decode 638 instructions further and 7 of them have no
unknown instruction left. `docs/mobile.md` records the measurement and the REA
section-header evidence.

Test: tests/test_x64_recomp.py::DecodeTests::test_group7_0f01_carries_modrm
fails before the fix (`0f 01 fc` decodes to size 2) and passes after.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:53:33 +00:00
Author
Owner

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

Approving. Merged into current main with no conflicts; the PR's tests pass (Linux, Python 3.13). 0F 01 is group 7: memory forms by ModRM.reg (sgdt, sidt, lgdt, lidt, smsw, -, lmsw, invlpg) and register forms as whole ModRM bytes. The table matches the opcode map (C1 vmcall ... CB stac, D0 xgetbv/D1 xsetbv, D5 xend/D6 xtest, D8-DF SVM, EE/EF rdpkru/wrpkru, F8 swapgs, F9 rdtscp, FA/FB monitorx/mwaitx, FC clzero), and the walk now consumes the ModRM byte instead of stopping one byte late.

AI-assisted (Claude).

**@⁠tiguan26** on GitHub · 2026-11-25 03:53 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1483#pullrequestreview-6051622426) · **approved these changes** Approving. Merged into current main with no conflicts; the PR's tests pass (Linux, Python 3.13). 0F 01 is group 7: memory forms by ModRM.reg (sgdt, sidt, lgdt, lidt, smsw, -, lmsw, invlpg) and register forms as whole ModRM bytes. The table matches the opcode map (C1 vmcall ... CB stac, D0 xgetbv/D1 xsetbv, D5 xend/D6 xtest, D8-DF SVM, EE/EF rdpkru/wrpkru, F8 swapgs, F9 rdtscp, FA/FB monitorx/mwaitx, FC clzero), and the walk now consumes the ModRM byte instead of stopping one byte late. AI-assisted (Claude).
Author
Owner

@⁠kevincommits on GitHub · 2026-11-25 22:10 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 d3b8251bc into the branch locally (635 commits behind, no conflicts).

  • 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.

Beyond the fixtures, I fed disasm some inputs the new test does not cover:

bytes main this branch
0f 01 d0 c3 op0f_01 (2), then rol xgetbv (3), ret (1)
0f 01 05 78 56 34 12 (RIP-relative) op0f_01 (2), then add sgdt (7)
66 0f 01 d0 op0f_01 (3), db xgetbv (4)
f3 0f 01 e8, 0f 01 c0, 0f 01 ff (no table entry) op0f_01, db sys at the right length
0f 01 (buffer ends before ModRM) op0f_01 (2) db error record, no exception
0f 01 04, 0f 01 44 24 (ends inside SIB / displacement) op0f_01, db db error record, no exception

So truncated buffers end in the decoder's own error record, not an IndexError, and unlisted register forms still get the right length. I found nothing wrong. 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:10 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1483#issuecomment-7176025265) 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 `d3b8251bc` into the branch locally (635 commits behind, no conflicts). - `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`. Beyond the fixtures, I fed `disasm` some inputs the new test does not cover: | bytes | `main` | this branch | |---|---|---| | `0f 01 d0 c3` | `op0f_01` (2), then `rol` | `xgetbv` (3), `ret` (1) | | `0f 01 05 78 56 34 12` (RIP-relative) | `op0f_01` (2), then `add` | `sgdt` (7) | | `66 0f 01 d0` | `op0f_01` (3), `db` | `xgetbv` (4) | | `f3 0f 01 e8`, `0f 01 c0`, `0f 01 ff` (no table entry) | `op0f_01`, `db` | `sys` at the right length | | `0f 01` (buffer ends before ModRM) | `op0f_01` (2) | `db` error record, no exception | | `0f 01 04`, `0f 01 44 24` (ends inside SIB / displacement) | `op0f_01`, `db` | `db` error record, no exception | So truncated buffers end in the decoder's own error record, not an `IndexError`, and unlisted register forms still get the right length. I found nothing wrong. 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/1483/head:github-bot/gh-1483'
git switch 'github-bot/gh-1483'

Merge

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