hle(xasync): avoid reading freed AsyncBlock on duplicate complete_call #2927
No reviewers
Labels
No labels
AMD
audio
bug
confirmed
crash
cutscene
dlss
documentation
duplicate
enhancement
fixed-on-main
frame-generation
good first issue
help wanted
input
INTEL
invalid
lighting
linux-proton
memory
needs-info
needs-logs
needs-rebase
NVIDIA
performance
pr-diagnostics
pr-docs
pr-feature
pr-fix
pr-performance
pr-refactor
pr-tests
question
RAYTRACING
rendering
report
series-s
shaders
spam
startup
upstream
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/trustno1-coder--VIctory-NVIDIA!2927
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2927"
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?
@RanoGames on GitHub · 2026-11-29 18:52 UTC · original
RanoGames:fix/xasync-complete-call-uaf→main· +54 −6 · 1 files · 1 commits · openWhat it changes
Fixes a use-after-free in
complete_callincrates/victory-hle/src/modules/xasync.rs:complete_callpreviously dereferenced(*block).queueand(*block).callbackbefore lockingcall.doneand checkingif done.is_some() { return; }.XAsyncCancel(or completed by another caller) and the title freed or unmapped theAsyncBlock, a late worker thread frompending_with(or a secondXAsyncComplete) would read freed memory beforedone.is_some()discarded the duplicate completion.(*block).queue/(*block).callbackread after thedone.is_some()guard under thecall.donelock.modules::xasync::tests::late_worker_completion_after_cancel_does_not_touch_freed_blockusingVirtualAlloc/VirtualFreeto unmap theAsyncBlockpage right afterXAsyncCancel.How it was tested
xasyncconcurrency fix)complete_calldereferencedblockbefore checkingcall.done, causing a use-after-free when a late worker completed afterXAsyncCancel+ freeingAsyncBlock.complete_callchecksdone.is_some()first and returns without touchingblock.cargo testpassed for the crates it touches: yes (cargo test --locked -p victory-hle -- modules::xasync::tests: 31 passed, 0 failed;cargo xtask policy --stagedpassed)Checked for overlap
finished_status), #2416 (only changesXTaskQueueUnregisterMonitor)@cloph-dsp on GitHub · 2026-11-30 04:13 UTC · original
Independent local verification (Windows, Rust 1.100.0/MSVC, no game data):
21ba02c5a8583c305da4bdb24623bc10c0fc273f, base/parent829188e969ffd2907e5867c5d9aca29bd91cb1b6.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 -Ffor the test name on the parent returned no match (test is new on this PR);git diff --check 829188e HEADclean (1 file, +64/-8).CARGO_TARGET_DIR(never shared) withCARGO_BUILD_JOBS=2.AI-assisted (Hy4 flash).
@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 putscleanup_waits_for_work_under_wayin every set that crashes.That test's
AsyncBlockis on its stack. AfterXAsyncCancel,DoWorkreturns andCall::runruns the deferred cleanup. The test sees the cleanup and returns, and its thread and stack go away. The pool worker then callscomplete_call, which reads(*block).queueand(*block).callbackbefore thedonecheck: 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):Whole
victory-hlesuite, 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'sasynchronous_file_creation_reads_the_rewritten_pathkeeps 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 beforeif 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).
@illuminatedfellow on GitHub · 2026-12-01 14:31 UTC · original
Tested on Linux. I ported
late_worker_completion_after_cancel_does_not_touch_freed_blockthere (mmap/munmapin place ofVirtualAlloc/VirtualFree): onmainthe test process dies with a segfault when the late completion reads the freed block; with this PR it passes.xasync.rsbuilt alone with a stub forxgameruntime: 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.@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.
@ -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);@gut0to on GitHub · 2026-12-01 15:01 UTC · original
Could this also protect an in-flight
OP_CANCELcallback from concurrent completion?XAsyncCancelcheckscall.done, drops that lock, then calls the provider withProviderData.block. A worker can publish success and make the title observe completion while that provider callback is still using the sameAsyncBlock;Call::busydelays 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 insideOP_CANCELwhile another thread completes the call.21ba02c5a8to9a6c6384e4View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.