Skip to content

GPU/descriptor: invalidate HostHeap::copied cache on API writes, unmake, and raw zeroing - #807

Open
AscilCH wants to merge 1 commit into
fxceless:mainfrom
AscilCH:fix/gpu-descriptor-host-copied-cache-invalidation
Open

AscilCH wants to merge 1 commit into
fxceless:mainfrom
AscilCH:fix/gpu-descriptor-host-copied-cache-invalidation

Conversation

@AscilCH

@AscilCH AscilCH commented Nov 25, 2026

Copy link
Copy Markdown

Summary

Fixes two cache-invalidation bugs in HostHeap::mirror_copied (crates/victory-gpu/src/descriptor/host.rs), complementary to #792 (which addresses the mirror_copied TOCTOU re-read separately):

  1. Stale HostHeap::copied fingerprint across API writes and unmake():
    self.copied[index] was previously written only by mirror_copied() and never cleared when mirror() (called by Create*View / CopyDescriptors), texture_slot_gone(), or stale placed-texture unmake() modified or nulled a slot.

    • Sequence raw-copy A -> API write B -> raw-copy A (or raw-copy A -> unmake -> raw-copy A) matched *last == print(A) and skipped mirroring, leaving the host heap slot permanently stuck on B or NULL.
    • Fix: clear self.copied[index] via forget_copied() (always outside the self.made lock to avoid lock-order inversion) whenever mirror() or unmake() modifies or nulls a slot.
  2. Raw zeroing of an API-populated slot skipped:
    When a slot was populated through D3D12 API calls (made[index] != 0, copied[index] == 0) and later cleared by raw zeroing in guest memory, *last == 0 && all_zero returned false without checking made[index], leaving the stale host descriptor active and pinning its Kept GPU resource.

    • Fix: when *last == 0 && all_zero, check self.made[index] (outside the self.copied lock) before skipping.

Verification

  • Added synthetic unit test descriptor::tests::copied_fingerprint_invalidates_across_api_writes_unmake_and_zeroing covering:
    1. raw A -> API B -> raw A
    2. API A -> raw zeroing
    3. raw A -> texture_slot_gone unmake -> raw A
  • cargo test -p victory-gpu --lib --locked -- descriptor:: — 29 passed, 0 failed, 0 ignored.
  • cargo clippy -p victory-gpu --all-targets --all-features --locked — 0 warnings in crates/victory-gpu/src/descriptor/host.rs and crates/victory-gpu/src/descriptor.rs.
  • cargo xtask policy — 171 files ok.

(AI-assisted; synthetic unit tests only, no game data.)

…ke, and raw zeroing

Fix two cache-invalidation bugs in `HostHeap::mirror_copied` / `HostHeap::mirror` / `HostHeap::texture_slot_gone`
(`crates/victory-gpu/src/descriptor/host.rs`):

1. Stale `self.copied` fingerprint across API writes and `unmake`:
   `self.copied[index]` was updated only by `mirror_copied()` and never cleared
   when `mirror()` (via `Create*View`/`CopyDescriptors`), `texture_slot_gone()`,
   or stale placed-texture `unmake()` overwrote or nulled a slot. Copying
   record A -> API write B -> raw-copy A (or raw-copy A -> `unmake` -> raw-copy A)
   hit `*last == print` and left the host slot permanently stuck on B or NULL.
   Invalidate `self.copied[index]` (outside the `self.made` lock to preserve
   lock order) whenever `mirror()` or `unmake()` modifies or clears a slot.

2. Raw zeroing of an API-created slot skipped:
   When a slot was written via API (`made[index] != 0`, `copied[index] == 0`)
   and later cleared by raw zeroing, `*last == 0 && all_zero` returned `false`
   without checking `made[index]`, leaving the old host descriptor alive and
   pinning its `Kept` GPU resource. Check `made[index]` before skipping.

Add `descriptor::tests::copied_fingerprint_invalidates_across_api_writes_unmake_and_zeroing`.
@Filip722

Copy link
Copy Markdown

Independent Proton result on an RTX 5060 Laptop GPU (8 GB), NVIDIA 615.78.08, Bazzite 44, Proton Experimental 11.0-20261112, and vkd3d-proton 3.1.0. Windows MSVC release tests were cross-built with Rust 1.98.1.

