Skip to content
Open
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
33 changes: 30 additions & 3 deletions core/libs/prx/libc/src/Threads.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "prx/libc/include/Api.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/GuestArena.hpp"
#include "prx/libc/include/PreciseWait.hpp"
#include <algorithm>
#include <atomic>
Expand Down Expand Up @@ -195,12 +196,17 @@ struct DinkumwareCondition {
DinkumwareNativeCondition native;
};

char destroyedMutexMarker;

DinkumwareMutex* ResolveMutex(void** handle, const char* function) {
if (!handle)
throw std::invalid_argument(std::string(function) + ": null mutex handle pointer");
auto* mutex = static_cast<DinkumwareMutex*>(*handle);
void* const current = __atomic_load_n(handle, __ATOMIC_ACQUIRE);
auto* mutex = static_cast<DinkumwareMutex*>(current);
if (!mutex)
throw std::invalid_argument(std::string(function) + ": null mutex handle");
if (current == &destroyedMutexMarker)
throw std::invalid_argument(std::string(function) + ": destroyed mutex");
if (mutex->tag != mutexTag)
throw std::invalid_argument(std::string(function) + ": not a mutex created by _Mtx_init");
return mutex;
Expand Down Expand Up @@ -254,14 +260,14 @@ int APS5_VABI _Mtx_init_with_default_name_override_nid_postfix(void** handle, in
void APS5_VABI _Mtx_destroy_nid_postfix(void** handle) {
if (!handle)
throw std::invalid_argument("_Mtx_destroy: null mutex handle pointer");
if (!*handle)
if (!*handle || *handle == &destroyedMutexMarker)
return;
auto* mutex = ResolveMutex(handle, "_Mtx_destroy");
if (mutex->count != 0)
throw std::logic_error("_Mtx_destroy: mutex destroyed while locked");
mutex->tag = 0;
delete mutex;
*handle = nullptr;
*handle = &destroyedMutexMarker;
}

int APS5_VABI _Mtx_lock_nid_postfix(void** handle) {
Expand All @@ -286,6 +292,25 @@ int APS5_VABI _Mtx_lock_nid_postfix(void** handle) {
}

int APS5_VABI _Mtx_trylock_nid_postfix(void** handle) {
void* current = handle ? __atomic_load_n(handle, __ATOMIC_ACQUIRE) : nullptr;
if (current == &destroyedMutexMarker)
return threadError;
if (handle && !current) {
const GuestArena::HostWrite slotWrite(handle, sizeof(*handle));
if (slotWrite.Open() && GuestWritable_nid_no_patch(handle, sizeof(*handle))) {
auto* created = new (std::nothrow) DinkumwareMutex();
if (!created)
return threadNomem;
void* expected = nullptr;
if (!__atomic_compare_exchange_n(handle, &expected, static_cast<void*>(created), false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
delete created;
current = __atomic_load_n(handle, __ATOMIC_ACQUIRE);
if (current == &destroyedMutexMarker)
return threadError;
}
}
if (current && static_cast<const DinkumwareMutex*>(current)->tag != mutexTag)
return threadError;
auto* mutex = ResolveMutex(handle, "_Mtx_trylock");
const auto self = std::this_thread::get_id();
if (mutex->owner.load(std::memory_order_acquire) == self) {
Expand Down Expand Up @@ -620,6 +645,7 @@ void APS5_VABI _ZNSt4_PadC2Ev_nid_postfix(GuestPad* pad) {
const int locked = _Mtx_lock_nid_postfix(&pad->mutex);
if (locked != threadSuccess) {
_Mtx_destroy_nid_postfix(&pad->mutex);
pad->mutex = nullptr;
_Cnd_destroy_nid_postfix(&pad->condition);

RequireThrd(locked, "_Pad::_Pad: _Mtx_lock");
Expand Down Expand Up @@ -653,6 +679,7 @@ void APS5_VABI _ZNSt4_PadD2Ev_nid_postfix(GuestPad* pad) {
if (!pad) APS5_INVALID_ARG_EX;
RequireThrd(_Mtx_unlock_nid_postfix(&pad->mutex), "_Pad::~_Pad: _Mtx_unlock");
_Mtx_destroy_nid_postfix(&pad->mutex);
pad->mutex = nullptr;
_Cnd_destroy_nid_postfix(&pad->condition);

}
Expand Down
10 changes: 5 additions & 5 deletions core/libs/tests/GuestLibcMutexInitWithName.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ int main() {
RequireThrows<std::logic_error>([&] { _Mtx_destroy_nid_postfix(&mutex); });
Require(_Mtx_unlock_nid_postfix(&mutex) == success);
_Mtx_destroy_nid_postfix(&mutex);
Require(mutex == nullptr);
Require(_Mtx_trylock_nid_postfix(&mutex) == 4);

_Mtx_destroy_nid_postfix(&mutex);
Require(mutex == nullptr);
Require(_Mtx_trylock_nid_postfix(&mutex) == 4);

void* unnamed = nullptr;
Require(_Mtx_init_with_name_nid_postfix(&unnamed, plain, nullptr) == success);
Require(unnamed != nullptr);
Require(_Mtx_trylock_nid_postfix(&unnamed) == success);
Require(_Mtx_unlock_nid_postfix(&unnamed) == success);
_Mtx_destroy_nid_postfix(&unnamed);
Require(unnamed == nullptr);
Require(_Mtx_trylock_nid_postfix(&unnamed) == 4);

void* overridden = nullptr;
Require(_Mtx_init_with_default_name_override_nid_postfix(&overridden, plain, "OverrideMutex") == success);
Require(overridden != nullptr);
_Mtx_destroy_nid_postfix(&overridden);
Require(overridden == nullptr);
Require(_Mtx_trylock_nid_postfix(&overridden) == 4);

void* recMutex = nullptr;
Require(_Mtx_init_with_name_nid_postfix(&recMutex, plain | recursive, "RecursiveMutex") == success);
Expand All @@ -72,7 +72,7 @@ int main() {
Require(_Mtx_unlock_nid_postfix(&recMutex) == success);
contender.join();
_Mtx_destroy_nid_postfix(&recMutex);
Require(recMutex == nullptr);
Require(_Mtx_trylock_nid_postfix(&recMutex) == 4);

RequireThrows<std::invalid_argument>([] {
_Mtx_init_with_name_nid_postfix(nullptr, plain, "NullHandle");
Expand Down
17 changes: 17 additions & 0 deletions core/libs/tests/GuestLibcStaticImports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ void TestMtxTrylock() {
}).join();

_Mtx_destroy_nid_postfix(&recMutex);

alignas(8) unsigned char destroyed[128] = {};
void* staleHandle = destroyed;
Require(_Mtx_trylock_nid_postfix(&staleHandle) == 4);

void* lazyHandle = nullptr;
Require(_Mtx_trylock_nid_postfix(&lazyHandle) == threadSuccess && lazyHandle != nullptr);
Require(_Mtx_trylock_nid_postfix(&lazyHandle) == threadBusy);
Require(_Mtx_unlock_nid_postfix(&lazyHandle) == threadSuccess);
_Mtx_destroy_nid_postfix(&lazyHandle);

void* destroyedHandle = nullptr;
Require(_Mtx_init_nid_postfix(&destroyedHandle, mutexTryable) == threadSuccess && destroyedHandle != nullptr);
_Mtx_destroy_nid_postfix(&destroyedHandle);
Require(_Mtx_trylock_nid_postfix(&destroyedHandle) == 4);
_Mtx_destroy_nid_postfix(&destroyedHandle);
Require(_Mtx_trylock_nid_postfix(&destroyedHandle) == 4);
}

void TestThrdSleep() {
Expand Down
36 changes: 34 additions & 2 deletions core/libs/tests/GuestLibcThreads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "prx/libkernel/Pthread/include/Cond.hpp"
#include "prx/libc/include/Api.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libc/include/GuestArena.hpp"
#include <array>
#include <atomic>
#include <barrier>
Expand Down Expand Up @@ -72,7 +73,7 @@ static void MutexBasics() {
_Cnd_destroy_nid_postfix(&foreignCondition);
_Mtx_destroy_nid_postfix(&mutex);

Require(mutex == nullptr);
Require(_Mtx_trylock_nid_postfix(&mutex) == 4);
RequireThrows<std::invalid_argument>([&] { _Mtx_lock_nid_postfix(&mutex); });
void* emptyMutex = nullptr;
_Mtx_destroy_nid_postfix(&emptyMutex);
Expand Down Expand Up @@ -112,7 +113,7 @@ static void DefaultNameOverrideMutex() {
Require(_Mtx_lock_nid_postfix(&mutex) == success && _Mtx_lock_nid_postfix(&mutex) == busy);
Require(_Mtx_unlock_nid_postfix(&mutex) == success);
_Mtx_destroy_nid_postfix(&mutex);
Require(mutex == nullptr);
Require(_Mtx_trylock_nid_postfix(&mutex) == 4);

void* unnamedMutex = nullptr;
Require(_Mtx_init_with_default_name_override_nid_postfix(&unnamedMutex, 0, nullptr) == success && unnamedMutex != nullptr);
Expand Down Expand Up @@ -676,6 +677,7 @@ static void UnusableHandlePointers() {
RequireThrows<std::invalid_argument>([&] { _Cnd_destroy_nid_postfix(unmapped); });
RequireThrows<std::invalid_argument>([&] { _ZNSt8__sce_v219_Mtx_init_with_nameEPP13pthread_mutexiPKc_nid_postfix(unmapped, plain, "unmapped"); });
RequireThrows<std::invalid_argument>([&] { _ZNSt8__sce_v29_Mtx_initEPP13pthread_mutexi_nid_postfix(readOnlyNull, plain); });
RequireThrows<std::invalid_argument>([&] { _Mtx_trylock_nid_postfix(readOnlyNull); });
Require(*static_cast<void* volatile*>(readOnlyNull) == nullptr);
_Cnd_destroy_nid_postfix(readOnlyNull);

Expand All @@ -689,6 +691,33 @@ static void UnusableHandlePointers() {
Require(condition == nullptr);
}

#ifdef _WIN32
static void WriteWatchedNullHandle() {
if (!GuestArena::GuestArenaAvailable_nid_postfix() || !GuestArena::GuestArenaWriteWatched_nid_postfix()) return;
constexpr std::size_t bytes = 0x10000;
auto* const watched = static_cast<unsigned char*>(GuestArena::GuestArenaAllocate_nid_postfix(bytes, bytes));
Require(watched != nullptr);
const HANDLE section = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_EXECUTE_READWRITE, 0, bytes, nullptr);
Require(section != nullptr);
GuestArena::GuestArenaMap_nid_postfix(watched, bytes, section, 0, PAGE_READWRITE);
CloseHandle(section);
auto** const slot = reinterpret_cast<void**>(watched);
*slot = nullptr;
std::array<void*, 16> pages{};
std::size_t count = pages.size();
Require(GuestArena::GuestArenaCollectWrites_nid_postfix(reinterpret_cast<std::uintptr_t>(watched), bytes, pages.data(), &count, true));
count = pages.size();
Require(GuestArena::GuestArenaCollectWrites_nid_postfix(reinterpret_cast<std::uintptr_t>(watched), bytes, pages.data(), &count, true));
Require(count == 0);
MEMORY_BASIC_INFORMATION memory{};
Require(VirtualQuery(slot, &memory, sizeof(memory)) == sizeof(memory) && memory.Protect == PAGE_READONLY);
Require(_Mtx_trylock_nid_postfix(slot) == success);
Require(*slot != nullptr);
Require(_Mtx_unlock_nid_postfix(slot) == success);
_Mtx_destroy_nid_postfix(slot);
}
#endif

int main() {
ThreadExitLifecycle();
KernelKeys();
Expand All @@ -707,4 +736,7 @@ int main() {
PadDetach();
ThreadHelpers();
UnusableHandlePointers();
#ifdef _WIN32
WriteWatchedNullHandle();
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# libc

## change [_Mtx_trylock]
- [_Mtx_trylock](../../../core/libs/prx/libc/src/Threads.cpp) (libc, NID `k6pGNMwJB08`) - returns busy for a non-recursive mutex already owned by the caller. The `libc.prx` that titles ship (disassembly of PPSA13934's copy, 0x5cf0, not measured on a console) passes its argument to `scePthreadMutexTrylock` and returns 0 for success, `_Thrd_busy` (3) for `SCE_KERNEL_ERROR_EBUSY` and `_Thrd_error` (4) for any other error; this libc keeps its own owner and count. A payload process on a PS5 (firmware 11.60) returned 4 for the handle of a destroyed mutex and 0 for a pointer to a null handle, and faulted for a null pointer. Here `_Mtx_destroy` leaves a non-null marker in the handle (the console value is unmeasured), and the marker or a non-mutex handle returns 4 (the latter tested with zeroed storage only); a pointer to a null handle in writable memory creates a plain mutex and locks it, a guess at what the console does, and a null or read-only slot throws.
Loading