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

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

@⁠IlyassRS3 on GitHub · 2026-11-25 17:30 UTC · original
IlyassRS3:fix/rt-vk-depth24-converter-overflow-and-collect → main · +489 −35 · 3 files · 1 commits · open

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)
**@⁠IlyassRS3** on GitHub · 2026-11-25 17:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1988) `IlyassRS3:fix/rt-vk-depth24-converter-overflow-and-collect` → `main` · +489 −35 · 3 files · 1 commits · open 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)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:14:01 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 17:30 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 17:30 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1988#pullrequestreview-6062103375) · **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.
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/1988/head:github-bot/gh-1988'
git switch 'github-bot/gh-1988'

Merge

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