feat(libc): implement dprintf and vdprintf #12933

Merged
heditrigui merged 3 commits from upstream-pr/12933 into main 2026-10-30 05:33:00 +00:00
heditrigui commented 2026-10-29 15:27:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by heditrigui on 2026-10-29T15:27:35Z.


What

This pull request implements dprintf and vdprintf (NIDs EYmqRgFzU6I and pr-qZYCHfTE) in libc, following POSIX.1-2008 and FreeBSD 11.4 dprintf.c and vdprintf.c:

  1. dprintf & vdprintf:
    • vdprintf formats output into a dynamically allocated guest buffer via vasprintf_nid_postfix and writes to the descriptor using a guest write hook registered by libkernel (_write_nid_postfix), falling back to host write when unhooked.
    • Retries after positive short writes until complete or an error occurs as in FreeBSD fflush.c.
    • Preserves the write error (errno) across the ApplicationHeapFree_nid_no_patch cleanup call.
    • dprintf unpacks variadic arguments using SysV ABI va_list helpers on Windows and standard va_start/va_end on POSIX, delegating directly to vdprintf.
    • File descriptors below 0 return -1 with errno set to EBADF (9); null format strings return -1 with errno set to EINVAL (22).
  2. Technical debt entry: Added technical debt documentation in docs/dev/debt/libc.md describing POSIX.1-2008 and FreeBSD 11.4 conformance, guest write hook backend, and unmeasured console behavior.

