Shaders: fix DXIL validator UB on null error blobs, unchecked GetStatus, and COM/DLL handle leaks #477

Open
github-bot wants to merge 2 commits from github-bot/gh-477 into main AGit
Owner

@⁠nathanbzrr on GitHub · 2026-11-24 03:50 UTC · original
nathanbzrr:fix/shader-validator-com-soundness → main · +124 −14 · 2 files · 2 commits · open

Problem

Validator in crates/victory-shader/src/emit/validator.rs had four COM/FFI soundness and resource management bugs:

  • IDxcOperationResult::GetStatus returned HRESULT was ignored; if GetStatus failed, status remained 0 (S_OK) and validate() falsely returned Ok(()).
  • std::slice::from_raw_parts(ptr(errors), len(errors)) was called without checking !p.is_null() && n > 0, which is UB (and panics in debug builds) when an empty IDxcBlob returns a null buffer pointer.
  • When IDxcValidator::Validate returned hr < 0 with a non-null IDxcOperationResult, result was leaked without Release().
  • LoadLibraryW's HMODULE was leaked on GetProcAddress failure, DxcCreateInstance failure, and Validator::drop(), and the missing-DLL error included the trailing \0 from name.

What changed

  • Stored module: HMODULE on Validator (null for non-DLL/mock instances); stripped the trailing NUL when formatting cannot load ...; called FreeLibrary(module) on GetProcAddress / DxcCreateInstance errors; and in Drop::drop released self.object before calling FreeLibrary(self.module).
  • In Validator::validate: released result when Validate fails (release already null-checks), checked the HRESULT returned by GetStatus, and guarded std::slice::from_raw_parts on !p.is_null() && n > 0.
  • Added one small unit test (emit::validator::tests::null_error_blob_falls_back_without_ub) reusing Blob / BLOB_VTABLE to exercise null/empty error blobs without dxil.dll.

Tests (head 79c27a9)

  • rustfmt --edition 2024 --check crates/victory-shader/src/emit/validator.rs: passed.
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings: passed.
  • cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings: passed.
  • cargo test -p victory-shader --locked -- --test-threads=1: 70 passed, 0 failed, 0 ignored (unit tests, including emit::validator::tests::null_error_blob_falls_back_without_ub and emit::tests::constant_color_validates); 0 doc-tests.
  • 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/477) `nathanbzrr:fix/shader-validator-com-soundness` → `main` · +124 −14 · 2 files · 2 commits · open ## Problem `Validator` in `crates/victory-shader/src/emit/validator.rs` had four COM/FFI soundness and resource management bugs: - `IDxcOperationResult::GetStatus` returned `HRESULT` was ignored; if `GetStatus` failed, `status` remained `0` (`S_OK`) and `validate()` falsely returned `Ok(())`. - `std::slice::from_raw_parts(ptr(errors), len(errors))` was called without checking `!p.is_null() && n > 0`, which is UB (and panics in debug builds) when an empty `IDxcBlob` returns a null buffer pointer. - When `IDxcValidator::Validate` returned `hr < 0` with a non-null `IDxcOperationResult`, `result` was leaked without `Release()`. - `LoadLibraryW`'s `HMODULE` was leaked on `GetProcAddress` failure, `DxcCreateInstance` failure, and `Validator::drop()`, and the missing-DLL error included the trailing `\0` from `name`. ## What changed - Stored `module: HMODULE` on `Validator` (null for non-DLL/mock instances); stripped the trailing NUL when formatting `cannot load ...`; called `FreeLibrary(module)` on `GetProcAddress` / `DxcCreateInstance` errors; and in `Drop::drop` released `self.object` before calling `FreeLibrary(self.module)`. - In `Validator::validate`: released `result` when `Validate` fails (`release` already null-checks), checked the `HRESULT` returned by `GetStatus`, and guarded `std::slice::from_raw_parts` on `!p.is_null() && n > 0`. - Added one small unit test (`emit::validator::tests::null_error_blob_falls_back_without_ub`) reusing `Blob` / `BLOB_VTABLE` to exercise null/empty error blobs without `dxil.dll`. ## Tests (head `79c27a9`) - `rustfmt --edition 2024 --check crates/victory-shader/src/emit/validator.rs`: passed. - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings`: passed. - `cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings`: passed. - `cargo test -p victory-shader --locked -- --test-threads=1`: 70 passed, 0 failed, 0 ignored (unit tests, including `emit::validator::tests::null_error_blob_falls_back_without_ub` and `emit::tests::constant_color_validates`); 0 doc-tests. - `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:25 +00:00
