runtime: guard overflow/div-by-zero and remove duplicate passes in d3d12_xs_alloc_size #2029

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

@⁠IlyassRS3 on GitHub · 2026-11-25 18:32 UTC · original
IlyassRS3:fix/rt-xs-alloc-size-overflow-and-single-pass → main · +274 −45 · 3 files · 1 commits · open

Coordination item: RT-XS-ALLOC-SIZE-OVERFLOW-AND-SINGLE-PASS (claimed on #1608).

Why

  1. align_up_saturating(value, to) in runtime/shims/d3d12_xs_alloc_size.h divided by to unconditionally, causing integer divide-by-zero if to == 0.
  2. console_render_target_size() and console_depth_stencil_size() documented returning 0 on overflow, yet accumulated slice += surface.tiled_size() and slice += kConsoleTileBytes without checking UINT64_MAX - slice. Multi-mip render targets (e.g. 0x3FFFFFBF x 0x3FFFFFBF 16-byte format, 2 mips) and multi-plane depth-stencil targets (e.g. 0x7FFFFF7F x 0x7FFFFF7F D32_FLOAT_S8X24_UINT) silently wrapped slice to 4,611,683,819,404,197,888 instead of returning 0. Checking desc.Width > UINT32_MAX before std::bit_width() also avoids entering the mip shift loop on >32-bit widths.
  3. console_placement() executed up to 3 passes when each != nullptr (an any_target pre-scan plus two full passes that each called pc_one(i) and re-evaluated console_texture_layout / console_render_target_size / console_depth_stencil_size), doubling ID3D12Device::GetResourceAllocationInfo calls and tripling XG layout work.

Changes

  • runtime/shims/d3d12_xs_alloc_size.h:
    • Guard if (to <= 1) return value; in align_up_saturating().
    • Reject desc.Width > UINT32_MAX upfront and check UINT64_MAX - slice before every mip/tail addition in console_render_target_size() and console_depth_stencil_size().
    • Precompute each descriptor's applicable console rule once (using an inline 16-entry buffer with vector spill for larger lists), exit with 0 pc_one calls when none apply, run a single pc_one layout pass staging each entries locally, and publish total/each only after succeeding with grows == true.
  • runtime/test/xs_alloc_size_test.cpp + runtime/CMakeLists.txt: add CPU-only xs_alloc_size unit test covering align_up_saturating(to=0), width/mip/plane overflow guards, fail-closed each/total preservation, single-pass pc_one call count, and the count > 16 spill path.

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind).
  • GPUs tested: none (CPU-only unit tests & A/B benchmark on macOS arm64 using local Win32/D3D12/DXGI struct stubs; Windows CTest builds against the real Windows SDK headers).
  • Compiled and ran runtime/test/xs_alloc_size_test.cpp with clang++ -std=c++20 -Wall -Wextra -Werror:
    • ASan+UBSan (-O1 -g -fsanitize=address,undefined): xs_alloc_size_test: OK (0 failures).
    • TSan (-O1 -g -fsanitize=thread): xs_alloc_size_test: OK (0 failures).
    • Release (-O2): xs_alloc_size_test: OK (0 failures).
  • Negative control + A/B -O2 benchmark (20,000 calls to console_placement over 8 mixed RT/CPU/DS descriptors with non-null each):
    • main: wrapped overflow cases to 4611683819404197888; pc_calls=320000 (22.657 ms).
    • this branch: returns 0 on both overflow cases; pc_calls=160000 (2× fewer pc_one calls, 14.105 ms / 1.61× faster, bit-exact layout checksum).
  • python3 -I scripts/check_no_game_data.py runtime/shims/d3d12_xs_alloc_size.h runtime/test/xs_alloc_size_test.cpp runtime/CMakeLists.txt: clean.
  • python3 -I tests/test_text_write_line_endings.py: 3/3 OK.
