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

Open
opened 2026-11-26 20:20:43 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1249
Author: @IlyassRS3
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: feat/recomp-census-xdata-unwind


📝 Commits (1)

  • db3ea85 recomp_census: fix UNW_FLAG_CHAININFO/CountOfCodes and decode .xdata unwind ops

📊 Changes

2 files changed (+686 additions, -6 deletions)

View changed files

➕ tests/test_recomp_census_unwind.py (+371 -0)
📝 tools/recomp_census.py (+315 -6)

📄 Description

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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1249 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/recomp-census-xdata-unwind` --- ### 📝 Commits (1) - [`db3ea85`](https://github.com/ubiquitarianism/leonida/commit/db3ea85daf091a2e5afc8c978fcff3e4e0ac4fad) recomp_census: fix UNW_FLAG_CHAININFO/CountOfCodes and decode .xdata unwind ops ### 📊 Changes **2 files changed** (+686 additions, -6 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_recomp_census_unwind.py` (+371 -0) 📝 `tools/recomp_census.py` (+315 -6) </details> ### 📄 Description ## 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#1280
No description provided.