Skip to content

Commit 5fc3ad3

Browse files
committed
Fix a fatal error not-terminating issue in Windows.
Fix the issue that fatal errors in certain cases don't terminate the process and the process keeps running in Windows by disabling the exception swallowing that supressed the illegal instruction exceptions coming from llvm.trap.
1 parent 802e63a commit 5fc3ad3

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

stdlib/public/runtime/Errors.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,3 +471,18 @@ void swift::swift_abortDisabledUnicodeSupport() {
471471
"Unicode normalization data is disabled on this platform");
472472

473473
}
474+
475+
#if defined(_WIN32)
476+
// On Windows, exceptions may be swallowed in some cases and the
477+
// process may not terminate as expected on crashes. For example,
478+
// illegal instructions used by llvm.trap. Disable the exception
479+
// swallowing so that the error handling works as expected.
480+
__attribute__((__constructor__))
481+
static void ConfigureExceptionPolicy() {
482+
BOOL Suppress = FALSE;
483+
SetUserObjectInformationA(GetCurrentProcess(),
484+
UOI_TIMERPROC_EXCEPTION_SUPPRESSION,
485+
&Suppress, sizeof(Suppress));
486+
}
487+
488+
#endif
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift -module-name=main %s -o %t/a.out
3+
// RUN: %target-codesign %t/a.out
4+
// RUN: not %target-run %t/a.out 2>&1 | %FileCheck %s
5+
// REQUIRES: executable_test
6+
// REQUIRES: OS=windows-msvc
7+
8+
// Check that a fatal error terminates the process and it appears only once.
9+
// CHECK: Fatal error
10+
// CHECK-NOT: Fatal error
11+
12+
import WinSDK
13+
14+
private var timerID: UINT_PTR = 1
15+
let doWork: TIMERPROC = { (_: HWND?, _: UINT, _: UINT_PTR, _: DWORD) in
16+
fatalError("oops")
17+
timerID = SetTimer(nil, timerID, UInt32(0), doWork)
18+
}
19+
var msg: MSG = .init()
20+
timerID = SetTimer(nil, timerID, UInt32(0), doWork)
21+
while (GetMessageA(&msg, nil, 0, 0)) {
22+
TranslateMessage(&msg);
23+
DispatchMessageA(&msg);
24+
}

0 commit comments

Comments
 (0)