HLE: defer XAsync cleanup until in-flight provider ops return #3281

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

@⁠fuzeofficlal on GitHub · 2026-11-26 15:40 UTC · original
fuzeofficlal:fix/hle-xasync-provider-lifecycle-sync → main · +608 −36 · 1 files · 1 commits · open

Summary

Synchronize XAsync provider operation lifetimes (OP_BEGIN, OP_DO_WORK, and OP_CANCEL) with completion and OP_CLEANUP in crates/victory-hle/src/modules/xasync.rs so cleanup and status publication cannot free provider context or the title's AsyncBlock while another thread is still executing a provider callback.

Problem

Previously, Call coordinated completion with a single claimed: AtomicBool, and neither XAsyncBegin, XAsyncSchedule's worker (OP_DO_WORK), nor XAsyncCancel (OP_CANCEL) tracked in-flight provider calls:

  1. Cancel during in-flight OP_DO_WORK (context + block UAF): If a title calls XAsyncCancel(block) while a worker thread is executing OP_DO_WORK, XAsyncCancel immediately runs complete_call(E_ABORT, 0), which invokes OP_CLEANUP (freeing the provider's context) and publishes completion / invokes the completion callback (allowing the title to free block) while OP_DO_WORK is still running on the worker thread.
  2. Complete racing XAsyncCancel (OP_CANCEL after or during OP_CLEANUP, duplicate OP_CANCEL): XAsyncCancel checks call.claimed.load(Acquire) non-atomically before calling OP_CANCEL. A concurrent XAsyncComplete can run OP_CLEANUP either before OP_CANCEL enters or while OP_CANCEL is executing, and two concurrent XAsyncCancel calls can invoke OP_CANCEL twice.
  3. TOCTOU before OP_DO_WORK and completion during OP_BEGIN: A scheduled worker that passes is_current before a concurrent cancel/completion can still enter OP_DO_WORK after cleanup, and an off-thread XAsyncComplete triggered inside OP_BEGIN can run OP_CLEANUP before OP_BEGIN returns.

Fix

  • Replace Call::claimed with Call::lifecycle: Mutex<Lifecycle> tracking active provider op threads (active: Vec<ThreadId>), cancellation dispatch (canceling), the winning completion (pending), and one-shot finalization (finalizing), guarded by an RAII OpGuard.
  • Enter OP_BEGIN before inserting call into CALLS, and atomically gate OP_DO_WORK and OP_CANCEL entry on !canceling && pending.is_none() && !finalizing (setting canceling = true once for OP_CANCEL).
  • In complete_call, record the first winning (result, required_size). Finalize immediately when no other thread is inside a provider op (preserving synchronous reentrant completion from OP_BEGIN/OP_DO_WORK/OP_CANCEL on the same thread); otherwise defer finalization without blocking (preventing AB-BA deadlocks when a title holds a lock across XAsyncCancel that OP_DO_WORK also acquires) so the last active OpGuard to exit runs finalize exactly once.
  • Check Arc::ptr_eq in release_call before removing from CALLS and invoking OP_CLEANUP, keeping CALLS, call.lifecycle, and call.done strictly unnested leaf locks never held across provider or title callbacks.

Verification

  • Added three deterministic multi-threaded regression tests in crates/victory-hle/src/modules/xasync.rs (all three fail deterministically on 63bcca43 and pass with the fix, including 20 consecutive stress rounds):
    • cancel_waits_for_in_flight_do_work_before_cleanup_and_completion
    • complete_waits_for_in_flight_cancel_and_blocks_late_cancel (includes a 64-iteration 3-thread barrier race)
    • concurrent_completion_defers_cleanup_during_begin_and_preserves_payload_on_cancel
  • Ran cargo test -p victory-hle --locked and --all-features (155 unit + 2 integration tests passed).
  • Ran cargo clippy --workspace --all-targets --locked -- -D warnings and --all-features (0 warnings).
  • Ran cargo test -p xtask --locked policy::tests::repository_passes_policy (passed).
**@⁠fuzeofficlal** on GitHub · 2026-11-26 15:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/3281) `fuzeofficlal:fix/hle-xasync-provider-lifecycle-sync` → `main` · +608 −36 · 1 files · 1 commits · open ## Summary Synchronize `XAsync` provider operation lifetimes (`OP_BEGIN`, `OP_DO_WORK`, and `OP_CANCEL`) with completion and `OP_CLEANUP` in `crates/victory-hle/src/modules/xasync.rs` so cleanup and status publication cannot free provider context or the title's `AsyncBlock` while another thread is still executing a provider callback. ## Problem Previously, `Call` coordinated completion with a single `claimed: AtomicBool`, and neither `XAsyncBegin`, `XAsyncSchedule`'s worker (`OP_DO_WORK`), nor `XAsyncCancel` (`OP_CANCEL`) tracked in-flight provider calls: 1. **Cancel during in-flight `OP_DO_WORK` (context + block UAF)**: If a title calls `XAsyncCancel(block)` while a worker thread is executing `OP_DO_WORK`, `XAsyncCancel` immediately runs `complete_call(E_ABORT, 0)`, which invokes `OP_CLEANUP` (freeing the provider's context) and publishes completion / invokes the completion callback (allowing the title to free `block`) while `OP_DO_WORK` is still running on the worker thread. 2. **Complete racing `XAsyncCancel` (`OP_CANCEL` after or during `OP_CLEANUP`, duplicate `OP_CANCEL`)**: `XAsyncCancel` checks `call.claimed.load(Acquire)` non-atomically before calling `OP_CANCEL`. A concurrent `XAsyncComplete` can run `OP_CLEANUP` either before `OP_CANCEL` enters or while `OP_CANCEL` is executing, and two concurrent `XAsyncCancel` calls can invoke `OP_CANCEL` twice. 3. **TOCTOU before `OP_DO_WORK` and completion during `OP_BEGIN`**: A scheduled worker that passes `is_current` before a concurrent cancel/completion can still enter `OP_DO_WORK` after cleanup, and an off-thread `XAsyncComplete` triggered inside `OP_BEGIN` can run `OP_CLEANUP` before `OP_BEGIN` returns. ## Fix - Replace `Call::claimed` with `Call::lifecycle: Mutex<Lifecycle>` tracking active provider op threads (`active: Vec<ThreadId>`), cancellation dispatch (`canceling`), the winning completion (`pending`), and one-shot finalization (`finalizing`), guarded by an RAII `OpGuard`. - Enter `OP_BEGIN` before inserting `call` into `CALLS`, and atomically gate `OP_DO_WORK` and `OP_CANCEL` entry on `!canceling && pending.is_none() && !finalizing` (setting `canceling = true` once for `OP_CANCEL`). - In `complete_call`, record the first winning `(result, required_size)`. Finalize immediately when no *other* thread is inside a provider op (preserving synchronous reentrant completion from `OP_BEGIN`/`OP_DO_WORK`/`OP_CANCEL` on the same thread); otherwise defer finalization without blocking (preventing AB-BA deadlocks when a title holds a lock across `XAsyncCancel` that `OP_DO_WORK` also acquires) so the last active `OpGuard` to exit runs `finalize` exactly once. - Check `Arc::ptr_eq` in `release_call` before removing from `CALLS` and invoking `OP_CLEANUP`, keeping `CALLS`, `call.lifecycle`, and `call.done` strictly unnested leaf locks never held across provider or title callbacks. ## Verification - Added three deterministic multi-threaded regression tests in `crates/victory-hle/src/modules/xasync.rs` (all three fail deterministically on `63bcca43` and pass with the fix, including 20 consecutive stress rounds): - `cancel_waits_for_in_flight_do_work_before_cleanup_and_completion` - `complete_waits_for_in_flight_cancel_and_blocks_late_cancel` (includes a 64-iteration 3-thread barrier race) - `concurrent_completion_defers_cleanup_during_begin_and_preserves_payload_on_cancel` - Ran `cargo test -p victory-hle --locked` and `--all-features` (155 unit + 2 integration tests passed). - Ran `cargo clippy --workspace --all-targets --locked -- -D warnings` and `--all-features` (0 warnings). - Ran `cargo test -p xtask --locked policy::tests::repository_passes_policy` (passed).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 15:42:17 +00:00
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 15:48 UTC · original · approved these changes

