D3D12.X: check vertex range overflow and SizeInBytes truncation in bind::draw_rects #1551

Open
b3IKK wants to merge 1 commit from upstream-pr/1561 into main
b3IKK commented 2026-11-24 17:41:24 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T17:41:24Z.


bind::draw_rects computed va as usize + first_vertex as usize * stride as usize, first_vertex as usize * s + len, and (rects * 6 * s) as u32 with unchecked arithmetic. Large title-supplied first_vertex or stride values could panic in debug builds, wrap past the > bytes bounds check in release builds, or silently truncate the expanded D3D12_VERTEX_BUFFER_VIEW::SizeInBytes.

This extracts rect_vb_range() using checked_mul, checked_add, and u32::try_from, returning false from draw_rects when any range or size check fails.

What is in it

  • crates/xcl/src/hle/d3d12x/bind.rs: add rect_vb_range(), use it in draw_rects, and add unit test rect_vb_range_rejects_overflow_and_u32_truncation.

Tested

  • hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (stable-x86_64-pc-windows-msvc), DXC 1.9.2609.5, debug (scripts/test.ps1) and release (scripts/smoke.ps1)
  • .\scripts\test.ps1 (cargo build --workspace + cargo test --workspace -- --test-threads=4): 976 passed, 0 failed, 45 ignored (SKIPPED: the wave64 / direct-DXIL per-lane tests that need 64-lane hardware or a semantics mode this adapter refuses, no heap windows, and cannot disable the driver cache (Windows Developer Mode is required))
  • cargo test -p xcl --lib -- rect_vb_range execute_indirect: 4 passed, 0 failed (the new test and the three execute_indirect tests next to it, one of which the rebase had to be merged around)

Regression run

main (20cf130, clean tree, no local changes):

xcl-smoke v1
commit: 20cf13022f764208384c610c2ae6a3f4eed201a2
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1556 (PresentX 1556)
presents per 2 s (last 3): 24 21 54
device removals: 0
crashes: 0
peak commit: 19607 MB

PR (618f4c2, clean tree, no local changes):

xcl-smoke v1
commit: 618f4c2d5417e9df54595583cdfc0a5a6f9f5728
gpu: NVIDIA GeForce RTX 5080
title: 9cc6a39e
seconds: 60
exit: 0x0
frames shown: 1642 (PresentX 1642)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 18702 MB

(The head is on 689f2db; main moved to 20cf130 while the suite ran, and the main block is that commit, run right after the PR's. Both peak higher than the blocks this PR had before, 14.6 GB: several long runs of the title on this machine in between filled its caches, and its runs now commit 17–20 GB in the first minute whatever the build. I have not isolated why.)

Not tested

  • a title that passes such a first_vertex or stride (the unit test calls rect_vb_range directly)
  • AMD GPUs and native wave64 hardware
  • other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine

AI use

  • OpenCode, Claude Fable 5.2: audited bind.rs, wrote the fix and unit test, rebased onto 689f2db (resolving the conflict with the test main added at the same place), ran the test suite and scripts/smoke.ps1 regression runs, and drafted this PR description.
<!-- greyfinch-sync:ivall/xcl:issue:5477299577 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1561). Originally posted by [b3IKK](https://github.com/b3IKK) on 2026-11-24T17:41:24Z. --- `bind::draw_rects` computed `va as usize + first_vertex as usize * stride as usize`, `first_vertex as usize * s + len`, and `(rects * 6 * s) as u32` with unchecked arithmetic. Large title-supplied `first_vertex` or `stride` values could panic in debug builds, wrap past the `> bytes` bounds check in release builds, or silently truncate the expanded `D3D12_VERTEX_BUFFER_VIEW::SizeInBytes`. This extracts `rect_vb_range()` using `checked_mul`, `checked_add`, and `u32::try_from`, returning `false` from `draw_rects` when any range or size check fails. **What is in it** - `crates/xcl/src/hle/d3d12x/bind.rs`: add `rect_vb_range()`, use it in `draw_rects`, and add unit test `rect_vb_range_rejects_overflow_and_u32_truncation`. **Tested** - hardware (GPU + driver, CPU), Windows build, Rust version, debug / release: NVIDIA GeForce RTX 5080 (driver 32.0.16.1088), AMD Ryzen 9 9950X3D, Windows 11 10.0.26300, Rust 1.100.0 (`stable-x86_64-pc-windows-msvc`), DXC 1.9.2609.5, debug (`scripts/test.ps1`) and release (`scripts/smoke.ps1`) - `.\scripts\test.ps1` (`cargo build --workspace` + `cargo test --workspace -- --test-threads=4`): 976 passed, 0 failed, 45 ignored (SKIPPED: the wave64 / direct-DXIL per-lane tests that need 64-lane hardware or a semantics mode this adapter refuses, `no heap windows`, and `cannot disable the driver cache (Windows Developer Mode is required)`) - `cargo test -p xcl --lib -- rect_vb_range execute_indirect`: 4 passed, 0 failed (the new test and the three `execute_indirect` tests next to it, one of which the rebase had to be merged around) **Regression run** main (`20cf130`, clean tree, no local changes): ``` xcl-smoke v1 commit: 20cf13022f764208384c610c2ae6a3f4eed201a2 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1556 (PresentX 1556) presents per 2 s (last 3): 24 21 54 device removals: 0 crashes: 0 peak commit: 19607 MB ``` PR (`618f4c2`, clean tree, no local changes): ``` xcl-smoke v1 commit: 618f4c2d5417e9df54595583cdfc0a5a6f9f5728 gpu: NVIDIA GeForce RTX 5080 title: 9cc6a39e seconds: 60 exit: 0x0 frames shown: 1642 (PresentX 1642) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 18702 MB ``` (The head is on `689f2db`; `main` moved to `20cf130` while the suite ran, and the main block is that commit, run right after the PR's. Both peak higher than the blocks this PR had before, 14.6 GB: several long runs of the title on this machine in between filled its caches, and its runs now commit 17–20 GB in the first minute whatever the build. I have not isolated why.) **Not tested** - a title that passes such a `first_vertex` or `stride` (the unit test calls `rect_vb_range` directly) - AMD GPUs and native wave64 hardware - other titles (Minecraft Preview, Unicorn Overlord, Fortnite, XDefiant) and Linux / Wine **AI use** - OpenCode, Claude Fable 5.2: audited `bind.rs`, wrote the fix and unit test, rebased onto `689f2db` (resolving the conflict with the test `main` added at the same place), ran the test suite and `scripts/smoke.ps1` regression runs, and drafted this PR description.
futurepro62 commented 2026-11-24 19:07:55 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-24T19:02:53Z.


Full-suite run, since this PR has no test report yet. Tested exact head 1d751bb (on its base 9e3831e).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17 (Windows SDK 10.0.26100), debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form: 776 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: rect_vb_range_rejects_overflow_and_u32_truncation.
  • Same head on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 777 passed, 0 failed, 41 ignored. SKIPPED: heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • heap_window_out_of_memory_falls_back_to_the_switch_form was skipped with --skip. It fails on main 9e3831e too, not because of this PR: under XCL_REQUIRE_GPU=mesh, runner::skip_test turns every skip except "no wave64" into a failure, including this test's opt-in "no heap windows" skip (XCL_HEAP_WINDOWS unset). Reported as #1651.
  • In the first run, with another PR's suite running on the same machine, hle::kernelx::tests::fine_timer_makes_short_sleeps_short failed (a Sleep(1) timing check, sensitive to load). The run above was alone on the machine and is clean.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:ivall/xcl:comment:7141518526 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1561#issuecomment-7141518526). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-24T19:02:53Z. --- Full-suite run, since this PR has no test report yet. Tested exact head `1d751bb` (on its base `9e3831e`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17 (Windows SDK 10.0.26100), debug, D3D12 debug layer installed. - `cargo build --workspace`, then `XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form`: **776 passed, 0 failed, 41 ignored**. SKIPPED (no wave64): `gpu_geometry_wave32_prefix_count_over_a_mask`, `gpu_wave32_program_in_wave64_hardware`. - The new tests ran and passed: `rect_vb_range_rejects_overflow_and_u32_truncation`. - Same head on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, `XCL_ADAPTER=radeon`), `cargo test --workspace -- --test-threads=4`: **777 passed, 0 failed, 41 ignored**. SKIPPED: `heap_window_out_of_memory_falls_back_to_the_switch_form` (no heap windows). - `heap_window_out_of_memory_falls_back_to_the_switch_form` was skipped with `--skip`. It fails on main `9e3831e` too, not because of this PR: under `XCL_REQUIRE_GPU=mesh`, `runner::skip_test` turns every skip except "no wave64" into a failure, including this test's opt-in "no heap windows" skip (`XCL_HEAP_WINDOWS` unset). Reported as #1651. - In the first run, with another PR's suite running on the same machine, `hle::kernelx::tests::fine_timer_makes_short_sleeps_short` failed (a `Sleep(1)` timing check, sensitive to load). The run above was alone on the machine and is clean. **Not tested** - a release build, a game (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
DavidM1911 commented 2026-11-24 21:08:54 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-24T19:32:32Z.


Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~22 ignored. (Intermittent single-test flake under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7142246164 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1561#issuecomment-7142246164). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-24T19:32:32Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~22 ignored. (Intermittent single-test flake under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified `main`.) Thanks!
b3IKK commented 2026-11-25 00:08:54 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by b3IKK on 2026-11-24T23:25:15Z.


Rebased onto 689f2db (GitHub showed a conflict: main had added a test at the same place in bind.rs; both are kept, nothing else changed). Head 618f4c2: 976 passed, 0 failed, 45 ignored; smoke blocks in the description. AI use: OpenCode, Claude Fable 5.2.

<!-- greyfinch-sync:ivall/xcl:comment:7147378880 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1561#issuecomment-7147378880). Originally posted by [b3IKK](https://github.com/b3IKK) on 2026-11-24T23:25:15Z. --- Rebased onto `689f2db` (GitHub showed a conflict: `main` had added a test at the same place in `bind.rs`; both are kept, nothing else changed). Head `618f4c2`: 976 passed, 0 failed, 45 ignored; smoke blocks in the description. AI use: OpenCode, Claude Fable 5.2.
DavidM1911 commented 2026-11-26 06:06:47 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-26T05:48:23Z.


Re-verification on RTX 3060 Ti (Windows 11), new head merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved.

Thanks!

— Devin

<!-- greyfinch-sync:ivall/xcl:comment:7183821740 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1561#issuecomment-7183821740). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-26T05:48:23Z. --- Re-verification on RTX 3060 Ti (Windows 11), new head merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, 27 ignored. The earlier merge conflict is resolved. Thanks! — Devin
github-actions[bot] commented 2026-12-01 02:33:02 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by github-actions[bot] on 2026-12-01T02:33:02Z.


Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

<!-- greyfinch-sync:ivall/xcl:comment:7298736898 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1561#issuecomment-7298736898). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-12-01T02:33:02Z. --- Nothing has happened on this PR for 3 days, so it is marked `stale` and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto `main` if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.
Foniass commented 2026-12-01 06:07:26 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T06:07:26Z.


Triage (#1607) – head 618f4c2

No open or merged PR makes the same change. #266 changes the same draw_rects but picks the rect's corner vertex, not range checks.
rect_vb_range is correct: checked first_vertex * stride, va + offset and rects * 6 * stride, with u32::try_from for SizeInBytes. The u64::MAX - 16 test case does exercise the overflow path.
Nit: the new function and its doc comment were inserted between draw_rects' doc comment and draw_rects, so the existing rect-list description now attaches to rect_vb_range and draw_rects loses its doc.
The branch conflicts with main and needs a rebase.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7302506197 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/1561#issuecomment-7302506197). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T06:07:26Z. --- **Triage (#1607)** – head 618f4c2 No open or merged PR makes the same change. #266 changes the same `draw_rects` but picks the rect's corner vertex, not range checks. `rect_vb_range` is correct: checked `first_vertex * stride`, `va + offset` and `rects * 6 * stride`, with `u32::try_from` for `SizeInBytes`. The `u64::MAX - 16` test case does exercise the overflow path. Nit: the new function and its doc comment were inserted between `draw_rects`' doc comment and `draw_rects`, so the existing rect-list description now attaches to `rect_vb_range` and `draw_rects` loses its doc. The branch conflicts with main and needs a rebase. Recommendation: keep open. AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/1561:upstream-pr/1561'
git switch 'upstream-pr/1561'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/1561'
git switch 'upstream-pr/1561'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/1561'
git switch 'upstream-pr/1561'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/1561'
git switch 'main'
git merge --squash 'upstream-pr/1561'
git switch 'main'
git merge --ff-only 'upstream-pr/1561'
git switch 'main'
git merge 'upstream-pr/1561'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
xcl-dev/xcl!1551
No description provided.