From 5cc1ea83ca93b66816eb1ce4211bd5cbe49d21db Mon Sep 17 00:00:00 2001 From: Maximilian Fleckenstein <166870690+maximilianfv@users.noreply.github.com> Date: Sat, 24 Oct 2026 05:00:03 -0300 Subject: [PATCH 1/4] fix(libkernel): count a dissolved datagram association as disconnected on macOS On macOS, connect to AF_UNSPEC on a connected datagram socket dissolves the association but returns -1, with EAFNOSUPPORT for IPv4 and EINVAL for IPv6. Disconnect took that as a failure, so connect to port 0 on a connected datagram socket failed with 47 instead of EADDRNOTAVAIL (49). Treat the call as successful when the socket is no longer connected. Co-authored-by: marion909 <103169397+marion909@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- core/libs/prx/libkernel/Socket/src/Udp.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/core/libs/prx/libkernel/Socket/src/Udp.cpp b/core/libs/prx/libkernel/Socket/src/Udp.cpp index 106a7501bcee..d431317f4401 100644 --- a/core/libs/prx/libkernel/Socket/src/Udp.cpp +++ b/core/libs/prx/libkernel/Socket/src/Udp.cpp @@ -339,6 +339,14 @@ int Disconnect(const Socket& socket) { sockaddr_storage unspecified{}; unspecified.ss_family = local.ss_family; if (::connect(socket.value, reinterpret_cast(&unspecified), localSize) != 0) return Fail(NativeError()); +#elif defined(__APPLE__) + sockaddr unspecified{}; + unspecified.sa_family = AF_UNSPEC; + if (::connect(socket.value, &unspecified, sizeof(unspecified)) != 0) { + const int error = NativeError(); + peerSize = sizeof(peer); + if (::getpeername(socket.value, reinterpret_cast(&peer), &peerSize) == 0 || errno != ENOTCONN) return Fail(error); + } #else sockaddr unspecified{}; unspecified.sa_family = AF_UNSPEC; From 55e1557a1fe106358404b9a79b7ca4f53acc2219 Mon Sep 17 00:00:00 2001 From: ruby_sc <169863926+Buraddomoon@users.noreply.github.com> Date: Wed, 28 Oct 2026 11:59:42 -0300 Subject: [PATCH 2/4] fix(libkernel): clear reported local address after UDP disconnect Preserve the local port while reporting a wildcard address after a datagram association is dissolved. Share the state across descriptor aliases and clear it on a successful bind or reconnect. Adapted from the fix branch linked in issue #8681, with expanded binding, reconnect and descriptor-alias regression coverage. Co-authored-by: xXPBSXx <20614131+xXPBSXx@users.noreply.github.com> --- core/libs/prx/libkernel/Socket/src/Udp.cpp | 34 ++++++++-- core/libs/tests/GuestUdp.cpp | 75 +++++++++++++++++++++- docs/dev/debt/libkernel.md | 2 +- 3 files changed, 102 insertions(+), 9 deletions(-) diff --git a/core/libs/prx/libkernel/Socket/src/Udp.cpp b/core/libs/prx/libkernel/Socket/src/Udp.cpp index de7b3a11d1fc..a022c9586e02 100644 --- a/core/libs/prx/libkernel/Socket/src/Udp.cpp +++ b/core/libs/prx/libkernel/Socket/src/Udp.cpp @@ -125,6 +125,7 @@ struct Socket : KernelSocketPoll::NativeOwner { GuestSockets::SocketOptionState options; bool receiveShut = false; bool sendShut = false; + bool localAddressCleared = false; std::uint32_t inode = 0; std::weak_ptr peer; Socket(NativeSocket value, int family, int type) : value(value), family(family), type(type) {} @@ -341,7 +342,20 @@ std::int64_t ZeroPortSend(const Socket& socket, const sockaddr_storage& destinat if (bound && remote.sin_addr.s_addr != htonl(INADDR_ANY) && remote.sin_addr.s_addr != htonl(INADDR_BROADCAST)) return static_cast(length); return Fail(49); } -int Disconnect(const Socket& socket) { +void ClearLocalAddress(Socket& socket, bool cleared) { + std::lock_guard lock(socket.modeMutex); + socket.localAddressCleared = cleared; +} +int LocalAddress(Socket& socket, sockaddr_storage& address, socklen_t& size) { + if (::getsockname(socket.value, reinterpret_cast(&address), &size)) return -1; + std::lock_guard lock(socket.modeMutex); + if (socket.localAddressCleared) { + if (address.ss_family == AF_INET) reinterpret_cast(address).sin_addr = {}; + else if (address.ss_family == AF_INET6) reinterpret_cast(address).sin6_addr = {}; + } + return 0; +} +int Disconnect(Socket& socket) { sockaddr_storage peer{}; socklen_t peerSize = sizeof(peer); if (::getpeername(socket.value, reinterpret_cast(&peer), &peerSize) != 0) return 0; @@ -360,14 +374,18 @@ int Disconnect(const Socket& socket) { sockaddr_storage current{}; socklen_t currentSize = sizeof(current); if (::getsockname(socket.value, reinterpret_cast(¤t), ¤tSize) != 0) return Fail(NativeError()); - if (PortOf(current) != 0) return 0; + if (PortOf(current) != 0) { + ClearLocalAddress(socket, true); + return 0; + } sockaddr_storage wildcard{}; wildcard.ss_family = local.ss_family; PortOf(wildcard) = PortOf(local); if (::bind(socket.value, reinterpret_cast(&wildcard), localSize) != 0) return Fail(NativeError()); + ClearLocalAddress(socket, true); return 0; } -bool ZeroPortConnect(const Socket& socket, const void* address, std::uint32_t length) { +bool ZeroPortConnect(Socket& socket, const void* address, std::uint32_t length) { if (!address || (socket.type != 1 && socket.type != 2) || (socket.family != 2 && socket.family != 28)) return false; const auto* bytes = static_cast(address); if (length < (socket.family == 2 ? 16u : 28u) || bytes[1] != socket.family || (bytes[2] | bytes[3]) != 0) return false; @@ -1321,7 +1339,9 @@ int APS5_VABI connect_nid_postfix(int descriptor, const void* address, std::uint std::lock_guard lock(socket->modeMutex); socket->options.reportedError = 0; } - return ::connect(socket->value, reinterpret_cast(&native), size) ? Fail(ConnectError()) : 0; + if (::connect(socket->value, reinterpret_cast(&native), size)) return Fail(ConnectError()); + ClearLocalAddress(*socket, false); + return 0; } int APS5_VABI listen_nid_postfix(int descriptor, int backlog) { const auto socket = Lookup(descriptor); @@ -1409,7 +1429,9 @@ int APS5_VABI bind_nid_postfix(int descriptor, const void* address, std::uint32_ const int mapped = LongMappedDualStack(*socket, address, length); if (mapped < 0) return -1; if (!Address(socket->family, address, mapped ? 28 : length, native, size)) return -1; - return ::bind(socket->value, reinterpret_cast(&native), size) ? Fail(NativeError()) : 0; + if (::bind(socket->value, reinterpret_cast(&native), size)) return Fail(NativeError()); + ClearLocalAddress(*socket, false); + return 0; } namespace { bool AlreadyShut(NativeSocket socket, int how) { @@ -1454,7 +1476,7 @@ int APS5_VABI getsockname_nid_postfix(int descriptor, void* address, std::uint32 if (socket->family == GuestUnix) return UnnamedAddress(address, length); sockaddr_storage native{}; socklen_t size = sizeof(native); - if (::getsockname(socket->value, reinterpret_cast(&native), &size)) { + if (LocalAddress(*socket, native, size)) { #ifdef _WIN32 if (WSAGetLastError() != WSAEINVAL) return Fail(NativeError()); native = {}; diff --git a/core/libs/tests/GuestUdp.cpp b/core/libs/tests/GuestUdp.cpp index a2dd3d37ccde..760dfedf4c83 100644 --- a/core/libs/tests/GuestUdp.cpp +++ b/core/libs/tests/GuestUdp.cpp @@ -92,6 +92,77 @@ static void CheckAddressLengths(int family) { Require(close_nid_postfix(sender) == 0 && close_nid_postfix(receiver) == 0); } +static void CheckDissolvedName(int family, int binding) { + const std::uint32_t addressSize = family == 2 ? 16 : 28; + const int peer = socket_nid_postfix(family, 2, 0); + if (peer < 0 && family == 28 && *__error_nid_postfix() == 47) return; + Require(peer >= 0); + std::array peerAddress{}; + peerAddress[0] = static_cast(addressSize); + peerAddress[1] = static_cast(family); + if (family == 2) { peerAddress[4] = 127; peerAddress[7] = 1; } + else peerAddress[23] = 1; + const int bound = bind_nid_postfix(peer, peerAddress.data(), addressSize); + if (bound < 0 && family == 28 && *__error_nid_postfix() == 49) { + Require(close_nid_postfix(peer) == 0); + return; + } + Require(bound == 0); + std::uint32_t size = peerAddress.size(); + Require(getsockname_nid_postfix(peer, peerAddress.data(), &size) == 0 && size == addressSize); + int dissolved = socket_nid_postfix(family, 2, 0); + Require(dissolved >= 0); + if (binding != 0) { + auto local = peerAddress; + local[2] = local[3] = 0; + Require(bind_nid_postfix(dissolved, local.data(), addressSize) == 0); + if (binding == 2) { + size = local.size(); + Require(getsockname_nid_postfix(dissolved, local.data(), &size) == 0 && size == addressSize); + Require(close_nid_postfix(dissolved) == 0); + dissolved = socket_nid_postfix(family, 2, 0); + Require(dissolved >= 0); + Require(bind_nid_postfix(dissolved, local.data(), addressSize) == 0); + } + } + const int alias = dup_nid_postfix(dissolved); + Require(alias >= 0); + Require(connect_nid_postfix(dissolved, peerAddress.data(), addressSize) == 0); + std::array connected{}; + size = connected.size(); + Require(getsockname_nid_postfix(alias, connected.data(), &size) == 0 && size == addressSize); + Require(connected[2] || connected[3]); + const auto begin = family == 2 ? 4 : 8; + const auto end = family == 2 ? 8 : 24; + Require(std::equal(connected.begin() + begin, connected.begin() + end, peerAddress.begin() + begin)); + for (const bool portZero : {false, true}) { + auto disconnect = peerAddress; + if (portZero) disconnect[2] = disconnect[3] = 0; + else disconnect[1] = 0; + Require(connect_nid_postfix(alias, disconnect.data(), addressSize) == -1 && *__error_nid_postfix() == (portZero ? 49 : 47)); + for (const int descriptor : {dissolved, alias}) { + std::array name{}; + size = name.size(); + Require(getpeername_nid_postfix(descriptor, name.data(), &size) == -1 && *__error_nid_postfix() == 57); + for (int attempt = 0; attempt < 2; ++attempt) { + size = name.size(); + Require(getsockname_nid_postfix(descriptor, name.data(), &size) == 0 && size == addressSize); + Require(name[2] == connected[2] && name[3] == connected[3]); + Require(std::all_of(name.begin() + begin, name.begin() + end, [](auto byte) { return byte == 0; })); + } + } + Require(connect_nid_postfix(dissolved, peerAddress.data(), addressSize) == 0); + std::array reconnected{}; + size = reconnected.size(); + Require(getsockname_nid_postfix(alias, reconnected.data(), &size) == 0 && size == addressSize); + Require(std::equal(connected.begin(), connected.begin() + addressSize, reconnected.begin())); + Require(send_nid_postfix(alias, "x", 1, 0) == 1); + char payload = 0; + Require(recv_nid_postfix(peer, &payload, 1, 0) == 1 && payload == 'x'); + } + Require(close_nid_postfix(alias) == 0 && close_nid_postfix(dissolved) == 0 && close_nid_postfix(peer) == 0); +} + static void CheckSendToPortZero() { const int receiver = socket_nid_postfix(2, 2, 0); const int sender = socket_nid_postfix(2, 2, 0); @@ -257,6 +328,8 @@ static void CheckArrivalTimestamps(int family) { int main() { CheckAddressLengths(2); CheckAddressLengths(28); + for (const int family : {2, 28}) + for (int binding = 0; binding < 3; ++binding) CheckDissolvedName(family, binding); CheckArrivalTimestamps(2); CheckArrivalTimestamps(28); CheckDontWait(); @@ -406,9 +479,7 @@ int main() { size = localName.size(); Require(getsockname_nid_postfix(dissolved, localName.data(), &size) == 0); Require(localName[2] == dissolvedAddress[2] && localName[3] == dissolvedAddress[3]); -#ifndef _WIN32 Require(localName[4] == 0 && localName[5] == 0 && localName[6] == 0 && localName[7] == 0); -#endif Require(send_nid_postfix(dissolved, "x", 1, 0) == -1 && *__error_nid_postfix() == 39); const char unrelated[] = "unrelated sender"; Require(sendto_nid_postfix(other, unrelated, sizeof(unrelated), 0, dissolvedAddress.data(), dissolvedAddress.size()) == sizeof(unrelated)); diff --git a/docs/dev/debt/libkernel.md b/docs/dev/debt/libkernel.md index ace3b493a0e7..cf9de0e39f6d 100644 --- a/docs/dev/debt/libkernel.md +++ b/docs/dev/debt/libkernel.md @@ -264,7 +264,7 @@ - [SO_NOSIGPIPE](../../../core/libs/prx/libkernel/Socket/src/SocketOptions.cpp) (libkernel) - kept per socket and read back as its bit. On Linux a `send`, `sendto`, `sendmsg` or `write` on that socket passes `MSG_NOSIGNAL` to the host, as [`kern_sendit`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_syscalls.c#L890-L895) and [`soo_write`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/sys_socket.c#L147-L151) leave out the signal, and an accepted socket takes it from its listener, as [`sonewconn`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_socket.c#L603) copies the options and as measured on PS5 firmware 13.60. Whether a write on it raises `SIGPIPE` there was not measured - [SO_SNDTIMEO, SO_RCVTIMEO](../../../core/libs/prx/libkernel/Socket/src/SocketOptions.cpp) (libkernel) - the value checks and the 32.32 fixed-point read-back follow [`sosetopt`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/uipc_socket.c#L2657-L2689), as a payload process on a PS5 (firmware 13.60) answered them. A receive or send on a blocking socket waits that long for the host to report the socket readable or writable and fails with `EWOULDBLOCK`, but only before the first transfer: once writable the host send has no limit, where FreeBSD returns the bytes sent so far, and a receive whose data another thread takes after the wait blocks again. `sendfile` ignores the send timeout, the host wait has millisecond steps, and an accepted socket takes both from its listener - [IPV6_V6ONLY](../../../core/libs/prx/libkernel/Socket/src/SocketOptions.cpp) (libkernel) - the `IPPROTO_IPV6` option 27 is set on the host socket and reads back as 0 or 1, after FreeBSD's [`ip6_ctloutput`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/netinet6/ip6_output.c#L1716-L1732): a value that is not exactly an `int` fails with `EINVAL`, as does setting it on a bound socket, which the host refuses too. `socket` turns it on for every `AF_INET6` socket, as a payload process on a PS5 (firmware 13.60) reported, and Java turns it off on the sockets it opens: Java 25 Advanced Network (PPSA99871) stops at `java.net.SocketException: Protocol not available` without it. The value checks and the bound-socket answer were not measured -- [connect to AF_UNSPEC](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) dissolves a datagram association as FreeBSD 11.4's `udp_disconnect()` does, which resets the local address to the wildcard address and keeps the port. On Linux and on Windows a socket bound to an explicit port keeps its local address as well (`127.0.0.1:` stays `127.0.0.1:`), because a bound socket cannot be bound again, and `getsockname` reports that host address. On Windows a `sendto` to a non-loopback address from such a socket then fails with `ENETUNREACH` (Winsock 10051, measured on Windows 10), where FreeBSD sends it from the wildcard address. One bound to port 0 is bound again to the wildcard address with the port it had +- [connect to AF_UNSPEC](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) - on Linux and Windows the native socket can retain its explicit local address after disconnection. On Windows a `sendto` to a non-loopback address from such a socket then fails with `ENETUNREACH` (Winsock 10051, measured on Windows 10), where FreeBSD 11.4 sends it from the wildcard address. Reporting the wildcard address through `getsockname` does not change that native binding - [connect](../../../core/libs/prx/libkernel/Socket/src/Udp.cpp) (libkernel) - `connect` to port 0 on an `AF_INET` or `AF_INET6` stream or datagram socket fails with `EADDRNOTAVAIL` (49), and on a connected datagram socket it disconnects first, as on a PS5 (firmware 13.60) and in FreeBSD 11.4's `soconnect()`. A connected stream socket keeps its host `EISCONN` answer. Known gap: `sendto` and `sendmsg` to port 0 on an `AF_INET` datagram socket fail with 49 (measured for an unbound socket) unless the socket is bound to a specific address and port and the destination is neither `INADDR_ANY` nor `INADDR_BROADCAST`, which is the path where `udp_output()` skips `in_pcbconnect_setup()` and FreeBSD sends to port 0. There the call returns the length without the host, as Linux's `udp_sendmsg` refuses port 0 with `EINVAL`; an `AF_INET6` socket still goes to the host. [sceNetConnect](../../../core/libs/prx/libSceNet/Export.cpp) is not changed - [msync](../../../core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp) (libkernel) follows FreeBSD 11's `kern_msync` and `vm_map_sync`: the address is rounded down to the page, a range that wraps is `EINVAL`, as is `MS_ASYNC` with `MS_INVALIDATE`, and a range whose start or any page is not mapped (as `sceKernelVirtualQuery` reports it, reservations included) is `ENOMEM`; a page unmapped from inside a larger mapping still counts as mapped there, since `sceKernelVirtualQuery` keeps reporting the whole mapping. The guest has no file mappings, so a successful call writes nothing back. `MS_INVALIDATE` on a mapped range fails with `EBUSY`, as it did in a payload process on a PS5 (firmware 13.60) for anonymous memory, locked or not, also with a hole unmapped inside it; a title process, reservations, direct memory and flexible memory were not measured - Guest memory protection on macOS ([mprotect](../../../core/libs/prx/libkernel/DirectMemory/DirectMemory.cpp), [sceKernelVirtualQuery](../../../core/libs/prx/libkernel/DirectMemory/Export.cpp)): a rounded edge smaller than a guest page at the start of a request that lies in the gap between two segments of an image, where no host page exists, is skipped instead of protected, and for memory outside the guest allocation registry `sceKernelVirtualQuery` reports the host's extent instead of the guest's: the region `mach_vm_region` returns, cut to the write watch run that holds the address. The guest rounds a range to its 16 KiB pages, counted from where the ELF address 0 of the image holding it was loaded, which is a multiple of the host page size only (4 KiB under Rosetta). `sceKernelVirtualQuery` takes a watched page's write permission from the write watch. From 8478890c36766603dc957bc7b07d6902cbf2e7b3 Mon Sep 17 00:00:00 2001 From: ruby_sc <169863926+Buraddomoon@users.noreply.github.com> Date: Wed, 28 Oct 2026 12:13:15 -0300 Subject: [PATCH 3/4] fix(libkernel): serialize UDP address state transitions Keep native UDP bind, connect and disconnect operations coherent with the shared reported-address state. Reuse the existing connect lock and avoid additional locking on stream sockets. Add a controlled overlapping disconnect/reconnect regression and check truncated address writes after disconnection. Clear an obsolete IPv6 scope ID when reporting the wildcard address. --- core/libs/prx/libkernel/CMakeLists.txt | 8 +++ core/libs/prx/libkernel/Socket/src/Udp.cpp | 33 ++++++----- core/libs/tests/GuestUdp.cpp | 17 ++++-- core/libs/tests/GuestUdpNameConcurrency.cpp | 61 +++++++++++++++++++++ 4 files changed, 99 insertions(+), 20 deletions(-) create mode 100644 core/libs/tests/GuestUdpNameConcurrency.cpp diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index 2a70b082f255..a834251a1936 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -1628,6 +1628,14 @@ if(BUILD_TESTING) configure_windows_unwind(guest_udp_tests) add_test(NAME guest_udp COMMAND guest_udp_tests) set_tests_properties(guest_udp PROPERTIES TIMEOUT 20) + if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + add_test_executable(guest_udp_name_concurrency_tests ${LIBS_INCLUDE_DIR}/tests/GuestUdpNameConcurrency.cpp) + target_include_directories(guest_udp_name_concurrency_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_udp_name_concurrency_tests PRIVATE libkernel libc) + target_link_options(guest_udp_name_concurrency_tests PRIVATE LINKER:--export-dynamic-symbol=connect) + add_test(NAME guest_udp_name_concurrency COMMAND guest_udp_name_concurrency_tests) + set_tests_properties(guest_udp_name_concurrency PROPERTIES TIMEOUT 10) + endif() if(WIN32) set_tests_properties(guest_udp PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$") diff --git a/core/libs/prx/libkernel/Socket/src/Udp.cpp b/core/libs/prx/libkernel/Socket/src/Udp.cpp index a022c9586e02..5e83e411c0c5 100644 --- a/core/libs/prx/libkernel/Socket/src/Udp.cpp +++ b/core/libs/prx/libkernel/Socket/src/Udp.cpp @@ -342,20 +342,22 @@ std::int64_t ZeroPortSend(const Socket& socket, const sockaddr_storage& destinat if (bound && remote.sin_addr.s_addr != htonl(INADDR_ANY) && remote.sin_addr.s_addr != htonl(INADDR_BROADCAST)) return static_cast(length); return Fail(49); } -void ClearLocalAddress(Socket& socket, bool cleared) { - std::lock_guard lock(socket.modeMutex); - socket.localAddressCleared = cleared; -} int LocalAddress(Socket& socket, sockaddr_storage& address, socklen_t& size) { + std::unique_lock lock(socket.modeMutex, std::defer_lock); + if (socket.type == 2) lock.lock(); if (::getsockname(socket.value, reinterpret_cast(&address), &size)) return -1; - std::lock_guard lock(socket.modeMutex); - if (socket.localAddressCleared) { + if (socket.type == 2 && socket.localAddressCleared) { if (address.ss_family == AF_INET) reinterpret_cast(address).sin_addr = {}; - else if (address.ss_family == AF_INET6) reinterpret_cast(address).sin6_addr = {}; + else if (address.ss_family == AF_INET6) { + auto& v6 = reinterpret_cast(address); + v6.sin6_addr = {}; + v6.sin6_scope_id = 0; + } } return 0; } int Disconnect(Socket& socket) { + std::lock_guard lock(socket.modeMutex); sockaddr_storage peer{}; socklen_t peerSize = sizeof(peer); if (::getpeername(socket.value, reinterpret_cast(&peer), &peerSize) != 0) return 0; @@ -375,14 +377,14 @@ int Disconnect(Socket& socket) { socklen_t currentSize = sizeof(current); if (::getsockname(socket.value, reinterpret_cast(¤t), ¤tSize) != 0) return Fail(NativeError()); if (PortOf(current) != 0) { - ClearLocalAddress(socket, true); + socket.localAddressCleared = true; return 0; } sockaddr_storage wildcard{}; wildcard.ss_family = local.ss_family; PortOf(wildcard) = PortOf(local); if (::bind(socket.value, reinterpret_cast(&wildcard), localSize) != 0) return Fail(NativeError()); - ClearLocalAddress(socket, true); + socket.localAddressCleared = true; return 0; } bool ZeroPortConnect(Socket& socket, const void* address, std::uint32_t length) { @@ -1335,12 +1337,11 @@ int APS5_VABI connect_nid_postfix(int descriptor, const void* address, std::uint if (socket->type == 1) RefuseClosedLoopbackAtOnce_nid_no_patch(socket->value, reinterpret_cast(&native)); #endif if (ZeroPortConnect(*socket, address, length)) return -1; - { - std::lock_guard lock(socket->modeMutex); - socket->options.reportedError = 0; - } + std::unique_lock lock(socket->modeMutex); + socket->options.reportedError = 0; + if (socket->type != 2) lock.unlock(); if (::connect(socket->value, reinterpret_cast(&native), size)) return Fail(ConnectError()); - ClearLocalAddress(*socket, false); + if (socket->type == 2) socket->localAddressCleared = false; return 0; } int APS5_VABI listen_nid_postfix(int descriptor, int backlog) { @@ -1429,8 +1430,10 @@ int APS5_VABI bind_nid_postfix(int descriptor, const void* address, std::uint32_ const int mapped = LongMappedDualStack(*socket, address, length); if (mapped < 0) return -1; if (!Address(socket->family, address, mapped ? 28 : length, native, size)) return -1; + std::unique_lock lock(socket->modeMutex, std::defer_lock); + if (socket->type == 2) lock.lock(); if (::bind(socket->value, reinterpret_cast(&native), size)) return Fail(NativeError()); - ClearLocalAddress(*socket, false); + if (socket->type == 2) socket->localAddressCleared = false; return 0; } namespace { diff --git a/core/libs/tests/GuestUdp.cpp b/core/libs/tests/GuestUdp.cpp index 760dfedf4c83..ec80b654d41d 100644 --- a/core/libs/tests/GuestUdp.cpp +++ b/core/libs/tests/GuestUdp.cpp @@ -144,11 +144,18 @@ static void CheckDissolvedName(int family, int binding) { std::array name{}; size = name.size(); Require(getpeername_nid_postfix(descriptor, name.data(), &size) == -1 && *__error_nid_postfix() == 57); - for (int attempt = 0; attempt < 2; ++attempt) { - size = name.size(); - Require(getsockname_nid_postfix(descriptor, name.data(), &size) == 0 && size == addressSize); - Require(name[2] == connected[2] && name[3] == connected[3]); - Require(std::all_of(name.begin() + begin, name.begin() + end, [](auto byte) { return byte == 0; })); + size = name.size(); + Require(getsockname_nid_postfix(descriptor, name.data(), &size) == 0 && size == addressSize); + Require(name[2] == connected[2] && name[3] == connected[3]); + Require(std::all_of(name.begin() + begin, name.begin() + end, [](auto byte) { return byte == 0; })); + if (family == 28) Require(std::all_of(name.begin() + 24, name.end(), [](auto byte) { return byte == 0; })); + for (const std::uint32_t capacity : {0u, 1u, 6u, addressSize}) { + std::array truncated; + truncated.fill(0xa5); + size = capacity; + Require(getsockname_nid_postfix(descriptor, truncated.data(), &size) == 0 && size == capacity); + Require(std::equal(name.begin(), name.begin() + size, truncated.begin())); + Require(std::all_of(truncated.begin() + size, truncated.end(), [](auto byte) { return byte == 0xa5; })); } } Require(connect_nid_postfix(dissolved, peerAddress.data(), addressSize) == 0); diff --git a/core/libs/tests/GuestUdpNameConcurrency.cpp b/core/libs/tests/GuestUdpNameConcurrency.cpp new file mode 100644 index 000000000000..fc185213250f --- /dev/null +++ b/core/libs/tests/GuestUdpNameConcurrency.cpp @@ -0,0 +1,61 @@ +#include "prx/libkernel/include/Api.hpp" +#include "prx/libkernel/Socket/include/SocketRuntime.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +static std::atomic pausedDescriptor{-1}; +static std::promise connectPaused; +static std::promise continueConnect; +static std::shared_future resumeConnect = continueConnect.get_future().share(); + +extern "C" int connect(int descriptor, const sockaddr* address, socklen_t size) { + const int result = static_cast(::syscall(SYS_connect, descriptor, address, size)); + int expected = descriptor; + if (result == 0 && pausedDescriptor.compare_exchange_strong(expected, -1)) { + connectPaused.set_value(); + resumeConnect.wait(); + } + return result; +} + +static void Require(bool value) { if (!value) std::abort(); } + +int main() { + const int peer = socket_nid_postfix(2, 2, 0); + const int client = socket_nid_postfix(2, 2, 0); + Require(peer >= 0 && client >= 0); + std::array destination{16, 2, 0, 0, 127, 0, 0, 1}; + Require(bind_nid_postfix(peer, destination.data(), destination.size()) == 0); + std::uint32_t size = destination.size(); + Require(getsockname_nid_postfix(peer, destination.data(), &size) == 0); + Require(connect_nid_postfix(client, destination.data(), destination.size()) == 0); + std::array local{}; + size = local.size(); + Require(getsockname_nid_postfix(client, local.data(), &size) == 0); + const auto native = GuestSockets::NativeDescriptor(client); + Require(native >= 0); + pausedDescriptor = static_cast(native); + const std::array unspecified{16, 0}; + auto disconnect = std::async(std::launch::async, [&] { + return connect_nid_postfix(client, unspecified.data(), unspecified.size()); + }); + connectPaused.get_future().wait(); + auto reconnect = std::async(std::launch::async, [&] { + return connect_nid_postfix(client, destination.data(), destination.size()); + }); + reconnect.wait_for(std::chrono::milliseconds(250)); + continueConnect.set_value(); + Require(disconnect.get() == -1 && reconnect.get() == 0); + std::array name{}; + size = name.size(); + Require(getpeername_nid_postfix(client, name.data(), &size) == 0 && name == destination); + size = name.size(); + Require(getsockname_nid_postfix(client, name.data(), &size) == 0 && name == local); + Require(close_nid_postfix(client) == 0 && close_nid_postfix(peer) == 0); +} From 9a4d2462d3e5fe2fc38a49348bd1a45cf55458f3 Mon Sep 17 00:00:00 2001 From: ruby_sc <169863926+Buraddomoon@users.noreply.github.com> Date: Wed, 28 Oct 2026 13:34:23 -0300 Subject: [PATCH 4/4] fix(libkernel): verify UDP disconnect outcomes after native errors Reuse the macOS disconnect prerequisite in the common POSIX path. Proceed only when getpeername positively reports ENOTCONN, retaining the original connect error for connected or indeterminate sockets. Exercise actual UDP sockets with injected EAFNOSUPPORT and EINVAL results for IPv4 and IPv6, both disconnect entry points, all three binding modes, and still-connected or failed peer-query outcomes. --- core/libs/prx/libkernel/CMakeLists.txt | 7 ++ core/libs/prx/libkernel/Socket/src/Udp.cpp | 6 +- core/libs/tests/GuestUdpDisconnectResult.cpp | 116 +++++++++++++++++++ 3 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 core/libs/tests/GuestUdpDisconnectResult.cpp diff --git a/core/libs/prx/libkernel/CMakeLists.txt b/core/libs/prx/libkernel/CMakeLists.txt index a834251a1936..e5ff9a32b72e 100644 --- a/core/libs/prx/libkernel/CMakeLists.txt +++ b/core/libs/prx/libkernel/CMakeLists.txt @@ -1635,6 +1635,13 @@ if(BUILD_TESTING) target_link_options(guest_udp_name_concurrency_tests PRIVATE LINKER:--export-dynamic-symbol=connect) add_test(NAME guest_udp_name_concurrency COMMAND guest_udp_name_concurrency_tests) set_tests_properties(guest_udp_name_concurrency PROPERTIES TIMEOUT 10) + add_test_executable(guest_udp_disconnect_result_tests ${LIBS_INCLUDE_DIR}/tests/GuestUdpDisconnectResult.cpp) + target_include_directories(guest_udp_disconnect_result_tests PRIVATE ${LIBS_INCLUDE_DIR}) + target_link_libraries(guest_udp_disconnect_result_tests PRIVATE libkernel libc) + target_link_options(guest_udp_disconnect_result_tests PRIVATE + LINKER:--export-dynamic-symbol=connect LINKER:--export-dynamic-symbol=getpeername) + add_test(NAME guest_udp_disconnect_result COMMAND guest_udp_disconnect_result_tests) + set_tests_properties(guest_udp_disconnect_result PROPERTIES TIMEOUT 10) endif() if(WIN32) set_tests_properties(guest_udp PROPERTIES ENVIRONMENT_MODIFICATION diff --git a/core/libs/prx/libkernel/Socket/src/Udp.cpp b/core/libs/prx/libkernel/Socket/src/Udp.cpp index 0e087fbf774c..a1e6abf75d10 100644 --- a/core/libs/prx/libkernel/Socket/src/Udp.cpp +++ b/core/libs/prx/libkernel/Socket/src/Udp.cpp @@ -368,7 +368,7 @@ int Disconnect(Socket& socket) { sockaddr_storage unspecified{}; unspecified.ss_family = local.ss_family; if (::connect(socket.value, reinterpret_cast(&unspecified), localSize) != 0) return Fail(NativeError()); -#elif defined(__APPLE__) +#else sockaddr unspecified{}; unspecified.sa_family = AF_UNSPEC; if (::connect(socket.value, &unspecified, sizeof(unspecified)) != 0) { @@ -376,10 +376,6 @@ int Disconnect(Socket& socket) { peerSize = sizeof(peer); if (::getpeername(socket.value, reinterpret_cast(&peer), &peerSize) == 0 || errno != ENOTCONN) return Fail(error); } -#else - sockaddr unspecified{}; - unspecified.sa_family = AF_UNSPEC; - if (::connect(socket.value, &unspecified, sizeof(unspecified)) != 0) return Fail(NativeError()); #endif sockaddr_storage current{}; socklen_t currentSize = sizeof(current); diff --git a/core/libs/tests/GuestUdpDisconnectResult.cpp b/core/libs/tests/GuestUdpDisconnectResult.cpp new file mode 100644 index 000000000000..f5911130b0ae --- /dev/null +++ b/core/libs/tests/GuestUdpDisconnectResult.cpp @@ -0,0 +1,116 @@ +#include "prx/libkernel/include/Api.hpp" +#include "prx/libkernel/Socket/include/SocketRuntime.hpp" +#include "prx/libc/include/Api.hpp" +#include +#include +#include +#include +#include +#include +#include +#include + +enum class NativeResult { Disconnected, Connected, UnknownPeer }; +static int injectedDescriptor = -1; +static int injectedError = 0; +static int peerError = 0; +static NativeResult injectedResult = NativeResult::Disconnected; + +extern "C" int connect(int descriptor, const sockaddr* address, socklen_t size) { + if (descriptor != injectedDescriptor || address->sa_family != AF_UNSPEC) + return static_cast(::syscall(SYS_connect, descriptor, address, size)); + if (injectedResult != NativeResult::Connected) { + const int result = static_cast(::syscall(SYS_connect, descriptor, address, size)); + if (result != 0) return result; + } + if (injectedResult == NativeResult::UnknownPeer) peerError = EACCES; + errno = injectedError; + return -1; +} + +extern "C" int getpeername(int descriptor, sockaddr* address, socklen_t* size) { + if (descriptor == injectedDescriptor && peerError != 0) { + errno = std::exchange(peerError, 0); + return -1; + } + return static_cast(::syscall(SYS_getpeername, descriptor, address, size)); +} + +static void Require(bool value) { if (!value) std::abort(); } + +static void CheckResult(int family, int binding, int error, NativeResult result, bool portZero) { + const std::uint32_t addressSize = family == 2 ? 16 : 28; + const int peer = socket_nid_postfix(family, 2, 0); + if (peer < 0 && family == 28 && *__error_nid_postfix() == 47) return; + Require(peer >= 0); + std::array destination{}; + destination[0] = static_cast(addressSize); + destination[1] = static_cast(family); + if (family == 2) { destination[4] = 127; destination[7] = 1; } + else destination[23] = 1; + const int bound = bind_nid_postfix(peer, destination.data(), addressSize); + if (bound < 0 && family == 28 && *__error_nid_postfix() == 49) { + Require(close_nid_postfix(peer) == 0); + return; + } + Require(bound == 0); + std::uint32_t size = destination.size(); + Require(getsockname_nid_postfix(peer, destination.data(), &size) == 0 && size == addressSize); + int client = socket_nid_postfix(family, 2, 0); + Require(client >= 0); + if (binding != 0) { + auto local = destination; + local[2] = local[3] = 0; + Require(bind_nid_postfix(client, local.data(), addressSize) == 0); + if (binding == 2) { + size = local.size(); + Require(getsockname_nid_postfix(client, local.data(), &size) == 0); + Require(close_nid_postfix(client) == 0); + client = socket_nid_postfix(family, 2, 0); + Require(client >= 0 && bind_nid_postfix(client, local.data(), addressSize) == 0); + } + } + Require(connect_nid_postfix(client, destination.data(), addressSize) == 0); + std::array before{}; + size = before.size(); + Require(getsockname_nid_postfix(client, before.data(), &size) == 0 && size == addressSize); + Require(before[2] || before[3]); + const auto native = GuestSockets::NativeDescriptor(client); + Require(native >= 0); + injectedDescriptor = static_cast(native); + injectedError = error; + injectedResult = result; + auto disconnect = destination; + if (portZero) disconnect[2] = disconnect[3] = 0; + else disconnect[1] = 0; + const int expected = result == NativeResult::Disconnected ? (portZero ? 49 : 47) : (error == EINVAL ? 22 : 47); + Require(connect_nid_postfix(client, disconnect.data(), addressSize) == -1 && *__error_nid_postfix() == expected); + injectedDescriptor = -1; + Require(peerError == 0); + std::array name{}; + size = name.size(); + if (result == NativeResult::Connected) { + Require(getpeername_nid_postfix(client, name.data(), &size) == 0 && name == destination); + size = name.size(); + Require(getsockname_nid_postfix(client, name.data(), &size) == 0 && name == before); + } else { + Require(getpeername_nid_postfix(client, name.data(), &size) == -1 && *__error_nid_postfix() == 57); + if (result == NativeResult::Disconnected) { + size = name.size(); + Require(getsockname_nid_postfix(client, name.data(), &size) == 0 && size == addressSize); + Require(name[2] == before[2] && name[3] == before[3]); + const auto begin = family == 2 ? 4 : 8; + const auto end = family == 2 ? 8 : 28; + Require(std::all_of(name.begin() + begin, name.begin() + end, [](auto byte) { return byte == 0; })); + } + } + Require(close_nid_postfix(client) == 0 && close_nid_postfix(peer) == 0); +} + +int main() { + for (const int family : {2, 28}) + for (int binding = 0; binding < 3; ++binding) + for (const int error : {EAFNOSUPPORT, EINVAL}) + for (const auto result : {NativeResult::Disconnected, NativeResult::Connected, NativeResult::UnknownPeer}) + for (const bool portZero : {false, true}) CheckResult(family, binding, error, result, portZero); +}