Skip to content
Closed
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
10 changes: 10 additions & 0 deletions core/libs/prx/libkernel/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ add_library(libkernel SHARED EXCLUDE_FROM_ALL
${CMAKE_CURRENT_SOURCE_DIR}/Pthread/src/Tsd.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Pthread/src/Rwlock.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Pthread/src/Sem.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Pthread/Posix/Atfork.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Pthread/Posix/Attr.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Pthread/Posix/Barrier.cpp
${CMAKE_CURRENT_SOURCE_DIR}/Pthread/Posix/Cond.cpp
Expand Down Expand Up @@ -535,6 +536,15 @@ if(BUILD_TESTING)
set_tests_properties(guest_parent_process PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
endif()
add_test_executable(guest_pthread_atfork_tests ${LIBS_INCLUDE_DIR}/tests/GuestPthreadAtfork.cpp)
target_include_directories(guest_pthread_atfork_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_pthread_atfork_tests PRIVATE libkernel libc)
configure_windows_unwind(guest_pthread_atfork_tests)
add_test(NAME guest_pthread_atfork COMMAND guest_pthread_atfork_tests)
if(WIN32)
set_tests_properties(guest_pthread_atfork PROPERTIES ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
endif()
add_test_executable(guest_kill_tests ${LIBS_INCLUDE_DIR}/tests/GuestKill.cpp)
target_include_directories(guest_kill_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_kill_tests PRIVATE libkernel libc)
Expand Down
11 changes: 11 additions & 0 deletions core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ constexpr int GuestSyncAsync = 0x1;
constexpr int GuestSyncInvalidate = 0x2;
constexpr int GuestAdviceCore = 9;
constexpr int GuestAdviceProtect = 10;
constexpr int GuestLockCurrent = 0x1;
constexpr int GuestLockFuture = 0x2;
constexpr std::uintptr_t GuestUserAddressEnd = 0x800000000000;
constexpr int GuestAlignedSuper = 0x1000000;
constexpr std::size_t SuperpageSize = std::size_t{1} << 21;
Expand Down Expand Up @@ -128,6 +130,15 @@ int APS5_VABI munlock_nid_postfix(const void* address, std::size_t length) {
return -1;
}

int APS5_VABI mlockall_nid_postfix(int flags) {
if (flags == 0 || (flags & ~(GuestLockCurrent | GuestLockFuture)) != 0) {
SetError(GuestInvalid);
return -1;
}
if ((flags & GuestLockFuture) != 0) throw std::runtime_error("mlockall: MCL_FUTURE is not implemented: later mappings are not locked");
throw std::runtime_error("mlockall: MCL_CURRENT is not implemented: the process also holds host stacks, heaps, modules and driver mappings");
}

int APS5_VABI msync_nid_postfix(void* address, std::size_t length, int flags) {
const auto failed = [](int error) {
SetError(error);
Expand Down
24 changes: 24 additions & 0 deletions core/libs/prx/libkernel/Pthread/Posix/Atfork.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "../include/Atfork.hpp"
#include <mutex>
#include <new>

namespace {
constexpr int GuestNoMemory = 12;
std::mutex atforkLock;
std::vector<PthreadAtforkHandlers> atforkHandlers;
}

extern "C" int APS5_VABI pthread_atfork_nid_postfix(PthreadAtforkHandler prepare, PthreadAtforkHandler parent, PthreadAtforkHandler child) {
std::lock_guard lock(atforkLock);
try {
atforkHandlers.push_back({prepare, parent, child});
} catch (const std::bad_alloc&) {
return GuestNoMemory;
}
return 0;
}

std::vector<PthreadAtforkHandlers> PthreadAtforkHandlers_nid_no_patch() {
std::lock_guard lock(atforkLock);
return atforkHandlers;
}
19 changes: 19 additions & 0 deletions core/libs/prx/libkernel/Pthread/include/Atfork.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef CORE_LIBS_PRX_LIBKERNEL_PTHREAD_ATFORK_HPP
#define CORE_LIBS_PRX_LIBKERNEL_PTHREAD_ATFORK_HPP

#include "prx/libc/include/general/VabiMacros.hpp"
#include <vector>

using PthreadAtforkHandler = void (APS5_VABI*)();

struct PthreadAtforkHandlers {
PthreadAtforkHandler prepare;
PthreadAtforkHandler parent;
PthreadAtforkHandler child;
};

extern "C" int APS5_VABI pthread_atfork_nid_postfix(PthreadAtforkHandler prepare, PthreadAtforkHandler parent, PthreadAtforkHandler child);

std::vector<PthreadAtforkHandlers> PthreadAtforkHandlers_nid_no_patch();

#endif
4 changes: 4 additions & 0 deletions core/libs/prx/libkernel/System/src/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ int APS5_VABI getrlimit_nid_postfix(int resource, GuestResourceLimit* limit) {
return 0;
}

int APS5_VABI getdtablesize_nid_postfix(void) {
throw std::runtime_error("getdtablesize: the console's RLIMIT_NOFILE soft limit and kern.maxfilesperproc are not known");
}

int APS5_VABI sceKernelIsTrinityMode(void) {
return 0;
}
Expand Down
12 changes: 12 additions & 0 deletions core/libs/tests/GuestGetrlimit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct ResourceLimit {

extern "C" int APS5_VABI getrlimit_nid_postfix(int resource, ResourceLimit* limit);
extern "C" int* APS5_VABI __error_nid_postfix();
extern "C" int APS5_VABI getdtablesize_nid_postfix(void);

static constexpr int GUEST_EFAULT = 14;
static constexpr int GUEST_EINVAL = 22;
Expand Down Expand Up @@ -73,6 +74,16 @@ static void RequireUnsupported(int resource) {
Require(rejected);
}

static void RequireTableSizeUnsupported() {
bool rejected = false;
try {
getdtablesize_nid_postfix();
} catch (const std::runtime_error&) {
rejected = true;
}
Require(rejected);
}

int main() {
#ifdef _WIN32
GuestLimit(GUEST_RLIMIT_DATA);
Expand All @@ -87,4 +98,5 @@ int main() {
RequireError(GUEST_RLIMIT_DATA, nullptr, GUEST_EFAULT);
RequireUnsupported(GUEST_RLIMIT_NOFILE);
RequireUnsupported(GUEST_RLIMIT_AS);
RequireTableSizeUnsupported();
}
25 changes: 25 additions & 0 deletions core/libs/tests/GuestMlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cstdint>
#include <cstdlib>
#include <limits>
#include <stdexcept>
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
Expand All @@ -20,11 +21,27 @@ int APS5_VABI sceKernelReserveVirtualRange(void**, std::size_t, int, std::size_t
int APS5_VABI sceKernelMunmap(void*, std::size_t);
int APS5_VABI mlock_nid_postfix(const void*, std::size_t);
int APS5_VABI munlock_nid_postfix(const void*, std::size_t);
int APS5_VABI mlockall_nid_postfix(int);
int* APS5_VABI __error_nid_postfix();
}

static void Require(bool value) { if (!value) std::abort(); }

static void RequireLockAllInvalid(int flags) {
*__error_nid_postfix() = 0;
Require(mlockall_nid_postfix(flags) == -1 && *__error_nid_postfix() == 22);
}

static void RequireLockAllUnsupported(int flags) {
bool rejected = false;
try {
mlockall_nid_postfix(flags);
} catch (const std::runtime_error&) {
rejected = true;
}
Require(rejected);
}

#if defined(__linux__)
static std::size_t LockedKilobytes() {
std::ifstream status("/proc/self/status");
Expand Down Expand Up @@ -114,5 +131,13 @@ int main() {
Require(mlock_nid_postfix(mapped, page) == -1 && *__error_nid_postfix() == 12);
*__error_nid_postfix() = 0;
Require(munlock_nid_postfix(mapped, page) == -1 && *__error_nid_postfix() == 12);

RequireLockAllInvalid(0);
RequireLockAllInvalid(4);
RequireLockAllInvalid(1 | 4);
RequireLockAllInvalid(-1);
RequireLockAllUnsupported(1);
RequireLockAllUnsupported(2);
RequireLockAllUnsupported(1 | 2);
return 0;
}
48 changes: 48 additions & 0 deletions core/libs/tests/GuestPthreadAtfork.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "prx/libkernel/Pthread/include/Atfork.hpp"
#include <atomic>
#include <cstddef>
#include <cstdlib>
#include <thread>
#include <vector>

static void Require(bool value) { if (!value) std::abort(); }

static std::atomic<int> calls{0};

static void APS5_VABI Prepare() { ++calls; }
static void APS5_VABI Parent() { ++calls; }
static void APS5_VABI Child() { ++calls; }

static bool Same(const PthreadAtforkHandlers& entry, PthreadAtforkHandler prepare, PthreadAtforkHandler parent, PthreadAtforkHandler child) {
return entry.prepare == prepare && entry.parent == parent && entry.child == child;
}

int main() {
Require(PthreadAtforkHandlers_nid_no_patch().empty());
Require(pthread_atfork_nid_postfix(Prepare, Parent, Child) == 0);
Require(pthread_atfork_nid_postfix(nullptr, Parent, nullptr) == 0);
Require(pthread_atfork_nid_postfix(nullptr, nullptr, nullptr) == 0);
Require(pthread_atfork_nid_postfix(Prepare, Parent, Child) == 0);
auto handlers = PthreadAtforkHandlers_nid_no_patch();
Require(handlers.size() == 4);
Require(Same(handlers[0], Prepare, Parent, Child));
Require(Same(handlers[1], nullptr, Parent, nullptr));
Require(Same(handlers[2], nullptr, nullptr, nullptr));
Require(Same(handlers[3], Prepare, Parent, Child));

constexpr std::size_t threadCount = 4;
constexpr std::size_t perThread = 64;
std::vector<std::thread> threads;
for (std::size_t index = 0; index < threadCount; ++index) {
threads.emplace_back([] {
for (std::size_t count = 0; count < perThread; ++count) Require(pthread_atfork_nid_postfix(nullptr, nullptr, Child) == 0);
});
}
for (auto& thread : threads) thread.join();
handlers = PthreadAtforkHandlers_nid_no_patch();
Require(handlers.size() == 4 + threadCount * perThread);
Require(Same(handlers[3], Prepare, Parent, Child));
for (std::size_t index = 4; index < handlers.size(); ++index) Require(Same(handlers[index], nullptr, nullptr, Child));
Require(calls == 0);
return 0;
}
3 changes: 3 additions & 0 deletions docs/dev/TechnicalDebt.md
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,6 @@ Throughout the project, every function at every stage either **does exactly what
- [_fstatfs](../../core/libs/prx/libkernel/File/src/Stdio.cpp) (libkernel) fills the FreeBSD 11 `struct statfs` of the PS5 payload SDK with the host volume that holds the file: block size, transfer size, total, free and available blocks, maximum name length and the read-only, `nosuid` and `noexec` flags (on Windows only read-only, and `f_files` and `f_ffree` are 0, as NTFS has no fixed number of files). The filesystem type number and name, mount paths, owner, filesystem id and I/O counters are left 0 or empty, since they would describe a host mount, not a console one. Sockets and pipes return `EINVAL`, as FreeBSD's `getvnode` does; a Windows descriptor that is neither a file nor a pipe throws. Stremio Plus (PPSA74126) reads the free space before a download and only logs the other fields
- [_WStoul](../../core/libs/prx/libc/src/Strings.cpp), [_Iswctype](../../core/libs/prx/libc/src/RuneLocale.cpp) and [swscanf_s](../../core/libs/prx/libc/src/ScanningWide.cpp) (libc) - `_WStoul` is `wcstoul` and throws for a base other than 0 or 2 to 36. `_Iswctype` takes the class numbers of Dinkumware's [xwcc.h](https://github.com/superna9999/ti-cgt6x/blob/cgt6x_7_3_4/include/xwcc.h) (1 `alnum` to 11 `xdigit`, 12 `blank`) and throws for any other value, including the FreeBSD masks `wctype` returns; the numbering is not confirmed on the console's `libc.prx`. `swscanf_s` follows C11 7.29.2.2 and K.3.9.1.5, but reads the size argument as `unsigned int` like `sscanf_s` where K.3.9.1.5 specifies `rsize_t`; the console's width is unverified. A runtime-constraint violation (null buffer, format or receiving pointer) only returns `EOF`, and a too small array gets a null first element as in `sscanf_s`. A literal mismatch is a matching failure and a field that is only a prefix, such as `0x` or `1e`, does not convert, as C11 requires; FreeBSD's `vfwscanf` reports an input failure for the first and converts the second. Integers out of range are truncated from `strtoll`/`strtoull`. It throws for `%p`, `%S`, `%C`, unknown length modifiers, scanset ranges, `%n` with suppression or width, and narrow `%s`, `%c` or `%[` input that the C-locale `wcrtomb` cannot convert
- [sceSharePlayGetCurrentConnectionInfoA](../../core/libs/prx/libSceSharePlay/Export.cpp) (libSceSharePlay) - reports no Share Play session, as the library keeps none: status 0 (DORMANT), mode 0, empty online IDs, account IDs 0 and both user IDs -1 (`SCE_USER_SERVICE_USER_ID_INVALID`). A null pointer returns `SCE_SHARE_PLAY_ERROR_INVALID_ARGS` (0x810E0001). The 72-byte `SceSharePlayConnectionInfoA` layout and the error code follow [KytyPS5](https://github.com/KytyPS5/KytyPS5/blob/main/src/libs/libNet.cpp) and [fpPS4](https://github.com/red-prig/fpPS4/blob/trunk/src/ps4_libsceshareplay.pas), which agree on both; KytyPS5 sets the user IDs to -1 and fpPS4 leaves them 0, and which one the console returns is not verified. KytyPS5 also returns `SCE_SHARE_PLAY_ERROR_NOT_INITIALIZED` before `sceSharePlayInitialize`; this library keeps no initialization state and does not.
- [pthread_atfork](../../core/libs/prx/libkernel/Pthread/Posix/Atfork.cpp) (libkernel) appends the three handlers to a process-wide list and returns 0, or `ENOMEM` when the entry cannot be allocated, as FreeBSD 11.4's [`_pthread_atfork`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libthr/thread/thr_fork.c#L78-L100) does; null handlers are accepted. The handlers never run: `fork` throws and `vfork` is absent, so no child process is created. Handlers of a module that is unloaded stay in the list, where FreeBSD's `__pthread_cxa_finalize` removes them. PPSA97358 calls it from the constructor `rcu_init` ([rcu.c](https://github.com/ZiZc3/XPSemu/blob/8eec07b4e31f66dce8e0b05c1ca1281af303e933/util/rcu.c#L488-L495)), before `main`
- [mlockall](../../core/libs/prx/libkernel/DirectMemory/PosixMemory.cpp) (libkernel) follows FreeBSD 11.4's [`sys_mlockall`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/vm/vm_mmap.c#L1067-L1131) only for the flag check: 0 or bits outside `MCL_CURRENT` (1) and `MCL_FUTURE` (2) fail with `EINVAL`. The title process is taken to pass `priv_check(PRIV_VM_MLOCK)`, as `mlock` does; whether the console answers `EPERM` is unverified. Valid flags throw: `MCL_FUTURE` would need every later guest and host mapping to be locked, and `MCL_CURRENT` the whole process, whose host stacks, heaps, modules and driver mappings `mlock` would lock region by region, with `ENOMEM` above a guest `RLIMIT_MEMLOCK` that is not known. PPSA97358 calls `mlockall(MCL_CURRENT | MCL_FUTURE)` only with `-overcommit mem-lock=on` ([os-posix.c](https://github.com/ZiZc3/XPSemu/blob/8eec07b4e31f66dce8e0b05c1ca1281af303e933/os-posix.c#L330-L355))
- [getdtablesize](../../core/libs/prx/libkernel/System/src/Process.cpp) (libkernel) throws: FreeBSD 11.4's [`sys_getdtablesize`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/sys/kern/kern_descrip.c#L338-L355) returns the smaller of the `RLIMIT_NOFILE` soft limit and `kern.maxfilesperproc`, and neither value is published for the console (`getrlimit` throws for `RLIMIT_NOFILE` for the same reason); fpPS4 returns `High(Integer)` as a placeholder. PPSA85300 and PPSA97358 import it and fail to load without it. Both call it only from their own `closefrom` ([VLC libc_stubs.c](https://github.com/ZiZc3/VLC/blob/f3253020a64368153233ede29183014c0b6c8979/ps5/compat/libc_stubs.c#L67-L73), [XPSemu libc_stubs.c](https://github.com/ZiZc3/XPSemu/blob/8eec07b4e31f66dce8e0b05c1ca1281af303e933/ps5/compat/libc_stubs.c#L67-L73)), which closes every descriptor below the value, so the value decides how many `close` calls run, including over the libSceNet descriptor range
Loading