HLE: retain pending_with state across cancellation and prevent stale block completions #2522

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

@⁠reeledMarek on GitHub · 2026-11-25 15:16 UTC · original
reeledMarek:fix/hle-xasync-pending-uaf → main · +122 −42 · 1 files · 1 commits · open

Problem

xasync::pending_with stored its shared closure/result buffer in a raw Box<Data> freed in OP_CLEANUP, while spawning a background thread that held the raw pointer and completed the call via XAsyncComplete(block, ...).

If the caller cancels the block with XAsyncCancel before the worker thread finishes:

  1. XAsyncCancel immediately triggers OP_CLEANUP, freeing Data while the worker still holds the pointer — causing a heap use-after-free when the worker writes its result bytes.
  2. XAsyncComplete(block, ...) matches by block address rather than the original Arc<Call>, so if the caller reuses the block for a new async call after cancellation, the late worker completes the unrelated new call.

Fix

  • Own Data in an Arc<Data> so OP_BEGIN clones a strong reference for the worker thread, keeping the state alive even if OP_CLEANUP runs on cancellation.
  • Capture the in-flight Arc<Call> during OP_BEGIN and complete via complete_call only when is_current(&current, block) is still true.

Testing

  • Added synthetic unit test canceled_pending_with_keeps_state_alive_and_leaves_reused_block_alone in crates/victory-hle/src/modules/xasync.rs.
  • Verified cargo test -p xtask --locked passes asset policy.
**@⁠reeledMarek** on GitHub · 2026-11-25 15:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/2522) `reeledMarek:fix/hle-xasync-pending-uaf` → `main` · +122 −42 · 1 files · 1 commits · open ### Problem `xasync::pending_with` stored its shared closure/result buffer in a raw `Box<Data>` freed in `OP_CLEANUP`, while spawning a background thread that held the raw pointer and completed the call via `XAsyncComplete(block, ...)`. If the caller cancels the block with `XAsyncCancel` before the worker thread finishes: 1. `XAsyncCancel` immediately triggers `OP_CLEANUP`, freeing `Data` while the worker still holds the pointer — causing a heap use-after-free when the worker writes its result bytes. 2. `XAsyncComplete(block, ...)` matches by block address rather than the original `Arc<Call>`, so if the caller reuses the block for a new async call after cancellation, the late worker completes the unrelated new call. ### Fix - Own `Data` in an `Arc<Data>` so `OP_BEGIN` clones a strong reference for the worker thread, keeping the state alive even if `OP_CLEANUP` runs on cancellation. - Capture the in-flight `Arc<Call>` during `OP_BEGIN` and complete via `complete_call` only when `is_current(&current, block)` is still true. ### Testing - Added synthetic unit test `canceled_pending_with_keeps_state_alive_and_leaves_reused_block_alone` in `crates/victory-hle/src/modules/xasync.rs`. - Verified `cargo test -p xtask --locked` passes asset policy.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:08:07 +00:00
github-bot force-pushed github-bot/gh-2522 from d27a5c1fa9 to 76f4d98999 2026-11-26 06:31:11 +00:00 Compare
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 15:52 UTC · original · edited 2026-11-26 07:22 UTC

Automated triage for the maintainers.

Merge candidate. Thanks for the rebase. pending_with in crates/victory-hle/src/modules/xasync.rs now keeps its state in an Arc. OP_BEGIN hands the worker its own strong reference, so a cancel that runs OP_CLEANUP first can no longer free the state under the worker. The worker completes through the Arc<Call> it captured (is_current + complete_call), not XAsyncComplete by block address, so a late worker leaves a reused block's new call alone. That fits main's XAsyncCancel → complete_call(E_ABORT) → release path. The new test covers both cancel-then-reuse cases.

