Skip to content

Commit b7e21da

Browse files
committed
StdlibUnittest: add a VEH for TRAP on Windows
Add a vectored exception handler for illegal instructions on Windows. This allows us to emulate `signal(SIGTRAP, ...)`. This allows better coverage of tests.
1 parent 9421d01 commit b7e21da

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

stdlib/private/StdlibUnittest/InterceptTraps.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <io.h>
2121
#include <process.h>
2222
#include <stdlib.h>
23+
#define WIN32_LEAN_AND_MEAN
24+
#include <Windows.h>
2325
#endif
2426

2527
#include "swift/Runtime/Config.h"
@@ -46,6 +48,23 @@ static void CrashCatcher(int Sig) {
4648
_exit(0);
4749
}
4850

51+
#if defined(_WIN32)
52+
static LONG WINAPI
53+
VectoredCrashHandler(PEXCEPTION_POINTERS ExceptionInfo) {
54+
switch (ExceptionInfo->ExceptionRecord->ExceptionCode) {
55+
case EXCEPTION_ILLEGAL_INSTRUCTION:
56+
_write(_fileno(stderr), "CRASHED: SIGTRAP\n", 17);
57+
_exit(0);
58+
59+
case EXCEPTION_DATATYPE_MISALIGNMENT:
60+
_write(_fileno(stderr), "CRASHED: SIGBUS\n", 16);
61+
_exit(0);
62+
}
63+
64+
return EXCEPTION_CONTINUE_SEARCH;
65+
}
66+
#endif
67+
4968
SWIFT_CC(swift) SWIFT_RUNTIME_LIBRARY_VISIBILITY extern "C"
5069
void installTrapInterceptor() {
5170
// Disable buffering on stdout so that everything is printed before crashing.
@@ -59,7 +78,9 @@ void installTrapInterceptor() {
5978
signal(SIGABRT, CrashCatcher);
6079
signal(SIGFPE, CrashCatcher);
6180
signal(SIGSEGV, CrashCatcher);
62-
#if !defined(_WIN32)
81+
#if defined(_WIN32)
82+
AddVectoredExceptionHandler(TRUE, VectoredCrashHandler);
83+
#else
6384
signal(SIGTRAP, CrashCatcher);
6485
signal(SIGBUS, CrashCatcher);
6586
signal(SIGSYS, CrashCatcher);

0 commit comments

Comments
 (0)