a73x

fd817e8a

Add grep gate that forbids unbounded Vulkan waits outside vk_sync

a73x   2026-04-18 15:22

tests/check_unbounded_vk.sh scans src/ for direct calls to
vkd.waitForFences / acquireNextImageKHR / deviceWaitIdle /
queueWaitIdle and fails CI if any appear outside src/vk_sync.zig.
Wired into zig build test.

Part of issue ab6c92f0.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

diff --git a/build.zig b/build.zig
index e9d664e..09778c1 100644
--- a/build.zig
+++ b/build.zig
@@ -341,6 +341,9 @@ pub fn build(b: *std.Build) void {
    const vk_sync_tests = b.addTest(.{ .root_module = vk_sync_test_mod });
    test_step.dependOn(&b.addRunArtifact(vk_sync_tests).step);

    const check_unbounded_vk = b.addSystemCommand(&.{ "tests/check_unbounded_vk.sh" });
    test_step.dependOn(&check_unbounded_vk.step);

    // capture module — --capture mode (render a VT script to PNG)
    const capture_mod = b.createModule(.{
        .root_source_file = b.path("src/capture.zig"),
diff --git a/tests/check_unbounded_vk.sh b/tests/check_unbounded_vk.sh
new file mode 100755
index 0000000..1bc88c4
--- /dev/null
+++ b/tests/check_unbounded_vk.sh
@@ -0,0 +1,47 @@
#!/usr/bin/env bash
# Grep gate: fail if any source file outside src/vk_sync.zig calls
# Vulkan blocking primitives directly. All such calls must go through
# src/vk_sync.zig helpers (waitFenceBounded, acquireImageBounded,
# waitIdleForShutdown, etc.).

set -euo pipefail

REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$REPO_ROOT"

PATTERNS=(
    'vkd\.waitForFences'
    'vkd\.acquireNextImageKHR'
    'vkd\.deviceWaitIdle'
    'vkd\.queueWaitIdle'
)

# Find all .zig files in src/ except vk_sync.zig
mapfile -t files < <(find src -name '*.zig' ! -path 'src/vk_sync.zig' | sort)

violations=0
violation_lines=()

for file in "${files[@]}"; do
    for pat in "${PATTERNS[@]}"; do
        while IFS= read -r hit; do
            violation_lines+=("$file:$hit")
            violations=$((violations + 1))
        done < <(grep -nE "$pat" "$file" || true)
    done
done

if [[ $violations -gt 0 ]]; then
    echo "ERROR: $violations unbounded Vulkan wait call(s) found outside src/vk_sync.zig:" >&2
    for line in "${violation_lines[@]}"; do
        echo "  $line" >&2
    done
    echo "" >&2
    echo "Use src/vk_sync.zig helpers instead:" >&2
    echo "  waitFenceBounded       — replace vkd.waitForFences" >&2
    echo "  acquireImageBounded    — replace vkd.acquireNextImageKHR" >&2
    echo "  waitIdleForShutdown    — replace vkd.deviceWaitIdle / queueWaitIdle" >&2
    exit 1
fi

echo "vk grep gate: ok (${#files[@]} files, no violations)"