Skip to content

vulkan: harden IndirectDispatchCounter validation, cleanup, and emit guards - #6191

Open
emirhan-karaca wants to merge 1 commit into
ItsNotRexed:mainfrom
emirhan-karaca:fix/vk-indirect-dispatch-count-hardening
Open

emirhan-karaca wants to merge 1 commit into
ItsNotRexed:mainfrom
emirhan-karaca:fix/vk-indirect-dispatch-count-hardening

Conversation

@emirhan-karaca

Copy link
Copy Markdown
Contributor

Summary

Hardens IndirectDispatchCounter (runtime/src/vulkan_indirect_dispatch_count.h) with the same defensive validation, partial-initialization cleanup, and dispatch guards already applied to other GPU helpers (Depth24Converter, PresentComposer, PredicatedBufferCopier, PredicateReducer), and adds a standalone MockVk + real-GPU BDA count-gating regression test (runtime/test/vk_indirect_dispatch_count_test.cpp).

Claim: #6089 (comment)

Changes

  • runtime/src/vulkan_indirect_dispatch_count.h:
    • Guard null get_ / device_ and null destroy function pointers in ~IndirectDispatchCounter(), ~Batch(), make_pipeline(), and emit().
    • Batch::prepare():
      • Reject stride < 12 (a D3D12 dispatch argument record is 3 dwords = 12 bytes) and unaligned argument_offset % 4 / count_offset % 4 (the BDA compute shader loads 32-bit words).
      • Check address-range overflow across args_span, args_va + argument_offset, count_va + count_offset + sizeof(uint32_t), and output_va + bytes.
      • Clamp memory_.memoryTypeCount to VK_MAX_MEMORY_TYPES before indexing memoryTypes[] / shifting 1u << i.
      • Keep buffer and memory in local handles until all creation, allocation, bind, and BDA checks succeed, and clear memory on failed vkAllocateMemory so neither partial entries remain in allocations nor garbage output handles are passed to vkDestroyBuffer / vkFreeMemory.
    • make_pipeline(): verify VK_SUCCESS && handle != VK_NULL_HANDLE at every creation step and destroy only handles created by successful calls (avoiding destroying garbage handles if a failed Vulkan call writes to the output pointer).
    • emit(): guard null cb / get_ / device_ / pipeline_ / layout_ / zero BDA / invalid stride or max_count / zero maxComputeWorkGroupCount[0], and compute workgroup count via (count - 1u) / 64u + 1u so the ceil-division cannot overflow uint32_t regardless of chunk size.
  • runtime/test/vk_indirect_dispatch_count_test.cpp & runtime/CMakeLists.txt:
    • Adds CTest target vk_indirect_dispatch_count (vk_indirect_dispatch_count_test, SKIP_RETURN_CODE 77, MSVC /W4 /permissive- /utf-8).
    • Covers MockVk hardening cases (1a–1e: stride/offset rejection, null handles, missing entry points, garbage-on-failure cleanup, memoryTypeCount > VK_MAX_MEMORY_TYPES clamping, address-range overflows, emit() guards & chunked dispatch math) plus real-GPU Vulkan 1.3 BDA count-gating (count = 3, 0, 99, stride = 16, argument_offset = 16, count_offset = 4), reusing IndirectDispatchCounter for readback so the private indirect buffer never requires VK_BUFFER_USAGE_TRANSFER_SRC_BIT.

Verification

  • Negative control (RED on unmodified origin/main 561b65a83):
    • vk_indirect_dispatch_count_test fails deterministically (exit 1) on both Intel UHD and NVIDIA RTX 5050 at line 254: !b->prepare(args_buf, 0, count_buf, 0, 4, 2, out) (stride = 4 < 12 was wrongly accepted).
  • Patched (GREEN, 0 warnings in vk_indirect_dispatch_count_test.cpp under g++ 16.2.0 -std=c++20 -O2 -Wall -Wextra -Wconversion -Wsign-conversion, Vulkan-Headers 369):
    • Intel(R) UHD Graphics (driver 101.7092): vk_indirect_dispatch_count_test 3/3 consecutive PASS, vk_queue_test PASS.
    • NVIDIA GeForce RTX 5050 Laptop GPU (driver 621.24): vk_indirect_dispatch_count_test 3/3 consecutive PASS, vk_queue_test PASS.
  • Scope / disclosures: AI-assisted; synthetic tests only, zero game files/keys/shaders. Local environment uses standalone MinGW g++ 16.2.0 without MSVC or VK_LAYER_KHRONOS_validation installed, so CI validation-layer and MSVC runs are appreciated.

@SirKingg SirKingg left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent Windows/MSVC/Vulkan review, AI-assisted. I found the test skip issue inline, so I am not approving yet.

Windows, MSVC 19.44.35230, Vulkan SDK/validation 1.4.363, RTX 4070 Ti driver 610.88; synthetic inputs only, no game files.

  • cmake -S runtime -B runtime\build-review-6191 -G Ninja -DCMAKE_BUILD_TYPE=RelWithDebInfo -DBUILD_TESTING=ON and cmake --build runtime\build-review-6191 --target vk_indirect_dispatch_count_test vk_sync_validation_selftest: passed under vcvars64.
  • With LEONIDA_VK_VALIDATION=1, ctest --test-dir runtime\build-review-6191 -C RelWithDebInfo -R '^vk_(indirect_dispatch_count|sync_validation)$' --repeat until-fail:3 --output-on-failure: both passed 3/3. The synchronization self-test independently detected one expected hazard without a barrier and zero with one; the PR test reported zero validation errors.
  • MSVC-compiled PR test against base header 561b65a: failed at the expected stride-4 rejection (line 254), exit 1. An isolated modified synthetic fixture exercised 300 records with a two-workgroup chunk limit and GPU counts 129, 0, and 999: passed with zero validation errors. It reuses the compute helper for readback, not actual vkCmdDispatchIndirect execution.
  • With LEONIDA_VK_VALIDATION=1 and VK_LOADER_LAYERS_DISABLE=VK_LAYER_KHRONOS_validation, ctest --test-dir runtime\build-review-6191 -C RelWithDebInfo -R '^vk_indirect_dispatch_count$' -V reports SKIP and exits 0 despite the requested layer being unavailable (see inline).

These focused tests do not establish behavior on other drivers, out-of-memory conditions on a real driver, or game workloads.


vktest::Device d;
d.request_buffer_device_address = true;
if (!d.create(false) || !d.buffer_device_address) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create() collapses DeviceStatus::failed and unavailable, so this returns 77 even when validation was explicitly requested but its layer or initialization fails. Reproduced with LEONIDA_VK_VALIDATION=1 and VK_LOADER_LAYERS_DISABLE=VK_LAYER_KHRONOS_validation: it prints that the layer is missing, then CTest marks the test skipped and exits 0, despite a working GPU. Could this use initialize(false), fail on DeviceStatus::failed, and reserve 77 for unavailable or missing BDA? A negative case would protect the validated run from silently skipping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants