GPU: retain page-table layout and copies for lists bound to a released view heap #3104

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/3106 into main
fuzeofficlal commented 2026-11-26 08:17:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T08:17:00Z.


Problem

Command lists that use title memory already keep the host ID3D12DescriptorHeap (draw::State::bind_heaps) and the page-table ID3D12Resource (memory::bind) alive across descriptor-heap release. However, memory::bind only stored the raw heap key: usize in Step::Memory, defer_missed, and defer_after, and completion callbacks looked up the table's Layout in SHARED by key after execution.

When a title releases a view heap (descriptor::Heap::drop -> memory::release(key)) while bound command lists are still pending or executing on the GPU:

  1. GPU UAF on retired shadow buffers: Dropping Shared immediately drops Shared.layout.copies: Vec<Arc<Shadow>>. If a buffer named in the retained page table was also unregistered, its ID3D12Resource can be freed while the GPU still accesses it through the retained page table and descriptor heap.
  2. Dropped writebacks and 2-frame withholding() poisoning: defer_missed calls layout(key) and gets an empty Layout::default(), causing has_missed to return true for every marked page (setting MISSED_IN and withholding all title-memory writes for the current and next frame), while defer_after drops all written pages and counts them as missed regions.
  3. Skipped dirty-copy refreshes: prepare(key) sees with_heap(key) return None and skips refreshing dirty copies still named in the retained page table, so shaders read stale host-copy contents.
  4. Cross-heap ABA corruption on heap address reuse: Because descriptor::Heap stores host: OnceLock<Option<HostHeap>> inline inside Box<Heap>, allocating a new heap at the released Box<Heap> address reuses both key and the HostHeap pointer (s.host). An old bound list's prepare can rewrite the new heap's page table, and its defer_missed/defer_after decode its dirty bitmap against the new heap's Layout—silently dropping writebacks or targeting the wrong copy/page.
  5. Unprepared released table routing reads to descriptor slot 0: Shared::new left the page-table buffer zero-filled before the first prepare, so if its heap was released before any submission prepared it, lookups evaluated e.x == 0 and indexed descriptor slot 0 instead of self.null.

Fix

  • Wrap Shared.layout in Arc<Mutex<Arc<Layout>>> and pass an opaque memory::Binding (carrying key and a clone of that shared handle) to HostList::defer_memory, defer_missed, and defer_after.
  • In memory::prepare, identify the live Shared entry with *k == binding.key && s.host == host && Arc::ptr_eq(&s.layout, &binding.layout). Because the live Binding keeps the Arc allocation alive, a new Shared::new at the same key and inline HostHeap address can never alias it. When the heap has been released or replaced, prepare falls back to binding.layout so dirty copies in the retained table are still refreshed.
  • Initialize level-1 (all zeros -> block 0) and block 0 ((null, 0) entries) in Shared::new so even a never-prepared released table routes every lookup to the null descriptor pair, matching Layout::default().
  • Serialize host::withholding::only_programs_using_title_memory_are_withheld with memory::tests::gpu_test_lock() so parallel test execution does not race on global MISSED_IN.

