[PR #1892] runtime: guard draw zero-clears, AtomicCopyBuffer aliasing/overflow, and zero_image_aspect scratch (RT-VK-REPLAY-DRAW-ZERO-AND-ATOMIC-GUARDS) #1903

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

📋 Pull Request Information

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

Base: main ← Head: rt-vk-replay-draw-zero-and-atomic-guards


📝 Commits (1)

  • 461ecf7 runtime: guard draw zero-clears, AtomicCopyBuffer aliasing/overflow, and zero_image_aspect scratch

📊 Changes

3 files changed (+421 additions, -6 deletions)

View changed files

📝 runtime/CMakeLists.txt (+11 -0)
📝 runtime/src/vulkan_replay.h (+35 -6)
➕ runtime/test/replay_draw_zero_and_atomic_guards_test.cpp (+375 -0)

📄 Description

Item: RT-VK-REPLAY-DRAW-ZERO-AND-ATOMIC-GUARDS
Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7165529828

Summary

Fixes three replay correctness bugs in runtime/src/vulkan_replay.h:

  1. zero_clear_unavailable() skips Draw / IndirectRasterDraw attachments (null-pointer crash):
    • emit() calls detail::to_attachment() -> detail::initialize() on every bound color and depth/stencil attachment of a non-skipped draw, invoking vk.clear_color_image / vk.clear_depth_stencil_image for any UNDEFINED piece.
    • zero_clear_unavailable() previously inspected copies, clears, and barriers, omitting draws (draw_command(command).state) and omitting planes_for_layout() for coupled depth/stencil ranges — allowing emit() to dereference a null vk.clear_color_image or vk.clear_depth_stencil_image (SIGSEGV on main).
    • Now checks d.colors and d.depth for every non-skipped draw and uses detail::planes_for_layout(resource, range, vk.separate_depth_stencil_layouts) inside missing().
  2. AtomicCopyBuffer preparation misses overflow & same-VkBuffer aliasing self-overlap (VUID-vkCmdCopyBuffer-pRegions-00117):
    • CommandList::atomic_copy() only compares ResourceIds (copy.dst == copy.src), so two distinct placed/arena ResourceIds backed by overlapping byte ranges of the same VkBuffer bypassed overlap rejection; prepare() also lacked UINT64_MAX checks on buffer_offset + offset and offset + bytes, plus a zero-size guard.
    • Now enforces the exact same overflow and same-VkBuffer overlap checks as CopyBuffer.
  3. zero_image_aspect() drops zero-initialization on unaligned scratch offsets:
    • When vk.copy_scratch(fill_bytes, ...) returns a 4-byte-aligned offset for an 8-byte or 16-byte block/texel format (e.g. 1x1 BC1/BC7/RGBA32F where needed == align), pad leaves ((fill_bytes - pad) / align) * align == 0 < fmt.bytes, causing zero_image_aspect() to return false and leave the image uninitialized.
    • Now requests fill_bytes + (align - 1) (overflow-guarded, with fallback to fill_bytes for exact-sized aligned pools), checks scratch != VK_NULL_HANDLE, and caps usable to fill_bytes.

No overlap with open PRs #787, #1123, #1179, or #1877.

Verification

  • AI-assisted: yes
  • GPUs tested: none (CPU-only macOS arm64 host with Khronos Vulkan-Headers)
  • New unit test runtime/test/replay_draw_zero_and_atomic_guards_test.cpp (CTest replay_draw_zero_and_atomic_guards):
    • clang++ -std=c++20 -Wall -Wextra -Wno-missing-field-initializers -Wno-switch -Wno-unused-private-field -Werror -O1 -g -fsanitize=address,undefined: passed
    • clang++ -std=c++20 -O1 -g -fsanitize=thread: passed
    • clang++ -std=c++20 -O2 (3 runs): passed
  • Existing replay unit tests compiled and passed: replay_clear_gate_test, replay_copy_offset_test, replay_texture_barrier_test, replay_query_edges_test
  • Per-bug mutation tests against main:
    • test_draw_zero_clear_unavailable_color_and_depth: SIGSEGV (returncode=-11) on main -> passes on branch
    • test_atomic_copy_overflow_and_aliasing_overlap: fails (returncode=1) on main -> passes on branch
    • test_zero_image_aspect_unaligned_scratch_offset: fails (returncode=1) on main -> passes on branch
  • python3 -I scripts/check_no_game_data.py on all changed files: passed
  • python3 -I tests/test_text_write_line_endings.py: passed (3 tests)

🔄 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/1892 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `rt-vk-replay-draw-zero-and-atomic-guards` --- ### 📝 Commits (1) - [`461ecf7`](https://github.com/ubiquitarianism/leonida/commit/461ecf77ddc4da4f057f33d4b82de395adf5935b) runtime: guard draw zero-clears, AtomicCopyBuffer aliasing/overflow, and zero_image_aspect scratch ### 📊 Changes **3 files changed** (+421 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+11 -0) 📝 `runtime/src/vulkan_replay.h` (+35 -6) ➕ `runtime/test/replay_draw_zero_and_atomic_guards_test.cpp` (+375 -0) </details> ### 📄 Description Item: `RT-VK-REPLAY-DRAW-ZERO-AND-ATOMIC-GUARDS` Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7165529828 ### Summary Fixes three replay correctness bugs in `runtime/src/vulkan_replay.h`: 1. **`zero_clear_unavailable()` skips `Draw` / `IndirectRasterDraw` attachments (null-pointer crash)**: - `emit()` calls `detail::to_attachment()` -> `detail::initialize()` on every bound color and depth/stencil attachment of a non-skipped draw, invoking `vk.clear_color_image` / `vk.clear_depth_stencil_image` for any `UNDEFINED` piece. - `zero_clear_unavailable()` previously inspected copies, clears, and barriers, omitting draws (`draw_command(command).state`) and omitting `planes_for_layout()` for coupled depth/stencil ranges — allowing `emit()` to dereference a null `vk.clear_color_image` or `vk.clear_depth_stencil_image` (SIGSEGV on `main`). - Now checks `d.colors` and `d.depth` for every non-skipped draw and uses `detail::planes_for_layout(resource, range, vk.separate_depth_stencil_layouts)` inside `missing()`. 2. **`AtomicCopyBuffer` preparation misses overflow & same-`VkBuffer` aliasing self-overlap (`VUID-vkCmdCopyBuffer-pRegions-00117`)**: - `CommandList::atomic_copy()` only compares `ResourceId`s (`copy.dst == copy.src`), so two distinct placed/arena `ResourceId`s backed by overlapping byte ranges of the same `VkBuffer` bypassed overlap rejection; `prepare()` also lacked `UINT64_MAX` checks on `buffer_offset + offset` and `offset + bytes`, plus a zero-size guard. - Now enforces the exact same overflow and same-`VkBuffer` overlap checks as `CopyBuffer`. 3. **`zero_image_aspect()` drops zero-initialization on unaligned scratch offsets**: - When `vk.copy_scratch(fill_bytes, ...)` returns a 4-byte-aligned offset for an 8-byte or 16-byte block/texel format (e.g. 1x1 BC1/BC7/RGBA32F where `needed == align`), `pad` leaves `((fill_bytes - pad) / align) * align == 0 < fmt.bytes`, causing `zero_image_aspect()` to return `false` and leave the image uninitialized. - Now requests `fill_bytes + (align - 1)` (overflow-guarded, with fallback to `fill_bytes` for exact-sized aligned pools), checks `scratch != VK_NULL_HANDLE`, and caps `usable` to `fill_bytes`. No overlap with open PRs `#787`, `#1123`, `#1179`, or `#1877`. ### Verification - **AI-assisted**: yes - **GPUs tested**: none (CPU-only macOS arm64 host with Khronos Vulkan-Headers) - New unit test `runtime/test/replay_draw_zero_and_atomic_guards_test.cpp` (CTest `replay_draw_zero_and_atomic_guards`): - `clang++ -std=c++20 -Wall -Wextra -Wno-missing-field-initializers -Wno-switch -Wno-unused-private-field -Werror -O1 -g -fsanitize=address,undefined`: passed - `clang++ -std=c++20 -O1 -g -fsanitize=thread`: passed - `clang++ -std=c++20 -O2` (3 runs): passed - Existing replay unit tests compiled and passed: `replay_clear_gate_test`, `replay_copy_offset_test`, `replay_texture_barrier_test`, `replay_query_edges_test` - **Per-bug mutation tests against `main`**: - `test_draw_zero_clear_unavailable_color_and_depth`: **SIGSEGV (`returncode=-11`) on `main`** -> passes on branch - `test_atomic_copy_overflow_and_aliasing_overlap`: **fails (`returncode=1`) on `main`** -> passes on branch - `test_zero_image_aspect_unaligned_scratch_offset`: **fails (`returncode=1`) on `main`** -> passes on branch - `python3 -I scripts/check_no_game_data.py` on all changed files: passed - `python3 -I tests/test_text_write_line_endings.py`: passed (3 tests) --- <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#1903
No description provided.