fix(libkernel): report socket status from fstat on a guest socket #4351

Closed
milescoviello wants to merge 1 commit from upstream-pr/4351 into main
milescoviello commented 2026-10-12 07:25:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T07:25:29Z.


What

Fixes #4051.

fstat on a guest socket descriptor passed the guest number (from 0x10000000 up) to the host, which has no such descriptor, so it threw FillFileStat: fstat failed for fd 268435460. sceKernelFstat returned EBADF for the same descriptor. FreeBSD fills the stat buffer for a socket through soo_stat (sys/kern/sys_socket.c):

	bzero((caddr_t)ub, sizeof (*ub));
	ub->st_mode = S_IFSOCK;
	...
	if ((sb->sb_state & SBS_CANTRCVMORE) == 0 || sbavail(sb))
		ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
	ub->st_size = sbavail(sb) - sb->sb_ctl;
	...
	if ((sb->sb_state & SBS_CANTSENDMORE) == 0)
		ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;

pru_sense_null (sys/kern/uipc_socket.c) then sets st_blksize = so->so_snd.sb_hiwat.

  • fstat and sceKernelFstat answer a guest socket from the socket table: S_IFSOCK, the read bits until the receive side is shut down and drained, the write bits until the send side is shut down, st_size from the queued bytes (FIONREAD), st_blksize from SO_SNDBUF. Every other field is zero, as after FreeBSD's bzero.
  • shutdown records which side it closed.
  • A closed socket descriptor now gets EBADF instead of the throw.
  • TechnicalDebt.md lists the differences: a peer's shutdown does not clear the bits, Linux reports FIONREAD per datagram and doubles SO_SNDBUF, and socketpair ends report st_dev/st_ino 0.

Tested

Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags.

  • guest_socket_pair gained checks on a stream pair: a fresh end reports mode 0140666 and size 0, 5 queued bytes show as st_size 5 through sceKernelFstat, SHUT_WR leaves 0140444, SHUT_RD keeps the read bits while data is queued and drops them once it is read (0140222), and a closed descriptor returns EBADF and the SCE EBADF code. With main's library sources it aborts with FillFileStat: fstat failed for fd 268435460. It passes with this change.
  • ctest -R '^guest_' -E '^guest_video_out_': 289/289 pass.

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
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4826233419 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4351). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T07:25:29Z. --- ### What Fixes #4051. `fstat` on a guest socket descriptor passed the guest number (from `0x10000000` up) to the host, which has no such descriptor, so it threw `FillFileStat: fstat failed for fd 268435460`. `sceKernelFstat` returned `EBADF` for the same descriptor. FreeBSD fills the stat buffer for a socket through `soo_stat` (`sys/kern/sys_socket.c`): ```c bzero((caddr_t)ub, sizeof (*ub)); ub->st_mode = S_IFSOCK; ... if ((sb->sb_state & SBS_CANTRCVMORE) == 0 || sbavail(sb)) ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH; ub->st_size = sbavail(sb) - sb->sb_ctl; ... if ((sb->sb_state & SBS_CANTSENDMORE) == 0) ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH; ``` `pru_sense_null` (`sys/kern/uipc_socket.c`) then sets `st_blksize = so->so_snd.sb_hiwat`. - `fstat` and `sceKernelFstat` answer a guest socket from the socket table: `S_IFSOCK`, the read bits until the receive side is shut down and drained, the write bits until the send side is shut down, `st_size` from the queued bytes (`FIONREAD`), `st_blksize` from `SO_SNDBUF`. Every other field is zero, as after FreeBSD's `bzero`. - `shutdown` records which side it closed. - A closed socket descriptor now gets `EBADF` instead of the throw. - TechnicalDebt.md lists the differences: a peer's shutdown does not clear the bits, Linux reports `FIONREAD` per datagram and doubles `SO_SNDBUF`, and `socketpair` ends report `st_dev`/`st_ino` 0. ### Tested Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags. - `guest_socket_pair` gained checks on a stream pair: a fresh end reports mode `0140666` and size 0, 5 queued bytes show as `st_size` 5 through `sceKernelFstat`, `SHUT_WR` leaves `0140444`, `SHUT_RD` keeps the read bits while data is queued and drops them once it is read (`0140222`), and a closed descriptor returns `EBADF` and the SCE `EBADF` code. With main's library sources it aborts with `FillFileStat: fstat failed for fd 268435460`. It passes with this change. - `ctest -R '^guest_' -E '^guest_video_out_'`: 289/289 pass. ### 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] Unblocks: none - [x] AI-assisted: yes
xXPBSXx commented 2026-10-12 13:33:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T13:33:51Z.


