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

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

@⁠IlyassRS3 on GitHub · 2026-11-25 14:44 UTC · original
IlyassRS3:rt-vk-replay-draw-zero-and-atomic-guards → main · +421 −6 · 3 files · 1 commits · open

Item: RT-VK-REPLAY-DRAW-ZERO-AND-ATOMIC-GUARDS
Claim: #1608

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)
**@⁠IlyassRS3** on GitHub · 2026-11-25 14:44 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1892) `IlyassRS3:rt-vk-replay-draw-zero-and-atomic-guards` → `main` · +421 −6 · 3 files · 1 commits · open Item: `RT-VK-REPLAY-DRAW-ZERO-AND-ATOMIC-GUARDS` Claim: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 ### 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)
Three correctness fixes in runtime/src/vulkan_replay.h:
1. zero_clear_unavailable(): check Draw and IndirectRasterDraw color and
   depth/stencil attachments (via draw_command(command).state and
   planes_for_layout) so unset vkCmdClearColorImage /
   vkCmdClearDepthStencilImage returns ReplayResult::unsupported instead
   of dereferencing a null pointer in detail::initialize().
2. AtomicCopyBuffer preparation: enforce non-zero size, UINT64_MAX
   offset/size overflow guards, and same-VkBuffer aliasing overlap
   refusal (VUID-vkCmdCopyBuffer-pRegions-00117), matching CopyBuffer.
3. zero_image_aspect(): request align - 1 extra bytes from copy_scratch
   (with fallback to fill_bytes) and guard null scratch handles so small
   8B/16B-block textures (BC1/BC7/RGBA32F) still zero-initialize when
   copy_scratch returns a 4-byte-aligned offset.

Item: RT-VK-REPLAY-DRAW-ZERO-AND-ATOMIC-GUARDS
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:10:20 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 14:44 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 14:44 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1892#pullrequestreview-6059780406) · **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.
Author
Owner

@⁠Timon360SB on GitHub · 2026-11-25 15:32 UTC · original

Independent verification — Turing and Blackwell

  • Base: 71a769d7d85208f86c6e2e50263aca7df1becce9
  • Head: 461ecf77ddc4da4f057f33d4b82de395adf5935b

Environment

GPU Windows 11 build
RTX 5070 (Blackwell) 26200
RTX 2060 SUPER (Turing) 28000

NVIDIA 621.14, Vulkan SDK 1.4.363.0,
MSVC 19.44.35230, Ninja RelWithDebInfo.

Three repetitions per GPU, revision and validation mode.
The new mock test was run once per system on HEAD.

Results

Test Base Head
replay_clear_gate 12/12 12/12
replay_copy_offset 12/12 12/12
replay_texture_barrier 12/12 12/12
replay_query_edges 12/12 12/12
d3d12_predication_atomic_copy 12/12 12/12
d3d12_draw 12/12 12/12
replay_draw_zero_and_atomic_guards N/A 2/2
Total 72/72 74/74

146 executions: 146 passed, 0 failed, 0 skipped.

Findings

  • The new mock test passes under Windows/MSVC on both
    systems, covering missing clear functions, atomic-copy
    aliasing and overflow, and scratch-buffer alignment.
  • Predication and atomic-copy tests reported 59 cases,
    zero failures and zero Vulkan validation errors in
    every execution.
  • Draw tests retained exact 4096/4096 pixel matches
    for MSAA, instance step rate and strip-cut checks.
  • No VUID diagnostics in the captured logs.
    Validation-layer feature-adjustment warnings were present.

No regressions observed on either GPU.
The new targeted fixture passes on HEAD; its negative
control against unpatched BASE was not independently run.

Synthetic tests only. No game files used.
AI-assisted: yes.

**@⁠Timon360SB** on GitHub · 2026-11-25 15:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1892#issuecomment-7167004269) ## Independent verification — Turing and Blackwell - **Base:** `71a769d7d85208f86c6e2e50263aca7df1becce9` - **Head:** `461ecf77ddc4da4f057f33d4b82de395adf5935b` ### Environment | GPU | Windows 11 build | |---|---| | RTX 5070 (Blackwell) | 26200 | | RTX 2060 SUPER (Turing) | 28000 | NVIDIA 621.14, Vulkan SDK 1.4.363.0, MSVC 19.44.35230, Ninja RelWithDebInfo. Three repetitions per GPU, revision and validation mode. The new mock test was run once per system on HEAD. ### Results | Test | Base | Head | |---|---:|---:| | `replay_clear_gate` | 12/12 | 12/12 | | `replay_copy_offset` | 12/12 | 12/12 | | `replay_texture_barrier` | 12/12 | 12/12 | | `replay_query_edges` | 12/12 | 12/12 | | `d3d12_predication_atomic_copy` | 12/12 | 12/12 | | `d3d12_draw` | 12/12 | 12/12 | | `replay_draw_zero_and_atomic_guards` | N/A | 2/2 | | **Total** | **72/72** | **74/74** | 146 executions: 146 passed, 0 failed, 0 skipped. ### Findings - The new mock test passes under Windows/MSVC on both systems, covering missing clear functions, atomic-copy aliasing and overflow, and scratch-buffer alignment. - Predication and atomic-copy tests reported 59 cases, zero failures and zero Vulkan validation errors in every execution. - Draw tests retained exact 4096/4096 pixel matches for MSAA, instance step rate and strip-cut checks. - No VUID diagnostics in the captured logs. Validation-layer feature-adjustment warnings were present. No regressions observed on either GPU. The new targeted fixture passes on HEAD; its negative control against unpatched BASE was not independently run. Synthetic tests only. No game files used. AI-assisted: yes.
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/1892/head:github-bot/gh-1892'
git switch 'github-bot/gh-1892'

Merge

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