[PR #1926] runtime: unlock TitleImagePool across allocate/bind, skip full pages lock-free, and guard bad types/procs (RT-TITLE-IMAGE-POOL-LOCK-AND-GUARDS) #1935

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

📋 Pull Request Information

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

Base: main ← Head: fix/rt-title-image-pool-lock-and-guards


📝 Commits (1)

  • 0d7871f runtime: unlock TitleImagePool across allocate/bind, skip full pages lock-free, and guard bad types/procs (RT-TITLE-IMAGE-POOL-LOCK-AND-GUARDS)

📊 Changes

2 files changed (+258 additions, -15 deletions)

View changed files

📝 runtime/src/vulkan_title_image_pool.h (+42 -13)
📝 runtime/test/vulkan_title_image_pool_test.cpp (+216 -2)

📄 Description

Item ID: RT-TITLE-IMAGE-POOL-LOCK-AND-GUARDS (claim: #1608, comment 7167409068)

Problem

In leonida::TitleImagePool (runtime/src/vulkan_title_image_pool.h, used to sub-allocate small optimal title textures):

  1. Coarse mutex held across Vulkan driver calls: TitleImagePool::bind() held mutex_ across both allocate(page_size, page->type) (device_.allocate() -> vkAllocateMemory) and bind_image() (vkBindImageMemory), serializing concurrent texture creation on unrelated size classes and already-available pages behind slow page allocation and driver bind calls.
  2. Per-full-page mutex lock on every scan: iterating classes_[stride] called page->take() (locking each page's std::mutex) even on pages that were already completely full.
  3. Premature publication and stale buckets: a newly allocated page was inserted into classes_[stride] before vkBindImageMemory succeeded, and empty expired size-class buckets stayed in classes_.
  4. Missing safety guards and shift UB:
    • req.memoryTypeBits & (1u << page->type) has undefined behavior if allocate() returns a type >= 32, and an incompatible type (not in req.memoryTypeBits) was neither rejected nor freed, allowing a mismatched page to enter the pool.
    • Non-power-of-two req.alignment corrupted (needed + quantum - 1) & ~(quantum - 1) stride rounding.
    • Null image, bind_image, destroy_image, or free_memory were not checked before allocating/destroying.

Fix

  • runtime/src/vulkan_title_image_pool.h:
    • Validate !image, null Vulkan procs, non-power-of-two req.alignment, and zero needed/memoryTypeBits up front before taking mutex_.
    • Track free_slots atomically (std::atomic<std::uint32_t>) on TitleImagePage so bind() skips full pages via has_free() without acquiring each full page's std::mutex.
    • Release TitleImagePool::mutex_ before calling allocate() and bind_image().
    • Validate page->memory && page->type < 32u && (req.memoryTypeBits & (1u << page->type)) immediately after allocate() (letting ~TitleImagePage() free page->memory cleanly on failure so no VRAM leaks).
    • Publish a newly created page into classes_[stride] only after bind_image() returns VK_SUCCESS, pruning expired weak_ptrs (and keeping the already-bound image intact if weak-pointer map insertion throws std::bad_alloc).
    • Guard ~TitleImagePage() and ~PooledTitleImage() against null function pointers/page pointers.
  • runtime/test/vulkan_title_image_pool_test.cpp:
    • Keep all existing alignment, type separation, retirement, reuse, and fallback checks unchanged.
    • Add coverage for null handles/procs, non-power-of-two alignments, out-of-range (type == 32) and incompatible memory types (verifying free_memory is called and no bad page is published), failed bind_image on fresh pages, two non-blocking concurrency proofs verifying mutex_ is released during allocate() and bind_image(), and a 4-thread TSan stress test across 3 size classes and 2 memory types with intermittent bind failures.

Verification

  • AI-assisted: yes
  • GPUs tested: none (macOS host, CPU-only unit tests against Khronos Vulkan-Headers)
  • Game files used: none
  • Commands run:
    • clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer runtime/test/vulkan_title_image_pool_test.cpp -> PASS
    • clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=thread -fno-omit-frame-pointer runtime/test/vulkan_title_image_pool_test.cpp -> PASS
    • clang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/vulkan_title_image_pool_test.cpp (x3) -> PASS
    • Downstream syntax check including runtime/src/vulkan_backend.h -> PASS
    • Negative mutation check running the new test against unpatched main:runtime/src/vulkan_title_image_pool.h -> FAILED as expected (exit 1)
    • python3 -I scripts/check_no_game_data.py runtime/src/vulkan_title_image_pool.h runtime/test/vulkan_title_image_pool_test.cpp -> PASS
    • python3 -I tests/test_text_write_line_endings.py -> PASS (3/3)

🔄 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/1926 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-title-image-pool-lock-and-guards` --- ### 📝 Commits (1) - [`0d7871f`](https://github.com/ubiquitarianism/leonida/commit/0d7871f50dea6f4d604ef33891bff14197864063) runtime: unlock TitleImagePool across allocate/bind, skip full pages lock-free, and guard bad types/procs (RT-TITLE-IMAGE-POOL-LOCK-AND-GUARDS) ### 📊 Changes **2 files changed** (+258 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/vulkan_title_image_pool.h` (+42 -13) 📝 `runtime/test/vulkan_title_image_pool_test.cpp` (+216 -2) </details> ### 📄 Description Item ID: `RT-TITLE-IMAGE-POOL-LOCK-AND-GUARDS` (claim: #1608, comment 7167409068) ## Problem In `leonida::TitleImagePool` (`runtime/src/vulkan_title_image_pool.h`, used to sub-allocate small optimal title textures): 1. **Coarse mutex held across Vulkan driver calls**: `TitleImagePool::bind()` held `mutex_` across both `allocate(page_size, page->type)` (`device_.allocate()` -> `vkAllocateMemory`) and `bind_image()` (`vkBindImageMemory`), serializing concurrent texture creation on unrelated size classes and already-available pages behind slow page allocation and driver bind calls. 2. **Per-full-page mutex lock on every scan**: iterating `classes_[stride]` called `page->take()` (locking each page's `std::mutex`) even on pages that were already completely full. 3. **Premature publication and stale buckets**: a newly allocated page was inserted into `classes_[stride]` before `vkBindImageMemory` succeeded, and empty expired size-class buckets stayed in `classes_`. 4. **Missing safety guards and shift UB**: - `req.memoryTypeBits & (1u << page->type)` has undefined behavior if `allocate()` returns a `type >= 32`, and an incompatible `type` (not in `req.memoryTypeBits`) was neither rejected nor freed, allowing a mismatched page to enter the pool. - Non-power-of-two `req.alignment` corrupted `(needed + quantum - 1) & ~(quantum - 1)` stride rounding. - Null `image`, `bind_image`, `destroy_image`, or `free_memory` were not checked before allocating/destroying. ## Fix - **`runtime/src/vulkan_title_image_pool.h`**: - Validate `!image`, null Vulkan procs, non-power-of-two `req.alignment`, and zero `needed`/`memoryTypeBits` up front before taking `mutex_`. - Track `free_slots` atomically (`std::atomic<std::uint32_t>`) on `TitleImagePage` so `bind()` skips full pages via `has_free()` without acquiring each full page's `std::mutex`. - Release `TitleImagePool::mutex_` before calling `allocate()` and `bind_image()`. - Validate `page->memory && page->type < 32u && (req.memoryTypeBits & (1u << page->type))` immediately after `allocate()` (letting `~TitleImagePage()` free `page->memory` cleanly on failure so no VRAM leaks). - Publish a newly created page into `classes_[stride]` only after `bind_image()` returns `VK_SUCCESS`, pruning expired `weak_ptr`s (and keeping the already-bound image intact if weak-pointer map insertion throws `std::bad_alloc`). - Guard `~TitleImagePage()` and `~PooledTitleImage()` against null function pointers/page pointers. - **`runtime/test/vulkan_title_image_pool_test.cpp`**: - Keep all existing alignment, type separation, retirement, reuse, and fallback checks unchanged. - Add coverage for null handles/procs, non-power-of-two alignments, out-of-range (`type == 32`) and incompatible memory types (verifying `free_memory` is called and no bad page is published), failed `bind_image` on fresh pages, two non-blocking concurrency proofs verifying `mutex_` is released during `allocate()` and `bind_image()`, and a 4-thread TSan stress test across 3 size classes and 2 memory types with intermittent bind failures. ## Verification - **AI-assisted**: yes - **GPUs tested**: none (macOS host, CPU-only unit tests against Khronos Vulkan-Headers) - **Game files used**: none - Commands run: - `clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer runtime/test/vulkan_title_image_pool_test.cpp` -> PASS - `clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=thread -fno-omit-frame-pointer runtime/test/vulkan_title_image_pool_test.cpp` -> PASS - `clang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/vulkan_title_image_pool_test.cpp` (x3) -> PASS - Downstream syntax check including `runtime/src/vulkan_backend.h` -> PASS - Negative mutation check running the new test against unpatched `main:runtime/src/vulkan_title_image_pool.h` -> FAILED as expected (exit 1) - `python3 -I scripts/check_no_game_data.py runtime/src/vulkan_title_image_pool.h runtime/test/vulkan_title_image_pool_test.cpp` -> PASS - `python3 -I tests/test_text_write_line_endings.py` -> PASS (3/3) --- <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#1935
No description provided.