Verification

  • Added three GPU regression tests in crates/victory-gpu/src/memory.rs (all 3 fail on 63bcca43 and pass with the fix):
    • memory::tests::a_released_view_heap_keeps_its_layout_and_copies_for_bound_lists
    • memory::tests::a_new_heap_at_a_released_key_does_not_steal_a_bound_lists_layout
    • memory::tests::an_unprepared_released_heap_routes_reads_to_null_and_marks_a_miss
  • VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --locked --features diagnostics: 249 passed, 0 failed (verified across 5 consecutive parallel runs).
  • Hardware D3D12 (RTX 5090): all 12 targeted memory / descriptor / withholding tests passed.
  • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings: 0 warnings.
  • cargo test -p xtask --locked policy::tests::repository_passes_policy: passed.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6622413171 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3106). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T08:17:00Z. --- ### Problem Command lists that use title memory already keep the host `ID3D12DescriptorHeap` (`draw::State::bind_heaps`) and the page-table `ID3D12Resource` (`memory::bind`) alive across descriptor-heap release. However, `memory::bind` only stored the raw heap `key: usize` in `Step::Memory`, `defer_missed`, and `defer_after`, and completion callbacks looked up the table's `Layout` in `SHARED` by `key` after execution. When a title releases a view heap (`descriptor::Heap::drop` -> `memory::release(key)`) while bound command lists are still pending or executing on the GPU: 1. **GPU UAF on retired shadow buffers**: Dropping `Shared` immediately drops `Shared.layout.copies: Vec<Arc<Shadow>>`. If a buffer named in the retained page table was also unregistered, its `ID3D12Resource` can be freed while the GPU still accesses it through the retained page table and descriptor heap. 2. **Dropped writebacks and 2-frame `withholding()` poisoning**: `defer_missed` calls `layout(key)` and gets an empty `Layout::default()`, causing `has_missed` to return `true` for every marked page (setting `MISSED_IN` and withholding all title-memory writes for the current and next frame), while `defer_after` drops all written pages and counts them as missed regions. 3. **Skipped dirty-copy refreshes**: `prepare(key)` sees `with_heap(key)` return `None` and skips refreshing dirty copies still named in the retained page table, so shaders read stale host-copy contents. 4. **Cross-heap ABA corruption on heap address reuse**: Because `descriptor::Heap` stores `host: OnceLock<Option<HostHeap>>` inline inside `Box<Heap>`, allocating a new heap at the released `Box<Heap>` address reuses both `key` and the `HostHeap` pointer (`s.host`). An old bound list's `prepare` can rewrite the new heap's page table, and its `defer_missed`/`defer_after` decode its dirty bitmap against the new heap's `Layout`—silently dropping writebacks or targeting the wrong copy/page. 5. **Unprepared released table routing reads to descriptor slot 0**: `Shared::new` left the page-table buffer zero-filled before the first `prepare`, so if its heap was released before any submission prepared it, lookups evaluated `e.x == 0` and indexed descriptor slot 0 instead of `self.null`. ### Fix - Wrap `Shared.layout` in `Arc<Mutex<Arc<Layout>>>` and pass an opaque `memory::Binding` (carrying `key` and a clone of that shared handle) to `HostList::defer_memory`, `defer_missed`, and `defer_after`. - In `memory::prepare`, identify the live `Shared` entry with `*k == binding.key && s.host == host && Arc::ptr_eq(&s.layout, &binding.layout)`. Because the live `Binding` keeps the `Arc` allocation alive, a new `Shared::new` at the same `key` and inline `HostHeap` address can never alias it. When the heap has been released or replaced, `prepare` falls back to `binding.layout` so dirty copies in the retained table are still refreshed. - Initialize level-1 (all zeros -> block 0) and block 0 (`(null, 0)` entries) in `Shared::new` so even a never-prepared released table routes every lookup to the null descriptor pair, matching `Layout::default()`. - Serialize `host::withholding::only_programs_using_title_memory_are_withheld` with `memory::tests::gpu_test_lock()` so parallel test execution does not race on global `MISSED_IN`. ### Verification - Added three GPU regression tests in `crates/victory-gpu/src/memory.rs` (all 3 fail on `63bcca43` and pass with the fix): - `memory::tests::a_released_view_heap_keeps_its_layout_and_copies_for_bound_lists` - `memory::tests::a_new_heap_at_a_released_key_does_not_steal_a_bound_lists_layout` - `memory::tests::an_unprepared_released_heap_routes_reads_to_null_and_marks_a_miss` - `VICTORY_D3D12_WARP=1 cargo test -p victory-gpu --locked --features diagnostics`: 249 passed, 0 failed (verified across 5 consecutive parallel runs). - Hardware D3D12 (RTX 5090): all 12 targeted `memory` / `descriptor` / `withholding` tests passed. - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings`: 0 warnings. - `cargo test -p xtask --locked policy::tests::repository_passes_policy`: passed.
Dukestar69 commented 2026-11-26 09:05:55 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T08:29:34Z.

Review: APPROVED


