gpu: fix alias liveness check, Memory::heaps drop eviction, and stream Bytecode UB #693

Merged
mint17 merged 1 commit from upstream-pr/693 into main 2026-11-29 08:16:58 +00:00
mint17 commented 2026-11-28 03:36:14 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by mint17 on 2026-11-28T03:36:14Z.


Addresses three correctness and memory-safety issues in crates/victory-gpu (zero conflict with #646 or #666):

Changes

  1. Alias liveness & refcount overflow hardening (object.rs, resource.rs):
    • Adds object::is_alive(this) (refs.load(Ordering::Acquire) > 0) and updates resource::is_aliased from alias_with(this, |_| ()).is_some() to alias_with(this, crate::object::is_alive).unwrap_or(false) so a dying target with refs == 0 still registered in TARGETS_AT before Resource::drop completes is not treated as an active alias.
    • Hardens object::add_ref_alive with checked_add(1) to prevent u32::MAX wrap-around.
  2. Evict Memory::heaps entry on Heap::drop (adapted from Marvisak#2234 — memory.rs, descriptor.rs):
    • Adds memory::release_heap(key: usize) matching Memory::heaps's HostHeap pointer key and calls it in Drop for Heap strictly after dropping the SHADER_HEAPS write guard.
  3. Unaligned pipeline stream Bytecode read & truncated subobject guard (adapted from Marvisak#2301 — pipeline.rs):
    • Replaces &*desc.stream.add(at + 8).cast::<Bytecode>() with .read_unaligned() and bounds-checks every subobject with saturating_sub / checked_add before reading payload or calling graphics.read.

Verification

  • cargo test --locked -p victory-gpu --lib -- add_ref_alive is_alive alias safety_tests -- --test-threads=1 (6 passed, 0 failed) and --features diagnostics (5 passed, 0 failed):
    • object::tests::add_ref_alive_and_is_alive_check_refcount_bounds
    • resource::tests::an_alias_source_is_held_after_the_title_releases_it (un-gated to #[cfg(windows)] with RAII cleanup)
    • resource::tests::is_aliased_ignores_dying_target_with_zero_refcount
    • descriptor::safety_tests::released_view_heap_evicts_memory_heap_entry (RAII-guarded)
    • pipeline::safety_tests::unaligned_stream_yields_its_shaders & truncated_shader_subobject_does_not_read_past_stream (RAII-guarded)
  • Zero Clippy warnings across all modified files (default and --all-features) and cargo test --locked -p xtask passed (6 passed, 0 failed, including asset policy).
  • Verified with synthetic unit and D3D12 host tests only (no game files).
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5536288757 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/693). Originally posted by [mint17](https://github.com/mint17) on 2026-11-28T03:36:14Z. --- Addresses three correctness and memory-safety issues in `crates/victory-gpu` (zero conflict with #646 or #666): ### Changes 1. **Alias liveness & refcount overflow hardening (`object.rs`, `resource.rs`):** - Adds `object::is_alive(this)` (`refs.load(Ordering::Acquire) > 0`) and updates `resource::is_aliased` from `alias_with(this, |_| ()).is_some()` to `alias_with(this, crate::object::is_alive).unwrap_or(false)` so a dying target with `refs == 0` still registered in `TARGETS_AT` before `Resource::drop` completes is not treated as an active alias. - Hardens `object::add_ref_alive` with `checked_add(1)` to prevent `u32::MAX` wrap-around. 2. **Evict `Memory::heaps` entry on `Heap::drop` (adapted from `Marvisak#2234` — `memory.rs`, `descriptor.rs`):** - Adds `memory::release_heap(key: usize)` matching `Memory::heaps`'s `HostHeap` pointer key and calls it in `Drop for Heap` strictly after dropping the `SHADER_HEAPS` write guard. 3. **Unaligned pipeline stream `Bytecode` read & truncated subobject guard (adapted from `Marvisak#2301` — `pipeline.rs`):** - Replaces `&*desc.stream.add(at + 8).cast::<Bytecode>()` with `.read_unaligned()` and bounds-checks every subobject with `saturating_sub` / `checked_add` before reading payload or calling `graphics.read`. ### Verification - `cargo test --locked -p victory-gpu --lib -- add_ref_alive is_alive alias safety_tests -- --test-threads=1` (6 passed, 0 failed) and `--features diagnostics` (5 passed, 0 failed): - `object::tests::add_ref_alive_and_is_alive_check_refcount_bounds` - `resource::tests::an_alias_source_is_held_after_the_title_releases_it` (un-gated to `#[cfg(windows)]` with RAII cleanup) - `resource::tests::is_aliased_ignores_dying_target_with_zero_refcount` - `descriptor::safety_tests::released_view_heap_evicts_memory_heap_entry` (RAII-guarded) - `pipeline::safety_tests::unaligned_stream_yields_its_shaders` & `truncated_shader_subobject_does_not_read_past_stream` (RAII-guarded) - Zero Clippy warnings across all modified files (default and `--all-features`) and `cargo test --locked -p xtask` passed (6 passed, 0 failed, including asset policy). - Verified with synthetic unit and D3D12 host tests only (no game files).
Dukestar69 commented 2026-11-28 04:48:17 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Dukestar69 on 2026-11-28T04:48:17Z.

Review: APPROVED


LGTM — verified on Windows (RTX 5090):

  • cargo test -p victory-gpu --lib memory:: (2/2), object:: (5/5, including add_ref_alive_and_is_alive_check_refcount_bounds), and pipeline::tests:: (2/2) all passed
  • 0 new compiler warnings in touched files
  • cargo xtask policy passed (294 files ok)

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:review:6095873443 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/693#pullrequestreview-6095873443). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-28T04:48:17Z. **Review:** APPROVED --- LGTM — verified on Windows (RTX 5090): - `cargo test -p victory-gpu --lib memory::` (2/2), `object::` (5/5, including `add_ref_alive_and_is_alive_check_refcount_bounds`), and `pipeline::tests::` (2/2) all passed - 0 new compiler warnings in touched files - `cargo xtask policy` passed (`294 files ok`) Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
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
trustno1-coder/VIctory-NVIDIA!693
No description provided.