HLE: wait for in-flight monitor callbacks in XTaskQueueUnregisterMonitor #3261

Open
fuzeofficlal wants to merge 1 commit from upstream-pr/3263 into main
fuzeofficlal commented 2026-11-26 14:58:33 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T14:58:33Z.


Summary

In crates/victory-hle/src/modules/xasync.rs, Queue::deliver cloned self.monitors out of the mutex and invoked callbacks without tracking active invocations, while XTaskQueueUnregisterMonitor only removed the token from the vector and returned immediately. Per the documented GDK XTaskQueueUnregisterMonitor contract, callers may free a monitor's context as soon as XTaskQueueUnregisterMonitor returns, which produced two use-after-free races:

  1. In-flight callback UAF: XTaskQueueUnregisterMonitor returned while another thread was still executing that monitor's callback, allowing the caller to free context underneath the running callback.
  2. Stale-snapshot sibling UAF: if a sibling monitor M2 was unregistered while an earlier monitor M1 was running, XTaskQueueUnregisterMonitor(q, M2) returned immediately (allowing M2's context to be freed), yet deliver still invoked M2 from its cloned snapshot once M1 returned.

Additionally, XTaskQueueRegisterMonitor published the monitor entry before writing *token, allowing an immediately triggered callback on another thread to race with the token write.

Changes

  • Replaced Queue::monitors with Monitors { entries: Mutex<Vec<Monitor>>, done: Condvar } in crates/victory-hle/src/modules/xasync.rs:
    • Monitors::notify snapshots active tokens (keeping a zero-allocation fast path when empty), re-checks under the lock right before each invocation that the token is still registered (skipping monitors unregistered mid-notification), records the active calling ThreadId, and clears it via a Drop guard that removes the entry and wakes waiters once removed && running.is_empty().
    • Monitors::unregister marks the entry removed, removes it immediately when idle, avoids self-deadlock (and dual-thread concurrent self-unregister deadlock) when called from a thread already inside that monitor callback, and otherwise blocks on Condvar until every in-flight invocation has returned.
    • XTaskQueueRegisterMonitor writes *token before registering the entry under the mutex so the release/acquire edge publishes the token before any callback can run.
  • Added two deterministic regression tests in crates/victory-hle/src/modules/xasync.rs:
    • unregister_monitor_waits_for_in_flight_callbacks (reproduces early return + context destruction during an in-flight callback on main, verifies blocking wait and suppression of future notifications after fix).
    • unregister_monitor_skips_unstarted_siblings_and_handles_reentrant_self_unregister (reproduces stale-snapshot sibling invocation on main, and verifies reentrant self-unregister via vtable slot 0xc0, nested submission, and dual-thread concurrent self-unregister racing an external waiter).

Verification

  • cargo test -p victory-hle --locked xasync: 15 passed, 0 failed (plus 10 consecutive stress runs of the new monitor concurrency tests: 20/20 passed).
  • cargo test -p victory-hle --locked and --all-features --locked: 156 passed, 0 failed each.
  • cargo clippy --workspace --all-targets --locked -- -D warnings (default and --all-features): clean.
  • cargo test -p xtask --locked policy::tests::repository_passes_policy: passed.
<!-- greyfinch-sync:Marvisak/VIctory:issue:5512327733 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3263). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T14:58:33Z. --- ### Summary In `crates/victory-hle/src/modules/xasync.rs`, `Queue::deliver` cloned `self.monitors` out of the mutex and invoked callbacks without tracking active invocations, while `XTaskQueueUnregisterMonitor` only removed the token from the vector and returned immediately. Per the documented GDK `XTaskQueueUnregisterMonitor` contract, callers may free a monitor's context as soon as `XTaskQueueUnregisterMonitor` returns, which produced two use-after-free races: 1. **In-flight callback UAF**: `XTaskQueueUnregisterMonitor` returned while another thread was still executing that monitor's callback, allowing the caller to free `context` underneath the running callback. 2. **Stale-snapshot sibling UAF**: if a sibling monitor `M2` was unregistered while an earlier monitor `M1` was running, `XTaskQueueUnregisterMonitor(q, M2)` returned immediately (allowing `M2`'s context to be freed), yet `deliver` still invoked `M2` from its cloned snapshot once `M1` returned. Additionally, `XTaskQueueRegisterMonitor` published the monitor entry before writing `*token`, allowing an immediately triggered callback on another thread to race with the token write. ### Changes - Replaced `Queue::monitors` with `Monitors { entries: Mutex<Vec<Monitor>>, done: Condvar }` in `crates/victory-hle/src/modules/xasync.rs`: - `Monitors::notify` snapshots active tokens (keeping a zero-allocation fast path when empty), re-checks under the lock right before each invocation that the token is still registered (skipping monitors unregistered mid-notification), records the active calling `ThreadId`, and clears it via a `Drop` guard that removes the entry and wakes waiters once `removed && running.is_empty()`. - `Monitors::unregister` marks the entry removed, removes it immediately when idle, avoids self-deadlock (and dual-thread concurrent self-unregister deadlock) when called from a thread already inside that monitor callback, and otherwise blocks on `Condvar` until every in-flight invocation has returned. - `XTaskQueueRegisterMonitor` writes `*token` before registering the entry under the mutex so the release/acquire edge publishes the token before any callback can run. - Added two deterministic regression tests in `crates/victory-hle/src/modules/xasync.rs`: - `unregister_monitor_waits_for_in_flight_callbacks` (reproduces early return + context destruction during an in-flight callback on `main`, verifies blocking wait and suppression of future notifications after fix). - `unregister_monitor_skips_unstarted_siblings_and_handles_reentrant_self_unregister` (reproduces stale-snapshot sibling invocation on `main`, and verifies reentrant self-unregister via vtable slot `0xc0`, nested submission, and dual-thread concurrent self-unregister racing an external waiter). ### Verification - `cargo test -p victory-hle --locked xasync`: 15 passed, 0 failed (plus 10 consecutive stress runs of the new monitor concurrency tests: 20/20 passed). - `cargo test -p victory-hle --locked` and `--all-features --locked`: 156 passed, 0 failed each. - `cargo clippy --workspace --all-targets --locked -- -D warnings` (default and `--all-features`): clean. - `cargo test -p xtask --locked policy::tests::repository_passes_policy`: passed.
Dukestar69 commented 2026-11-26 15:09:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T15:09:49Z.