Author
Owner

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

Verified bd59ba3 on Arch Linux x86_64 (rustc 1.98.1): cargo test -p victory-shader 56 passed, 0 failed, 0 ignored.

Caveat: the validator is Windows-only, so the Linux suite does not exercise the null error blobs, GetStatus or the COM/DLL handle paths this PR changes. Under Wine 11.22 without dxil.dll, the same tests skip validation, so this needs a machine with the validator (VICTORY_DXIL) to be meaningful.

**@⁠DaviTostes** on GitHub · 2026-11-24 03:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/477#issuecomment-7121554165) Verified `bd59ba3` on **Arch Linux x86_64** (rustc 1.98.1): `cargo test -p victory-shader` **56 passed, 0 failed, 0 ignored**. Caveat: the validator is Windows-only, so the Linux suite does not exercise the null error blobs, `GetStatus` or the COM/DLL handle paths this PR changes. Under Wine 11.22 without `dxil.dll`, the same tests skip validation, so this needs a machine with the validator (`VICTORY_DXIL`) to be meaningful.
Author
Owner

@⁠nathanbzrr on GitHub · 2026-11-24 21:35 UTC · original

Thanks @⁠catminou4 for the review!

Confirming from crates/victory-shader/src/emit/validator.rs:

  • Both Validator::object and Validator::module are private fields on Validator.
  • Validator::load is the sole production constructor: it only constructs Ok(Validator { object, module }) after LoadLibraryW succeeds with a non-null HMODULE and DxcCreateInstance succeeds (freeing module / releasing object on every early error path).
  • The only other Validator { object, module } literals are the three mock tests under #[cfg(test)] mod tests, all of which explicitly set module: std::ptr::null_mut().

Leaving commit bd59ba3 unchanged so Bundle #1511 stays on the verified head.

**@⁠nathanbzrr** on GitHub · 2026-11-24 21:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/477#issuecomment-7145133186) Thanks @⁠catminou4 for the review! Confirming from [`crates/victory-shader/src/emit/validator.rs`](https://github.com/Marvisak/VIctory/blob/bd59ba346da72d106ef47c2c837779ef8d8e5abd/crates/victory-shader/src/emit/validator.rs): - Both `Validator::object` and `Validator::module` are private fields on `Validator`. - `Validator::load` is the sole production constructor: it only constructs `Ok(Validator { object, module })` after `LoadLibraryW` succeeds with a non-null `HMODULE` and `DxcCreateInstance` succeeds (freeing `module` / releasing `object` on every early error path). - The only other `Validator { object, module }` literals are the three mock tests under `#[cfg(test)] mod tests`, all of which explicitly set `module: std::ptr::null_mut()`. Leaving commit `bd59ba3` unchanged so Bundle `#1511` stays on the verified head.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:27 UTC · original · edited 2026-11-25 03:25 UTC

Automated triage for the maintainers.

Merge candidate. In crates/victory-shader/src/emit/validator.rs:

  • Validator::validate now checks the GetStatus HRESULT;
  • it builds the error slice only from a non-null, non-empty blob;
  • it releases result and errors on every path;
  • Validator::load frees dxil.dll on each early error;
  • Drop releases the object before FreeLibrary;
  • null_error_blob_falls_back_without_ub covers the null and empty blob shapes.

The new head also adds Win32_Foundation to victory-shader's windows-sys features. That is a feature flag of a crate the package already uses, and Cargo.lock is untouched. CI passes.

On your reply: the explicit Win32_Foundation feature is what the earlier note asked for. Nothing else is needed from this review.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/477#issuecomment-7148530576) · edited 2026-11-25 03:25 UTC <!-- victory-triage v1 head=249de665216e46dbcd9c1bc5f285fe8e3d7a572a verdict=merge-candidate at=2026-11-25T03:26:10Z --> Automated triage for the maintainers. **Merge candidate.** In `crates/victory-shader/src/emit/validator.rs`: - `Validator::validate` now checks the `GetStatus` HRESULT; - it builds the error slice only from a non-null, non-empty blob; - it releases `result` and `errors` on every path; - `Validator::load` frees `dxil.dll` on each early error; - `Drop` releases the object before `FreeLibrary`; - `null_error_blob_falls_back_without_ub` covers the null and empty blob shapes. The new head also adds `Win32_Foundation` to victory-shader's `windows-sys` features. That is a feature flag of a crate the package already uses, and `Cargo.lock` is untouched. CI passes. On your reply: the explicit `Win32_Foundation` feature is what the earlier note asked for. Nothing else is needed from this review. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠nathanbzrr on GitHub · 2026-11-25 00:48 UTC · original

