runtime: unpublish destroyed TitleBufferWindows blocks and remove 4096 slot ceiling #3349
Code
Clone this repository
Checkout
From your project repository, check out a new branch and test the changes.
git fetch -u origin "github-pr/3349:github-pr/3349" git switch "github-pr/3349"
Open
leonida/mandy
LewisStephens99 wants to merge 1 commit from
github-pr/3349 into main
pull from: github-pr/3349
merge into: leonida:main
leonida:main
leonida:github-pr/3295
leonida:beta
leonida:docs/changelog-3483-3500
leonida:github-pr/3294
leonida:github-pr/2975
leonida:github-pr/2815
leonida:feat/gate-driver-start
leonida:feat/skip-pipelines-release
leonida:perf/lazy-compare
leonida:perf/mwaitx-wait
leonida:test/prio-ceiling-main
leonida:fix/ab-cpu-stdout
leonida:fix/gate-old-crashes
leonida:diag/ab-cpu
leonida:test/main-0312
leonida:github-pr/3291
leonida:diag/indirect-clamp-census
leonida:feat/barrier-enhanced
leonida:perf/texture-frame-ceiling
leonida:perf/dstorage-threads
leonida:fix/console-queue-dma-tables
leonida:fix/port-descriptors-record-parts
leonida:fix/frame-waits-fence
leonida:fix/console-sampler-words
leonida:fix/graphics-userdata-spill
leonida:fix/xg-reserved-layout-rotation
leonida:fix/barrier-console-flags
leonida:fix/switches-wait-test-child
leonida:test/list-value-waits
leonida:test/lot-gxdk-0412
leonida:fix/bvh-compacted-size
leonida:docs/i18n-quickstarts
leonida:feat/native-pipeline-slots
leonida:fix/clippy-leonida-gpu
leonida:fix/list-value-producers
leonida:fix/descriptor-page-straddle
leonida:fix/xmem-low-window
leonida:fix/ffx-jitter-never-zero
leonida:fix/run-stdout-closed
leonida:github-pr/2892
leonida:github-pr/2896
leonida:feat/warm-save-every
leonida:github-pr/2980
leonida:github-pr/2994
leonida:github-pr/3005
leonida:github-pr/3010
leonida:github-pr/3136
leonida:github-pr/3303
leonida:github-pr/3220
leonida:github-pr/3143
leonida:github-pr/3330
leonida:github-pr/3226
leonida:github-pr/3187
leonida:carry/3395
leonida:github-pr/2946
leonida:github-pr/2947
leonida:github-pr/2945
leonida:github-pr/2983
leonida:github-pr/3001
leonida:github-pr/3372
leonida:github-pr/3128
leonida:github-pr/3296
leonida:github-pr/2588
leonida:github-pr/3119
leonida:github-pr/3378
leonida:fix/crash-diagnostics
leonida:feat/dlss-mip-bias
leonida:fix/retired-out-of-order
leonida:fix/dxr-postbuild-barrier
leonida:fix/amd-relay-default
leonida:fix/removed-report-wait
leonida:feat/heap-twin
leonida:feat/vram-margin
leonida:github-pr/2472
leonida:github-pr/3123
leonida:github-pr/3088
leonida:assets/annonce-v1
leonida:github-pr/2794
leonida:github-pr/3300
leonida:github-pr/3304
leonida:github-pr/3308
leonida:github-pr/3367
leonida:github-pr/3369
leonida:fix/launcher-dlss-diagnostics
leonida:github-pr/3269
leonida:github-pr/2957
leonida:github-pr/3205
leonida:fix/crash-zero-guard
leonida:github-pr/3185
leonida:github-pr/3099
leonida:github-pr/3104
leonida:github-pr/3348
leonida:github-pr/3379
leonida:github-pr/3377
leonida:perf/darner-title-priority-ceiling
leonida:github-pr/3376
leonida:gh-readonly-queue/main/pr-3311-b5c45b663dbc5a9c3532bb3f307e5f81b32faefa
leonida:gh-readonly-queue/main/pr-3088-af22e7847ff5808f72d1a1881c24be2fafd20b54
leonida:gh-readonly-queue/main/pr-3123-7f3fa026a94c6646e89244f28e1ce22290361867
leonida:github-pr/3373
leonida:github-pr/3368
leonida:github-pr/3198
leonida:gh-readonly-queue/main/pr-3354-637baa0b77ba47222e7280b17c1a59c6a1781d57
leonida:gh-readonly-queue/main/pr-3365-d7a0e0196a43834813014b4bd3aa5502a8263b14
leonida:gh-readonly-queue/main/pr-3361-5dfe8bbd05ed12fb2f2e1718d9af77a7c5f46fd8
leonida:gh-readonly-queue/main/pr-3366-8da9148204e9d12c585efa9ac2b7cc1ab08c7682
leonida:github-pr/3346
leonida:github-pr/3375
leonida:github-pr/2958
leonida:github-pr/3151
leonida:github-pr/3307
leonida:github-pr/3278
leonida:github-pr/3374
leonida:fix/runtime-test-conservative-fixtures
leonida:github-pr/3366
leonida:ci/shard-semantic-linux
leonida:github-pr/3354
leonida:docs/fg-limit-fps-switch
leonida:github-pr/3365
leonida:fix/read-file-hard-link
leonida:github-pr/3364
leonida:fix/book-slot-heap-never-read
leonida:github-pr/3361
leonida:github-pr/3263
leonida:github-pr/3360
leonida:github-pr/3353
leonida:github-pr/3331
leonida:fix/alias-later-queued-submissions
leonida:github-pr/3352
leonida:github-pr/3228
leonida:claude/gallant-sagan-aamatr
leonida:github-pr/3200
leonida:github-pr/3132
leonida:github-pr/3323
leonida:github-pr/3332
leonida:github-pr/3340
leonida:github-pr/3311
leonida:github-pr/3334
leonida:github-pr/3297
leonida:github-pr/3256
leonida:github-pr/2567
leonida:github-pr/2851
leonida:d3d12/agility-sdk-621
leonida:github-pr/3164
leonida:github-pr/3114
leonida:github-pr/3050
leonida:github-pr/3113
leonida:github-pr/3326
leonida:github-pr/3144
leonida:github-pr/2642
leonida:github-pr/3072
leonida:github-pr/2789
leonida:github-pr/2643
leonida:github-pr/3009
leonida:github-pr/3035
leonida:github-pr/3031
leonida:github-pr/2686
leonida:github-pr/2834
leonida:github-pr/3321
leonida:github-pr/3325
leonida:github-pr/3290
leonida:github-pr/3324
leonida:github-pr/3130
leonida:claude/fervent-curie-m19lnr
leonida:github-pr/3309
leonida:github-pr/3305
leonida:github-pr/3306
leonida:github-pr/3302
leonida:github-pr/3301
leonida:github-pr/3345
leonida:github-pr/3211
leonida:github-pr/3027
leonida:github-pr/3286
leonida:github-pr/3235
leonida:github-pr/3265
leonida:github-pr/3275
leonida:codex/skip-no-hit-ray-passes
leonida:github-pr/2823
leonida:github-pr/2911
leonida:github-pr/3262
leonida:github-pr/3125
leonida:github-pr/3206
leonida:github-pr/3250
leonida:github-pr/3238
leonida:github-pr/3188
leonida:github-pr/2952
leonida:github-pr/3217
leonida:github-pr/3168
leonida:t3/preserve-fsr-sdk-restart-path
leonida:t3/propagate-host-failure-hresult
leonida:github-pr/3209
leonida:t3/launcher-custom-parameters
leonida:github-pr/3207
leonida:github-pr/3183
leonida:github-pr/3160
leonida:github-pr/3201
leonida:ci/less-to-run
leonida:github-pr/2930
leonida:github-pr/2797
leonida:github-pr/2999
leonida:perf/native-compute-shader-forms
leonida:github-pr/3191
leonida:github-pr/3011
leonida:github-pr/3019
leonida:github-pr/2996
leonida:gh-readonly-queue/main/pr-2789-45dc41f95c1cd0dcc29c2c50582e0e9af8a77259
leonida:claude/blissful-franklin-ukp7ag
leonida:github-pr/3141
leonida:github-pr/3156
leonida:github-pr/3150
leonida:github-pr/3153
leonida:github-pr/3163
leonida:github-pr/3142
leonida:github-pr/3139
leonida:github-pr/2841
leonida:github-pr/3069
leonida:github-pr/3098
leonida:github-pr/3107
leonida:github-pr/3071
leonida:github-pr/2774
leonida:github-pr/2889
leonida:github-pr/3075
leonida:github-pr/2661
leonida:github-pr/3064
leonida:github-pr/3054
leonida:github-pr/3049
leonida:github-pr/2855
leonida:github-pr/2943
leonida:github-pr/3016
leonida:github-pr/2887
leonida:github-pr/3006
leonida:github-pr/3003
leonida:github-pr/2993
leonida:notes/card-time-and-plant-flicker
leonida:github-pr/2989
leonida:feat/vulkan-rs
leonida:github-pr/2985
leonida:github-pr/2951
leonida:github-pr/2982
leonida:github-pr/752
leonida:github-pr/792
leonida:github-pr/2877
leonida:github-pr/2654
leonida:github-pr/2893
leonida:github-pr/2948
leonida:github-pr/2944
leonida:github-pr/2919
leonida:github-pr/2915
leonida:archive/ci/less-to-run/20261202T1121Z
leonida:github-pr/2800
leonida:github-pr/2822
leonida:github-pr/2833
leonida:github-pr/2840
leonida:github-pr/2831
leonida:github-pr/2832
leonida:github-pr/2801
leonida:github-pr/2890
leonida:github-pr/2891
leonida:github-pr/2664
leonida:github-pr/2807
leonida:rt-call-trace
leonida:github-pr/2755
leonida:perf/ahead-split
leonida:github-pr/2587
leonida:github-pr/2698
leonida:github-pr/2609
leonida:github-pr/3219
leonida:docs/readme-runtime-proposal
leonida:github-pr/2017
leonida:github-pr/1741
leonida:github-pr/1667
leonida:codex/coord-claim-check-20261124-7e48
No reviewers
Assignees
Clear assignees
No assignees
Labels
Clear labels
bug
Something isn't working
dependencies
Pull requests that update a dependency file
documentation
Improvements or additions to documentation
enhancement
New feature or request
missing claim
Needs a claim in #14
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
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
Projects
Clear projects
No items
No project
Milestone
Clear milestone
No items
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".
No due date set.
Reference
leonida/mandy!3349
Loading…
Reference in a new issue
No description provided.
Delete branch "github-pr/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?
This branch is out-of-date with the base branch
Reviewers 0 approvals
Details
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Claimed in the coordination issue as: RT-TITLE-BUFFER-WINDOWS-DESTROY-AND-REUSE (https://github.com/ProjectLeonida/mandy/issues/3312#issuecomment-7382163942)
What and why
Fixes two bugs in
rt::d3d12::TitleBufferWindows(runtime/shims/d3d12_title_buffer_windows.h), flagged in https://github.com/ProjectLeonida/mandy/issues/3312#issuecomment-7379137473: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).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).