Skip to content

RT-5: import_fail_fast.py, where the retail executable stops itself on an import's result (78 sites, 27 imports) - #7010

Open
TreXito wants to merge 5 commits into
ItsNotRexed:mainfrom
TreXito:exe/import-fail-fast
Open

TreXito wants to merge 5 commits into
ItsNotRexed:mainfrom
TreXito:exe/import-fail-fast

Conversation

@TreXito

@TreXito TreXito commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: #6910 (comment) (RT-5 tooling)

Depends on: #6843 (this branch is stacked on it and reuses its tools/import_call_sites.py; only the last commit is new)

@ItsNotRexed exe PR, as asked.

What and why

Every stop on the retail boot path so far has been the title's own int3: it tests what an import returned and breaks when the value is the failing one (#6824 stops this way at XMemAllocatePhysicalPages). This adds a tool that finds those checks ahead of a run, from the executable file alone. A shim author can then see which results the title will not accept before a run reaches them.

The doc quotes only import names, counts and conditions: no addresses, offsets or bytes from the executable. The tool prints RVAs only for whoever runs it on their own copy.

Tested

  • python -I tests/test_import_fail_fast.py passes: 8 tests on a PE built byte by byte, with 24 call-site cases. These cover each test form, copies, the jump-follow, a noreturn call, int 29h/ud2, a direct slot call, an ordinal import, a tail jump and a stray FF 15 (both ignored), the cases the tool must not judge, and the CLI's table, --json, --import, --all and exit 2.
  • A mutation check: disabling jump-following, copy tracking, cdqe or int 29h each makes the test fail.
  • These also pass: tests/test_import_call_sites.py, tests/test_gdk_runtime_calls.py, every tests/test_docs_*.py, tests/test_ci_registration.py, python -m compileall -q make_game.py tools tests and scripts/check_no_game_data.py.
    • tests/test_text_write_line_endings.py fails on the base too (tools/f64_omod_probe.py:268), so that failure is not from this change.
  • Retail exe (SHA-256 e95020b2…08993, my own copy), read statically on Linux with Python 3.13.5: the result is above. An independent Capstone-based pass found the same sites per import, except 2 XMemVirtualAlloc sites that it missed. I checked both by hand: one tests a copy of the result, and one reaches int3 through a jmp.
  • Local runs: Linux, Python 3.14.4. CI uses 3.12; the code uses nothing newer.

AI-assisted: yes (Claude Code)

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. Only import names, counts and conditions.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: no translator change)

🤖 Generated with Claude Code

Etoile-Bleu and others added 2 commits November 23, 2026 16:15
…oc sites pass an attribute word the shim refuses

tools/import_call_sites.py finds an import's thunk and the call/jmp rel32 instructions that reach it, from the file
alone. On game_scarlett_master.exe: XMemVirtualAlloc 23 sites (every attributes value nonzero, protections carrying
PAGE_GRAPHICS bits), the physical-page family 21 sites with an AWE-shaped signature and 64 KiB pages, and the
constants the other xmem imports are called with. docs/xmem_exe_calls.md records them and what is still unmeasured.
…elf on an import's result

From the file alone, per import: the call sites that test the returned value and branch to int3, ud2,
int 29h or a call that does not return, with the value that takes that side. Call sites come from
import_call_sites.py's thunk calls plus `call [rip+slot]` for the api-ms-win-* imports; the result is
tracked through mov/movzx/movsx copies and cdqe, and each side is followed through up to two jumps.

On the retail 1.0.2.1 executable: 515 imports called, 27 with a fatal result check, 78 sites.
docs/exe_fail_fast.md groups them by how main binds each import: XMemVirtualAlloc stops on NULL at 14
of its 23 sites, and the unbound xmem physical-page imports stop on the default return of 0 at 8.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
TreXito and others added 3 commits November 23, 2026 15:47
…sNotRexed#6792)

