7
7
// ===----------------------------------------------------------------------===//
8
8
9
9
#include " llvm/Support/CrashRecoveryContext.h"
10
+
10
11
#include " llvm/Config/llvm-config.h"
11
12
#include " llvm/Support/ErrorHandling.h"
12
13
#include " llvm/Support/ExitCodes.h"
13
14
#include " llvm/Support/Signals.h"
14
15
#include " llvm/Support/thread.h"
15
16
#include < cassert>
16
17
#include < mutex>
18
+ #ifndef __wasi__
17
19
#include < setjmp.h>
20
+ #endif
18
21
19
22
using namespace llvm ;
20
23
@@ -31,7 +34,9 @@ struct CrashRecoveryContextImpl {
31
34
const CrashRecoveryContextImpl *Next;
32
35
33
36
CrashRecoveryContext *CRC;
37
+ #ifndef __wasi__
34
38
::jmp_buf JumpBuffer;
39
+ #endif
35
40
volatile unsigned Failed : 1 ;
36
41
unsigned SwitchedThread : 1 ;
37
42
unsigned ValidJumpBuffer : 1 ;
@@ -72,9 +77,11 @@ struct CrashRecoveryContextImpl {
72
77
73
78
CRC->RetCode = RetCode;
74
79
80
+ #ifndef __wasi__
75
81
// Jump back to the RunSafely we were called under.
76
82
if (ValidJumpBuffer)
77
83
longjmp (JumpBuffer, 1 );
84
+ #endif
78
85
79
86
// Otherwise let the caller decide of the outcome of the crash. Currently
80
87
// this occurs when using SEH on Windows with MSVC or clang-cl.
@@ -118,7 +125,7 @@ CrashRecoveryContext::~CrashRecoveryContext() {
118
125
}
119
126
IsRecoveringFromCrash = PC;
120
127
121
- CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *) Impl;
128
+ CrashRecoveryContextImpl *CRCI = (CrashRecoveryContextImpl *)Impl;
122
129
delete CRCI;
123
130
}
124
131
@@ -154,8 +161,8 @@ void CrashRecoveryContext::Disable() {
154
161
uninstallExceptionOrSignalHandlers ();
155
162
}
156
163
157
- void CrashRecoveryContext::registerCleanup (CrashRecoveryContextCleanup *cleanup)
158
- {
164
+ void CrashRecoveryContext::registerCleanup (
165
+ CrashRecoveryContextCleanup *cleanup) {
159
166
if (!cleanup)
160
167
return ;
161
168
if (head)
@@ -164,16 +171,15 @@ void CrashRecoveryContext::registerCleanup(CrashRecoveryContextCleanup *cleanup)
164
171
head = cleanup;
165
172
}
166
173
167
- void
168
- CrashRecoveryContext::unregisterCleanup ( CrashRecoveryContextCleanup *cleanup) {
174
+ void CrashRecoveryContext::unregisterCleanup (
175
+ CrashRecoveryContextCleanup *cleanup) {
169
176
if (!cleanup)
170
177
return ;
171
178
if (cleanup == head) {
172
179
head = cleanup->next ;
173
180
if (head)
174
181
head->prev = nullptr ;
175
- }
176
- else {
182
+ } else {
177
183
cleanup->prev ->next = cleanup->next ;
178
184
if (cleanup->next )
179
185
cleanup->next ->prev = cleanup->prev ;
@@ -263,16 +269,14 @@ bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) {
263
269
264
270
#include " llvm/Support/Windows/WindowsSupport.h"
265
271
266
- static LONG CALLBACK ExceptionHandler (PEXCEPTION_POINTERS ExceptionInfo)
267
- {
272
+ static LONG CALLBACK ExceptionHandler (PEXCEPTION_POINTERS ExceptionInfo) {
268
273
// DBG_PRINTEXCEPTION_WIDE_C is not properly defined on all supported
269
274
// compilers and platforms, so we define it manually.
270
275
constexpr ULONG DbgPrintExceptionWideC = 0x4001000AL ;
271
- switch (ExceptionInfo->ExceptionRecord ->ExceptionCode )
272
- {
276
+ switch (ExceptionInfo->ExceptionRecord ->ExceptionCode ) {
273
277
case DBG_PRINTEXCEPTION_C:
274
278
case DbgPrintExceptionWideC:
275
- case 0x406D1388 : // set debugger thread name
279
+ case 0x406D1388 : // set debugger thread name
276
280
return EXCEPTION_CONTINUE_EXECUTION;
277
281
}
278
282
@@ -307,7 +311,7 @@ static LONG CALLBACK ExceptionHandler(PEXCEPTION_POINTERS ExceptionInfo)
307
311
// CrashRecoveryContext at all. So we make use of a thread-local
308
312
// exception table. The handles contained in here will either be
309
313
// non-NULL, valid VEH handles, or NULL.
310
- static LLVM_THREAD_LOCAL const void * sCurrentExceptionHandle ;
314
+ static LLVM_THREAD_LOCAL const void * sCurrentExceptionHandle ;
311
315
312
316
static void installExceptionOrSignalHandlers () {
313
317
// We can set up vectored exception handling now. We will install our
@@ -342,10 +346,11 @@ static void uninstallExceptionOrSignalHandlers() {
342
346
// reliable fashion -- if we get a signal outside of a crash recovery context we
343
347
// simply disable crash recovery and raise the signal again.
344
348
349
+ #ifndef __wasi__
345
350
#include < signal.h>
346
351
347
- static const int Signals[] =
348
- { SIGABRT, SIGBUS, SIGFPE, SIGILL, SIGSEGV, SIGTRAP };
352
+ static const int Signals[] = {SIGABRT, SIGBUS, SIGFPE,
353
+ SIGILL, SIGSEGV, SIGTRAP};
349
354
static const unsigned NumSignals = std::size(Signals);
350
355
static struct sigaction PrevActions[NumSignals];
351
356
@@ -389,8 +394,10 @@ static void CrashRecoverySignalHandler(int Signal) {
389
394
if (CRCI)
390
395
const_cast <CrashRecoveryContextImpl *>(CRCI)->HandleCrash (RetCode, Signal);
391
396
}
397
+ #endif
392
398
393
399
static void installExceptionOrSignalHandlers () {
400
+ #ifndef __wasi__
394
401
// Setup the signal handler.
395
402
struct sigaction Handler;
396
403
Handler.sa_handler = CrashRecoverySignalHandler;
@@ -400,12 +407,15 @@ static void installExceptionOrSignalHandlers() {
400
407
for (unsigned i = 0 ; i != NumSignals; ++i) {
401
408
sigaction (Signals[i], &Handler, &PrevActions[i]);
402
409
}
410
+ #endif
403
411
}
404
412
405
413
static void uninstallExceptionOrSignalHandlers () {
414
+ #ifndef __wasi__
406
415
// Restore the previous signal handlers.
407
416
for (unsigned i = 0 ; i != NumSignals; ++i)
408
417
sigaction (Signals[i], &PrevActions[i], nullptr );
418
+ #endif
409
419
}
410
420
411
421
#endif // !_WIN32
@@ -418,9 +428,11 @@ bool CrashRecoveryContext::RunSafely(function_ref<void()> Fn) {
418
428
Impl = CRCI;
419
429
420
430
CRCI->ValidJumpBuffer = true ;
431
+ #ifndef __wasi__
421
432
if (setjmp (CRCI->JumpBuffer ) != 0 ) {
422
433
return false ;
423
434
}
435
+ #endif
424
436
}
425
437
426
438
Fn ();
@@ -469,7 +481,7 @@ bool CrashRecoveryContext::throwIfCrash(int RetCode) {
469
481
return false ;
470
482
#if defined(_WIN32)
471
483
::RaiseException (RetCode, 0 , 0 , NULL );
472
- #else
484
+ #elif !defined(__wasi__)
473
485
llvm::sys::unregisterHandlers ();
474
486
raise (RetCode - 128 );
475
487
#endif
@@ -502,7 +514,7 @@ struct RunSafelyOnThreadInfo {
502
514
503
515
static void RunSafelyOnThread_Dispatch (void *UserData) {
504
516
RunSafelyOnThreadInfo *Info =
505
- reinterpret_cast <RunSafelyOnThreadInfo*>(UserData);
517
+ reinterpret_cast <RunSafelyOnThreadInfo *>(UserData);
506
518
507
519
if (Info->UseBackgroundPriority )
508
520
setThreadBackgroundPriority ();
@@ -512,7 +524,7 @@ static void RunSafelyOnThread_Dispatch(void *UserData) {
512
524
bool CrashRecoveryContext::RunSafelyOnThread (function_ref<void ()> Fn,
513
525
unsigned RequestedStackSize) {
514
526
bool UseBackgroundPriority = hasThreadBackgroundPriority ();
515
- RunSafelyOnThreadInfo Info = { Fn, this , UseBackgroundPriority, false };
527
+ RunSafelyOnThreadInfo Info = {Fn, this , UseBackgroundPriority, false };
516
528
llvm::thread Thread (RequestedStackSize == 0
517
529
? std::nullopt
518
530
: std::optional<unsigned >(RequestedStackSize),
0 commit comments