shader: look up an input channel without underflowing #56

Manually merged
github merged 1 commit from gh-pr/56/head into gh-pr/56/base 2026-11-26 03:48:33 +00:00
Owner

@⁠9hvn9yt2vk-netizen on GitHub · 2026-11-23 23:58 UTC · original
9hvn9yt2vk-netizen:find-input-underflow → main · +58 −1 · 2 files · 1 commits · merged 2026-11-24 00:34 UTC

find_input matched each input element with
(row and channel in the element).then_some((id, channel - e.col)). then_some evaluates its argument first, so the subtraction ran for every element, also those whose first channel is past the one asked for. In a build with overflow checks (tests, debug runs) a pixel shader reading a channel below any element that starts at z or w panicked, whichever row that element was in, and packed inputs such as a float2 in .zw are common. Release builds wrapped and threw the value away.

then() computes it only when the element holds the channel.

Found with a random-input harness over pixel, vertex and mesh programs with matching signatures. The new test panics without the fix.

**@⁠9hvn9yt2vk-netizen** on GitHub · 2026-11-23 23:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/56) `9hvn9yt2vk-netizen:find-input-underflow` → `main` · +58 −1 · 2 files · 1 commits · merged 2026-11-24 00:34 UTC find_input matched each input element with `(row and channel in the element).then_some((id, channel - e.col))`. then_some evaluates its argument first, so the subtraction ran for every element, also those whose first channel is past the one asked for. In a build with overflow checks (tests, debug runs) a pixel shader reading a channel below any element that starts at z or w panicked, whichever row that element was in, and packed inputs such as a float2 in .zw are common. Release builds wrapped and threw the value away. then() computes it only when the element holds the channel. Found with a random-input harness over pixel, vertex and mesh programs with matching signatures. The new test panics without the fix.
find_input matched each input element with
`(row and channel in the element).then_some((id, channel - e.col))`.
then_some evaluates its argument first, so the subtraction ran for every
element, also those whose first channel is past the one asked for. In a
build with overflow checks (tests, debug runs) a pixel shader reading a
channel below any element that starts at z or w panicked, whichever row
that element was in, and packed inputs such as a float2 in .zw are
common. Release builds wrapped and threw the value away.

then() computes it only when the element holds the channel.

Found with a random-input harness over pixel, vertex and mesh programs
with matching signatures. The new test panics without the fix.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:48:32 +00:00
github manually merged commit 5fbea4730b into gh-pr/56/base 2026-11-26 03:48:33 +00:00
Author
Owner

@⁠9hvn9yt2vk-netizen on GitHub · 2026-11-23 23:59 UTC · original

juwwww

**@⁠9hvn9yt2vk-netizen** on GitHub · 2026-11-23 23:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/56#issuecomment-7117586826) juwwww
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 00:02 UTC · original

Tested 5fbea47 stacked on #39 on Arch Linux x86_64, rustc 1.98.1: pass.

  • main alone does not build on Linux (E0425 in victory-gpu), so the check uses #39 as the base; the merge is clean.
  • cargo test --workspace: 156 passed, 0 failed, 3 ignored, including the new tests this PR adds in emit/mod.rs (55 lines).
  • No game data used.

AI-assisted (DeepSeek V4.1 Flash coding agent).

**@⁠DaviTostes** on GitHub · 2026-11-24 00:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/56#issuecomment-7117645074) Tested `5fbea47` stacked on **#39** on **Arch Linux x86_64, rustc 1.98.1**: **pass**. - `main` alone does not build on Linux (E0425 in `victory-gpu`), so the check uses #39 as the base; the merge is clean. - `cargo test --workspace`: **156 passed, 0 failed, 3 ignored**, including the new tests this PR adds in `emit/mod.rs` (55 lines). - No game data used. AI-assisted (DeepSeek V4.1 Flash coding agent).
Author
Owner

@⁠lordon1a on GitHub · 2026-11-24 00:17 UTC · original

Verified 5fbea4730 on Windows 11 (x86_64-pc-windows-gnullvm, rustc 1.100.0, applied cleanly to main@35b054ec0): pass.

  • cargo xtask policy: 150 files ok
  • cargo test -p victory-shader: 58 passed, 0 failed (including reading_a_channel_below_an_input_element_does_not_underflow)
  • Negative control (reverted crates/victory-shader/src/emit/lower.rs to main): 1 failed (reading_a_channel_below_an_input_element_does_not_underflow panics on underflow as expected).

AI-assisted.

**@⁠lordon1a** on GitHub · 2026-11-24 00:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/56#issuecomment-7117911227) Verified `5fbea4730` on Windows 11 (`x86_64-pc-windows-gnullvm`, `rustc 1.100.0`, applied cleanly to `main@35b054ec0`): **pass**. - `cargo xtask policy`: 150 files ok - `cargo test -p victory-shader`: **58 passed, 0 failed** (including `reading_a_channel_below_an_input_element_does_not_underflow`) - Negative control (reverted `crates/victory-shader/src/emit/lower.rs` to `main`): **1 failed** (`reading_a_channel_below_an_input_element_does_not_underflow` panics on underflow as expected). AI-assisted.
Author
Owner

@⁠tedin7 on GitHub · 2026-11-24 00:37 UTC · original

checked: this was already merged (8fe0b74) while I was running these, so this is a post-merge confirmation of 5fbea47 rather than a pre-merge review.

Environment: Linux x86_64 (Debian, kernel 6.12), Rust 1.98.1 from rust-toolchain.toml, cargo -j4, no game files, no GPU.

What I ran (in a throwaway worktree of the PR head 5fbea47, base 97c78d1):

  • cargo test -j4 -p victory-shader: 52 passed, 0 failed.
  • cargo clippy -j4 -p victory-shader --tests: no new warnings from this change. One existing dead_code warning (constant_color in the test code), unrelated to this PR.
  • Test fails before the fix: I restored lower.rs from the base commit (97c78d1, with then_some) and ran cargo test -p victory-shader reading_a_channel. reading_a_channel_below_an_input_element_does_not_underflow FAILED with "attempt to subtract with overflow" at lower.rs:1017. With the PR's lower.rs it passes. So the test guards the fix.
  • Merge check: a trial git merge-tree of the PR head against main at the time was clean.

Review notes:

  • The claim holds. then_some evaluates channel - e.col eagerly for every element, and then(|| ...) only when the range check passes. The range check already makes the subtraction safe.
  • I grepped the other then_some uses in the workspace for a similar eager-arithmetic pattern and found none that can overflow (casts and lookups only).

Not verified: cargo test --workspace on Linux. Main does not build victory-gpu on Linux at the moment (E0425, host_resource is #[cfg(windows)] but used from code that is not), as the earlier Linux check on this PR also found; #39 looks like the fix. Also not verified: Windows runtime, D3D12, any GPU or dxil validation.

**@⁠tedin7** on GitHub · 2026-11-24 00:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/56#issuecomment-7118258913) checked: this was already merged (8fe0b74) while I was running these, so this is a post-merge confirmation of 5fbea47 rather than a pre-merge review. Environment: Linux x86_64 (Debian, kernel 6.12), Rust 1.98.1 from rust-toolchain.toml, cargo -j4, no game files, no GPU. What I ran (in a throwaway worktree of the PR head 5fbea47, base 97c78d1): - `cargo test -j4 -p victory-shader`: 52 passed, 0 failed. - `cargo clippy -j4 -p victory-shader --tests`: no new warnings from this change. One existing dead_code warning (`constant_color` in the test code), unrelated to this PR. - Test fails before the fix: I restored `lower.rs` from the base commit (97c78d1, with `then_some`) and ran `cargo test -p victory-shader reading_a_channel`. `reading_a_channel_below_an_input_element_does_not_underflow` FAILED with "attempt to subtract with overflow" at lower.rs:1017. With the PR's `lower.rs` it passes. So the test guards the fix. - Merge check: a trial `git merge-tree` of the PR head against main at the time was clean. Review notes: - The claim holds. `then_some` evaluates `channel - e.col` eagerly for every element, and `then(|| ...)` only when the range check passes. The range check already makes the subtraction safe. - I grepped the other `then_some` uses in the workspace for a similar eager-arithmetic pattern and found none that can overflow (casts and lookups only). Not verified: `cargo test --workspace` on Linux. Main does not build `victory-gpu` on Linux at the moment (E0425, `host_resource` is `#[cfg(windows)]` but used from code that is not), as the earlier Linux check on this PR also found; #39 looks like the fix. Also not verified: Windows runtime, D3D12, any GPU or dxil validation.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!56
No description provided.