Skip to content

runtime: harden Depth24Converter bounds, rollback, and semaphore-cached collect (RT-VK-DEPTH24-CONVERTER-OVERFLOW-AND-COLLECT) - #1988

Open
IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:fix/rt-vk-depth24-converter-overflow-and-collect
Open

IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:fix/rt-vk-depth24-converter-overflow-and-collect

Conversation

@IlyassRS3

Copy link
Copy Markdown
Contributor

Item ID: RT-VK-DEPTH24-CONVERTER-OVERFLOW-AND-COLLECT (claimed in #1608)

Summary

Hardens and optimizes leonida::Depth24Converter (runtime/src/vulkan_depth24.h), which backs D24_UNORM_S8_UINT copies on GPUs without native D24 (including MoltenVK/macOS and AMD/Intel drivers) as well as 96-bit R32G32B32 texel repacks:

  1. Null loader/device guard in Functions::load(): rejects null PFN_vkGetDeviceProcAddr or VK_NULL_HANDLE device before dereferencing get, so loaded() returns false and ~Depth24Converter() skips all teardown calls safely.
  2. Overflow-proof 64-bit validation and O(1) partitioning in prepare() / make_part() / scratch() / run():
    • Guards (height - 1) * app_row_words + aw, (slices - 1) * app_slice_words + one_slice_words, span_words * 4, app_offset + span_bytes, sw * h * s * 4, caller scratch_offset % 4 / scratch_offset + total_scratch_bytes, and make_part()'s window + range > app_size against uint64_t wrap.
    • Replaces the O(slices + rows) linear while loops (which could wrap uint64_t in app_bytes/scratch_bytes) with exact O(1) division, and validates max_range_ > 256 and row/slice feasibility before allocating any internal scratch buffer.
    • Guards scratch() against alignment/addition wrap and uses bitmask alignment (& ~(alignment - 1)).
    • Guards run() against unloaded/zero extents and uses (extent - 1u) / 8u + 1u so width/height near UINT32_MAX cannot wrap to 0 workgroups.
  3. Atomic rollback on mid-loop make_part() failure: clears parts and restores whole.scratch / whole.scratch_offset if descriptor allocation fails partway through a multi-part copy.
  4. Per-semaphore cached queries and O(N) compaction in collect() and forget():
    • Caches vkGetSemaphoreCounterValue() per distinct VkSemaphore within each collect() pass (avoiding redundant per-batch driver calls and repeated DeviceFaultDiagnostics::observe() calls on a lost semaphore).
    • Replaces O(N²) std::vector::erase() loops with in-place compaction + single resize().
  5. New portable pure-CPU unit test runtime/test/vulkan_depth24_unit_test.cpp wired into runtime/CMakeLists.txt (vulkan_depth24_unit) covering all 6 areas with mock Vulkan dispatch callbacks (no GPU or Vulkan loader required).

Testing & Disclosure

  • AI-assisted: yes
  • GPUs tested: none (macOS host, CPU-only portable unit test with mock Vulkan dispatch)
  • clang++ -std=c++20 -g -O1 -fsanitize=address,undefined -fno-omit-frame-pointer -Wall -Wextra -Werror -Wno-missing-field-initializers -I<vulkan-headers> runtime/test/vulkan_depth24_unit_test.cpp: PASS
  • clang++ -std=c++20 -g -O1 -fsanitize=thread -fno-omit-frame-pointer -Wall -Wextra -Werror -Wno-missing-field-initializers -I<vulkan-headers> runtime/test/vulkan_depth24_unit_test.cpp: PASS
  • clang++ -std=c++20 -O2 -Wall -Wextra -Werror -Wno-missing-field-initializers -I<vulkan-headers> runtime/test/vulkan_depth24_unit_test.cpp: PASS
  • python3 -I scripts/check_no_game_data.py runtime/src/vulkan_depth24.h runtime/test/vulkan_depth24_unit_test.cpp runtime/CMakeLists.txt: PASS
  • python3 -I tests/test_text_write_line_endings.py: PASS (3/3)

Copilot AI balanced review requested due to automatic review settings November 25, 2026 17:30

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.

@zmasi

zmasi commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

#1988 | head 7267cd0 (merged cleanly with main 4150b1a) | ctest --test-dir runtime/build -C RelWithDebInfo -R "^(vulkan_depth24_unit|d3d12_depth_stencil_uav)$" --output-on-failure (LEONIDA_VK_VALIDATION=1) | red on main: vulkan_depth24_unit crashes (SEGFAULT dereferencing null PFN_vkGetDeviceProcAddr in Functions::load()) | green on PR+main: 2/2 CTests pass (vulkan_depth24_unit + d3d12_depth_stencil_uav) on RTX 3090 with 0 Khronos validation errors; check_no_game_data.py 0 violations | Windows 11, MSVC 19.44, RTX 3090, Vulkan SDK 1.4.363.1 | AI-assisted: yes

@TheGeogeo

Copy link
Copy Markdown
Contributor

Checked 7267cd00d against main 8fb974878 on Windows 11 10.0.26300: it does not merge (git merge-tree --write-tree), so I ran no tests.

AI-assisted (Claude Code).

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

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants