GPU: a copy-back still pending when the title takes the range back writes nothing #3036
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!3036
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/3036/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@HoLa-Lee on GitHub · 2026-11-26 04:19 UTC · original
HoLa-Lee:gpu/no-copy-back-after-release→main· +75 −2 · 1 files · 1 commits · closed without mergingWhat this fixes
FIND-14 in the coordination thread:
watch.rsdocuments that "memory freed andallocated again while a tracker holds it is not seen", and nothing connected that
with
Shadow's pending snapshot. A copy holds the address it was registered withand writes back through it, so a title range freed and re-used between a
submission and its copy-back can have that submission's shader bytes written into
whatever took the addresses.
Reproduced with no game and no AMD card, in the test this PR adds: register a
page of title memory, take the submission's snapshot, let a shader write one byte
into the copy,
unregisterandVirtualFreethe range, take the same addressesagain with
VirtualAllocfilled with0xEE, then run the copy-back. Before thechange:
The change
Shadowcarries a flag thatunregistersets;copy_back_or_notreturns beforetouching anything once it is set. A copy whose title buffer is gone writes
nothing back, and stays alive for the lists that still hold it - its own bytes
are the lists' to use, the title's memory is not.
The 2 are the FIND-9 view-heap pair on this NVIDIA host, which is #2937's.
What this does not claim
#2025's intermittent corruption was AMD and intermittent. This is one way
stale shader bytes can land in re-used title memory, deterministically and on any
host; it is not proof that it is that report's mechanism. What it gives #2025 is
a reproduction of the class, with no game - which is what FIND-14 asked for
("a synthetic concurrency test exercising remap-during-pending-copy-back if
someone wants to pursue it without the game").
Note on the test's setup
It only observes the hazard when the allocator hands the same address back
(
q == p); when it does not, the test frees the new allocation, prints that itwas not observed and returns, rather than writing through an address that belongs
to nobody - which would be the same bug in the test. On this machine the same
address comes back every run.
FIND-14 in the coordination thread: `watch.rs` documents that "memory freed and allocated again while a tracker holds it is not seen", and nothing connects that with `Shadow`'s pending snapshot - the copy holds the address it was registered with and writes back through it. So a title range freed and re-used between a submission and its copy-back can have that submission's shader bytes written into whatever took the addresses. Reproduced with no game and no AMD card, in the test this commit adds: register a page of title memory, take the submission's snapshot, let a shader write one byte into the copy, `unregister` and `VirtualFree` the range, take the same addresses again with `VirtualAlloc` and fill them with 0xEE, then run the copy-back. Before the change that byte is 0x99 - the shader's - inside the title's new allocation: left: 153 (0x99: what the shader wrote for a submission that is over) right: 238 (0xEE: what the title's next use of the address held) `Shadow` now carries a flag that `unregister` sets, and `copy_back_or_not` returns before touching anything once it is set: a copy whose title buffer is gone writes nothing back, and stays alive for the lists that still hold it - its own bytes are the lists' to use, the title's memory is not. cargo test -p victory-gpu --lib --profile diag --features diagnostics 245 passed, 2 failed, 1 ignored (the 2 are the FIND-9 view-heap pair, which is #2937's, not this commit's) cargo clippy -p victory-gpu --all-features -- -D warnings: clean Whether this is the mechanism behind #2025's intermittent corruption is not claimed: it is one way stale shader bytes reach re-used title memory, and #2025 was AMD and intermittent. What it gives #2025 is a reproduction of the class on any host, with no game.@pickles976 on GitHub · 2026-11-26 04:24 UTC · original
Verified ordinary path; remaining stale write through marked-page path. Exact runtime head
0014e65fa78ea59d9418da3b4f89d4d7937616a7, RTX 4060 Laptop GPU 8 GiB / 16 GiB RAM, Linux + Wine 11.0 / vkd3d-proton 3.2.0 / NVIDIA 590.48.01.Local test-only changes: require host buffer/mark allocation (no GPU skip), request the exact freed address with
VirtualAlloc(p, ...)and assert it was returned, then exercise both the original ordinary copy-back and a marked-page variant. No production source changes. Fresh Windows-MSVC test build from this checkout; one test thread; actual NVIDIA adapter enumerated. Result:The marked case uses the actual
copy_back_marks(&[], false)dispatcher, not just a direct call to the leaf function:pool(1)/register/find(required host allocation) /copy.update(p, PAGE).copy.writing(p, 64, true); guest bytep+32 = 0x44to fault/save the pre-write page used by marked merging.base = copy.marks.unwrap(); set that page bit inbitmap(false)using the same fixture technique asmarks_of_shaders_using_title_memory_are_held_back.copy.write_host(p+24, &[0x99])to simulate the shader's changed byte.unregister(p, p); assertVirtualFree(p, 0, MEM_RELEASE)succeeds.q = VirtualAlloc(p, PAGE, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE); assertq == p; fill the entire new allocation with0xEE.copy_back_marks(&[], false); byte24 becomes 0x99, not the required 0xEE. The staleArc<Shadow>remains alive, as a pending list may hold it.Root: the new
gonegate is only incopy_back_or_not. The live weak-owner registry can still upgrade this shadow incopy_back_marks, which callscopy_back_marked; that function never checksgone. The normal host submission calls marked copy-back before ordinary copy-back, so the latter returning zero cannot protect the newly reused memory. Relevant head locations:shadow.rs:503-544, 938-968;host.rs:1389-1405.Suggested fix: include marked-page writes in the same retirement/lifetime protocol; add this marked remap test alongside the existing ordinary one. A separate source-review concern, not concurrency-tested here: a single pre-write atomic check does not serialize an unregister/remap that happens after that check. Retirement needs a shared exclusion/generation protocol covering validation through the guest write, with consistent lock ordering; switching Relaxed to SeqCst alone would not close that interleaving. Preserve snapshot/pending cleanup when suppressing writes too (the new early return bypasses it until shadow destruction).
Command through local Wine runner:
cargo test --locked --target x86_64-pc-windows-msvc -p victory-gpu --lib a_pending_copy_back -- --nocapture --test-threads=1. Local logverify-pr3036-wine-retired-copyback.log; the marked variant isa_pending_copy_back_marked_after_reusein localVIctory-pr3036/. These are real host buffers and simulated shader writes/mark bits, not GPU shader execution, an AMD reproduction, or a gameplay result. No title assets used. AI-assisted verification and report: OpenAI Codex.@pawrenity on GitHub · 2026-11-26 04:41 UTC · original
FIND-14: side-by-side check of #3036 and #2612
Hardware: NVIDIA GeForce RTX 4070, Windows 11 (native D3D12, MSVC). Base:
origin/main63bcca4, which is the merge base of both PRs. All test runs below used--locked --libonvictory-gpu, with every targeted test run alone under--test-threads=1. No title data was used.In the cross-tests I added
x_tests to theshadow.rstest module. Their "no host GPU" early returns (and #3036's "address not reused" return) panic instead of returning, so a pass always means the assertion actually ran. The tests:unregister,VirtualFree,VirtualAllocof the same address filled with0xEE,copy_back.copy_back_marks.copy_back_page(the computed-address path).shadowtests that build without its new APIs:remapping_while_copy_back_is_pending_keeps_new_title_bytesandremapping_after_title_write_discards_saved_bytes_for_marked_copy_back. They report the change throughtitle_memory_remapped. #2612's other four tests depend on APIs that only #2612 adds (was_remapped,title_memory_remapping, the pre-write hook), so they cannot be run against main or #3036.0014e658a998fcvictory-gpulib0x99, expected0xEEwatch::write_backfails all 3 pause-and-remap tests0x99)0x99)0x99)copy_back_page)0x99)0x99)0x00, expected0x99)-D warnings, all targets and featuresvictory-gpu)victory-gpu,victory-hle).victory-hle xmemtests: 14 passorigin/mainshadow.rs#3036: defects
copy_back_or_notchecksgone(shadow.rs:418). The marked path (copy_back_marked,shadow.rs:503, reached throughcopy_back_marks) andcopy_back_page(shadow.rs:208) still write into the reused memory (R2 and R3). This confirms the marked-path report already posted on this PR, now on native Windows.unregister), not on the memory. A remap under a buffer that is still registered is not covered (M1/M2 fail). Remaps come fromXMemMapPhysicalPages,map_aliasandtitle_memory_remapped. The fix also has no notion of a partial range: one release suppresses the whole copy.0x99, #3036 leaves0x00). Main's comment onunregistersays lists that still hold the copy keep it alive. I have not verified whether titles release buffers this way while a submission is pending.goneis loaded once,Relaxed, before the copy-back starts. Anunregisterfollowed by a free after that check, but beforewrite_mapped, is not excluded. The early return also skipspending.remove,give_backof the snapshot anddisarm(shadow.rs:479onward) until theShadowdrops.#2612: defects and gaps
title_memory_remapped. A free followed by reuse that nobody reports gets no protection on any path (R1, R2 and R3 fail). (From reading the code only):XMemFreeDefault(xmem.rs:334) andXMemVirtualAlloc(xmem.rs:87) do not report, so this applies to anXMemFreefollowed by a new allocation at the same address. This is the same gap already raised forXMemVirtualAllocon this PR.copy_back_or_notinlines a copy ofmerge_writeback(shadow.rs:457). It is equivalent, butwriteback_merge_matches_bytewise_oracleno longer covers it, which the maintainer triage already asked to revert.write_backandTracked::takenlockMAPPINGwithout re-entry, so neither may be reached while aRemappingguard is held. I found no such path, but the invariant is not documented.MAPPING, which the remap paths also take, and the negative control above shows the three pause-and-remap tests depend on that recheck.Recommendation
On this evidence, #2612 is the stronger base. It covers all three copy-back paths for remaps that are reported, handles partial ranges, and serialises the write against the remap. #3036 covers only the ordinary path, misses remaps, and drops legitimate writes when a buffer object is released while its memory stays live (K). Neither PR closes a free followed by reuse that is never reported (R2 and R3 fail on both). The narrowest way to close it seems to be reporting frees and new allocations from
XMemFree/XMemVirtualAllocthroughtitle_memory_remapped, which would put them on #2612's existing gate. That suggestion is untested. I would not merge #3036 as it stands. Its test (R1, with the early returns turned into panics) is useful and could be added to #2612 together with such a report.AI-assisted (Claude Code).
@Machad3x on GitHub · 2026-11-26 05:58 UTC · original
One way to close the K case (release without free) and the marked-path report above, tried in another tree (Windows 11, RTX 3080 Ti,
cargo test -p victory-gpu --libpasses, 329 tests, with and without GPU upload heaps).gonewhere memory is freed, not inunregister:Memory'sDrop(committed resources and heaps) and the upload block'sDropbeforeVirtualFreecallfreed(start, len). Releasing a placed or title-mapped resource then still writes back.unregisterkeeps aWeakof the released copy in a second map, so a heap freed after its placed resources' objects still finds their copies.freedalso marks the single-view copies in the range (EXACT) and removes them, so the next buffer at those addresses does not pick up a quiet copy.copy_back_or_notaskeep_writeswhen set, so pending ranges and snapshots are still released.Tests there: a pending ordinary copy-back after free and reuse writes nothing; a marked page writes nothing (without the check it wrote 65536 bytes into the freed page); a release whose memory stays still writes back; a view copy of freed bytes is marked and the next view gets a new copy.
Not covered: a remap under a still registered buffer (M1/M2 above), memory the title unmaps itself, a copy-back already under way when the memory is freed, and there is no test that goes through a real placed-resource heap release. The tree differs from this branch (its marked path is organised differently), so this is a description rather than a patch. No title data in the tests.
AI assistance: written and tested with Claude Code.
@Marvisak on GitHub · 2026-11-26 06:13 UTC · original
Automated triage for the maintainers.
The test is a useful reproduction of FIND-14, but the fix in
crates/victory-gpu/src/shadow.rsis incomplete, and it changes behaviour thatmainrelies on:goneflag is checked only inShadow::copy_back_or_not.Shadow::copy_back_marked, which is reached throughcopy_back_marks, andShadow::copy_back_pagestill write through the old address. The marked path runs before the ordinary copy-back on a host submission, so a reused page can still get the stale bytes. The two verification comments above reproduce this.copy_back_or_not. That cleanup doespending.remove,give_backof the snapshot segments andTracked::disarm. Snapshots stay held until theShadowdrops. If the copy-back must be suppressed, it should still go through thekeep_writespath (back = false) so this state is released.unregisternow setsgonewhen the buffer object is released, not when its memory is freed. Onmain,unregisterdocuments that "lists that still hold its copy keep it alive". With this change, a release whose memory stays mapped (for example a placed or aliased resource) loses that submission's shader writes.shadow.rs. Please coordinate with it. One option is to move this PR's test there, together with a report of frees and reallocations.What would change the verdict: set the flag where memory is actually freed, or route it through
title_memory_remapped; gate the marked and per-page paths too; keep the pending and snapshot cleanup; add the marked-path variant of the test.Generated by Claude Code
@HoLa-Lee on GitHub · 2026-11-26 06:19 UTC · original
Closing in favour of #2612, and all three review points are right.
gonecovers one of three paths.copy_back_marked(throughcopy_back_marks) andcopy_back_pagewrite through the old address too, and the marked path runs first on a host submission - @pickles976 reproduced exactly that (marked=true), and it is the same hole.pending.remove, the snapshot'sgive_back,Tracked::disarm), so snapshots stay held until theShadowdrops. Suppression has to go through theback = falsepath, not around it.unregisteris the wrong hook. It fires when the buffer object is released, andmaindocuments that "lists that still hold its copy keep it alive" - so a placed or aliased resource whose memory stays mapped loses that submission's shader writes. The flag belongs where memory is actually freed.#2612 does the first and third at the right place (the runtime's
title_memory_remapped), which is why it should land and this should not: two fixes for one finding, conflicting inshadow.rs, is worse than one.What this PR has that #2612 does not, and I have handed over rather than dropped: the reproduction. On #2612's own head
4f47bdcit fails -title_memory_remapped. That is the gap the triage calls "keep the pending and snapshot cleanup ... route it throughtitle_memory_remapped" and @Machad3x calls the K case; the test is now offered on #2612 with the exact bytes and the trigger, and its author can take it as-is or have it as a separate test-only PR againstmain.Thanks to @pickles976, @pawrenity, @Machad3x and the triage for three independent reproductions of the same hole - the honest outcome is one fix, and #2612 is it.
Pull request closed