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.
This commit is contained in:
Hiroshi Yamauchi
2023-08-25 10:25:47 -07:00
parent 802e63a778
commit 5fc3ad3a81
2 changed files with 39 additions and 0 deletions

View File

@@ -471,3 +471,18 @@ void swift::swift_abortDisabledUnicodeSupport() {
"Unicode normalization data is disabled on this platform");
}
#if defined(_WIN32)
// On Windows, exceptions may be swallowed in some cases and the
// process may not terminate as expected on crashes. For example,
// illegal instructions used by llvm.trap. Disable the exception
// swallowing so that the error handling works as expected.
__attribute__((__constructor__))
static void ConfigureExceptionPolicy() {
BOOL Suppress = FALSE;
SetUserObjectInformationA(GetCurrentProcess(),
UOI_TIMERPROC_EXCEPTION_SUPPRESSION,
&Suppress, sizeof(Suppress));
}
#endif

View File

@@ -0,0 +1,24 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -module-name=main %s -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: not %target-run %t/a.out 2>&1 | %FileCheck %s
// REQUIRES: executable_test
// REQUIRES: OS=windows-msvc
// Check that a fatal error terminates the process and it appears only once.
// CHECK: Fatal error
// CHECK-NOT: Fatal error
import WinSDK
private var timerID: UINT_PTR = 1
let doWork: TIMERPROC = { (_: HWND?, _: UINT, _: UINT_PTR, _: DWORD) in
fatalError("oops")
timerID = SetTimer(nil, timerID, UInt32(0), doWork)
}
var msg: MSG = .init()
timerID = SetTimer(nil, timerID, UInt32(0), doWork)
while (GetMessageA(&msg, nil, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageA(&msg);
}