victory-gpu(shadow): flush SNAPS before dropping Shadow to prevent use-after-free #935

Open
faycalbaklouti wants to merge 2 commits from upstream-pr/936 into main
faycalbaklouti commented 2026-11-25 13:14:33 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by faycalbaklouti on 2026-11-25T13:14:33Z.


Target upstream: fxceless/victory-fork (note: SNAPS / VICTORY_WIDE_BACK is specific to fxceless/victory-fork and does not exist in Marvisak/VIctory or Belobug/VIctory-Belobug-AMD)

Problem

When VICTORY_WIDE_BACK=1 is enabled, Shadow::writing queues raw pointer tuples (data_ptr, snapshot_ptr, len) into the global SNAPS queue for deferred parallel copying in take_snapshots(). If a Shadow is dropped while SNAPS still holds pointers into its backing mapping or pooled snapshot buffer, the subsequent take_snapshots() call reads/writes freed memory (use-after-free).

Solution

  • In impl Drop for Shadow, flush any queued entries in SNAPS via take_snapshots() before releasing the Shadow's resources.
  • Add unit test take_snapshots_flushes_queued_snaps_before_release.

Verification

  • git apply --check passes cleanly on fxceless/victory-fork main.

About our fork & censorship-resistant preservation effort:
This fix comes from gta6-vi-port, our open-source Linux (GE-Proton) + NVIDIA RTX 4090 fork of VIctory and peer-to-peer Radicle archival effort (mirroring 92+ community porting repositories across public Radicle seeds so open-source porting research survives centralized takedowns; open-source code only, no game files/keys/binaries).

