Skip to content

fix(libc): tell read-only and write-only streams apart on macOS so wrong-mode getc and putc keep errno - #28991

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
magnub:fix/libc-macos-stream-mode-errno
Dec 4, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
magnub:fix/libc-macos-stream-mode-errno

Conversation

@magnub

@magnub magnub commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

What

Follow-up to #22696. It made fgetc, fputc and their unlocked forms leave errno alone when the stream has the wrong mode, as the console does, by asking FileStream whether the stream was opened for reading or writing. OpenForReading, OpenForWriting and the mode check in SwitchToReadingUnlocked read the UCRT flags on Windows and __freadable/__fwritable on glibc, but answer "yes" on every other host. On macOS a wrong-mode fputc or fgetc therefore reached the host fputc/fgetc, which sets EBADF, and file_streams and guest_filesystem fail ("fputc after fgetc on a read-only stream", then "fgetc on a write-only stream").

On macOS the three checks now read the BSD stream flags the host FILE already exposes (__SRD, __SWR, __SRW; FileStream already reads __SNBF there), so a read-only or write-only stream fails the wrong-mode call before the host call, as on Windows and glibc.

Tested

Apple M1 Pro, macOS 27.0.1, Apple clang 21.0.0, x86_64 build under Rosetta 2, on main 2c43b61 with the macOS build fixes #11049, #26739, #27739, #27767, #27843 and #27639 merged locally so that main builds. libc, libkernel and libSceLibcInternal were rebuilt, since all three include FileStream.hpp.

  • file_streams: aborts on main ("fputc after fgetc on a read-only stream"; with only the write check fixed, "fgetc on a write-only stream"); passes 3/3 with this change.
  • guest_filesystem: aborts on main; passes 3/3 with this change.
  • Tests matching stream|stdio|file|printf|scanf|wchar|getc|putc|ungetc|fopen|popen (203): 3 fail on main, 1 with this change; file_streams and guest_filesystem are fixed and nothing new fails. The remaining failure, guest_host_streams, fails the same way with and without this change.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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

🤖 Generated with Claude Code

https://claude.ai/code/session_012LL9hfx4BY6MbUCYMduDAQ

Summary by CodeRabbit

  • Bug Fixes
    • File streams on Apple platforms now correctly recognize whether they support reading or writing, including streams opened for both.
    • Attempts to switch a stream to reading when it does not support reading now report an error and update its status instead of succeeding silently.

@coderabbitai

coderabbitai Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Apple stream mode handling

Layer / File(s) Summary
Apple stream access checks and read switching
core/libs/prx/libc/include/FileStream.hpp
OpenForReading and OpenForWriting check Apple stream flags for read and write capability. SwitchToReadingUnlocked returns false for unreadable streams, optionally sets errno to 9, marks guestError, and synchronizes status. For readable streams, it returns true without changing the stream mode.

Possibly related PRs

  • boykopovar/AnyPS5#22696: Changes wrong-mode getc and putc paths to preserve errno using the stream-switch functions.

Suggested reviewers: privacyonion, xxpbsxx, marczx10

Change: Bug fix

Merge Risk: Minimal · up to f1b3e

This change makes wrong-mode reads and writes on macOS streams fail early with the expected error, matching Windows and glibc. No concrete merge-blocking risk was found.

Security architecture risk: Minimal · up to f1b3e

On macOS, FileStream now rejects wrong-mode getc calls without changing the caller’s errno. The putc functions still rely on native stdio to reject the write and now preserve the caller’s errno on a wrong-mode failure. The change does not grant access to new streams or broaden what callers can read or write.

Security review details

Security Blast Radius

  • observed — The changed checks act on the native FILE already associated with the stream passed to libc. A read denial prevents I/O on that handle; it does not select a different handle or add a file-opening path.

Trust Boundaries and Controls

  • observed — The capability decision uses host FILE state rather than guest-visible status flags. It is a stream-mode check, not a new filesystem authorization control.

Dependencies

  • observed — FileStream now depends on Apple’s native FILE flags to identify read and write capability, accepting the update-mode bit in both checks. This uses the existing native handle rather than a separate copy of its mode.

Data and control flow

  • observed — Unlike the read path, fputc and putc_unlocked still invoke native stdio on macOS. The writability check decides whether to translate the host errno; it does not prevent the host call or replace the existing failed-write cleanup.

Resilience and Maintainability Implications

  • observed — A wrong-mode read refusal publishes a guest stream error without consuming native input. clearerr clears the guest error and native status, but it does not change the stream’s access mode: a repeated wrong-mode read fails again.

Rollout and rollback

  • observed — In an updated macOS build, FileStream checks readability when StandardStreams attempts a read switch and stops the read on refusal; native stdio remains responsible for rejecting wrong-mode writes. Neither decision is an operator-configured prerequisite.
Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 05b8ca9a-ad65-49c3-82c5-5157309ff407
Commits

Reviewing files that changed from the base of the PR and between 5cbfc40 and f1b3e97.

Files selected for processing: 1

  • core/libs/prx/libc/include/FileStream.hpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@github-actions github-actions Bot added the libc label Dec 4, 2026
@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 12682 core/libs/prx/libc/include/FileStream.hpp
PR 12688 core/libs/prx/libc/include/FileStream.hpp
PR 12700 core/libs/prx/libc/include/FileStream.hpp
PR 12965 core/libs/prx/libc/include/FileStream.hpp

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

The change at head f1b3e9709be199bd35f609728f962a4928c2be88 checks Apple stream access flags before reading and preserves errno on wrong-mode calls.

What I ran

Read the changed file and relevant callers at the exact head above. No build or tests were run. The author’s test results were not independently verified.

env -u ANYPS5_READ_LOG python3 repo_read.py search 'OpenForReading'
env -u ANYPS5_READ_LOG python3 repo_read.py search 'OpenForWriting'
env -u ANYPS5_READ_LOG python3 repo_read.py search 'SwitchToReadingUnlocked'
env -u ANYPS5_READ_LOG python3 repo_read.py search '__SRW' core/libs/prx/libc
env -u ANYPS5_READ_LOG python3 repo_read.py cat core/libs/prx/libc/src/StandardStreams.cpp 100 150
env -u ANYPS5_READ_LOG python3 repo_read.py cat core/libs/prx/libc/src/StandardStreams.cpp 150 235
env -u ANYPS5_READ_LOG python3 repo_read.py cat core/libs/prx/libc/src/StandardStreams.cpp 325 395

Results

Check Result Note
Changed file and callers passed All reviewed files were read
Build and tests not run No verification run by reviewer

Negative control

Not run; no test was changed or executed.

Findings

Blocking

None.

Non-blocking

None.

Sources

Not verified

The author-reported macOS test results were not independently confirmed. No other files were excluded: core/libs/prx/libc/include/FileStream.hpp was the only file listed for review and was read in full.

To get this to a good state

Nothing is required.

@oneandonlydean
oneandonlydean merged commit 30458eb into boykopovar:main Dec 4, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants