d3d12_device_calls: forget a slot a store or a call may have replaced (D3D12-DEVICE-CALLS-STALE-SLOT) #2343

Merged
syfyra merged 1 commit from upstream-pr/2343 into main 2026-12-02 13:06:00 +00:00
syfyra commented 2026-11-26 14:12:47 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by syfyra on 2026-11-26T14:12:47Z.


Claimed in the coordination issue as: D3D12-DEVICE-CALLS-STALE-SLOT (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7194420859)

What and why

tools/d3d12_device_calls.py promises statically resolved device sites and to report the rest as not resolved, "never guessing a slot"; under-counting is the accepted direction ("a slot can be called more often than the report says"). Its walk keeps a [reg+disp] slot that held the device (stack slots survive calls) and dropped it only on a plain store to the slot's exact address. Two ways a slot gets another object were missed, and the next call through the slot was then reported as a device slot:

  1. A call given the slot's address. MSVC reuses frame slots, so the device spilled to [rsp+40h] can share the slot with an out variable:

    mov  [rsp+40h],rcx          ; device spilled
    lea  r9,[rsp+40h]           ; &queue: the same slot
    call [rax+40h]              ; device slot 8, CreateCommandQueue
    mov  rcx,[rsp+40h]          ; the queue
    mov  rax,[rcx]
    call [rax+30h]              ; queue->SetName, reported as device slot 6
    

    The slots whose addresses are passed in rcx/rdx/r8/r9 are now forgotten at the call. A creator's out slot is still added back after it, so the existing out-pointer tracking is unchanged.

  2. An SSE/AVX store over the slot (movups [rsp+40h],xmm0, how struct copies and zeroing are compiled), or any store that covers only part of it (mov dword [rsp+44h],0). Every store now ends the slots whose bytes it touches. The vector stores (0F 11/29/2B/7F/E7/13/17/D6, movnti, and their VEX/EVEX forms) take their widest size, 16 bytes and 32 under VEX, because the decoder keeps neither the prefix nor VEX.L. Forgetting a slot only loses a site, while keeping one wrongly invents a site.

A store or out-pointer next to the slot leaves it alone (tested both ways). The docstring's walk description says so.

Different topic from #2262 (--out validation and atomic reports, same file); the two merge cleanly.

Tested

Windows 11, Python 3.14.7, the test file's synthetic PE fixture only:

  • python -I tests/test_d3d12_device_calls.py: 17 tests OK. Two new tests run a function that spills the device to [rsp+40h], does one thing, reloads the slot and calls its slot 6:

    • a plain store, movups at the slot, movups at rsp+38h, vmovdqu ymm at rsp+30h and a 4-byte store into the upper half all end the slot;
    • movups at rsp+48h and a store at rsp+38h keep it;
    • an out-pointer to the slot ends it, while one to rsp+48h keeps it (slot 8 is counted in both).

    On main the five overwriting cases fail and the keeping ones pass.

  • tests/test_gdk_runtime_calls.py OK; python -m compileall; scripts/check_no_game_data.py on both files.

Not measured: how many sites this removes from a real executable (no game files here).

GPUs tested: n/a (CPU-only tool)

AI-assisted: yes (Claude Code, Claude Opus 5.5)

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. (n/a: no translator change)