Tested

  • Conventions: Ran python tools/check_conventions.py --base upstream/main; all conventions checks passed.
  • Public headers audit: Ran python core/libs/tests/test_public_headers.py --audit-only; verified 373 libraries, 14,002 exports.
  • Unit tests: Extended core/libs/tests/GuestAllocatingStrings.cpp testing:
    • Fixed Windows test helper ABI declaration with static int APS5_VABI on CallVasprintf and CallVdprintf.
    • Descriptor from open_nid_postfix writing formatted string and readback verification via read_nid_postfix.
    • Connected guest socket pair via socketpair_nid_postfix verifying formatted transmission across virtual socket boundary.
    • Standard file descriptors 1 and 2 with empty string (0 bytes written).
    • Closed and out-of-range guest descriptors (99999) returning -1 with EBADF (9).
    • Short-write-then-success continuation.
    • Short-write-then-error stopping and returning -1 with preserved errno.
    • Non-blocking error preserving guest errno (35 / EAGAIN).
    • Heap allocation failure returning -1 with ENOMEM (12).

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5070841585 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12933). Originally posted by [heditrigui](https://github.com/heditrigui) on 2026-10-29T15:27:35Z. --- ### What This pull request implements `dprintf` and `vdprintf` (NIDs `EYmqRgFzU6I` and `pr-qZYCHfTE`) in `libc`, following POSIX.1-2008 and FreeBSD 11.4 `dprintf.c` and `vdprintf.c`: 1. **`dprintf` & `vdprintf`**: - `vdprintf` formats output into a dynamically allocated guest buffer via `vasprintf_nid_postfix` and writes to the descriptor using a guest write hook registered by `libkernel` (`_write_nid_postfix`), falling back to host write when unhooked. - Retries after positive short writes until complete or an error occurs as in FreeBSD `fflush.c`. - Preserves the write error (`errno`) across the `ApplicationHeapFree_nid_no_patch` cleanup call. - `dprintf` unpacks variadic arguments using SysV ABI va_list helpers on Windows and standard `va_start`/`va_end` on POSIX, delegating directly to `vdprintf`. - File descriptors below 0 return `-1` with `errno` set to `EBADF` (9); null format strings return `-1` with `errno` set to `EINVAL` (22). 2. **Technical debt entry**: Added technical debt documentation in `docs/dev/debt/libc.md` describing POSIX.1-2008 and FreeBSD 11.4 conformance, guest write hook backend, and unmeasured console behavior. ### Tested - **Conventions**: Ran `python tools/check_conventions.py --base upstream/main`; all conventions checks passed. - **Public headers audit**: Ran `python core/libs/tests/test_public_headers.py --audit-only`; verified 373 libraries, 14,002 exports. - **Unit tests**: Extended `core/libs/tests/GuestAllocatingStrings.cpp` testing: - Fixed Windows test helper ABI declaration with `static int APS5_VABI` on `CallVasprintf` and `CallVdprintf`. - Descriptor from `open_nid_postfix` writing formatted string and readback verification via `read_nid_postfix`. - Connected guest socket pair via `socketpair_nid_postfix` verifying formatted transmission across virtual socket boundary. - Standard file descriptors 1 and 2 with empty string (0 bytes written). - Closed and out-of-range guest descriptors (99999) returning -1 with `EBADF` (9). - Short-write-then-success continuation. - Short-write-then-error stopping and returning -1 with preserved errno. - Non-blocking error preserving guest errno (35 / `EAGAIN`). - Heap allocation failure returning -1 with `ENOMEM` (12). ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current main; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation .md files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
rooklz commented 2026-10-29 16:39:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T16:39:47Z.

Review: CHANGES_REQUESTED


Re-review at 3dfc973fb8

Thanks, the guest write hook, the short-write loop, saving errno and the APS5_VABI helpers cover points 1-4 from the earlier reviews. Two problems remain.

1. GuestAllocatingStrings.cpp does not compile for Windows. GuestWriteHook_nid_no_patch is std::int64_t (APS5_VABI *)(...), which means sysv_abi on Windows. The three test hooks are plain lambdas, and a plain lambda decays to an MS-ABI function pointer, so the conversion is rejected:

x86_64-w64-mingw32-g++ (16.2.0) -std=c++20 -fsyntax-only -Icore/libs -Icore core/libs/tests/GuestAllocatingStrings.cpp
GuestAllocatingStrings.cpp:232:40: error: invalid conversion from 'int64_t (*)(int, const void*, std::size_t)' to 'GuestWriteHook_nid_no_patch' {aka 'long long int (__attribute__((sysv_abi)) *)(int, const void*, long long unsigned int)'} [-fpermissive]
(same at :240 and :250)

CI stays green because the windows host-only lane doesn't select libc-linked test targets, the same reason the earlier SegFault was missed. Fix: put the attribute on each lambda.

-    LibcSetGuestWriteHook_nid_no_patch(+[](int, const void*, std::size_t count) -> std::int64_t {
+    LibcSetGuestWriteHook_nid_no_patch(+[](int, const void*, std::size_t count) APS5_VABI -> std::int64_t {

That goes on all three (the third one has an unnamed std::size_t). With this change, mingw g++ 16.2.0 and clang++ -arch x86_64 both report 0 errors for the test file, and AllocatingStrings.cpp was 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 with if (fd > SHRT_MAX) { errno = EMFILE; return (EOF); }. The test instead asserts dprintf_nid_postfix(99999, "invalid") == -1 with errno 9 (EBADF), which is what the host write path gives. There are two options. One is to add the fd > 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 EBADF before it formats anything, so dprintf(-1, nullptr) gives EBADF and not EINVAL. That's fine, but the debt line lists both rules, so it could state the order.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5701447775 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12933#pullrequestreview-5701447775). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T16:39:47Z. **Review:** CHANGES_REQUESTED --- ### Re-review at 3dfc973fb8 Thanks, the guest write hook, the short-write loop, saving errno and the `APS5_VABI` helpers cover points 1-4 from the earlier reviews. Two problems remain. **1. `GuestAllocatingStrings.cpp` does not compile for Windows.** `GuestWriteHook_nid_no_patch` is `std::int64_t (APS5_VABI *)(...)`, which means `sysv_abi` on Windows. The three test hooks are plain lambdas, and a plain lambda decays to an MS-ABI function pointer, so the conversion is rejected: ``` x86_64-w64-mingw32-g++ (16.2.0) -std=c++20 -fsyntax-only -Icore/libs -Icore core/libs/tests/GuestAllocatingStrings.cpp GuestAllocatingStrings.cpp:232:40: error: invalid conversion from 'int64_t (*)(int, const void*, std::size_t)' to 'GuestWriteHook_nid_no_patch' {aka 'long long int (__attribute__((sysv_abi)) *)(int, const void*, long long unsigned int)'} [-fpermissive] (same at :240 and :250) ``` CI stays green because the `windows` host-only lane doesn't select libc-linked test targets, the same reason the earlier SegFault was missed. Fix: put the attribute on each lambda. ```diff - LibcSetGuestWriteHook_nid_no_patch(+[](int, const void*, std::size_t count) -> std::int64_t { + LibcSetGuestWriteHook_nid_no_patch(+[](int, const void*, std::size_t count) APS5_VABI -> std::int64_t { ``` That goes on all three (the third one has an unnamed `std::size_t`). With this change, mingw g++ 16.2.0 and `clang++ -arch x86_64` both report 0 errors for the test file, and `AllocatingStrings.cpp` was 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 with `if (fd > SHRT_MAX) { errno = EMFILE; return (EOF); }`. The test instead asserts `dprintf_nid_postfix(99999, "invalid") == -1` with errno 9 (`EBADF`), which is what the host write path gives. There are two options. One is to add the `fd > 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 `EBADF` before it formats anything, so `dprintf(-1, nullptr)` gives `EBADF` and not `EINVAL`. That's fine, but the debt line lists both rules, so it could state the order. AI-assisted: yes (Claude Code).
rooklz commented 2026-10-29 17:20:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T17:20:00Z.

Review: APPROVED


Re-review at c33785b183

Both points from my last review are fixed, so I'm approving.

  • Windows build. APS5_VABI is now on all three test hooks. Running -fsyntax-only on GuestAllocatingStrings.cpp and AllocatingStrings.cpp gives 0 errors with both x86_64-w64-mingw32-g++ 16.2.0 and clang++ -arch x86_64 -std=c++20 (before this commit the test file had 3 errors under mingw).
  • EMFILE. The fd > SHRT_MAX → EMFILE (24) check now comes first, which matches the FreeBSD 11.4 vdprintf.c the 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 --check and git merge-tree against bb04a4f1ea are clean, and check_conventions --base upstream/main passes.

One thing to keep in mind, not blocking: the 32767 → EBADF row assumes the test process has no descriptor 32767 open. That holds for this test binary.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5702035329 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12933#pullrequestreview-5702035329). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T17:20:00Z. **Review:** APPROVED --- ### Re-review at c33785b183 Both points from my last review are fixed, so I'm approving. - **Windows build.** `APS5_VABI` is now on all three test hooks. Running `-fsyntax-only` on `GuestAllocatingStrings.cpp` and `AllocatingStrings.cpp` gives 0 errors with both `x86_64-w64-mingw32-g++` 16.2.0 and `clang++ -arch x86_64 -std=c++20` (before this commit the test file had 3 errors under mingw). - **EMFILE.** The `fd > SHRT_MAX` → `EMFILE` (24) check now comes first, which matches the FreeBSD 11.4 `vdprintf.c` the 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 --check` and `git merge-tree` against `bb04a4f1ea` are clean, and `check_conventions --base upstream/main` passes. One thing to keep in mind, not blocking: the 32767 → `EBADF` row assumes the test process has no descriptor 32767 open. That holds for this test binary. AI-assisted: yes (Claude Code).
Sign in to join this conversation.
No reviewers
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!12933
No description provided.