d3d12: a geometry tile dropped while slot-133 shadows guard it reopens them too (RT-NULL-VTABLE-P4E683D6) #2471

Closed
leonida/mandy
robot00f wants to merge 0 commits from fix/geometry-views-shadow-protect into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

robot00f commented 2026-11-27 15:20:31 +00:00

d3d12: a geometry tile dropped while slot-133 shadows guard it reopens them too (RT-NULL-VTABLE-P4E683D6)

Claimed in the coordination issue as: RT-NULL-VTABLE-P4E683D6.

What and why

The geometry watch (runtime/src/title_geometry_watch.h) makes a title tile read-only so it sees the title's writes to it. A 4 KiB page of that tile can also be one a slot-133 shadow (ShadowPages in runtime/shims/d3d12_vulkan_device.cpp) keeps read-only: both guard the title's geometry, each queries and protects under its own lock.

Two of the three ways a tile is opened again already tell the shadows, and the third did not:

  • a write fault: shadow_write_fault asks the watch first, and when the watch reopened the whole 64 KiB tile (tile_reopened) every shadow over it calls reopen_range (commit ba626820);
  • a kernel write: open_shadow_pages does the same for the range, rounded out to the tiles the watch reopened;
  • the last reference to a cell going away (release): it made the pages writable and only left a title_memory_map::changed entry, whose effect reaches the shadows at their next refresh().

Between that opening and the next refresh, ShadowPages::write_back still sees the page as kProtected, its held() predicate is false, and it copies the shadow's older bytes over the title's write. That is the measured shape of +0x4e683d6: the title read an object at [arg+0x10], found its vtable word zero ([rcx] = 0, fault at address 0x0, rax = 0) and called through it. The stop only happened in the run that had LEONIDA_GEOMETRY_MB=2048, the only one where the geometry watch guarded thousands of 64 KiB tiles; the run without it went past that point.

What

release() now calls a reopener the layer installs, over the tile whose pages it just made writable, with the watch's mutex released: the fault handler takes a shadow's lock and then the watch's mutex, so calling the shadows under it would invert the order and deadlock. The D3D12 layer points the reopener at reopen_shadow_ranges, the same shadow half open_shadow_pages used. The journal entry stays, for shadows that cover a page of the tile but are not open at the time.

Test

runtime/test/geometry_writes_test.cpp (ctest -R ^geometry_writes$), on a private allocation, no GPU and no game data:

  • an armed cell is read-only; releasing it opens the page, calls the reopener once with the whole tile, records the journal entry and leaves the page read-write and the watch empty;
  • the hook asks the watch again (notify), which takes the watch's mutex: a release that called it with the mutex held deadlocks there (ctest reports the timeout);
  • a cell that guarded nothing calls nothing and records nothing;
  • two holders: nothing until the last one goes.

Negative control: with the reopener call removed, the test fails 5 checks (calls == 1, the two range checks, the mutex check and the two-holder case).

Which GPU

Measured on the card of the report: GeForce GTX 1050 Ti, NVIDIA 582.28, Windows 10 Pro 19045. geometry_writes_test passes there, and d3d12_existing_heap (the slot-133 shadow test) fails exactly as it does on main — texture memory unavailable: 2048 bytes, alignment 1024, type bits 0x0 (vk format 9 256x8), the case #1894 fixes; with that head applied it passes 3/3 with this change. So nothing new fails here.

AI-assisted (OpenCode, DeepSeek).

