-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Conditionalize use of POSIX features missing on WASI/WebAssembly #92677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,17 @@ | |
#include "llvm/Support/Signals.h" | ||
#include "llvm/Support/thread.h" | ||
#include <cassert> | ||
#if !defined(__wasi__) | ||
#include <csignal> | ||
#endif | ||
#if LLVM_ENABLE_THREADS | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unfortunate; there's no reason (in theory) that mutex shouldn't work fine in single-threaded mode. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it actually does work, since I ended up building LLVM always with the |
||
#include <mutex> | ||
#endif | ||
#if HAVE_SETJMP | ||
// We can rely on setjmp to exist everywhere except for a subset of WebAssembly | ||
// builds. | ||
#include <setjmp.h> | ||
#endif | ||
|
||
using namespace llvm; | ||
|
||
|
@@ -31,7 +40,9 @@ struct CrashRecoveryContextImpl { | |
const CrashRecoveryContextImpl *Next; | ||
|
||
CrashRecoveryContext *CRC; | ||
#ifdef HAVE_SETJMP | ||
::jmp_buf JumpBuffer; | ||
#endif | ||
volatile unsigned Failed : 1; | ||
unsigned SwitchedThread : 1; | ||
unsigned ValidJumpBuffer : 1; | ||
|
@@ -50,7 +61,7 @@ struct CrashRecoveryContextImpl { | |
/// Called when the separate crash-recovery thread was finished, to | ||
/// indicate that we don't need to clear the thread-local CurrentContext. | ||
void setSwitchedThread() { | ||
#if defined(LLVM_ENABLE_THREADS) && LLVM_ENABLE_THREADS != 0 | ||
#if LLVM_ENABLE_THREADS | ||
SwitchedThread = true; | ||
#endif | ||
} | ||
|
@@ -72,19 +83,23 @@ struct CrashRecoveryContextImpl { | |
|
||
CRC->RetCode = RetCode; | ||
|
||
#if HAVE_SETJMP | ||
// Jump back to the RunSafely we were called under. | ||
if (ValidJumpBuffer) | ||
longjmp(JumpBuffer, 1); | ||
#endif | ||
|
||
// Otherwise let the caller decide of the outcome of the crash. Currently | ||
// this occurs when using SEH on Windows with MSVC or clang-cl. | ||
} | ||
}; | ||
|
||
std::mutex &getCrashRecoveryContextMutex() { | ||
#if LLVM_ENABLE_THREADS | ||
static std::mutex &getCrashRecoveryContextMutex() { | ||
static std::mutex CrashRecoveryContextMutex; | ||
return CrashRecoveryContextMutex; | ||
} | ||
#endif | ||
|
||
static bool gCrashRecoveryEnabled = false; | ||
|
||
|
@@ -138,7 +153,9 @@ CrashRecoveryContext *CrashRecoveryContext::GetCurrent() { | |
} | ||
|
||
void CrashRecoveryContext::Enable() { | ||
#if LLVM_ENABLE_THREADS | ||
std::lock_guard<std::mutex> L(getCrashRecoveryContextMutex()); | ||
#endif | ||
// FIXME: Shouldn't this be a refcount or something? | ||
if (gCrashRecoveryEnabled) | ||
return; | ||
|
@@ -147,7 +164,9 @@ void CrashRecoveryContext::Enable() { | |
} | ||
|
||
void CrashRecoveryContext::Disable() { | ||
#if LLVM_ENABLE_THREADS | ||
std::lock_guard<std::mutex> L(getCrashRecoveryContextMutex()); | ||
#endif | ||
if (!gCrashRecoveryEnabled) | ||
return; | ||
gCrashRecoveryEnabled = false; | ||
|
@@ -329,7 +348,16 @@ static void uninstallExceptionOrSignalHandlers() { | |
} | ||
} | ||
|
||
#else // !_WIN32 | ||
#elif defined(__wasi__) | ||
|
||
// WASI implementation. | ||
// | ||
// WASI traps are always fatal, and recovery is not possible. Do nothing. | ||
|
||
static void installExceptionOrSignalHandlers() {} | ||
static void uninstallExceptionOrSignalHandlers() {} | ||
|
||
#else // !_WIN32 && !__wasi__ | ||
|
||
// Generic POSIX implementation. | ||
// | ||
|
@@ -417,10 +445,12 @@ bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) { | |
CrashRecoveryContextImpl *CRCI = new CrashRecoveryContextImpl(this); | ||
Impl = CRCI; | ||
|
||
#if HAVE_SETJMP | ||
CRCI->ValidJumpBuffer = true; | ||
if (setjmp(CRCI->JumpBuffer) != 0) { | ||
return false; | ||
} | ||
#endif | ||
} | ||
|
||
Fn(); | ||
|
@@ -467,7 +497,9 @@ bool CrashRecoveryContext::isCrash(int RetCode) { | |
bool CrashRecoveryContext::throwIfCrash(int RetCode) { | ||
if (!isCrash(RetCode)) | ||
return false; | ||
#if defined(_WIN32) | ||
#if defined(__wasi__) | ||
abort(); | ||
#elif defined(_WIN32) | ||
::RaiseException(RetCode, 0, 0, NULL); | ||
#else | ||
llvm::sys::unregisterHandlers(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,7 +94,7 @@ static std::error_code getHostID(SmallVectorImpl<char> &HostID) { | |
StringRef UUIDRef(UUIDStr); | ||
HostID.append(UUIDRef.begin(), UUIDRef.end()); | ||
|
||
#elif LLVM_ON_UNIX | ||
#elif !defined(__wasi__) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're changing behavior for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, this is the cause of buildbot failures that I missed during my own review. |
||
char HostName[256]; | ||
HostName[255] = 0; | ||
HostName[0] = 0; | ||
|
@@ -111,7 +111,7 @@ static std::error_code getHostID(SmallVectorImpl<char> &HostID) { | |
} | ||
|
||
bool LockFileManager::processStillExecuting(StringRef HostID, int PID) { | ||
#if LLVM_ON_UNIX && !defined(__ANDROID__) | ||
#if LLVM_ON_UNIX && !defined(__ANDROID__) && !defined(__wasi__) | ||
SmallString<256> StoredHostID; | ||
if (getHostID(StoredHostID)) | ||
return true; // Conservatively assume it's executing on error. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -273,9 +273,19 @@ static bool printMarkupStackTrace(StringRef Argv0, void **StackTrace, int Depth, | |
} | ||
|
||
// Include the platform-specific parts of this class. | ||
#ifdef LLVM_ON_UNIX | ||
#if defined(__wasi__) | ||
// WASI does not have signals. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move these stubs to WASI/Signals.inc for consistency with the other platform support files. |
||
void llvm::sys::AddSignalHandler(sys::SignalHandlerCallback FnPtr, | ||
void *Cookie) {} | ||
void llvm::sys::RunInterruptHandlers() {} | ||
void sys::CleanupOnSignal(uintptr_t Context) {} | ||
bool llvm::sys::RemoveFileOnSignal(StringRef Filename, std::string *ErrMsg) { | ||
return false; | ||
} | ||
void llvm::sys::DontRemoveFileOnSignal(StringRef Filename) {} | ||
void llvm::sys::DisableSystemDialogsOnCrash() {} | ||
#elif defined(LLVM_ON_UNIX) | ||
#include "Unix/Signals.inc" | ||
#endif | ||
#ifdef _WIN32 | ||
#elif defined(_WIN32) | ||
#include "Windows/Signals.inc" | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's already a check below,
Probably that should be a constant in no-threads builds, then maybe this change isn't needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, I don't quite understand. Are you asking me to change
ThreadPoolStrategy::ThreadsRequested
to be a constant? I could do that but it seems like an invasive change. Looking at other uses ofThreadsRequested
, it looks like many of them are guarded in essentially the same way as what this patch introduces, e.g. here:llvm-project/llvm/lib/Support/Parallel.cpp
Lines 196 to 201 in 6441df3
or here:
llvm-project/llvm/lib/Support/Parallel.cpp
Lines 227 to 230 in 6441df3