fix(libkernel): pass ioctl on a sceNetSocket socket to libSceNet #15890

Merged
Retr0G28 merged 1 commit from upstream-pr/15890 into main 2026-11-05 23:17:32 +00:00
Retr0G28 commented 2026-11-04 22:17:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Retr0G28 on 2026-11-04T22:17:23Z.


What

ioctl and _ioctl on a socket created by sceNetSocket did not reach libSceNet, though fcntl on the same descriptor does, through KernelSetSocketStatusControl. The kernel took the descriptor as a file. On Linux the descriptor is a duplicate of the host socket, so FIONBIO switched the host O_NONBLOCK but not the library's SO_NBIO state, which sceNetRecv, F_GETFL and SO_NBIO read. On Windows it is a NUL placeholder, so FIONREAD and enabling FIONBIO threw as on a character device. This was the debt entry ioctl, _ioctl ... do not pass a sceNetSocket socket to libSceNet as fcntl does.

FreeBSD 11.4's kern_ioctl handles every socket the same way through fo_ioctl, that is soo_ioctl, where FIONBIO sets the socket's non-blocking state and FIONREAD reports its queued bytes. sceNetIoctl already implements both on a sceNetSocket socket.

  • SocketPoll.hpp and Socket.cpp add an IoctlControl hook next to StatusControl, with KernelSetSocketIoctlControl_nid_no_patch and KernelControlSocketIoctl_nid_no_patch.
  • libSceNet registers ControlSocketIoctlForKernel. It returns false for a descriptor that isn't one of its sockets. Otherwise it calls sceNetIoctl and hands its errno back, as ControlSocketStatusForKernel does for fcntl.
  • ioctl_nid_postfix finds a libSceNet socket with the existing KernelFindSocketNative_nid_no_patch lookup. It then applies the checks a kernel socket gets: a malformed request fails with ENOTTY, and a null argument for a request with a length fails with EFAULT. Then it passes the request to the hook.
  • FIOCLEX and FIONCLEX keep their current path to the descriptor beneath the socket. fcntl's F_GETFD and F_SETFD on such a socket still fail with EINVAL, so the socket has no close-on-exec flag to set yet. The debt entry now records that gap in place of the fixed one.

Tested

Windows 11, WinLibs MinGW-w64 GCC 15.2.0, CMake + Ninja, Release, -DBUILD_TESTING=ON, on main c3a5290c7d.

  • guest_sce_net's CheckIoctl now calls ioctl_nid_postfix on a sceNetSocket datagram socket. It checks that:
    • FIONBIO with 0 and 3 sets SO_NBIO to 0 and 1, and F_GETFL reports O_RDWR and O_RDWR | O_NONBLOCK;
    • FIONREAD reports 5 with a 5-byte datagram queued and 0 after it is received;
    • FIONBIO and FIONREAD with a null argument fail with EFAULT.
  • With main's Udp.cpp and the rest of this PR, guest_sce_net fails (exit 0xc0000409). With this PR it passes.
  • guest_sce_net, guest_sce_net_kernel_forwards and guest_kernel_fcntl pass.
  • python tools/check_conventions.py --base origin/main passes.
  • Not run on Linux or macOS. No title or homebrew run.