Reviewed the diff and verified locally on Windows 11 x64:

  • Coordinating OP_BEGIN / OP_DO_WORK / OP_CANCEL with completion claiming via Call::lifecycle and RAII OpGuard cleanly defers OP_CLEANUP and completion publication until the last in-flight provider op exits, preventing use-after-cleanup races across concurrent completion and cancellation.
  • cargo test -p victory-hle --lib modules::xasync:: -- --test-threads=1: 16 passed, 0 failed (including cancel_waits_for_in_flight_do_work_before_cleanup_and_completion, complete_waits_for_in_flight_cancel_and_blocks_late_cancel, and concurrent_completion_defers_cleanup_during_begin_and_preserves_payload_on_cancel).
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 15:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/3281#pullrequestreview-6077023618) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64: - Coordinating `OP_BEGIN` / `OP_DO_WORK` / `OP_CANCEL` with completion claiming via `Call::lifecycle` and RAII `OpGuard` cleanly defers `OP_CLEANUP` and completion publication until the last in-flight provider op exits, preventing use-after-cleanup races across concurrent completion and cancellation. - `cargo test -p victory-hle --lib modules::xasync:: -- --test-threads=1`: 16 passed, 0 failed (including `cancel_waits_for_in_flight_do_work_before_cleanup_and_completion`, `complete_waits_for_in_flight_cancel_and_blocks_late_cancel`, and `concurrent_completion_defers_cleanup_during_begin_and_preserves_payload_on_cancel`). - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 16:55 UTC · original

Independent verification of #3281 (claimed on #1633). Test report only, no code changes. AI use: Claude Code (Claude Sonnet 5.5), run on behalf of oni-swr.

  • Head: 658b2fdeb90a19c1e2b8840351b444df0f6ec43f. Control: main = 63bcca43c6a00a758510398fa43096ce7e4f7df0 (the PR's merge base).
  • Windows 11 Pro, CPU only (HLE needs no GPU). No game files. I found no CONTRIBUTING.md in the repository, so I followed the #1633 rules.
  • The existing approving review on this head ran only the modules::xasync:: filter with --test-threads=1, and had no control on main, so I did not treat it as covering this.
  • Commands: cargo test -j 4 -p victory-hle --lib --locked on head and on the control; cargo test -j 4 -p victory-hle --locked on head; the 3 new tests run 10 times on head and 7 times on the control.
  • Control = main with only the PR's three new tests appended to the existing test module (the module imports are identical, so nothing else was needed). No source change.
Run Result
head, lib suite 155 passed, 0 failed
head, whole package (lib + 2 integration + doc) all ok (155 + 1 + 1)
head, the 3 new tests, 10 runs 3 passed every time
main, plain lib suite 152 passed, 0 failed
main + the 3 new tests, lib suite 152 passed, 3 failed
main + the 3 new tests, 3 tests run 5 more times 3 failed every time

All three claimed tests exist at head: cancel_waits_for_in_flight_do_work_before_cleanup_and_completion, complete_waits_for_in_flight_cancel_and_blocks_late_cancel, concurrent_completion_defers_cleanup_during_begin_and_preserves_payload_on_cancel.

Do they catch a real bug on main? Yes, all three, 7 of 7 control runs, and they fail fast (assertions, about 0.00 s) rather than hanging on their barriers. Failure messages on main: cleanup freed provider context while OP_DO_WORK was still running, cleanup ran while OP_CANCEL was still executing, and !begin_ctx.cleaned_during_begin (cleanup ran during OP_BEGIN).

No flakes seen. The PR description says the last test includes a "64-iteration 3-thread barrier race"; I ran it 10 times on head without a failure. Not verified: real titles, other CPUs or memory models, clippy and policy xtask.

**@⁠oni-swr** on GitHub · 2026-11-26 16:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/3281#issuecomment-7198274908) Independent verification of #3281 (claimed on #1633). Test report only, no code changes. AI use: Claude Code (Claude Sonnet 5.5), run on behalf of oni-swr. - Head: `658b2fdeb90a19c1e2b8840351b444df0f6ec43f`. Control: `main` = `63bcca43c6a00a758510398fa43096ce7e4f7df0` (the PR's merge base). - Windows 11 Pro, CPU only (HLE needs no GPU). No game files. I found no CONTRIBUTING.md in the repository, so I followed the #1633 rules. - The existing approving review on this head ran only the `modules::xasync::` filter with `--test-threads=1`, and had no control on main, so I did not treat it as covering this. - Commands: `cargo test -j 4 -p victory-hle --lib --locked` on head and on the control; `cargo test -j 4 -p victory-hle --locked` on head; the 3 new tests run 10 times on head and 7 times on the control. - Control = main with only the PR's three new tests appended to the existing test module (the module imports are identical, so nothing else was needed). No source change. | Run | Result | |---|---| | head, lib suite | 155 passed, 0 failed | | head, whole package (lib + 2 integration + doc) | all ok (155 + 1 + 1) | | head, the 3 new tests, 10 runs | 3 passed every time | | main, plain lib suite | 152 passed, 0 failed | | main + the 3 new tests, lib suite | 152 passed, **3 failed** | | main + the 3 new tests, 3 tests run 5 more times | 3 failed every time | All three claimed tests exist at head: `cancel_waits_for_in_flight_do_work_before_cleanup_and_completion`, `complete_waits_for_in_flight_cancel_and_blocks_late_cancel`, `concurrent_completion_defers_cleanup_during_begin_and_preserves_payload_on_cancel`. **Do they catch a real bug on main?** Yes, all three, 7 of 7 control runs, and they fail fast (assertions, about 0.00 s) rather than hanging on their barriers. Failure messages on main: `cleanup freed provider context while OP_DO_WORK was still running`, `cleanup ran while OP_CANCEL was still executing`, and `!begin_ctx.cleaned_during_begin` (cleanup ran during `OP_BEGIN`). No flakes seen. The PR description says the last test includes a "64-iteration 3-thread barrier race"; I ran it 10 times on head without a failure. Not verified: real titles, other CPUs or memory models, clippy and policy xtask.
This pull request can be merged automatically.
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/3281/head:github-bot/gh-3281'
git switch 'github-bot/gh-3281'

Merge

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