runtime: unpublish destroyed TitleBufferWindows blocks and remove 4096 slot ceiling #3349
Open
github-bot
wants to merge 1 commit from
github-bot/gh-3349 into main
AGit
pull from: github-bot/gh-3349
merge into: github:main
github:main
github:perf/darner-title-priority-ceiling
github:gh-readonly-queue/main/pr-3311-b5c45b663dbc5a9c3532bb3f307e5f81b32faefa
github:gh-readonly-queue/main/pr-3088-af22e7847ff5808f72d1a1881c24be2fafd20b54
github:gh-readonly-queue/main/pr-3123-7f3fa026a94c6646e89244f28e1ce22290361867
github:gh-readonly-queue/main/pr-3354-637baa0b77ba47222e7280b17c1a59c6a1781d57
github:gh-readonly-queue/main/pr-3365-d7a0e0196a43834813014b4bd3aa5502a8263b14
github:gh-readonly-queue/main/pr-3361-5dfe8bbd05ed12fb2f2e1718d9af77a7c5f46fd8
github:gh-readonly-queue/main/pr-3366-8da9148204e9d12c585efa9ac2b7cc1ab08c7682
github:fix/runtime-test-conservative-fixtures
github:ci/shard-semantic-linux
github:docs/fg-limit-fps-switch
github:fix/runtime-test-fixture-paths
github:fix/runtime-test-fixture-readers
github:fix/read-file-hard-link
github:fix/book-slot-heap-never-read
github:fix/depth-resolve-spv-path
github:fix/indirect-unused-batched
github:gh-pr/3358/base
github:fix/xmem-refusal-diagnostics
github:gh-pr/3357/base
github:ci/shorten-ci-time
github:fix/alias-later-queued-submissions
github:claude/gallant-sagan-aamatr
github:gh-pr/3342/base
github:gh-pr/3341/base
github:d3d12/agility-sdk-621
github:gh-pr/3293/base
github:gh-pr/3319/base
github:gh-pr/3318/base
github:gh-pr/3315/base
github:gh-pr/3313/base
github:gh-pr/3314/base
github:claude/fervent-curie-m19lnr
github:gh-pr/3216/base
github:gh-pr/3272/base
github:gh-pr/3276/base
github:gh-pr/3279/base
github:gh-pr/3257/base
github:gh-pr/3258/base
github:gh-pr/3271/base
github:codex/skip-no-hit-ray-passes
github:gh-pr/3230/base
github:gh-pr/3266/base
github:gh-pr/3247/base
github:gh-pr/3246/base
github:gh-pr/3244/base
github:claude/sharp-sagan-1jqnp3
github:t3/preserve-fsr-sdk-restart-path
github:t3/propagate-host-failure-hresult
github:t3/launcher-custom-parameters
github:t3/series-x-2160-startup
github:codex/d3d12-uncapped-simulation
github:ci/less-to-run
github:perf/native-compute-shader-forms
github:claude/blissful-franklin-ukp7ag
github:codex/full-precompile-default
github:gh-pr/3166/base
github:gh-pr/3148/base
github:gh-pr/3138/base
github:gh-pr/3106/base
github:gh-pr/3057/base
github:gh-pr/2756/base
github:gh-pr/2812/base
github:gh-pr/3032/base
github:gh-pr/2992/base
github:notes/card-time-and-plant-flicker
github:gh-pr/2986/base
github:gh-pr/3000/base
github:gh-pr/2966/base
github:gh-pr/2924/base
github:gh-pr/726/base
github:gh-pr/2761/base
github:gh-pr/2663/base
github:gh-pr/2861/base
github:gh-pr/2862/base
github:gh-pr/2820/base
github:gh-pr/2811/base
github:gh-pr/2803/base
github:rt-call-trace
github:gh-pr/2802/base
github:gh-pr/2766/base
github:perf/ahead-split
github:gh-pr/2589/base
github:gh-pr/2699/base
github:gh-pr/2696/base
github:docs/readme-runtime-proposal
github:gh-pr/1346/base
github:codex/coord-claim-check-20261124-7e48
No reviewers
Labels
Clear labels
accessibility
Barrier affecting people with disabilities
area: ci
.github/ workflows and settings
area: d3d12
The D3D12 runtime in d3d12/ (Rust: leonida.exe and its crates)
area: docs
Documentation
area: launcher
The launcher (launcher/)
area: runtime (legacy)
The earlier Vulkan runtime in runtime/
area: shader translator
The shader translator (leonida-shader, tools/rdna2spirv) and its tests
area: tools
tools/ and scripts/
area: viewer
The map viewer (viewer/)
bug
Something isn't working
dependencies
Pull requests that update a dependency file
documentation
Improvements or additions to documentation
duplicate
This issue or pull request already exists
enhancement
New feature or request
good first issue
Good for newcomers
help wanted
Extra attention is needed
invalid
This doesn't seem right
missing claim
Needs a claim in #14
needs in-game run
Changes the code a frame runs through: needs the 300 s in-game gate (docs/game_gate.md)
needs rebase
Conflicts with the base branch: rebase it on main
needs-triage
Needs maintainer evaluation before the next triage state
question
Further information is requested
ready-for-agent
Fully specified and ready for an AFK agent
submodules
Pull requests that update submodules code
wontfix
This will not be worked on
No labels
accessibility
area: ci
area: d3d12
area: docs
area: launcher
area: runtime (legacy)
area: shader translator
area: tools
area: viewer
bug
dependencies
documentation
duplicate
enhancement
good first issue
help wanted
invalid
missing claim
needs in-game run
needs rebase
needs-triage
question
ready-for-agent
submodules
wontfix
Milestone
Clear milestone
No items
No milestone
Assignees
Clear assignees
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.
Dependencies
No dependencies set
Reference
github/ubiquitarianism--leonida!3349
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-3349"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@LewisStephens99 on GitHub · 2026-12-03 15:14 UTC · original
LewisStephens99:fix/title-buffer-windows-destroy-reuse→main· +585 −101 · 3 files · 1 commits · openClaimed in the coordination issue as: RT-TITLE-BUFFER-WINDOWS-DESTROY-AND-REUSE (#3312)
What and why
Fixes two bugs in
rt::d3d12::TitleBufferWindows(runtime/shims/d3d12_title_buffer_windows.h), flagged in #3312:Published::stageduse-after-free afterdestroy_block:destroy_blockcleared onlypublished_[i].addressto0and then erased theBlock(destroyingstd::unique_ptr<StagedSlot> staged), leavingpublished_[i].stageddangling.commit_uploads(),note_host_readbacks(), andstaging_stats()checked onlyb.stagedwithout checkingb.address, dereferencing freedStagedSlotmemory on any call after a staged block was destroyed.kPublished = 4096slot ceiling:allocate()only appended topublished_[published_count_]up to 4,096 and never reclaimed destroyed slots. After 4,096 block creations (even if all earlier blocks were freed), subsequent blocks went silently unpublished and became invisible tofor_each_block,find_mapped,mark_dirty_ptr,find_staged_range,record_uploads, andfind_uncached_buffer.Fix:
std::array<Published, 4096>with a dynamically sizedstd::vector<Published>guarded bymutable std::shared_mutex published_mutex_(with an atomicpublished_count_fast-path check for empty pools, andfor_each_blocksnapshotting(address, capacity)before invoking the caller callback outside the lock sopublished_mutex_remains strictly ordered beforeArenaStaging::mutex_and never wraps outer locks or callbacks).published_id(preserved acrossrelease()) so stale slot tokens passed tocollect_host_readbacks/note_host_readbacksnever alias later blocks.destroy_block(), takeunique_lock(published_mutex_), fold the destroyed staged block'sArenaStaging::Statsintoretired_stats_atomically, swap-erase the entry frompublished_, and updatepublished_count_before releasing Vulkan handles and freeingBlock::staged.release(), clearpublished_,published_count_, andretired_stats_underunique_lock(published_mutex_)before tearing downblocks_.std::uintptr_trange arithmetic infind_mapped,mark_dirty_ptr,find_staged_range, andfind_uncached_buffer.Threads::Threadsonarena_staging_testinruntime/CMakeLists.txtfor the new multi-threaded stress test.Tested
Synthetic unit tests added to
runtime/test/arena_staging_test.cpp(no game files):test_staged_destroy_unpublishes_and_preserves_stats: destroys a staged block with pendinglast_upload, verifiescommit_uploads(false)/commit_uploads(true)/collect_host_readbacks/note_host_readbacks/staging_statsnever touch freed memory, ignore stale slot tokens, and preserve retired block staging stats untilrelease().test_published_slot_reuse_past_4096_blocks: churns 4,200 create/destroy cycles in both unstaged (uncached) and staged modes, then verifies block #4,203 is published and reachable viafor_each_block,find_mapped,find_uncached_buffer,mark_dirty_ptr,find_staged_range,record_uploads,commit_uploads,collect_host_readbacks,note_host_readbacks, andstaging_stats.test_concurrent_readers_during_destroy_and_reuse: 4 concurrent threads (reader,uploader,readbacker, andwriter) hammeringallocate/free/destroy_blockalongside readers, upload drain/commit, and live+stale readbacks.Red on
main(128579e9e, unmodifiedd3d12_title_buffer_windows.h):RelWithDebInfobuild:arena_staging_testfailed withline 598: st1.readbacks == 0(exit 1;note_host_readbackson destroyedslot3mutated freedStagedSlot)./fsanitize=address): aborted withERROR: AddressSanitizer: heap-use-after-freeinTitleBufferWindows::commit_uploads(bool)(d3d12_title_buffer_windows.h:551), freed inTitleBufferWindows::destroy_block(d3d12_title_buffer_windows.h:112).main's header: failed withline 660: visited == 3 && saw_new(exit 1).Green on this branch (
6c96c8968, rebased onacb0657e2):RelWithDebInfo,/W4 /permissive- /utf-8, Windows SDK 10.0.26100.0): all 149 dependent runtime targets includinggta6rt_d3d12_com(d3d12_vulkan_device.cpp),arena_staging_test, andgeometry_views_testbuilt cleanly with zero new warnings.arena_staging_test: PASS (5/5 normal runs, exit 0; 3/3 MSVC/fsanitize=addressruns, exit 0).geometry_views_test: PASS (89 checks, 0 failures).title_window,d3d12_indirect_rt,arena_staging,address_guard,title_window_plan_bounds,console_heap_slot_records,replay_state_cache,title_window_plan,geometry_views): 9/9 PASS.python -I scripts/check_no_game_data.py: PASS (exit 0).Threads::Threadsinruntime/CMakeLists.txt,uintptr_toffset helper in tests, 4-thread concurrent upload/readback coverage, and explicit<algorithm>/<map>headers +mutable copies; Round 2 verdictAPPROVE.GPUs tested: NVIDIA RTX A2000 12GB (driver 621.47, Vulkan 1.4.357, Windows 11 Pro build 26300.10075) — targeted CTests above including
d3d12_indirect_rt; unit tests inarena_staging_testandgeometry_views_testrun without a GPU.AI-assisted: yes (Gemini 4 Neon — implementation, tests, and separate read-only review pass).
Checklist
main(acb0657e2) and merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed (n/a — runtime C++ fix only).@Kal0s-design on GitHub · 2026-12-03 15:28 UTC · original
Ran natively on Windows 11, head
6c96c8968merged into currentmain(75ebcfe67, clean merge).Both bugs are real on
mainand gone on the merge. Onmain, ASan catchescommit_uploadsreading the freedStagedSloton one thread, right after a staged block is destroyed. With threads it also catchesstaging_statsandrecord_uploadsdoing the same. One cost to know about: the lookups now take ashared_mutex, so stagedmark_dirty_ptrfrom 4 threads at once is about 2x slower with few blocks.Tests (measured). MSVC, RelWithDebInfo. The unmodified merge was built with CMake + Ninja, only
arena_staging_test. The PR's test file compiles againstmain's header unchanged, so I ran it on both. For the table I built withcldirectly using the target's flags, with or without/fsanitize=address, and changed onlymain()so each test runs on its own and a failure doesn't hide the next one.mainmain, ASanstaged_destroy_unpublishes_and_preserves_statsst1.readbacks == 0commit_uploads, freed indestroy_blockpublished_slot_reuse_past_4096_blocksvisited == 3 && saw_newconcurrent_readers_during_destroy_and_reusestaging_stats, 1 inrecord_uploadsviaArenaStaging::drain)The unmodified merge build passed 5/5 runs. The concurrent test goes red on
mainonly under ASan. In the plain build the freed memory still reads fine.Read, not run: in the runtime,
allocate/freetakeexternal_windows_mutex, and the upload record/commit callbacks are called from the submit path invulkan_backend.h, where I didn't see that lock taken. Frees are deferred until a retire ticket completes, so I can't say how often a destroy overlaps a submit. The single-thread case doesn't need a race. A staged block destroyed with a pendinglast_uploadis enough.Slot reuse (measured, throwaway cases with the test's mock device, no real memory allocated). It's not a free list. Destroy swap-erases the entry from a
std::vectorunder the unique lock, and readers scan the vector under the shared lock. There's no cap left and no log when it grows. Readback slot tokens are now a monotonic block id, so a stale token can't alias a new block.mainfind_mappedandfor_each_blockfind the new blockfind_mappedhitsfreeagain on an already destroyed blockThe order
for_each_blockreports changes after a swap-erase. The one consumer I found (the console heap table) goes throughmerge_console_heap_spans, which sorts, so that's fine. Destroy during iteration only happens indrop_unfitting_spare, which returns right after the destroy.Lookup cost (measured, staged blocks, ns per call per thread,
main→ merge, two runs each with similar numbers). 2M calls per thread forfind_mappedandmark_dirty_ptr(always the same 64 bytes), 100k single-thread calls forfor_each_block.mark_dirty_ptr1 threadmark_dirty_ptr4 threadsfind_mapped1 threadfind_mapped4 threadsfor_each_blockmark_dirty_ptralready contended onmain(the per-blockArenaStagingmutex), and the shared lock adds about 150 ns per call with 1-16 blocks. It only applies in staged mode and only to pointers outside the arena (arena_mark_dirty_ptrreturns early for those inside). Thefind_mappedmulti-thread column matters less: its only runtime caller,copy_destination, runs undershadow_import_mutex.for_each_blocknow builds a vector on every call, but the runtime calls it about twice per submission. I didn't check how often the game reaches the externalmark_dirty_ptrpath from several threads.Read, not run: lock order is
published_mutex_thenArenaStaging::mutex_everywhere (readers drain/mark under the shared lock, destroy reads stats under the unique lock), and nothing takes the shared lock again while holding it.for_each_blockcalls the callback outside the lock.title_block_goneruns after the lock is released.Conflicts: four other open PRs touch these files. #3331 (also
arena_staging_test.cpp) merges cleanly on top of this merge, and that combinedarena_staging_testbuilds and passes 3/3. #2807 and #2567 merge cleanly. #2580 conflicts withmainalready, in Rust files this PR doesn't touch. This PR adds none.Not checked: the game, and whether the lock shows up in frame times. Builds other than
arena_staging_test. Other CTests from the PR description. Thread counts above 4.GPUs tested: n/a (CPU-only tests).
AI-assisted: yes (Claude Code).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.