recomp_census: fix UNW_FLAG_CHAININFO/CountOfCodes and decode .xdata unwind ops (RECOMP-CENSUS-XDATA-UNWIND) #1249

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

@⁠IlyassRS3 on GitHub · 2026-11-24 20:51 UTC · original
IlyassRS3:feat/recomp-census-xdata-unwind → main · +686 −6 · 2 files · 1 commits · open

What changed

Fixes two x64 .xdata UNWIND_INFO header bugs in tools/recomp_census.py and adds full UNWIND_CODE decoding and UNW_FLAG_CHAININFO chain walking to report the SEH / frame-layout breakdown needed by docs/mobile.md Next Step #3 (arm64 SEH / unwind translation):

Bug fixes in tools/recomp_census.py

  1. Wrong UNW_FLAG_* bitmask (UNW_FLAG_NAMES): UNW_FLAG_CHAININFO is 0x4 (per Microsoft x64 exception handling / LLVM Win64EH.h), not 0x8 (0x4 was previously mislabelled FHANDLER, so real chained unwind entries were misreported as FHANDLER and CHAINED was never reported).
  2. Wrong byte for CountOfCodes (unwind_flags): UNWIND_INFO byte 1 is SizeOfProlog and byte 2 is CountOfCodes; unwind_flags() previously returned byte 1 as code_count.

.xdata unwind decoder & census (tools/recomp_census.py)

  • Add parse_unwind_info(img, unwind_rva) decoding all 11 x64 UNWIND_CODE opcodes (0..10: UWOP_PUSH_NONVOL, UWOP_ALLOC_LARGE 2- and 3-slot forms, UWOP_ALLOC_SMALL, UWOP_SET_FPREG, UWOP_SAVE_NONVOL, UWOP_SAVE_NONVOL_FAR, v2 UWOP_EPILOG, UWOP_SPARE_CODE, UWOP_SAVE_XMM128, UWOP_SAVE_XMM128_FAR, UWOP_PUSH_MACHFRAME), verified against LLVM Win64EH.h/Win64EHDumper.cpp and Microsoft CoreCLR amd64/unwinder.cpp.
  • Respect even-slot padding ((CountOfCodes + 1) & ~1) * 2 before trailing ExceptionHandler RVA or chained RUNTIME_FUNCTION.
  • Follow UNW_FLAG_CHAININFO chains with cycle detection and bounded depth (MAX_UNWIND_CHAIN = 64), aggregating fixed stack allocation, saved nonvolatile GPRs/XMMs, frame register/offset, epilog counts, and primary handler RVA.
  • Report malformed/truncated/unsupported .xdata records cleanly via error without crashing.
  • Add an "unwind" breakdown to census() JSON output and an ## Unwind (.xdata) details section to to_markdown() while keeping all existing JSON keys and Markdown headings backwards-compatible.
  • Add tests/test_recomp_census_unwind.py (17 unit tests using synthetic byte-built PE32+ fixtures, no game data).
  • Intentionally leaves main(), the file docstring, and tests/test_recomp_census.py untouched so this merges cleanly with open PR #1088 (RECOMP-CENSUS-WRONG-INPUT).

