fix(libc): open the app0 archive without holding its mutex across path resolution #10100

Merged
xXPBSXx merged 1 commit from upstream-pr/10100 into main 2026-11-02 08:00:08 +00:00
xXPBSXx commented 2026-10-23 09:01:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-23T09:01:32Z.


What

Resolve /app0 before taking the archive mutex in Opened() (core/libs/prx/libc/src/AppArchive.cpp) and re-check opened under it, so the archive mutex is never held while the resolver mutex is acquired. Two threads that race on the first opening both resolve the path; the second one sees opened and returns.

Fixes: #10099

Tested

  • Linux ThreadSanitizer (Ubuntu 24.04 under WSL2: a virtualised kernel, not bare-metal; GCC 13.3, -fsanitize=thread, setarch x86_64 -R), the 16 tests that reported the cycle on main 21554447d0: before 16 lock-order-inversion warnings and 16 failing tests; with this change 0 lock-order-inversion warnings and 1 failing test (guest_avplayer, FFmpeg-internal data races, unrelated).
  • Also checked by a reviewer on bare-metal Linux (Ubuntu 26.04 x86-64, i7-8700, not a VM; GCC 15.2, -DANYPS5_ENABLE_TSAN=ON, RelWithDebInfo) at this head merged onto main c3ee5a425: the archive-to-resolver cycle was reported in 16 tests of the non-GPU suite on main and in 10 of 10 repeated runs of guest_kernel_sys, guest_filesystem and guest_realpath; with this PR those three tests show 0 reports in 10 runs and no lock-order-inversion report is left in any of the 16 tests. guest_posix_spawn still stops on its deliberate SIGBUS, which TSan treats as fatal on main too. The reviewer merged it together with #10810 and #11125 for the run.
  • Windows 10, MinGW-w64 GCC 15.2, Release: guest_app_archive_present, guest_kernel_app_archive_*, guest_kernel_sys, guest_filesystem, guest_access, guest_realpath: 11 of 11 pass.
  • check_conventions --base upstream/main passes; merge-tree against upstream/main and #7447 is clean.
  • Not tested: macOS. No ordinary test fails without the change: the cycle only shows under ThreadSanitizer.

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 #10099
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4980378716 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10100). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-23T09:01:32Z. --- ### What Resolve `/app0` before taking the archive mutex in `Opened()` (core/libs/prx/libc/src/AppArchive.cpp) and re-check `opened` under it, so the archive mutex is never held while the resolver mutex is acquired. Two threads that race on the first opening both resolve the path; the second one sees `opened` and returns. Fixes: #10099 ### Tested - Linux ThreadSanitizer (Ubuntu 24.04 under WSL2: a virtualised kernel, not bare-metal; GCC 13.3, `-fsanitize=thread`, `setarch x86_64 -R`), the 16 tests that reported the cycle on main 21554447d0: before 16 lock-order-inversion warnings and 16 failing tests; with this change 0 lock-order-inversion warnings and 1 failing test (guest_avplayer, FFmpeg-internal data races, unrelated). - Also checked by a reviewer on bare-metal Linux (Ubuntu 26.04 x86-64, i7-8700, not a VM; GCC 15.2, `-DANYPS5_ENABLE_TSAN=ON`, RelWithDebInfo) at this head merged onto `main` c3ee5a425: the archive-to-resolver cycle was reported in 16 tests of the non-GPU suite on `main` and in 10 of 10 repeated runs of `guest_kernel_sys`, `guest_filesystem` and `guest_realpath`; with this PR those three tests show 0 reports in 10 runs and no `lock-order-inversion` report is left in any of the 16 tests. `guest_posix_spawn` still stops on its deliberate SIGBUS, which TSan treats as fatal on `main` too. The reviewer merged it together with #10810 and #11125 for the run. - Windows 10, MinGW-w64 GCC 15.2, Release: guest_app_archive_present, guest_kernel_app_archive_*, guest_kernel_sys, guest_filesystem, guest_access, guest_realpath: 11 of 11 pass. - `check_conventions --base upstream/main` passes; `merge-tree` against `upstream/main` and #7447 is clean. - Not tested: macOS. No ordinary test fails without the change: the cycle only shows under ThreadSanitizer. ### Checklist - [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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical 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 #10099 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-23 10:27:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T10:27:38Z.

Review: APPROVED


I reviewed exact head 88ad0a1440. Opened() now checks the initialized fast path under the archive mutex, releases it before resolving /app0, and rechecks under the mutex before publishing the archive state. That removes the archive→resolver edge without creating a double-initialization race. The cached archive state is process-lifetime, so returning its reference after releasing the lock remains consistent with the existing use pattern.

The linked issue’s ThreadSanitizer report shows the inversion across 16 tests on main; the PR reports zero inversion warnings after the change and 11/11 focused Windows tests. Exact-head conventions, diff-check and current-main merge check pass. I also compared open #8520: it changes archive enumeration code in the same source file but not Opened(), and the two heads merge cleanly. Hosted policy, relinker, ASan, register-context and write-watch checks pass; the full Linux/Windows product jobs are skipped for these paths. My local full-build configure stopped because SDL2’s submodule is uninitialized here and the SDL XRandR support is unavailable, so I did not run a local C++ test. No blocker found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5626993299 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10100#pullrequestreview-5626993299). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T10:27:38Z. **Review:** APPROVED --- I reviewed exact head 88ad0a1440fee9f6c125164f4e4a650e70ee4ff5. `Opened()` now checks the initialized fast path under the archive mutex, releases it before resolving `/app0`, and rechecks under the mutex before publishing the archive state. That removes the archive→resolver edge without creating a double-initialization race. The cached archive state is process-lifetime, so returning its reference after releasing the lock remains consistent with the existing use pattern. The linked issue’s ThreadSanitizer report shows the inversion across 16 tests on `main`; the PR reports zero inversion warnings after the change and 11/11 focused Windows tests. Exact-head conventions, diff-check and current-main merge check pass. I also compared open #8520: it changes archive enumeration code in the same source file but not `Opened()`, and the two heads merge cleanly. Hosted policy, relinker, ASan, register-context and write-watch checks pass; the full Linux/Windows product jobs are skipped for these paths. My local full-build configure stopped because SDL2’s submodule is uninitialized here and the SDL XRandR support is unavailable, so I did not run a local C++ test. No blocker found.
MarcZX10 commented 2026-10-23 23:47:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T23:47:35Z.

