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

Open
reeledMarek wants to merge 1 commit from upstream-pr/2522 into main
reeledMarek commented 2026-11-25 15:16:36 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-25T15:16:36Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6607727021 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2522). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-25T15:16:36Z. --- ### 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.
Marvisak commented 2026-11-25 16:09:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15:52:43Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7167392368 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2522#issuecomment-7167392368). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T15:52:43Z. --- <!-- 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)_
reeledMarek commented 2026-11-26 06:03:50 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-26T05:56:00Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7183936354 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2522#issuecomment-7183936354). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-26T05:56:00Z. --- Rebased cleanly onto current `main` (`63bcca4`, resolving the test-append conflict with `30a5202`).
fuzeofficlal commented 2026-11-26 10:04:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T09:34:01Z.

Review: CHANGES_REQUESTED


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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6071625685 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2522#pullrequestreview-6071625685). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T09:34:01Z. **Review:** CHANGES_REQUESTED --- 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 commented 2026-11-26 11:03:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T11:01:29Z.

Review: APPROVED


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.
<!-- greyfinch-sync:Marvisak/VIctory:review:6072967645 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2522#pullrequestreview-6072967645). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T11:01:29Z. **Review:** APPROVED --- 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.
reeledMarek commented 2026-11-26 11:04:02 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by reeledMarek on 2026-11-26T10:37:35Z.


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.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7189614723 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2522#issuecomment-7189614723). Originally posted by [reeledMarek](https://github.com/reeledMarek) on 2026-11-26T10:37:35Z. --- 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.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2522:upstream-pr/2522'
git switch 'upstream-pr/2522'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2522'
git switch 'upstream-pr/2522'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2522'
git switch 'upstream-pr/2522'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2522'
git switch 'main'
git merge --squash 'upstream-pr/2522'
git switch 'main'
git merge --ff-only 'upstream-pr/2522'
git switch 'main'
git merge 'upstream-pr/2522'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
Marvisak/VIctory!2520
No description provided.