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

Merged
kett1m merged 1 commit from upstream-pr/1483 into main 2026-11-28 10:19:55 +00:00
kett1m commented 2026-11-25 01:19:11 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kett1m on 2026-11-25T01:19:11Z.


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

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5483243446 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1483). Originally posted by [kett1m](https://github.com/kett1m) on 2026-11-25T01:19:11Z. --- `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).
tiguan26 commented 2026-11-25 03:53:34 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by tiguan26 on 2026-11-25T03:53:34Z.

Review: APPROVED


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

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6051622426 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1483#pullrequestreview-6051622426). Originally posted by [tiguan26](https://github.com/tiguan26) on 2026-11-25T03:53:34Z. **Review:** APPROVED --- 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).
kevincommits commented 2026-11-25 22:10:16 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-25T22:10:16Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7176025265 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1483#issuecomment-7176025265). Originally posted by [kevincommits](https://github.com/kevincommits) on 2026-11-25T22:10:16Z. --- 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.
MoonEyes commented 2026-11-28 10:19:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T10:19:49Z.


Red/green: the PR's tests fail on main 4938673 and pass on head c1df54c119 (Linux, Python 3.13.18).
Targeted batch of 11 PRs (their tests, the tests importing the changed modules, the docs tests) on main 3ca0707: no new failure, Linux / Python 3.13.
AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7237825052 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1483#issuecomment-7237825052). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T10:19:49Z. --- Red/green: the PR's tests fail on main 4938673 and pass on head c1df54c119fa (Linux, Python 3.13.18). Targeted batch of 11 PRs (their tests, the tests importing the changed modules, the docs tests) on main 3ca0707: no new failure, Linux / Python 3.13. AI-assisted (Claude Code, Claude Opus 5.5)
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
ProjectLeonida/mandy!1483
No description provided.