XDXR-BUILDER-PREVALIDATE: pre-validate XdxrBuilder inputs and avoid 32-bit workgroup overflow #485

Manually merged
github merged 1 commit from gh-pr/485/head into gh-pr/485/base 2026-11-26 09:03:32 +00:00
Owner

@⁠xroneyaa on GitHub · 2026-11-24 06:10 UTC · original
xroneyaa:fix/xdxr-builder-validation → main · +198 −4 · 3 files · 1 commits · merged 2026-11-24 16:25 UTC

Claimed in the coordination issue as: XDXR-BUILDER-PREVALIDATE

What and why

XdxrBuilder::record_blas() previously recorded begin()/refit_begin() and all mode_triangles dispatches into VkCommandBuffer cb before checking first != layout.elements, leaving partial dispatches in cb on mismatch, and accumulated first in std::uint32_t (which could overflow back to layout.elements). Both record_blas() and record_tlas() also did not check for uncomputed layouts (layout.size == 0) or null dst/scratch addresses before recording, and XdxrBuilder::dispatch() computed (threads + 63) / 64 in 32 bits.

This pre-validates !layout.size || !dst || !scratch, sums geometry triangle counts in std::uint64_t up front in record_blas(), checks (layout.elements && !instances) up front in record_tlas(), and widens (std::uint64_t(threads) + 63u) / 64u in dispatch(). Adds CPU-only unit test runtime/test/xdxr_builder_validation_test.cpp registered as xdxr_builder_validation in runtime/CMakeLists.txt.

Tested

  • Rebuilt xdxr_builder_validation_test, vk_xdxr_build_test, and d3d12_xdxr_rt_test with MSVC 19.50 / Ninja (0 warnings in touched files)
  • ctest --test-dir runtime/build -R "^(xdxr_builder_validation|xdxr_build|xdxr_build_check|d3d12_xdxr_rt)$" --output-on-failure (with VK_LOADER_LAYERS_DISABLE=~implicit~) -> 4/4 Passed (xdxr_builder_validation 0.01 s, xdxr_build 0.30 s, xdxr_build_check 72.71 s, d3d12_xdxr_rt 0.71 s)
  • python -I scripts/check_no_game_data.py runtime/src/xdxr_build.h runtime/test/xdxr_builder_validation_test.cpp runtime/CMakeLists.txt -> exit 0

GPUs tested: NVIDIA GeForce RTX 3060 Ti, driver 610.88, Windows 11 (xdxr_build, xdxr_build_check, d3d12_xdxr_rt); xdxr_builder_validation runs CPU-only with a mock Vulkan dispatch table

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed (n/a — no translator rule added).
**@⁠xroneyaa** on GitHub · 2026-11-24 06:10 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/485) `xroneyaa:fix/xdxr-builder-validation` → `main` · +198 −4 · 3 files · 1 commits · merged 2026-11-24 16:25 UTC Claimed in the coordination issue as: XDXR-BUILDER-PREVALIDATE ### What and why `XdxrBuilder::record_blas()` previously recorded `begin()`/`refit_begin()` and all `mode_triangles` dispatches into `VkCommandBuffer cb` before checking `first != layout.elements`, leaving partial dispatches in `cb` on mismatch, and accumulated `first` in `std::uint32_t` (which could overflow back to `layout.elements`). Both `record_blas()` and `record_tlas()` also did not check for uncomputed layouts (`layout.size == 0`) or null `dst`/`scratch` addresses before recording, and `XdxrBuilder::dispatch()` computed `(threads + 63) / 64` in 32 bits. This pre-validates `!layout.size || !dst || !scratch`, sums geometry triangle counts in `std::uint64_t` up front in `record_blas()`, checks `(layout.elements && !instances)` up front in `record_tlas()`, and widens `(std::uint64_t(threads) + 63u) / 64u` in `dispatch()`. Adds CPU-only unit test `runtime/test/xdxr_builder_validation_test.cpp` registered as `xdxr_builder_validation` in `runtime/CMakeLists.txt`. ### Tested - Rebuilt `xdxr_builder_validation_test`, `vk_xdxr_build_test`, and `d3d12_xdxr_rt_test` with MSVC 19.50 / Ninja (0 warnings in touched files) - `ctest --test-dir runtime/build -R "^(xdxr_builder_validation|xdxr_build|xdxr_build_check|d3d12_xdxr_rt)$" --output-on-failure` (with `VK_LOADER_LAYERS_DISABLE=~implicit~`) -> 4/4 Passed (`xdxr_builder_validation` 0.01 s, `xdxr_build` 0.30 s, `xdxr_build_check` 72.71 s, `d3d12_xdxr_rt` 0.71 s) - `python -I scripts/check_no_game_data.py runtime/src/xdxr_build.h runtime/test/xdxr_builder_validation_test.cpp runtime/CMakeLists.txt` -> exit 0 GPUs tested: NVIDIA GeForce RTX 3060 Ti, driver 610.88, Windows 11 (`xdxr_build`, `xdxr_build_check`, `d3d12_xdxr_rt`); `xdxr_builder_validation` runs CPU-only with a mock Vulkan dispatch table AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed (n/a — no translator rule added).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:03:30 +00:00
github manually merged commit d2f6033ee7 into gh-pr/485/base 2026-11-26 09:03:32 +00:00
Author
Owner

@⁠Oliwettoo on GitHub · 2026-11-24 10:39 UTC · original