d3d12: a geometry tile dropped while slot-133 shadows guard it reopens them too (RT-NULL-VTABLE-P4E683D6) Claimed in the coordination issue as: RT-NULL-VTABLE-P4E683D6. ### What and why The geometry watch (`runtime/src/title_geometry_watch.h`) makes a title tile read-only so it sees the title's writes to it. A 4 KiB page of that tile can also be one a slot-133 shadow (`ShadowPages` in `runtime/shims/d3d12_vulkan_device.cpp`) keeps read-only: both guard the title's geometry, each queries and protects under its own lock. Two of the three ways a tile is opened again already tell the shadows, and the third did not: - a write fault: `shadow_write_fault` asks the watch first, and when the watch reopened the whole 64 KiB tile (`tile_reopened`) every shadow over it calls `reopen_range` (commit `ba626820`); - a kernel write: `open_shadow_pages` does the same for the range, rounded out to the tiles the watch reopened; - the last reference to a cell going away (`release`): it made the pages writable and only left a `title_memory_map::changed` entry, whose effect reaches the shadows at their next `refresh()`. Between that opening and the next refresh, `ShadowPages::write_back` still sees the page as `kProtected`, its `held()` predicate is false, and it copies the shadow's older bytes over the title's write. That is the measured shape of `+0x4e683d6`: the title read an object at `[arg+0x10]`, found its vtable word zero (`[rcx] = 0`, fault at address `0x0`, `rax = 0`) and called through it. The stop only happened in the run that had `LEONIDA_GEOMETRY_MB=2048`, the only one where the geometry watch guarded thousands of 64 KiB tiles; the run without it went past that point. ### What `release()` now calls a reopener the layer installs, over the tile whose pages it just made writable, with the watch's mutex **released**: the fault handler takes a shadow's lock and then the watch's mutex, so calling the shadows under it would invert the order and deadlock. The D3D12 layer points the reopener at `reopen_shadow_ranges`, the same shadow half `open_shadow_pages` used. The journal entry stays, for shadows that cover a page of the tile but are not open at the time. ### Test `runtime/test/geometry_writes_test.cpp` (`ctest -R ^geometry_writes$`), on a private allocation, no GPU and no game data: - an armed cell is read-only; releasing it opens the page, calls the reopener once with the whole tile, records the journal entry and leaves the page read-write and the watch empty; - the hook asks the watch again (`notify`), which takes the watch's mutex: a release that called it with the mutex held deadlocks there (ctest reports the timeout); - a cell that guarded nothing calls nothing and records nothing; - two holders: nothing until the last one goes. Negative control: with the reopener call removed, the test fails 5 checks (`calls == 1`, the two range checks, the mutex check and the two-holder case). ### Which GPU Measured on the card of the report: GeForce GTX 1050 Ti, NVIDIA 582.28, Windows 10 Pro 19045. `geometry_writes_test` passes there, and `d3d12_existing_heap` (the slot-133 shadow test) fails exactly as it does on `main` — `texture memory unavailable: 2048 bytes, alignment 1024, type bits 0x0 (vk format 9 256x8)`, the case [#1894](https://github.com/ubiquitarianism/leonida/pull/1894) fixes; with that head applied it passes 3/3 with this change. So nothing new fails here. AI-assisted (OpenCode, DeepSeek).
lixonik reviewed 2026-11-27 15:48:21 +00:00
lixonik left a comment
No description provided.
Reviewed and locally tested at `1de863bca` (merged cleanly onto `main` `ec91d695c`). ### Verification - Environment: Windows 11, MSVC 19.44, NVIDIA GeForce RTX 5070 (driver 610.88, Vulkan 1.4.341; `VK_LAYER_KHRONOS_validation` not installed locally). - `ctest`: `geometry_writes`, `d3d12_existing_heap`, `d3d12_existing_heap_no_write_watch`, `d3d12_title_window_write_back`, and `d3d12_title_window_write_back_fault` all passed (5/5). - `scripts/check_no_game_data.py` and `git diff --check`: clean. ### Review notes - Lock ordering looks sound across all `geometry_writes::release()` callers: `release()` restores protections and erases the cell under `s.mutex`, unlocks `s.mutex`, and only then invokes `reopen_shadow_ranges()` (`g_shadow_pages_lock` shared --> `ShadowPages::lock_` exclusive --> `geometry_writes::notify`), matching `shadow_write_fault`'s order. Both `ShadowPages::reopen_all()` and `ShadowPages::update_aliased()` already release `ShadowPages::lock_` before calling `release_cells()` / `geometry_writes::release()`, and `GeometryViews` only holds its own mutex. - Keeping `reopen(va, it->second)` before `s.cells.erase(it)` avoids leaving a page `PAGE_READONLY` with no live cell to answer a concurrent write fault. There is still a narrow concurrent window between `reopen()` making the page writable and `reopen_ranges()` acquiring `ShadowPages::lock_` where another thread's `ShadowPages::write_back()` (e.g., if `release()` runs on the async geometry refresher thread) could observe `kProtected` before the reopen lands, though that is much tighter than leaving `kProtected` stale until the next refresh. - No blocking issues found. AI-assisted.
timbot103 commented 2026-11-28 02:02:25 +00:00

Reviewed at head 1de863bca on Windows 11 (MSVC 2022 19.44).

Verification

  • Built geometry_writes_test and gta6rt.
  • geometry_writes_test: passed.

Notes (non-blocking)

  • Restoring pages before erasing the final cell and invoking the shadow reopen callback after releasing the geometry mutex cleanly avoids lock inversion (title_geometry_watch.h:147-183; d3d12_vulkan_device.cpp:396-402, 878-890, 1127-1157, 1380-1406).
  • Minor test note (geometry_writes_test.cpp:30-42, 68-85): Because the first test releases the only watched cell, state().count is already 0 when recorder() calls notify(), so notify() exits on its atomic fast path without taking the watch mutex and watch_mutex_free succeeds even if the mutex were still held. Keeping a second cell watched during the callback makes the lock-free-callback check truly exercise the mutex.
  • Minor docs note (docs/d3d12_vulkan.md:1729-1732): Because GeometryViews::update can run on the async refresher independently of submission write-back, a brief window still exists between restoring PTEs and acquiring the shadow lock where a write-back can observe stale kProtected; softening the unconditional "before the next copy-back" wording makes the doc accurate.

Approve.

AI-assisted review + local MSVC verification.

