fix(libSceHmdSetupDialog): keep a terminated dialog terminated during Open and Close #12931

Merged
XairRrR merged 1 commit from upstream-pr/12931 into main 2026-11-02 17:25:42 +00:00
XairRrR commented 2026-10-29 15:10:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T15:10:29Z.


What

sceHmdSetupDialogOpen and sceHmdSetupDialogClose read the dialog status, checked it, and then stored FINISHED in a separate step. If another thread called sceHmdSetupDialogTerminate between the read and the store, Terminate returned 0 and set the status to NONE, and then Open or Close wrote FINISHED over it. The library was left looking initialized after it had been terminated: sceHmdSetupDialogGetStatus returned FINISHED (3) instead of NONE (0), and the next sceHmdSetupDialogInitialize failed with ALREADY_INITIALIZED (0x80B80004), so the dialog could not be used again.

Open and Close now move the status to FINISHED with a compare-exchange loop that repeats the checks against the value it replaces. A concurrent Terminate either happens first, so the call returns NOT_INITIALIZED, or happens after, so the status stays at NONE. The single-threaded results are unchanged.

guest_hmd_setup_dialog gains a race check: for 200000 rounds each of Open and Close, it initializes the dialog, runs the call while a second thread terminates it, and requires the status to be NONE afterwards. Both threads yield while they wait for each other, so the check also finishes quickly on a single CPU.

Tested

Arch Linux 7.2.11-zen, GCC 16.2.1, Ninja, Release, Intel Core i9-14900KF.

  • Built guest_hmd_setup_dialog_tests and libs.
  • ctest --test-dir build -R '^guest_hmd_setup_dialog$': 1/1 passed (0.14 s). The test binary also passed 20 of 20 direct runs (about 0.1 s each) and 5 of 5 runs pinned to one CPU with taskset -c 0 (about 0.27 s each).
  • Negative control: with only core/libs/prx/libSceHmdSetupDialog/Export.cpp restored from main, the test failed with 1/1 Test #1985: guest_hmd_setup_dialog ...........Subprocess aborted***Exception: 0.08 sec (Require(sceHmdSetupDialogGetStatus() == kStatusNone) aborts in the race check). It failed 3 of 3 runs under ctest and 20 of 20 direct runs.
  • python3 tools/check_conventions.py --base origin/main: all conventions checks passed. git diff --check origin/main: clean. python3 tools/pr_overlap.py --branch auto/fix-libscehmdsetupdialog-terminate-race: no overlap with open pull requests.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5070557369 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12931). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-10-29T15:10:29Z. --- ### What `sceHmdSetupDialogOpen` and `sceHmdSetupDialogClose` read the dialog status, checked it, and then stored `FINISHED` in a separate step. If another thread called `sceHmdSetupDialogTerminate` between the read and the store, Terminate returned 0 and set the status to `NONE`, and then Open or Close wrote `FINISHED` over it. The library was left looking initialized after it had been terminated: `sceHmdSetupDialogGetStatus` returned `FINISHED` (3) instead of `NONE` (0), and the next `sceHmdSetupDialogInitialize` failed with `ALREADY_INITIALIZED` (0x80B80004), so the dialog could not be used again. Open and Close now move the status to `FINISHED` with a compare-exchange loop that repeats the checks against the value it replaces. A concurrent Terminate either happens first, so the call returns `NOT_INITIALIZED`, or happens after, so the status stays at `NONE`. The single-threaded results are unchanged. `guest_hmd_setup_dialog` gains a race check: for 200000 rounds each of Open and Close, it initializes the dialog, runs the call while a second thread terminates it, and requires the status to be `NONE` afterwards. Both threads yield while they wait for each other, so the check also finishes quickly on a single CPU. ### Tested Arch Linux 7.2.11-zen, GCC 16.2.1, Ninja, Release, Intel Core i9-14900KF. - Built `guest_hmd_setup_dialog_tests` and `libs`. - `ctest --test-dir build -R '^guest_hmd_setup_dialog$'`: 1/1 passed (0.14 s). The test binary also passed 20 of 20 direct runs (about 0.1 s each) and 5 of 5 runs pinned to one CPU with `taskset -c 0` (about 0.27 s each). - Negative control: with only `core/libs/prx/libSceHmdSetupDialog/Export.cpp` restored from `main`, the test failed with `1/1 Test #1985: guest_hmd_setup_dialog ...........Subprocess aborted***Exception: 0.08 sec` (`Require(sceHmdSetupDialogGetStatus() == kStatusNone)` aborts in the race check). It failed 3 of 3 runs under ctest and 20 of 20 direct runs. - `python3 tools/check_conventions.py --base origin/main`: all conventions checks passed. `git diff --check origin/main`: clean. `python3 tools/pr_overlap.py --branch auto/fix-libscehmdsetupdialog-terminate-race`: no overlap with open pull requests. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
rooklz commented 2026-10-29 17:27:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T17:27:39Z.

Review: COMMENTED


Review of dcf847e73e (macOS arm64, Apple M5, 10 cores, Apple clang)

