d3d12_vulkan: a title texture destroyed inside the registry is removed after the registry lets go (RT-TITLE-TEXTURE-GONE) #1798

Merged
ImLele69 merged 2 commits from upstream-pr/1798 into main 2026-11-25 18:42:53 +00:00
ImLele69 commented 2026-11-25 12:16:29 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ImLele69 on 2026-11-25T12:16:29Z.


Claimed in the coordination issue as: RT-TITLE-TEXTURE-GONE — https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162151750

What and why

~Resource calls DeviceState::title_texture_gone, which takes title_texture_mutex. The comment above the registry already says the caller of a removal holds that mutex while the resource is destroyed. But the last reference to a slot-70 texture can be a descriptor that write_placed_texture_view overwrites while it holds the mutex: place_title_view → write_title_srv → CreateShaderResourceView releases the old view's resource, its destructor runs, and the same thread takes the same std::mutex again.

MSVC's std::mutex detects that and throws std::system_error (resource_deadlock_would_occur). Under Wine, msvcp140.dll does not implement std::_Throw_Cpp_error, so the process aborts instead:

wine: Unimplemented function msvcp140.dll.?_Throw_Cpp_error@std@@YAXH@Z called at address ... (thread 0190), starting debugger...

Startup trace exception record, with a linker map of that build: kernelbase < MSVCP140 < … < ~Resource < Device::CreateShaderResourceView < write_title_srv < write_placed_texture_view < proxy_create_placed_view_x < proxy_slot<70>, on a title thread during the story load (16,808 registered images).

Change

title_texture_gone checks t_in_title_textures, which is set exactly while a thread is inside the registry holding title_texture_mutex (InTitleTextures). There, the entry only stops naming the resource (resource = nullptr, no erase, so no iterator of a caller walking title_textures is touched), and (resource, address) goes to titles_gone. drain_titles_gone removes those entries and their GPU placements with the mutex taken normally, right after write_placed_texture_view's registry block and after refresh_title_textures unlocks. place_title_view treats an entry without a resource as absent.

Outside the registry nothing changes.

Tested

  • gta6rt.exe cross-built (clang-cl 20 + xwin) from this branch's commit on main 0d80bc5df.
  • Retail 1.0.2.1 under Wine 11.21 (Fedora 44, RTX 2070 SUPER 8 GB, i9-9900KF, 16 GB RAM), counts only: the abort above, once, without the change; with it, six story-load boots on two bases (c7e82e26e with local changes, and 0d80bc5df exactly as in this branch) ran to the world start (36,000-40,480 registered images) without it. They end on the known world-start device loss, which is unrelated. The race needs a slot-70 view overwritten while it holds the last reference, so one run without the abort proves little on its own; six do not prove it either, but the abort did not come back.
  • I have not reproduced it on native Windows, where the same recursion would surface as a std::system_error out of CreateShaderResourceView rather than an abort.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5491148387 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1798). Originally posted by [ImLele69](https://github.com/ImLele69) on 2026-11-25T12:16:29Z. --- Claimed in the coordination issue as: RT-TITLE-TEXTURE-GONE — https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162151750 ### What and why `~Resource` calls `DeviceState::title_texture_gone`, which takes `title_texture_mutex`. The comment above the registry already says the caller of a removal holds that mutex while the resource is destroyed. But the last reference to a slot-70 texture can be a descriptor that `write_placed_texture_view` overwrites while it holds the mutex: `place_title_view` → `write_title_srv` → `CreateShaderResourceView` releases the old view's resource, its destructor runs, and the same thread takes the same `std::mutex` again. MSVC's `std::mutex` detects that and throws `std::system_error` (`resource_deadlock_would_occur`). Under Wine, `msvcp140.dll` does not implement `std::_Throw_Cpp_error`, so the process aborts instead: ``` wine: Unimplemented function msvcp140.dll.?_Throw_Cpp_error@std@@YAXH@Z called at address ... (thread 0190), starting debugger... ``` Startup trace exception record, with a linker map of that build: `kernelbase` < `MSVCP140` < … < `~Resource` < `Device::CreateShaderResourceView` < `write_title_srv` < `write_placed_texture_view` < `proxy_create_placed_view_x` < `proxy_slot<70>`, on a title thread during the story load (16,808 registered images). ### Change `title_texture_gone` checks `t_in_title_textures`, which is set exactly while a thread is inside the registry holding `title_texture_mutex` (`InTitleTextures`). There, the entry only stops naming the resource (`resource = nullptr`, no erase, so no iterator of a caller walking `title_textures` is touched), and `(resource, address)` goes to `titles_gone`. `drain_titles_gone` removes those entries and their GPU placements with the mutex taken normally, right after `write_placed_texture_view`'s registry block and after `refresh_title_textures` unlocks. `place_title_view` treats an entry without a resource as absent. Outside the registry nothing changes. ### Tested - `gta6rt.exe` cross-built (clang-cl 20 + xwin) from this branch's commit on `main` `0d80bc5df`. - Retail 1.0.2.1 under Wine 11.21 (Fedora 44, RTX 2070 SUPER 8 GB, i9-9900KF, 16 GB RAM), counts only: the abort above, once, without the change; with it, six story-load boots on two bases (`c7e82e26e` with local changes, and `0d80bc5df` exactly as in this branch) ran to the world start (36,000-40,480 registered images) without it. They end on the known world-start device loss, which is unrelated. The race needs a slot-70 view overwritten while it holds the last reference, so one run without the abort proves little on its own; six do not prove it either, but the abort did not come back. - I have not reproduced it on native Windows, where the same recursion would surface as a `std::system_error` out of `CreateShaderResourceView` rather than an abort. AI-assisted: yes (Claude Code).
QemmHD commented 2026-11-25 13:56:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by QemmHD on 2026-11-25T13:56:59Z.


Review (code read; tests on an RTX 4070 SUPER to follow): there's a narrow use-after-free window in the deferred path.

  • Thread T, a title thread in write_placed_texture_view, frees texture X inside the title_texture_mutex section. With this change, X's gpu_placements entry stays until drain_titles_gone.
  • Thread S, the submit path (fix_console_slots → bind_title_image, and matching_gpu_placement), holds only descriptor_mutex and can reach matches(*X) on the freed X before the drain.
  • ~Resource already takes descriptor_mutex unconditionally (image_owner/forget_image), so the freeing thread never holds it when the deferral kicks in.

Suggested fix: remove X from gpu_placements immediately in the fast path, under descriptor_mutex, and defer only the title_textures erase, which is the part that needs the registry lock to be released. Then nothing reachable from the submit path points at freed memory.

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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7164525542 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1798#issuecomment-7164525542). Originally posted by [QemmHD](https://github.com/QemmHD) on 2026-11-25T13:56:59Z. --- Review (code read; tests on an RTX 4070 SUPER to follow): there's a narrow use-after-free window in the deferred path. - **Thread T**, a title thread in `write_placed_texture_view`, frees texture X inside the `title_texture_mutex` section. With this change, X's `gpu_placements` entry stays until `drain_titles_gone`. - **Thread S**, the submit path (`fix_console_slots` → `bind_title_image`, and `matching_gpu_placement`), holds only `descriptor_mutex` and can reach `matches(*X)` on the freed X before the drain. - `~Resource` already takes `descriptor_mutex` unconditionally (`image_owner`/`forget_image`), so the freeing thread never holds it when the deferral kicks in. **Suggested fix:** remove X from `gpu_placements` immediately in the fast path, under `descriptor_mutex`, and defer only the `title_textures` erase, which is the part that needs the registry lock to be released. Then nothing reachable from the submit path points at freed memory. AI-assisted (Claude Code, Claude Opus 5.5).
potatoe0100 commented 2026-11-25 13:58:08 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by potatoe0100 on 2026-11-25T13:58:08Z.


Tested on Windows 11 (26300), NVIDIA GeForce RTX 4060, driver 621.14, VS 2022 RelWithDebInfo + Ninja, merged onto main 384abe5b: the 24 title/texture tests (d3d12_title_va*, d3d12_title_*_window, d3d12_placed_heap_lifetime, d3d12_texture_uav, title_window_plan, ...) pass. None of them reaches the re-entrant destroy, so this is mostly a read of the diff.

One crash in addition to @QemmHD's point: entries with resource == nullptr are now visible to log_title_census before drain_titles_gone runs. It is called inside both registry blocks, in write_placed_texture_view (64th and every 2048th view) and in refresh_title_textures (every 10 s), both before the drain. Its loop does

if (!t->owned && t->unbound_bytes) t->set_resource(t->resource);

and set_resource dereferences r (r->image_backing_bytes()). The entries this PR nulls are the non-owned slot-133 ones, which can have unbound_bytes set. Suggested fix: if (!t->owned && t->resource && t->unbound_bytes). The other walkers I checked already skip a null resource (visit in the refresh pass, the mapped-texture listing, the UAV path's candidate, retire_title_texture).

Minor: the comment above title_texture_snapshot in refresh_title_textures still says the registry mutex is recursive for its owner on Windows, which this PR shows is not the case.

AI-assisted (Claude).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7164554004 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1798#issuecomment-7164554004). Originally posted by [potatoe0100](https://github.com/potatoe0100) on 2026-11-25T13:58:08Z. --- Tested on Windows 11 (26300), NVIDIA GeForce RTX 4060, driver 621.14, VS 2022 RelWithDebInfo + Ninja, merged onto `main` 384abe5b: the 24 title/texture tests (`d3d12_title_va*`, `d3d12_title_*_window`, `d3d12_placed_heap_lifetime`, `d3d12_texture_uav`, `title_window_plan`, ...) pass. None of them reaches the re-entrant destroy, so this is mostly a read of the diff. One crash in addition to @QemmHD's point: entries with `resource == nullptr` are now visible to `log_title_census` before `drain_titles_gone` runs. It is called inside both registry blocks, in `write_placed_texture_view` (64th and every 2048th view) and in `refresh_title_textures` (every 10 s), both before the drain. Its loop does ```cpp if (!t->owned && t->unbound_bytes) t->set_resource(t->resource); ``` and `set_resource` dereferences `r` (`r->image_backing_bytes()`). The entries this PR nulls are the non-owned slot-133 ones, which can have `unbound_bytes` set. Suggested fix: `if (!t->owned && t->resource && t->unbound_bytes)`. The other walkers I checked already skip a null resource (`visit` in the refresh pass, the mapped-texture listing, the UAV path's `candidate`, `retire_title_texture`). Minor: the comment above `title_texture_snapshot` in `refresh_title_textures` still says the registry mutex is recursive for its owner on Windows, which this PR shows is not the case. AI-assisted (Claude).
aslan2142 commented 2026-11-25 14:42:44 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-11-25T14:42:44Z.


Thanks, @ImLele69: the self-deadlock is real on main (write_placed_texture_view and refresh_title_textures hold title_texture_mutex when ~Resource calls title_texture_gone). Two things before it can merge:

  • Use-after-free window. Inside the registry, the texture stays in gpu_placements until drain_titles_gone, but ~Resource still finishes and frees it. bind_title_image reads gpu_placements under descriptor_mutex alone, from any thread, so it can read the freed resource; the fill worker's locked blocks never drain, which keeps the window open longer. Could you defer the delete itself until the drain, or remove the placement at once when descriptor_mutex isn't held?
  • A test. A unit test that destroys a registered title texture while the registry lock is held, with a timeout, failing on main and passing here.

Minor: refresh_title_textures keeps t_in_title_textures set after lock.unlock(), and its comment about a recursive mutex is out of date.

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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7165696622 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1798#issuecomment-7165696622). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-11-25T14:42:44Z. --- Thanks, @ImLele69: the self-deadlock is real on `main` (`write_placed_texture_view` and `refresh_title_textures` hold `title_texture_mutex` when `~Resource` calls `title_texture_gone`). Two things before it can merge: - **Use-after-free window.** Inside the registry, the texture stays in `gpu_placements` until `drain_titles_gone`, but `~Resource` still finishes and frees it. `bind_title_image` reads `gpu_placements` under `descriptor_mutex` alone, from any thread, so it can read the freed resource; the fill worker's locked blocks never drain, which keeps the window open longer. Could you defer the delete itself until the drain, or remove the placement at once when `descriptor_mutex` isn't held? - **A test.** A unit test that destroys a registered title texture while the registry lock is held, with a timeout, failing on `main` and passing here. Minor: `refresh_title_textures` keeps `t_in_title_textures` set after `lock.unlock()`, and its comment about a recursive mutex is out of date. AI-assisted (Claude Code, Claude Opus 5.5).
ImLele69 commented 2026-11-25 15:59:12 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by ImLele69 on 2026-11-25T15:59:12Z.


Thanks @QemmHD, @potatoe0100 and @aslan2142. All three points are right. 152cee255 fixes them:

  • Use-after-free: inside the registry, title_texture_gone now removes the placement from gpu_placements at once, under descriptor_mutex. Only the title_textures erase still waits for drain_titles_gone, so nothing the submit path can reach points at the freed resource. Taking descriptor_mutex there is safe because the freeing thread never holds it: ~Resource takes it itself (image_owner, forget_image), as QemmHD noted. The drain no longer touches gpu_placements.
  • log_title_census: it skips an entry without a resource (!t->owned && t->resource && t->unbound_bytes).
  • refresh_title_textures: the InTitleTextures scope ends before lock.unlock(), so the drain and the Release() calls after it run outside the registry. The comment about a recursive mutex is replaced.

It cross-builds with clang-cl, and the same change is in the tree I'm booting under Wine now.

The test: I haven't written it yet. I want it to go through the real re-entrant path (the last reference released by a descriptor that write_placed_texture_view overwrites), and I haven't yet found a way to build that without a title-written heap. If d3d12_existing_heap_test's check_placed_texture_views is the right place (two write_placed_texture_view calls on the same address with different descriptions, with a timeout), I'll add it there and check that it fails on main.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7167490444 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1798#issuecomment-7167490444). Originally posted by [ImLele69](https://github.com/ImLele69) on 2026-11-25T15:59:12Z. --- Thanks @QemmHD, @potatoe0100 and @aslan2142. All three points are right. 152cee255 fixes them: - **Use-after-free:** inside the registry, `title_texture_gone` now removes the placement from `gpu_placements` at once, under `descriptor_mutex`. Only the `title_textures` erase still waits for `drain_titles_gone`, so nothing the submit path can reach points at the freed resource. Taking `descriptor_mutex` there is safe because the freeing thread never holds it: `~Resource` takes it itself (`image_owner`, `forget_image`), as QemmHD noted. The drain no longer touches `gpu_placements`. - **`log_title_census`:** it skips an entry without a resource (`!t->owned && t->resource && t->unbound_bytes`). - **`refresh_title_textures`:** the `InTitleTextures` scope ends before `lock.unlock()`, so the drain and the `Release()` calls after it run outside the registry. The comment about a recursive mutex is replaced. It cross-builds with clang-cl, and the same change is in the tree I'm booting under Wine now. **The test:** I haven't written it yet. I want it to go through the real re-entrant path (the last reference released by a descriptor that `write_placed_texture_view` overwrites), and I haven't yet found a way to build that without a title-written heap. If `d3d12_existing_heap_test`'s `check_placed_texture_views` is the right place (two `write_placed_texture_view` calls on the same address with different descriptions, with a timeout), I'll add it there and check that it fails on `main`. AI-assisted (Claude Code).
QemmHD commented 2026-11-25 18:42:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by QemmHD on 2026-11-25T18:42:51Z.


Merging.

Why merged

  • The bug: a texture whose last reference was a descriptor overwritten inside the title-texture registry was destroyed on a thread that already held the registry mutex. Its destructor then locked that mutex again (resource_deadlock_would_occur, an abort under Wine).
  • The fix: such a texture now leaves gpu_placements immediately, under descriptor_mutex, which that thread never holds. Its registry entry stops naming it until drain_titles_gone erases it after the registry lets go.

Why it's good

  • It's small, and on the slot-70 view path used during the load.
  • The deferred part only holds a registry entry with no resource, so nothing the submission thread reads points at freed memory. That was the window in the first revision, fixed in 152cee255.
  • log_title_census and the refresh skip such entries.
  • Tested on an RTX 4070 SUPER (Windows 11), merged onto current main:
    • full ctest 448/449, where the only failure is the known depth-resolve MFAA case;
    • the 13 title/texture/heap tests pass under Vulkan validation with no VUID;
    • the consistency set is unchanged (one timing flake passed on re-run; the PR is C++ only).
    • The diff was reviewed for safety: no game data, no new I/O.

Follow-ups welcome

  • A test that releases the last reference from inside write_placed_texture_view.
  • The same guard on the slot-71 UAV path.

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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7171292612 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1798#issuecomment-7171292612). Originally posted by [QemmHD](https://github.com/QemmHD) on 2026-11-25T18:42:51Z. --- Merging. **Why merged** - **The bug:** a texture whose last reference was a descriptor overwritten inside the title-texture registry was destroyed on a thread that already held the registry mutex. Its destructor then locked that mutex again (`resource_deadlock_would_occur`, an abort under Wine). - **The fix:** such a texture now leaves `gpu_placements` immediately, under `descriptor_mutex`, which that thread never holds. Its registry entry stops naming it until `drain_titles_gone` erases it after the registry lets go. **Why it's good** - It's small, and on the slot-70 view path used during the load. - The deferred part only holds a registry entry with no resource, so nothing the submission thread reads points at freed memory. That was the window in the first revision, fixed in 152cee255. - `log_title_census` and the refresh skip such entries. - **Tested** on an RTX 4070 SUPER (Windows 11), merged onto current main: - full ctest 448/449, where the only failure is the known depth-resolve MFAA case; - the 13 title/texture/heap tests pass under Vulkan validation with no VUID; - the consistency set is unchanged (one timing flake passed on re-run; the PR is C++ only). - The diff was reviewed for safety: no game data, no new I/O. **Follow-ups welcome** - A test that releases the last reference from inside `write_placed_texture_view`. - The same guard on the slot-71 UAV path. 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!1798
No description provided.