GPU: sanitize CreateSamplerX LOD range, bias clamp, and invalid enum values #474

Manually merged
github merged 2 commits from gh-pr/474/head into gh-pr/474/base 2026-11-26 04:05:14 +00:00
Owner

@⁠nathanbzrr on GitHub · 2026-11-24 03:50 UTC · original
nathanbzrr:fix/gpu-console-sampler-validation → main · +121 −11 · 1 files · 2 commits · merged 2026-11-25 00:11 UTC

Problem

Console sampler descriptions translated in crates/victory-gpu/src/sampler.rs (Fields::host) could pass out-of-range, non-finite, or mismatched values into D3D12_SAMPLER_DESC:

  • When mip == 0 (no mip filter), MinLOD/MaxLOD used (self.min_lod, self.min_lod) without sanitizing non-finite (NaN/+/-inf) values.
  • When mip != 0, MaxLOD < MinLOD and non-finite (NaN/+/-inf) LOD values were forwarded verbatim to ID3D12Device::CreateSampler.
  • MipLODBias was forwarded unclamped outside [D3D12_MIP_LOD_BIAS_MIN, D3D12_MIP_LOD_BIAS_MAX] ([-16.0, 15.99]) and could pass NaN.
  • AddressU/AddressV/AddressW outside 1..=5 and ComparisonFunc when reduction != 1 (non-comparison filters) or outside valid bounds were forwarded verbatim.

What changed

  • Added address_mode(mode: u32) -> D3D12_TEXTURE_ADDRESS_MODE in crates/victory-gpu/src/sampler.rs mapping 1..=5 directly and falling back to D3D12_TEXTURE_ADDRESS_MODE_CLAMP for 0 or values > 5.
  • Clamped MipLODBias to [D3D12_MIP_LOD_BIAS_MIN, D3D12_MIP_LOD_BIAS_MAX] with a 0.0 fallback for NaN.
  • Gated ComparisonFunc on reduction == 1 (whose compares helper guarantees 2..=7), emitting D3D12_COMPARISON_FUNC_NEVER otherwise.
  • Sanitized min_lod via finite_lod(self.min_lod, 0.0) (NaN -> 0.0, infinities clamped to [-D3D12_FLOAT32_MAX, D3D12_FLOAT32_MAX]). When mip == 0, kept (min_lod, min_lod) clamping behavior matching main; otherwise sanitized max_lod and clamped max_lod = max_lod.max(min_lod).
  • Added synthetic unit tests mip_none_clamps_to_min_lod_and_inverted_lod_range_clamps and bias_address_and_comparison_stay_in_host_bounds.

Tests (isolated branch e34cb53)

  • rustfmt --edition 2024 --check crates\victory-gpu\src\sampler.rs: passed (exit 0).
  • cargo test -p victory-gpu -- --test-threads=1: 45 passed, 0 failed, 1 ignored benchmark (includes sampler::tests::console_descriptions_decode, sampler::tests::mip_none_clamps_to_min_lod_and_inverted_lod_range_clamps, and sampler::tests::bias_address_and_comparison_stay_in_host_bounds).
  • cargo clippy -p victory-gpu --all-targets: passed (exit 0; 0 warnings in crates/victory-gpu/src/sampler.rs, 1 pre-existing warning at crates/victory-gpu/src/host.rs:267).
  • cargo xtask policy: asset policy: 151 files ok.

No game files. AI-assisted; reviewed and verified with the tests above.

