mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-22 00:22:57 -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)
133 lines
4.5 KiB
C++
133 lines
4.5 KiB
C++
#include "prx/libc/include/General.hpp"
|
|
#include "prx/libkernel/File/include/FileFlags.hpp"
|
|
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
#include <string>
|
|
|
|
struct GuestStatfs {
|
|
std::uint32_t f_version;
|
|
std::uint32_t f_type;
|
|
std::uint64_t f_flags;
|
|
std::uint64_t f_bsize;
|
|
std::uint64_t f_iosize;
|
|
std::uint64_t f_blocks;
|
|
std::uint64_t f_bfree;
|
|
std::int64_t f_bavail;
|
|
std::uint64_t f_files;
|
|
std::int64_t f_ffree;
|
|
std::uint64_t f_syncwrites;
|
|
std::uint64_t f_asyncwrites;
|
|
std::uint64_t f_syncreads;
|
|
std::uint64_t f_asyncreads;
|
|
std::uint64_t f_spare[10];
|
|
std::uint32_t f_namemax;
|
|
std::uint32_t f_owner;
|
|
std::int32_t f_fsid[2];
|
|
char f_charspare[80];
|
|
char f_fstypename[16];
|
|
char f_mntfromname[88];
|
|
char f_mntonname[88];
|
|
};
|
|
static_assert(sizeof(GuestStatfs) == 472);
|
|
|
|
extern "C" {
|
|
int APS5_VABI _fstatfs_nid_postfix(int, GuestStatfs*);
|
|
int APS5_VABI sceKernelOpen(const char*, int, std::uint16_t);
|
|
int APS5_VABI close_nid_postfix(int);
|
|
int APS5_VABI pipe_nid_postfix(int*);
|
|
int APS5_VABI socketpair_nid_postfix(int, int, int, int*);
|
|
int* APS5_VABI __error_nid_postfix();
|
|
}
|
|
|
|
static void Check(bool value, int line) {
|
|
if (!value) {
|
|
std::fprintf(stderr, "fstatfs check failed at line %d\n", line);
|
|
std::abort();
|
|
}
|
|
}
|
|
#define Require(value) Check((value), __LINE__)
|
|
|
|
static constexpr std::uint32_t StatfsVersion = 0x20030518;
|
|
static constexpr int GuestEbadf = 9;
|
|
static constexpr int GuestEfault = 14;
|
|
static constexpr int GuestEinval = 22;
|
|
static constexpr int DescriptorSetSize = 1024;
|
|
|
|
static void RequireFailure(int descriptor, int error) {
|
|
GuestStatfs fs;
|
|
std::memset(&fs, 0x5a, sizeof(fs));
|
|
*__error_nid_postfix() = 0;
|
|
Require(_fstatfs_nid_postfix(descriptor, &fs) == -1);
|
|
Require(*__error_nid_postfix() == error);
|
|
for (std::size_t i = 0; i < sizeof(fs); ++i) Require(reinterpret_cast<const unsigned char*>(&fs)[i] == 0x5a);
|
|
}
|
|
|
|
static void RequireVolume(int descriptor, const std::filesystem::path& path) {
|
|
GuestStatfs fs;
|
|
std::memset(&fs, 0x5a, sizeof(fs));
|
|
Require(_fstatfs_nid_postfix(descriptor, &fs) == 0);
|
|
const auto space = std::filesystem::space(path);
|
|
Require(fs.f_version == StatfsVersion);
|
|
Require(fs.f_bsize > 0 && fs.f_iosize > 0);
|
|
Require(fs.f_blocks > 0 && fs.f_bfree <= fs.f_blocks);
|
|
Require(fs.f_bavail >= 0 && static_cast<std::uint64_t>(fs.f_bavail) <= fs.f_bfree);
|
|
Require(fs.f_blocks * fs.f_bsize <= space.capacity && space.capacity - fs.f_blocks * fs.f_bsize < fs.f_bsize);
|
|
Require((fs.f_flags & 0x1) == 0);
|
|
Require(fs.f_namemax >= 255);
|
|
Require(fs.f_fstypename[0] == '\0' && fs.f_mntfromname[0] == '\0' && fs.f_mntonname[0] == '\0');
|
|
}
|
|
|
|
int main() {
|
|
const auto root = std::filesystem::temp_directory_path() /
|
|
("anyps5-fstatfs-" + std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
|
|
Require(std::filesystem::create_directory(root));
|
|
const auto filePath = root / "file.txt";
|
|
{
|
|
std::ofstream file(filePath, std::ios::binary);
|
|
file << "fstatfs";
|
|
Require(static_cast<bool>(file));
|
|
}
|
|
|
|
AddPathAlias_nid_no_patch("/anyps5-fstatfs-test", root.string().c_str());
|
|
const int file = sceKernelOpen("/anyps5-fstatfs-test/file.txt", SCE_KERNEL_O_RDONLY, 0);
|
|
Require(file >= 0);
|
|
RequireVolume(file, filePath);
|
|
*__error_nid_postfix() = 0;
|
|
Require(_fstatfs_nid_postfix(file, nullptr) == -1);
|
|
Require(*__error_nid_postfix() == GuestEfault);
|
|
|
|
const int directory = sceKernelOpen("/anyps5-fstatfs-test", SCE_KERNEL_O_RDONLY | SCE_KERNEL_O_DIRECTORY, 0);
|
|
Require(directory >= 0);
|
|
RequireVolume(directory, root);
|
|
Require(close_nid_postfix(directory) == 0);
|
|
|
|
int pipe[2];
|
|
Require(pipe_nid_postfix(pipe) == 0);
|
|
RequireFailure(pipe[0], GuestEinval);
|
|
RequireFailure(pipe[1], GuestEinval);
|
|
Require(close_nid_postfix(pipe[0]) == 0);
|
|
Require(close_nid_postfix(pipe[1]) == 0);
|
|
|
|
int sockets[2];
|
|
Require(socketpair_nid_postfix(1, 1, 0, sockets) == 0);
|
|
Require(sockets[0] >= 0 && sockets[0] < DescriptorSetSize);
|
|
RequireFailure(sockets[0], GuestEinval);
|
|
Require(close_nid_postfix(sockets[0]) == 0);
|
|
Require(close_nid_postfix(sockets[1]) == 0);
|
|
RequireFailure(sockets[0], GuestEbadf);
|
|
|
|
RequireFailure(-1, GuestEbadf);
|
|
Require(close_nid_postfix(file) == 0);
|
|
#ifndef _WIN32
|
|
RequireFailure(file, GuestEbadf);
|
|
#endif
|
|
|
|
std::filesystem::remove_all(root);
|
|
}
|