fix(libc): publish the guest FILE status of a stream with atomic accesses #9223

Merged
xXPBSXx merged 1 commit from upstream-pr/9223 into main 2026-10-22 18:24:21 +00:00
xXPBSXx commented 2026-10-21 15:33:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T15:33:26Z.


What

FileStream::SyncStatus and SyncUnlockedStatus wrote readRemaining, writeRemaining and the flags word of the guest FILE after the stdio call without the FILE lock, so two threads on the same stream race, and the read-modify-write of flags can drop an EOF or error bit. The status is now published through one helper, PublishStatus, which stores the two counters atomically and updates the flag word with a compare-exchange loop. The values written are unchanged; only the access is atomic. libc/stdio, but a thread-safety change only, no change of behaviour.

Fixes #9222

Tested

  • ThreadSanitizer (Ubuntu 24.04 on WSL2, GCC 13.3; a virtualised kernel, not bare-metal), guest_standard_streams (3 tests): 4 data race reports without the change, 0 with it, 3 of 3 pass. No new test: the plain build cannot observe the race; the existing guest_standard_streams test is the one that fails under the sanitizer.
  • Windows 10, MinGW-w64 GCC 15.2, Release: 45 of 45 pass in the stdio and file-stream family.
  • python tools/check_conventions.py --base upstream/main passes; git merge-tree against main is clean.
  • Not tested: Linux without sanitizer and macOS (the change uses GCC/Clang __atomic builtins).

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

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4954160772 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9223). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T15:33:26Z. --- ### What `FileStream::SyncStatus` and `SyncUnlockedStatus` wrote `readRemaining`, `writeRemaining` and the `flags` word of the guest FILE after the stdio call without the FILE lock, so two threads on the same stream race, and the read-modify-write of `flags` can drop an EOF or error bit. The status is now published through one helper, `PublishStatus`, which stores the two counters atomically and updates the flag word with a compare-exchange loop. The values written are unchanged; only the access is atomic. libc/stdio, but a thread-safety change only, no change of behaviour. Fixes #9222 ### Tested - ThreadSanitizer (Ubuntu 24.04 on WSL2, GCC 13.3; a virtualised kernel, not bare-metal), `guest_standard_streams` (3 tests): 4 data race reports without the change, 0 with it, 3 of 3 pass. No new test: the plain build cannot observe the race; the existing `guest_standard_streams` test is the one that fails under the sanitizer. - Windows 10, MinGW-w64 GCC 15.2, Release: 45 of 45 pass in the stdio and file-stream family. - `python tools/check_conventions.py --base upstream/main` passes; `git merge-tree` against `main` is clean. - Not tested: Linux without sanitizer and macOS (the change uses GCC/Clang `__atomic` builtins). ### 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](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 #9222 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
MarcZX10 commented 2026-10-21 22:54:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T22:54:05Z.

Review: APPROVED


Reviewed the focused FileStream change against the reported TSan race. Both status-sync paths now publish the counters atomically and update the shared flags with a compare-exchange loop, so concurrent status publications no longer race or overwrite unrelated flag bits. Initialization, reopen and close assignments remain lifecycle operations; this PR does not expand into other stream state. The author reports 3/3 guest_standard_streams TSan tests and 45/45 Windows stdio/file-stream tests; hosted eligibility, conventions, intake, labels, progress report, and all relinker/register-context/write-watch jobs pass, with Linux and Windows aggregate jobs skipped by the workflow. I checked the automated FileStream overlaps manually: the macOS PRs change only the feof_unlocked qualification in SyncUnlockedStatus, and archive PR #7447 changes lifecycle hooks; those hunks are distinct from this atomic publication fix.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5607208014 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9223#pullrequestreview-5607208014). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T22:54:05Z. **Review:** APPROVED --- Reviewed the focused `FileStream` change against the reported TSan race. Both status-sync paths now publish the counters atomically and update the shared flags with a compare-exchange loop, so concurrent status publications no longer race or overwrite unrelated flag bits. Initialization, reopen and close assignments remain lifecycle operations; this PR does not expand into other stream state. The author reports 3/3 `guest_standard_streams` TSan tests and 45/45 Windows stdio/file-stream tests; hosted eligibility, conventions, intake, labels, progress report, and all relinker/register-context/write-watch jobs pass, with Linux and Windows aggregate jobs skipped by the workflow. I checked the automated FileStream overlaps manually: the macOS PRs change only the `feof_unlocked` qualification in `SyncUnlockedStatus`, and archive PR #7447 changes lifecycle hooks; those hunks are distinct from this atomic publication fix.
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!9223
No description provided.