Trimmed in 79c27a9 (fix/shader-validator-com-soundness — 1 file changed, 123 insertions(+), 13 deletions(-) vs merge base d2a4bae):

  • Removed the redundant if !result.is_null() guard before release(result) in Validator::validate.
  • Removed the system-DLL FreeLibrary probe test (load_errors_strip_nul_and_free_module) and the ~300+ lines of mock COM vtable scaffolding, leaving one small test (emit::validator::tests::null_error_blob_falls_back_without_ub) that reuses Blob / BLOB_VTABLE.

Verified on 79c27a9 (Windows x86_64 MSVC):

  • rustfmt --edition 2024 --check crates/victory-shader/src/emit/validator.rs: passed
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings: passed
  • cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings: passed
  • cargo test -p victory-shader --locked -- --test-threads=1: 70 passed, 0 failed, 0 ignored (0 doc-tests)
  • cargo xtask policy: asset policy: 151 files ok
**@⁠nathanbzrr** on GitHub · 2026-11-25 00:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/477#issuecomment-7148903306) Trimmed in `79c27a9` (`fix/shader-validator-com-soundness` — `1 file changed, 123 insertions(+), 13 deletions(-)` vs merge base `d2a4bae`): - Removed the redundant `if !result.is_null()` guard before `release(result)` in `Validator::validate`. - Removed the system-DLL `FreeLibrary` probe test (`load_errors_strip_nul_and_free_module`) and the ~300+ lines of mock COM vtable scaffolding, leaving one small test (`emit::validator::tests::null_error_blob_falls_back_without_ub`) that reuses `Blob` / `BLOB_VTABLE`. Verified on `79c27a9` (Windows x86_64 MSVC): - `rustfmt --edition 2024 --check crates/victory-shader/src/emit/validator.rs`: passed - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings`: passed - `cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings`: passed - `cargo test -p victory-shader --locked -- --test-threads=1`: 70 passed, 0 failed, 0 ignored (0 doc-tests) - `cargo xtask policy`: `asset policy: 151 files ok`
Author
Owner

@⁠Calvix2k on GitHub · 2026-11-25 02:30 UTC · original

Independent verification on Windows 10 19045 (RX 7800 XT host, not relevant to this change):

  • your head 558aa9a + the "skip the GPU suites a device cannot run" patch from #1991 (this machine reports SM 6.5, so the probe suites would otherwise fail for an unrelated reason): 248 passed, 0 failed, 3 ignored for cargo test -p victory-shader --lib.
  • emit::validator::tests::null_error_blob_falls_back_without_ub passes on the unpatched head too (1 passed), and the SDK dxil.dll here is the 1.7 one from 10.0.26100, so the null-blob path is exercised with the same engine your report used.

No objection to the diff from a COM/HRESULT standpoint: GetStatus failure is now an error instead of a silent Ok(()), the slice is built only for a non-null, non-empty blob, and the operation result is released on the error path.

**@⁠Calvix2k** on GitHub · 2026-11-25 02:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/477#issuecomment-7150817373) Independent verification on Windows 10 19045 (RX 7800 XT host, not relevant to this change): - your head `558aa9a` + the "skip the GPU suites a device cannot run" patch from #1991 (this machine reports SM 6.5, so the probe suites would otherwise fail for an unrelated reason): **248 passed, 0 failed, 3 ignored** for `cargo test -p victory-shader --lib`. - `emit::validator::tests::null_error_blob_falls_back_without_ub` passes on the unpatched head too (1 passed), and the SDK `dxil.dll` here is the 1.7 one from 10.0.26100, so the null-blob path is exercised with the same engine your report used. No objection to the diff from a COM/HRESULT standpoint: `GetStatus` failure is now an error instead of a silent `Ok(())`, the slice is built only for a non-null, non-empty blob, and the operation result is released on the error path.
Author
Owner

@⁠nathanbzrr on GitHub · 2026-11-25 03:08 UTC · original

Added Win32_Foundation explicitly to windows-sys features in crates/victory-shader/Cargo.toml (head 249de66, without touching Cargo.lock), per the triage note.

