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

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

@⁠fuzeofficlal on GitHub · 2026-11-26 08:17 UTC · original
fuzeofficlal:fix/gpu-page-table-layout-retention → main · +501 −49 · 3 files · 1 commits · open

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.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 08:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/3106) `fuzeofficlal:fix/gpu-page-table-layout-retention` → `main` · +501 −49 · 3 files · 1 commits · open ### 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.
Bound command lists already keep the host descriptor heap and page-table
buffer alive across view-heap release, but memory::bind previously stored
only the raw heap key in defer_memory, defer_missed, and defer_after.

Releasing the heap dropped Shared and its Layout::copies while bound lists
were pending or executing, allowing referenced Shadow GPU buffers to drop
early, skipping dirty-copy refreshes in prepare, falsely marking pages as
missed (poisoning withholding for two frames) while dropping writebacks,
and aliasing a replacement heap created at the same key/HostHeap address.

Share the table's layout handle (Arc<Mutex<Arc<Layout>>>) through an opaque
memory::Binding held by the list's deferred steps and completion closures,
identify the live Shared entry by Arc::ptr_eq(&s.layout, &binding.layout)
in prepare with fallback to the retained layout, and initialize block 0 of
a freshly allocated page table to its null descriptor pair so even an
unprepared released table safely routes lookups to null.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:24:52 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 08:29 UTC · original · approved these changes

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).

**@⁠Dukestar69** on GitHub · 2026-11-26 08:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/3106#pullrequestreview-6070710193) · **approved these changes** 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).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 08:32 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-26 08:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/3106#issuecomment-7186713041) <!-- 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 can be merged automatically.
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/3106/head:github-bot/gh-3106'
git switch 'github-bot/gh-3106'

Merge

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