mirror of
https://github.com/shadps4-emu/shadPS4.git
synced 2024-12-28 18:46:06 +00:00
thread: Apply alternate signal stack to created threads. (#1724)
This commit is contained in:
parent
f1b23c616e
commit
cd9fc5d0e9
|
@ -1,15 +1,15 @@
|
||||||
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
|
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
|
||||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
|
||||||
#include "libraries/kernel/threads/pthread.h"
|
#include "common/alignment.h"
|
||||||
#include "thread.h"
|
|
||||||
|
|
||||||
#include "core/libraries/kernel/threads/pthread.h"
|
#include "core/libraries/kernel/threads/pthread.h"
|
||||||
|
#include "thread.h"
|
||||||
|
|
||||||
#ifdef _WIN64
|
#ifdef _WIN64
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#include "common/ntapi.h"
|
#include "common/ntapi.h"
|
||||||
#else
|
#else
|
||||||
|
#include <csignal>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -113,6 +113,17 @@ void NativeThread::Exit() {
|
||||||
|
|
||||||
NtTerminateThread(nullptr, 0);
|
NtTerminateThread(nullptr, 0);
|
||||||
#else
|
#else
|
||||||
|
// Disable and free the signal stack.
|
||||||
|
constexpr stack_t sig_stack = {
|
||||||
|
.ss_flags = SS_DISABLE,
|
||||||
|
};
|
||||||
|
sigaltstack(&sig_stack, nullptr);
|
||||||
|
|
||||||
|
if (sig_stack_ptr) {
|
||||||
|
free(sig_stack_ptr);
|
||||||
|
sig_stack_ptr = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
pthread_exit(nullptr);
|
pthread_exit(nullptr);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -122,6 +133,19 @@ void NativeThread::Initialize() {
|
||||||
tid = GetCurrentThreadId();
|
tid = GetCurrentThreadId();
|
||||||
#else
|
#else
|
||||||
tid = (u64)pthread_self();
|
tid = (u64)pthread_self();
|
||||||
|
|
||||||
|
// Set up an alternate signal handler stack to avoid overflowing small thread stacks.
|
||||||
|
const size_t page_size = getpagesize();
|
||||||
|
const size_t sig_stack_size = Common::AlignUp(std::max<size_t>(64_KB, MINSIGSTKSZ), page_size);
|
||||||
|
ASSERT_MSG(posix_memalign(&sig_stack_ptr, page_size, sig_stack_size) == 0,
|
||||||
|
"Failed to allocate signal stack: {}", errno);
|
||||||
|
|
||||||
|
const stack_t sig_stack = {
|
||||||
|
.ss_sp = sig_stack_ptr,
|
||||||
|
.ss_size = sig_stack_size,
|
||||||
|
.ss_flags = 0,
|
||||||
|
};
|
||||||
|
ASSERT_MSG(sigaltstack(&sig_stack, nullptr) == 0, "Failed to set signal stack: {}", errno);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,7 @@ private:
|
||||||
void* native_handle;
|
void* native_handle;
|
||||||
#else
|
#else
|
||||||
uintptr_t native_handle;
|
uintptr_t native_handle;
|
||||||
|
void* sig_stack_ptr;
|
||||||
#endif
|
#endif
|
||||||
u64 tid;
|
u64 tid;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue