feat(libc): implement dprintf and vdprintf #12933
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12933"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by heditrigui on 2026-10-29T15:27:35Z.
What
This pull request implements
dprintfandvdprintf(NIDsEYmqRgFzU6Iandpr-qZYCHfTE) inlibc, following POSIX.1-2008 and FreeBSD 11.4dprintf.candvdprintf.c:dprintf&vdprintf:vdprintfformats output into a dynamically allocated guest buffer viavasprintf_nid_postfixand writes to the descriptor using a guest write hook registered bylibkernel(_write_nid_postfix), falling back to host write when unhooked.fflush.c.errno) across theApplicationHeapFree_nid_no_patchcleanup call.dprintfunpacks variadic arguments using SysV ABI va_list helpers on Windows and standardva_start/va_endon POSIX, delegating directly tovdprintf.-1witherrnoset toEBADF(9); null format strings return-1witherrnoset toEINVAL(22).docs/dev/debt/libc.mddescribing POSIX.1-2008 and FreeBSD 11.4 conformance, guest write hook backend, and unmeasured console behavior.Tested
python tools/check_conventions.py --base upstream/main; all conventions checks passed.python core/libs/tests/test_public_headers.py --audit-only; verified 373 libraries, 14,002 exports.core/libs/tests/GuestAllocatingStrings.cpptesting:static int APS5_VABIonCallVasprintfandCallVdprintf.open_nid_postfixwriting formatted string and readback verification viaread_nid_postfix.socketpair_nid_postfixverifying formatted transmission across virtual socket boundary.EBADF(9).EAGAIN).ENOMEM(12).Checklist
Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T16:39:47Z.
Review: CHANGES_REQUESTED
Re-review at
3dfc973fb8Thanks, the guest write hook, the short-write loop, saving errno and the
APS5_VABIhelpers cover points 1-4 from the earlier reviews. Two problems remain.1.
GuestAllocatingStrings.cppdoes not compile for Windows.GuestWriteHook_nid_no_patchisstd::int64_t (APS5_VABI *)(...), which meanssysv_abion Windows. The three test hooks are plain lambdas, and a plain lambda decays to an MS-ABI function pointer, so the conversion is rejected:CI stays green because the
windowshost-only lane doesn't select libc-linked test targets, the same reason the earlier SegFault was missed. Fix: put the attribute on each lambda.That goes on all three (the third one has an unnamed
std::size_t). With this change, mingw g++ 16.2.0 andclang++ -arch x86_64both report 0 errors for the test file, andAllocatingStrings.cppwas already clean on both.2. fd 99999 doesn't match the source the debt entry cites. The entry cites FreeBSD 11.4
vdprintf.c, and that function begins withif (fd > SHRT_MAX) { errno = EMFILE; return (EOF); }. The test instead assertsdprintf_nid_postfix(99999, "invalid") == -1with errno 9 (EBADF), which is what the host write path gives. There are two options. One is to add thefd > 32767→EMFILE(24) check and change that row. The other is to keep the current behaviour and have the debt line say it differs from the reference. In either case, nobody has measured the console value yet.Minor: when the descriptor is negative the function returns
EBADFbefore it formats anything, sodprintf(-1, nullptr)givesEBADFand notEINVAL. That's fine, but the debt line lists both rules, so it could state the order.AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T17:20:00Z.
Review: APPROVED
Re-review at
c33785b183Both points from my last review are fixed, so I'm approving.
APS5_VABIis now on all three test hooks. Running-fsyntax-onlyonGuestAllocatingStrings.cppandAllocatingStrings.cppgives 0 errors with bothx86_64-w64-mingw32-g++16.2.0 andclang++ -arch x86_64 -std=c++20(before this commit the test file had 3 errors under mingw).fd > SHRT_MAX→EMFILE(24) check now comes first, which matches the FreeBSD 11.4vdprintf.cthe debt entry cites, and the debt line gives the order. I copied the guard into a standalone macOS arm64 ASan+UBSan harness and ran the PR's rows (99999 → 24, 32767 → 9, -1 → 9). Head passes. Changing>to>=fails the 32767 row, and removing the guard fails the 99999 row, so the new rows kill both mutants. Swapping the order of the two descriptor checks is equivalent because the ranges don't overlap.Also checked:
git diff --checkandgit merge-treeagainstbb04a4f1eaare clean, andcheck_conventions --base upstream/mainpasses.One thing to keep in mind, not blocking: the 32767 →
EBADFrow assumes the test process has no descriptor 32767 open. That holds for this test binary.AI-assisted: yes (Claude Code).