Skip to content

shader: look up an input channel without underflowing - #56

Merged
Marvisak merged 1 commit into
Marvisak:mainfrom
9hvn9yt2vk-netizen:find-input-underflow
Nov 24, 2026
Merged

Marvisak merged 1 commit into
Marvisak:mainfrom
9hvn9yt2vk-netizen:find-input-underflow

Conversation

@9hvn9yt2vk-netizen

Copy link
Copy Markdown
Contributor

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.
@9hvn9yt2vk-netizen

Copy link
Copy Markdown
Contributor Author

juwwww

@DaviTostes

Copy link
Copy Markdown

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 Build: make workspace tests run on Linux #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).

@lordon1a

Copy link
Copy Markdown
Contributor

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.

@Marvisak
Marvisak merged commit 8fe0b74 into Marvisak:main Nov 24, 2026
@tedin7

tedin7 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants