Skip to content

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
IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:fix/rt-title-image-pool-lock-and-guards
Open

IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:fix/rt-title-image-pool-lock-and-guards

Conversation

@IlyassRS3

Copy link
Copy Markdown
Contributor

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)

…lock-free, and guard bad types/procs (RT-TITLE-IMAGE-POOL-LOCK-AND-GUARDS)
Copilot AI balanced review requested due to automatic review settings November 25, 2026 15:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@GGandCo

GGandCo commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Native Windows/Intel verification of #1926 (0d7871f) together with #1920, on main b0f3d39 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants