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
10 changes: 5 additions & 5 deletions core/libs/prx/libkernel/Pthread/Posix/Once.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libkernel/Pthread/include/Cancel.hpp"
#include "prx/libkernel/Time/include/TimedWait.hpp"
#include <cerrno>
#include <condition_variable>
#include <cstddef>
#include <cstdint>
#include <mutex>
Expand All @@ -13,7 +13,7 @@ namespace {

static_assert(offsetof(GuestOnce, mutex) == 8 && sizeof(GuestOnce) == 16);
std::mutex onceMutex;
std::condition_variable onceChanged;
TimedWait::Condition onceChanged;

bool ReadControlState(const GuestOnce* control, std::int32_t& state) {
const int savedErrno = errno;
Expand All @@ -36,7 +36,7 @@ void APS5_VABI ResetOnce(void* control) {
std::lock_guard lock(onceMutex);
WriteControlState(static_cast<GuestOnce*>(control), 0);
}
onceChanged.notify_all();
onceChanged.NotifyAll();
}
}

Expand All @@ -46,7 +46,7 @@ extern "C" int APS5_VABI pthread_once_nid_postfix(
std::unique_lock lock(onceMutex);
std::int32_t state = 0;
bool readable = true;
onceChanged.wait(lock, [&] {
onceChanged.Wait(lock, [&] {
readable = ReadControlState(control, state);
return !readable || state != 2;
});
Expand All @@ -72,6 +72,6 @@ extern "C" int APS5_VABI pthread_once_nid_postfix(
lock.lock();
const bool updated = WriteControlState(control, 1);
lock.unlock();
onceChanged.notify_all();
onceChanged.NotifyAll();
return updated ? 0 : 14;
}
8 changes: 6 additions & 2 deletions core/libs/prx/libkernel/Pthread/src/Cond.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,12 @@ struct WakingLock {
PthreadMutex mutex;
TNative& native;
void lock() {
if (!MutexOperations::SpinAcquire(mutex, native))
native.lock();
if (MutexOperations::SpinAcquire(mutex, native)) return;
#ifdef _WIN32
TimedWait::ParkAcquire(mutex, [&] { return native.try_lock(); }, nullptr);
#else
native.lock();
#endif
}
void unlock() {
native.unlock();
Expand Down
133 changes: 133 additions & 0 deletions core/libs/tests/GuestRaiseLeavesHostCode.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "prx/libkernel/include/Api.hpp"
#include "prx/libkernel/Pthread/include/Cond.hpp"
#include "prx/libkernel/Pthread/include/Mutex.hpp"
#include "SceTypes.hpp"
#include <atomic>
#include <chrono>
Expand All @@ -13,6 +15,7 @@

static constexpr int SigUsr1 = 30;
static constexpr int Rounds = 10;
static constexpr int MutexTypeRecursive = 2;
static constexpr std::size_t RipOffset = 0xe0;
static constexpr std::size_t ConvertedBytes = 64u << 20u;
static void Require(bool value) { if (!value) std::abort(); }
Expand All @@ -35,6 +38,10 @@ static std::atomic<int> beforeWait{0};
static HANDLE alertableHandle = nullptr;
static HANDLE release = nullptr;
static HANDLE alertableRelease = nullptr;
static std::atomic<int> stops{0};
static std::atomic<int> resumed{0};
static std::atomic<bool> resume{false};
static std::atomic<int> initializations{0};

static void APS5_VABI Handler(int, void* context) {
std::uint64_t rip = 0;
Expand Down Expand Up @@ -108,6 +115,124 @@ static void WaitDeliveries(int expected) {
}
}

static void APS5_VABI Stop(int, void*) {
stops.fetch_add(1);
while (!resume.load()) std::this_thread::yield();
resumed.fetch_add(1);
}

static void WaitCount(const std::atomic<int>& count, int expected) {
const auto deadline = std::chrono::steady_clock::now() + std::chrono::seconds(2);
while (count.load() < expected) {
Require(std::chrono::steady_clock::now() < deadline);
std::this_thread::yield();
}
}

struct Relock {
PthreadMutex mutex = nullptr;
PthreadCond cond = nullptr;
int levels = 1;
bool timed = false;
bool signaled = false;
std::atomic<bool> waiting{false};
std::atomic<int> result{-1};
};

static void* APS5_VABI Relocker(void* opaque) {
auto& relock = *static_cast<Relock*>(opaque);
for (int level = 0; level < relock.levels; ++level) Require(scePthreadMutexLock(&relock.mutex) == 0);
relock.waiting.store(true);
int waited = 0;
while (waited == 0 && !relock.signaled)
waited = relock.timed ? scePthreadCondTimedwait(&relock.cond, &relock.mutex, 10000000) : scePthreadCondWait(&relock.cond, &relock.mutex);
relock.result.store(waited);
for (int level = 0; level < relock.levels; ++level) Require(scePthreadMutexUnlock(&relock.mutex) == 0);
Require(scePthreadMutexUnlock(&relock.mutex) != 0);
return nullptr;
}

static void RaiseInRelock(int levels, bool timed) {
for (int round = 0; round < Rounds; ++round) {
Relock relock;
relock.levels = levels;
relock.timed = timed;
PthreadMutexattr attr = nullptr;
Require(scePthreadMutexattrInit(&attr) == 0);
if (levels > 1) Require(scePthreadMutexattrSettype(&attr, MutexTypeRecursive) == 0);
Require(scePthreadMutexInit(&relock.mutex, &attr, nullptr) == 0);
Require(scePthreadMutexattrDestroy(&attr) == 0);
Require(scePthreadCondInit(&relock.cond, nullptr, nullptr) == 0);
Pthread waiter = nullptr;
Require(scePthreadCreate(&waiter, nullptr, Relocker, &relock, "relocking after a wake") == 0);
while (!relock.waiting.load()) std::this_thread::yield();
Require(scePthreadMutexLock(&relock.mutex) == 0);
relock.signaled = true;
Require(scePthreadCondSignal(&relock.cond) == 0);
for (int raise = 0; raise < 2; ++raise) {
std::this_thread::sleep_for(std::chrono::milliseconds(20));
resume.store(false);
const int before = stops.load();
Require(sceKernelRaiseException(waiter, SigUsr1) == 0);
WaitCount(stops, before + 1);
Require(relock.result.load() == -1);
resume.store(true);
WaitCount(resumed, before + 1);
}
Require(scePthreadMutexUnlock(&relock.mutex) == 0);
Require(scePthreadJoin(waiter, nullptr) == 0);
Require(relock.result.load() == 0);
Require(scePthreadCondDestroy(&relock.cond) == 0);
Require(scePthreadMutexDestroy(&relock.mutex) == 0);
}
}

static void APS5_VABI Initialize() {
initializations.fetch_add(1);
while (!resume.load()) std::this_thread::yield();
}

struct OnceCall {
GuestOnce* control = nullptr;
std::atomic<bool> calling{false};
std::atomic<int> result{-1};
};

static void* APS5_VABI CallOnce(void* opaque) {
auto& call = *static_cast<OnceCall*>(opaque);
call.calling.store(true);
call.result.store(pthread_once_nid_postfix(call.control, Initialize));
return nullptr;
}

static void RaiseInOnce() {
for (int round = 0; round < Rounds; ++round) {
GuestOnce control{0, nullptr};
OnceCall first{&control};
OnceCall second{&control};
resume.store(false);
const int before = stops.load();
Pthread initializer = nullptr;
Require(scePthreadCreate(&initializer, nullptr, CallOnce, &first, "running a once initializer") == 0);
while (initializations.load() != round + 1) std::this_thread::yield();
Require(sceKernelRaiseException(initializer, SigUsr1) == 0);
WaitCount(stops, before + 1);
Pthread waiter = nullptr;
Require(scePthreadCreate(&waiter, nullptr, CallOnce, &second, "waiting on a once initializer") == 0);
while (!second.calling.load()) std::this_thread::yield();
std::this_thread::sleep_for(std::chrono::milliseconds(20));
Require(sceKernelRaiseException(waiter, SigUsr1) == 0);
WaitCount(stops, before + 2);
Require(first.result.load() == -1 && second.result.load() == -1);
resume.store(true);
Require(scePthreadJoin(initializer, nullptr) == 0);
Require(scePthreadJoin(waiter, nullptr) == 0);
Require(first.result.load() == 0 && second.result.load() == 0);
Require(control.state == 1);
Require(initializations.load() == round + 1);
}
}

int main() {
const auto* image = reinterpret_cast<const unsigned char*>(GetModuleHandleW(nullptr));
const auto* headers = reinterpret_cast<const IMAGE_NT_HEADERS*>(image + reinterpret_cast<const IMAGE_DOS_HEADER*>(image)->e_lfanew);
Expand Down Expand Up @@ -175,4 +300,12 @@ int main() {
Require(outsideImage.load() == 0);
Require(beforeWait.load() == Rounds);
Require(insideAlertableWait.load() == 0);

Require(sceKernelInstallExceptionHandler(SigUsr1, reinterpret_cast<void*>(&Stop)) == 0);
RaiseInRelock(1, false);
RaiseInRelock(3, false);
RaiseInRelock(1, true);
RaiseInRelock(3, true);
RaiseInOnce();
Require(sceKernelRemoveExceptionHandler(SigUsr1) == 0);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# libkernel

## change waiting on a thread already stopped

- [sceKernelRaiseException](../../../core/libs/prx/libkernel/System/src/Exception.cpp) on Windows runs the handler after the raise has returned when the target is not in guest code (the executable and the `*.guest.prx` modules): a target in a libkernel wait takes it as an APC at that wait; a target elsewhere in host code, running or blocked, takes it when it returns into guest code, through a debug register 0 execute breakpoint at that return address or a host thread that keeps sampling it. A target in guest code is redirected at once. Unlike the console, the handler's context at such a return holds no vector register beyond what the C runtime context saves; a target that stays in host code waiting on a thread already stopped, as in `pthread_barrier_wait`, `scePthreadOnce`, libc `_Mtx_lock`, `std::call_once` or `__cxa_guard_acquire`, is never stopped; a target inside NtContinue makes the raise throw after 1 s; a debugger or title using debug register 0 on a raised thread is not modeled
Loading