Windows test of head a84aa9ade merged onto current main (a1a9c3db5, clean merge): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build 0 errors. The 27 tests matching guest_(socket|poll|select|dup|sce_net|fstat|posix_filesystem|tcp|udp|fcntl|equeue|filesystem|net) pass, including guest_socket_pair (the new fstat cases), guest_tcp, guest_udp and guest_poll.

Not checked: that the new guest_socket_pair cases fail without the change, Linux, and a title run. Note for ordering: this PR conflicts with #4391 (Stdio.cpp, SocketRuntime.hpp, TechnicalDebt.md), which makes fstat on a guest socket throw as unimplemented; see https://github.com/boykopovar/AnyPS5/pull/4391#issuecomment-6125218729.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6125342642 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4351#issuecomment-6125342642). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T13:33:51Z. --- Windows test of head `a84aa9ade` merged onto current `main` (`a1a9c3db5`, clean merge): Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build 0 errors. The 27 tests matching `guest_(socket|poll|select|dup|sce_net|fstat|posix_filesystem|tcp|udp|fcntl|equeue|filesystem|net)` pass, including `guest_socket_pair` (the new fstat cases), `guest_tcp`, `guest_udp` and `guest_poll`. Not checked: that the new `guest_socket_pair` cases fail without the change, Linux, and a title run. Note for ordering: this PR conflicts with #4391 (`Stdio.cpp`, `SocketRuntime.hpp`, `TechnicalDebt.md`), which makes `fstat` on a guest socket throw as unimplemented; see https://github.com/boykopovar/AnyPS5/pull/4391#issuecomment-6125218729. AI-assisted (Claude Code).
milescoviello commented 2026-10-12 13:35:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T13:35:36Z.


Thanks for the Windows run and for mapping the overlap with #4391. This PR finds guest sockets with the FirstDescriptor range, which #4391 removes; if #4391 lands first I'll rebase this onto GuestSockets::IsOpen and rerun the tests, and if this lands first the same change applies to #4391's merge.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6125372687 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4351#issuecomment-6125372687). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T13:35:36Z. --- Thanks for the Windows run and for mapping the overlap with #4391. This PR finds guest sockets with the `FirstDescriptor` range, which #4391 removes; if #4391 lands first I'll rebase this onto `GuestSockets::IsOpen` and rerun the tests, and if this lands first the same change applies to #4391's merge. AI-assisted: yes
xXPBSXx commented 2026-10-12 14:42:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T14:42:08Z.


Windows test: head a84aa9ade merged onto current main 8fa5cc486 (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j2: full build 0 errors; ctest with the filter guest_socket_pair: guest_socket_pair passed (1 test(s), 0 failed); python tools/check_conventions.py --base upstream/main passed. I did not run the new tests against main without the change.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6126528671 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4351#issuecomment-6126528671). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T14:42:08Z. --- Windows test: head `a84aa9ade` merged onto current `main` `8fa5cc486` (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, `-j2`: full build 0 errors; ctest with the filter `guest_socket_pair`: guest_socket_pair passed (1 test(s), 0 failed); `python tools/check_conventions.py --base upstream/main` passed. I did not run the new tests against `main` without the change. AI-assisted (Claude Code).
github-actions[bot] commented 2026-10-12 21:10:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T21:10:32Z.


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
#3644 docs/dev/debt/libkernel.md L142-143
#4796 core/libs/prx/libkernel/Socket/src/Udp.cpp L79
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6132667152 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4351#issuecomment-6132667152). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T21:10:32Z. --- <!-- 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 | |---|---|---|---| | #3644 | `docs/dev/debt/libkernel.md` [L142-143](https://github.com/boykopovar/AnyPS5/blob/1c5e62878ab01af05d5a75bd94146228f5dea9d4/docs/dev/debt/libkernel.md#L142-L143) | | | | #4796 | `core/libs/prx/libkernel/Socket/src/Udp.cpp` [L79](https://github.com/boykopovar/AnyPS5/blob/1c5e62878ab01af05d5a75bd94146228f5dea9d4/core/libs/prx/libkernel/Socket/src/Udp.cpp#L79) | | |
oneandonlydean commented 2026-10-13 00:21:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T00:21:23Z.


Closing: no title is shown to call fstat on a socket, where main stops at FillFileStat: fstat failed for fd ..., and the result here differs from the console in the ways its TechnicalDebt entry lists. Reopen with the title ID and that log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6135187921 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4351#issuecomment-6135187921). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-13T00:21:23Z. --- Closing: no title is shown to call `fstat` on a socket, where main stops at `FillFileStat: fstat failed for fd ...`, and the result here differs from the console in the ways its TechnicalDebt entry lists. Reopen with the title ID and that log line.

Pull request closed

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!4351
No description provided.