Review: APPROVED


Reviewed the full one-file change at 88ad0a1440fee9f6c125164f4e4a650e70ee4ff5 against #10099. The initial archive-mutex check is released before /app0 resolution, then the state is checked again under the mutex before initialization. This removes the archive-to-resolver lock nesting while keeping initialization serialized when multiple callers race.

The applicable hosted checks pass, including conventions and the register-context and relinker matrices. The full Linux and Windows product builds are skipped by the repository's PR workflow; I relied on the Linux ThreadSanitizer and Windows guest-test results reported in the PR description for those checks. I could not run the product tests locally because SDL2 development files are unavailable in this environment.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5635595080 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10100#pullrequestreview-5635595080). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T23:47:35Z. **Review:** APPROVED --- Reviewed the full one-file change at `88ad0a1440fee9f6c125164f4e4a650e70ee4ff5` against #10099. The initial archive-mutex check is released before `/app0` resolution, then the state is checked again under the mutex before initialization. This removes the archive-to-resolver lock nesting while keeping initialization serialized when multiple callers race. The applicable hosted checks pass, including conventions and the register-context and relinker matrices. The full Linux and Windows product builds are skipped by the repository's PR workflow; I relied on the Linux ThreadSanitizer and Windows guest-test results reported in the PR description for those checks. I could not run the product tests locally because SDL2 development files are unavailable in this environment.
jotitadev commented 2026-10-27 05:37:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T05:37:46Z.

Review: COMMENTED


ThreadSanitizer run of head 88ad0a144 on bare-metal Linux.

Setup: Ubuntu 26.04 x86-64 (i7-8700, not a VM), GCC 15.2, -DANYPS5_ENABLE_TSAN=ON (the option from #11179), RelWithDebInfo, TSAN_OPTIONS=suppressions=tools/tsan-suppressions.txt:second_deadlock_stack=1. Base: main c3ee5a425. This head merges onto it cleanly; AppArchive.cpp auto-merges around 0a64d23bd. I merged it together with #10810 and #11125 for the run. Those two touch other files and other reports.

Baseline (main c3ee5a425):

  • Full non-GPU suite (ctest -E "^agc|gpu|vulkan|video_out"): the archive→resolver cycle from #10099 is reported in 16 tests. Examples: guest_kernel_sys, guest_filesystem, guest_realpath, guest_access, guest_font, guest_dynamic_loader and guest_posix_spawn.
  • The cycle: Opened (AppArchive.cpp:38) holds the archive mutex while ResolveDiskPath takes the Directories() mutex (General.cpp:803). chdir → AppArchiveRefuseOnly → Opened takes them in the other order.
  • guest_kernel_sys, guest_filesystem and guest_realpath reported it in 10 of 10 repeated runs (30 of 30).

With this PR:

  • Same three tests, 10 repeated runs: 0 reports.
  • Full non-GPU suite: no lock-order-inversion report left in any of the 16 tests. All of them pass, except guest_posix_spawn, whose child raises SIGBUS on purpose, which TSan treats as fatal on main too.
  • No new report in any other test.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5662691497 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10100#pullrequestreview-5662691497). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-27T05:37:46Z. **Review:** COMMENTED --- ThreadSanitizer run of head `88ad0a144` on bare-metal Linux. **Setup:** Ubuntu 26.04 x86-64 (i7-8700, not a VM), GCC 15.2, `-DANYPS5_ENABLE_TSAN=ON` (the option from #11179), RelWithDebInfo, `TSAN_OPTIONS=suppressions=tools/tsan-suppressions.txt:second_deadlock_stack=1`. Base: `main` `c3ee5a425`. This head merges onto it cleanly; `AppArchive.cpp` auto-merges around 0a64d23bd. I merged it together with #10810 and #11125 for the run. Those two touch other files and other reports. **Baseline (`main` `c3ee5a425`):** - Full non-GPU suite (`ctest -E "^agc|gpu|vulkan|video_out"`): the archive→resolver cycle from #10099 is reported in 16 tests. Examples: `guest_kernel_sys`, `guest_filesystem`, `guest_realpath`, `guest_access`, `guest_font`, `guest_dynamic_loader` and `guest_posix_spawn`. - The cycle: `Opened` (AppArchive.cpp:38) holds the archive mutex while `ResolveDiskPath` takes the `Directories()` mutex (General.cpp:803). `chdir` → `AppArchiveRefuseOnly` → `Opened` takes them in the other order. - `guest_kernel_sys`, `guest_filesystem` and `guest_realpath` reported it in 10 of 10 repeated runs (30 of 30). **With this PR:** - Same three tests, 10 repeated runs: 0 reports. - Full non-GPU suite: no `lock-order-inversion` report left in any of the 16 tests. All of them pass, except `guest_posix_spawn`, whose child raises SIGBUS on purpose, which TSan treats as fatal on `main` too. - No new report in any other test. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
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!10100
No description provided.