runtime: unlock ImageMemoryPool across driver allocations/frees and fix BlockRanges alignment overflow #1975

Merged
leonida/mandy
quietfold merged 1 commit from fix/rt-image-memory-unlock-alloc-and-overflow into main 2026-11-28 17:56:28 +00:00

Pull request successfully merged and closed

quietfold commented 2026-11-25 17:12:58 +00:00

Claim: RT-IMAGE-MEMORY-UNLOCK-ALLOC-AND-OVERFLOW (claim comment #7169137144 on #1608).

Fixes (runtime/src/image_memory.h)

  1. Unlock mutex_ across allocate_() and failure-path free_() in ImageMemoryPool::allocate():
    • Previously allocate() held mutex_ across allocate_(type, block_bytes) (a 100+ µs vkAllocateMemory kernel call) and across failure-path free_(memory) — stalling every concurrent texture allocation, stats() query, and DeferredDeletionQueue range release, and risking AB-BA lock-order inversion with driver/device locks (release() already unlocked mutex_ before calling free_()).
    • Now all allocate_() and free_() callbacks run outside mutex_, with exception-safe cleanup if std::make_unique<Block> throws.
  2. Fix uint64_t wraparound in BlockRanges::allocate() alignment calculation:
    • Previously (piece + alignment - 1) / alignment * alignment could wrap uint64_t for large alignment on a non-zero piece.
    • Replaced with overflow-free modulo arithmetic (rem = piece % alignment; lead = rem ? alignment - rem : 0).
  3. Single-lookup coalescing in BlockRanges::release():
    • Uses one by_offset_.lower_bound(offset) and iterator-based erasure instead of two separate tree lookups, and guards zero-size releases.

Verification

  • AI-assisted: yes.
  • GPUs tested: none (host is macOS arm64; no retail files; 0 CMakeLists.txt changes).
  • Expanded runtime/test/image_memory_test.cpp with allocate_runs_unlocked() (blocks inside allocate_() while verifying another thread completes an existing-block allocation, stats(), and release() without deadlocking) plus BlockRanges alignment-overflow (UINT64_MAX, (1<<63)+1, 0) and zero-size release tests.
  • Ran image_memory_test cleanly under:
    • clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror -pthread
    • clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror -pthread
    • clang++ -std=c++20 -O2 -Wall -Wextra -Werror -pthread
  • Ran python3 -I scripts/check_no_game_data.py on both files and python3 -I tests/test_text_write_line_endings.py: all passed.
Claim: `RT-IMAGE-MEMORY-UNLOCK-ALLOC-AND-OVERFLOW` (claim comment `#7169137144` on #1608). ### Fixes (`runtime/src/image_memory.h`) 1. **Unlock `mutex_` across `allocate_()` and failure-path `free_()` in `ImageMemoryPool::allocate()`**: - Previously `allocate()` held `mutex_` across `allocate_(type, block_bytes)` (a 100+ µs `vkAllocateMemory` kernel call) and across failure-path `free_(memory)` — stalling every concurrent texture allocation, `stats()` query, and `DeferredDeletionQueue` range release, and risking AB-BA lock-order inversion with driver/device locks (`release()` already unlocked `mutex_` before calling `free_()`). - Now all `allocate_()` and `free_()` callbacks run outside `mutex_`, with exception-safe cleanup if `std::make_unique<Block>` throws. 2. **Fix `uint64_t` wraparound in `BlockRanges::allocate()` alignment calculation**: - Previously `(piece + alignment - 1) / alignment * alignment` could wrap `uint64_t` for large `alignment` on a non-zero `piece`. - Replaced with overflow-free modulo arithmetic (`rem = piece % alignment; lead = rem ? alignment - rem : 0`). 3. **Single-lookup coalescing in `BlockRanges::release()`**: - Uses one `by_offset_.lower_bound(offset)` and iterator-based erasure instead of two separate tree lookups, and guards zero-size releases. ### Verification - **AI-assisted**: yes. - **GPUs tested**: none (host is macOS arm64; no retail files; 0 `CMakeLists.txt` changes). - Expanded `runtime/test/image_memory_test.cpp` with `allocate_runs_unlocked()` (blocks inside `allocate_()` while verifying another thread completes an existing-block allocation, `stats()`, and `release()` without deadlocking) plus `BlockRanges` alignment-overflow (`UINT64_MAX`, `(1<<63)+1`, `0`) and zero-size release tests. - Ran `image_memory_test` cleanly under: - `clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror -pthread` - `clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror -pthread` - `clang++ -std=c++20 -O2 -Wall -Wextra -Werror -pthread` - Ran `python3 -I scripts/check_no_game_data.py` on both files and `python3 -I tests/test_text_write_line_endings.py`: all passed.
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 17:13:10 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
zmasi commented 2026-11-28 12:38:45 +00:00

Verified on Windows 11 (10.0.26200.9974) with MSVC 19.44.35228 (x64, /std:c++20 /EHsc /W4 /permissive- /utf-8 /O2) against main d48a957bb + 1f1248b95d (claim #2570#issuecomment-7239309422):

  • Line-by-line diff audit: Clean (runtime/src/image_memory.h, runtime/test/image_memory_test.cpp); avoids (piece + alignment - 1) uint64_t wraparound via piece % alignment, guards zero size/alignment, reuses the lower_bound iterator in BlockRanges::release, and unlocks mutex_ across driver allocate_() / free_(). check_no_game_data.py clean.
  • Red (d48a957bb + PR test): image_memory_test.exe deadlocks (TIMEOUT after 12s) in allocate_runs_unlocked() because main holds mutex_ across allocate_().
  • Green (d48a957bb + 1f1248b95d merge): Auto-merges cleanly (rc=0), compiles with 0 warnings under /W4 /permissive-, and passes (image memory: textures share blocks, and released ranges are used again, rc=0).
  • Coordination note with #1698: Confirmed git merge-tree conflicts with sibling PR #1698 on image_memory.h / image_memory_test.cpp; when reconciling the two, keep #1698's pre-allocated Range + pool.blocks.reserve(...) so push_back / new Range after lock.lock() cannot leak memory or ranges_ under std::bad_alloc.

AI-assisted.

Verified on Windows 11 (`10.0.26200.9974`) with MSVC `19.44.35228` (`x64`, `/std:c++20 /EHsc /W4 /permissive- /utf-8 /O2`) against `main` `d48a957bb` + `1f1248b95d` (claim `#2570#issuecomment-7239309422`): - **Line-by-line diff audit**: Clean (`runtime/src/image_memory.h`, `runtime/test/image_memory_test.cpp`); avoids `(piece + alignment - 1)` `uint64_t` wraparound via `piece % alignment`, guards zero size/alignment, reuses the `lower_bound` iterator in `BlockRanges::release`, and unlocks `mutex_` across driver `allocate_()` / `free_()`. `check_no_game_data.py` clean. - **Red (`d48a957bb` + PR test)**: `image_memory_test.exe` deadlocks (`TIMEOUT after 12s`) in `allocate_runs_unlocked()` because `main` holds `mutex_` across `allocate_()`. - **Green (`d48a957bb` + `1f1248b95d` merge)**: Auto-merges cleanly (`rc=0`), compiles with `0` warnings under `/W4 /permissive-`, and passes (`image memory: textures share blocks, and released ranges are used again`, `rc=0`). - **Coordination note with `#1698`**: Confirmed `git merge-tree` conflicts with sibling PR `#1698` on `image_memory.h` / `image_memory_test.cpp`; when reconciling the two, keep `#1698`'s pre-allocated `Range` + `pool.blocks.reserve(...)` so `push_back` / `new Range` after `lock.lock()` cannot leak `memory` or `ranges_` under `std::bad_alloc`. AI-assisted.
MoonEyes commented 2026-11-28 17:56:09 +00:00

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.

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.
OreoFR commented 2026-11-28 21:57:23 +00:00

Verified by OreoFR

  • Base: main c1ecf04ed + PR head 1f1248b95; green run in a merge package of 16 PRs, no new ctest failure vs main
  • Test: ctest -R ^image_memory$
  • Red on main: hang, 300 s timeout (pool locked during the driver allocation; 0.4 s on main without the test)
  • Green with the PR: OK (0.4 s)
  • Verdict: READY
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

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

**Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `1f1248b95`; green run in a merge package of 16 PRs, no new ctest failure vs main - Test: `ctest -R ^image_memory$` - Red on main: hang, 300 s timeout (pool locked during the driver allocation; 0.4 s on main without the test) - Green with the PR: OK (0.4 s) - Verdict: READY - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
Sign in to join this conversation.
No description provided.