Re-verified on 249de66 (Windows MSVC, VICTORY_DXIL SDK 10.0.26100.0):

  • cargo run -p xtask -- policy --staged: asset policy: 1 files ok
  • cargo clippy --workspace --all-targets -- -D warnings & --all-features: clean
  • cargo test -p victory-shader --locked: 248 passed, 0 failed, 3 ignored (including emit::validator::tests::null_error_blob_falls_back_without_ub)
**@⁠nathanbzrr** on GitHub · 2026-11-25 03:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/477#issuecomment-7151629887) Added `Win32_Foundation` explicitly to `windows-sys` features in `crates/victory-shader/Cargo.toml` (head `249de66`, without touching `Cargo.lock`), per the triage note. Re-verified on `249de66` (Windows MSVC, `VICTORY_DXIL` SDK `10.0.26100.0`): - `cargo run -p xtask -- policy --staged`: `asset policy: 1 files ok` - `cargo clippy --workspace --all-targets -- -D warnings` & `--all-features`: clean - `cargo test -p victory-shader --locked`: 248 passed, 0 failed, 3 ignored (including `emit::validator::tests::null_error_blob_falls_back_without_ub`)
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 01:08 UTC · original

Code review and Windows 11 (dxil.dll) verification at head 249de665216e merged cleanly onto main (63bcca43c6a0):

  • COM / DLL lifetime & UB audit (crates/victory-shader/src/emit/validator.rs:156-247):
    • Validator::load releases acquired COM objects and HMODULE on GetProcAddress/DxcCreateInstance failures; Drop releases self.object strictly before calling FreeLibrary(self.module), keeping OS module refcounts balanced across multi-threaded validator creation/drop.
    • validate() checks Validate, result.is_null(), and GetStatus, releases both result and errors on every exit path, and checks !p.is_null() && n > 0 before slice::from_raw_parts, falling back to format!("rejected ({status:#x})") on empty/null error blobs.
  • Verified (Windows 11, dxil.dll 10.0.26100.0, NVIDIA RTX 5090 + WARP, Rust 1.98.1):
    • cargo xtask policy: 191 files ok; cargo clippy --locked -p victory-shader --tests -- -D warnings: 0 warnings
    • validator:: emit::host::: 2/2 passed on both RTX 5090 and WARP (including null_error_blob_falls_back_without_ub); live dxil.dll validation (v_fract_f32_never_returns_one) passed on both RTX 5090 and WARP.

LGTM.

**@⁠fuzeofficlal** on GitHub · 2026-11-26 01:08 UTC · [original](https://github.com/Marvisak/VIctory/pull/477#issuecomment-7179471792) Code review and Windows 11 (`dxil.dll`) verification at head `249de665216e` merged cleanly onto `main` (`63bcca43c6a0`): - **COM / DLL lifetime & UB audit (`crates/victory-shader/src/emit/validator.rs:156-247`)**: - `Validator::load` releases acquired COM objects and `HMODULE` on `GetProcAddress`/`DxcCreateInstance` failures; `Drop` releases `self.object` strictly *before* calling `FreeLibrary(self.module)`, keeping OS module refcounts balanced across multi-threaded validator creation/drop. - `validate()` checks `Validate`, `result.is_null()`, and `GetStatus`, releases both `result` and `errors` on every exit path, and checks `!p.is_null() && n > 0` before `slice::from_raw_parts`, falling back to `format!("rejected ({status:#x})")` on empty/null error blobs. - **Verified (Windows 11, `dxil.dll` 10.0.26100.0, NVIDIA RTX 5090 + WARP, Rust 1.98.1)**: - `cargo xtask policy`: `191 files ok`; `cargo clippy --locked -p victory-shader --tests -- -D warnings`: 0 warnings - `validator:: emit::host::`: **2/2 passed** on both RTX 5090 and WARP (including `null_error_blob_falls_back_without_ub`); live `dxil.dll` validation (`v_fract_f32_never_returns_one`) passed on both RTX 5090 and WARP. LGTM.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/477/head:github-bot/gh-477'
git switch 'github-bot/gh-477'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-477'
git switch 'github-bot/gh-477'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-477'
git switch 'github-bot/gh-477'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-477'
git switch 'main'
git merge --squash 'github-bot/gh-477'
git switch 'main'
git merge --ff-only 'github-bot/gh-477'
git switch 'main'
git merge 'github-bot/gh-477'
git push origin 'main'
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!477
No description provided.