Reviewed at head `1de863bca` on Windows 11 (MSVC 2022 `19.44`). ### Verification - Built `geometry_writes_test` and `gta6rt`. - `geometry_writes_test`: **passed**. ### Notes (non-blocking) - Restoring pages before erasing the final cell and invoking the shadow reopen callback after releasing the geometry mutex cleanly avoids lock inversion (`title_geometry_watch.h:147-183`; `d3d12_vulkan_device.cpp:396-402`, `878-890`, `1127-1157`, `1380-1406`). - **Minor test note (`geometry_writes_test.cpp:30-42`, `68-85`):** Because the first test releases the only watched cell, `state().count` is already `0` when `recorder()` calls `notify()`, so `notify()` exits on its atomic fast path without taking the watch mutex and `watch_mutex_free` succeeds even if the mutex were still held. Keeping a second cell watched during the callback makes the lock-free-callback check truly exercise the mutex. - **Minor docs note (`docs/d3d12_vulkan.md:1729-1732`):** Because `GeometryViews::update` can run on the async refresher independently of submission write-back, a brief window still exists between restoring PTEs and acquiring the shadow lock where a write-back can observe stale `kProtected`; softening the unconditional "before the next copy-back" wording makes the doc accurate. Approve. AI-assisted review + local MSVC verification.
lixonik approved these changes 2026-11-28 15:01:38 +00:00
lixonik left a comment

Re-reviewed 1de863bca and verified on RTX 5070 with Khronos validation enabled.

Code audit:

  • runtime/src/title_geometry_watch.h, runtime/shims/d3d12_vulkan_device.cpp: invoking reopen_shadow_ranges outside s.mutex whenever geometry_writes::release() makes guarded pages writable without a write fault cleanly preserves lock ordering (g_shadow_pages_lock -> geometry watch mutex) and prevents slot-133 write-backs from overwriting subsequent title writes with stale shadow bytes.

Verification (Windows 11, MSVC 19.44, Ninja Release, LEONIDA_VK_VALIDATION=1):

  • Built geometry_writes_test and d3d12_existing_heap_test.
  • ctest -R '^(geometry_writes|d3d12_existing_heap|d3d12_existing_heap_no_write_watch)$': 3/3 passed (0 validation errors).
  • python -I scripts/check_no_game_data.py: passed.

GPUs tested: NVIDIA GeForce RTX 5070 12 GB (Windows 11, driver 610.88, Vulkan 1.4.341, VK_LAYER_KHRONOS_validation 1.4.363).

AI-assisted (Antigravity, Gemini).

Re-reviewed `1de863bca` and verified on RTX 5070 with Khronos validation enabled. Code audit: - `runtime/src/title_geometry_watch.h`, `runtime/shims/d3d12_vulkan_device.cpp`: invoking `reopen_shadow_ranges` outside `s.mutex` whenever `geometry_writes::release()` makes guarded pages writable without a write fault cleanly preserves lock ordering (`g_shadow_pages_lock` -> geometry watch mutex) and prevents slot-133 write-backs from overwriting subsequent title writes with stale shadow bytes. Verification (Windows 11, MSVC 19.44, Ninja Release, `LEONIDA_VK_VALIDATION=1`): - Built `geometry_writes_test` and `d3d12_existing_heap_test`. - `ctest -R '^(geometry_writes|d3d12_existing_heap|d3d12_existing_heap_no_write_watch)$'`: **3/3 passed** (0 validation errors). - `python -I scripts/check_no_game_data.py`: passed. GPUs tested: NVIDIA GeForce RTX 5070 12 GB (Windows 11, driver 610.88, Vulkan 1.4.341, `VK_LAYER_KHRONOS_validation` 1.4.363). AI-assisted (Antigravity, Gemini).
zmasi commented 2026-11-28 20:39:55 +00:00

#2471 | head 1de863bcafbcbde398d068471fbfc811fb77525d (merged cleanly with main 1bc055e6c2dedaa0de23ec79d8c4007c1dff1fb3) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(geometry_writes|d3d12_existing_heap|d3d12_mapped_plane_async)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) + python -I tests/test_docs_encoding.py | red on main: geometry_writes_test.cpp fails to compile (error C2039: 'set_reopener': is not a member of 'leonida::geometry_writes') | green on PR+main: 3/3 CTests pass (geometry_writes, d3d12_existing_heap slot-133 shadow suite on RTX 3090, and d3d12_mapped_plane_async with 0 Khronos validation errors); test_docs_encoding.py and check_no_game_data.py 0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

#2471 | head 1de863bcafbcbde398d068471fbfc811fb77525d (merged cleanly with main 1bc055e6c2dedaa0de23ec79d8c4007c1dff1fb3) | `ctest --test-dir runtime/build -C RelWithDebInfo -R "^(geometry_writes|d3d12_existing_heap|d3d12_mapped_plane_async)$" --output-on-failure` (`LEONIDA_VK_VALIDATION=1`) + `python -I tests/test_docs_encoding.py` | red on main: `geometry_writes_test.cpp` fails to compile (`error C2039: 'set_reopener': is not a member of 'leonida::geometry_writes'`) | green on PR+main: 3/3 CTests pass (`geometry_writes`, `d3d12_existing_heap` slot-133 shadow suite on RTX 3090, and `d3d12_mapped_plane_async` with `0` Khronos validation errors); `test_docs_encoding.py` and `check_no_game_data.py` `0` violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes
aslan2142 commented 2026-12-01 16:44:55 +00:00

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.
Sign in to join this conversation.
No description provided.