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

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

@⁠fuzeofficlal on GitHub · 2026-11-26 14:58 UTC · original
fuzeofficlal:fix/hle-xtaskqueue-unregister-monitor-sync → main · +422 −11 · 1 files · 1 commits · open

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.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 14:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/3263) `fuzeofficlal:fix/hle-xtaskqueue-unregister-monitor-sync` → `main` · +422 −11 · 1 files · 1 commits · open ### 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.
Queue::deliver previously cloned the monitor list out of its mutex and invoked callbacks without tracking active invocations, while XTaskQueueUnregisterMonitor only removed the token from the list and returned immediately. Under the documented GDK contract, a title may free a monitor's context as soon as XTaskQueueUnregisterMonitor returns, causing (1) heap use-after-free when an in-flight monitor callback on another thread is still running, and (2) heap use-after-free when a sibling monitor unregistered mid-notification is still invoked from a stale snapshot.

Track per-monitor removal and active thread frames inside Monitors { entries, done }:
- notify() snapshots registered tokens, re-verifies each token is still active right before invoking it (skipping monitors unregistered meanwhile), and uses a drop guard to clear the active thread entry and wake waiters even on unwind.
- unregister() marks the monitor removed so no new invocation can start, deletes it immediately when idle, skips blocking when called reentrantly from a thread currently inside that same monitor callback (avoiding self-deadlock and dual-thread self-unregister deadlock), and otherwise blocks on a Condvar until every in-flight invocation has returned.
- XTaskQueueRegisterMonitor writes *token before publishing the monitor under the mutex so any immediately invoked callback observes its own token race-free.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 15:02:25 +00:00
Author
Owner

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

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

**@⁠Dukestar69** on GitHub · 2026-11-26 15:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/3263#pullrequestreview-6076502665) · **approved these changes** 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).
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/3263/head:github-bot/gh-3263'
git switch 'github-bot/gh-3263'

Merge

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