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

Open
github-bot wants to merge 2 commits from github-bot/gh-1121 into main AGit
Owner

@⁠kett1m on GitHub · 2026-11-24 18:50 UTC · original
kett1m:x64-recomp-vex-bounds → main · +114 −1 · 3 files · 2 commits · open

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.
**@⁠kett1m** on GitHub · 2026-11-24 18:50 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1121) `kett1m:x64-recomp-vex-bounds` → `main` · +114 −1 · 3 files · 2 commits · open 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.
`decode_one` gave every VEX map-1 opcode the operand shape `V W`, which reads a ModRM
byte. VEX.128/256.0F.WIG 77 (vzeroupper / vzeroall) is the one map-1 opcode with no ModRM
byte at all, so it decoded as four bytes and the byte it took as an operand was really the
next instruction: a function's decode fell out of step from its first vzeroupper on, and
the shifted walk then ran past the end of the function.

Measured on the retail Xbox Series image (`game_scarlett_master.exe`, SHA-256
e95020b2fa4a721db3df59fdecf75621730ac559d8835252f6c3b658d5a08993) with REA `gnu.objdump`.
Evidence `ev_e29cbdffd0dc7c46889b2a1f955011ce9f2795017233c97107ded73f9186dd90` (section
headers) maps `.text` RVA 0x140001000 to file 0x400 and `.pdata` RVA 0x14abcf000 to file
0x6e03a00; `ev_4ec749bb6395770cf8a099344c042bfd7ba67ff269f47e7ae115443028d4e127` (file
headers) gives entry 0x1456a1ec8. Ten `.pdata` functions hold such a vzeroupper: RVAs
0xa4ab00, 0x1fcf4d0, 0x348a250, 0x35b4ec0, 0x3b88490, 0x3cb18c0, 0x3ede150, 0x4c703e0,
0x4d56520, 0x58430e0. RVA 0xa4ab00 (file 0xa49f00, 95 bytes) ends `90 48 83 c4 68 c3`,
that is `nop; add rsp,68h; ret`; the walk read `c4 68 c3` there, because the vzeroupper at
offset 81 (RVA 0xa4ab51, bytes `c5 f8 77`) had eaten the `e8` of a `call` and put the rest
of the function one byte early.

Before this change `python -I tools/recomp_census.py <exe>` aborted with `IndexError: index
out of range` at `tools/x64_recomp.py` line 532. After it the census runs over all 299,257
`.pdata` functions: 21,518,571 decoded instructions, 84.77% translated coverage, 41,091
decode errors, 48,224 `v77`, and no `.pdata` function ends inside a VEX or EVEX prefix any
more (10 before).

Decoding every `.pdata` function before and after this change, 20,620 of the 299,257 (6.9%)
decode to a different instruction stream, and on 10 of them the unfixed walk ran past the
buffer -- those are the ones that aborted the census. Counting decode gaps (the unknown `db`
insn `disasm` yields): 453 functions have a gap only in the unfixed walk, whose shifted bytes
run into an undecodable byte, and 0 have a gap only after the change, so it removes those 453
without adding one.

`tools/gdk_runtime_calls.py` already makes this exception for the same opcode
(`modrm = not (vmap == 1 and b == 0x77)`), and tests/test_gdk_runtime_calls.py pins
`C5 F8 77` at 3 bytes, so the rule is not new to this repository. The tests are synthetic,
decoder bytes and the byte-built PE fixture in tests/test_recomp_census.py, and both fail
against the unfixed decoder.

Hardware: AMD Ryzen 5 7600X, Linux 7.3.4-1-default, Python 3.13.18; decoder and census are
CPU-only, no GPU. AI-assisted (DeepSeek V4.1 Flash).
`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 the two 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: the retail image (`game_scarlett_master.exe`, SHA-256
e95020b2fa4a721db3df59fdecf75621730ac559d8835252f6c3b658d5a08993) aborted
`tools/recomp_census.py` with `IndexError: index out of range` at `tools/x64_recomp.py`
line 532, reached through the vzeroupper desync fixed in the previous commit.

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. Tests: `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). A census-level test
(`test_truncated_vex_prefix_is_a_gap_not_a_crash`) shows a `.pdata` function cut inside a
prefix is now reported as a decode gap.

With the vzeroupper fix in, no `.pdata` function of the retail image reaches this path any
more (0 of 299,257); the report of `tools/recomp_census.py` is unchanged by this commit. The
bound is what keeps a buffer a caller really cut, at a `.pdata` or disassembly boundary,
from killing the caller. Tests are synthetic; no game files.

Hardware: AMD Ryzen 5 7600X, Linux 7.3.4-1-default, Python 3.13.18; CPU-only, no GPU.
AI-assisted (DeepSeek V4.1 Flash).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:39:18 +00:00
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 18:56 UTC · original · approved these changes

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.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 18:56 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1121#pullrequestreview-6045426974) · **approved these changes** 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.
Author
Owner

@⁠Deedlyy on GitHub · 2026-11-24 20:15 UTC · original

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

**@⁠Deedlyy** on GitHub · 2026-11-24 20:15 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1121#issuecomment-7143268743) **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
Author
Owner

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

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.

**@⁠kevincommits** on GitHub · 2026-11-26 04:44 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1121#issuecomment-7182862418) 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.
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/1121/head:github-bot/gh-1121'
git switch 'github-bot/gh-1121'

Merge

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