XAsync: a canceled pending call's work touches neither freed state nor the next call #236

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

@⁠Dufftwt on GitHub · 2026-11-24 01:42 UTC · original
Dufftwt:fix/xasync-canceled-pending-call → main · +63 −9 · 1 files · 1 commits · open

What's wrong

xasync::pending_with runs an async call's work on a thread of its own and completes the call when the work returns. XGameUiShowTextEntryAsync uses it: the work is the text-entry dialog, which runs until the user closes it.

If the title cancels such a call, XAsyncCancel completes it at once with E_ABORT. That releases the call, and the provider's cleanup frees its state. The work thread doesn't know: the dialog is still open. When it finally returns, the thread does two things wrong:

  1. It writes the result into the freed state, a use-after-free. The comment above it says "cleanup waits for completion", which a cancel makes untrue.
  2. It calls XAsyncComplete on the block, not on its call. By then the title may have begun its next call on the same block, as titles commonly do. That call is then completed with the old call's status and size, and its own work is still running:
pending_with(block, dialog A)       -> pending
XAsyncCancel(block)                 -> A ends with E_ABORT, A's state freed
pending_with(block, dialog B)       -> B pending on the same block
dialog A closes                     -> writes into A's freed state, completes B with A's result
XAsyncGetStatus(block)              -> S_OK, while dialog B is still open

