HLE: wait for in-flight lifecycle callbacks on unregister #3272

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

@⁠fuzeofficlal on GitHub · 2026-11-26 15:21 UTC · original
fuzeofficlal:fix/hle-lifecycle-unregister-sync → main · +442 −47 · 1 files · 1 commits · open

Summary

  • Synchronize UnregisterAppStateChangeNotification and UnregisterAppConstrainedChangeNotification with end_title / constrain_then_suspend so unregistering waits until any in-flight invocation of that registration on another thread has returned.
  • Stop cloning CONSTRAINED and ROUTINES upfront in end_title; dispatch each phase from live registrations and re-check membership before every callback so routines unregistered mid-dispatch (including suspend routines unregistered during the constrained phase) are skipped.
  • Track active (id, ThreadId) invocations with an unwind-safe RAII guard so same-thread self-unregistration never deadlocks while another thread concurrently unregistering the same registration still waits until the callback returns, and unregistering unrelated or zero registrations does not block behind other active callbacks.
  • Write *registration before publishing the entry in Notifications::register.

Root Cause

Under Windows Process State Manager (api-ms-win-core-psm-appnotify-*), returning from UnregisterAppStateChangeNotification or UnregisterAppConstrainedChangeNotification guarantees that the callback is neither executing on another thread nor will be invoked again, allowing the title to destroy the callback's context immediately afterward.

Previously, end_title cloned both callback vectors before spawning the suspend thread, constrain_then_suspend iterated those stale snapshots without re-checking registration state, and both Unregister* exports only removed the entry from the Vec and returned immediately. This left two use-after-free paths during shutdown/teardown:

  1. Unregistering an in-flight callback returned before the background thread finished accessing context.
  2. Unregistering a not-yet-started sibling constrained callback or suspend callback (either from another thread or from an earlier constrained/suspend callback) did not prevent the stale snapshot from invoking it with freed context.

Verification

  • Reproduced both UAF paths deterministically as RED failures on origin/main (63bcca43) before the fix (returned_early=true, saw_destroyed=true and unregistered sibling constrained callback must not run).
  • Added regression tests covering in-flight wait for both state and constrained notifications, skipping unregistered intra-phase siblings and cross-phase suspend routines, same-thread self-unregistration combined with a second thread's concurrent wait, and non-blocking unregistration of zero/unrelated ids.
  • Verified after the final edit:
    • cargo test -p victory-hle --locked (155 unit + 2 integration tests passed)
    • cargo test -p victory-hle --all-features --locked (passed)
    • 20 consecutive rounds of the new concurrency tests (20/20 passed)
    • cargo clippy --workspace --all-targets --locked -- -D warnings (passed)
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings (passed)
    • cargo test -p xtask --locked policy::tests::repository_passes_policy (passed)
