AnyPS5/core/libs/tests/GuestRaiseException.cpp

200 lines
7.7 KiB
C++

#include "SceTypes.hpp"
#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <mutex>
#include <stdexcept>
#include <thread>
extern "C" {
int APS5_VABI sceKernelInstallExceptionHandler(int signum, void* handler);
int APS5_VABI sceKernelRemoveExceptionHandler(int signum);
int APS5_VABI sceKernelRaiseException(Pthread thread, int signum);
int APS5_VABI scePthreadCreate(Pthread* thread, const PthreadAttr* attr, PthreadEntry entry, void* arg, const char* name);
int APS5_VABI scePthreadJoin(Pthread thread, void** retval);
Pthread APS5_VABI scePthreadSelf();
int APS5_VABI sceKernelCreateSema(KernelSema* sem, const char* name, uint32_t attr, int init, int max, void* opt);
int APS5_VABI sceKernelDeleteSema(KernelSema sem);
int APS5_VABI sceKernelSignalSema(KernelSema sem, int count);
int APS5_VABI sceKernelWaitSema(KernelSema sem, int need, KernelUseconds* time);
}
static constexpr int SCE_KERNEL_ERROR_EINVAL = static_cast<int>(0x80020016);
static constexpr int SCE_KERNEL_ERROR_ESRCH = static_cast<int>(0x80020003);
static constexpr int SIGUSR1 = 30;
static constexpr int Repeats = 100;
static void Require(bool value) { if (!value) std::abort(); }
static std::atomic<int> calls{0};
static std::atomic<std::thread::id> handlerThread;
static std::atomic<std::uint64_t> handlerRsp{0};
static std::atomic<std::uintptr_t> handlerFrame{0};
static void APS5_VABI Handler(int signum, void* context) {
Require(signum == SIGUSR1);
std::uint64_t rsp = 0;
std::memcpy(&rsp, static_cast<unsigned char*>(context) + 0xf8, sizeof(rsp));
int local = 0;
handlerFrame.store(reinterpret_cast<std::uintptr_t>(&local));
handlerRsp.store(rsp);
handlerThread.store(std::this_thread::get_id());
calls.fetch_add(1);
}
struct Worker {
std::atomic<bool> started{false};
std::atomic<bool> stop{false};
std::thread::id id;
KernelSema sem = nullptr;
int waitResult = -1;
};
static void* APS5_VABI Busy(void* arg) {
auto& worker = *static_cast<Worker*>(arg);
worker.id = std::this_thread::get_id();
worker.started.store(true);
volatile std::uint64_t spins = 0;
while (!worker.stop.load()) spins = spins + 1;
return nullptr;
}
static void* APS5_VABI Waiting(void* arg) {
auto& worker = *static_cast<Worker*>(arg);
worker.id = std::this_thread::get_id();
worker.started.store(true);
worker.waitResult = sceKernelWaitSema(worker.sem, 1, nullptr);
return nullptr;
}
static std::mutex hostLock;
static constexpr int HostRounds = 20;
static std::atomic<int> hostRound{0};
static void* APS5_VABI HostBlocked(void* arg) {
auto& worker = *static_cast<Worker*>(arg);
worker.id = std::this_thread::get_id();
for (int round = 0; round < HostRounds; ++round) {
while (hostRound.load() != round) std::this_thread::yield();
worker.started.store(true);
std::lock_guard lock(hostLock);
}
return nullptr;
}
static constexpr int LeavingRounds = 200;
static std::atomic<int> leavingRound{0};
static void* APS5_VABI Leaving(void* arg) {
auto& worker = *static_cast<Worker*>(arg);
worker.id = std::this_thread::get_id();
for (int round = 0; round < LeavingRounds; ++round) {
worker.started.store(true);
Require(sceKernelWaitSema(worker.sem, 1, nullptr) == 0);
volatile std::uint64_t spins = 0;
while (leavingRound.load() == round) spins = spins + 1;
}
return nullptr;
}
static std::atomic<bool> finishedReturned{false};
static void* APS5_VABI Finished(void*) {
finishedReturned.store(true);
return nullptr;
}
static void ExpectDelivery(int before, std::thread::id thread) {
for (int attempt = 0; attempt < 5000 && calls.load() == before; ++attempt) std::this_thread::sleep_for(std::chrono::milliseconds(1));
Require(calls.load() == before + 1);
Require(handlerThread.load() == thread);
Require(handlerRsp.load() != 0 && handlerFrame.load() < handlerRsp.load());
}
int main() {
Require(sceKernelRaiseException(scePthreadSelf(), 11) == SCE_KERNEL_ERROR_EINVAL);
bool rejected = false;
try {
sceKernelRaiseException(scePthreadSelf(), SIGUSR1);
} catch (const std::runtime_error&) {
rejected = true;
}
Require(rejected);
Require(sceKernelInstallExceptionHandler(SIGUSR1, reinterpret_cast<void*>(&Handler)) == 0);
Require(sceKernelRaiseException(scePthreadSelf(), SIGUSR1) == 0);
Require(calls.load() == 1 && handlerThread.load() == std::this_thread::get_id());
Worker busy;
Pthread busyThread = nullptr;
Require(scePthreadCreate(&busyThread, nullptr, Busy, &busy, "busy") == 0);
while (!busy.started.load()) std::this_thread::yield();
for (int raised = 0; raised < Repeats; ++raised) {
Require(sceKernelRaiseException(busyThread, SIGUSR1) == 0);
ExpectDelivery(1 + raised, busy.id);
}
busy.stop.store(true);
Require(scePthreadJoin(busyThread, nullptr) == 0);
Worker waiting;
Require(sceKernelCreateSema(&waiting.sem, "raise", 0, 0, 1, nullptr) == 0);
Pthread waitingThread = nullptr;
Require(scePthreadCreate(&waitingThread, nullptr, Waiting, &waiting, "waiting") == 0);
while (!waiting.started.load()) std::this_thread::yield();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
for (int raised = 0; raised < Repeats; ++raised) {
Require(sceKernelRaiseException(waitingThread, SIGUSR1) == 0);
ExpectDelivery(1 + Repeats + raised, waiting.id);
}
Require(sceKernelSignalSema(waiting.sem, 1) == 0);
Require(scePthreadJoin(waitingThread, nullptr) == 0);
Require(waiting.waitResult == 0);
Require(sceKernelDeleteSema(waiting.sem) == 0);
Worker blocked;
Pthread blockedThread = nullptr;
hostLock.lock();
Require(scePthreadCreate(&blockedThread, nullptr, HostBlocked, &blocked, "host blocked") == 0);
for (int round = 0; round < HostRounds; ++round) {
while (!blocked.started.load()) std::this_thread::yield();
std::this_thread::sleep_for(std::chrono::milliseconds(20));
Require(sceKernelRaiseException(blockedThread, SIGUSR1) == 0);
hostLock.unlock();
ExpectDelivery(1 + 2 * Repeats + round, blocked.id);
hostLock.lock();
blocked.started.store(false);
hostRound.store(round + 1);
}
hostLock.unlock();
Require(scePthreadJoin(blockedThread, nullptr) == 0);
Worker leaving;
Require(sceKernelCreateSema(&leaving.sem, "leaving", 0, 0, 1, nullptr) == 0);
Pthread leavingThread = nullptr;
Require(scePthreadCreate(&leavingThread, nullptr, Leaving, &leaving, "leaving") == 0);
for (int round = 0; round < LeavingRounds; ++round) {
while (!leaving.started.load()) std::this_thread::yield();
leaving.started.store(false);
std::this_thread::sleep_for(std::chrono::microseconds(200));
Require(sceKernelSignalSema(leaving.sem, 1) == 0);
for (int spin = 0; spin < round % 16 * 64; ++spin) std::this_thread::yield();
Require(sceKernelRaiseException(leavingThread, SIGUSR1) == 0);
ExpectDelivery(1 + 2 * Repeats + HostRounds + round, leaving.id);
leavingRound.store(round + 1);
}
Require(scePthreadJoin(leavingThread, nullptr) == 0);
Require(sceKernelDeleteSema(leaving.sem) == 0);
Pthread finishedThread = nullptr;
Require(scePthreadCreate(&finishedThread, nullptr, Finished, nullptr, "finished") == 0);
while (!finishedReturned.load()) std::this_thread::yield();
std::this_thread::sleep_for(std::chrono::milliseconds(50));
Require(sceKernelRaiseException(finishedThread, SIGUSR1) == SCE_KERNEL_ERROR_ESRCH);
Require(scePthreadJoin(finishedThread, nullptr) == 0);
Require(sceKernelRemoveExceptionHandler(SIGUSR1) == 0);
}