**@⁠IlyassRS3** on GitHub · 2026-11-25 18:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2029) `IlyassRS3:fix/rt-xs-alloc-size-overflow-and-single-pass` → `main` · +274 −45 · 3 files · 1 commits · open Coordination item: `RT-XS-ALLOC-SIZE-OVERFLOW-AND-SINGLE-PASS` (claimed on #1608). ### Why 1. `align_up_saturating(value, to)` in `runtime/shims/d3d12_xs_alloc_size.h` divided by `to` unconditionally, causing integer divide-by-zero if `to == 0`. 2. `console_render_target_size()` and `console_depth_stencil_size()` documented returning `0` on overflow, yet accumulated `slice += surface.tiled_size()` and `slice += kConsoleTileBytes` without checking `UINT64_MAX - slice`. Multi-mip render targets (e.g. `0x3FFFFFBF x 0x3FFFFFBF` 16-byte format, 2 mips) and multi-plane depth-stencil targets (e.g. `0x7FFFFF7F x 0x7FFFFF7F` `D32_FLOAT_S8X24_UINT`) silently wrapped `slice` to `4,611,683,819,404,197,888` instead of returning `0`. Checking `desc.Width > UINT32_MAX` before `std::bit_width()` also avoids entering the mip shift loop on >32-bit widths. 3. `console_placement()` executed up to 3 passes when `each != nullptr` (an `any_target` pre-scan plus two full passes that each called `pc_one(i)` and re-evaluated `console_texture_layout` / `console_render_target_size` / `console_depth_stencil_size`), doubling `ID3D12Device::GetResourceAllocationInfo` calls and tripling XG layout work. ### Changes - `runtime/shims/d3d12_xs_alloc_size.h`: - Guard `if (to <= 1) return value;` in `align_up_saturating()`. - Reject `desc.Width > UINT32_MAX` upfront and check `UINT64_MAX - slice` before every mip/tail addition in `console_render_target_size()` and `console_depth_stencil_size()`. - Precompute each descriptor's applicable console rule once (using an inline 16-entry buffer with vector spill for larger lists), exit with 0 `pc_one` calls when none apply, run a single `pc_one` layout pass staging `each` entries locally, and publish `total`/`each` only after succeeding with `grows == true`. - `runtime/test/xs_alloc_size_test.cpp` + `runtime/CMakeLists.txt`: add CPU-only `xs_alloc_size` unit test covering `align_up_saturating(to=0)`, width/mip/plane overflow guards, fail-closed `each`/`total` preservation, single-pass `pc_one` call count, and the `count > 16` spill path. ### Verification - AI-assisted: yes (Antigravity agent by Google DeepMind). - GPUs tested: none (CPU-only unit tests & A/B benchmark on macOS arm64 using local Win32/D3D12/DXGI struct stubs; Windows CTest builds against the real Windows SDK headers). - Compiled and ran `runtime/test/xs_alloc_size_test.cpp` with `clang++ -std=c++20 -Wall -Wextra -Werror`: - ASan+UBSan (`-O1 -g -fsanitize=address,undefined`): `xs_alloc_size_test: OK (0 failures)`. - TSan (`-O1 -g -fsanitize=thread`): `xs_alloc_size_test: OK (0 failures)`. - Release (`-O2`): `xs_alloc_size_test: OK (0 failures)`. - Negative control + A/B `-O2` benchmark (20,000 calls to `console_placement` over 8 mixed RT/CPU/DS descriptors with non-null `each`): - `main`: wrapped overflow cases to `4611683819404197888`; `pc_calls=320000` (`22.657 ms`). - this branch: returns `0` on both overflow cases; `pc_calls=160000` (**2× fewer `pc_one` calls**, `14.105 ms` / **1.61× faster**, bit-exact layout checksum). - `python3 -I scripts/check_no_game_data.py runtime/shims/d3d12_xs_alloc_size.h runtime/test/xs_alloc_size_test.cpp runtime/CMakeLists.txt`: clean. - `python3 -I tests/test_text_write_line_endings.py`: 3/3 OK.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:15:14 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 18:32 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 18:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2029#pullrequestreview-6062941504) · **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.
This pull request has changes conflicting with the target branch.
  • runtime/shims/d3d12_xs_alloc_size.h
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2029/head:github-bot/gh-2029'
git switch 'github-bot/gh-2029'

Merge

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