<!-- greyfinch-sync:fxceless/victory-fork:issue:5492036022 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/936). Originally posted by [faycalbaklouti](https://github.com/faycalbaklouti) on 2026-11-25T13:14:33Z. --- **Target upstream:** `fxceless/victory-fork` (note: `SNAPS` / `VICTORY_WIDE_BACK` is specific to `fxceless/victory-fork` and does not exist in `Marvisak/VIctory` or `Belobug/VIctory-Belobug-AMD`) ## Problem When `VICTORY_WIDE_BACK=1` is enabled, `Shadow::writing` queues raw pointer tuples `(data_ptr, snapshot_ptr, len)` into the global `SNAPS` queue for deferred parallel copying in `take_snapshots()`. If a `Shadow` is dropped while `SNAPS` still holds pointers into its backing mapping or pooled snapshot buffer, the subsequent `take_snapshots()` call reads/writes freed memory (use-after-free). ## Solution - In `impl Drop for Shadow`, flush any queued entries in `SNAPS` via `take_snapshots()` before releasing the `Shadow`'s resources. - Add unit test `take_snapshots_flushes_queued_snaps_before_release`. ## Verification - `git apply --check` passes cleanly on `fxceless/victory-fork` `main`. --- **About our fork & censorship-resistant preservation effort:** This fix comes from **[gta6-vi-port](https://github.com/go2afri/gta6-vi-port)**, our open-source Linux (GE-Proton) + NVIDIA RTX 4090 fork of VIctory and peer-to-peer Radicle archival effort (mirroring 92+ community porting repositories across public Radicle seeds so open-source porting research survives centralized takedowns; open-source code only, no game files/keys/binaries). - **Canonical Radicle repo:** https://radicle.network/nodes/rosa.radicle.network/rad:z39ofPhQC1siudH9BF3xsmAMmUqxc (`rad:z39ofPhQC1siudH9BF3xsmAMmUqxc`) - **Live 1080p development stream:** https://vdo.ninja/?view=gta6rtx4090live1080p60 (Tor v3 `.onion` VP9+Opus viewer: `http://kwnoaeecpmxd6eetqfa7fxlymvxnh5du2i6wxr44t4mc7cgch5pw23id.onion/`)
tikD1337 commented 2026-11-25 14:10:52 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by tikD1337 on 2026-11-25T13:50:50Z.


Thanks for the fix. On main @ a253706 (Windows 11, RTX 4070 Ti SUPER, Rust 1.98.1), the library builds, but the test target does not:

error[E0425]: cannot find function `pooled` in this scope
   --> crates\victory-gpu\src\shadow.rs:1936:23
error[E0425]: cannot find value `SNAPS` in this scope
error[E0425]: cannot find function `take_snapshots` in this scope

The new test sits in mod merge_tests, which only imports PIECE, back_piece, changed_runs. One line fixes it:

use super::{give_back, pooled, take_snapshots, SNAPS};

With that line: take_snapshots_flushes_queued_snaps_before_release passes, and cargo test --release -p victory-gpu --lib -- --test-threads=1 gives 170 passed, 1 failed. The failure is the known flaky indirect::tests::warp_budget_saturates_without_wraparound_and_preserves_draw_fields that #849 fixes.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7164376122 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/936#issuecomment-7164376122). Originally posted by [tikD1337](https://github.com/tikD1337) on 2026-11-25T13:50:50Z. --- Thanks for the fix. On `main` @ `a253706` (Windows 11, RTX 4070 Ti SUPER, Rust 1.98.1), the library builds, but the test target does not: ``` error[E0425]: cannot find function `pooled` in this scope --> crates\victory-gpu\src\shadow.rs:1936:23 error[E0425]: cannot find value `SNAPS` in this scope error[E0425]: cannot find function `take_snapshots` in this scope ``` The new test sits in `mod merge_tests`, which only imports `PIECE, back_piece, changed_runs`. One line fixes it: ```rust use super::{give_back, pooled, take_snapshots, SNAPS}; ``` With that line: `take_snapshots_flushes_queued_snaps_before_release` passes, and `cargo test --release -p victory-gpu --lib -- --test-threads=1` gives 170 passed, 1 failed. The failure is the known flaky `indirect::tests::warp_budget_saturates_without_wraparound_and_preserves_draw_fields` that #849 fixes.
faycalbaklouti commented 2026-11-25 15:08:31 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by faycalbaklouti on 2026-11-25T14:18:11Z.


Good catch, thanks for testing it! Added use super::{give_back, pooled, take_snapshots, SNAPS}; to mod merge_tests in c00ac40 and verified the victory-gpu Windows test target compiles cleanly (cargo xwin test --target x86_64-pc-windows-msvc -p victory-gpu --no-run).

<!-- greyfinch-sync:fxceless/victory-fork:comment:7165067637 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/936#issuecomment-7165067637). Originally posted by [faycalbaklouti](https://github.com/faycalbaklouti) on 2026-11-25T14:18:11Z. --- Good catch, thanks for testing it! Added `use super::{give_back, pooled, take_snapshots, SNAPS};` to `mod merge_tests` in c00ac40 and verified the `victory-gpu` Windows test target compiles cleanly (`cargo xwin test --target x86_64-pc-windows-msvc -p victory-gpu --no-run`).
faycalbaklouti commented 2026-11-30 21:56:36 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by faycalbaklouti on 2026-11-30T21:56:36Z.


The earlier missing-import fix and this lifetime fix are distinct. The revised Shadow::writing finishes the snapshot while it still holds both buffers; a flush in Drop cannot wait for work another thread already removed from the queue. The regression now starts with four known bytes and checks their contents after a write; it passed in the workspace run. The cost of removing batching across declarations has not been measured.

Compared cda70978a8 with 368967f8fe using the target workflow’s two workspace Clippy builds and workspace tests. Both Clippy variants fail on the base precedence lint. Workspace tests execute and stop in the GPU unit suite with the same five failing assertions on both SHAs. This is baseline-identical on the Windows target under Wine, not a green native Windows run. The separate touched-package suite and each named head witness satisfy the package gate; clean-base zero matches are not passes.

head clippy: exit 101
error: operator precedence might not be obvious
error: could not compile `victory-formats` (lib test) due to 1 previous error
head all-features: exit 101
error: operator precedence might not be obvious
error: could not compile `victory-formats` (lib test) due to 1 previous error
head test: exit 101
    Finished `test` profile [unoptimized] target(s) in 55.62s
test result: ok. 18 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.06s
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.35s
test result: ok. 13 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s
test result: ok. 42 passed; 0 failed; 5 ignored; 0 measured; 0 filtered out; finished in 6.44s
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
test result: FAILED. 412 passed; 5 failed; 35 ignored; 0 measured; 0 filtered out; finished in 12.38s
error: test failed, to rerun pass `-p victory-gpu --lib`
base clippy: exit 101
error: operator precedence might not be obvious
error: could not compile `victory-formats` (lib test) due to 1 previous error
base all-features: exit 101
error: operator precedence might not be obvious
error: could not compile `victory-formats` (lib test) due to 1 previous error
base test: exit 101
    Finished `test` profile [unoptimized] target(s) in 55.47s
test result: ok. 18 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.06s
test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.34s
test result: ok. 13 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s
test result: ok. 42 passed; 0 failed; 5 ignored; 0 measured; 0 filtered out; finished in 6.44s
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
test result: FAILED. 411 passed; 5 failed; 35 ignored; 0 measured; 0 filtered out; finished in 12.30s
error: test failed, to rerun pass `-p victory-gpu --lib`
supplemental head cda70978a8: exit 0
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 451 filtered out; finished in 0.00s
supplemental control 368967f8fe: exit 101
test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 451 filtered out; finished in 0.00s
Clippy: crates/victory-formats/src/texture.rs:1346:55 (`operator precedence might not be obvious`). Tests: cores.rs:171 (`cpu_sets` empty); watch.rs:5075 (two placeholder checks), 5204 (no mapping), 3494 (known holes). The Wine child and display wrapper return 101 from the failing test binary. See both raw test logs for exact names and summaries. Per-package commands and exact-name comparisons: PACKAGE-TESTS.json, order 3.
Touched-package validation, Windows target under Wine:
head cda70978a8: cargo xwin test --locked --target x86_64-pc-windows-msvc -p victory-gpu --lib — exit 101
test result: FAILED. 412 passed; 5 failed; 35 ignored; 0 measured; 0 filtered out; finished in 12.15s
head cda70978a8: cargo xwin test --locked --target x86_64-pc-windows-msvc -p victory-gpu --lib -- --exact shadow::wide_tests::declaring_a_write_finishes_its_snapshot_before_returning --nocapture --test-threads=1 — exit 0
test shadow::wide_tests::declaring_a_write_finishes_its_snapshot_before_returning ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 451 filtered out; finished in 0.00s
base 368967f8fe: cargo xwin test --locked --target x86_64-pc-windows-msvc -p victory-gpu --lib — exit 101
test result: FAILED. 411 passed; 5 failed; 35 ignored; 0 measured; 0 filtered out; finished in 12.09s
base 368967f8fe: cargo xwin test --locked --target x86_64-pc-windows-msvc -p victory-gpu --lib -- --exact shadow::wide_tests::declaring_a_write_finishes_its_snapshot_before_returning --nocapture --test-threads=1 — exit 0
Absent from clean base source; zero selected, not a passing witness.
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 451 filtered out; finished in 0.00s
Matched package failures: cores::tests::the_fast_cores_are_those_of_the_best_class_if_there_is_a_worse_one, watch::many_askers_tests::bits_and_holes_with_faults_that_take_no_lock, watch::many_askers_tests::holes_are_known_until_the_runtime_tells, watch::many_askers_tests::no_mapping_is_lost_between_askers, watch::tests::what_is_left_alone_is_no_known_hole

Radicle project hub (open-source porting and preservation history): https://radicle.network/nodes/rosa.radicle.network/rad:z3rqQw7796wMbgj8RszExtWR4wMSE
Live development stream: https://vdo.ninja/?view=gta6rtx4090live1080p60

<!-- greyfinch-sync:fxceless/victory-fork:comment:7293122065 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/936#issuecomment-7293122065). Originally posted by [faycalbaklouti](https://github.com/faycalbaklouti) on 2026-11-30T21:56:36Z. --- The earlier missing-import fix and this lifetime fix are distinct. The revised `Shadow::writing` finishes the snapshot while it still holds both buffers; a flush in `Drop` cannot wait for work another thread already removed from the queue. The regression now starts with four known bytes and checks their contents after a write; it passed in the workspace run. The cost of removing batching across declarations has not been measured. Compared `cda70978a8` with `368967f8fe` using the target workflow’s two workspace Clippy builds and workspace tests. Both Clippy variants fail on the base precedence lint. Workspace tests execute and stop in the GPU unit suite with the same five failing assertions on both SHAs. This is baseline-identical on the Windows target under Wine, not a green native Windows run. The separate touched-package suite and each named head witness satisfy the package gate; clean-base zero matches are not passes. ```text head clippy: exit 101 error: operator precedence might not be obvious error: could not compile `victory-formats` (lib test) due to 1 previous error head all-features: exit 101 error: operator precedence might not be obvious error: could not compile `victory-formats` (lib test) due to 1 previous error head test: exit 101 Finished `test` profile [unoptimized] target(s) in 55.62s test result: ok. 18 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.06s test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.35s test result: ok. 13 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s test result: ok. 42 passed; 0 failed; 5 ignored; 0 measured; 0 filtered out; finished in 6.44s test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s test result: FAILED. 412 passed; 5 failed; 35 ignored; 0 measured; 0 filtered out; finished in 12.38s error: test failed, to rerun pass `-p victory-gpu --lib` base clippy: exit 101 error: operator precedence might not be obvious error: could not compile `victory-formats` (lib test) due to 1 previous error base all-features: exit 101 error: operator precedence might not be obvious error: could not compile `victory-formats` (lib test) due to 1 previous error base test: exit 101 Finished `test` profile [unoptimized] target(s) in 55.47s test result: ok. 18 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.06s test result: ok. 2 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.34s test result: ok. 13 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s test result: ok. 42 passed; 0 failed; 5 ignored; 0 measured; 0 filtered out; finished in 6.44s test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s test result: FAILED. 411 passed; 5 failed; 35 ignored; 0 measured; 0 filtered out; finished in 12.30s error: test failed, to rerun pass `-p victory-gpu --lib` supplemental head cda70978a8: exit 0 test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 451 filtered out; finished in 0.00s supplemental control 368967f8fe: exit 101 test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 451 filtered out; finished in 0.00s Clippy: crates/victory-formats/src/texture.rs:1346:55 (`operator precedence might not be obvious`). Tests: cores.rs:171 (`cpu_sets` empty); watch.rs:5075 (two placeholder checks), 5204 (no mapping), 3494 (known holes). The Wine child and display wrapper return 101 from the failing test binary. See both raw test logs for exact names and summaries. Per-package commands and exact-name comparisons: PACKAGE-TESTS.json, order 3. Touched-package validation, Windows target under Wine: head cda70978a8: cargo xwin test --locked --target x86_64-pc-windows-msvc -p victory-gpu --lib — exit 101 test result: FAILED. 412 passed; 5 failed; 35 ignored; 0 measured; 0 filtered out; finished in 12.15s head cda70978a8: cargo xwin test --locked --target x86_64-pc-windows-msvc -p victory-gpu --lib -- --exact shadow::wide_tests::declaring_a_write_finishes_its_snapshot_before_returning --nocapture --test-threads=1 — exit 0 test shadow::wide_tests::declaring_a_write_finishes_its_snapshot_before_returning ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 451 filtered out; finished in 0.00s base 368967f8fe: cargo xwin test --locked --target x86_64-pc-windows-msvc -p victory-gpu --lib — exit 101 test result: FAILED. 411 passed; 5 failed; 35 ignored; 0 measured; 0 filtered out; finished in 12.09s base 368967f8fe: cargo xwin test --locked --target x86_64-pc-windows-msvc -p victory-gpu --lib -- --exact shadow::wide_tests::declaring_a_write_finishes_its_snapshot_before_returning --nocapture --test-threads=1 — exit 0 Absent from clean base source; zero selected, not a passing witness. test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 451 filtered out; finished in 0.00s Matched package failures: cores::tests::the_fast_cores_are_those_of_the_best_class_if_there_is_a_worse_one, watch::many_askers_tests::bits_and_holes_with_faults_that_take_no_lock, watch::many_askers_tests::holes_are_known_until_the_runtime_tells, watch::many_askers_tests::no_mapping_is_lost_between_askers, watch::tests::what_is_left_alone_is_no_known_hole ``` --- **Radicle project hub (open-source porting and preservation history):** https://radicle.network/nodes/rosa.radicle.network/rad:z3rqQw7796wMbgj8RszExtWR4wMSE **Live development stream:** https://vdo.ninja/?view=gta6rtx4090live1080p60
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/936:upstream-pr/936'
git switch 'upstream-pr/936'

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/936'
git switch 'upstream-pr/936'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/936'
git switch 'upstream-pr/936'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/936'
git switch 'main'
git merge --squash 'upstream-pr/936'
git switch 'main'
git merge --ff-only 'upstream-pr/936'
git switch 'main'
git merge 'upstream-pr/936'
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
fxceless/victory-fork!935
No description provided.