x64_recomp: a buffer that ends inside a VEX/EVEX prefix is a DecodeError (X64-RECOMP-VEX-BOUNDS) #1121

Merged
kett1m merged 3 commits from upstream-pr/1121 into main 2026-12-02 11:17:53 +00:00
kett1m commented 2026-11-24 18:50:36 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kett1m on 2026-11-24T18:50:36Z.


Claimed in the coordination issue as: X64-RECOMP-VEX-BOUNDS

Depends on: #1120

What and why

decode_one's docstring promises it never reads past end, but the VEX/EVEX prefix path
broke that promise in three places. The three-byte VEX form read buf[pos+2] behind a
pos + 1 < end guard, and both VEX forms and EVEX read the opcode that follows the prefix
at buf[pos] after advancing pos past end, unchecked. A buffer that ends inside a
prefix raised IndexError, which disasm() does not catch (it catches DecodeError
only), so the walk died.

Measured on the retail Xbox Series image (game_scarlett_master.exe, SHA-256
e95020b2fa4a721db3df59fdecf75621730ac559d8835252f6c3b658d5a08993) with REA
inspect_with_objdump. Section-header evidence
ev_e29cbdffd0dc7c46889b2a1f955011ce9f2795017233c97107ded73f9186dd90 maps .text RVA
0x140001000 to file 0x400 and .pdata RVA 0x14abcf000 to file 0x6e03a00;
file-header evidence ev_4ec749bb6395770cf8a099344c042bfd7ba67ff269f47e7ae115443028d4e127
gives entry 0x1456a1ec8. The unfixed walk aborted python3 -I tools/recomp_census.py <exe>
with IndexError: index out of range at tools/x64_recomp.py line 532, reached through the
vzeroupper desync fixed in #1120.

The fix raises DecodeError where the prefix loop and the REX path already do, so a cut
prefix is a reported gap rather than a crash. With #1120 in, no .pdata function of the
retail image reaches this path any more (0 of 299,257); the bound is what keeps a buffer a
caller really cut, at a .pdata or disassembly boundary, from killing the caller.

Tested

python3 -I tests/test_x64_recomp.py: 36 tests OK, 26 qemu skips.
python3 -I tests/test_recomp_census.py: 9 tests OK.
test_truncated_vex_and_evex_are_decode_errors covers the four prefix shapes at the end of
a buffer. test_never_reads_past_end decodes every sequence of one to four bytes over a
28-byte alphabet of prefixes, REX bytes, escape bytes and opcodes (637,420 sequences) and
requires each to stay inside the buffer or raise DecodeError. Both fail against the
unfixed decoder (IndexError: index out of range on c462, and the four shapes). The
census-level test shows a .pdata function cut inside a prefix is now a decode gap.
python3 -m compileall -q make_game.py tools tests: exit 0.
python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp.py tests/test_recomp_census.py: exit 0, no output.
The tests are synthetic bytes and a byte-built PE; no game files.

GPUs tested: n/a (decoder and census are CPU-only)

AI-assisted: yes (DeepSeek V4.1 Flash)

Checklist

  • Based on current main and merges cleanly after #1120.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption, including in logs or reports quoted here.
  • No new translator rule, so no docs/design.md contract row.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5478317892 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1121). Originally posted by [kett1m](https://github.com/kett1m) on 2026-11-24T18:50:36Z. --- Claimed in the coordination issue as: X64-RECOMP-VEX-BOUNDS Depends on: #1120 ### What and why `decode_one`'s docstring promises it never reads past `end`, but the VEX/EVEX prefix path broke that promise in three places. The three-byte VEX form read `buf[pos+2]` behind a `pos + 1 < end` guard, and both VEX forms and EVEX read the opcode that follows the prefix at `buf[pos]` after advancing `pos` past `end`, unchecked. A buffer that ends inside a prefix raised `IndexError`, which `disasm()` does not catch (it catches `DecodeError` only), so the walk died. Measured on the retail Xbox Series image (`game_scarlett_master.exe`, SHA-256 `e95020b2fa4a721db3df59fdecf75621730ac559d8835252f6c3b658d5a08993`) with REA `inspect_with_objdump`. Section-header evidence `ev_e29cbdffd0dc7c46889b2a1f955011ce9f2795017233c97107ded73f9186dd90` maps `.text` RVA `0x140001000` to file `0x400` and `.pdata` RVA `0x14abcf000` to file `0x6e03a00`; file-header evidence `ev_4ec749bb6395770cf8a099344c042bfd7ba67ff269f47e7ae115443028d4e127` gives entry `0x1456a1ec8`. The unfixed walk aborted `python3 -I tools/recomp_census.py <exe>` with `IndexError: index out of range` at `tools/x64_recomp.py` line 532, reached through the vzeroupper desync fixed in #1120. The fix raises `DecodeError` where the prefix loop and the REX path already do, so a cut prefix is a reported gap rather than a crash. With #1120 in, no `.pdata` function of the retail image reaches this path any more (0 of 299,257); the bound is what keeps a buffer a caller really cut, at a `.pdata` or disassembly boundary, from killing the caller. ### Tested `python3 -I tests/test_x64_recomp.py`: 36 tests OK, 26 qemu skips. `python3 -I tests/test_recomp_census.py`: 9 tests OK. `test_truncated_vex_and_evex_are_decode_errors` covers the four prefix shapes at the end of a buffer. `test_never_reads_past_end` decodes every sequence of one to four bytes over a 28-byte alphabet of prefixes, REX bytes, escape bytes and opcodes (637,420 sequences) and requires each to stay inside the buffer or raise `DecodeError`. Both fail against the unfixed decoder (`IndexError: index out of range` on `c462`, and the four shapes). The census-level test shows a `.pdata` function cut inside a prefix is now a decode gap. `python3 -m compileall -q make_game.py tools tests`: exit 0. `python3 -I scripts/check_no_game_data.py tools/x64_recomp.py tests/test_x64_recomp.py tests/test_recomp_census.py`: exit 0, no output. The tests are synthetic bytes and a byte-built PE; no game files. GPUs tested: n/a (decoder and census are CPU-only) AI-assisted: yes (DeepSeek V4.1 Flash) ### Checklist - [x] Based on current `main` and merges cleanly after #1120. - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption, including in logs or reports quoted here. - [x] No new translator rule, so no `docs/design.md` contract row.
abnalstlll-byte commented 2026-11-24 18:56:08 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by abnalstlll-byte on 2026-11-24T18:56:08Z.

Review: APPROVED


Reviewed and verified. Resolves unhandled IndexError in x64_recomp.py:decode_one when buffers terminate inside 3-byte VEX (0xC4) or EVEX prefixes. Safely bounds prefix reads and raises DecodeError per contract, allowing disassemblers to report gaps gracefully. Verified with tests/test_x64_recomp.py and tests/test_recomp_census.py. 100% clean-room.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6045426974 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1121#pullrequestreview-6045426974). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-24T18:56:08Z. **Review:** APPROVED --- Reviewed and verified. Resolves unhandled `IndexError` in `x64_recomp.py:decode_one` when buffers terminate inside 3-byte VEX (0xC4) or EVEX prefixes. Safely bounds prefix reads and raises `DecodeError` per contract, allowing disassemblers to report gaps gracefully. Verified with `tests/test_x64_recomp.py` and `tests/test_recomp_census.py`. 100% clean-room.
Deedlyy commented 2026-11-24 20:15:46 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Deedlyy on 2026-11-24T20:15:46Z.


Recommendation: [WAIT]

Reasoning: A buffer ending inside a VEX/EVEX prefix now raises DecodeError instead of an uncaught IndexError, fixing three out-of-bounds reads in decode_one. The change itself looks good and is tested. It depends on #1120: merge that first, then rebase this. CI (label, change-scope, no-game-data and the Runtime jobs) is still queued awaiting maintainer workflow approval, so nothing has run in CI yet; GitHub reports no merge conflict with main.

Impact level: 3/10

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7143268743 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1121#issuecomment-7143268743). Originally posted by [Deedlyy](https://github.com/Deedlyy) on 2026-11-24T20:15:46Z. --- **Recommendation:** [WAIT] **Reasoning:** A buffer ending inside a VEX/EVEX prefix now raises `DecodeError` instead of an uncaught `IndexError`, fixing three out-of-bounds reads in `decode_one`. The change itself looks good and is tested. It depends on #1120: merge that first, then rebase this. CI (label, change-scope, no-game-data and the Runtime jobs) is still queued awaiting maintainer workflow approval, so nothing has run in CI yet; GitHub reports no merge conflict with `main`. **Impact level:** 3/10
kevincommits commented 2026-11-26 04:44:34 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by kevincommits on 2026-11-26T04:44:34Z.


Independent run. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, LLVM from Homebrew. I merged main at a73a57e13 into the branch locally (1,098 commits behind; three files auto-merged, no conflicts). Head tested: 84239b50b.

On 200,000 random buffers main reads past the end of the buffer 5,059 times; this branch never does.

  • Random buffers through decode_one. 1–8 bytes each, fixed seed, half of them starting with a run of prefix and escape bytes (62 c4 c5 0f 66 67 f0 f2 f3 2e 64 65 48 40 4f), the same inputs on both trees:

    tree clean decode DecodeError IndexError size larger than buffer
    this branch (merged) 146,135 53,865 0 0
    main 146,126 48,815 5,059 0

    The first main failure is the two-byte buffer c4 0f. Every one of the 5,059 becomes a DecodeError or a clean decode here, and nothing that decoded cleanly on main stops decoding.

  • vzeroupper / vzeroall length checked against an independent decoder. llvm-mc --disassemble -triple=x86_64 -mattr=+avx gives c5 f8 77 = vzeroupper and c5 fc 77 = vzeroall, 3 bytes each, with the following c3 as its own retq, as the new tests expect.

  • Counts on the merged tree. tests/test_x64_recomp.py 48/48 (main: 44) and tests/test_recomp_census.py 13/13 (main: 11). Full standalone suite: 513 run, 512 pass; the 1 that fails is main's test_mod_pack_output_alias.

  • Overlap. #1246 adds a test at the same spot in both test files (test_prefixes in tests/test_x64_recomp.py, the edge tests in tests/test_recomp_census.py); whichever lands second will need a small rebase. I did not merge the two together.

Not tested: execution (the change is decode-only) and buffers longer than 8 bytes. No game files used. AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7182862418 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1121#issuecomment-7182862418). Originally posted by [kevincommits](https://github.com/kevincommits) on 2026-11-26T04:44:34Z. --- Independent run. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, LLVM from Homebrew. I merged `main` at `a73a57e13` into the branch locally (1,098 commits behind; three files auto-merged, no conflicts). Head tested: `84239b50b`. **On 200,000 random buffers `main` reads past the end of the buffer 5,059 times; this branch never does.** - **Random buffers through `decode_one`.** 1–8 bytes each, fixed seed, half of them starting with a run of prefix and escape bytes (`62 c4 c5 0f 66 67 f0 f2 f3 2e 64 65 48 40 4f`), the same inputs on both trees: | tree | clean decode | `DecodeError` | `IndexError` | size larger than buffer | |---|---|---|---|---| | this branch (merged) | 146,135 | 53,865 | 0 | 0 | | `main` | 146,126 | 48,815 | 5,059 | 0 | The first `main` failure is the two-byte buffer `c4 0f`. Every one of the 5,059 becomes a `DecodeError` or a clean decode here, and nothing that decoded cleanly on `main` stops decoding. - **`vzeroupper` / `vzeroall` length checked against an independent decoder.** `llvm-mc --disassemble -triple=x86_64 -mattr=+avx` gives `c5 f8 77` = `vzeroupper` and `c5 fc 77` = `vzeroall`, 3 bytes each, with the following `c3` as its own `retq`, as the new tests expect. - **Counts on the merged tree.** `tests/test_x64_recomp.py` 48/48 (`main`: 44) and `tests/test_recomp_census.py` 13/13 (`main`: 11). Full standalone suite: 513 run, 512 pass; the 1 that fails is `main`'s `test_mod_pack_output_alias`. - **Overlap.** #1246 adds a test at the same spot in both test files (`test_prefixes` in `tests/test_x64_recomp.py`, the edge tests in `tests/test_recomp_census.py`); whichever lands second will need a small rebase. I did not merge the two together. Not tested: execution (the change is decode-only) and buffers longer than 8 bytes. No game files used. AI-assisted.
MoonEyes commented 2026-11-28 17:58:53 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T17:58:53Z.


Hi @kett1m, thanks for this pull request. It currently conflicts with main (conflicting file(s): tests/test_x64_recomp.py), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7243435805 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1121#issuecomment-7243435805). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T17:58:53Z. --- Hi @kett1m, thanks for this pull request. It currently conflicts with `main` (conflicting file(s): `tests/test_x64_recomp.py`), so it carries the `needs rebase` label. CONTRIBUTING.md asks authors to rebase their branch on `main`; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here. AI-assisted (Claude Code, Claude Opus 5.5).
aslan2142 commented 2026-12-01 11:40:05 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T11:40:05Z.

Review: COMMENTED


Reviewed the diff against main: the VEX/EVEX truncation DecodeError guards in tools/x64_recomp.py:568-595 and exhaustive tests in tests/test_x64_recomp.py:95-121 look solid and clean-room verified. Holding approval/merge because this PR is stacked on unmerged #1120 and has stale failing CI — once #1120 lands, please rebase onto main so CI runs cleanly.

(AI-assisted review.)

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6130490821 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1121#pullrequestreview-6130490821). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T11:40:05Z. **Review:** COMMENTED --- Reviewed the diff against `main`: the VEX/EVEX truncation `DecodeError` guards in `tools/x64_recomp.py:568-595` and exhaustive tests in `tests/test_x64_recomp.py:95-121` look solid and clean-room verified. Holding approval/merge because this PR is stacked on unmerged `#1120` and has stale failing CI — once `#1120` lands, please rebase onto `main` so CI runs cleanly. *(AI-assisted review.)*
MoonEyes commented 2026-12-02 10:48:30 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T10:48:30Z.


For information: this pull request is carried in #3101, which merges your head 72eedf158 (your commits are kept) into current main and resolves the conflict in tests/test_x64_recomp.py by keeping main's tests and your two new ones. Nothing else changes. If you rebase this one yourself, say so and #3101 can be closed.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7343009871 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1121#issuecomment-7343009871). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T10:48:30Z. --- For information: this pull request is carried in #3101, which merges your head `72eedf158` (your commits are kept) into current `main` and resolves the conflict in `tests/test_x64_recomp.py` by keeping main's tests and your two new ones. Nothing else changes. If you rebase this one yourself, say so and #3101 can be closed. AI-assisted (Claude Code).
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!1121
No description provided.