Verified on RTX 5090 & WARP against origin/main (63bcca4):

  • cargo test -p victory-gpu --lib memory:: -- --test-threads=1: 10/11 passed on RTX 5090 and WARP (all 3 new released-view-heap tests pass: a_new_heap_at_a_released_key_does_not_steal_a_bound_lists_layout, a_released_view_heap_keeps_its_layout_and_copies_for_bound_lists, an_unprepared_released_heap_routes_reads_to_null_and_marks_a_miss; the only failure is pre-existing #1744 lists_share_a_table_and_copies_keep_their_views).
  • cargo test -p victory-gpu --lib draw:: -- --test-threads=1: 20/20 passed on RTX 5090.
  • cargo clippy -p victory-gpu --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly retains page-table layout and copies for command lists still bound to a released view heap and prevents a new heap at a reused key from stealing them.

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

<!-- greyfinch-sync:Marvisak/VIctory:review:6070710193 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3106#pullrequestreview-6070710193). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T08:29:34Z. **Review:** APPROVED --- Verified on RTX 5090 & WARP against `origin/main` (`63bcca4`): - `cargo test -p victory-gpu --lib memory:: -- --test-threads=1`: 10/11 passed on RTX 5090 and WARP (all 3 new released-view-heap tests pass: `a_new_heap_at_a_released_key_does_not_steal_a_bound_lists_layout`, `a_released_view_heap_keeps_its_layout_and_copies_for_bound_lists`, `an_unprepared_released_heap_routes_reads_to_null_and_marks_a_miss`; the only failure is pre-existing `#1744` `lists_share_a_table_and_copies_keep_their_views`). - `cargo test -p victory-gpu --lib draw:: -- --test-threads=1`: 20/20 passed on RTX 5090. - `cargo clippy -p victory-gpu --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly retains page-table layout and copies for command lists still bound to a released view heap and prevents a new heap at a reused key from stealing them. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Marvisak commented 2026-11-26 09:06:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T08:32:13Z.


Automated triage for the maintainers.

Verdict: needs work. CI check passes.

This PR changes crates/victory-gpu/src/memory.rs so a list holds a Binding (key plus a shared Arc<Mutex<Arc<Layout>>>) in place of the bare heap key. prepare, defer_missed and defer_after then keep using the table's layout after memory::release. I checked it against main: layout(key) does return Layout::default() once the SHARED entry is gone, so a list submitted after its heap was released loses its refreshes and copy-backs and counts every mark as a miss. The diagnosis reads as correct.

What it needs:

  • Evidence that the title does this. The PR is about 500 lines of lifetime handling. It also keeps copies alive and writes them back to title memory after shadow::unregister, and your first test asserts title() == 0x33 after the buffer was released. All the scenarios in the description are synthetic. Please show a run log where the title releases a view heap while a list bound to it is still unsubmitted or executing (for example, a debug line in release when a bound list exists). That would show the path is real and that writing back into released buffers is wanted.
  • Split out the independent fix. The null initialisation in Shared::new (level 1 to block 0, block 0 to (null, 0)) is small and stands on its own. As a separate PR it could be reviewed quickly.
  • The gpu_test_lock change to host::withholding::only_programs_using_title_memory_are_withheld is a separate test fix as well.

With that evidence, or with the PR narrowed to the parts the evidence supports, this would be reviewed for merge.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7186713041 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3106#issuecomment-7186713041). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T08:32:13Z. --- <!-- victory-triage v1 head=a81d5dd4aec5b41a164f083e36961e85e227d58d verdict=needs-work at=2026-11-26T08:31:06Z --> Automated triage for the maintainers. **Verdict: needs work.** CI `check` passes. This PR changes `crates/victory-gpu/src/memory.rs` so a list holds a `Binding` (key plus a shared `Arc<Mutex<Arc<Layout>>>`) in place of the bare heap key. `prepare`, `defer_missed` and `defer_after` then keep using the table's layout after `memory::release`. I checked it against main: `layout(key)` does return `Layout::default()` once the `SHARED` entry is gone, so a list submitted after its heap was released loses its refreshes and copy-backs and counts every mark as a miss. The diagnosis reads as correct. What it needs: - **Evidence that the title does this.** The PR is about 500 lines of lifetime handling. It also keeps copies alive and writes them back to title memory after `shadow::unregister`, and your first test asserts `title() == 0x33` after the buffer was released. All the scenarios in the description are synthetic. Please show a run log where the title releases a view heap while a list bound to it is still unsubmitted or executing (for example, a debug line in `release` when a bound list exists). That would show the path is real and that writing back into released buffers is wanted. - **Split out the independent fix.** The null initialisation in `Shared::new` (level 1 to block 0, block 0 to `(null, 0)`) is small and stands on its own. As a separate PR it could be reviewed quickly. - The `gpu_test_lock` change to `host::withholding::only_programs_using_title_memory_are_withheld` is a separate test fix as well. With that evidence, or with the PR narrowed to the parts the evidence supports, this would be reviewed for merge. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3106:upstream-pr/3106'
git switch 'upstream-pr/3106'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3106'
git switch 'upstream-pr/3106'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3106'
git switch 'upstream-pr/3106'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3106'
git switch 'main'
git merge --squash 'upstream-pr/3106'
git switch 'main'
git merge --ff-only 'upstream-pr/3106'
git switch 'main'
git merge 'upstream-pr/3106'
git push origin 'main'
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!3104
No description provided.