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

Open
opened 2026-11-26 21:19:40 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1988
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/rt-vk-depth24-converter-overflow-and-collect


📝 Commits (1)

  • 7267cd0 runtime: harden Depth24Converter bounds, rollback, and semaphore-cached collect

📊 Changes

3 files changed (+489 additions, -35 deletions)

View changed files

📝 runtime/CMakeLists.txt (+9 -0)
📝 runtime/src/vulkan_depth24.h (+86 -35)
➕ runtime/test/vulkan_depth24_unit_test.cpp (+394 -0)

📄 Description

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)

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1988 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-vk-depth24-converter-overflow-and-collect` --- ### 📝 Commits (1) - [`7267cd0`](https://github.com/ubiquitarianism/leonida/commit/7267cd00d9bb0971c78930ea54c0d3f2cf43329b) runtime: harden Depth24Converter bounds, rollback, and semaphore-cached collect ### 📊 Changes **3 files changed** (+489 additions, -35 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+9 -0) 📝 `runtime/src/vulkan_depth24.h` (+86 -35) ➕ `runtime/test/vulkan_depth24_unit_test.cpp` (+394 -0) </details> ### 📄 Description 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1995
No description provided.