feat(libkernel): implement sendfile #3568

Merged
DariusHQ merged 1 commit from upstream-pr/3568 into main 2026-10-11 21:43:14 +00:00
DariusHQ commented 2026-10-11 13:26:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DariusHQ on 2026-10-11T13:26:56Z.


What

sendfile (YA0r4LCkfeY) in libkernel. Nothing in the tree implements it yet.

PS5MC (PPSA99420 1.2, VivaLaVent/PS5MC) stops at it at start-up on main 2220effbe (nothing since touches it), Windows, catalog build with eboot.bin and sce_module/libc.prx unwrapped as #2605 requires (tracked in #1578):

FAIL: unresolved ELF import YA0r4LCkfeY from libkernel.prx

With this branch it goes on to wcstok (8ngzWNZzFJU):

FAIL: unresolved ELF import 8ngzWNZzFJU from libSceLibcInternal.prx

Its only call site is in libmicrohttpd (Kodi's web server, connection.c), which sends file responses with it.

It follows FreeBSD 11's kern_sendfile.c:

  • headers, then the file from offset (to its end when nbytes is 0, only headers and trailers past the end), then trailers;
  • the file offset does not move;
  • on a non-blocking socket it fails with EAGAIN after a partial send, with *sbytes saying how far it got;
  • a negative offset (EINVAL), more than 1024 header or trailer entries (EINVAL) and an unreadable file (EBADF) fail before anything is sent and leave *sbytes alone;
  • a socket as the file and a socket that is not a stream (EINVAL), an unconnected socket (ENOTCONN) and a descriptor that is not a socket (EBADF, as for send) fail with *sbytes set to 0, as FreeBSD writes it once the file is open.

The file is read with sceKernelPread in 64 KiB blocks and sent with send, so flags are ignored. TechnicalDebt lists where that differs from FreeBSD: any descriptor sceKernelPread reads is accepted as the file, ENOTSOCK is EBADF, SF_NODISKIO never fails with EBUSY, and SF_SYNC returns once the data is queued.

Tested

Windows 11 (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build with BUILD_TESTING=ON on main a11c8f7f7 with four branches merged together: this one and the %#.0o, ' flag, positional-argument and sendfile branches opened alongside it: ctest 833/837 passed. The four failures fail the same way on main here:

  • windows_exceptions (#3411);
  • guest_kernel_sys: symlink throws Function not implemented on Windows, which #3510 addresses;
  • guest_filesystem: restoring the directory time fails on Windows, which #3518 addresses;
  • agc_driver_emulated_color_compare: the GPU here cannot sample the sRGB image it uses.

guest_sendfile, on a socketpair, checks:

  • a range with two headers and a trailer, and *sbytes;
  • the rest of a 200 000-byte file from an offset, read by another thread, more than one block;
  • nbytes past the end of the file, an offset past the end (headers and trailers only), null header pointer with a count, null sbytes;
  • that the file offset stays 0;
  • every error above, with *sbytes;
  • EAGAIN from a non-blocking socket fed a 32 MiB file with no reader, and that the bytes *sbytes reports arrived intact.

It passed 10/10 runs. Mutation check, all caught:

  • a negative offset reaching the read;
  • no limit on header and trailer entries;
  • headers or trailers not sent;
  • *sbytes written for an unreadable file;
  • a datagram socket accepted;
  • the connection not checked before sending;
  • nbytes ignored;
  • ESPIPE instead of EINVAL for a socket as the file;
  • the partial count lost on EAGAIN.

tools/import_audit.py on PS5MC relinked with --windows --registry: absent imports go from 28 to 27.

Not run on Linux (no Linux host here). The code is shared; on Linux it sends with MSG_NOSIGNAL, so a closed peer gives EPIPE rather than a host SIGPIPE.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4819395385 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3568). Originally posted by [DariusHQ](https://github.com/DariusHQ) on 2026-10-11T13:26:56Z. --- ### What `sendfile` (`YA0r4LCkfeY`) in libkernel. Nothing in the tree implements it yet. PS5MC (PPSA99420 1.2, [VivaLaVent/PS5MC](https://github.com/VivaLaVent/PS5MC)) stops at it at start-up on `main` `2220effbe` (nothing since touches it), Windows, catalog build with `eboot.bin` and `sce_module/libc.prx` unwrapped as #2605 requires (tracked in #1578): ``` FAIL: unresolved ELF import YA0r4LCkfeY from libkernel.prx ``` With this branch it goes on to `wcstok` (`8ngzWNZzFJU`): ``` FAIL: unresolved ELF import 8ngzWNZzFJU from libSceLibcInternal.prx ``` Its only call site is in libmicrohttpd (Kodi's web server, `connection.c`), which sends file responses with it. It follows FreeBSD 11's [`kern_sendfile.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/sys/kern/kern_sendfile.c): - headers, then the file from `offset` (to its end when `nbytes` is 0, only headers and trailers past the end), then trailers; - the file offset does not move; - on a non-blocking socket it fails with `EAGAIN` after a partial send, with `*sbytes` saying how far it got; - a negative offset (`EINVAL`), more than 1024 header or trailer entries (`EINVAL`) and an unreadable file (`EBADF`) fail before anything is sent and leave `*sbytes` alone; - a socket as the file and a socket that is not a stream (`EINVAL`), an unconnected socket (`ENOTCONN`) and a descriptor that is not a socket (`EBADF`, as for `send`) fail with `*sbytes` set to 0, as FreeBSD writes it once the file is open. The file is read with `sceKernelPread` in 64 KiB blocks and sent with `send`, so `flags` are ignored. TechnicalDebt lists where that differs from FreeBSD: any descriptor `sceKernelPread` reads is accepted as the file, `ENOTSOCK` is `EBADF`, `SF_NODISKIO` never fails with `EBUSY`, and `SF_SYNC` returns once the data is queued. ### Tested Windows 11 (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build with `BUILD_TESTING=ON` on `main` `a11c8f7f7` with four branches merged together: this one and the `%#.0o`, `'` flag, positional-argument and `sendfile` branches opened alongside it: `ctest` 833/837 passed. The four failures fail the same way on `main` here: - `windows_exceptions` (#3411); - `guest_kernel_sys`: `symlink` throws `Function not implemented` on Windows, which #3510 addresses; - `guest_filesystem`: restoring the directory time fails on Windows, which #3518 addresses; - `agc_driver_emulated_color_compare`: the GPU here cannot sample the sRGB image it uses. `guest_sendfile`, on a `socketpair`, checks: - a range with two headers and a trailer, and `*sbytes`; - the rest of a 200 000-byte file from an offset, read by another thread, more than one block; - `nbytes` past the end of the file, an offset past the end (headers and trailers only), null header pointer with a count, null `sbytes`; - that the file offset stays 0; - every error above, with `*sbytes`; - `EAGAIN` from a non-blocking socket fed a 32 MiB file with no reader, and that the bytes `*sbytes` reports arrived intact. It passed 10/10 runs. Mutation check, all caught: - a negative offset reaching the read; - no limit on header and trailer entries; - headers or trailers not sent; - `*sbytes` written for an unreadable file; - a datagram socket accepted; - the connection not checked before sending; - `nbytes` ignored; - `ESPIPE` instead of `EINVAL` for a socket as the file; - the partial count lost on `EAGAIN`. `tools/import_audit.py` on PS5MC relinked with `--windows --registry`: absent imports go from 28 to 27. Not run on Linux (no Linux host here). The code is shared; on Linux it sends with `MSG_NOSIGNAL`, so a closed peer gives `EPIPE` rather than a host `SIGPIPE`. ### Checklist - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
github-actions[bot] commented 2026-10-11 13:27:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T13:27:23Z.


Progress report (a11c8f7 → bad1903)

📈 System libraries: 85.82% (+0.01%, +1 implemented functions)

✅ 1 implemented
Library Name
libkernel sendfile_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6109516126 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3568#issuecomment-6109516126). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-11T13:27:23Z. --- <!-- progress-report --> ### Progress report (a11c8f7 → bad1903) 📈 **System libraries**: 85.82% (+0.01%, +1 implemented functions) <details> <summary>✅ 1 implemented</summary> | Library | Name | | - | - | | libkernel | `sendfile_nid_postfix` | </details>
babyhuey commented 2026-10-11 14:46:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-11T14:46:09Z.


Checked on Linux (Ubuntu 24.04, GCC 13) and Windows 11 (MinGW-w64 GCC 15.2 from WinLibs), Release, with this PR (bad19037) merged onto main a11c8f7f.

  • guest_sendfile passes 5/5 on both (--repeat until-fail:5); no warnings in the changed files on Windows.
  • PS5MC (PPSA99420) gets past sendfile on both. On main Windows stops at FAIL: unresolved ELF import YA0r4LCkfeY (sendfile) from libkernel.prx; with this PR it stops at FAIL: unresolved ELF import 8ngzWNZzFJU (wcstok) from libSceLibcInternal.prx (#3583). Linux gives the same: undefined symbol: 8ngzWNZzFJU.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6110212598 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3568#issuecomment-6110212598). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-11T14:46:09Z. --- Checked on Linux (Ubuntu 24.04, GCC 13) and Windows 11 (MinGW-w64 GCC 15.2 from WinLibs), Release, with this PR (bad19037) merged onto `main` a11c8f7f. - `guest_sendfile` passes 5/5 on both (`--repeat until-fail:5`); no warnings in the changed files on Windows. - PS5MC (PPSA99420) gets past `sendfile` on both. On `main` Windows stops at `FAIL: unresolved ELF import YA0r4LCkfeY (sendfile) from libkernel.prx`; with this PR it stops at `FAIL: unresolved ELF import 8ngzWNZzFJU (wcstok) from libSceLibcInternal.prx` (#3583). Linux gives the same: `undefined symbol: 8ngzWNZzFJU`.
github-actions[bot] commented 2026-10-11 18:36:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T18:36:26Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#3840 docs/dev/TechnicalDebt.md L605-606
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6112329595 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3568#issuecomment-6112329595). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-11T18:36:26Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #3840 | `docs/dev/TechnicalDebt.md` [L605-606](https://github.com/boykopovar/AnyPS5/blob/41f013f664f6ec36a8111f2c6f9772b76e562288/docs/dev/TechnicalDebt.md#L605-L606) | | |
adriadam10 commented 2026-10-11 21:43:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:43:10Z.


Checked on Linux with the PR merged onto current main: builds, guest_sendfile passes 5/5, and it doesn't link without the change. Reviewed the header/trailer limits, the *sbytes handling on each error path, the EAGAIN partial count and the TechnicalDebt entry against FreeBSD 11 kern_sendfile.c. Windows runs by @babyhuey and your own cover the MinGW side. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114109322 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3568#issuecomment-6114109322). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T21:43:10Z. --- Checked on Linux with the PR merged onto current `main`: builds, `guest_sendfile` passes 5/5, and it doesn't link without the change. Reviewed the header/trailer limits, the `*sbytes` handling on each error path, the `EAGAIN` partial count and the TechnicalDebt entry against FreeBSD 11 `kern_sendfile.c`. Windows runs by @babyhuey and your own cover the MinGW side. Merging.
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!3568
No description provided.