diff --git a/core/libs/prx/libc/src/Threads.cpp b/core/libs/prx/libc/src/Threads.cpp index 4bb1b0520ec6..0af3d7647f62 100644 --- a/core/libs/prx/libc/src/Threads.cpp +++ b/core/libs/prx/libc/src/Threads.cpp @@ -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 #include @@ -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(*handle); + void* const current = __atomic_load_n(handle, __ATOMIC_ACQUIRE); + auto* mutex = static_cast(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; @@ -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) { @@ -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(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(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) { @@ -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"); @@ -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); } diff --git a/core/libs/tests/GuestLibcMutexInitWithName.cpp b/core/libs/tests/GuestLibcMutexInitWithName.cpp index 50c2dd0e5e68..83ab1ae00a7b 100644 --- a/core/libs/tests/GuestLibcMutexInitWithName.cpp +++ b/core/libs/tests/GuestLibcMutexInitWithName.cpp @@ -32,10 +32,10 @@ int main() { RequireThrows([&] { _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); @@ -43,13 +43,13 @@ int main() { 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); @@ -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([] { _Mtx_init_with_name_nid_postfix(nullptr, plain, "NullHandle"); diff --git a/core/libs/tests/GuestLibcStaticImports.cpp b/core/libs/tests/GuestLibcStaticImports.cpp index 7db359e681a2..3c9127b2a1a0 100644 --- a/core/libs/tests/GuestLibcStaticImports.cpp +++ b/core/libs/tests/GuestLibcStaticImports.cpp @@ -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() { diff --git a/core/libs/tests/GuestLibcThreads.cpp b/core/libs/tests/GuestLibcThreads.cpp index 6c70d0b99ecf..03d7553687bf 100644 --- a/core/libs/tests/GuestLibcThreads.cpp +++ b/core/libs/tests/GuestLibcThreads.cpp @@ -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 #include #include @@ -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([&] { _Mtx_lock_nid_postfix(&mutex); }); void* emptyMutex = nullptr; _Mtx_destroy_nid_postfix(&emptyMutex); @@ -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); @@ -676,6 +677,7 @@ static void UnusableHandlePointers() { RequireThrows([&] { _Cnd_destroy_nid_postfix(unmapped); }); RequireThrows([&] { _ZNSt8__sce_v219_Mtx_init_with_nameEPP13pthread_mutexiPKc_nid_postfix(unmapped, plain, "unmapped"); }); RequireThrows([&] { _ZNSt8__sce_v29_Mtx_initEPP13pthread_mutexi_nid_postfix(readOnlyNull, plain); }); + RequireThrows([&] { _Mtx_trylock_nid_postfix(readOnlyNull); }); Require(*static_cast(readOnlyNull) == nullptr); _Cnd_destroy_nid_postfix(readOnlyNull); @@ -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(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(watched); + *slot = nullptr; + std::array pages{}; + std::size_t count = pages.size(); + Require(GuestArena::GuestArenaCollectWrites_nid_postfix(reinterpret_cast(watched), bytes, pages.data(), &count, true)); + count = pages.size(); + Require(GuestArena::GuestArenaCollectWrites_nid_postfix(reinterpret_cast(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(); @@ -707,4 +736,7 @@ int main() { PadDetach(); ThreadHelpers(); UnusableHandlePointers(); +#ifdef _WIN32 + WriteWatchedNullHandle(); +#endif } diff --git a/docs/dev/debt/changes/fix-libc-mtx-trylock-null-and-destroyed-handle.md b/docs/dev/debt/changes/fix-libc-mtx-trylock-null-and-destroyed-handle.md new file mode 100644 index 000000000000..adc3ba62c173 --- /dev/null +++ b/docs/dev/debt/changes/fix-libc-mtx-trylock-null-and-destroyed-handle.md @@ -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.