hle(xasync): avoid reading freed AsyncBlock on duplicate complete_call #2927

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

@⁠RanoGames on GitHub · 2026-11-29 18:52 UTC · original
RanoGames:fix/xasync-complete-call-uaf → main · +54 −6 · 1 files · 1 commits · open

What it changes

Fixes a use-after-free in complete_call in crates/victory-hle/src/modules/xasync.rs:

  • complete_call previously dereferenced (*block).queue and (*block).callback before locking call.done and checking if done.is_some() { return; }.
  • When a call was already canceled via XAsyncCancel (or completed by another caller) and the title freed or unmapped the AsyncBlock, a late worker thread from pending_with (or a second XAsyncComplete) would read freed memory before done.is_some() discarded the duplicate completion.
  • Moved the (*block).queue / (*block).callback read after the done.is_some() guard under the call.done lock.
  • Added regression test modules::xasync::tests::late_worker_completion_after_cancel_does_not_touch_freed_block using VirtualAlloc/VirtualFree to unmap the AsyncBlock page right after XAsyncCancel.

How it was tested

  • GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): N/A (unit-tested xasync concurrency fix)
  • Before: complete_call dereferenced block before checking call.done, causing a use-after-free when a late worker completed after XAsyncCancel + freeing AsyncBlock.
  • After: complete_call checks done.is_some() first and returns without touching block.
  • For shader changes, the wave sizes tested (32 and/or 64 lanes): N/A
  • cargo test passed for the crates it touches: yes (cargo test --locked -p victory-hle -- modules::xasync::tests: 31 passed, 0 failed; cargo xtask policy --staged passed)

Checked for overlap

  • I searched the open pull requests for the same issue and for the files this touches. Related: #2402 (only changes finished_status), #2416 (only changes XTaskQueueUnregisterMonitor)
**@⁠RanoGames** on GitHub · 2026-11-29 18:52 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2927) `RanoGames:fix/xasync-complete-call-uaf` → `main` · +54 −6 · 1 files · 1 commits · open ## What it changes Fixes a use-after-free in `complete_call` in `crates/victory-hle/src/modules/xasync.rs`: - `complete_call` previously dereferenced `(*block).queue` and `(*block).callback` before locking `call.done` and checking `if done.is_some() { return; }`. - When a call was already canceled via `XAsyncCancel` (or completed by another caller) and the title freed or unmapped the `AsyncBlock`, a late worker thread from `pending_with` (or a second `XAsyncComplete`) would read freed memory before `done.is_some()` discarded the duplicate completion. - Moved the `(*block).queue` / `(*block).callback` read after the `done.is_some()` guard under the `call.done` lock. - Added regression test `modules::xasync::tests::late_worker_completion_after_cancel_does_not_touch_freed_block` using `VirtualAlloc`/`VirtualFree` to unmap the `AsyncBlock` page right after `XAsyncCancel`. ## How it was tested - GPU and profile (Series X or S, resolution, DLSS, frame generation, ray tracing): N/A (unit-tested `xasync` concurrency fix) - Before: `complete_call` dereferenced `block` before checking `call.done`, causing a use-after-free when a late worker completed after `XAsyncCancel` + freeing `AsyncBlock`. - After: `complete_call` checks `done.is_some()` first and returns without touching `block`. - For shader changes, the wave sizes tested (32 and/or 64 lanes): N/A - `cargo test` passed for the crates it touches: yes (`cargo test --locked -p victory-hle -- modules::xasync::tests`: 31 passed, 0 failed; `cargo xtask policy --staged` passed) ## Checked for overlap - [x] I searched the open pull requests for the same issue and for the files this touches. Related: #2402 (only changes `finished_status`), #2416 (only changes `XTaskQueueUnregisterMonitor`)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-29 18:56:50 +00:00
Author
Owner

@⁠cloph-dsp on GitHub · 2026-11-30 04:13 UTC · original

Independent local verification (Windows, Rust 1.100.0/MSVC, no game data):

  • PR #2927 at head 21ba02c5a8583c305da4bdb24623bc10c0fc273f, base/parent 829188e969ffd2907e5867c5d9aca29bd91cb1b6.
  • cargo test --locked -p victory-hle --lib -- modules::xasync::tests::late_worker_completion_after_cancel_does_not_touch_freed_block --exact --test-threads=1 --quiet → 1 passed, 0 failed, 428 filtered, exit 0.
  • git grep -F for the test name on the parent returned no match (test is new on this PR); git diff --check 829188e HEAD clean (1 file, +64/-8).
  • Used a per-worktree CARGO_TARGET_DIR (never shared) with CARGO_BUILD_JOBS=2.
  • Only the named synthetic test was replayed; full workspace suite and clippy were not run locally. 2 pre-existing warnings in the victory-hle lib test build.
  • GPU/driver: N/A (CPU-only HLE xasync test). No game data, assets, extracted shaders, DRM or decrypt involved.

AI-assisted (Hy4 flash).

**@⁠cloph-dsp** on GitHub · 2026-11-30 04:13 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2927#issuecomment-7268575577) Independent local verification (Windows, Rust 1.100.0/MSVC, no game data): - PR #2927 at head `21ba02c5a8583c305da4bdb24623bc10c0fc273f`, base/parent `829188e969ffd2907e5867c5d9aca29bd91cb1b6`. - `cargo test --locked -p victory-hle --lib -- modules::xasync::tests::late_worker_completion_after_cancel_does_not_touch_freed_block --exact --test-threads=1 --quiet` → 1 passed, 0 failed, 428 filtered, exit 0. - `git grep -F` for the test name on the parent returned no match (test is new on this PR); `git diff --check 829188e HEAD` clean (1 file, +64/-8). - Used a per-worktree `CARGO_TARGET_DIR` (never shared) with `CARGO_BUILD_JOBS=2`. - Only the named synthetic test was replayed; full workspace suite and clippy were not run locally. 2 pre-existing warnings in the victory-hle lib test build. - GPU/driver: N/A (CPU-only HLE xasync test). No game data, assets, extracted shaders, DRM or decrypt involved. AI-assisted (Hy4 flash).
Author
Owner

@⁠KruIN689 on GitHub · 2026-12-01 04:11 UTC · original

Independent test: this fix also removes a test-process crash that main's own tests hit, not only the title path in its description.

What crashes on main. victory-hle's test process dies now and then with exit 0x80000001 (STATUS_GUARD_PAGE_VIOLATION) on CI, and with 0xC0000005 locally. It shows on a busy or small machine: on a 32-thread PC it reproduces only when the process is pinned to 2 CPUs. Bisecting the xasync tests puts cleanup_waits_for_work_under_way in every set that crashes.

That test's AsyncBlock is on its stack. After XAsyncCancel, DoWork returns and Call::run runs the deferred cleanup. The test sees the cleanup and returns, and its thread and stack go away. The pool worker then calls complete_call, which reads (*block).queue and (*block).callback before the done check: that read lands in freed stack memory. With this PR's order, the losing completion returns before it touches the block.

Numbers (Windows 11, Ryzen 9 9950X3D, cargo test -p victory-hle --lib, the test binary pinned to CPUs 0-1, xasync tests only, 20 runs each):

tests run main (palmlight, same code as upstream here) + this PR's change
cleanup_waits + delayed_callbacks + idle_pool_workers_end 10 of 20 crashed 0 of 20
those + finished_preserves_the_completion_status 5 of 20 0 of 20, twice
cleanup_waits + idle_pool_workers_end 2 of 20 —
any set without cleanup_waits 0 of 20 each —
all xasync tests 2 of 8 0 of 20

Whole victory-hle suite, pinned, 3×10 runs in turns: main 10 of 30 crashed, with this change 5 of 30. The rest comes from a second, unrelated cause: media's asynchronous_file_creation_reads_the_rewritten_path keeps its COM callback on the stack, and Media Foundation releases it after the test is over. That is fixed separately (#4732). With both: 0 of 30, against main's 9 of 30.

Upstream main still reads the block before the guard (complete_call, let (handle, routine) comes before if done.is_some()). The PR now conflicts with main. In palmlight the conflict was only the new test's position at the end of the tests module; both test sets stay. Ported with authorship kept: Lyrnc/palmlight#92. A rebase here would let CI's flaky hle crash go away upstream too.

Limits: CPU-only. No debugger dump, so the stack trace is reasoned from the code plus the bisect, not captured.

Tested with the help of Claude Code (Claude Opus 5.5).

