d3d12-gpu: name the caller at the heap guard fault, so the CRT copy's writer can be traced (stacked on #3429) #3467

Code

Clone this repository

Checkout

From your project repository, check out a new branch and test the changes.

git fetch -u  "d3d12/fault-caller:KelboLive-d3d12/fault-caller"
git switch "KelboLive-d3d12/fault-caller"
Open
leonida/mandy
KelboLive wants to merge 6 commits from KelboLive/mandy:d3d12/fault-caller into main

This pull request can be merged automatically.

Conversation
Reviewers 0 approvals
  • No reviewers
Details
This pull request can be merged automatically.
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 'd3d12/fault-caller:KelboLive-d3d12/fault-caller'
git switch 'KelboLive-d3d12/fault-caller'
First-time contributor

Stacked on #3429 (Depends on: #3429). Please review only the last commit (3e5e8c29c, 2 files, +89/-12); the diff against main includes #3429's commits.

What/why

#3394's phase-2 result: the biggest writers of missed view records are VCRUNTIME140.dll's copy routine, and the writers list cannot show who called it. The next diagnostic the run asked for is the caller at the fault, not inside the memcpy wrap.

Change

  • The guard VEH (watch.rs on_write) passes the faulting context's Rsp to port_descriptors::faulted(at, rip, stack).
  • caller_at/caller_from: reads the return address at [Rsp], but only after VirtualQuery confirms the word is committed, readable, in one region, and neither guard nor no-access; otherwise 0 (no panic path).
  • Adopted gains a callers array beside writers; missed appends ({writer_name(caller)}) to the writer string only when the caller is nonzero, so the line reads e.g. ucrtbase.dll+0x... (title+0x4d9f... 12).
  • Reuses #3429's writer_name unchanged for both writer and caller; no second naming helper, no writer rewrite.

Tests

  • a_code_address_is_named_by_its_module and an_unreadable_fault_stack_keeps_no_caller (pure caller_from: empty/rejected keeps 0; an accepted local word decodes).
  • 8 port_descriptors tests, 759 serial, clippy -D warnings, no-game-data and diff clean.

Not verified

No game run: that a live guard fault's Rsp points at the title caller of the CRT memcpy rather than another CRT frame, that VirtualQuery accepts the real fault-time stack top in every fault shape, and that the named caller is actionable. A run with LEONIDA_PORT_DESCRIPTORS=1 will show the (caller) next to the VCRUNTIME140 writers.

Item: D3D12-FAULT-CALLER, claimed in #3312. Depends on: #3429.
AI-assisted: DeepSeek V4.1 Flash (OpenCode).

