fix(libc): FileStream::SyncStatus races when two threads use the same stdio stream (ThreadSanitizer: data race) #9222

Closed
opened 2026-10-21 15:33:10 +00:00 by xXPBSXx · 0 comments
xXPBSXx commented 2026-10-21 15:33:10 +00:00 (Migrated from github.com)
Contributor

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


FileStream::SyncStatus() and SyncUnlockedStatus() (core/libs/prx/libc/include/FileStream.hpp) update the guest-visible FILE prefix after a stdio call with plain writes: readRemaining = 0, writeRemaining = 0 and flags = (flags & ~0x60) | .... fputc_nid_postfix calls std::fputc (which locks the FILE) and then SyncStatus() after it has unlocked, so two threads writing to one stream run these writes concurrently, and the read-modify-write of flags can lose an update (for example an EOF or error bit).

Found with ThreadSanitizer (Ubuntu 24.04 WSL2, GCC 13.3, RelWithDebInfo, -fsanitize=thread, run with setarch -R, main 27fabb6b7): guest_standard_streams reports 4 data races at FileStream.hpp:205 and :207, from fputc_nid_postfix (StandardStreams.cpp:107) called by the test's writer thread (GuestStandardStreams.cpp:896) and by the main thread (:900). The same function is used by about 20 call sites in libc, so other tests report it too (SyncUnlockedStatus at :213 and :222).

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5963562934 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/9222). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T15:33:10Z. --- `FileStream::SyncStatus()` and `SyncUnlockedStatus()` (core/libs/prx/libc/include/FileStream.hpp) update the guest-visible `FILE` prefix after a stdio call with plain writes: `readRemaining = 0`, `writeRemaining = 0` and `flags = (flags & ~0x60) | ...`. `fputc_nid_postfix` calls `std::fputc` (which locks the FILE) and then `SyncStatus()` after it has unlocked, so two threads writing to one stream run these writes concurrently, and the read-modify-write of `flags` can lose an update (for example an EOF or error bit). Found with ThreadSanitizer (Ubuntu 24.04 WSL2, GCC 13.3, RelWithDebInfo, `-fsanitize=thread`, run with `setarch -R`, `main` 27fabb6b7): `guest_standard_streams` reports 4 data races at FileStream.hpp:205 and :207, from `fputc_nid_postfix` (StandardStreams.cpp:107) called by the test's writer thread (GuestStandardStreams.cpp:896) and by the main thread (:900). The same function is used by about 20 call sites in libc, so other tests report it too (`SyncUnlockedStatus` at :213 and :222). AI-assisted (Claude Code).
Sign in to join this conversation.
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#9222
No description provided.