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

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

@⁠IlyassRS3 on GitHub · 2026-11-25 15:54 UTC · original
IlyassRS3:fix/rt-title-image-pool-lock-and-guards → main · +258 −15 · 2 files · 1 commits · open

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)
**@⁠IlyassRS3** on GitHub · 2026-11-25 15:54 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1926) `IlyassRS3:fix/rt-title-image-pool-lock-and-guards` → `main` · +258 −15 · 2 files · 1 commits · open 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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:11:32 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 16:01 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 16:01 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1926#pullrequestreview-6060884790) · **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.
Author
Owner

@⁠GGandCo on GitHub · 2026-11-25 16:32 UTC · original

Native Windows/Intel verification of #1926 (0d7871f50) together with #1920, on main b0f3d3998 plus local integration fixes:

  • Intel UHD Graphics, driver 32.0.101.6314, MSVC 14.51, Vulkan SDK 1.4.363.1.
  • title_va_alias, title_window_plan, title_window_plan_bounds and vulkan_title_image_pool: 4/4 passed.
  • xmem, d3d12_xmem_placed (production batch route, no injected mapping hook), d3d12_host_prefix and d3d12_texture_memory: 4/4 passed with LEONIDA_VK_VALIDATION=1.
  • Focused staged plane/BC-array, aliased-guard and mapped single-byte WB/WC readback cases also passed. Host image copies were disabled; on-demand pipelines enabled.

Combined integration, not an isolated A/B of this PR. No successful retail load or performance improvement claimed. The full existing_heap fixture still has a pre-existing first-render-target device-loss failure on this Intel setup, reproduced before these changes; the focused cases above were run separately. No game data included. AI-assisted with OpenAI Codex.

**@⁠GGandCo** on GitHub · 2026-11-25 16:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1926#issuecomment-7168138140) Native Windows/Intel verification of #1926 (0d7871f50) together with #1920, on main b0f3d3998 plus local integration fixes: - Intel UHD Graphics, driver 32.0.101.6314, MSVC 14.51, Vulkan SDK 1.4.363.1. - title_va_alias, title_window_plan, title_window_plan_bounds and vulkan_title_image_pool: 4/4 passed. - xmem, d3d12_xmem_placed (production batch route, no injected mapping hook), d3d12_host_prefix and d3d12_texture_memory: 4/4 passed with LEONIDA_VK_VALIDATION=1. - Focused staged plane/BC-array, aliased-guard and mapped single-byte WB/WC readback cases also passed. Host image copies were disabled; on-demand pipelines enabled. Combined integration, not an isolated A/B of this PR. No successful retail load or performance improvement claimed. The full existing_heap fixture still has a pre-existing first-render-target device-loss failure on this Intel setup, reproduced before these changes; the focused cases above were run separately. No game data included. AI-assisted with OpenAI Codex.
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/1926/head:github-bot/gh-1926'
git switch 'github-bot/gh-1926'

Merge

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