Item ID

  • Coordination issue #14 item: RECOMP-CENSUS-XDATA-UNWIND
  • Depends on: none (verified zero-conflict 3-way merge with #1088)

Verification

Tested on macOS 27.2.1 (Apple M5 arm64), Python 3.14 (no game files, no GPU, standard library only):

  • python3 -I tests/test_recomp_census.py -v -> 7/7 OK
  • python3 -I tests/test_recomp_census_unwind.py -v -> 17/17 OK
  • Revert-source mutation check (reverting tools/recomp_census.py to upstream/main while keeping tests/test_recomp_census_unwind.py) -> FAILED (3 failures, 23 errors), directly proving FHANDLER != CHAINED, 12 != 0 (SizeOfProlog vs CountOfCodes), and 0x8 falsely flagged as CHAINED on main.
  • Scratch 3-way merge with open PR #1088 (RECOMP-CENSUS-WRONG-INPUT): auto-merged with 0 conflicts, running 11/11 test_recomp_census.py + 17/17 test_recomp_census_unwind.py (28/28 OK).
  • python3 -m compileall -q tools/recomp_census.py tests/test_recomp_census.py tests/test_recomp_census_unwind.py -> OK
  • python3 -I scripts/check_no_game_data.py tools/recomp_census.py tests/test_recomp_census_unwind.py -> OK
  • python3 -I tests/test_ci_registration.py -> 15/15 OK (new test file selected by CI stdlib loop)
  • python3 -I tests/test_text_write_line_endings.py -> 3/3 OK

Checklist

  • No game files, binaries, shaders, textures, audio, or logs quoting them
  • No keys, decryption, or online services
  • Claimed in the coordination issue (#14) before opening this PR
  • AI-assisted: yes (implemented and verified with AI assistance)
**@⁠IlyassRS3** on GitHub · 2026-11-24 20:51 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1249) `IlyassRS3:feat/recomp-census-xdata-unwind` → `main` · +686 −6 · 2 files · 1 commits · open ## What changed Fixes two x64 `.xdata` `UNWIND_INFO` header bugs in `tools/recomp_census.py` and adds full `UNWIND_CODE` decoding and `UNW_FLAG_CHAININFO` chain walking to report the SEH / frame-layout breakdown needed by `docs/mobile.md` Next Step #3 (arm64 SEH / unwind translation): ### Bug fixes in `tools/recomp_census.py` 1. **Wrong `UNW_FLAG_*` bitmask (`UNW_FLAG_NAMES`)**: `UNW_FLAG_CHAININFO` is `0x4` (per Microsoft x64 exception handling / LLVM `Win64EH.h`), not `0x8` (`0x4` was previously mislabelled `FHANDLER`, so real chained unwind entries were misreported as `FHANDLER` and `CHAINED` was never reported). 2. **Wrong byte for `CountOfCodes` (`unwind_flags`)**: `UNWIND_INFO` byte 1 is `SizeOfProlog` and byte 2 is `CountOfCodes`; `unwind_flags()` previously returned byte 1 as `code_count`. ### `.xdata` unwind decoder & census (`tools/recomp_census.py`) - Add `parse_unwind_info(img, unwind_rva)` decoding all 11 x64 `UNWIND_CODE` opcodes (`0..10`: `UWOP_PUSH_NONVOL`, `UWOP_ALLOC_LARGE` 2- and 3-slot forms, `UWOP_ALLOC_SMALL`, `UWOP_SET_FPREG`, `UWOP_SAVE_NONVOL`, `UWOP_SAVE_NONVOL_FAR`, v2 `UWOP_EPILOG`, `UWOP_SPARE_CODE`, `UWOP_SAVE_XMM128`, `UWOP_SAVE_XMM128_FAR`, `UWOP_PUSH_MACHFRAME`), verified against LLVM `Win64EH.h`/`Win64EHDumper.cpp` and Microsoft CoreCLR `amd64/unwinder.cpp`. - Respect even-slot padding `((CountOfCodes + 1) & ~1) * 2` before trailing `ExceptionHandler` RVA or chained `RUNTIME_FUNCTION`. - Follow `UNW_FLAG_CHAININFO` chains with cycle detection and bounded depth (`MAX_UNWIND_CHAIN = 64`), aggregating fixed stack allocation, saved nonvolatile GPRs/XMMs, frame register/offset, epilog counts, and primary handler RVA. - Report malformed/truncated/unsupported `.xdata` records cleanly via `error` without crashing. - Add an `"unwind"` breakdown to `census()` JSON output and an `## Unwind (.xdata) details` section to `to_markdown()` while keeping all existing JSON keys and Markdown headings backwards-compatible. - Add `tests/test_recomp_census_unwind.py` (17 unit tests using synthetic byte-built PE32+ fixtures, no game data). - Intentionally leaves `main()`, the file docstring, and `tests/test_recomp_census.py` untouched so this merges cleanly with open PR #1088 (`RECOMP-CENSUS-WRONG-INPUT`). ## Item ID - Coordination issue #14 item: `RECOMP-CENSUS-XDATA-UNWIND` - Depends on: none (verified zero-conflict 3-way merge with #1088) ## Verification Tested on macOS 27.2.1 (Apple M5 arm64), Python 3.14 (no game files, no GPU, standard library only): - `python3 -I tests/test_recomp_census.py -v` -> **7/7 OK** - `python3 -I tests/test_recomp_census_unwind.py -v` -> **17/17 OK** - Revert-source mutation check (reverting `tools/recomp_census.py` to `upstream/main` while keeping `tests/test_recomp_census_unwind.py`) -> **FAILED (3 failures, 23 errors)**, directly proving `FHANDLER != CHAINED`, `12 != 0` (`SizeOfProlog` vs `CountOfCodes`), and `0x8` falsely flagged as `CHAINED` on `main`. - Scratch 3-way merge with open PR #1088 (`RECOMP-CENSUS-WRONG-INPUT`): auto-merged with **0 conflicts**, running **11/11** `test_recomp_census.py` + **17/17** `test_recomp_census_unwind.py` (**28/28 OK**). - `python3 -m compileall -q tools/recomp_census.py tests/test_recomp_census.py tests/test_recomp_census_unwind.py` -> OK - `python3 -I scripts/check_no_game_data.py tools/recomp_census.py tests/test_recomp_census_unwind.py` -> OK - `python3 -I tests/test_ci_registration.py` -> **15/15 OK** (new test file selected by CI stdlib loop) - `python3 -I tests/test_text_write_line_endings.py` -> **3/3 OK** ## Checklist - [x] No game files, binaries, shaders, textures, audio, or logs quoting them - [x] No keys, decryption, or online services - [x] Claimed in the coordination issue (#14) before opening this PR - [x] AI-assisted: yes (implemented and verified with AI assistance)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:44:25 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-24 20:52 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-24 20:52 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1249#pullrequestreview-6047202027) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
Author
Owner

@⁠kevincommits on GitHub · 2026-11-26 05:01 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,001 commits behind; tools/recomp_census.py auto-merged, no conflicts). Head tested: db3ea85da.