Looks good. The important part is that record_blas now checks the triangle total before anything is recorded into the command buffer: on main a mismatch returned false only after the build dispatches were already in it. Built xdxr_builder_validation_test with g++ and Vulkan-Headers on Linux: it passes with the PR and fails against main's xdxr_build.h, so it guards the change. Nit: the new add_test sits in its own if(BUILD_TESTING), unlike the other tests in that Vulkan::Headers block; either is fine, but matching the neighbours keeps it uniform. Ship. AI-assisted review.

**@⁠Oliwettoo** on GitHub · 2026-11-24 10:39 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/485#issuecomment-7129077300) Looks good. The important part is that `record_blas` now checks the triangle total before anything is recorded into the command buffer: on `main` a mismatch returned `false` only after the build dispatches were already in it. Built `xdxr_builder_validation_test` with g++ and Vulkan-Headers on Linux: it passes with the PR and fails against `main`'s `xdxr_build.h`, so it guards the change. Nit: the new `add_test` sits in its own `if(BUILD_TESTING)`, unlike the other tests in that `Vulkan::Headers` block; either is fine, but matching the neighbours keeps it uniform. Ship. AI-assisted review.
Author
Owner

@⁠kawaii-shakal on GitHub · 2026-11-24 11:39 UTC · original

Independent run on Windows: passes, with and without validation.

Head d2f6033ee built as it is (RelWithDebInfo, MSVC 19.50.35721.0), ctest -R xdxr:

LEONIDA_VK_VALIDATION Result
unset 7/7 passed
1 (Khronos validation with synchronization validation, Vulkan SDK 1.4.363.0) 7/7 passed, including xdxr_builder_validation; all 30 xdxr_build_check scenes (BOX16, paired TRI1, refit, clone, compacted, TLAS) report 0 validation errors and 0 rays differing

Read the diff as well: checking the triangle inputs before anything is dispatched, rather than inside the recorded work, and computing the workgroup count in 64 bits so (threads + 63) / 64 cannot wrap, both look right to me.

GPU: NVIDIA GeForce RTX 5070 Ti, driver 617.42, Vulkan 1.4.351, Windows 11 Pro 26300.

AI-assisted.

**@⁠kawaii-shakal** on GitHub · 2026-11-24 11:39 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/485#issuecomment-7130373168) Independent run on Windows: passes, with and without validation. Head d2f6033ee built as it is (RelWithDebInfo, MSVC 19.50.35721.0), `ctest -R xdxr`: | `LEONIDA_VK_VALIDATION` | Result | |---|---| | unset | 7/7 passed | | `1` (Khronos validation with synchronization validation, Vulkan SDK 1.4.363.0) | 7/7 passed, including `xdxr_builder_validation`; all 30 `xdxr_build_check` scenes (BOX16, paired TRI1, refit, clone, compacted, TLAS) report 0 validation errors and 0 rays differing | Read the diff as well: checking the triangle inputs before anything is dispatched, rather than inside the recorded work, and computing the workgroup count in 64 bits so `(threads + 63) / 64` cannot wrap, both look right to me. GPU: NVIDIA GeForce RTX 5070 Ti, driver 617.42, Vulkan 1.4.351, Windows 11 Pro 26300. AI-assisted.
Author
Owner

@⁠QemmHD on GitHub · 2026-11-24 16:25 UTC · original

Merging.

Why merged: The xdxr builder checks the BLAS triangle total and null addresses before anything is recorded, and the dispatch-count overflow is fixed. The CMake change only adds the new test target.

Why it's good: xdxr_builder_validation, xdxr_build, xdxr_build_output and xdxr_descs all pass both plain and under validation.

Tested on Windows 11 / RTX 4070 SUPER (NVIDIA 621.24), VS 2022 + Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0. Alone on main, its tests passed both plain and with LEONIDA_VK_VALIDATION=1. Cumulative: #388, #418, #421, #434, #470, #484, #485, #486 and #488 merged onto main 1504c1af6 (a225e593e). There the repo-consistency set gives 566 passed / 5 skipped / 0 failed, compileall is clean, and full ctest is 306/308. The 2 failures are the same pre-existing ones as main (d3d12_execute_indirect_validation, d3d12_depth_resolve), with the same skip set. git merge-tree against current main 98a977b4b: no conflicts. Security read of the three-dot diff: no binaries, game data, downloads, network calls or CI changes.

AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠QemmHD** on GitHub · 2026-11-24 16:25 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/485#issuecomment-7137641233) Merging. **Why merged:** The xdxr builder checks the BLAS triangle total and null addresses before anything is recorded, and the dispatch-count overflow is fixed. The CMake change only adds the new test target. **Why it's good:** xdxr_builder_validation, xdxr_build, xdxr_build_output and xdxr_descs all pass both plain and under validation. Tested on Windows 11 / RTX 4070 SUPER (NVIDIA 621.24), VS 2022 + Ninja RelWithDebInfo, Vulkan SDK 1.4.363.0. Alone on main, its tests passed both plain and with LEONIDA_VK_VALIDATION=1. Cumulative: #388, #418, #421, #434, #470, #484, #485, #486 and #488 merged onto main 1504c1af6 (a225e593e). There the repo-consistency set gives 566 passed / 5 skipped / 0 failed, compileall is clean, and full ctest is 306/308. The 2 failures are the same pre-existing ones as main (d3d12_execute_indirect_validation, d3d12_depth_resolve), with the same skip set. git merge-tree against current main 98a977b4b: no conflicts. Security read of the three-dot diff: no binaries, game data, downloads, network calls or CI changes. AI-assisted (Claude Code, Claude Opus 5.5).
This discussion has been locked. Commenting is limited to contributors.
No description provided.