🤖 Generated with Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5511587687 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2343). Originally posted by [syfyra](https://github.com/syfyra) on 2026-11-26T14:12:47Z. --- Claimed in the coordination issue as: D3D12-DEVICE-CALLS-STALE-SLOT (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7194420859) ### What and why `tools/d3d12_device_calls.py` promises statically resolved device sites and to report the rest as not resolved, "never guessing a slot"; under-counting is the accepted direction ("a slot can be called more often than the report says"). Its walk keeps a `[reg+disp]` slot that held the device (stack slots survive calls) and dropped it only on a plain store to the slot's exact address. Two ways a slot gets another object were missed, and the next call through the slot was then reported as a device slot: 1. **A call given the slot's address.** MSVC reuses frame slots, so the device spilled to `[rsp+40h]` can share the slot with an out variable: mov [rsp+40h],rcx ; device spilled lea r9,[rsp+40h] ; &queue: the same slot call [rax+40h] ; device slot 8, CreateCommandQueue mov rcx,[rsp+40h] ; the queue mov rax,[rcx] call [rax+30h] ; queue->SetName, reported as device slot 6 The slots whose addresses are passed in rcx/rdx/r8/r9 are now forgotten at the call. A creator's out slot is still added back after it, so the existing out-pointer tracking is unchanged. 2. **An SSE/AVX store over the slot** (`movups [rsp+40h],xmm0`, how struct copies and zeroing are compiled), or any store that covers only part of it (`mov dword [rsp+44h],0`). Every store now ends the slots whose bytes it touches. The vector stores (0F 11/29/2B/7F/E7/13/17/D6, movnti, and their VEX/EVEX forms) take their widest size, 16 bytes and 32 under VEX, because the decoder keeps neither the prefix nor VEX.L. Forgetting a slot only loses a site, while keeping one wrongly invents a site. A store or out-pointer next to the slot leaves it alone (tested both ways). The docstring's walk description says so. Different topic from #2262 (`--out` validation and atomic reports, same file); the two merge cleanly. ### Tested Windows 11, Python 3.14.7, the test file's synthetic PE fixture only: - `python -I tests/test_d3d12_device_calls.py`: 17 tests OK. Two new tests run a function that spills the device to `[rsp+40h]`, does one thing, reloads the slot and calls its slot 6: - a plain store, `movups` at the slot, `movups` at `rsp+38h`, `vmovdqu ymm` at `rsp+30h` and a 4-byte store into the upper half all end the slot; - `movups` at `rsp+48h` and a store at `rsp+38h` keep it; - an out-pointer to the slot ends it, while one to `rsp+48h` keeps it (slot 8 is counted in both). On `main` the five overwriting cases fail and the keeping ones pass. - `tests/test_gdk_runtime_calls.py` OK; `python -m compileall`; `scripts/check_no_game_data.py` on both files. Not measured: how many sites this removes from a real executable (no game files here). GPUs tested: n/a (CPU-only tool) AI-assisted: yes (Claude Code, Claude Opus 5.5) ### 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. (n/a: no translator change) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
lixonik commented 2026-11-26 15:40:28 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-26T15:40:28Z.

Review: COMMENTED


Windows 11: 17/17 device-calls tests passed. A synthetic PE with a ZMM-width store still produces a false call-site attribution. This was a static scan of authored bytes. Details inline.

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6076918914 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2343#pullrequestreview-6076918914). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-26T15:40:28Z. **Review:** COMMENTED --- Windows 11: 17/17 device-calls tests passed. A synthetic PE with a ZMM-width store still produces a false call-site attribution. This was a static scan of authored bytes. Details inline. AI-assisted.
lixonik commented 2026-11-26 15:40:28 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-11-26T15:40:28Z.

Code review location: tools/d3d12_device_calls.py; line 72

@@ -63,6 +64,12 @@
 import sys
 
 HERE = os.path.dirname(os.path.abspath(__file__))
+# SSE/AVX stores to memory (op as tools/gdk_runtime_calls.py numbers them) and the bytes they write. The decoder keeps
+# neither the prefix nor VEX.L, so each takes its widest legacy form (movups over movss, 16 bytes) and 32 bytes
+# under VEX/EVEX: a slot is better forgotten than kept after a store that may have replaced it.
+_LEGACY_STORES = {0x11: 16, 0x29: 16, 0x2B: 16, 0x7F: 16, 0xE7: 16, 0x13: 8, 0x17: 8, 0xD6: 8, 0xC3: 8}
+VECTOR_STORES = {**{0x100 | b: n for b, n in _LEGACY_STORES.items()},
+                 **{0x1100 | b: 32 for b in _LEGACY_STORES if b != 0xC3}}

[P2] Account for 64-byte ZMM stores on the EVEX path.

In the PR's spilled_device fixture, vmovups %zmm0, 0x20(%rsp) (64 bytes, disp32) overwrites the device spill at [rsp+0x40]. The scanner still reports slot 6 because the recorded width ends exactly before that slot. Preserve EVEX width in the decoder or conservatively account for a 64-byte store when the width is unavailable.

<!-- greyfinch-sync:ubiquitarianism/leonida:review-comment:4710578014 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/2343#discussion_r4710578014). Originally posted by [lixonik](https://github.com/lixonik) on 2026-11-26T15:40:28Z. **Code review location:** `tools/d3d12_device_calls.py`; line 72 ~~~~diff @@ -63,6 +64,12 @@ import sys HERE = os.path.dirname(os.path.abspath(__file__)) +# SSE/AVX stores to memory (op as tools/gdk_runtime_calls.py numbers them) and the bytes they write. The decoder keeps +# neither the prefix nor VEX.L, so each takes its widest legacy form (movups over movss, 16 bytes) and 32 bytes +# under VEX/EVEX: a slot is better forgotten than kept after a store that may have replaced it. +_LEGACY_STORES = {0x11: 16, 0x29: 16, 0x2B: 16, 0x7F: 16, 0xE7: 16, 0x13: 8, 0x17: 8, 0xD6: 8, 0xC3: 8} +VECTOR_STORES = {**{0x100 | b: n for b, n in _LEGACY_STORES.items()}, + **{0x1100 | b: 32 for b in _LEGACY_STORES if b != 0xC3}} ~~~~ --- [P2] Account for 64-byte ZMM stores on the EVEX path. In the PR's `spilled_device` fixture, `vmovups %zmm0, 0x20(%rsp)` (64 bytes, disp32) overwrites the device spill at `[rsp+0x40]`. The scanner still reports slot 6 because the recorded width ends exactly before that slot. Preserve EVEX width in the decoder or conservatively account for a 64-byte store when the width is unavailable.
aslan2142 commented 2026-12-01 12:02:47 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T12:02:47Z.

Review: COMMENTED


Draft review against current main: invalidating tracked slots overwritten by a store or call in tools/d3d12_device_calls.py with tests in tests/test_d3d12_device_calls.py looks clean and clean-room verified. Still Draft with no CI run yet — please mark Ready for Review so CI runs green.

(AI-assisted review.)

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6130888865 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2343#pullrequestreview-6130888865). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T12:02:47Z. **Review:** COMMENTED --- Draft review against current `main`: invalidating tracked slots overwritten by a store or call in `tools/d3d12_device_calls.py` with tests in `tests/test_d3d12_device_calls.py` looks clean and clean-room verified. Still Draft with no CI run yet — please mark Ready for Review so CI runs green. *(AI-assisted review.)*
aslan2142 commented 2026-12-01 12:30:26 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T12:30:26Z.


Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review.

(AI-assisted triage.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7311229158 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2343#issuecomment-7311229158). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T12:30:26Z. --- Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review. *(AI-assisted triage.)*
aslan2142 commented 2026-12-01 13:01:09 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T13:01:09Z.

Review: CHANGES_REQUESTED


Now ready for review, requesting changes before merge:

  • EVEX 64-byte store width bug (tools/d3d12_device_calls.py:72): as reproduced by @lixonik, an EVEX 64-byte ZMM store (vmovups %zmm0, 0x20(%rsp)) overwrites the spilled device slot at [rsp+0x40], yet the scanner still attributes slot 6 because the recorded store width stops short of 0x40. Please preserve EVEX vector width in the decoder (or conservatively invalidate 64 bytes for EVEX stores when width is unavailable) and add a regression test.
  • CI: no CI runs on the current head yet; please get fresh green CI after fixing.

(AI-assisted review.)

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6131752907 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2343#pullrequestreview-6131752907). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T13:01:09Z. **Review:** CHANGES_REQUESTED --- Now ready for review, requesting changes before merge: - **EVEX 64-byte store width bug (`tools/d3d12_device_calls.py:72`)**: as reproduced by `@lixonik`, an EVEX 64-byte ZMM store (`vmovups %zmm0, 0x20(%rsp)`) overwrites the spilled device slot at `[rsp+0x40]`, yet the scanner still attributes slot 6 because the recorded store width stops short of `0x40`. Please preserve EVEX vector width in the decoder (or conservatively invalidate 64 bytes for EVEX stores when width is unavailable) and add a regression test. - **CI**: no CI runs on the current head yet; please get fresh green CI after fixing. *(AI-assisted review.)*
MoonEyes commented 2026-12-02 10:32:45 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T10:32:45Z.


For information: this pull request is carried in #3093, which merges your head (your commits are kept) into current main, resolves the conflicts keeping both sides, and fixes the points raised in review here (details in its description). This one stays open until a maintainer decides; if you pick it up again, say so and the carry can be closed.

AI-assisted (Claude Code, Claude Opus 5.5)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7342626766 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2343#issuecomment-7342626766). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T10:32:45Z. --- For information: this pull request is carried in #3093, which merges your head (your commits are kept) into current `main`, resolves the conflicts keeping both sides, and fixes the points raised in review here (details in its description). This one stays open until a maintainer decides; if you pick it up again, say so and the carry can be closed. AI-assisted (Claude Code, Claude Opus 5.5)
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!2343
No description provided.