XMemVirtualAlloc now takes the attribute word and drops the PAGE_GRAPHICS_*
bits (ItsNotRexed#6908). It still refuses the 2 MiB page mode (0x20003000), which 4 of
the 14 fatal sites pass; the other 10 reach VirtualAlloc. The 6 calls refused
for 0x4000/0x40000 are not fatal sites. D3D12ConfigureVirtualMemoryX has a
shim returning S_OK (ItsNotRexed#6792), so it moves from "No binding" to the shims table.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TreXito

TreXito commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Merged main and refreshed docs/exe_fail_fast.md to main 829e5ede. After #6908, XMemVirtualAlloc takes the attribute word and still refuses the 2 MiB page mode (0x20003000). 4 of its 14 fatal sites pass that mode; the other 10 pass 64 KiB forms that reach VirtualAlloc. D3D12ConfigureVirtualMemoryX is bound since #6792 and returns S_OK, so it moved to the shims table. tests/test_import_fail_fast.py and tests/test_docs_repo_paths.py pass.

@dcastell1

Copy link
Copy Markdown
Contributor

Code read + CPU tests, b721585: pass (tests/test_import_fail_fast.py → 8 passed, 27 subtests).

I also ran the tool against the retail image, and the PR's numbers reproduce exactly:

515 import(s) called, 27 with a fatal result check, 78 fatal site(s)

It holds up under the JSON too (587 import entries, 515 with calls > 0, 27 with a non-empty fatal, 78 sites). Heaviest DLLs by fatal sites: xmem.dll 7, MSVCP140.dll 3, then api-ms-win-core-file-l1-1-0, api-ms-win-core-synch-l1-1-0 and api-ms-win-crt-string-l1-1-0 with 2 each.

One usability note: --json emits {"image_base", "imports": {dll!name: {calls, fatal: [...]}}} while the text mode says "515 import(s) called" and the JSON has 587 entries. That is correct once you filter calls > 0, but anyone scripting it will trip over it; one line in the docstring would save that.

@Jadiac5

Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 4440b6f on Windows 11, AMD Radeon RX 9070 XT (RDNA4), driver 32.0.32029.2010: pass (CPU only, no GPU)

  • Python 3.11.2 (the PR's runs were 3.13 and 3.14), python -I. Nothing to build.
  • tests/test_import_fail_fast.py: 8 tests OK. tests/test_import_call_sites.py OK (8), tests/test_gdk_runtime_calls.py OK (23), tests/test_ci_registration.py OK (14), all 11 tests/test_docs_*.py OK, python -m compileall -q make_game.py tools tests OK, scripts/check_no_game_data.py exit 0.
  • Negative control: four one-line mutations of tools/import_fail_fast.py, each on its own, each makes the test fail: jump-following off (2 failures), int 29h off (3), cdqe off (2), copy tracking off (3). Restored afterwards, test OK again.
  • Static check with the repo tool on my own copy of the executable file (read only, never run or mapped): 515 imports called, 27 with a fatal result check, 78 sites. This matches the description. Run time about 2 s.
  • git merge-tree of the head with current main: clean.
  • Note for anyone who repeats the mutation test: delete __pycache__ between runs. Same-size edits within one second can leave a stale .pyc and give a wrong result. That is a local effect, not a PR problem.
  • Findings: none.
  • Not done: not run against the retail executable (only the static read above). Did not check the per-import grouping in docs/exe_fail_fast.md against main's shims. Did not recheck the 2 sites the description says another tool missed.

AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor

Tested 4440b6f merged with main 8575d36 on Linux/Python 3.12.16: pass

Merge was clean (new tools/import_fail_fast.py, tests/test_import_fail_fast.py, docs/exe_fail_fast.md, one line in docs/README.md). All four files are new, so restoring files from main would only remove the tool; I used two mutations of the tool as the negative control instead.

  • python -I tests/test_import_fail_fast.py: Ran 8 tests, OK. python -I tests/test_import_call_sites.py (the module it builds on): OK.
  • Mutation 1 (fatal_side no longer recognises int3/ud2): Ran 8 tests, FAILED (failures=15).
  • Mutation 2 (the "both sides fatal" check in classify disabled): Ran 8 tests, FAILED (failures=4).
  • Both mutations reverted; the tree matched the PR again.
  • python -I tools/import_fail_fast.py <3-byte file> prints import_fail_fast.py: no MZ header and exits 2.

Diff read: the test image has 24 hand-written call sites (test/cmp forms, register copies, overwritten result, extra call or branch before the test, window limits, tail jump, call [rip+x] outside the address table), so it covers the cases the docstring describes. The figures in docs/exe_fail_fast.md (515 imports called, 27 with a fatal check, 78 sites, the Capstone cross-check, the per-import rows) come from the retail executable and I did not re-run them.

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.

5 participants