**@⁠nathanbzrr** on GitHub · 2026-11-24 03:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/474) `nathanbzrr:fix/gpu-console-sampler-validation` → `main` · +121 −11 · 1 files · 2 commits · merged 2026-11-25 00:11 UTC ## Problem Console sampler descriptions translated in `crates/victory-gpu/src/sampler.rs` (`Fields::host`) could pass out-of-range, non-finite, or mismatched values into `D3D12_SAMPLER_DESC`: - When `mip == 0` (no mip filter), `MinLOD`/`MaxLOD` used `(self.min_lod, self.min_lod)` without sanitizing non-finite (`NaN`/`+/-inf`) values. - When `mip != 0`, `MaxLOD < MinLOD` and non-finite (`NaN`/`+/-inf`) LOD values were forwarded verbatim to `ID3D12Device::CreateSampler`. - `MipLODBias` was forwarded unclamped outside `[D3D12_MIP_LOD_BIAS_MIN, D3D12_MIP_LOD_BIAS_MAX]` (`[-16.0, 15.99]`) and could pass `NaN`. - `AddressU`/`AddressV`/`AddressW` outside `1..=5` and `ComparisonFunc` when `reduction != 1` (non-comparison filters) or outside valid bounds were forwarded verbatim. ## What changed - Added `address_mode(mode: u32) -> D3D12_TEXTURE_ADDRESS_MODE` in `crates/victory-gpu/src/sampler.rs` mapping `1..=5` directly and falling back to `D3D12_TEXTURE_ADDRESS_MODE_CLAMP` for `0` or values `> 5`. - Clamped `MipLODBias` to `[D3D12_MIP_LOD_BIAS_MIN, D3D12_MIP_LOD_BIAS_MAX]` with a `0.0` fallback for `NaN`. - Gated `ComparisonFunc` on `reduction == 1` (whose `compares` helper guarantees `2..=7`), emitting `D3D12_COMPARISON_FUNC_NEVER` otherwise. - Sanitized `min_lod` via `finite_lod(self.min_lod, 0.0)` (`NaN` -> `0.0`, infinities clamped to `[-D3D12_FLOAT32_MAX, D3D12_FLOAT32_MAX]`). When `mip == 0`, kept `(min_lod, min_lod)` clamping behavior matching `main`; otherwise sanitized `max_lod` and clamped `max_lod = max_lod.max(min_lod)`. - Added synthetic unit tests `mip_none_clamps_to_min_lod_and_inverted_lod_range_clamps` and `bias_address_and_comparison_stay_in_host_bounds`. ## Tests (isolated branch `e34cb53`) - `rustfmt --edition 2024 --check crates\victory-gpu\src\sampler.rs`: passed (exit 0). - `cargo test -p victory-gpu -- --test-threads=1`: 45 passed, 0 failed, 1 ignored benchmark (includes `sampler::tests::console_descriptions_decode`, `sampler::tests::mip_none_clamps_to_min_lod_and_inverted_lod_range_clamps`, and `sampler::tests::bias_address_and_comparison_stay_in_host_bounds`). - `cargo clippy -p victory-gpu --all-targets`: passed (exit 0; 0 warnings in `crates/victory-gpu/src/sampler.rs`, 1 pre-existing warning at `crates/victory-gpu/src/host.rs:267`). - `cargo xtask policy`: `asset policy: 151 files ok`. No game files. AI-assisted; reviewed and verified with the tests above.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:05:12 +00:00
github manually merged commit e34cb5396a into gh-pr/474/base 2026-11-26 04:05:14 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 03:56 UTC · original

Verified e6fe8ec on Arch Linux x86_64, Windows MSVC release build under Wine 11.22 (victory-gpu, --test-threads=1): 45 passed, 0 failed, 1 ignored (one more passing test than main, presumably the new sampler case).

Not covered: the title's samplers with these enum/edge combinations (no game data here).

**@⁠DaviTostes** on GitHub · 2026-11-24 03:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/474#issuecomment-7121555988) Verified `e6fe8ec` on **Arch Linux x86_64**, Windows MSVC release build under **Wine 11.22** (`victory-gpu`, `--test-threads=1`): **45 passed, 0 failed, 1 ignored** (one more passing test than `main`, presumably the new sampler case). Not covered: the title's samplers with these enum/edge combinations (no game data here).
Author
Owner

@⁠nathanbzrr on GitHub · 2026-11-24 06:49 UTC · original

Thanks @⁠mercuryVM for catching the unverified mip == 0 change!

Updated branch fix/gpu-console-sampler-validation in commit e34cb53 to keep finite (min_lod, min_lod) when mip == 0 (matching main's min_lod clamping while still sanitizing NaN/+/-inf via finite_lod), keeping the remaining hardening (MaxLOD >= MinLOD when mip != 0, MipLODBias clamp/NaN fallback, AddressU/V/W fallback to CLAMP, and ComparisonFunc gated on reduction == 1).

Also updated the unit test (mip_none_clamps_to_min_lod_and_inverted_lod_range_clamps) and PR description. Re-verified on e34cb53:

  • rustfmt --edition 2024 --check crates\victory-gpu\src\sampler.rs: passed
  • cargo test -p victory-gpu -- --test-threads=1: 45 passed, 0 failed, 1 ignored (all 3 sampler::tests::* pass)
  • cargo clippy -p victory-gpu --all-targets: 0 warnings in sampler.rs
  • cargo xtask policy: asset policy: 151 files ok
**@⁠nathanbzrr** on GitHub · 2026-11-24 06:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/474#issuecomment-7124321515) Thanks @⁠mercuryVM for catching the unverified `mip == 0` change! Updated branch `fix/gpu-console-sampler-validation` in commit `e34cb53` to keep finite `(min_lod, min_lod)` when `mip == 0` (matching `main`'s `min_lod` clamping while still sanitizing `NaN`/`+/-inf` via `finite_lod`), keeping the remaining hardening (`MaxLOD >= MinLOD` when `mip != 0`, `MipLODBias` clamp/`NaN` fallback, `AddressU`/`V`/`W` fallback to `CLAMP`, and `ComparisonFunc` gated on `reduction == 1`). Also updated the unit test (`mip_none_clamps_to_min_lod_and_inverted_lod_range_clamps`) and PR description. Re-verified on `e34cb53`: - `rustfmt --edition 2024 --check crates\victory-gpu\src\sampler.rs`: passed - `cargo test -p victory-gpu -- --test-threads=1`: 45 passed, 0 failed, 1 ignored (all 3 `sampler::tests::*` pass) - `cargo clippy -p victory-gpu --all-targets`: 0 warnings in `sampler.rs` - `cargo xtask policy`: `asset policy: 151 files ok`
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!474
No description provided.