**Stacked on #3429** (Depends on: #3429). Please review only the last commit (`3e5e8c29c`, 2 files, +89/-12); the diff against `main` includes #3429's commits. ## What/why #3394's phase-2 result: the biggest writers of missed view records are `VCRUNTIME140.dll`'s copy routine, and the writers list cannot show **who called it**. The next diagnostic the run asked for is the caller at the fault, not inside the memcpy wrap. ## Change - The guard VEH (`watch.rs` `on_write`) passes the faulting context's `Rsp` to `port_descriptors::faulted(at, rip, stack)`. - `caller_at`/`caller_from`: reads the return address at `[Rsp]`, but only after `VirtualQuery` confirms the word is committed, readable, in one region, and neither guard nor no-access; otherwise 0 (no panic path). - `Adopted` gains a `callers` array beside `writers`; `missed` appends `({writer_name(caller)})` to the writer string only when the caller is nonzero, so the line reads e.g. `ucrtbase.dll+0x... (title+0x4d9f... 12)`. - Reuses #3429's `writer_name` unchanged for both writer and caller; no second naming helper, no writer rewrite. ## Tests - `a_code_address_is_named_by_its_module` and `an_unreadable_fault_stack_keeps_no_caller` (pure `caller_from`: empty/rejected keeps 0; an accepted local word decodes). - 8 `port_descriptors` tests, **759 serial**, clippy `-D warnings`, no-game-data and diff clean. ## Not verified No game run: that a live guard fault's `Rsp` points at the title caller of the CRT `memcpy` rather than another CRT frame, that `VirtualQuery` accepts the real fault-time stack top in every fault shape, and that the named caller is actionable. A run with `LEONIDA_PORT_DESCRIPTORS=1` will show the `(caller)` next to the `VCRUNTIME140` writers. Item: D3D12-FAULT-CALLER, claimed in #3312. Depends on: #3429. AI-assisted: DeepSeek V4.1 Flash (OpenCode).
sample_caller() formatted the frame itself as file+offset, so a caller in the title's image printed as leonida-image-<hash>.dll+0x... while the writers list printed the same image as title+0x.... It still skips frames in our own image, then names the chosen frame with writer_name(): title+0x... in the title's image, module.dll+0x... elsewhere. A frame with no fault on its block, or one whose module will not resolve, is skipped rather than counted. The one-in-64 sampling and sampling-time-only naming are unchanged.
The guard handler records the faulting instruction per 64 KiB block, so a memcpy into the marked view heap names the runtime, not the title code that called it. Pass the faulting frame's stack top to port_descriptors, read [Rsp] only when VirtualQuery says the word is committed and readable (0 otherwise), and store it beside the writer. The writers log and top-six append '(caller)' with #3429's writer_name only when the caller is nonzero.
KelboLive force-pushed d3d12/fault-caller from 3e5e8c29ca to 3587853c72 2026-12-04 15:10:29 +00:00 Compare
Author
First-time contributor

Rebased with its #3429 base onto main 44c480a5f (head 3587853c7). Re-ran: 762 passed, 0 failed, 60 ignored. A read-only review is being applied (filter unresolvable/own-module callers, stack-limit check, region helper and tests) and will arrive as a follow-up commit.

Rebased with its #3429 base onto `main` `44c480a5f` (head `3587853c7`). Re-ran: 762 passed, 0 failed, 60 ignored. A read-only review is being applied (filter unresolvable/own-module callers, stack-limit check, region helper and tests) and will arrive as a follow-up commit.
KelboLive force-pushed d3d12/fault-caller from 3587853c72 to e40c55b94d 2026-12-04 15:27:49 +00:00 Compare
Author
First-time contributor

Review follow-up is in, rebased onto main 2698f6dd1 and its #3429 base (0d6518513); head e40c55b94.

What changed

  • writer_line(writer, caller) is now a pure decision: a zero caller, our own module, or " (module not found)" appends nothing; a named module appends writer (caller). It reuses our_module(), the same skip sample_caller uses.
  • The docs no longer call the captured word "the return address" unqualified: it is the word at the faulting frame's stack top, a return address only when the faulting routine is frameless (the x64 CRT copies are), approximate otherwise, and per 64 KiB block like the writer.
  • caller_at bounds the read to GetCurrentThreadStackLimits before VirtualQuery, so a garbage Rsp is rejected outright and the query→read window is smaller. The region test is a pure, overflow-checked in_region.
  • WRITERS clears at 4096 keys like COPY_CALLERS, and the frame line now says its entries are writer (caller) pairs, since the key space widens.
  • A contributor run found sample_caller printed the title image as leonida-image-<hash>.dll+0x... while writer_name printed title+0x... for the same bytes. Both now go through writer_name; the test pins title+0x0/title+0x40.

Tests: 12 port_descriptors, 780 full serial (0 failed, 60 ignored), clippy -D warnings, switch page, no-game-data and diff clean.

Still a run-side check: that a live guard fault's Rsp lands on the title frame that called the CRT copy, and that the filtered caller is actionable in the VCRUNTIME140 rows.