CI: check passing; merges cleanly.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 15:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/2522#issuecomment-7167392368) · edited 2026-11-26 07:22 UTC <!-- victory-triage v1 head=76f4d9899929eb9d0e91d7d756647ceab9b97b6c verdict=merge-candidate at=2026-11-26T07:22:27Z --> Automated triage for the maintainers. **Merge candidate.** Thanks for the rebase. `pending_with` in `crates/victory-hle/src/modules/xasync.rs` now keeps its state in an `Arc`. `OP_BEGIN` hands the worker its own strong reference, so a cancel that runs `OP_CLEANUP` first can no longer free the state under the worker. The worker completes through the `Arc<Call>` it captured (`is_current` + `complete_call`), not `XAsyncComplete` by block address, so a late worker leaves a reused block's new call alone. That fits `main`'s `XAsyncCancel` → `complete_call(E_ABORT)` → `release` path. The new test covers both cancel-then-reuse cases. CI: `check` passing; merges cleanly. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠reeledMarek on GitHub · 2026-11-26 05:56 UTC · original

Rebased cleanly onto current main (63bcca4, resolving the test-append conflict with 30a5202).

**@⁠reeledMarek** on GitHub · 2026-11-26 05:56 UTC · [original](https://github.com/Marvisak/VIctory/pull/2522#issuecomment-7183936354) Rebased cleanly onto current `main` (`63bcca4`, resolving the test-append conflict with `30a5202`).
github-bot force-pushed github-bot/gh-2522 from 76f4d98999 to 367cd009b6 2026-11-26 11:11:16 +00:00 Compare
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 09:34 UTC · original · requested changes

Verified 76f4d989 locally on Windows 11 x64 (git merge-tree onto 63bcca43 is clean):

  • cargo clippy -p victory-hle --all-targets --locked -- -D warnings: clean (9.46s).
  • cargo test -p xtask --locked policy::tests::repository_passes_policy: 1 passed, 0 failed (0.56s).
  • cargo test -p victory-hle --locked: 155 passed, 0 failed (153 lib including all 14 modules::xasync::tests + 1 fidelityfx + 1 no_dll_planting, 4.53s).

The Arc<Data> worker reference fixes the lifetime once OP_BEGIN has run, but two startup race conditions remain between XAsyncBegin (xasync.rs:604-606) and OP_BEGIN (xasync.rs:855-870):

  1. Startup context UAF (xasync.rs:558-563, 604-606, 855-865): XAsyncBegin publishes call into CALLS (:604) before calling provider(OP_BEGIN) (:606). If XAsyncCancel runs in that window, release invokes OP_CLEANUP and drops the sole Arc<Data> reference before OP_BEGIN calls Arc::increment_strong_count(state).
  2. Startup Call identity race (xasync.rs:604-606, 866-883): because OP_BEGIN re-looks up call(data.block) by raw block address, a cancel + block-reuse in that startup window can cause current to bind to the new Call instead of the canceled one.

Please keep the provider context alive across OP_BEGIN startup and bind the worker directly to the initiating Arc<Call> (and synchronize on the old worker's completion checkpoint instead of sleep(50ms) in the reuse test).

**@⁠fuzeofficlal** on GitHub · 2026-11-26 09:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/2522#pullrequestreview-6071625685) · **requested changes** Verified `76f4d989` locally on Windows 11 x64 (`git merge-tree` onto `63bcca43` is clean): - `cargo clippy -p victory-hle --all-targets --locked -- -D warnings`: clean (`9.46s`). - `cargo test -p xtask --locked policy::tests::repository_passes_policy`: 1 passed, 0 failed (`0.56s`). - `cargo test -p victory-hle --locked`: 155 passed, 0 failed (`153` lib including all 14 `modules::xasync::tests` + `1` fidelityfx + `1` no_dll_planting, `4.53s`). The `Arc<Data>` worker reference fixes the lifetime once `OP_BEGIN` has run, but two startup race conditions remain between `XAsyncBegin` (`xasync.rs:604-606`) and `OP_BEGIN` (`xasync.rs:855-870`): 1. **Startup context UAF (`xasync.rs:558-563, 604-606, 855-865`)**: `XAsyncBegin` publishes `call` into `CALLS` (`:604`) before calling `provider(OP_BEGIN)` (`:606`). If `XAsyncCancel` runs in that window, `release` invokes `OP_CLEANUP` and drops the sole `Arc<Data>` reference before `OP_BEGIN` calls `Arc::increment_strong_count(state)`. 2. **Startup `Call` identity race (`xasync.rs:604-606, 866-883`)**: because `OP_BEGIN` re-looks up `call(data.block)` by raw block address, a cancel + block-reuse in that startup window can cause `current` to bind to the *new* `Call` instead of the canceled one. Please keep the provider context alive across `OP_BEGIN` startup and bind the worker directly to the initiating `Arc<Call>` (and synchronize on the old worker's completion checkpoint instead of `sleep(50ms)` in the reuse test).
Author
Owner

@⁠reeledMarek on GitHub · 2026-11-26 10:37 UTC · original

Thanks @⁠fuzeofficlal — addressed both startup races and the test synchronization:

  • Factored call registration into register_call returning the newly registered Arc<Call>.
  • In pending_with (via pending_with_handle), cloned worker_state = Arc::clone(&state) before publishing the call into CALLS and bound the spawned worker directly to the returned Arc<Call>, eliminating both the startup OP_CLEANUP UAF window and the call(data.block) re-lookup identity race.
  • Replaced the sleep(50ms) in canceled_pending_with_keeps_state_alive_and_leaves_reused_block_alone with first_worker.unwrap().join().unwrap() to deterministically synchronize after the old worker's full completion path.
**@⁠reeledMarek** on GitHub · 2026-11-26 10:37 UTC · [original](https://github.com/Marvisak/VIctory/pull/2522#issuecomment-7189614723) Thanks @⁠fuzeofficlal — addressed both startup races and the test synchronization: - Factored call registration into `register_call` returning the newly registered `Arc<Call>`. - In `pending_with` (via `pending_with_handle`), cloned `worker_state = Arc::clone(&state)` *before* publishing the call into `CALLS` and bound the spawned worker directly to the returned `Arc<Call>`, eliminating both the startup `OP_CLEANUP` UAF window and the `call(data.block)` re-lookup identity race. - Replaced the `sleep(50ms)` in `canceled_pending_with_keeps_state_alive_and_leaves_reused_block_alone` with `first_worker.unwrap().join().unwrap()` to deterministically synchronize after the old worker's full completion path.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 11:01 UTC · original · approved these changes

Reviewed 367cd009 and confirmed both startup/reuse races from the previous round are cleanly fixed: pending_with_handle clones worker_state before publishing the call through register_call, binds the worker thread directly to its Arc<Call> via complete_call instead of re-looking up the block address in CALLS, and joins both worker handles deterministically in canceled_pending_with_keeps_state_alive_and_leaves_reused_block_alone.

Verified locally on Windows 11 x64 (367cd009):

  • cargo run --locked -p xtask -- policy (191 files ok) and cargo clippy --workspace --all-targets -- -D warnings (default and --all-features): clean.
  • cargo test -p victory-hle --lib modules::xasync: 14 passed, 0 failed.
  • cargo test -p victory-hle --all-targets: 153 lib + 2 integration tests passed, 0 failed.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 11:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/2522#pullrequestreview-6072967645) · **approved these changes** Reviewed `367cd009` and confirmed both startup/reuse races from the previous round are cleanly fixed: `pending_with_handle` clones `worker_state` before publishing the call through `register_call`, binds the worker thread directly to its `Arc<Call>` via `complete_call` instead of re-looking up the block address in `CALLS`, and joins both worker handles deterministically in `canceled_pending_with_keeps_state_alive_and_leaves_reused_block_alone`. Verified locally on Windows 11 x64 (`367cd009`): - `cargo run --locked -p xtask -- policy` (`191 files ok`) and `cargo clippy --workspace --all-targets -- -D warnings` (default and `--all-features`): clean. - `cargo test -p victory-hle --lib modules::xasync`: 14 passed, 0 failed. - `cargo test -p victory-hle --all-targets`: 153 lib + 2 integration tests passed, 0 failed.
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/2522/head:github-bot/gh-2522'
git switch 'github-bot/gh-2522'

Merge

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