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

Open
github-bot wants to merge 1 commit from github-bot/gh-1975 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 17:12 UTC · original
IlyassRS3:fix/rt-image-memory-unlock-alloc-and-overflow → main · +111 −21 · 2 files · 1 commits · open

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.
**@⁠IlyassRS3** on GitHub · 2026-11-25 17:12 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1975) `IlyassRS3:fix/rt-image-memory-unlock-alloc-and-overflow` → `main` · +111 −21 · 2 files · 1 commits · open 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:13:22 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 17:13 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 17:13 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1975#pullrequestreview-6061851224) · **reviewed** 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1975/head:github-bot/gh-1975'
git switch 'github-bot/gh-1975'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1975'
git switch 'github-bot/gh-1975'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1975'
git switch 'github-bot/gh-1975'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1975'
git switch 'main'
git merge --squash 'github-bot/gh-1975'
git switch 'main'
git merge --ff-only 'github-bot/gh-1975'
git switch 'main'
git merge 'github-bot/gh-1975'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.