Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions core/libs/prx/libkernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1628,6 +1628,21 @@ 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)
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
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
Expand Down
51 changes: 40 additions & 11 deletions core/libs/prx/libkernel/Socket/src/Udp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Socket> peer;
Socket(NativeSocket value, int family, int type) : value(value), family(family), type(type) {}
Expand Down Expand Up @@ -341,7 +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<std::int64_t>(length);
return Fail(49);
}
int Disconnect(const Socket& socket) {
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<sockaddr*>(&address), &size)) return -1;
if (socket.type == 2 && socket.localAddressCleared) {
if (address.ss_family == AF_INET) reinterpret_cast<sockaddr_in&>(address).sin_addr = {};
else if (address.ss_family == AF_INET6) {
auto& v6 = reinterpret_cast<sockaddr_in6&>(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<sockaddr*>(&peer), &peerSize) != 0) return 0;
Expand All @@ -355,19 +371,27 @@ int Disconnect(const Socket& socket) {
#else
sockaddr unspecified{};
unspecified.sa_family = AF_UNSPEC;
if (::connect(socket.value, &unspecified, sizeof(unspecified)) != 0) return Fail(NativeError());
if (::connect(socket.value, &unspecified, sizeof(unspecified)) != 0) {
const int error = NativeError();
peerSize = sizeof(peer);
if (::getpeername(socket.value, reinterpret_cast<sockaddr*>(&peer), &peerSize) == 0 || errno != ENOTCONN) return Fail(error);
}
#endif
sockaddr_storage current{};
socklen_t currentSize = sizeof(current);
if (::getsockname(socket.value, reinterpret_cast<sockaddr*>(&current), &currentSize) != 0) return Fail(NativeError());
if (PortOf(current) != 0) return 0;
if (PortOf(current) != 0) {
socket.localAddressCleared = true;
return 0;
}
sockaddr_storage wildcard{};
wildcard.ss_family = local.ss_family;
PortOf(wildcard) = PortOf(local);
if (::bind(socket.value, reinterpret_cast<sockaddr*>(&wildcard), localSize) != 0) return Fail(NativeError());
socket.localAddressCleared = 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<const unsigned char*>(address);
if (length < (socket.family == 2 ? 16u : 28u) || bytes[1] != socket.family || (bytes[2] | bytes[3]) != 0) return false;
Expand Down Expand Up @@ -1317,11 +1341,12 @@ 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<const sockaddr*>(&native));
#endif
if (ZeroPortConnect(*socket, address, length)) return -1;
{
std::lock_guard lock(socket->modeMutex);
socket->options.reportedError = 0;
}
return ::connect(socket->value, reinterpret_cast<sockaddr*>(&native), size) ? Fail(ConnectError()) : 0;
std::unique_lock lock(socket->modeMutex);
socket->options.reportedError = 0;
if (socket->type != 2) lock.unlock();
if (::connect(socket->value, reinterpret_cast<sockaddr*>(&native), size)) return Fail(ConnectError());
if (socket->type == 2) socket->localAddressCleared = false;
return 0;
}
int APS5_VABI listen_nid_postfix(int descriptor, int backlog) {
const auto socket = Lookup(descriptor);
Expand Down Expand Up @@ -1409,7 +1434,11 @@ 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<sockaddr*>(&native), size) ? Fail(NativeError()) : 0;
std::unique_lock lock(socket->modeMutex, std::defer_lock);
if (socket->type == 2) lock.lock();
if (::bind(socket->value, reinterpret_cast<sockaddr*>(&native), size)) return Fail(NativeError());
if (socket->type == 2) socket->localAddressCleared = false;
return 0;
}
namespace {
bool AlreadyShut(NativeSocket socket, int how) {
Expand Down Expand Up @@ -1454,7 +1483,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<sockaddr*>(&native), &size)) {
if (LocalAddress(*socket, native, size)) {
#ifdef _WIN32
if (WSAGetLastError() != WSAEINVAL) return Fail(NativeError());
native = {};
Expand Down
82 changes: 80 additions & 2 deletions core/libs/tests/GuestUdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,84 @@ 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<unsigned char, 28> peerAddress{};
peerAddress[0] = static_cast<unsigned char>(addressSize);
peerAddress[1] = static_cast<unsigned char>(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<unsigned char, 28> 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<unsigned char, 28> name{};
size = name.size();
Require(getpeername_nid_postfix(descriptor, name.data(), &size) == -1 && *__error_nid_postfix() == 57);
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<unsigned char, 32> 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);
std::array<unsigned char, 28> 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);
Expand Down Expand Up @@ -257,6 +335,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();
Expand Down Expand Up @@ -406,9 +486,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));
Expand Down
116 changes: 116 additions & 0 deletions core/libs/tests/GuestUdpDisconnectResult.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include "prx/libkernel/include/Api.hpp"
#include "prx/libkernel/Socket/include/SocketRuntime.hpp"
#include "prx/libc/include/Api.hpp"
#include <sys/socket.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <algorithm>
#include <array>
#include <cerrno>
#include <cstdlib>
#include <utility>

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<int>(::syscall(SYS_connect, descriptor, address, size));
if (injectedResult != NativeResult::Connected) {
const int result = static_cast<int>(::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<int>(::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<unsigned char, 28> destination{};
destination[0] = static_cast<unsigned char>(addressSize);
destination[1] = static_cast<unsigned char>(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<unsigned char, 28> 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<int>(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<unsigned char, 28> 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);
}
Loading
Loading