77// ===----------------------------------------------------------------------===//
88
99#include " llvm/Support/CrashRecoveryContext.h"
10+
1011#include " llvm/Config/llvm-config.h"
1112#include " llvm/Support/ErrorHandling.h"
1213#include " llvm/Support/ExitCodes.h"
1314#include " llvm/Support/Signals.h"
1415#include " llvm/Support/thread.h"
1516#include < cassert>
1617#include < mutex>
18+ #ifndef __wasi__
1719#include < setjmp.h>
20+ #endif
1821
1922using namespace llvm ;
2023
@@ -31,7 +34,9 @@ struct CrashRecoveryContextImpl {
3134 const CrashRecoveryContextImpl *Next;
3235
3336 CrashRecoveryContext *CRC;
37+ #ifndef __wasi__
3438 ::jmp_buf JumpBuffer;
39+ #endif
3540 volatile unsigned Failed : 1 ;
3641 unsigned SwitchedThread : 1 ;
3742 unsigned ValidJumpBuffer : 1 ;
@@ -72,9 +77,11 @@ struct CrashRecoveryContextImpl {
7277
7378 CRC->RetCode = RetCode;
7479
80+ #ifndef __wasi__
7581 // Jump back to the RunSafely we were called under.
7682 if (ValidJumpBuffer)
7783 longjmp (JumpBuffer, 1 );
84+ #endif
7885
7986 // Otherwise let the caller decide of the outcome of the crash. Currently
8087 // this occurs when using SEH on Windows with MSVC or clang-cl.
@@ -118,7 +125,7 @@ CrashRecoveryContext::~CrashRecoveryContext() {
118125 }
119126 IsRecoveringFromCrash = PC;
120127
121- CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
128+ CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *)Impl;
122129 delete CRCI;
123130}
124131
@@ -154,8 +161,8 @@ void CrashRecoveryContext::Disable() {
154161 uninstallExceptionOrSignalHandlers ();
155162}
156163
157- void CrashRecoveryContext::registerCleanup (CrashRecoveryContextCleanup *cleanup)
158- {
164+ void CrashRecoveryContext::registerCleanup (
165+ CrashRecoveryContextCleanup *cleanup) {
159166 if (!cleanup)
160167 return ;
161168 if (head)
@@ -164,16 +171,15 @@ void CrashRecoveryContext::registerCleanup(CrashRecoveryContextCleanup *cleanup)
164171 head = cleanup;
165172}
166173
167- void
168- CrashRecoveryContext::unregisterCleanup ( CrashRecoveryContextCleanup *cleanup) {
174+ void CrashRecoveryContext::unregisterCleanup (
175+ CrashRecoveryContextCleanup *cleanup) {
169176 if (!cleanup)
170177 return ;
171178 if (cleanup == head) {
172179 head = cleanup->next ;
173180 if (head)
174181 head->prev = nullptr ;
175- }
176- else {
182+ } else {
177183 cleanup->prev ->next = cleanup->next ;
178184 if (cleanup->next )
179185 cleanup->next ->prev = cleanup->prev ;
@@ -263,16 +269,14 @@ bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) {
263269
264270#include " llvm/Support/Windows/WindowsSupport.h"
265271
266- static LONG CALLBACK ExceptionHandler (PEXCEPTION_POINTERS ExceptionInfo)
267- {
272+ static LONG CALLBACK ExceptionHandler (PEXCEPTION_POINTERS ExceptionInfo) {
268273 // DBG_PRINTEXCEPTION_WIDE_C is not properly defined on all supported
269274 // compilers and platforms, so we define it manually.
270275 constexpr ULONG DbgPrintExceptionWideC = 0x4001000AL ;
271- switch (ExceptionInfo->ExceptionRecord ->ExceptionCode )
272- {
276+ switch (ExceptionInfo->ExceptionRecord ->ExceptionCode ) {
273277 case DBG_PRINTEXCEPTION_C:
274278 case DbgPrintExceptionWideC:
275- case 0x406D1388 : // set debugger thread name
279+ case 0x406D1388 : // set debugger thread name
276280 return EXCEPTION_CONTINUE_EXECUTION;
277281 }
278282
@@ -307,7 +311,7 @@ static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo)
307311// CrashRecoveryContext at all. So we make use of a thread-local
308312// exception table. The handles contained in here will either be
309313// non-NULL, valid VEH handles, or NULL.
310- static LLVM_THREAD_LOCAL const void * sCurrentExceptionHandle ;
314+ static LLVM_THREAD_LOCAL const void * sCurrentExceptionHandle ;
311315
312316static void installExceptionOrSignalHandlers () {
313317 // We can set up vectored exception handling now. We will install our
@@ -342,10 +346,11 @@ static void uninstallExceptionOrSignalHandlers() {
342346// reliable fashion -- if we get a signal outside of a crash recovery context we
343347// simply disable crash recovery and raise the signal again.
344348
349+ #ifndef __wasi__
345350#include < signal.h>
346351
347- static const int Signals[] =
348- { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
352+ static const int Signals[] = {SIGABRT, SIGBUS, SIGFPE,
353+ SIGILL, SIGSEGV, SIGTRAP};
349354static const unsigned NumSignals = std::size(Signals);
350355static struct sigaction PrevActions[NumSignals];
351356
@@ -389,8 +394,10 @@ static void CrashRecoverySignalHandler(int Signal) {
389394 if (CRCI)
390395 const_cast <CrashRecoveryContextImpl *>(CRCI)->HandleCrash (RetCode, Signal);
391396}
397+ #endif
392398
393399static void installExceptionOrSignalHandlers () {
400+ #ifndef __wasi__
394401 // Setup the signal handler.
395402 struct sigaction Handler;
396403 Handler.sa_handler = CrashRecoverySignalHandler;
@@ -400,12 +407,15 @@ static void installExceptionOrSignalHandlers() {
400407 for (unsigned i = 0 ; i != NumSignals; ++i) {
401408 sigaction (Signals[i], &Handler, &PrevActions[i]);
402409 }
410+ #endif
403411}
404412
405413static void uninstallExceptionOrSignalHandlers () {
414+ #ifndef __wasi__
406415 // Restore the previous signal handlers.
407416 for (unsigned i = 0 ; i != NumSignals; ++i)
408417 sigaction (Signals[i], &PrevActions[i], nullptr );
418+ #endif
409419}
410420
411421#endif // !_WIN32
@@ -418,9 +428,11 @@ bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) {
418428 Impl = CRCI;
419429
420430 CRCI->ValidJumpBuffer = true ;
431+ #ifndef __wasi__
421432 if (setjmp (CRCI->JumpBuffer ) != 0 ) {
422433 return false ;
423434 }
435+ #endif
424436 }
425437
426438 Fn ();
@@ -469,7 +481,7 @@ bool CrashRecoveryContext::throwIfCrash(int RetCode) {
469481 return false ;
470482#if defined(_WIN32)
471483 ::RaiseException (RetCode, 0 , 0 , NULL );
472- #else
484+ #elif !defined(__wasi__)
473485 llvm::sys::unregisterHandlers ();
474486 raise (RetCode - 128 );
475487#endif
@@ -502,7 +514,7 @@ struct RunSafelyOnThreadInfo {
502514
503515static void RunSafelyOnThread_Dispatch (void *UserData) {
504516 RunSafelyOnThreadInfo *Info =
505- reinterpret_cast <RunSafelyOnThreadInfo*>(UserData);
517+ reinterpret_cast <RunSafelyOnThreadInfo *>(UserData);
506518
507519 if (Info->UseBackgroundPriority )
508520 setThreadBackgroundPriority ();
@@ -512,7 +524,7 @@ static void RunSafelyOnThread_Dispatch(void *UserData) {
512524bool CrashRecoveryContext::RunSafelyOnThread (function_ref<void ()> Fn,
513525 unsigned RequestedStackSize) {
514526 bool UseBackgroundPriority = hasThreadBackgroundPriority ();
515- RunSafelyOnThreadInfo Info = { Fn, this , UseBackgroundPriority, false };
527+ RunSafelyOnThreadInfo Info = {Fn, this , UseBackgroundPriority, false };
516528 llvm::thread Thread (RequestedStackSize == 0
517529 ? std::nullopt
518530 : std::optional<unsigned >(RequestedStackSize),
0 commit comments