(If B's state happens to reuse A's freed allocation, B even returns A's text.)

What the fix does

Two changes in pending_with, both contained in that function:

  • The state is shared with the work thread through an Arc instead of a raw pointer. Cleanup drops the call's reference; the thread keeps its own, so writing the result is always safe.
  • At OP_BEGIN the thread records which call it belongs to (the Arc<Call> that XAsyncBegin just registered for the block). When the work returns, the thread completes the block only if that call is still the block's call. A canceled call has ended, so its late result is dropped.

How it behaves after the fix

  • Canceling a pending call ends it with E_ABORT, as before.
  • When the canceled work finishes later, its result is discarded. The title's next call on the same block isn't touched and completes with its own result.
  • Calls that aren't canceled complete exactly as before.

Tests

The new test a_canceled_pending_call_leaves_the_next_call_alone follows the sequence above. Each call's work waits on a channel until the test lets it finish:

  1. It begins and cancels call A, then begins call B on the same block.
  2. It lets A's work finish. After 200 ms, B must still be pending.
  3. It lets B's work finish. B must complete with its own result ([9]).

I ran cargo test --workspace on GitHub's windows-latest with Rust 1.98.1 from rust-toolchain.toml:

  • This PR: everything passes. victory-hle has 87 passed, the other crates have 0 failed.
  • The new test alone on main: it fails at step 2. B's status is S_OK (left: 0) instead of E_PENDING (right: -2147483638, i.e. 0x8000000A): A's late completion ended B.
  • I ran with --test-threads=1. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (STATUS_ACCESS_VIOLATION), on unmodified main as well.

Not tested: a title canceling its text entry.

Related: #122 fixes the same pattern ("complete the call that finished, not whatever call the block holds now") for work scheduled with XAsyncSchedule. This PR doesn't depend on it, and the two merge cleanly in either order. They also merge cleanly with #114 and #132.

**@⁠Dufftwt** on GitHub · 2026-11-24 01:42 UTC · [original](https://github.com/Marvisak/VIctory/pull/236) `Dufftwt:fix/xasync-canceled-pending-call` → `main` · +63 −9 · 1 files · 1 commits · open ## What's wrong `xasync::pending_with` runs an async call's work on a thread of its own and completes the call when the work returns. `XGameUiShowTextEntryAsync` uses it: the work is the text-entry dialog, which runs until the user closes it. If the title cancels such a call, `XAsyncCancel` completes it at once with `E_ABORT`. That releases the call, and the provider's cleanup frees its state. The work thread doesn't know: the dialog is still open. When it finally returns, the thread does two things wrong: 1. It **writes the result into the freed state**, a use-after-free. The comment above it says "cleanup waits for completion", which a cancel makes untrue. 2. It calls `XAsyncComplete` **on the block, not on its call**. By then the title may have begun its next call on the same block, as titles commonly do. That call is then completed with the old call's status and size, and its own work is still running: ``` pending_with(block, dialog A) -> pending XAsyncCancel(block) -> A ends with E_ABORT, A's state freed pending_with(block, dialog B) -> B pending on the same block dialog A closes -> writes into A's freed state, completes B with A's result XAsyncGetStatus(block) -> S_OK, while dialog B is still open ``` (If B's state happens to reuse A's freed allocation, B even returns A's text.) ## What the fix does Two changes in `pending_with`, both contained in that function: - The state is shared with the work thread through an `Arc` instead of a raw pointer. Cleanup drops the call's reference; the thread keeps its own, so writing the result is always safe. - At `OP_BEGIN` the thread records which call it belongs to (the `Arc<Call>` that `XAsyncBegin` just registered for the block). When the work returns, the thread completes the block only if that call is still the block's call. A canceled call has ended, so its late result is dropped. ## How it behaves after the fix - Canceling a pending call ends it with `E_ABORT`, as before. - When the canceled work finishes later, its result is discarded. The title's next call on the same block isn't touched and completes with its own result. - Calls that aren't canceled complete exactly as before. ## Tests The new test `a_canceled_pending_call_leaves_the_next_call_alone` follows the sequence above. Each call's work waits on a channel until the test lets it finish: 1. It begins and cancels call A, then begins call B on the same block. 2. It lets A's work finish. After 200 ms, B must still be pending. 3. It lets B's work finish. B must complete with its own result (`[9]`). I ran `cargo test --workspace` on GitHub's `windows-latest` with Rust 1.98.1 from `rust-toolchain.toml`: - **This PR:** everything passes. `victory-hle` has 87 passed, the other crates have 0 failed. - **The new test alone on `main`:** it fails at step 2. B's status is `S_OK` (`left: 0`) instead of `E_PENDING` (`right: -2147483638`, i.e. `0x8000000A`): A's late completion ended B. - I ran with `--test-threads=1`. With 4 threads, the shader crate's GPU tests sometimes crash on that runner (`STATUS_ACCESS_VIOLATION`), on unmodified `main` as well. **Not tested:** a title canceling its text entry. Related: #122 fixes the same pattern ("complete the call that finished, not whatever call the block holds now") for work scheduled with `XAsyncSchedule`. This PR doesn't depend on it, and the two merge cleanly in either order. They also merge cleanly with #114 and #132.
pending_with runs its work (a text-entry dialog) on a thread of its own
and lets the call complete when the work returns. Canceling ends the
call at once: the call is completed with E_ABORT and its state freed,
while the work goes on. When it returned, the thread wrote the result
into the freed state and completed the block, which by then might hold
the title's next call: that call ended with the old call's result.

The thread now shares the state (an Arc) instead of borrowing it, and
completes only its own call, while it is still the block's.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:55:55 +00:00
Author
Owner

@⁠marquezinii on GitHub · 2026-11-24 04:22 UTC · original

Reviewed f0393dd. Sharing the state through an Arc does remove the use-after-free, and Arc::ptr_eq cannot be fooled by a reused address because the thread keeps mine alive. cargo test -p victory-hle --lib passes on Windows 11.

One window is left open. The worker checks that the block still holds its own call, and then XAsyncComplete(block, hr, len) looks the call up again by block address. If the title cancels call A and begins call B on the same block between those two lookups, A's late result completes B, which is the case the PR sets out to prevent. With a 100 ms sleep inserted between the check and the complete (to widen the window, not committed), a test that cancels A and begins B meanwhile fails 5 of 5: B's status is S_OK instead of E_PENDING. Without the sleep the window is tiny, so the normal tests do not show it.

A way to close it: complete through the Arc<Call> the worker captured (an internal complete_call(block, &call, hr, len) that returns early when that call's done is already set), so no second lookup by address happens. Cancel sets done on A, so A's late result is dropped.

**@⁠marquezinii** on GitHub · 2026-11-24 04:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/236#issuecomment-7121959913) Reviewed `f0393dd`. Sharing the state through an `Arc` does remove the use-after-free, and `Arc::ptr_eq` cannot be fooled by a reused address because the thread keeps `mine` alive. `cargo test -p victory-hle --lib` passes on Windows 11. One window is left open. The worker checks that the block still holds its own call, and then `XAsyncComplete(block, hr, len)` looks the call up again by block address. If the title cancels call A and begins call B on the same block between those two lookups, A's late result completes B, which is the case the PR sets out to prevent. With a 100 ms sleep inserted between the check and the complete (to widen the window, not committed), a test that cancels A and begins B meanwhile fails 5 of 5: B's status is `S_OK` instead of `E_PENDING`. Without the sleep the window is tiny, so the normal tests do not show it. A way to close it: complete through the `Arc<Call>` the worker captured (an internal `complete_call(block, &call, hr, len)` that returns early when that call's `done` is already set), so no second lookup by address happens. Cancel sets `done` on A, so A's late result is dropped.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:22 UTC · original

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-hle/src/modules/xasync.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/236#issuecomment-7148447758) <!-- victory-triage v1 head=38bf927807fd7df475c6f743682acab825acdb4e verdict=needs-rebase at=2026-11-25T00:13:20Z --> Automated triage for the maintainers. **Needs rebase.** This PR does not merge cleanly into current `main` (ac2525d). Conflicting files: `crates/victory-hle/src/modules/xasync.rs`. The content has not been reviewed yet; it will be once the branch is rebased onto `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request has changes conflicting with the target branch.
  • crates/victory-hle/src/modules/xasync.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/236/head:github-bot/gh-236'
git switch 'github-bot/gh-236'

Merge

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