shader: look up an input channel without underflowing #56

Merged
9hvn9yt2vk-netizen merged 1 commit from upstream-pr/56 into main 2026-11-24 00:34:59 +00:00
9hvn9yt2vk-netizen commented 2026-11-23 23:58:57 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 9hvn9yt2vk-netizen on 2026-11-23T23:58:57Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6571405585 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/56). Originally posted by [9hvn9yt2vk-netizen](https://github.com/9hvn9yt2vk-netizen) on 2026-11-23T23:58:57Z. --- 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.
upstream-sync closed this pull request 2026-11-24 06:51:07 +00:00
9hvn9yt2vk-netizen commented 2026-11-24 06:54:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by 9hvn9yt2vk-netizen on 2026-11-23T23:59:45Z.


juwwww

<!-- greyfinch-sync:Marvisak/VIctory:comment:7117586826 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/56#issuecomment-7117586826). Originally posted by [9hvn9yt2vk-netizen](https://github.com/9hvn9yt2vk-netizen) on 2026-11-23T23:59:45Z. --- juwwww
DaviTostes commented 2026-11-24 06:54:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DaviTostes on 2026-11-24T00:02:53Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7117645074 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/56#issuecomment-7117645074). Originally posted by [DaviTostes](https://github.com/DaviTostes) on 2026-11-24T00:02:53Z. --- 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).
lordon1a commented 2026-11-24 06:54:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by lordon1a on 2026-11-24T00:17:26Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7117911227 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/56#issuecomment-7117911227). Originally posted by [lordon1a](https://github.com/lordon1a) on 2026-11-24T00:17:26Z. --- 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.
tedin7 commented 2026-11-24 06:54:40 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by tedin7 on 2026-11-24T00:37:16Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7118258913 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/56#issuecomment-7118258913). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T00:37:16Z. --- 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 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
Marvisak/VIctory!56
No description provided.