[PR #2222] [MERGED] xdxr: check the update source and bound the builder's reads (RT-XDXR-UPDATE-SOURCE-CHECK) #2226

Closed
opened 2026-11-26 21:20:29 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2222
Author: @DadaCap
Created: 11/26/2026
Status: ✅ Merged
Merged: 11/26/2026
Merged by: @aslan2142

Base: main ← Head: xdxr-update-source-check


📝 Commits (1)

  • 7727506 xdxr: check an update's source before refitting it; bound vertex reads by VertexCount

📊 Changes

7 files changed (+8391 additions, -7876 deletions)

View changed files

📝 docs/bvh.md (+4 -1)
📝 runtime/shims/d3d12_vulkan_device.cpp (+2 -2)
📝 runtime/src/vulkan_backend.h (+1 -0)
📝 runtime/src/xdxr_build.comp (+49 -2)
📝 runtime/src/xdxr_build.h (+45 -14)
📝 runtime/src/xdxr_build.inc (+8241 -7853)
📝 runtime/test/vk_xdxr_build_test.cpp (+49 -4)

📄 Description

Claimed in the coordination issue as: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7184751281

Depends on: none. It merges cleanly with current main, #2002, #1874 and #1418. It conflicts with #690 in runtime/src/xdxr_build.comp, runtime/src/xdxr_build.h and the generated runtime/src/xdxr_build.inc (both change the builder's modes and push constants); whichever lands second redoes those hunks and regenerates the .inc.

What and why

Two ways the XDXR builder (xdxr_build.comp) read memory it had no reason to touch:

  • An update source that is not a structure of this layout. D3D12 leaves an update from a structure the title never built undefined, and a title can pass one. The refit copied the source and then walked up its parent table and node pointers as they were, so a source of zeros, random bytes or another layout sent the walk (and its reads and writes) anywhere in the device's address space.
  • A vertex index past VertexCount. The builder read every vertex its index named. An index at or past the geometry's VertexCount (garbage in the index buffer, or VertexCount 0) read past the vertex buffer, possibly by gigabytes.

The fix

  • MODE_CHECK (new, one invocation): after the source is copied, it compares the header words build_header writes that the layout fixes (flags and version, element count, node, element and parent offsets, the root pointer) and the size against the layout, and writes the verdict to a scratch word.
  • An update now records both tails: the refit steps carry FLAG_IF_REFIT, a full build's steps (MODE_INIT and finish) carry FLAG_IF_BUILD, and each dispatch returns at once unless the verdict picks it (XdxrBuilder::refit_begin / update_finish). A source that fails the check is rebuilt from the update's geometry and nothing else of it is read.
  • The refit walk follows only pointers that name one of the layout's nodes (layout_node, parent_slot), so a source that passes the check but holds a garbage tree still ends and stays inside the structure.
  • vertex() returns NaN for an index at or past VertexCount, which makes its triangle inactive as a NaN vertex already did. XdxrTriangles::vertex_count carries the desc's VertexCount (vulkan_backend.h); the default bounds nothing, for callers with no desc. The push block grows by one word (static_assert 108 bytes).
  • docs/bvh.md states the bound; the comment in d3d12_vulkan_device.cpp that said such indices read past the buffer now says they are not read.
  • xdxr_build.inc regenerated with glslc --target-env=vulkan1.2 -O -mfmt=c (Vulkan SDK 1.4.363.1); main's .comp reproduces main's .inc byte for byte with the same command.

Tested

  • vk_xdxr_build_test (hand-made geometry, no game data):
    • blas_vertex_count: an unindexed geometry whose last triangle lies past VertexCount over finite vertices, an index far past the buffer, and VertexCount 0; the reference treats those triangles as inactive and xdxr_bvh.py check-build validates the result.
    • eight update-source cases, each on the large tree and the small-sort path: a source of zeros, of random bytes, the built structure with another element count in its header (these become a full build of the moved geometry and are validated like any build), and a valid header over a random tree (the refit must end; its result is not checked).
  • Built gta6rt and the XDXR/BVH test targets (Windows 11, MSVC, Ninja, RelWithDebInfo) and ran ctest -R "xdxr|bvh": 11 passed (xdxr_build_output, xdxr_descs, xdxr_builder_validation, xdxr_build, xdxr_build_check, d3d12_xdxr_rt, d3d12_xdxr_rt_check, xbox_bvh_builder, xbox_bvh_builder_console, xbox_bvh_builder_check, vk_xdxr_postbuild_readback), 1 skipped (bvh_builder, which skips in this configuration). Not run: the full ctest suite and the translator suite (no translator change).
  • python -I scripts/check_no_game_data.py on the changed files: clean. python -I tests/test_text_write_line_endings.py: OK.
  • Retail game runs on an RTX 5080 (driver 621.24, Windows 11): before, the device was lost in most runs that stayed in the world, at 2-8 minutes, with the BLAS update in flight; with the change, two 15-minute runs stayed in the world without a loss, and the kernel took the full-build path for invalid sources in both. Those runs used a local build with this change and other local work, including a diagnostic that is not part of this pull request. No log lines, addresses or captures are quoted here.

GPUs tested: NVIDIA GeForce RTX 5080, driver 621.24, Windows 11 (the ctest entries and the game runs).

AI-assisted: yes (Claude Code, Opus 5.5)

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)

🤖 Generated with Claude Code

https://claude.ai/code/session_01Gt9hwRYcpySx4P4ncQ3wD5


🔄 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/2222 **Author:** [@DadaCap](https://github.com/DadaCap) **Created:** 11/26/2026 **Status:** ✅ Merged **Merged:** 11/26/2026 **Merged by:** [@aslan2142](https://github.com/aslan2142) **Base:** `main` ← **Head:** `xdxr-update-source-check` --- ### 📝 Commits (1) - [`7727506`](https://github.com/ubiquitarianism/leonida/commit/77275065bc0554a80eb523c24457dc8597c8f362) xdxr: check an update's source before refitting it; bound vertex reads by VertexCount ### 📊 Changes **7 files changed** (+8391 additions, -7876 deletions) <details> <summary>View changed files</summary> 📝 `docs/bvh.md` (+4 -1) 📝 `runtime/shims/d3d12_vulkan_device.cpp` (+2 -2) 📝 `runtime/src/vulkan_backend.h` (+1 -0) 📝 `runtime/src/xdxr_build.comp` (+49 -2) 📝 `runtime/src/xdxr_build.h` (+45 -14) 📝 `runtime/src/xdxr_build.inc` (+8241 -7853) 📝 `runtime/test/vk_xdxr_build_test.cpp` (+49 -4) </details> ### 📄 Description Claimed in the coordination issue as: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7184751281 Depends on: none. It merges cleanly with current `main`, #2002, #1874 and #1418. It conflicts with #690 in `runtime/src/xdxr_build.comp`, `runtime/src/xdxr_build.h` and the generated `runtime/src/xdxr_build.inc` (both change the builder's modes and push constants); whichever lands second redoes those hunks and regenerates the `.inc`. ### What and why Two ways the XDXR builder (`xdxr_build.comp`) read memory it had no reason to touch: - **An update source that is not a structure of this layout.** D3D12 leaves an update from a structure the title never built undefined, and a title can pass one. The refit copied the source and then walked up its parent table and node pointers as they were, so a source of zeros, random bytes or another layout sent the walk (and its reads and writes) anywhere in the device's address space. - **A vertex index past `VertexCount`.** The builder read every vertex its index named. An index at or past the geometry's `VertexCount` (garbage in the index buffer, or `VertexCount` 0) read past the vertex buffer, possibly by gigabytes. ### The fix - `MODE_CHECK` (new, one invocation): after the source is copied, it compares the header words `build_header` writes that the layout fixes (flags and version, element count, node, element and parent offsets, the root pointer) and the size against the layout, and writes the verdict to a scratch word. - An update now records both tails: the refit steps carry `FLAG_IF_REFIT`, a full build's steps (`MODE_INIT` and `finish`) carry `FLAG_IF_BUILD`, and each dispatch returns at once unless the verdict picks it (`XdxrBuilder::refit_begin` / `update_finish`). A source that fails the check is rebuilt from the update's geometry and nothing else of it is read. - The refit walk follows only pointers that name one of the layout's nodes (`layout_node`, `parent_slot`), so a source that passes the check but holds a garbage tree still ends and stays inside the structure. - `vertex()` returns NaN for an index at or past `VertexCount`, which makes its triangle inactive as a NaN vertex already did. `XdxrTriangles::vertex_count` carries the desc's `VertexCount` (`vulkan_backend.h`); the default bounds nothing, for callers with no desc. The push block grows by one word (`static_assert` 108 bytes). - `docs/bvh.md` states the bound; the comment in `d3d12_vulkan_device.cpp` that said such indices read past the buffer now says they are not read. - `xdxr_build.inc` regenerated with `glslc --target-env=vulkan1.2 -O -mfmt=c` (Vulkan SDK 1.4.363.1); `main`'s `.comp` reproduces `main`'s `.inc` byte for byte with the same command. ### Tested - `vk_xdxr_build_test` (hand-made geometry, no game data): - `blas_vertex_count`: an unindexed geometry whose last triangle lies past `VertexCount` over finite vertices, an index far past the buffer, and `VertexCount` 0; the reference treats those triangles as inactive and `xdxr_bvh.py check-build` validates the result. - eight update-source cases, each on the large tree and the small-sort path: a source of zeros, of random bytes, the built structure with another element count in its header (these become a full build of the moved geometry and are validated like any build), and a valid header over a random tree (the refit must end; its result is not checked). - Built `gta6rt` and the XDXR/BVH test targets (Windows 11, MSVC, Ninja, RelWithDebInfo) and ran `ctest -R "xdxr|bvh"`: 11 passed (`xdxr_build_output`, `xdxr_descs`, `xdxr_builder_validation`, `xdxr_build`, `xdxr_build_check`, `d3d12_xdxr_rt`, `d3d12_xdxr_rt_check`, `xbox_bvh_builder`, `xbox_bvh_builder_console`, `xbox_bvh_builder_check`, `vk_xdxr_postbuild_readback`), 1 skipped (`bvh_builder`, which skips in this configuration). Not run: the full ctest suite and the translator suite (no translator change). - `python -I scripts/check_no_game_data.py` on the changed files: clean. `python -I tests/test_text_write_line_endings.py`: OK. - Retail game runs on an RTX 5080 (driver 621.24, Windows 11): before, the device was lost in most runs that stayed in the world, at 2-8 minutes, with the BLAS update in flight; with the change, two 15-minute runs stayed in the world without a loss, and the kernel took the full-build path for invalid sources in both. Those runs used a local build with this change and other local work, including a diagnostic that is not part of this pull request. No log lines, addresses or captures are quoted here. GPUs tested: NVIDIA GeForce RTX 5080, driver 621.24, Windows 11 (the ctest entries and the game runs). AI-assisted: yes (Claude Code, Opus 5.5) ### 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) 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01Gt9hwRYcpySx4P4ncQ3wD5 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 21:20:29 +01:00
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#2226
No description provided.