Files
AnyPS5/core/libs/tests/GuestSocketDescriptors.cpp
T
screamm e99816e8f3 fix(libkernel): give guest sockets ordinary descriptor numbers below FD_SETSIZE
libkernel numbered guest sockets from GuestSockets::FirstDescriptor
(0x10000000), so a title that puts a socket in a 1024-bit fd_set writes
fd / 8 bytes past it. The Epic Online Services SDK does this on its
RtcNetworkThread and three titles stop there (#1794):

Palworld PPSA23126 (queeeef, main a11c8f7f7):
  FATAL: unhandled exception 0xc0000005 on thread 37164 'RtcNetworkThread'
    rip 0x00007ffcdb1ea2be eossdk-ps5-shipping.prx.guest.prx+0xdea2be
    rcx 0000000010000005 rsi 0000000000400000 r12 0000000010000005
The Mound PPSA26223 (xXPBSXx, main 0a8b1d32b):
  FATAL: unhandled exception 0xc0000005 on thread 'RtcNetworkThread',
  rip eossdk-ps5-shipping.prx.guest.prx+0xefc6f4, rcx = 0x10000005,
  rsi = 0x400000
Dying Light 2 PPSA02262 (tv7):
  FATAL: unhandled exception 0xc0000005 on thread 'RtcNetworkThread'
    rcx 0000000010000005 ... rsi 0000000000400000 ... r12 0000000010000005

On FreeBSD 11.4 (8c639945) FD_SETSIZE is 1024 (sys/sys/select.h:58-59)
and sockets take the lowest free entry of the one process descriptor
table that files use (kern_descrip.c:1643-1660 fdalloc,
uipc_syscalls.c kern_socket through falloc).

Every socket number is now a real host descriptor: a zero-access NUL
handle registered with _open_osfhandle on Windows (as directory
descriptors already are) and an F_DUPFD_CLOEXEC duplicate of the host
socket on Linux. The socket table maps that number to the socket, so
"is this a socket" is a table lookup instead of a range check at the 31
former FirstDescriptor comparisons. close releases both the socket entry
and the number, dup and dup2 duplicate the number through the host table
and keep the socket shared, dup2 of a file onto a socket number drops the
socket entry, and sceKernelClose now closes sockets too. sceNetSocket and
sceNetAccept take their ids from the same table instead of a counter from
32, so libkernel and libSceNet numbers never collide.

select polls guest sockets alongside libSceNet sockets, the random device,
stdio and (on Linux) host descriptors; poll throws only for an open
descriptor that is not a guest socket and reports POLLNVAL otherwise.
File calls that used to fail with EBADF on the socket range return
FreeBSD's answer for a socket: lseek ESPIPE (kern_lseek), ftruncate and
getdirentries EINVAL (invfo_truncate, getvnode), flock EOPNOTSUPP;
fstat on a socket throws. pread on a closed descriptor returns EBADF.

title failing on host descriptor numbers; these three titles are that
case, and this change keeps host numbering for files and only moves
sockets into it.

Tests: guest_socket_descriptors (new: socket below 1024, select on a
socket and a file together, close frees the number for a file, dup of a
socket, sceKernelClose), guest_select (socket and pipe together, Linux),
guest_poll, guest_dup, guest_socket_pair, guest_fstatfs,
guest_posix_filesystem and guest_sce_net updated.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
(cherry picked from commit 20378375b31eb5db922db6e55f45e44a4dae6445)
2026-10-13 03:22:36 +00:00

186 lines
6.4 KiB
C++

#include "prx/libc/include/general/VabiMacros.hpp"
#include <algorithm>
#include <array>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <stdexcept>
#include <vector>
struct GuestTimeval {
std::int64_t seconds;
std::int64_t microseconds;
};
struct GuestDescriptorSet {
std::array<std::uint64_t, 1024 / 64> words{};
void Add(int descriptor) {
words[descriptor / 64] |= (std::uint64_t{1} << (descriptor % 64));
}
bool Has(int descriptor) const {
return ((words[descriptor / 64] >> (descriptor % 64)) & 1u) != 0;
}
};
struct PollDescriptor {
int descriptor;
short events;
short revents;
};
extern "C" {
int APS5_VABI socket_nid_postfix(int, int, int);
int APS5_VABI bind_nid_postfix(int, const void*, std::uint32_t);
int APS5_VABI getsockname_nid_postfix(int, void*, std::uint32_t*);
int APS5_VABI connect_nid_postfix(int, const void*, std::uint32_t);
std::int64_t APS5_VABI send_nid_postfix(int, const void*, std::uint64_t, int);
std::int64_t APS5_VABI recv_nid_postfix(int, void*, std::uint64_t, int);
int APS5_VABI close_nid_postfix(int);
int APS5_VABI sceKernelClose(int);
int APS5_VABI dup_nid_postfix(int);
int APS5_VABI open_nid_postfix(const char*, int, int);
std::int64_t APS5_VABI read_nid_postfix(int, void*, std::uint64_t);
int APS5_VABI select_nid_postfix(int, void*, void*, void*, const void*);
int APS5_VABI poll_nid_postfix(PollDescriptor*, std::uint32_t, int);
int* APS5_VABI __error_nid_postfix();
}
static void Check(bool value, int line) {
if (!value) {
std::fprintf(stderr, "socket descriptor check failed at line %d, errno %d\n", line, *__error_nid_postfix());
std::abort();
}
}
#define Require(value) Check((value), __LINE__)
constexpr int DescriptorSetSize = 1024;
constexpr int Inet = 2;
constexpr int Datagram = 2;
constexpr short In = 0x1;
constexpr int KernelBadDescriptor = static_cast<int>(0x80020009u);
template <typename TCall> static bool Throws(TCall call) {
try {
call();
} catch (const std::runtime_error&) {
return true;
}
return false;
}
static int Socket() {
const int descriptor = socket_nid_postfix(Inet, Datagram, 0);
Require(descriptor >= 0 && descriptor < DescriptorSetSize);
return descriptor;
}
static int BoundSocket(std::array<unsigned char, 16>& address) {
const int descriptor = Socket();
address = {16, 2, 0, 0, 127, 0, 0, 1};
Require(bind_nid_postfix(descriptor, address.data(), address.size()) == 0);
std::uint32_t size = address.size();
Require(getsockname_nid_postfix(descriptor, address.data(), &size) == 0);
return descriptor;
}
static int ConnectedSocket(const std::array<unsigned char, 16>& address) {
const int descriptor = Socket();
Require(connect_nid_postfix(descriptor, address.data(), address.size()) == 0);
return descriptor;
}
static void WaitReadable(int descriptor) {
GuestDescriptorSet read{};
read.Add(descriptor);
GuestTimeval limit{2, 0};
Require(select_nid_postfix(descriptor + 1, read.words.data(), nullptr, nullptr, &limit) == 1);
Require(read.Has(descriptor));
}
static void SelectSocketAndFile() {
std::array<unsigned char, 16> address{};
const int receiver = BoundSocket(address);
const int sender = ConnectedSocket(address);
const int file = open_nid_postfix("/dev/urandom", 0, 0);
Require(file >= 0 && file < DescriptorSetSize && file != receiver && file != sender);
const int highest = std::max({receiver, sender, file});
{
GuestDescriptorSet read{};
read.Add(receiver);
GuestTimeval none{0, 0};
Require(select_nid_postfix(receiver + 1, read.words.data(), nullptr, nullptr, &none) == 0);
Require(!read.Has(receiver));
}
{
GuestDescriptorSet read{};
read.Add(receiver);
read.Add(file);
GuestTimeval none{0, 0};
Require(select_nid_postfix(highest + 1, read.words.data(), nullptr, nullptr, &none) == 1);
Require(read.Has(file) && !read.Has(receiver));
}
const char byte = 'd';
Require(send_nid_postfix(sender, &byte, 1, 0) == 1);
WaitReadable(receiver);
{
GuestDescriptorSet read{};
GuestDescriptorSet write{};
read.Add(receiver);
read.Add(file);
write.Add(sender);
GuestTimeval none{0, 0};
Require(select_nid_postfix(highest + 1, read.words.data(), write.words.data(), nullptr, &none) == 3);
Require(read.Has(receiver) && read.Has(file) && write.Has(sender));
}
char received = 0;
Require(recv_nid_postfix(receiver, &received, 1, 0) == 1 && received == byte);
for (const int descriptor : {receiver, sender, file}) Require(close_nid_postfix(descriptor) == 0);
}
static void CloseReleasesNumber() {
const int socket = Socket();
Require(close_nid_postfix(socket) == 0);
std::vector<int> files;
while (files.size() < 8 && (files.empty() || files.back() != socket)) {
const int file = open_nid_postfix("/dev/urandom", 0, 0);
Require(file >= 0);
files.push_back(file);
}
Require(files.back() == socket);
unsigned char bytes[4]{};
Require(read_nid_postfix(socket, bytes, sizeof(bytes)) == sizeof(bytes));
PollDescriptor entry{socket, In, 0};
Require(Throws([&] { poll_nid_postfix(&entry, 1, 0); }));
for (const int file : files) Require(close_nid_postfix(file) == 0);
const int kernelClosed = Socket();
Require(sceKernelClose(kernelClosed) == 0);
Require(sceKernelClose(kernelClosed) == KernelBadDescriptor);
entry = {kernelClosed, In, 0};
Require(poll_nid_postfix(&entry, 1, 0) == 1 && entry.revents == 0x20);
}
static void DuplicateKeepsSocket() {
std::array<unsigned char, 16> address{};
const int receiver = BoundSocket(address);
const int sender = ConnectedSocket(address);
const int duplicate = dup_nid_postfix(receiver);
Require(duplicate >= 0 && duplicate < DescriptorSetSize && duplicate != receiver && duplicate != sender);
Require(close_nid_postfix(receiver) == 0);
const char byte = 'u';
Require(send_nid_postfix(sender, &byte, 1, 0) == 1);
WaitReadable(duplicate);
char received = 0;
Require(recv_nid_postfix(duplicate, &received, 1, 0) == 1 && received == byte);
Require(close_nid_postfix(duplicate) == 0);
Require(close_nid_postfix(sender) == 0);
}
int main() {
SelectSocketAndFile();
CloseReleasesNumber();
DuplicateKeepsSocket();
return 0;
}