**@⁠KruIN689** on GitHub · 2026-12-01 04:11 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2927#issuecomment-7300478071) Independent test: this fix also removes a test-process crash that main's own tests hit, not only the title path in its description. **What crashes on main.** `victory-hle`'s test process dies now and then with exit 0x80000001 (STATUS_GUARD_PAGE_VIOLATION) on CI, and with 0xC0000005 locally. It shows on a busy or small machine: on a 32-thread PC it reproduces only when the process is pinned to 2 CPUs. Bisecting the xasync tests puts `cleanup_waits_for_work_under_way` in every set that crashes. That test's `AsyncBlock` is on its stack. After `XAsyncCancel`, `DoWork` returns and `Call::run` runs the deferred cleanup. The test sees the cleanup and returns, and its thread and stack go away. The pool worker then calls `complete_call`, which reads `(*block).queue` and `(*block).callback` before the `done` check: that read lands in freed stack memory. With this PR's order, the losing completion returns before it touches the block. **Numbers** (Windows 11, Ryzen 9 9950X3D, `cargo test -p victory-hle --lib`, the test binary pinned to CPUs 0-1, xasync tests only, 20 runs each): | tests run | main (palmlight, same code as upstream here) | + this PR's change | |---|---|---| | cleanup_waits + delayed_callbacks + idle_pool_workers_end | 10 of 20 crashed | 0 of 20 | | those + finished_preserves_the_completion_status | 5 of 20 | 0 of 20, twice | | cleanup_waits + idle_pool_workers_end | 2 of 20 | — | | any set without cleanup_waits | 0 of 20 each | — | | all xasync tests | 2 of 8 | 0 of 20 | Whole `victory-hle` suite, pinned, 3×10 runs in turns: main 10 of 30 crashed, with this change 5 of 30. The rest comes from a second, unrelated cause: `media`'s `asynchronous_file_creation_reads_the_rewritten_path` keeps its COM callback on the stack, and Media Foundation releases it after the test is over. That is fixed separately (#4732). With both: 0 of 30, against main's 9 of 30. Upstream main still reads the block before the guard (`complete_call`, `let (handle, routine)` comes before `if done.is_some()`). The PR now conflicts with main. In palmlight the conflict was only the new test's position at the end of the tests module; both test sets stay. Ported with authorship kept: Lyrnc/palmlight#92. A rebase here would let CI's flaky hle crash go away upstream too. Limits: CPU-only. No debugger dump, so the stack trace is reasoned from the code plus the bisect, not captured. *Tested with the help of Claude Code (Claude Opus 5.5).*
Author
Owner

@⁠illuminatedfellow on GitHub · 2026-12-01 14:31 UTC · original

Tested on Linux. I ported late_worker_completion_after_cancel_does_not_touch_freed_block there (mmap/munmap in place of VirtualAlloc/VirtualFree): on main the test process dies with a segfault when the late completion reads the freed block; with this PR it passes. xasync.rs built alone with a stub for xgameruntime: all tests pass, 40 runs without a failure, clippy clean.

Since #2402 and #2416 were merged, this conflicts with main (5fabb46e), only in the tests module: both add a test at the same spot. Keeping both tests resolves it; with the three combined, all 34 tests pass.

**@⁠illuminatedfellow** on GitHub · 2026-12-01 14:31 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2927#issuecomment-7314399773) Tested on Linux. I ported `late_worker_completion_after_cancel_does_not_touch_freed_block` there (`mmap`/`munmap` in place of `VirtualAlloc`/`VirtualFree`): on `main` the test process dies with a segfault when the late completion reads the freed block; with this PR it passes. `xasync.rs` built alone with a stub for `xgameruntime`: all tests pass, 40 runs without a failure, clippy clean. Since #2402 and #2416 were merged, this conflicts with `main` (5fabb46e), only in the tests module: both add a test at the same spot. Keeping both tests resolves it; with the three combined, all 34 tests pass.
Author
Owner

@⁠gut0to on GitHub · 2026-12-01 15:01 UTC · original · reviewed

AI-assisted review: I traced the cancel callback, worker completion, and status publication paths in this diff. I used AI assistance and verified the interleaving against the current source.

**@⁠gut0to** on GitHub · 2026-12-01 15:01 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2927#pullrequestreview-6133875576) · **reviewed** AI-assisted review: I traced the cancel callback, worker completion, and status publication paths in this diff. I used AI assistance and verified the interleaving against the current source.
@ -1138,4 +1130,3 @@
};
// The first completion wins. Upstream guards this with the `done` lock
// rather than a claimed flag, so the guard below reads `is_some()`.
let mut done = lock(&call.done);
Author
Owner

@⁠gut0to on GitHub · 2026-12-01 15:01 UTC · original

Could this also protect an in-flight OP_CANCEL callback from concurrent completion? XAsyncCancel checks call.done, drops that lock, then calls the provider with ProviderData.block. A worker can publish success and make the title observe completion while that provider callback is still using the same AsyncBlock; Call::busy delays provider cleanup, but it does not keep the caller-owned block alive. Please serialize cancellation against completion without holding a lock across reentrant provider code, and add a test that blocks inside OP_CANCEL while another thread completes the call.

**@⁠gut0to** on GitHub · 2026-12-01 15:01 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/2927#discussion_r4756106218) Could this also protect an in-flight `OP_CANCEL` callback from concurrent completion? `XAsyncCancel` checks `call.done`, drops that lock, then calls the provider with `ProviderData.block`. A worker can publish success and make the title observe completion while that provider callback is still using the same `AsyncBlock`; `Call::busy` delays provider cleanup, but it does not keep the caller-owned block alive. Please serialize cancellation against completion without holding a lock across reentrant provider code, and add a test that blocks inside `OP_CANCEL` while another thread completes the call.
github-bot force-pushed github-bot/gh-2927 from 21ba02c5a8 to 9a6c6384e4 2026-12-01 16:29:22 +00:00 Compare
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/2927/head:github-bot/gh-2927'
git switch 'github-bot/gh-2927'

Merge

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