Review: APPROVED


Reviewed the diff and verified locally on Windows 11 x64:

  • Tracking active monitor callback threads with an RAII drop guard + Condvar wait in Monitors::unregister cleanly ensures XTaskQueueUnregisterMonitor waits for in-flight external callbacks while avoiding self-deadlock on reentrant self-unregistration and skipping unstarted sibling monitors.
  • cargo test -p victory-hle --lib modules::xasync:: -- --test-threads=1: 15 passed, 0 failed (including unregister_monitor_waits_for_in_flight_callbacks and unregister_monitor_skips_unstarted_siblings_and_handles_reentrant_self_unregister).
  • 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).

<!-- greyfinch-sync:Marvisak/VIctory:review:6076502665 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3263#pullrequestreview-6076502665). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T15:09:49Z. **Review:** APPROVED --- Reviewed the diff and verified locally on Windows 11 x64: - Tracking active monitor callback threads with an RAII drop guard + `Condvar` wait in `Monitors::unregister` cleanly ensures `XTaskQueueUnregisterMonitor` waits for in-flight external callbacks while avoiding self-deadlock on reentrant self-unregistration and skipping unstarted sibling monitors. - `cargo test -p victory-hle --lib modules::xasync:: -- --test-threads=1`: 15 passed, 0 failed (including `unregister_monitor_waits_for_in_flight_callbacks` and `unregister_monitor_skips_unstarted_siblings_and_handles_reentrant_self_unregister`). - `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).
Jackster commented 2026-11-26 22:52:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Jackster on 2026-11-26T22:52:34Z.


Windows 11, 63bcca4 + this PR:

  • cargo test -p victory-hle: lib 154 passed / 0 failed (unpatched 63bcca4 here: 152 passed; +2), plus the integration suite (1 passed).
  • cargo clippy -p victory-hle --all-targets -- -D warnings: clean.

The two new tests are unregister_monitor_waits_for_in_flight_callbacks and unregister_monitor_skips_unstarted_siblings_and_handles_reentrant_self_unregister; the re-entrant self-unregister case completes without deadlock on this machine. Synthetic only. AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7205060045 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3263#issuecomment-7205060045). Originally posted by [Jackster](https://github.com/Jackster) on 2026-11-26T22:52:34Z. --- Windows 11, `63bcca4` + this PR: - `cargo test -p victory-hle`: lib **154 passed / 0 failed** (unpatched `63bcca4` here: 152 passed; +2), plus the integration suite (1 passed). - `cargo clippy -p victory-hle --all-targets -- -D warnings`: clean. The two new tests are `unregister_monitor_waits_for_in_flight_callbacks` and `unregister_monitor_skips_unstarted_siblings_and_handles_reentrant_self_unregister`; the re-entrant self-unregister case completes without deadlock on this machine. Synthetic only. AI-assisted.
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/3263:upstream-pr/3263'
git switch 'upstream-pr/3263'

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/3263'
git switch 'upstream-pr/3263'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3263'
git switch 'upstream-pr/3263'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3263'
git switch 'main'
git merge --squash 'upstream-pr/3263'
git switch 'main'
git merge --ff-only 'upstream-pr/3263'
git switch 'main'
git merge 'upstream-pr/3263'
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!3261
No description provided.