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

Closed
leonida/mandy
quietfold wants to merge 0 commits from fix/rt-vk-depth24-converter-overflow-and-collect into main

Pull request closed

This pull request cannot be reopened because the branch was deleted.

quietfold commented 2026-11-25 17:30:45 +00:00

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)
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-pull-request-reviewer[bot] reviewed 2026-11-25 17:30:58 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
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 commented 2026-11-28 11:58:03 +00:00

#1988 | head 7267cd00d9bb0971c78930ea54c0d3f2cf43329b (merged cleanly with main 4150b1a1cd9de753749e0417ff71a450b6ce92cc) | 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

#1988 | head 7267cd00d9bb0971c78930ea54c0d3f2cf43329b (merged cleanly with main 4150b1a1cd9de753749e0417ff71a450b6ce92cc) | `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 commented 2026-11-29 11:54:18 +00:00

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.

  • Conflict: runtime/CMakeLists.txt, add/add. f06577169 (#2527, 2026-11-27) added the address_guard_buffer_test block right after vulkan_scratch, where this PR adds vulkan_depth24_unit_test.
  • Still needed (STALE, needs a rebase): main has neither vulkan_depth24_unit_test nor this PR's runtime/src/vulkan_depth24.h changes. The rebase looks mechanical: keep both CMake blocks.

AI-assisted (Claude Code).

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. - Conflict: `runtime/CMakeLists.txt`, add/add. `f06577169` (#2527, 2026-11-27) added the `address_guard_buffer_test` block right after `vulkan_scratch`, where this PR adds `vulkan_depth24_unit_test`. - Still needed (STALE, needs a rebase): `main` has neither `vulkan_depth24_unit_test` nor this PR's `runtime/src/vulkan_depth24.h` changes. The rebase looks mechanical: keep both CMake blocks. AI-assisted (Claude Code).
aslan2142 commented 2026-12-01 16:40:49 +00:00

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.
Sign in to join this conversation.
No description provided.