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

Manually merged
github merged 2 commits from gh-pr/1798/head into gh-pr/1798/base 2026-11-26 10:05:56 +00:00
Owner

@⁠ImLele69 on GitHub · 2026-11-25 12:16 UTC · original
ImLele69:rt-title-texture-gone → main · +43 −6 · 1 files · 2 commits · merged 2026-11-25 18:42 UTC

Claimed in the coordination issue as: RT-TITLE-TEXTURE-GONE — #1608

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).

**@⁠ImLele69** on GitHub · 2026-11-25 12:16 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1798) `ImLele69:rt-title-texture-gone` → `main` · +43 −6 · 1 files · 2 commits · merged 2026-11-25 18:42 UTC Claimed in the coordination issue as: RT-TITLE-TEXTURE-GONE — https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 ### 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).
The last reference to a slot-70 texture can be a descriptor that write_placed_texture_view
overwrites while it holds title_texture_mutex: ~Resource then called title_texture_gone, which
took the same mutex again. MSVC's std::mutex throws resource_deadlock_would_occur there, and
Wine's msvcp140 has no _Throw_Cpp_error, so the boot aborted (stack: proxy_create_placed_view_x
< write_placed_texture_view < write_title_srv < CreateShaderResourceView < ~Resource). Inside
the registry the entry now only stops naming the resource, and drain_titles_gone removes it and
its GPU placement once the thread has released the mutex.
Review of #1798: the submit path reads gpu_placements under descriptor_mutex alone, so the
placement of a texture destroyed inside the registry must go before ~Resource frees it.
descriptor_mutex is never held there (~Resource takes it itself, in image_owner and
forget_image); only the title_textures erase waits for drain_titles_gone. log_title_census
skips an entry without a resource, and refresh_title_textures leaves the registry scope
before it unlocks.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:05:53 +00:00
github manually merged commit 152cee2550 into gh-pr/1798/base 2026-11-26 10:05:56 +00:00
Author
Owner

@⁠QemmHD on GitHub · 2026-11-25 13:56 UTC · original

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).

**@⁠QemmHD** on GitHub · 2026-11-25 13:56 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1798#issuecomment-7164525542) 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).
Author
Owner

@⁠potatoe0100 on GitHub · 2026-11-25 13:58 UTC · original

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).

**@⁠potatoe0100** on GitHub · 2026-11-25 13:58 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1798#issuecomment-7164554004) 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).
Author
Owner

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

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).

**@⁠aslan2142** on GitHub · 2026-11-25 14:42 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1798#issuecomment-7165696622) 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).
Author
Owner

@⁠ImLele69 on GitHub · 2026-11-25 15:59 UTC · original

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).

**@⁠ImLele69** on GitHub · 2026-11-25 15:59 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1798#issuecomment-7167490444) 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).
Author
Owner

@⁠QemmHD on GitHub · 2026-11-25 18:42 UTC · original

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).

**@⁠QemmHD** on GitHub · 2026-11-25 18:42 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1798#issuecomment-7171292612) 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).
This discussion has been locked. Commenting is limited to contributors.
No description provided.