On 12 hand-built version-1 unwind records, parse_unwind_info decodes exactly what llvm-readobj --unwind decodes.

  • Counts on the merged tree. tests/test_recomp_census_unwind.py 17/17, tests/test_recomp_census.py 11/11 (same as main). Full standalone suite: 514 run, 513 pass; the 1 that fails is main's test_mod_pack_output_alias.

  • Checked against an independent decoder. I built one PE32+ image with this PR's own build_pe_with_xdata (12 functions, 12 unwind records, bytes written by hand from the format; no game file), gave llvm-readobj a copy with .text/.pdata/.xdata section headers over the same bytes (it finds unwind data by section name), and compared operation by operation:

    record what it holds same as llvm-readobj
    push+small ALLOC_SMALL 32, PUSH_NONVOL rbx yes
    large16 / large32 ALLOC_LARGE 256 (scaled 16-bit) / 0x20040 (unscaled 32-bit) yes / yes
    fpreg SET_FPREG rbp with frame offset 0x20, PUSH_NONVOL rbp yes
    save_nonvol / save_nonvol_far rsi at 0x20 / r12 at 0x10020 yes / yes
    save_xmm / save_xmm_far xmm6 at 0x30 / xmm15 at 0x30000 yes / yes
    machframe PUSH_MACHFRAME with error code yes
    ehandler_odd one code slot, 2 bytes of padding, then the handler RVA yes
    uhandler_even two code slots, no padding, then the handler RVA yes
    all_regs PUSH_NONVOL for all 16 registers yes

    So slot counts, the ×8 and ×16 scaling, the even-slot padding before the handler RVA and the register numbering all match.

  • The two header fixes are right by the format. UNW_FLAG_CHAININFO is 0x4 (main has 0x4 as FHANDLER and 0x8 as CHAINED), and CountOfCodes is byte 2 (main returns byte 1, the prolog size).

Not tested against a reference: version-2 records (UWOP_EPILOG, UWOP_SPARE_CODE), chained records and the cycle/depth limits, which are covered only by the PR's own tests. No game files used. AI-assisted.

**@⁠kevincommits** on GitHub · 2026-11-26 05:01 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1249#issuecomment-7183112130) 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,001 commits behind; `tools/recomp_census.py` auto-merged, no conflicts). Head tested: `db3ea85da`. **On 12 hand-built version-1 unwind records, `parse_unwind_info` decodes exactly what `llvm-readobj --unwind` decodes.** - **Counts on the merged tree.** `tests/test_recomp_census_unwind.py` 17/17, `tests/test_recomp_census.py` 11/11 (same as `main`). Full standalone suite: 514 run, 513 pass; the 1 that fails is `main`'s `test_mod_pack_output_alias`. - **Checked against an independent decoder.** I built one PE32+ image with this PR's own `build_pe_with_xdata` (12 functions, 12 unwind records, bytes written by hand from the format; no game file), gave `llvm-readobj` a copy with `.text`/`.pdata`/`.xdata` section headers over the same bytes (it finds unwind data by section name), and compared operation by operation: | record | what it holds | same as llvm-readobj | |---|---|---| | `push+small` | `ALLOC_SMALL` 32, `PUSH_NONVOL` rbx | yes | | `large16` / `large32` | `ALLOC_LARGE` 256 (scaled 16-bit) / 0x20040 (unscaled 32-bit) | yes / yes | | `fpreg` | `SET_FPREG` rbp with frame offset 0x20, `PUSH_NONVOL` rbp | yes | | `save_nonvol` / `save_nonvol_far` | rsi at 0x20 / r12 at 0x10020 | yes / yes | | `save_xmm` / `save_xmm_far` | xmm6 at 0x30 / xmm15 at 0x30000 | yes / yes | | `machframe` | `PUSH_MACHFRAME` with error code | yes | | `ehandler_odd` | one code slot, 2 bytes of padding, then the handler RVA | yes | | `uhandler_even` | two code slots, no padding, then the handler RVA | yes | | `all_regs` | `PUSH_NONVOL` for all 16 registers | yes | So slot counts, the ×8 and ×16 scaling, the even-slot padding before the handler RVA and the register numbering all match. - **The two header fixes are right by the format.** `UNW_FLAG_CHAININFO` is 0x4 (`main` has 0x4 as `FHANDLER` and 0x8 as `CHAINED`), and `CountOfCodes` is byte 2 (`main` returns byte 1, the prolog size). Not tested against a reference: version-2 records (`UWOP_EPILOG`, `UWOP_SPARE_CODE`), chained records and the cycle/depth limits, which are covered only by the PR's own tests. 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/1249/head:github-bot/gh-1249'
git switch 'github-bot/gh-1249'

Merge

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