mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-21 08:12:09 -05:00
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, maina11c8f7f7): 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, main0a8b1d32b): 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)
86 lines
2.9 KiB
C++
86 lines
2.9 KiB
C++
#include "prx/libc/include/GuestArena.hpp"
|
|
#include "prx/libc/include/general/VabiMacros.hpp"
|
|
#include "prx/libkernel/File/include/FileFlags.hpp"
|
|
#include "prx/libkernel/Socket/include/SocketRuntime.hpp"
|
|
#include <cerrno>
|
|
#include <cstring>
|
|
#include <random>
|
|
#include <string>
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#include <io.h>
|
|
extern "C" _invalid_parameter_handler _set_thread_local_invalid_parameter_handler(_invalid_parameter_handler);
|
|
#else
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
extern "C" {
|
|
int* APS5_VABI __error_nid_postfix();
|
|
int APS5_VABI open_nid_postfix(const char*, int, int);
|
|
}
|
|
|
|
namespace {
|
|
constexpr char Characters[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
|
|
int Failure(int error) {
|
|
*__error_nid_postfix() = error;
|
|
return -1;
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
void IgnoreInvalidParameter(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, std::uintptr_t) {}
|
|
#endif
|
|
}
|
|
|
|
extern "C" int APS5_VABI mkstemp_nid_postfix(char* pattern) {
|
|
if (!pattern) return Failure(14);
|
|
const auto length = std::strlen(pattern);
|
|
if (length == 0) return Failure(22);
|
|
if (length >= 1024) return Failure(63);
|
|
auto start = length;
|
|
while (start > 0 && pattern[start - 1] == 'X') --start;
|
|
const GuestArena::HostWrite destination(pattern + start, length - start);
|
|
if (!destination.Open()) return Failure(14);
|
|
if (start != length) {
|
|
std::random_device random;
|
|
std::uniform_int_distribution<unsigned int> character(0, sizeof(Characters) - 2);
|
|
for (auto position = start; position < length; ++position)
|
|
pattern[position] = Characters[character(random)];
|
|
}
|
|
const std::string initial(pattern + start, length - start);
|
|
for (;;) {
|
|
const int descriptor = open_nid_postfix(pattern, SCE_KERNEL_O_RDWR | SCE_KERNEL_O_CREAT | SCE_KERNEL_O_EXCL, 0600);
|
|
if (descriptor >= 0 || *__error_nid_postfix() != 17) return descriptor;
|
|
auto position = start;
|
|
for (; position < length; ++position) {
|
|
const auto index = std::strchr(Characters, pattern[position]) - Characters;
|
|
pattern[position] = Characters[(index + 1) % (sizeof(Characters) - 1)];
|
|
if (pattern[position] != initial[position - start]) break;
|
|
}
|
|
if (position == length) return -1;
|
|
}
|
|
}
|
|
|
|
extern "C" int APS5_VABI isatty_nid_postfix(int descriptor) {
|
|
if (GuestSocketIsOpen_nid_no_patch(descriptor)) {
|
|
Failure(25);
|
|
return 0;
|
|
}
|
|
#ifdef _WIN32
|
|
const auto previous = _set_thread_local_invalid_parameter_handler(IgnoreInvalidParameter);
|
|
const auto handle = reinterpret_cast<HANDLE>(::_get_osfhandle(descriptor));
|
|
_set_thread_local_invalid_parameter_handler(previous);
|
|
if (handle == INVALID_HANDLE_VALUE) {
|
|
Failure(9);
|
|
return 0;
|
|
}
|
|
DWORD mode = 0;
|
|
if (::GetConsoleMode(handle, &mode)) return 1;
|
|
Failure(25);
|
|
#else
|
|
if (::isatty(descriptor)) return 1;
|
|
Failure(errno == EBADF ? 9 : 25);
|
|
#endif
|
|
return 0;
|
|
}
|