OLD-7998: restore old upstream PR 7998 - d3d12_device_calls: count the ID3D12Device slots the retail executable calls, from the file alone #697

Manually merged
github merged 5 commits from gh-pr/697/head into gh-pr/697/base 2026-11-26 09:16:54 +00:00
Owner

@⁠aslan2142 on GitHub · 2026-11-24 12:17 UTC · original
aslan2142:rt566-d3d12-device-calls → main · +1048 −1 · 4 files · 5 commits · merged 2026-11-25 22:22 UTC

Claimed in the coordination issue as: OLD-7998 #14

Restoration of my original pull request, old upstream PR 7998 in ItsNotRexed/Leonida, which was closed unmerged when that repository went private on 2026-11-23 (old numbers link nowhere now). Rebased onto main at 21625bca8c; the docs/README.md conflict is resolved by keeping main's index and extending its retail_boot_chain.md row with this tool, and the old pull request numbers in the docs, test comments and commit messages are now plain text. The code is unchanged.

What and why

  • New tools/d3d12_device_calls.py (standard library, python -I). It counts which ID3D12Device vtable slots the executable calls and how often. It reads only the file; nothing is run.
  • It follows the out-pointer of D3D12XboxCreateDevice / D3D12CreateDevice to the globals and stack slots that hold the device. Then it counts the call [vtable+disp], call reg and verified-dispatch (method in rax) sites for each slot.
  • For each slot it also records:
    • the call and tail-jump counts;
    • which register holds the device (rcx, or rdx for the console's return-buffer form);
    • an estimated argument count.
  • It walks forward branches inside a function. A jmp inside the walked range is a branch, not a tail call.
  • It counts what it cannot follow and never guesses a slot for it:
    • vtable loads with no matching call;
    • calls to targets that are not verified dispatch routines;
    • the device passed to another function or stored to non-stack memory;
    • creator out-pointers it cannot follow.
  • The text report holds only slot numbers and counts. The JSON also holds each site's RVA for local follow-up. --out is required.
  • docs/retail_boot_chain.md §5 describes the method, its limits and the census.
  • Census on the 1.0.2.1 executable:
    • 1 creator call site;
    • 95 sites across 42 slots: 94 calls and 1 tail jump;
    • 12 sites not resolved.
  • A slot with no row is not shown to be uncalled.

Tested

  • Linux, Python 3.14.10, CPU only. The tests use synthetic, hand-built PE fixtures; no game files are needed.
  • python -I tests/test_d3d12_device_calls.py:
    • on main: fails at import (ModuleNotFoundError, the tool is missing);
    • on this branch: 15 of 15 pass.
  • python -m compileall -q tools tests: clean.
  • tests/test_docs_*.py and tests/test_text_write_line_endings.py: 56 tests in 13 files pass, the same as on main.
  • git diff --check: clean.
  • python -I scripts/check_no_game_data.py on the four changed files: clean.

GPUs tested: n/a

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠aslan2142** on GitHub · 2026-11-24 12:17 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/697) `aslan2142:rt566-d3d12-device-calls` → `main` · +1048 −1 · 4 files · 5 commits · merged 2026-11-25 22:22 UTC Claimed in the coordination issue as: OLD-7998 https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14 Restoration of my original pull request, old upstream PR 7998 in `ItsNotRexed/Leonida`, which was closed unmerged when that repository went private on 2026-11-23 (old numbers link nowhere now). Rebased onto main at 21625bca8c675c62f9dbffc318ce6a59d57faf31; the `docs/README.md` conflict is resolved by keeping main's index and extending its `retail_boot_chain.md` row with this tool, and the old pull request numbers in the docs, test comments and commit messages are now plain text. The code is unchanged. ### What and why - New `tools/d3d12_device_calls.py` (standard library, `python -I`). It counts which `ID3D12Device` vtable slots the executable calls and how often. It reads only the file; nothing is run. - It follows the out-pointer of `D3D12XboxCreateDevice` / `D3D12CreateDevice` to the globals and stack slots that hold the device. Then it counts the `call [vtable+disp]`, `call reg` and verified-dispatch (`method in rax`) sites for each slot. - For each slot it also records: - the call and tail-jump counts; - which register holds the device (`rcx`, or `rdx` for the console's return-buffer form); - an estimated argument count. - It walks forward branches inside a function. A `jmp` inside the walked range is a branch, not a tail call. - It counts what it cannot follow and never guesses a slot for it: - vtable loads with no matching call; - calls to targets that are not verified dispatch routines; - the device passed to another function or stored to non-stack memory; - creator out-pointers it cannot follow. - The text report holds only slot numbers and counts. The JSON also holds each site's RVA for local follow-up. `--out` is required. - `docs/retail_boot_chain.md` §5 describes the method, its limits and the census. - Census on the 1.0.2.1 executable: - 1 creator call site; - 95 sites across 42 slots: 94 calls and 1 tail jump; - 12 sites not resolved. - A slot with no row is not shown to be uncalled. ### Tested - Linux, Python 3.14.10, CPU only. The tests use synthetic, hand-built PE fixtures; no game files are needed. - `python -I tests/test_d3d12_device_calls.py`: - on main: fails at import (`ModuleNotFoundError`, the tool is missing); - on this branch: 15 of 15 pass. - `python -m compileall -q tools tests`: clean. - `tests/test_docs_*.py` and `tests/test_text_write_line_endings.py`: 56 tests in 13 files pass, the same as on main. - `git diff --check`: clean. - `python -I scripts/check_no_game_data.py` on the four changed files: clean. GPUs tested: n/a AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. AI-assisted (Claude Code, Claude Opus 5.5).
tools/d3d12_device_calls.py follows D3D12XboxCreateDevice's (or D3D12CreateDevice's) out-pointer to
the globals and stack slots that hold the device, and counts per vtable slot the `mov v,[device]` then
`call [v+disp]` / `call reg` / dispatch-with-rax sites, with the register that holds the device (rcx,
or rdx for the console's return-buffer form) and an argument-count estimate. What it can't follow
(vtable loads with no matched call, the device handed to other functions or stored to non-stack
memory, creator out-pointers it can't follow) is counted, not guessed. Slots 0-43 carry the PC
method names; past 43 only the slots the repository has measured are named.

The text report holds slot numbers and counts only; --out is required. Tested on a hand-built PE
(tests/test_d3d12_device_calls.py); docs/retail_boot_chain.md §5 has the method and its limits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A direct call with a method pointer left in rax was counted as that slot whatever the call reached,
so a helper that only returns made a slot appear (shdann's repro on old upstream PR 7998). The target, or for
`call [rip+p]` the code the file's pointer p holds, must now reach `jmp rax` or `call rax` within 16
instructions without writing rax, calling or returning first (conditional branches allowed): the CFG
no-op dispatch and a guard check that ends in `jmp rax`. Verified targets are cached. Any other
target goes to a new "dispatch calls to unverified targets" count, never a slot.

The fixture's GUARD pointer now holds a `jmp rax` stub; new functions call a guard-check stub (slot
counted) and a helper that only returns (not counted, reported as not resolved), and shdann's patch
is a test. The census on the 1.0.2.1 executable is unchanged: all 83 sites are `call [vtable+disp]`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
From the two independent checks on old upstream PR 7998:
- The tool reads the executable only, so a slot called only from a bundled DLL is absent. Slot 76 is
  the example: docs/d3d12_vulkan.md records the FidelityFX upscaler DLL calling it, so its absence
  is not "unused by the title" (TheAirborneCoder).
- A credited table of TreXito's independent static read for slots 49, 50, 55, 57 and 81, with
  matching site counts. These are their reads, not the tool's.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Every E9 was a tail call, including the `jmp rel32` MSVC emits for a long branch inside a function.
With the device in an argument register, such a jump counted as the device passed to another call,
it cleared the state, and it ended a window walk early (YordiLorenzo, pr-sorter). Slot 14 dropped out
of the fixture with pr-sorter's patch.

The walk now follows forward branches. A `jmp` (E9 or EB) or a conditional branch to a later
address in the walked range carries the known state to its target. There it is met with the
fall-through's state, keeping a fact only when both paths agree (vtable loads from either path are
merged). Past a `ret` or unconditional jump, the carried state replaces it. A jump back into the range
(a loop) carries nothing and isn't a tail call. Only a `jmp` out of the range is a tail call.

pr-sorter's repro is a test, together with the short form and a backward jump.

Census on the 1.0.2.1 executable: 83 -> 95 sites, still 42 slots, with no site or slot lost. 12 more
sites are behind a conditional branch around an early return: slot 17 (+5), slot 24 (+5), slot 133
(+2). Not resolved stays at 12: vtable loads with no matched call 2 -> 1, calls handed the device
9 -> 10, stores 1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
BatSmacker84 on old upstream PR 7998:
- The summary line now separates indirect vtable calls, indirect vtable tail jumps, and sites
  through a verified dispatch routine. The JSON gains calls, tail_jumps and dispatch_sites. On the
  1.0.2.1 executable: 95 sites = 94 calls + 1 tail jump (slot 2), 0 through a dispatch routine.
- "lists every" and "lower bound" are gone from the docstring, the help text and §5. The tool reports
  the statically resolved sites for the device paths it follows; a slot without a row isn't shown
  to be uncalled.

tupacfan04's secondary points from the review of the dispatch-routine commit, still open until now:
- A creator out-pointer in an object member was labelled "memory slot", yet it was dropped at the
  next call. It is now "object member (followed until the next call)"; a stack slot is "stack slot".
- The no-.pdata window walk is tested, and it now goes on past a ret while a forward branch target
  is still ahead.
- Conditional branches have tests: the device carried past an early return, and a join where only
  one path holds the device.

Census unchanged: 95 sites across 42 slots, 12 not resolved. 15 tests pass.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:16:53 +00:00
github manually merged commit 0a5c8d56eb into gh-pr/697/base 2026-11-26 09:16:54 +00:00
Author
Owner

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

Reviewed and verified OLD-7998 restoration. The tool ools/d3d12_device_calls.py correctly conducts static inspection of ID3D12Device vtable dispatch call sites from PE headers and bytecode without loading or executing the binary. All synthetic unit tests in ests/test_d3d12_device_calls.py adhere strictly to standard library requirements and clean-room constraints. Documentation update in docs/retail_boot_chain.md matches measured behavior. LGTM.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 12:20 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/697#pullrequestreview-6039082922) · **approved these changes** Reviewed and verified OLD-7998 restoration. The tool ools/d3d12_device_calls.py correctly conducts static inspection of ID3D12Device vtable dispatch call sites from PE headers and bytecode without loading or executing the binary. All synthetic unit tests in ests/test_d3d12_device_calls.py adhere strictly to standard library requirements and clean-room constraints. Documentation update in docs/retail_boot_chain.md matches measured behavior. LGTM.
Author
Owner

@⁠aslan2142 on GitHub · 2026-11-25 22:22 UTC · original

Merged: d3d12_device_calls counts the ID3D12Device slots the retail executable calls, from the file alone.

  • Changed since review: Nothing. It merges cleanly with main, and no review point is open.
  • Tested: tests/test_d3d12_device_calls.py passes on main + this head (Linux, CPython 3.14).
  • Checked on main 83f791ae4 + 0a5c8d56e: the semantic suite passes (492 files, 6,315 tests, 0 files failed); test_d3d12_device_calls pass; the 15 docs and writer tests pass; check_no_game_data.py is clean on the 4 changed files.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠aslan2142** on GitHub · 2026-11-25 22:22 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/697#issuecomment-7176284987) Merged: `d3d12_device_calls` counts the ID3D12Device slots the retail executable calls, from the file alone. - **Changed since review:** Nothing. It merges cleanly with `main`, and no review point is open. - **Tested:** `tests/test_d3d12_device_calls.py` passes on `main` + this head (Linux, CPython 3.14). - **Checked on `main` `83f791ae4` + `0a5c8d56e`:** the semantic suite passes (492 files, 6,315 tests, 0 files failed); `test_d3d12_device_calls` pass; the 15 docs and writer tests pass; `check_no_game_data.py` is clean on the 4 changed files. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.