Review follow-up is in, rebased onto `main` `2698f6dd1` and its #3429 base (`0d6518513`); head **`e40c55b94`**. **What changed** - `writer_line(writer, caller)` is now a pure decision: a zero caller, our own module, or `" (module not found)"` appends nothing; a named module appends `writer (caller)`. It reuses `our_module()`, the same skip `sample_caller` uses. - The docs no longer call the captured word "the return address" unqualified: it is the word at the faulting frame's stack top, a return address only when the faulting routine is frameless (the x64 CRT copies are), approximate otherwise, and per 64 KiB block like the writer. - `caller_at` bounds the read to `GetCurrentThreadStackLimits` before `VirtualQuery`, so a garbage `Rsp` is rejected outright and the query→read window is smaller. The region test is a pure, overflow-checked `in_region`. - `WRITERS` clears at 4096 keys like `COPY_CALLERS`, and the frame line now says its entries are `writer (caller)` pairs, since the key space widens. - A contributor run found `sample_caller` printed the title image as `leonida-image-<hash>.dll+0x...` while `writer_name` printed `title+0x...` for the same bytes. Both now go through `writer_name`; the test pins `title+0x0`/`title+0x40`. **Tests:** 12 `port_descriptors`, **780 full serial** (0 failed, 60 ignored), clippy `-D warnings`, switch page, no-game-data and diff clean. Still a run-side check: that a live guard fault's `Rsp` lands on the title frame that called the CRT copy, and that the filtered caller is actionable in the `VCRUNTIME140` rows.
First-time contributor

RUN — In-game Guard-Fault Caller Probe & Test Gate (NVIDIA RTX 5070 12 GB, Windows 11)

Tested PR head 3587853c72 fast-forwarded cleanly onto main (30c683055), diagnostic release build (-SeriesS, no frame generation, LEONIDA_PORT_DESCRIPTORS=1, 315 s automated run from the same save), compared against main (30c683055):

Metric main (30c683055) PR #3467 (3587853c72, PORT_DESCRIPTORS=1)
World samples (>=1000 draws) 36 (max_draws=5187) 39 (max_draws=5461)
Median FPS / frame time 23.0 fps (43.5 ms) 24.3 fps (41.1 ms)
Slowest 10% / fastest 10% 13.7 / 25.0 fps 15.3 / 25.6 fps
3000-4999 draws/frame median 23.2 fps (28 samples) 24.4 fps (32 samples)
Errors / device lost 0 / 0 0 / 0

