Skip to content

fix(libc): translate host write errors in fprintf, fwprintf and fseeko as fputs and fflush do - #2066

Open
Thimo188 wants to merge 1 commit into
mainfrom
upstream-pr/28286
Open

Thimo188 wants to merge 1 commit into
mainfrom
upstream-pr/28286

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#28286, originally opened by mnoori11.

Source commit: 06143fae93b17f4d270f45c7b4a52fce095aca65. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


What

When the host write behind a guest stream fails with EAGAIN on Linux, fputs reports 35 (FreeBSD's EAGAIN) but fprintf on the same stream reports 11, which a title reads as FreeBSD's EDEADLK (errno.h:59-60, :92). These writes pass the host errno through untranslated:

  • vfprintf (fprintf, vfprintf): the std::fwrite at Formatting.cpp:745 (Windows) and :751, and the host std::vfprintf at :757.
  • WriteSecure (fprintf_s, vfprintf_s, printf_s, vprintf_s): Formatting.cpp:1051.
  • wprintf, vfwprintf (fwprintf, vwprintf) and vfwprintf_s (fwprintf_s, wprintf_s, vwprintf_s): FormattingWide.cpp:501, :522, :555.
  • fseeko (and fseek, rewind, fsetpos): FileAndHeap.cpp:1206 maps only EOVERFLOW, so a failed flush of pending output before the seek keeps the host number.

FreeBSD 11.4 returns EOF from vfprintf and vfwprintf with the errno that write(2) set in __sflush (vfprintf.c:1043-1044, vfwprintf.c:1101-1102, fflush.c:125-137), and _fseeko flushes a writing stream before it seeks (fseek.c:169-172, :283-286).

Each of the formatted writes now follows fputs, fwrite and fflush: it saves errno and zeroes it before the host fwrite or vfprintf, then sets GuestErrnoFromHost_nid_no_patch of what the host set, or restores the saved value if the host set nothing (one small helper per file). The other stream writes already translate this way: fputs, fputc, fwrite and fflush (boykopovar#14559), dprintf (boykopovar#15912), fputwc and fputws (boykopovar#16913), fclose (boykopovar#25515) and the ungetc/ungetwc flush (boykopovar#25593). In fseeko the EOVERFLOW special case becomes the general mapping, which still gives 84 for EOVERFLOW. Guest values set in place of a write (EILSEQ in the wide catches, the closed-stream EBADF, fseeko's target checks) are not translated. No title is known to hit this; the case for the change is the siblings above. In practice it is EAGAIN on a non-blocking pipe or socket stream, or EDQUOT.

Side effects: the host vfprintf path also maps Linux EOVERFLOW (75) for output above INT_MAX to 84, and fseeko maps a host errno the table does not know to EIO (5), as fputs does, where main passed it through. In-tree callers of these functions (libSceLibcInternal's __vfprintf, err/warn, and fseek users in other libraries) see the translated values too.

Neighbours: our boykopovar#25024 changes only PrintWindows and says "translating the host errno in the printf family is a separate change"; this is that change for the stream functions, and printf, vprintf and puts are left for after boykopovar#25024. boykopovar#11332 (conflicting) adds a count < 0 return just above the narrow fwrite lines, so a textual conflict is likely. boykopovar#24406 (conflicting) adds IsBadDescriptor checks at the top of vfprintf, fseeko and other stream functions. boykopovar#22047 adds a closed-stream check at the top of fprintf. boykopovar#24377 changes the macOS branch of WritingStream, which ResolveSeekTarget calls for SEEK_END.

Tested

Linux (WSL2, x86-64), GCC, full build. file_streams gains five entries in the pipe writer cases (fprintf with %s, which takes the host vfprintf, fprintf with %.0f, which is guest-formatted, fprintf_s, fwprintf, fwprintf_s): each fails on a full non-blocking pipe with errno 35 and the error flag set, then writes once the pipe is drained. Two new blocks check that fseeko(SEEK_SET) and rewind with output pending on a full pipe give errno 35. The existing recovery step of the writer cases checks that a successful write leaves a preset errno unchanged.

  • main with the new test: file_streams fails. A probe calling the same functions on main's libc prints errno=11 for all seven (fprintf %s, fprintf %.0f, fprintf_s, fwprintf, fwprintf_s, fseeko, rewind).
  • This PR: file_streams, guest_formatting and guest_wide_formatting pass, and the probe prints errno=35 for all seven. Full ctest: 7,117 of 7,312 pass; the 195 failures are GPU and environment tests that fail the same way on main on this machine (WSL2: no GPU, so Vulkan falls back to llvmpipe, and the 6.6 kernel lacks asynchronous userfaultfd write protection).
  • Not run on Windows or macOS. No test covers wprintf to stdout or the Windows vfprintf branch. In CI the new cases run only in the Linux job, on pushes and manual runs (build.yml line 516). On a pull request the Windows job builds and runs the changed core/libs tests (lines 746, 934-937), where the pipe section of file_streams is compiled out (#ifndef _WIN32). The macOS jobs build the relinker only (line 354) or their own test projects.

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

Summary by CodeRabbit

  • Bug Fixes
    • Improved error reporting when file-seeking operations fail, so errors are consistently represented in the expected environment.
    • Improved error handling for formatted text output in narrow and wide character modes, including secure variants. Failed writes now report the appropriate error, while successful operations preserve existing error state.
  • Tests
    • Added coverage for failed and recovered output operations and file-seeking behavior, including checks for error state preservation.

…o as fputs and fflush do

When the host write behind fprintf, fprintf_s, fwprintf and their v and
_s forms failed, or fseeko could not flush pending output, the host
errno reached the guest as it was: 11 for EAGAIN on Linux, which is
EDEADLK in the guest numbering, where fputs, fwrite and fflush report
35. These calls now translate it the same way.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Dec 2, 2026

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Stream errno handling

Layer / File(s) Summary
Formatted output error handling
core/libs/prx/libc/src/Formatting.cpp, core/libs/prx/libc/src/FormattingWide.cpp, core/libs/tests/FileStreams.cpp
Narrow and wide formatted output use write helpers that check complete writes and translate nonzero host errors to guest errno. Tests cover full-pipe write failures and post-drain recovery for formatted output APIs.
Seek error translation
core/libs/prx/libc/src/FileAndHeap.cpp, core/libs/tests/FileStreams.cpp
fseeko_nid_postfix maps failed host seeks to guest errno. Tests check fseeko failure, the stream error indicator, and errno after rewind.

Change: Bug fix

Merge Risk: Minimal · up to 06143

This change makes formatted output and failed seeks report translated guest error numbers, consistent with the existing write functions. No concrete merge-blocking risk was found.

Security architecture risk: Minimal · up to 06143

AnyPS5 libc now reports host write and seek failures using guest error codes. Guest software can recognize temporary backpressure as EAGAIN without gaining access to any new host stream or changing how streams are owned or closed.

Security review details

Security Blast Radius

  • observed — The patch changes what guest callers learn after I/O on an existing stream. Guest-controlled formatting inputs and seek arguments still reach the same host operations on the same stream; no new host resource is selected by the error translation.

Trust Boundaries and Controls

  • observed — The merge-base comparison preserves the existing stream-lifetime guard, seek locking, and formatting checks. Error translation happens after the host call and does not participate in deciding whether the operation can proceed.

Resilience and Maintainability Implications

  • observed — The new formatted write cases exercise failure on a full pipe followed by a caller-initiated write after draining it. They check that a successful write does not clear the sticky stream error. Error translation itself neither retries I/O nor guarantees that a failed write transferred no bytes.

Rollout and rollback

  • inferred — No operator action or authority change is required by the inspected change. Reverting to the merge-base implementation would restore the previous error reporting, not undo I/O that had already occurred. There is no persisted state migration in the patch.
Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 4
  • Waived: $1.00 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

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: dc435abe-b6c3-4f63-b337-9b8b37726500
Commits

Reviewing files that changed from the base of the PR and between 4131f51 and 06143fa.

Files selected for processing: 4

  • core/libs/prx/libc/src/FileAndHeap.cpp
  • core/libs/prx/libc/src/Formatting.cpp
  • core/libs/prx/libc/src/FormattingWide.cpp
  • core/libs/tests/FileStreams.cpp

Limit details: You’ve used all 10 included reviews currently available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants