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

Open
opened 2026-11-26 21:19:37 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1975
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/rt-image-memory-unlock-alloc-and-overflow


📝 Commits (1)

  • 1f1248b runtime: unlock ImageMemoryPool across driver allocations and fix BlockRanges alignment overflow

📊 Changes

2 files changed (+111 additions, -21 deletions)

View changed files

📝 runtime/src/image_memory.h (+41 -21)
📝 runtime/test/image_memory_test.cpp (+70 -0)

📄 Description

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.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1975 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-image-memory-unlock-alloc-and-overflow` --- ### 📝 Commits (1) - [`1f1248b`](https://github.com/ubiquitarianism/leonida/commit/1f1248b95d1050baf103a2cdb5967d32f3b2333d) runtime: unlock ImageMemoryPool across driver allocations and fix BlockRanges alignment overflow ### 📊 Changes **2 files changed** (+111 additions, -21 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/image_memory.h` (+41 -21) 📝 `runtime/test/image_memory_test.cpp` (+70 -0) </details> ### 📄 Description 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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
starred/leonida#1981
No description provided.