fix(libSceNet): keep close-on-exec for fcntl on a sceNetSocket socket as FreeBSD does #16826

Open
Ragbie wants to merge 2 commits from upstream-pr/16826 into main
Ragbie commented 2026-11-07 17:22:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ragbie on 2026-11-07T17:22:50Z.


What

On a socket created with sceNetSocket, guest fcntl(F_GETFD) and fcntl(F_SETFD) failed with EINVAL, while ioctl(FIOCLEX) and ioctl(FIONCLEX) already set the close-on-exec flag of the descriptor beneath it (#15890 recorded this in docs/dev/debt/libkernel.md). FreeBSD 11.4 keeps that flag in the descriptor table for every descriptor, whatever the file type: kern_fcntl reads and writes UF_EXCLOSE for F_GETFD/F_SETFD, kern_ioctl changes the same flag for FIOCLEX/FIONCLEX, and _finstall starts a new descriptor with it clear unless O_CLOEXEC is given.

  • libSceNet/Export.cpp: ControlSocketStatusForKernel no longer answers F_GETFD and F_SETFD, so fcntl takes them to File::Fcntl, which reads and writes the record FIOCLEX already uses. F_GETFL/F_SETFL stay with libSceNet; every other command still fails with EINVAL, as the debt entry now says.
  • Socket/src/Udp.cpp: KernelReserveSocketNumber_nid_no_patch records the new number with the flag clear, and KernelReleaseSocketNumber_nid_no_patch forgets the record before the number is closed. Without the first, the File record of a new socket on Linux and macOS was built from the host descriptor, which F_DUPFD_CLOEXEC creates with FD_CLOEXEC, so F_GETFD returned 1. Without the second, a later descriptor reusing the number that does not record itself (for example shm_open on Linux) inherited the socket's record.

Tested

  • guest_sce_net (CheckKernelStatusFlags): F_GETFD is 0 on a new socket, F_SETFD/FIONCLEX/FIOCLEX round-trip through F_GETFD, F_GETFL is unaffected, and a socket that reuses the number of one closed with the flag set starts clear.
  • Windows 11 (WinLibs GCC 15.2, Release): fails on main with the new test (the old expectation was -1/EINVAL), passes with this PR. Full ctest, run as the non-GPU tests (3021) and then the GPU tests (1504): the only failures are select_tests_MissingCommandTests, agc_driver_image_plane_address_on_line, agc_driver_image_sample_lod_clamp and agc_driver_image_sample_lod_clamp_shader_lane, which fail on main on this machine too, and vulkan_validation_gate_commands, whose symbolic-link case needs a privilege this account lacks. After rebasing onto main 12dafe4160, the 52 guest_sce_net, guest_net*, guest_socket* and guest_recvmsg* tests pass.
  • Linux (Ubuntu 24.04 container, GCC 13.3, Release): guest_sce_net passes; an earlier version that only forgot the record on release failed it there (the new socket reported 1), which Windows did not show.
  • python tools/check_conventions.py --base upstream/main passes.

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 (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5204128361 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16826). Originally posted by [Ragbie](https://github.com/Ragbie) on 2026-11-07T17:22:50Z. --- ### What On a socket created with `sceNetSocket`, guest `fcntl(F_GETFD)` and `fcntl(F_SETFD)` failed with `EINVAL`, while `ioctl(FIOCLEX)` and `ioctl(FIONCLEX)` already set the close-on-exec flag of the descriptor beneath it (#15890 recorded this in `docs/dev/debt/libkernel.md`). FreeBSD 11.4 keeps that flag in the descriptor table for every descriptor, whatever the file type: [`kern_fcntl`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_descrip.c#L518-L540) reads and writes `UF_EXCLOSE` for `F_GETFD`/`F_SETFD`, [`kern_ioctl`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/sys_generic.c#L814-L820) changes the same flag for `FIOCLEX`/`FIONCLEX`, and [`_finstall`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/kern_descrip.c#L1811) starts a new descriptor with it clear unless `O_CLOEXEC` is given. - `libSceNet/Export.cpp`: `ControlSocketStatusForKernel` no longer answers `F_GETFD` and `F_SETFD`, so `fcntl` takes them to `File::Fcntl`, which reads and writes the record `FIOCLEX` already uses. `F_GETFL`/`F_SETFL` stay with libSceNet; every other command still fails with `EINVAL`, as the debt entry now says. - `Socket/src/Udp.cpp`: `KernelReserveSocketNumber_nid_no_patch` records the new number with the flag clear, and `KernelReleaseSocketNumber_nid_no_patch` forgets the record before the number is closed. Without the first, the File record of a new socket on Linux and macOS was built from the host descriptor, which `F_DUPFD_CLOEXEC` creates with `FD_CLOEXEC`, so `F_GETFD` returned 1. Without the second, a later descriptor reusing the number that does not record itself (for example `shm_open` on Linux) inherited the socket's record. ### Tested - `guest_sce_net` (`CheckKernelStatusFlags`): `F_GETFD` is 0 on a new socket, `F_SETFD`/`FIONCLEX`/`FIOCLEX` round-trip through `F_GETFD`, `F_GETFL` is unaffected, and a socket that reuses the number of one closed with the flag set starts clear. - Windows 11 (WinLibs GCC 15.2, Release): fails on `main` with the new test (the old expectation was `-1`/`EINVAL`), passes with this PR. Full `ctest`, run as the non-GPU tests (3021) and then the GPU tests (1504): the only failures are `select_tests_MissingCommandTests`, `agc_driver_image_plane_address_on_line`, `agc_driver_image_sample_lod_clamp` and `agc_driver_image_sample_lod_clamp_shader_lane`, which fail on `main` on this machine too, and `vulkan_validation_gate_commands`, whose symbolic-link case needs a privilege this account lacks. After rebasing onto `main` `12dafe4160`, the 52 `guest_sce_net`, `guest_net*`, `guest_socket*` and `guest_recvmsg*` tests pass. - Linux (Ubuntu 24.04 container, GCC 13.3, Release): `guest_sce_net` passes; an earlier version that only forgot the record on release failed it there (the new socket reported 1), which Windows did not show. - `python tools/check_conventions.py --base upstream/main` passes. ### 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 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 (Claude Code)
MarcZX10 commented 2026-11-21 05:01:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T05:01:11Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the implementation and added test cover the described close-on-exec behavior.

What I ran

Reviewed head 28c672092a1d85a13f6984898f02a7f5868c0ef6. No tests or builds were run. Read description.md, commits.md, manifest.json, diff.patch, Export.cpp, Udp.cpp, GuestSceNet.cpp, and DescriptorControl.cpp; all changed files listed in the manifest were included and read. files.json was not available through the reader.

Results

Check Result Note
Diff and targeted code review passed Descriptor flag tracking follows existing record API
Tests and build not run Author reports platform test results

Findings

Blocking

None.

Non-blocking

None.

Not verified

The reported Windows and Linux test runs were not independently reproduced. No other files were excluded according to the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6002958990 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16826#pullrequestreview-6002958990). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T05:01:11Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the implementation and added test cover the described close-on-exec behavior. ## What I ran Reviewed head `28c672092a1d85a13f6984898f02a7f5868c0ef6`. No tests or builds were run. Read `description.md`, `commits.md`, `manifest.json`, `diff.patch`, `Export.cpp`, `Udp.cpp`, `GuestSceNet.cpp`, and `DescriptorControl.cpp`; all changed files listed in the manifest were included and read. `files.json` was not available through the reader. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted code review | passed | Descriptor flag tracking follows existing record API | | Tests and build | not run | Author reports platform test results | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The reported Windows and Linux test runs were not independently reproduced. No other files were excluded according to the manifest. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 16:29:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T16:29:38Z.

Review: COMMENTED


This head conflicts with current main in libSceNet/Export.cpp and GuestSceNet.cpp. Main now retains the supported F_SETFL status bits on the native socket and checks their inheritance through accept. Keep that status handling and those tests when adding the F_GETFD/F_SETFD fall-through and the descriptor-reuse check. After this test sets status flags, F_GETFL should still return 2 | kept while the close-on-exec flag changes independently.

Please carry the debt entry change in a fragment under docs/dev/debt/changes/.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6173949876 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16826#pullrequestreview-6173949876). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T16:29:38Z. **Review:** COMMENTED --- This head conflicts with current main in `libSceNet/Export.cpp` and `GuestSceNet.cpp`. Main now retains the supported `F_SETFL` status bits on the native socket and checks their inheritance through `accept`. Keep that status handling and those tests when adding the `F_GETFD`/`F_SETFD` fall-through and the descriptor-reuse check. After this test sets status flags, `F_GETFL` should still return `2 | kept` while the close-on-exec flag changes independently. Please carry the debt entry change in a fragment under `docs/dev/debt/changes/`. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/16826:upstream-pr/16826'
git switch 'upstream-pr/16826'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/16826'
git switch 'upstream-pr/16826'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/16826'
git switch 'upstream-pr/16826'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/16826'
git switch 'main'
git merge --squash 'upstream-pr/16826'
git switch 'main'
git merge --ff-only 'upstream-pr/16826'
git switch 'main'
git merge 'upstream-pr/16826'
git push origin 'main'
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!16826
No description provided.