gpu: fix aliased texture UAF, null CBV, heap eviction, split slot leak, and stream UB #291

Open
github-bot wants to merge 1 commit from github-bot/gh-291 into main AGit
Owner

@⁠mint17 on GitHub · 2026-11-27 13:51 UTC · original
mint17:fix/gpu-resource-lifetime-and-ub → main · +395 −18 · 10 files · 1 commits · open

Ports five missing upstream correctness and memory-safety fixes into crates/victory-gpu, adapted for Vice-Tay's architecture (zero merge conflict with #234, #245, or #272):

Fixes Included

  1. Pin aliased target textures across texture::alias (adapted from fxceless#1336):
    • Adds object::add_ref_alive / is_alive and resource::Held (Drop releasing via object::release_boxed::<Resource>) so resource::alias_of pins the target while crate::texture::alias copies from it (updating both draw.rs and external.rs Recording::prepare_input).
    • Splits non-owning resource::has_alias for boolean-only checks in resource::hosted (which holds r.host.made) and texture/ahead.rs, avoiding an r.host.made -> TEXTURES AB-BA deadlock and refcount churn.
  2. Clear stale slots on null CBV without allocating a 0x0 shadow (adapted from Marvisak#2449):
    • Updates descriptor::create_cbv to record View::Constant(0, 0) when desc is null instead of returning early and leaving a stale view active.
    • Guards address == 0 || size == 0 in HostHeap::make_twinned and State::tables before shadow::find / scratch_held / defer_constants so null CBVs flow cleanly into null_for + clear_twin without allocating a 4 MiB loose shadow at VA 0x0 or copying from a null pointer.
  3. Evict Memory::heaps entry on Heap::drop (adapted from Marvisak#2234):
    • Adds memory::release_heap(key: usize) matching Vice-Tay's HostHeap pointer key in Memory::heaps and calls it in Drop for Heap after releasing SHADER_HEAPS.
  4. Reclaim dynamic descriptor slots in queue::submit_split (adapted from fxceless#1320):
    • Calls crate::list::tables_done(&lists[..cut]) immediately after synchronous host.execute(&now) (matching submit_cut).
  5. Fix unaligned pipeline stream Bytecode read and guard truncated subobjects (adapted from Marvisak#2301):
    • 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.

(Note: object::add_ref_alive and resource::Held use the same placement and semantics as #257 so both PRs reconcile cleanly regardless of merge order.)

Verification

  • cargo test --locked -p victory-gpu --lib -- add_ref_alive_rejects_zero_refcount an_alias_source_is_held_after_the_title_releases_it alias_after_title_upload_copies_the_gpu_atlas safety_tests -- --test-threads=1 (7 passed, 0 failed — default and --features diagnostics, rebased on 877a40e).
    • Includes RAII-guarded GPU tests for an_alias_source_is_held_after_the_title_releases_it, null_cbv_make_at_does_not_allocate_shadow_and_clears_kept, and released_view_heap_evicts_memory_heap_entry, plus CPU tests for add_ref_alive/is_alive, null_cbv_clears_a_stale_view, unaligned_stream_yields_its_shaders, and truncated_shader_subobject_does_not_read_past_stream.
  • cargo clippy -p victory-gpu --all-targets --locked -- -D warnings (default + --all-features) and cargo test --locked -p xtask (11 passed, 0 failed).
  • Verified with synthetic unit and D3D12 host tests only (no title files locally).
**@⁠mint17** on GitHub · 2026-11-27 13:51 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/291) `mint17:fix/gpu-resource-lifetime-and-ub` → `main` · +395 −18 · 10 files · 1 commits · open Ports five missing upstream correctness and memory-safety fixes into `crates/victory-gpu`, adapted for Vice-Tay's architecture (zero merge conflict with #234, #245, or #272): ### Fixes Included 1. **Pin aliased target textures across `texture::alias` (adapted from `fxceless#1336`):** - Adds `object::add_ref_alive` / `is_alive` and `resource::Held` (`Drop` releasing via `object::release_boxed::<Resource>`) so `resource::alias_of` pins the target while `crate::texture::alias` copies from it (updating both `draw.rs` and `external.rs` `Recording::prepare_input`). - Splits non-owning `resource::has_alias` for boolean-only checks in `resource::hosted` (which holds `r.host.made`) and `texture/ahead.rs`, avoiding an `r.host.made -> TEXTURES` AB-BA deadlock and refcount churn. 2. **Clear stale slots on null CBV without allocating a 0x0 shadow (adapted from `Marvisak#2449`):** - Updates `descriptor::create_cbv` to record `View::Constant(0, 0)` when `desc` is null instead of returning early and leaving a stale view active. - Guards `address == 0 || size == 0` in `HostHeap::make_twinned` and `State::tables` before `shadow::find` / `scratch_held` / `defer_constants` so null CBVs flow cleanly into `null_for` + `clear_twin` without allocating a 4 MiB loose shadow at VA `0x0` or copying from a null pointer. 3. **Evict `Memory::heaps` entry on `Heap::drop` (adapted from `Marvisak#2234`):** - Adds `memory::release_heap(key: usize)` matching Vice-Tay's `HostHeap` pointer key in `Memory::heaps` and calls it in `Drop for Heap` after releasing `SHADER_HEAPS`. 4. **Reclaim dynamic descriptor slots in `queue::submit_split` (adapted from `fxceless#1320`):** - Calls `crate::list::tables_done(&lists[..cut])` immediately after synchronous `host.execute(&now)` (matching `submit_cut`). 5. **Fix unaligned pipeline stream `Bytecode` read and guard truncated subobjects (adapted from `Marvisak#2301`):** - 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`. *(Note: `object::add_ref_alive` and `resource::Held` use the same placement and semantics as #257 so both PRs reconcile cleanly regardless of merge order.)* ### Verification - `cargo test --locked -p victory-gpu --lib -- add_ref_alive_rejects_zero_refcount an_alias_source_is_held_after_the_title_releases_it alias_after_title_upload_copies_the_gpu_atlas safety_tests -- --test-threads=1` (7 passed, 0 failed — default and `--features diagnostics`, rebased on `877a40e`). - Includes RAII-guarded GPU tests for `an_alias_source_is_held_after_the_title_releases_it`, `null_cbv_make_at_does_not_allocate_shadow_and_clears_kept`, and `released_view_heap_evicts_memory_heap_entry`, plus CPU tests for `add_ref_alive`/`is_alive`, `null_cbv_clears_a_stale_view`, `unaligned_stream_yields_its_shaders`, and `truncated_shader_subobject_does_not_read_past_stream`. - `cargo clippy -p victory-gpu --all-targets --locked -- -D warnings` (default + `--all-features`) and `cargo test --locked -p xtask` (11 passed, 0 failed). - Verified with synthetic unit and D3D12 host tests only (no title files locally).
Port five missing upstream correctness and memory-safety fixes into
crates/victory-gpu, adapted for Vice-Tay's architecture:

- Pin aliased target textures across texture::alias (#1336): add
  object::add_ref_alive / is_alive and resource::Held, return Option<Held>
  from alias_of (updating both draw.rs and external.rs), and split
  lock-safe has_alias for boolean-only checks in resource::hosted and
  texture/ahead.rs (avoiding r.host.made -> TEXTURES AB-BA deadlock).
- Clear stale slots on null CBV (#2449): record View::Constant(0, 0) in
  create_cbv instead of returning early, and guard address == 0 || size == 0
  in HostHeap::make_twinned and State::tables so null CBVs flow cleanly
  to null_for + clear_twin without allocating a 4 MiB loose shadow at 0x0.
- Evict Memory::heaps entry on Heap::drop (#2234, Vice-Tay adapted): add
  memory::release_heap keyed by HostHeap pointer address and invoke it in
  Drop for Heap after releasing SHADER_HEAPS.
- Reclaim dynamic descriptor slots after host.execute in queue::submit_split
  (#1320) via crate::list::tables_done(&lists[..cut]).
- Fix unaligned pipeline stream Bytecode read and guard truncated stream
  subobjects in pipeline::create_stream (#2301) using read_unaligned() and
  checked_add / saturating_sub bounds checks.
- Add targeted unit and D3D12 safety tests for all five fixes.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 14:01:53 +00:00
Author
Owner

@⁠TaynanGT on GitHub · 2026-11-27 14:11 UTC · original

@⁠mint17 The targeted tests and four CI checks on 4718827 cover the five fixes well. The split between the owning alias lookup and the boolean check matters for lock ordering. I’ll carry that distinction through when reconciling the shared lifetime code with #257.

**@⁠TaynanGT** on GitHub · 2026-11-27 14:11 UTC · [original](https://github.com/TaynanGT/Vice-Tay/pull/291#issuecomment-7220456988) @⁠mint17 The targeted tests and four CI checks on `4718827` cover the five fixes well. The split between the owning alias lookup and the boolean check matters for lock ordering. I’ll carry that distinction through when reconciling the shared lifetime code with #257.
This pull request has changes conflicting with the target branch.
  • crates/victory-gpu/src/pipeline.rs
  • crates/victory-gpu/src/resource.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-291'
git switch 'github-bot/gh-291'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-291'
git switch 'github-bot/gh-291'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-291'
git switch 'main'
git merge --squash 'github-bot/gh-291'
git switch 'main'
git merge --ff-only 'github-bot/gh-291'
git switch 'main'
git merge 'github-bot/gh-291'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.