Overlap (tools/pr_overlap.py), checked with git merge-tree against this branch:

  • #11219 answers unknown requests on a kernel INET socket with EADDRNOTAVAIL. Its code merges cleanly with this PR. The debt file conflicts on lines only: #11219 rewrites the ioctl entry directly above the one changed here. Whichever merges second keeps both entries. With #11219, a sceNetSocket socket still answers other requests through sceNetIoctl with ENOTTY, as the sceNetIoctl entry in libSceNet.md records.
  • #12646 (EFAULT for invalid socket ioctl pointers) merges cleanly.
  • #11896, #12682, #12688, #12700 and #12965 already conflict with main in the same files. This PR adds no conflict to their code: #12688, #12700 and #12965 change the kernel socket FIONBIO branch, not the path added here.
  • #13486 conflicts only in relinker files.
  • The other PRs listed change other entries of libkernel.md.

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:5162512233 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15890). Originally posted by [Retr0G28](https://github.com/Retr0G28) on 2026-11-04T22:17:23Z. --- ### What `ioctl` and `_ioctl` on a socket created by `sceNetSocket` did not reach libSceNet, though `fcntl` on the same descriptor does, through `KernelSetSocketStatusControl`. The kernel took the descriptor as a file. On Linux the descriptor is a duplicate of the host socket, so `FIONBIO` switched the host `O_NONBLOCK` but not the library's `SO_NBIO` state, which `sceNetRecv`, `F_GETFL` and `SO_NBIO` read. On Windows it is a `NUL` placeholder, so `FIONREAD` and enabling `FIONBIO` threw as on a character device. This was the debt entry `ioctl, _ioctl ... do not pass a sceNetSocket socket to libSceNet as fcntl does`. FreeBSD 11.4's [`kern_ioctl`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/sys_generic.c#L758-L840) handles every socket the same way through `fo_ioctl`, that is [`soo_ioctl`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/sys_socket.c#L156-L262), where `FIONBIO` sets the socket's non-blocking state and `FIONREAD` reports its queued bytes. `sceNetIoctl` already implements both on a `sceNetSocket` socket. - `SocketPoll.hpp` and `Socket.cpp` add an `IoctlControl` hook next to `StatusControl`, with `KernelSetSocketIoctlControl_nid_no_patch` and `KernelControlSocketIoctl_nid_no_patch`. - libSceNet registers `ControlSocketIoctlForKernel`. It returns `false` for a descriptor that isn't one of its sockets. Otherwise it calls `sceNetIoctl` and hands its `errno` back, as `ControlSocketStatusForKernel` does for `fcntl`. - `ioctl_nid_postfix` finds a libSceNet socket with the existing `KernelFindSocketNative_nid_no_patch` lookup. It then applies the checks a kernel socket gets: a malformed request fails with `ENOTTY`, and a null argument for a request with a length fails with `EFAULT`. Then it passes the request to the hook. - `FIOCLEX` and `FIONCLEX` keep their current path to the descriptor beneath the socket. `fcntl`'s `F_GETFD` and `F_SETFD` on such a socket still fail with `EINVAL`, so the socket has no close-on-exec flag to set yet. The debt entry now records that gap in place of the fixed one. ### Tested Windows 11, WinLibs MinGW-w64 GCC 15.2.0, CMake + Ninja, Release, `-DBUILD_TESTING=ON`, on `main` c3a5290c7d. - `guest_sce_net`'s `CheckIoctl` now calls `ioctl_nid_postfix` on a `sceNetSocket` datagram socket. It checks that: - `FIONBIO` with 0 and 3 sets `SO_NBIO` to 0 and 1, and `F_GETFL` reports `O_RDWR` and `O_RDWR | O_NONBLOCK`; - `FIONREAD` reports 5 with a 5-byte datagram queued and 0 after it is received; - `FIONBIO` and `FIONREAD` with a null argument fail with `EFAULT`. - With `main`'s `Udp.cpp` and the rest of this PR, `guest_sce_net` fails (exit 0xc0000409). With this PR it passes. - `guest_sce_net`, `guest_sce_net_kernel_forwards` and `guest_kernel_fcntl` pass. - `python tools/check_conventions.py --base origin/main` passes. - Not run on Linux or macOS. No title or homebrew run. Overlap (`tools/pr_overlap.py`), checked with `git merge-tree` against this branch: - #11219 answers unknown requests on a kernel INET socket with `EADDRNOTAVAIL`. Its code merges cleanly with this PR. The debt file conflicts on lines only: #11219 rewrites the `ioctl` entry directly above the one changed here. Whichever merges second keeps both entries. With #11219, a `sceNetSocket` socket still answers other requests through `sceNetIoctl` with `ENOTTY`, as the `sceNetIoctl` entry in `libSceNet.md` records. - #12646 (`EFAULT` for invalid socket `ioctl` pointers) merges cleanly. - #11896, #12682, #12688, #12700 and #12965 already conflict with `main` in the same files. This PR adds no conflict to their code: #12688, #12700 and #12965 change the kernel socket `FIONBIO` branch, not the path added here. - #13486 conflicts only in relinker files. - The other PRs listed change other entries of `libkernel.md`. ### 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
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!15890
No description provided.