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

Open
opened 2026-11-26 21:19:48 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2029
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/rt-xs-alloc-size-overflow-and-single-pass


📝 Commits (1)

  • 0c4bdde runtime: guard overflow/div-by-zero and remove duplicate passes in d3d12_xs_alloc_size

📊 Changes

3 files changed (+274 additions, -45 deletions)

View changed files

📝 runtime/CMakeLists.txt (+9 -0)
📝 runtime/shims/d3d12_xs_alloc_size.h (+94 -45)
➕ runtime/test/xs_alloc_size_test.cpp (+171 -0)

📄 Description

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.

🔄 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/2029 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-xs-alloc-size-overflow-and-single-pass` --- ### 📝 Commits (1) - [`0c4bdde`](https://github.com/ubiquitarianism/leonida/commit/0c4bdde51c696c7fd2dc3e77004c8a7ad9fd4134) runtime: guard overflow/div-by-zero and remove duplicate passes in d3d12_xs_alloc_size ### 📊 Changes **3 files changed** (+274 additions, -45 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+9 -0) 📝 `runtime/shims/d3d12_xs_alloc_size.h` (+94 -45) ➕ `runtime/test/xs_alloc_size_test.cpp` (+171 -0) </details> ### 📄 Description 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
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#2035
No description provided.