fix(libkernel): create pipes with pipe and fcntl on macOS, where pipe2 needs macOS 27 #19903

Merged
XIIYAZ merged 1 commit from upstream-pr/19903 into main 2026-11-14 11:06:44 +00:00
XIIYAZ commented 2026-11-14 07:27:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XIIYAZ on 2026-11-14T07:27:58Z.


What

pipe2_nid_postfix calls the host ::pipe2 on every non-Windows host. In the macOS 27 SDK, sys/unistd.h declares pipe2 (and dup3) with __API_AVAILABLE(macos(27.0), ...), and the libraries are built with a lower minimum version (LC_BUILD_VERSION minos 26.0 here), so pipe2 is weakly imported. On macOS 26 it resolves to null, and the first guest pipe or pipe2 jumps to address 0:

* thread #1, stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
  * frame #0: 0x0000000000000000
    frame #1: libkernel.prx`pipe2_nid_postfix + 79

The compiler reports it as 'pipe2' is only available on macOS 27.0 or newer at Stdio.cpp:767. With an SDK older than 27, pipe2 is not declared and the file does not compile.

On macOS the pipe is now made with pipe, then FD_CLOEXEC and O_NONBLOCK are set with fcntl when requested; if either fcntl fails, both descriptors are closed and the call fails with that errno. This works on every macOS version and SDK. Unlike pipe2, close-on-exec is not set atomically with the creation, so a concurrent fork/exec in another thread can inherit the descriptors in that window. Linux and Windows are unchanged.

Tested

macOS 26.7.3, Apple M4, Xcode SDK 27.2, Apple clang 21.0.0, CMake 4.4.4, Ninja, full build (-DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DCMAKE_OSX_ARCHITECTURES=x86_64, tests run under Rosetta) on main with #17425, #11049 and #11437 merged locally for the other macOS build breaks.

  • Full ctest before: 5002/5082 passed; 35 failures were segfaults.
  • After this change, rerunning the 80 failures: 32 pass that failed before (4 other tests also passed on a rerun without this change and are left out as flaky). The 32: guest_dup, guest_file_timestamps, guest_filesystem, guest_fstatfs, guest_fstatvfs, guest_getrlimit, guest_host_standard_error, guest_host_streams, guest_kernel_fcntl, guest_kernel_fcntl_sync, guest_kernel_fstat, guest_kernel_ioctl_fionread, guest_kernel_pipe2, guest_kernel_sock_names, guest_kernel_socket_shutdown, guest_kernel_sys_aio, guest_kernel_sys_dup, guest_kernel_sys_pselect, guest_kernel_sys_pwrite, guest_kernel_sys_pwrite_patched, guest_kernel_web_forwards, guest_kqueue, guest_pipe_fault, guest_poll, guest_posix_filesystem, guest_posix_scalar_io, guest_select, guest_socket_descriptor_flags, guest_socket_events, guest_socket_on_non_socket, guest_socket_rights, guest_terminal.
  • The 'pipe2' is only available on macOS 27.0 or newer warning for Stdio.cpp is gone.

python3 tools/check_conventions.py --base origin/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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5308706812 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/19903). Originally posted by [XIIYAZ](https://github.com/XIIYAZ) on 2026-11-14T07:27:58Z. --- ### What `pipe2_nid_postfix` calls the host `::pipe2` on every non-Windows host. In the macOS 27 SDK, `sys/unistd.h` declares `pipe2` (and `dup3`) with `__API_AVAILABLE(macos(27.0), ...)`, and the libraries are built with a lower minimum version (`LC_BUILD_VERSION minos 26.0` here), so `pipe2` is weakly imported. On macOS 26 it resolves to null, and the first guest `pipe` or `pipe2` jumps to address 0: ``` * thread #1, stop reason = EXC_BAD_ACCESS (code=1, address=0x0) * frame #0: 0x0000000000000000 frame #1: libkernel.prx`pipe2_nid_postfix + 79 ``` The compiler reports it as `'pipe2' is only available on macOS 27.0 or newer` at `Stdio.cpp:767`. With an SDK older than 27, `pipe2` is not declared and the file does not compile. On macOS the pipe is now made with `pipe`, then `FD_CLOEXEC` and `O_NONBLOCK` are set with `fcntl` when requested; if either `fcntl` fails, both descriptors are closed and the call fails with that `errno`. This works on every macOS version and SDK. Unlike `pipe2`, close-on-exec is not set atomically with the creation, so a concurrent `fork`/`exec` in another thread can inherit the descriptors in that window. Linux and Windows are unchanged. ### Tested macOS 26.7.3, Apple M4, Xcode SDK 27.2, Apple clang 21.0.0, CMake 4.4.4, Ninja, full build (`-DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=ON -DCMAKE_OSX_ARCHITECTURES=x86_64`, tests run under Rosetta) on `main` with #17425, #11049 and #11437 merged locally for the other macOS build breaks. - Full ctest before: 5002/5082 passed; 35 failures were segfaults. - After this change, rerunning the 80 failures: 32 pass that failed before (4 other tests also passed on a rerun without this change and are left out as flaky). The 32: `guest_dup`, `guest_file_timestamps`, `guest_filesystem`, `guest_fstatfs`, `guest_fstatvfs`, `guest_getrlimit`, `guest_host_standard_error`, `guest_host_streams`, `guest_kernel_fcntl`, `guest_kernel_fcntl_sync`, `guest_kernel_fstat`, `guest_kernel_ioctl_fionread`, `guest_kernel_pipe2`, `guest_kernel_sock_names`, `guest_kernel_socket_shutdown`, `guest_kernel_sys_aio`, `guest_kernel_sys_dup`, `guest_kernel_sys_pselect`, `guest_kernel_sys_pwrite`, `guest_kernel_sys_pwrite_patched`, `guest_kernel_web_forwards`, `guest_kqueue`, `guest_pipe_fault`, `guest_poll`, `guest_posix_filesystem`, `guest_posix_scalar_io`, `guest_select`, `guest_socket_descriptor_flags`, `guest_socket_events`, `guest_socket_on_non_socket`, `guest_socket_rights`, `guest_terminal`. - The `'pipe2' is only available on macOS 27.0 or newer` warning for `Stdio.cpp` is gone. `python3 tools/check_conventions.py --base origin/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
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!19903
No description provided.