Key Finding — What Calls VCRUNTIME140.dll at the Guard Fault (#3394)

The new caller field immediately solved the VCRUNTIME140.dll mystery:

  • Across the 315 s run (5,561 missed view records total; 4,016 wrapped memcpy/memmove calls into the heap per frame, 0 records marked, 0 off-grid view tags), all four top VCRUNTIME140.dll writer entries (757 + 349 + 343 + 293 = 1,742 in the cumulative top-6, and 1,496 across 8 VCRUNTIME140.dll sites in the logged warnings) resolve to the exact same single caller inside leonida.exe.
  • Symbolizing that RVA against the build's PDB resolves directly to d3d12/crates/leonida-gpu/src/port_descriptors.rs:543 — i.e. our own port_descriptors::copy → copied() → real(to, from, n) wrapper around the title's memcpy!
  • Why port_descriptors::copy shows up as the top missed writer despite marking 0 view records (and 0 off-grid tags): port_descriptors::faulted() (port_descriptors.rs:322) records a single writer/caller per 64 KiB block (off >> 16, 2,048 slots / 16 pages). Because the title issues ~4,000 non-view memcpy calls per frame into the heap through port_descriptors::copy, those VCRUNTIME140.dll faults overwrite a.writers[block] in the same 64 KiB block and mask the actual unwatched view-record writer before the fallback scans the page.
  • Among the warnings not overwritten by port_descriptors::copy, the remaining attributed writers are:
    • One unmapped executable page (module not found, 912 warnings; two fixed offsets on the page — consistent with a dynamically allocated stub/trampoline).
    • Three distinct sites inside the title's image (268, 262, and 65 warnings).

This suggests two immediate follow-ups for #3394:

  1. In faulted() (or while inside port_descriptors::copied), avoid overwriting a.writers[block] when the fault is caused by port_descriptors::copy/shift (since copied() already scans the entire copied range for view records), and/or narrow writer tracking from 64 KiB to 4 KiB pages so non-view memcpy traffic stops masking the true writers.
  2. Check whether the unmapped executable page (912 warnings) is one of our own JIT/trampoline/stub allocations (e.g. console_insn or port_descriptors patched-store stubs).

Unit & Repo Checks

  • cargo test -p leonida-gpu --lib --locked port_descriptors — 8/8 PASS
  • cargo clippy -p leonida-gpu --all-targets --locked -- -D warnings — 0 warnings
  • python -I scripts/check_no_game_data.py — PASS
  • python -I tools/d3d12_switches.py --check d3d12/docs/switches.md — FAIL (exit 1): missing from the page: LEONIDA_WAIT_TEST_CHILD (from the list.rs test commit in the stack)

AI-assisted.

### RUN — In-game Guard-Fault Caller Probe & Test Gate (NVIDIA RTX 5070 12 GB, Windows 11) Tested PR head `3587853c72` fast-forwarded cleanly onto `main` (`30c683055`), diagnostic release build (`-SeriesS`, no frame generation, `LEONIDA_PORT_DESCRIPTORS=1`, 315 s automated run from the same save), compared against `main` (`30c683055`): | Metric | `main` (`30c683055`) | PR `#3467` (`3587853c72`, `PORT_DESCRIPTORS=1`) | | --- | --- | --- | | World samples (`>=1000` draws) | `36` (`max_draws=5187`) | `39` (`max_draws=5461`) | | Median FPS / frame time | `23.0 fps` (`43.5 ms`) | `24.3 fps` (`41.1 ms`) | | Slowest 10% / fastest 10% | `13.7` / `25.0 fps` | `15.3` / `25.6 fps` | | `3000-4999` draws/frame median | `23.2 fps` (`28` samples) | `24.4 fps` (`32` samples) | | Errors / device lost | `0` / `0` | `0` / `0` | ### Key Finding — What Calls `VCRUNTIME140.dll` at the Guard Fault (#3394) The new caller field immediately solved the `VCRUNTIME140.dll` mystery: - Across the 315 s run (`5,561` missed view records total; `4,016` wrapped `memcpy`/`memmove` calls into the heap per frame, **`0` records marked**, **`0` off-grid view tags**), **all four top `VCRUNTIME140.dll` writer entries** (`757 + 349 + 343 + 293 = 1,742` in the cumulative top-6, and `1,496` across 8 `VCRUNTIME140.dll` sites in the logged warnings) resolve to the **exact same single caller inside `leonida.exe`**. - Symbolizing that RVA against the build's PDB resolves directly to **`d3d12/crates/leonida-gpu/src/port_descriptors.rs:543`** — i.e. our own **`port_descriptors::copy` → `copied()` → `real(to, from, n)`** wrapper around the title's `memcpy`! - **Why `port_descriptors::copy` shows up as the top `missed` writer despite marking `0` view records (and `0` off-grid tags):** `port_descriptors::faulted()` (`port_descriptors.rs:322`) records a single `writer`/`caller` per **64 KiB block** (`off >> 16`, 2,048 slots / 16 pages). Because the title issues ~4,000 non-view `memcpy` calls per frame into the heap through `port_descriptors::copy`, those `VCRUNTIME140.dll` faults overwrite `a.writers[block]` in the same 64 KiB block and mask the actual unwatched view-record writer before the fallback scans the page. - Among the warnings not overwritten by `port_descriptors::copy`, the remaining attributed writers are: - One unmapped executable page (`module not found`, `912` warnings; two fixed offsets on the page — consistent with a dynamically allocated stub/trampoline). - Three distinct sites inside the title's image (`268`, `262`, and `65` warnings). This suggests two immediate follow-ups for #3394: 1. In `faulted()` (or while inside `port_descriptors::copied`), avoid overwriting `a.writers[block]` when the fault is caused by `port_descriptors::copy`/`shift` (since `copied()` already scans the entire copied range for view records), and/or narrow writer tracking from 64 KiB to 4 KiB pages so non-view `memcpy` traffic stops masking the true writers. 2. Check whether the unmapped executable page (`912` warnings) is one of our own JIT/trampoline/stub allocations (e.g. `console_insn` or `port_descriptors` patched-store stubs). ### Unit & Repo Checks - `cargo test -p leonida-gpu --lib --locked port_descriptors` — `8/8` PASS - `cargo clippy -p leonida-gpu --all-targets --locked -- -D warnings` — `0` warnings - `python -I scripts/check_no_game_data.py` — PASS - `python -I tools/d3d12_switches.py --check d3d12/docs/switches.md` — **FAIL** (exit `1`): `missing from the page: LEONIDA_WAIT_TEST_CHILD` (from the `list.rs` test commit in the stack) AI-assisted.
KelboLive force-pushed d3d12/fault-caller from e40c55b94d to 1d2c3651d5 2026-12-04 15:57:02 +00:00 Compare
Author
First-time contributor

Rebased onto main bba262b94 + its #3429 base (e842ed15a); head 1d2c3651d. Re-ran: 785 passed, 0 failed, 60 ignored; 12 port_descriptors; clippy, switch page, no-game-data and diff clean.

Rebased onto `main` `bba262b94` + its #3429 base (`e842ed15a`); head **`1d2c3651d`**. Re-ran: 785 passed, 0 failed, 60 ignored; 12 `port_descriptors`; clippy, switch page, no-game-data and diff clean.
KelboLive force-pushed d3d12/fault-caller from 1d2c3651d5 to 34fee83da1 2026-12-04 16:15:51 +00:00 Compare
Author
First-time contributor

Rebased onto main e1be896b2 + heap cdf83ebf1 (head 34fee83da). 785 passed, 0 failed, 60 ignored; 12 port_descriptors; clippy/switch/no-game-data/diff clean.

Rebased onto `main` `e1be896b2` + heap `cdf83ebf1` (head `34fee83da`). 785 passed, 0 failed, 60 ignored; 12 `port_descriptors`; clippy/switch/no-game-data/diff clean.
KelboLive force-pushed d3d12/fault-caller from 34fee83da1 to cb6c119466 2026-12-04 16:32:27 +00:00 Compare
Author
First-time contributor

Rebased onto main 00e245be5 + heap b37c53ba5 (head cb6c11946). 795 passed, 0 failed, 60 ignored; 12 port_descriptors; checks clean.

Rebased onto `main` `00e245be5` + heap `b37c53ba5` (head `cb6c11946`). 795 passed, 0 failed, 60 ignored; 12 `port_descriptors`; checks clean.
Contributor

The synthetic fixture suggested in #3429 could also exercise caller attribution here. Route identical descriptor writes from two dummy modules through the same copy routine, and include a case with no readable caller. Compare per-caller class counts against the generated ground truth and retain an explicit unknown bucket. Use the same input sequence and measurement window for both checks.

AI-assisted with OpenAI Codex.

The synthetic fixture suggested in #3429 could also exercise caller attribution here. Route identical descriptor writes from two dummy modules through the same copy routine, and include a case with no readable caller. Compare per-caller class counts against the generated ground truth and retain an explicit unknown bucket. Use the same input sequence and measurement window for both checks. AI-assisted with OpenAI Codex.
KelboLive force-pushed d3d12/fault-caller from cb6c119466 to 5e2d23916f 2026-12-04 17:15:08 +00:00 Compare
Author
First-time contributor

Rebased onto main f22b9fa64 + heap 359832fb1 (head 5e2d23916). 798 passed, 0 failed, 60 ignored; 12 port_descriptors.

Rebased onto `main` `f22b9fa64` + heap `359832fb1` (head `5e2d23916`). 798 passed, 0 failed, 60 ignored; 12 `port_descriptors`.
KelboLive force-pushed d3d12/fault-caller from 5e2d23916f to c87825d2e1 2026-12-04 18:30:16 +00:00 Compare
Author
First-time contributor

Rebased onto main 6491bc1a1 + heap d20591eb7 (head c87825d2e). 798 passed, 0 failed, 60 ignored; 12 port_descriptors.

Rebased onto `main` `6491bc1a1` + heap `d20591eb7` (head `c87825d2e`). 798 passed, 0 failed, 60 ignored; 12 `port_descriptors`.
KelboLive force-pushed d3d12/fault-caller from c87825d2e1 to b6e02f72a9 2026-12-04 18:44:09 +00:00 Compare
Author
First-time contributor

Rebased onto main 6b0498d37 + heap cd2cba27f (head b6e02f72a). 798 passed, 0 failed, 60 ignored.

Rebased onto `main` `6b0498d37` + heap `cd2cba27f` (head `b6e02f72a`). 798 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/fault-caller from b6e02f72a9 to 575c0f0520 2026-12-04 20:52:50 +00:00 Compare
Author
First-time contributor

Rebased onto main 0036195a7 + heap 578a94485 (head 575c0f052). 798 passed, 0 failed, 60 ignored.

Rebased onto `main` `0036195a7` + heap `578a94485` (head `575c0f052`). 798 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/fault-caller from 575c0f0520 to dc1538b8e7 2026-12-04 23:20:16 +00:00 Compare
Author
First-time contributor

Rebased onto main f4b0404e9 + heap 5fd5290e9 (head dc1538b8e). 798 passed, 0 failed, 60 ignored.

Rebased onto `main` `f4b0404e9` + heap `5fd5290e9` (head `dc1538b8e`). 798 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/fault-caller from dc1538b8e7 to 29369a5f67 2026-12-05 05:34:42 +00:00 Compare
Author
First-time contributor

Rebased onto main 93b9d90a2 + heap 0cb08a735 (head 29369a5f6). 798 passed, 0 failed, 60 ignored.

Rebased onto `main` `93b9d90a2` + heap `0cb08a735` (head `29369a5f6`). 798 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/fault-caller from 29369a5f67 to 6e61aaf0e8 2026-12-05 08:44:21 +00:00 Compare
Author
First-time contributor

Rebased onto main c5355dd25 + heap 400c011e1 (head 6e61aaf0e). 799 passed, 0 failed, 60 ignored.

Rebased onto `main` `c5355dd25` + heap `400c011e1` (head `6e61aaf0e`). 799 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/fault-caller from 6e61aaf0e8 to 2b1dfe5fed 2026-12-05 08:53:04 +00:00 Compare
Author
First-time contributor

Rebased onto main e1b0248c1 + heap 8e6be47bd (head 2b1dfe5fe). 799 passed, 0 failed, 60 ignored.

Rebased onto `main` `e1b0248c1` + heap `8e6be47bd` (head `2b1dfe5fe`). 799 passed, 0 failed, 60 ignored.
KelboLive force-pushed d3d12/fault-caller from 2b1dfe5fed to d6219b9e75 2026-12-05 09:59:36 +00:00 Compare
Author
First-time contributor

Rebased onto main d99bb4d7a + heap 0f323d533 (head d6219b9e7). 802 passed, 0 failed, 60 ignored.

Rebased onto `main` `d99bb4d7a` + heap `0f323d533` (head `d6219b9e7`). 802 passed, 0 failed, 60 ignored.
Sign in to join this conversation.
No description provided.