I applied the same synthetic fenced D3D12 sampler fixture and #20's DXC support to parent 9376fd6 and PR head 0cee70d. The tested sequence was raw sampler A → API sampler B → raw sampler A. The GPU adapter was checked by LUID in both runs.

Sample Parent PR head
Restored sampler 17 (wrap B) 231 (clamp A)
Stable clamp control 231 231
Separate wrap control 61 61

The PR’s existing copied_fingerprint_invalidates_across_api_writes_unmake_and_zeroing test also passed under Proton. The GPU fixture adapts the sampler probe from #792 to this sequence. These runs used synthetic inputs and verify this descriptor behavior on Proton.

OpenAI Codex (GPT-6.2-Sol) adapted and ran the tests and drafted this report.

@MamaLeVrai

Copy link
Copy Markdown

Test report on real hardware: AMD RX 6950 XT (RDNA2), Windows 11, Rust 1.98.1, with a GPU present. I merged this PR's head 0cee70d into main 9376fd6 in a throwaway tree (no conflict). cargo clippy --workspace --all-targets --locked -- -D warnings fails, with the same two errors as main itself on this machine (function placed_filler_twins is never used, field depth_written is never read, victory-gpu): this PR adds none and removes none. cargo test --workspace --locked: the victory-gpu test binary dies with STATUS_ACCESS_VIOLATION (0xc0000005) every time: 2 of 2 full runs, then 3 of 3 runs of cargo test -p victory-gpu --lib, then once more with --test-threads=1. With --test-threads=1 the last test started is descriptor::tests::copied_fingerprint_invalidates_across_api_writes_unmake_and_zeroing, the test this PR adds, so the crash is in it or in the code it calls. main alone passes (540 passed, two runs). The title was not run, so this says nothing about in-game effect. No game data.

AI-assisted (Claude Code); no game data.

@MamaLeVrai

Copy link
Copy Markdown

Follow-up to my test report above, with where the crash is and what I checked (RX 6950 XT, Windows 11, this PR's head 0cee70d merged into main 9376fd6).

Where. The crash is deterministic: descriptor::tests::copied_fingerprint_invalidates_across_api_writes_unmake_and_zeroing dies with STATUS_ACCESS_VIOLATION even when run alone. I put progress markers in the test: scenarios A and B pass, and the process dies inside host.texture_slot_gone(7) in scenario C (the markers before and after that call: only the first prints).

Why, as far as I can tell. The heap in this test is a sampler heap (kind: SAMPLERS). texture_slot_gone is for view heaps: it calls unmake, which reads the slot's made key as a view key (view_at(key as u32, (key >> 32) as u32)) and writes a null view descriptor at self.cpu(index). For a sampler slot the key is a sampler's, and the CPU handle belongs to a sampler heap, so a view descriptor is written there.

What I checked. With a local guard if self.sampler { return false; } at the top of texture_slot_gone, the crash disappears and the test fails cleanly at assert!(host.texture_slot_gone(7)) (line 1334), because the call now returns false for a sampler heap. So the crash is that call on that heap.

Two ways out. Run scenario C against a CBV_SRV_UAV heap with a view made in slot 7 (which is what texture_slot_gone is for), or keep scenarios A and B on the sampler heap and drop C from this test. A guard like the one above in texture_slot_gone is also worth considering, since calling it on a sampler heap writes the wrong kind of descriptor; that part is your call, I did not test it beyond the run described.

Nothing here touches the title.

AI-assisted (Claude Code); no game data.

@LeonardoS11

Copy link
Copy Markdown

A note for the rebase onto a253706, found while carrying this PR on a branch of ours. a253706 replaced the heap sync with mirror_copied_page (one lock a page), and on that path this PR's fix does not reach: a slot made through the API and then zeroed as bytes keeps its old descriptor, and this PR's own test (copied_fingerprint_invalidates…) fails once the PR is applied on a253706 unchanged.

What made it pass here: the page pass collects the slots it found zeroed and checks them against made after it has let go of copied, so the order of the two locks is not inverted. It is 27 lines in descriptor/host.rs, commit 9c8a7b7 in LeonardoS11/victory-fork (branch port/v7), in case you want to take it or compare. With it cargo test --workspace on that branch is 579 passed and 1 failed, the failure being the indirect budget test that is flaky on main (#847, #849).

(Found and fixed on our branch with Claude Code; not run in the game on its own.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants