Skip to content

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

Merged
MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:feat/recomp-census-xdata-unwind
Nov 29, 2026
Merged

MoonEyes merged 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:feat/recomp-census-xdata-unwind

Conversation

@IlyassRS3

Copy link
Copy Markdown
Contributor

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 recomp_census: a wrong input is one ERROR line, not a traceback (RECOMP-CENSUS-WRONG-INPUT) #1088 (RECOMP-CENSUS-WRONG-INPUT).

Item ID

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 recomp_census: a wrong input is one ERROR line, not a traceback (RECOMP-CENSUS-WRONG-INPUT) #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 (Coordinating who works on what #14) before opening this PR
  • AI-assisted: yes (implemented and verified with AI assistance)

Copilot AI balanced review requested due to automatic review settings November 24, 2026 20:51

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@kevincommits

Copy link
Copy Markdown
Contributor

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.

@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#1249 | head db3ea85 (merged cleanly with main c7753ed) | python -I tests/test_recomp_census_unwind.py -v + python -I tests/test_recomp_census.py -v | red on main: FAILED (failures=3, errors=23) (recomp_census.unwind_flags read SizeOfProlog from byte 1 instead of CountOfCodes from byte 2, mapped bit 0x4 to FHANDLER instead of UNW_FLAG_CHAININFO/CHAINED, and lacked parse_unwind_info) | green on PR+main: 17 ran, OK (test_recomp_census_unwind.py) + 11 ran, OK (test_recomp_census.py) | Windows 11 (10.0.26200.9974), Python 3.12.10 | AI-assisted: yes

Audited db3ea85daf (tools/recomp_census.py and tests/test_recomp_census_unwind.py) on Windows 11, verifying UNW_FLAG_CHAININFO = 0x4, byte-2 CountOfCodes, all x64 .xdata UNWIND_CODE opcodes (0..10, including multi-slot UWOP_ALLOC_LARGE, UWOP_SAVE_NONVOL*, UWOP_SAVE_XMM128*, UWOP_SPARE_CODE, and v2 UWOP_EPILOG), even-slot alignment padding before exception-handler RVAs and chained RUNTIME_FUNCTION trailers, and cycle/depth bounds (MAX_UNWIND_CHAIN = 64). Also verified tests/test_ci_registration.py (stdlib job selection), tests/test_text_write_line_endings.py, and scripts/check_no_game_data.py.

@enlight3d

Copy link
Copy Markdown
Contributor

Independent run of #1249 (python -I, no build, no game; GPU not used).

  • PR head: db3ea85
  • main: 829530d (PR merged into it locally with git merge --no-ff, clean, only tools/recomp_census.py and the new test touched)
  • Machine: Windows 11 Pro 10.0.26300, CPython 3.13.12, standard library only

Commands (one process each, from the repo root): python -I tests/test_recomp_census_unwind.py, python -I tests/test_recomp_census.py, plus test_ci_registration.py, test_text_write_line_endings.py, test_tool_stdout_encoding.py, test_check_no_game_data.py, test_docs_repo_paths.py.

  • Red: with tools/recomp_census.py reverted to main and the new test kept: FAILED (failures=3, errors=23). The failures are the two header fixes (CHAINED reported for a record that has no chain flag; a CountOfCodes of 12 where 0 is expected, i.e. byte 1 read instead of byte 2; FHANDLER where CHAINED is expected), the errors are the missing parse_unwind_info and the missing flag constants. So it is a behaviour red for the two bug fixes plus a new-feature red for the decoder.
  • Green at head: test_recomp_census_unwind.py 17/17 OK, test_recomp_census.py 7/7 OK (the head branch is based on an older main, which has 7 tests there).
  • Green merged into main: test_recomp_census_unwind.py 17/17 OK, test_recomp_census.py 11/11 OK, ci_registration 18/18, line endings 3/3, check_no_game_data 21 (2 skipped), docs_repo_paths 5/5.
  • Merged vs plain main: no new failure. test_tool_stdout_encoding.py fails identically on plain main and merged (same list: tools/pc_prompt_glyphs.py, tools/xbox_prompt_glyphs.py, scripts/bench_translate.py, scripts/export_old_pr_manifest.py), so it is pre-existing; this PR adds no entry to it.

@MoonEyes
MoonEyes merged commit 87a39df into ProjectLeonida:main Nov 29, 2026
1 check failed
@MoonEyes

Copy link
Copy Markdown
Collaborator

Merged after enlight3d's red/green check (check) (RTX 3090, driver 621.40, Windows 11) and a local re-check here. Merged after local re-verification: this PR merged onto main a3484ce without conflict, and its test file(s) (tests/test_recomp_census_unwind.py) pass there. Earlier today the same tests failed on main without the PR (red/green) and a bundle with other open PRs showed no new failures. check_no_game_data.py is clean. Windows 11, Python 3.14; no GPU needed for these tests.

AI-assisted (Claude Code, Claude Opus 5.5), merged for MoonEyes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants