Skip to content

fix(libc): match title printf behavior on Windows - #3987

Merged
adriadam10 merged 7 commits into
boykopovar:mainfrom
MarcZX10:codex/carry-windows-format-freebsd-3975
Oct 24, 2026
Merged

adriadam10 merged 7 commits into
boykopovar:mainfrom
MarcZX10:codex/carry-windows-format-freebsd-3975

Conversation

@MarcZX10

@MarcZX10 MarcZX10 commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

What

On Windows, match the measured title libc.prx Dinkumware _Printf behavior for %q, %D, %O, %U, and unknown conversion characters. These are emitted as literal text; unknown conversions keep the leading %, use the measured width and - behavior, and discard length modifiers. Width handling for %% remains covered. A trailing % reads beyond the format NUL in the measured binaries, so this implementation throws instead of reproducing that undefined over-read. Unknown conversions with other flags or precision still throw because those cases were not measured.

The probe in #3975 loaded the decrypted libc.prx from PPSA13934, PPSA07923, PPSA05916 and PPSA04158 and called each module's snprintf export. All four returned byte-identical results for the tested formats.

The overlap scan found adjacent test insertions with #7386 in Formatting.cpp; the wide-scanf and printf checks are independent, and I proposed landing this small test block first so #7386 can rebase while keeping its tests. Other shared formatter/debt files belong to separate open work on argument positions (#9552), float conversions (#9699, #9916, #9927 and #9958), and UTF-16 string conversion (#9939); I reviewed their changed hunks, and this change does not depend on those implementations. Preserve each PR's test and debt entries when rebasing.

Tested

  • Linux/GCC 12: a focused harness called FormatWindows directly and passed the measured literal, width, length-modifier and incomplete-format cases.
  • MinGW-w64 GCC 12 POSIX: the focused harness compiled for Windows; it was not run on Windows.
  • python3 tools/check_conventions.py --base upstream/main --head HEAD passed; git diff --check passed.
  • The project guest_formatting test was not run on Windows for this head.

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

@LeandroLP23

Copy link
Copy Markdown
Contributor

Windows run (Windows 11 26300, WinLibs GCC 15.2.0 r7, Release, full build):

main + #4021 alone and main + #4021 + #3982 both pass guest_formatting, so the failure comes from this PR's test, not from #3982.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

CI status note: all reported checks are passing except register-context (ubuntu-latest, Release), which remains marked pending. Its Actions job record shows conclusion: success and a completed_at timestamp while still reporting status: in_progress; I found no failing test output. Linux and Windows full-suite jobs are skipped by the workflow.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Needs a rebase: main formats %1$d and %'d on Windows since #3569 and #3567, so the throw loop added to Formatting.cpp fails there (LeandroLP23's run above). Which title prints %q, %D/%O/%U or an unknown conversion? Without one I'd keep the throw.

@MarcZX10
MarcZX10 force-pushed the codex/carry-windows-format-freebsd-3975 branch from 4b5a478 to 36276a0 Compare October 12, 2026 17:00
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 12, 2026
@MarcZX10
MarcZX10 force-pushed the codex/carry-windows-format-freebsd-3975 branch from 36276a0 to 6fa2daa Compare October 12, 2026 18:36
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 12, 2026
@MarcZX10
MarcZX10 force-pushed the codex/carry-windows-format-freebsd-3975 branch from 6fa2daa to 308d006 Compare October 12, 2026 18:47
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 12, 2026
@MarcZX10
MarcZX10 force-pushed the codex/carry-windows-format-freebsd-3975 branch from 308d006 to e5b256f Compare October 13, 2026 03:41
@MarcZX10

Copy link
Copy Markdown
Contributor Author

Rebased onto current main and addressed the Windows test failure: %1$d and %'d have since become supported by #3569 and #3567, so I removed them from the unsupported-format throw list. The carried implementation commits retain Hussein Kadhim as author; the follow-up test commit is by MarcZX100. Validation on Linux/GCC 12.2 Release: guest_formatting_tests built and guest_formatting passed (1/1); conventions, test registration, and git diff --check pass. The Windows-only formatting cases still need a hosted/Windows run.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

I do not have a title log showing a call with %q, %D, %O, %U, or another conversion that the PR newly rejects. The current evidence only compares the formatter with FreeBSD behavior. I will not claim that this establishes console behavior; if title or console evidence is required for the change, I agree it is still missing.

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the rebase. Two things before this can go in:

  • Same question as @oneandonlydean's, still open: no title or console evidence that %q, %D/%O/%U, an unknown conversion or a trailing % is ever printed. Until then the guest aborting is the safer behaviour than a plausible-but-wrong output.
  • The only source is FreeBSD 11.4 vfprintf.c, and the assumption in the TechnicalDebt line that the console takes these from the same code is unchecked. The console's libc.prx exports Dinkumware's _Printf / _Printf.percent, whose handling of unknown conversions and of q can differ (e.g. printing the whole %y spec rather than just y). Disassembling _Printf in a title's libc.prx and checking those four cases would settle it; then the code and the expected values in the test should follow that.

Also, the current head hasn't had a Windows run (the earlier ones were before the rebase); guest_formatting only exercises these cases on Windows.

@MarcZX10
MarcZX10 force-pushed the codex/carry-windows-format-freebsd-3975 branch from be146cb to 26761c6 Compare October 19, 2026 15:09
@MarcZX10

Copy link
Copy Markdown
Contributor Author

Rebased onto current main at 00d7e611ef and corrected the debt entry to describe this as Windows host-side behavior; the title libc.prx behavior remains unverified. All four original implementation/test commits are preserved with their original authors, followed by a conflict-resolution commit under my account. The current head passes git diff --check and the conventions checker. I could not run the Windows-only tests here; CMake configuration is blocked by the missing SDL2 CMake source and XRandR development support. The unsupported-conversion semantics remain under review pending target-module evidence.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 19, 2026
@MarcZX10

Copy link
Copy Markdown
Contributor Author

I do not have the requested title libc.prx disassembly or a console-side _Printf run for %q, %D/%O/%U, unknown conversions, and trailing %; the libSceLibcInternal.sprx probe does not resolve that gap. I also have not run the Windows-only guest_formatting cases on the rebased head. I agree those are still blockers, so I am not treating this PR as merge-ready or claiming the behavior matches the console. I will leave the implementation unchanged until target-specific evidence is available.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

Thanks for the clarification and for the firmware 12.70 payload measurement. Since it exercises libSceLibcInternal.sprx, it does not establish how a title’s libc.prx _Printf handles these conversions. I’ll treat it as a useful separate data point, not evidence for this PR; the unsupported %q, %D/%O/%U, unknown-conversion, and trailing-percent behavior remains unverified for the title module. The reviewer’s requested title trace or _Printf inspection is still needed before changing the current throw behavior.

@MarcZX10

Copy link
Copy Markdown
Contributor Author

Updated the PR after the title-module probe in #3975 and rebased onto current main (11d6efa06). The probe loads the decrypted libc.prx shipped by PPSA13934, PPSA07923, PPSA05916 and PPSA04158 and calls each module's snprintf export, so it directly covers the formatter this PR changes. The implementation and regression expectations now follow those measured literal outputs and width/length behavior. A trailing % over-reads in the probe and is treated as undefined; this change throws instead of reproducing that read.

The focused direct FormatWindows harness passes on Linux/GCC 12, and MinGW-w64 GCC 12 compiles it for Windows. The full Windows guest_formatting executable has not been run on this head. The hosted conventions and labels checks are pending; the PR body records the exact checks and overlap review.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
@fogydes

fogydes commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

I built the branch's WindowsFormatting.hpp (head d2186b5e2a) with MinGW g++ 15.2.0 on Windows myself and called LibcDetail::FormatWindows directly with a hand-seeded SysV VaList (no consuming conversions in these formats, so the arg area is a fixed pattern). [verified by build+run on this machine]

Results, against the new asserts in core/libs/tests/Formatting.cpp:371–374:

format test expects measured
"%qd|%qu|%qx|%qo" == 15 15, output %qd|%qu|%qx|%qo — match
"%D|%hD|%O|%U|%jD" == 19 14, output %D|%D|%O|%U|%D — length assert fails deterministically
"[%5%][%-5%][%y][%5y][%-3y][%hhz]" == 34 34, exact expected string — match

The == 19 at Formatting.cpp:373 looks like a counting slip: the asserted output itself ("%D|%D|%O|%U|%D") is 14 characters, and snprintf-style return = untruncated length = 14. One-character fix: == 19 → == 14. On Windows this test currently fails as written; worth fixing before the _WIN32 CI run is trusted. (The dispatcher the test exercises is LibcDetail::FormatWindows at Formatting.cpp:412, the _WIN32 path.) [verified structurally + by my run; the branch's own hostname-tagged Windows run is author-reported]

Otherwise the unknown-conversion branch behaves as the debt entry says: widths/- preserved, length modifiers discarded, %+y/trailing % throw, 100%% literal (all matched in the same run). Envelope: 183 commits behind base 11d6efa06c, ahead 6, merges cleanly against 1e20650e84 (git merge-tree exit 0); main has churned libc formatting since, but not these conversion paths. [verified]

husseinkad and others added 7 commits October 24, 2026 10:22
…s on Windows

The Windows printf family threw on the q length modifier, the D, O and U
conversions, an unknown conversion character and a format ending in a percent
sign, where FreeBSD's vfprintf formats the first three and prints the unknown
character itself or stops at the end of the format.

(cherry picked from commit 0e3b289)
…Windows does not

The unknown-conversion branch printed %m, positional arguments, the grouping
flag and modifiers in an unusual order as literal characters. Only characters
FreeBSD's vfprintf prints as themselves are now printed.

(cherry picked from commit 0106858)
@adriadam10
adriadam10 merged commit dca72a8 into boykopovar:main Oct 24, 2026
17 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the new branches of FormatWindows against the libc.prx measurements in #3975 (PPSA13934, PPSA07923, PPSA05916, PPSA04158): %q/%D/%O/%U literal, unknown conversions keep %, width and - ([ %y], [%y ]), length modifiers dropped (%hhz -> %z), %5% padded; trailing %, other flags and precision still throw. The expected lengths in the new test match those rows (the == 19 slip reported by @fogydes was already corrected to 14); the Windows build covers guest_formatting; merged.

orezraey added a commit to orezraey/AnyPS5 that referenced this pull request Oct 24, 2026
Keep boykopovar#3987's q, D, O, U, width, '-' and trailing '%' handling for
unknown conversions and fold it into this branch's measured path:
'#' and precision are ignored as measured, '+', space, apostrophe
and '0' with a width still throw.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants