runtime: unlock ImageMemoryPool across driver allocations/frees and fix BlockRanges alignment overflow #1975
No reviewers
No assignees
Labels
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
No project
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
No due date set.
Reference
leonida/mandy!1975
Loading…
Reference in a new issue
No description provided.
Delete branch "fix/rt-image-memory-unlock-alloc-and-overflow"
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?
Pull request successfully merged and closed
Claim:
RT-IMAGE-MEMORY-UNLOCK-ALLOC-AND-OVERFLOW(claim comment#7169137144on #1608).Fixes (
runtime/src/image_memory.h)mutex_acrossallocate_()and failure-pathfree_()inImageMemoryPool::allocate():allocate()heldmutex_acrossallocate_(type, block_bytes)(a 100+ µsvkAllocateMemorykernel call) and across failure-pathfree_(memory)— stalling every concurrent texture allocation,stats()query, andDeferredDeletionQueuerange release, and risking AB-BA lock-order inversion with driver/device locks (release()already unlockedmutex_before callingfree_()).allocate_()andfree_()callbacks run outsidemutex_, with exception-safe cleanup ifstd::make_unique<Block>throws.uint64_twraparound inBlockRanges::allocate()alignment calculation:(piece + alignment - 1) / alignment * alignmentcould wrapuint64_tfor largealignmenton a non-zeropiece.rem = piece % alignment; lead = rem ? alignment - rem : 0).BlockRanges::release():by_offset_.lower_bound(offset)and iterator-based erasure instead of two separate tree lookups, and guards zero-size releases.Verification
CMakeLists.txtchanges).runtime/test/image_memory_test.cppwithallocate_runs_unlocked()(blocks insideallocate_()while verifying another thread completes an existing-block allocation,stats(), andrelease()without deadlocking) plusBlockRangesalignment-overflow (UINT64_MAX,(1<<63)+1,0) and zero-size release tests.image_memory_testcleanly under:clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror -pthreadclang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror -pthreadclang++ -std=c++20 -O2 -Wall -Wextra -Werror -pthreadpython3 -I scripts/check_no_game_data.pyon both files andpython3 -I tests/test_text_write_line_endings.py: all passed.Verified on Windows 11 (
10.0.26200.9974) with MSVC19.44.35228(x64,/std:c++20 /EHsc /W4 /permissive- /utf-8 /O2) againstmaind48a957bb+1f1248b95d(claim#2570#issuecomment-7239309422):runtime/src/image_memory.h,runtime/test/image_memory_test.cpp); avoids(piece + alignment - 1)uint64_twraparound viapiece % alignment, guards zero size/alignment, reuses thelower_bounditerator inBlockRanges::release, and unlocksmutex_across driverallocate_()/free_().check_no_game_data.pyclean.d48a957bb+ PR test):image_memory_test.exedeadlocks (TIMEOUT after 12s) inallocate_runs_unlocked()becausemainholdsmutex_acrossallocate_().d48a957bb+1f1248b95dmerge): Auto-merges cleanly (rc=0), compiles with0warnings under/W4 /permissive-, and passes (image memory: textures share blocks, and released ranges are used again,rc=0).#1698: Confirmedgit merge-treeconflicts with sibling PR#1698onimage_memory.h/image_memory_test.cpp; when reconciling the two, keep#1698's pre-allocatedRange+pool.blocks.reserve(...)sopush_back/new Rangeafterlock.lock()cannot leakmemoryorranges_understd::bad_alloc.AI-assisted.
Red/green: On main image_memory_test hangs until the 300 s timeout (the pool stays locked across driver allocations), and with the PR ctest -R "^image_memory$" passes in 0.4 s.
Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.
Verified by OreoFR
c1ecf04ed+ PR head1f1248b95; green run in a merge package of 16 PRs, no new ctest failure vs mainctest -R ^image_memory$AI-assisted (Claude Code, Claude Opus 5.5).