From cdbbf5654bdc538dbb8565d8f3a19c82bed99978 Mon Sep 17 00:00:00 2001 From: Babu Akhil <96844928+babuakhil@users.noreply.github.com> Date: Wed, 2 Dec 2026 22:11:37 +0530 Subject: [PATCH] fix(libkernel): raise SIGPIPE for a Windows write to a pipe without a reader, as FreeBSD 11.4's dofilewrite does A write to a pipe whose read end is closed failed with EPIPE on Windows but raised nothing, where FreeBSD 11.4's dofilewrite sends SIGPIPE to the writing thread and a Linux host raises its own SIGPIPE, which reaches the guest as signal 13. The Windows write and vector write paths now note the broken pipe, and RunKernelFileCall raises guest signal 13 with SI_KERNEL once the call has returned and released its locks, so write, _write, sceKernelWrite, writev and sceKernelWritev all raise it. A handler runs, a blocked signal stays pending and an ignored one is dropped; the call still fails with EPIPE. The pipe test installs a handler and checks that each of write, _write and writev raises the signal once and still fails with EPIPE, that a working pipe raises nothing, and that an ignored SIGPIPE is dropped. Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libkernel/File/include/File.hpp | 10 ++++- core/libs/prx/libkernel/File/src/Open.cpp | 14 +++++- core/libs/prx/libkernel/File/src/Stdio.cpp | 5 ++- .../libkernel/System/include/FaultSignals.hpp | 1 + .../libs/prx/libkernel/System/src/Signals.cpp | 8 ++++ core/libs/tests/GuestKernelPipeBounds.cpp | 44 +++++++++++++++++++ .../fix-libkernel-windows-pipe-sigpipe.md | 5 +++ 7 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 docs/dev/debt/changes/fix-libkernel-windows-pipe-sigpipe.md diff --git a/core/libs/prx/libkernel/File/include/File.hpp b/core/libs/prx/libkernel/File/include/File.hpp index caa42aace1b0..b85ccae8b8f1 100644 --- a/core/libs/prx/libkernel/File/include/File.hpp +++ b/core/libs/prx/libkernel/File/include/File.hpp @@ -47,10 +47,18 @@ inline std::int64_t KernelFileResult(int saved, std::int64_t result) { *__error_nid_postfix() = result < 0 ? (static_cast(result) & 0xffff) : saved; return result; } +#ifdef _WIN32 +void NoteBrokenPipeWrite(); +void RaiseBrokenPipeAfterWrite(); +#endif template auto RunKernelFileCall(TCall call) { const int saved = *__error_nid_postfix(); - return KernelFileResult(saved, call()); + const auto result = call(); +#ifdef _WIN32 + RaiseBrokenPipeAfterWrite(); +#endif + return KernelFileResult(saved, result); } #ifdef _WIN32 std::optional WindowsDescriptorPath_nid_no_patch(int descriptor); diff --git a/core/libs/prx/libkernel/File/src/Open.cpp b/core/libs/prx/libkernel/File/src/Open.cpp index 6e03917be604..e3dfe7b2793b 100644 --- a/core/libs/prx/libkernel/File/src/Open.cpp +++ b/core/libs/prx/libkernel/File/src/Open.cpp @@ -42,6 +42,8 @@ #include "prx/libkernel/File/include/WindowsSymbolicLink.hpp" #include "prx/libkernel/File/include/WindowsPermissions.hpp" #include "prx/libc/include/CreationMask.hpp" +#include "prx/libkernel/System/include/FaultSignals.hpp" +#include static HANDLE OpenNativeHandle(const std::filesystem::path& p, int nativeFlags, DWORD desired, SECURITY_ATTRIBUTES& security, DWORD attributes, bool& created) { constexpr DWORD share = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; @@ -146,6 +148,13 @@ static std::int64_t NativeRead(int fd, void* buf, std::size_t n) { } while (done < n); return static_cast(done); } +static thread_local bool brokenPipeWrite = false; +void File::NoteBrokenPipeWrite() { + brokenPipeWrite = true; +} +void File::RaiseBrokenPipeAfterWrite() { + if (std::exchange(brokenPipeWrite, false)) RaiseBrokenPipeSignal_nid_no_patch(); +} static std::int64_t NativeWrite(int fd, const void* buf, std::size_t n) { const auto positioned = File::LockPositionedTransfer(fd); if (File::HasFixedSizeWrites(fd)) { @@ -172,7 +181,10 @@ static std::int64_t NativeWrite(int fd, const void* buf, std::size_t n) { if (result < 0) { if (done != 0) break; if (errno == ENOSPC && _doserrno == 0 && ::GetFileType(handle) == FILE_TYPE_PIPE) errno = EAGAIN; - if ((_doserrno == ERROR_NO_DATA || _doserrno == ERROR_BROKEN_PIPE) && ::GetFileType(handle) == FILE_TYPE_PIPE) errno = EPIPE; + if ((_doserrno == ERROR_NO_DATA || _doserrno == ERROR_BROKEN_PIPE) && ::GetFileType(handle) == FILE_TYPE_PIPE) { + errno = EPIPE; + File::NoteBrokenPipeWrite(); + } return -1; } done += static_cast(result); diff --git a/core/libs/prx/libkernel/File/src/Stdio.cpp b/core/libs/prx/libkernel/File/src/Stdio.cpp index 12b4e291d2b4..653a27e57a5f 100644 --- a/core/libs/prx/libkernel/File/src/Stdio.cpp +++ b/core/libs/prx/libkernel/File/src/Stdio.cpp @@ -480,7 +480,10 @@ static std::int64_t NativeTransferBuffer(int descriptor, void* buf, std::size_t } if (!write) return ::_read(descriptor, buf, static_cast(nbytes)); const int result = ::_write(descriptor, buf, static_cast(nbytes)); - if (result < 0 && (_doserrno == ERROR_NO_DATA || _doserrno == ERROR_BROKEN_PIPE) && ::GetFileType(reinterpret_cast(::_get_osfhandle(descriptor))) == FILE_TYPE_PIPE) errno = EPIPE; + if (result < 0 && (_doserrno == ERROR_NO_DATA || _doserrno == ERROR_BROKEN_PIPE) && ::GetFileType(reinterpret_cast(::_get_osfhandle(descriptor))) == FILE_TYPE_PIPE) { + errno = EPIPE; + File::NoteBrokenPipeWrite(); + } return result; } static std::int64_t NativeTransfer(int descriptor, void* buf, std::size_t nbytes, bool write) { diff --git a/core/libs/prx/libkernel/System/include/FaultSignals.hpp b/core/libs/prx/libkernel/System/include/FaultSignals.hpp index 6e0029d40cfb..6c9ec51d6cdf 100644 --- a/core/libs/prx/libkernel/System/include/FaultSignals.hpp +++ b/core/libs/prx/libkernel/System/include/FaultSignals.hpp @@ -3,6 +3,7 @@ #include +extern "C" void RaiseBrokenPipeSignal_nid_no_patch(); bool SiginfoFaultHandlerInstalled(int guest); void RunSiginfoFaultHandler(int guest, std::int32_t code, std::uintptr_t address, std::uint32_t trap, void* ucontext); void InstallSiginfoFaultDelivery(); diff --git a/core/libs/prx/libkernel/System/src/Signals.cpp b/core/libs/prx/libkernel/System/src/Signals.cpp index c1a3d6fb89a9..c8155206d3f4 100644 --- a/core/libs/prx/libkernel/System/src/Signals.cpp +++ b/core/libs/prx/libkernel/System/src/Signals.cpp @@ -966,6 +966,14 @@ int ChangeHold(int how, int guest) { } +extern "C" { +static int RaiseWithCode(int guest, std::int32_t code); +} + +void RaiseBrokenPipeSignal_nid_no_patch() { + RaiseWithCode(13, GuestSiKernel); +} + bool SiginfoFaultHandlerInstalled(int guest) { return guest >= 1 && guest <= MaxSignal && siginfoHandlers[guest].load() > 1 && !SignalBlocked(guest); } diff --git a/core/libs/tests/GuestKernelPipeBounds.cpp b/core/libs/tests/GuestKernelPipeBounds.cpp index ea55876c8da9..ebf34160c128 100644 --- a/core/libs/tests/GuestKernelPipeBounds.cpp +++ b/core/libs/tests/GuestKernelPipeBounds.cpp @@ -1,5 +1,6 @@ #include "prx/libkernel/include/Api.hpp" #include "prx/libc/include/Api.hpp" +#include "prx/libc/include/general/VabiMacros.hpp" #include #include @@ -132,6 +133,48 @@ void AuditBrokenPipeEpipeBounds() { Require(close_nid_postfix(broken[1]) == 0); } +int brokenPipeSignals = 0; +int brokenPipeSignal = 0; + +void APS5_VABI CountBrokenPipe(int signal) { + ++brokenPipeSignals; + brokenPipeSignal = signal; + *__error_nid_postfix() = 0; +} + +void AuditBrokenPipeRaisesSigpipe() { + constexpr int sigpipe = 13; + const auto ignore = reinterpret_cast(static_cast(1)); + int broken[2] = {-1, -1}; + Require(pipe_nid_postfix(broken) == 0); + Require(close_nid_postfix(broken[0]) == 0); + Require(signal_nid_postfix(sigpipe, CountBrokenPipe) != reinterpret_cast(static_cast(-1))); + + char byte = 'z'; + *__error_nid_postfix() = 0; + Require(write_nid_postfix(broken[1], &byte, 1) == -1); + Require(*__error_nid_postfix() == guestEpipe && brokenPipeSignals == 1 && brokenPipeSignal == sigpipe); + + *__error_nid_postfix() = 0; + Require(_write_nid_postfix(broken[1], &byte, 1) == -1); + Require(*__error_nid_postfix() == guestEpipe && brokenPipeSignals == 2); + + KernelIovec vector{&byte, 1}; + *__error_nid_postfix() = 0; + Require(writev_nid_postfix(broken[1], &vector, 1) == -1); + Require(*__error_nid_postfix() == guestEpipe && brokenPipeSignals == 3); + + int working[2] = {-1, -1}; + Require(pipe_nid_postfix(working) == 0); + Require(write_nid_postfix(working[1], &byte, 1) == 1 && brokenPipeSignals == 3); + + Require(signal_nid_postfix(sigpipe, ignore) == CountBrokenPipe); + Require(write_nid_postfix(broken[1], &byte, 1) == -1); + Require(*__error_nid_postfix() == guestEpipe && brokenPipeSignals == 3); + + for (const int descriptor : {broken[1], working[0], working[1]}) Require(close_nid_postfix(descriptor) == 0); +} + void AuditNonblockingReadWriteBounds() { int nbPipe[2] = {-1, -1}; Require(pipe_nid_postfix(nbPipe) == 0); @@ -214,6 +257,7 @@ int main() { AuditDescriptorAllocationAndRecycling(); AuditPipePayloadAndEofBounds(); AuditBrokenPipeEpipeBounds(); + AuditBrokenPipeRaisesSigpipe(); AuditNonblockingReadWriteBounds(); AuditCloseOnExecDescriptorFlags(); return 0; diff --git a/docs/dev/debt/changes/fix-libkernel-windows-pipe-sigpipe.md b/docs/dev/debt/changes/fix-libkernel-windows-pipe-sigpipe.md new file mode 100644 index 000000000000..599e83f02dfd --- /dev/null +++ b/docs/dev/debt/changes/fix-libkernel-windows-pipe-sigpipe.md @@ -0,0 +1,5 @@ +# libkernel + +## change [write to a pipe without a reader] + +- [write to a pipe without a reader](../../../core/libs/prx/libkernel/File/src/Open.cpp) (libkernel) - `write`, `sceKernelWrite`, `writev` and `sceKernelWritev` fail with `EPIPE` once the read end is closed, as FreeBSD 11.4's [`pipe_write`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_pipe.c#L1062-L1069) does, and send `SIGPIPE` to the writing thread, as [`dofilewrite`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/sys_generic.c#L597-L601) does. On Linux the host raises its own `SIGPIPE`, which is guest signal 13; on Windows, where the host reports `ERROR_NO_DATA`, the call raises guest signal 13 with `SI_KERNEL` once it returns, so a handler runs, a blocked signal stays pending and an ignored one is dropped, but the default action does not end the process there. A write on a guest socket on Windows raises nothing. Not measured on a console