The fix is correct. Open and Close now re-check NONE/RUNNING/null against the value the compare-exchange replaces, so a concurrent Terminate either goes first (the call returns NOT_INITIALIZED) or goes after (the status stays NONE). Single-threaded results are unchanged: the first iteration does the same checks in the same order on the same loaded value.

Gates: git diff --check clean; git merge-tree against main bb04a4f1ea clean; no other open PR touches libSceHmdSetupDialog.

Standalone build: the PR's verbatim Export.cpp plus the verbatim GuestHmdSetupDialog.cpp.

  • -O2: pass, 0.08 s.
  • ASan+UBSan: pass, 0.12 s.
  • TSan: pass with no reports, 1.2 s.

Negative control is weak on arm64 (non-blocking, but worth fixing). With main's Export.cpp, the race check fails only 1/40 runs at -O2 and 2/20 under ASan. Restoring only Open (open_old) fails 1/40, and restoring only Close (close_old) fails 0/40. So on this machine the Close half of the fix is effectively untested.

The cause: the main thread calls finish() once, right after round.store(i), and Open/Close usually complete before the terminator thread wakes from yield(). The read→store window is rarely hit.

Suggested change: keep calling finish() until Terminate has run. This is deterministic, because at least one call is guaranteed to overlap or follow the Terminate.

         Require(sceHmdSetupDialogInitialize() == 0);
         round.store(i);
-        finish();
-        while (done.load() != i) std::this_thread::yield();
+        while (done.load() != i) {
+            finish();
+            std::this_thread::yield();
+        }
         Require(sceHmdSetupDialogGetStatus() == kStatusNone);
Export.cpp variant PR test (-O2) with the suggestion (-O2) with the suggestion (ASan+UBSan)
main 1/40 fail 40/40 fail 20/20 fail
Open fix only reverted 1/40 40/40 —
Close fix only reverted 0/40 40/40 —
PR head 0/40 0/40 0/20

The PR head still takes 0.10 s at -O2 and 0.12 s under ASan. The loop still yields, so it stays safe on a single CPU. Even with kRaceRounds = 20000, all three mutants fail 40/40.

Other mutants (with the PR test):

  • Dropping the null check in the loop: killed.
  • compare_exchange_weak → strong: equivalent.
  • Dropping the in-loop RUNNING check: survives. Nothing sets RUNNING today, so this is unreachable.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5702135025 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12931#pullrequestreview-5702135025). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T17:27:39Z. **Review:** COMMENTED --- ### Review of dcf847e73e (macOS arm64, Apple M5, 10 cores, Apple clang) The fix is correct. Open and Close now re-check NONE/RUNNING/null against the value the compare-exchange replaces, so a concurrent Terminate either goes first (the call returns NOT_INITIALIZED) or goes after (the status stays NONE). Single-threaded results are unchanged: the first iteration does the same checks in the same order on the same loaded value. **Gates:** `git diff --check` clean; `git merge-tree` against `main` bb04a4f1ea clean; no other open PR touches libSceHmdSetupDialog. **Standalone build:** the PR's verbatim `Export.cpp` plus the verbatim `GuestHmdSetupDialog.cpp`. - `-O2`: pass, 0.08 s. - ASan+UBSan: pass, 0.12 s. - TSan: pass with no reports, 1.2 s. **Negative control is weak on arm64 (non-blocking, but worth fixing).** With `main`'s `Export.cpp`, the race check fails only **1/40** runs at `-O2` and **2/20** under ASan. Restoring only Open (`open_old`) fails 1/40, and restoring only Close (`close_old`) fails **0/40**. So on this machine the Close half of the fix is effectively untested. The cause: the main thread calls `finish()` once, right after `round.store(i)`, and Open/Close usually complete before the terminator thread wakes from `yield()`. The read→store window is rarely hit. Suggested change: keep calling `finish()` until Terminate has run. This is deterministic, because at least one call is guaranteed to overlap or follow the Terminate. ```diff Require(sceHmdSetupDialogInitialize() == 0); round.store(i); - finish(); - while (done.load() != i) std::this_thread::yield(); + while (done.load() != i) { + finish(); + std::this_thread::yield(); + } Require(sceHmdSetupDialogGetStatus() == kStatusNone); ``` | Export.cpp variant | PR test (-O2) | with the suggestion (-O2) | with the suggestion (ASan+UBSan) | |---|---|---|---| | main | 1/40 fail | **40/40 fail** | **20/20 fail** | | Open fix only reverted | 1/40 | **40/40** | — | | Close fix only reverted | 0/40 | **40/40** | — | | PR head | 0/40 | 0/40 | 0/20 | The PR head still takes 0.10 s at `-O2` and 0.12 s under ASan. The loop still yields, so it stays safe on a single CPU. Even with `kRaceRounds = 20000`, all three mutants fail 40/40. **Other mutants (with the PR test):** - Dropping the null check in the loop: killed. - `compare_exchange_weak` → `strong`: equivalent. - Dropping the in-loop RUNNING check: survives. Nothing sets RUNNING today, so this is unreachable.
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
boykopovar/AnyPS5!12931
No description provided.