**@⁠fuzeofficlal** on GitHub · 2026-11-26 15:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/3272) `fuzeofficlal:fix/hle-lifecycle-unregister-sync` → `main` · +442 −47 · 1 files · 1 commits · open ## Summary - Synchronize `UnregisterAppStateChangeNotification` and `UnregisterAppConstrainedChangeNotification` with `end_title` / `constrain_then_suspend` so unregistering waits until any in-flight invocation of that registration on another thread has returned. - Stop cloning `CONSTRAINED` and `ROUTINES` upfront in `end_title`; dispatch each phase from live registrations and re-check membership before every callback so routines unregistered mid-dispatch (including suspend routines unregistered during the constrained phase) are skipped. - Track active `(id, ThreadId)` invocations with an unwind-safe RAII guard so same-thread self-unregistration never deadlocks while another thread concurrently unregistering the same registration still waits until the callback returns, and unregistering unrelated or zero registrations does not block behind other active callbacks. - Write `*registration` before publishing the entry in `Notifications::register`. ## Root Cause Under Windows Process State Manager (`api-ms-win-core-psm-appnotify-*`), returning from `UnregisterAppStateChangeNotification` or `UnregisterAppConstrainedChangeNotification` guarantees that the callback is neither executing on another thread nor will be invoked again, allowing the title to destroy the callback's `context` immediately afterward. Previously, `end_title` cloned both callback vectors before spawning the suspend thread, `constrain_then_suspend` iterated those stale snapshots without re-checking registration state, and both `Unregister*` exports only removed the entry from the `Vec` and returned immediately. This left two use-after-free paths during shutdown/teardown: 1. Unregistering an in-flight callback returned before the background thread finished accessing `context`. 2. Unregistering a not-yet-started sibling constrained callback or suspend callback (either from another thread or from an earlier constrained/suspend callback) did not prevent the stale snapshot from invoking it with freed `context`. ## Verification - Reproduced both UAF paths deterministically as RED failures on `origin/main` (`63bcca43`) before the fix (`returned_early=true, saw_destroyed=true` and `unregistered sibling constrained callback must not run`). - Added regression tests covering in-flight wait for both state and constrained notifications, skipping unregistered intra-phase siblings and cross-phase suspend routines, same-thread self-unregistration combined with a second thread's concurrent wait, and non-blocking unregistration of zero/unrelated ids. - Verified after the final edit: - `cargo test -p victory-hle --locked` (155 unit + 2 integration tests passed) - `cargo test -p victory-hle --all-features --locked` (passed) - 20 consecutive rounds of the new concurrency tests (20/20 passed) - `cargo clippy --workspace --all-targets --locked -- -D warnings` (passed) - `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` (passed) - `cargo test -p xtask --locked policy::tests::repository_passes_policy` (passed)
UnregisterAppStateChangeNotification and UnregisterAppConstrainedChangeNotification previously only removed the registration from the vector while end_title dispatched cloned snapshots on a background thread. A title freeing callback context immediately after unregistering could hit a heap use-after-free from either an in-flight invocation or a stale snapshot entry not yet started (including suspend callbacks unregistered during the constrained phase).

Track registered entries and in-flight (id, ThreadId) invocations behind a shared Notifications mutex+condvar, re-checking each entry before invoking it and waiting in unregister for any in-flight call on another thread to return via an unwind-safe RAII guard while allowing same-thread self-unregistration without deadlocking.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 15:25:18 +00:00
Author
Owner

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

Reviewed the diff and verified locally on Windows 11 x64:

  • Encapsulating lifecycle callbacks in Notifications (Mutex + Condvar + active (id, ThreadId) tracking), writing the registration handle before publishing, waiting for in-flight external callbacks on unregister, and avoiding self-deadlock on reentrant self-unregistration cleanly matches Windows PSM semantics.
  • cargo test -p victory-hle --lib modules::lifecycle:: -- --test-threads=1: 15 passed, 0 failed (including unregister_waits_for_in_flight_lifecycle_callbacks, unregister_skips_unstarted_sibling_and_suspend_callbacks, and self_unregister_does_not_deadlock_while_other_thread_waits_and_unrelated_returns).
  • 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:27 UTC · [original](https://github.com/Marvisak/VIctory/pull/3272#pullrequestreview-6076753927) · **approved these changes** Reviewed the diff and verified locally on Windows 11 x64: - Encapsulating lifecycle callbacks in `Notifications` (`Mutex` + `Condvar` + active `(id, ThreadId)` tracking), writing the registration handle before publishing, waiting for in-flight external callbacks on `unregister`, and avoiding self-deadlock on reentrant self-unregistration cleanly matches Windows PSM semantics. - `cargo test -p victory-hle --lib modules::lifecycle:: -- --test-threads=1`: 15 passed, 0 failed (including `unregister_waits_for_in_flight_lifecycle_callbacks`, `unregister_skips_unstarted_sibling_and_suspend_callbacks`, and `self_unregister_does_not_deadlock_while_other_thread_waits_and_unrelated_returns`). - `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/3272/head:github-bot/gh-3272'
git switch 'github-bot/gh-3272'

Merge

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