Skip to content

StdlibUnittest: port the test harness to Windows #21356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions stdlib/private/StdlibUnittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ if (NOT IS_BUILD_TYPE_OPTIMIZED)
list(APPEND swift_stdlib_unittest_compile_flags "-DSWIFT_STDLIB_DEBUG")
endif()

# TODO: support this on non-POSIX platforms. It cannot be currently as it
# depends on pthreads.
add_swift_target_library(swiftStdlibUnittest ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STDLIB
# This file should be listed the first. Module name is inferred from the
# filename.
Expand Down Expand Up @@ -42,7 +40,8 @@ add_swift_target_library(swiftStdlibUnittest ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES}
SWIFT_MODULE_DEPENDS_FREEBSD Glibc
SWIFT_MODULE_DEPENDS_CYGWIN Glibc
SWIFT_MODULE_DEPENDS_HAIKU Glibc
SWIFT_MODULE_DEPENDS_WINDOWS MSVCRT WinSDK
SWIFT_COMPILE_FLAGS ${swift_stdlib_unittest_compile_flags}
TARGET_SDKS ALL_POSIX_PLATFORMS
INSTALL_IN_COMPONENT stdlib-experimental)
set_source_files_properties(InspectValue.cpp PROPERTIES COMPILE_FLAGS -std=c++14)

48 changes: 44 additions & 4 deletions stdlib/private/StdlibUnittest/InterceptTraps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,77 @@
#include <stdio.h>
#include <signal.h>
#include <string.h>
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#include <unistd.h>
#endif
#if defined(_WIN32)
#include <io.h>
#include <process.h>
#include <stdlib.h>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#endif

#include "swift/Runtime/Config.h"

static void CrashCatcher(int Sig) {
const char *Msg;
switch (Sig) {
case SIGILL: Msg = "CRASHED: SIGILL\n"; break;
case SIGTRAP: Msg = "CRASHED: SIGTRAP\n"; break;
case SIGABRT: Msg = "CRASHED: SIGABRT\n"; break;
case SIGFPE: Msg = "CRASHED: SIGFPE\n"; break;
case SIGBUS: Msg = "CRASHED: SIGBUS\n"; break;
case SIGSEGV: Msg = "CRASHED: SIGSEGV\n"; break;
#if !defined(_WIN32)
case SIGTRAP: Msg = "CRASHED: SIGTRAP\n"; break;
case SIGBUS: Msg = "CRASHED: SIGBUS\n"; break;
case SIGSYS: Msg = "CRASHED: SIGSYS\n"; break;
#endif
default: Msg = "CRASHED: SIG????\n"; break;
}
#if defined(_WIN32)
_write(_fileno(stderr), Msg, strlen(Msg));
#else
write(STDERR_FILENO, Msg, strlen(Msg));
#endif
_exit(0);
}

#if defined(_WIN32)
static LONG WINAPI
VectoredCrashHandler(PEXCEPTION_POINTERS ExceptionInfo) {
switch (ExceptionInfo->ExceptionRecord->ExceptionCode) {
case EXCEPTION_ILLEGAL_INSTRUCTION:
_write(_fileno(stderr), "CRASHED: SIGTRAP\n", 17);
_exit(0);

case EXCEPTION_DATATYPE_MISALIGNMENT:
_write(_fileno(stderr), "CRASHED: SIGBUS\n", 16);
_exit(0);
}

return EXCEPTION_CONTINUE_SEARCH;
}
#endif

SWIFT_CC(swift) SWIFT_RUNTIME_LIBRARY_VISIBILITY extern "C"
void installTrapInterceptor() {
// Disable buffering on stdout so that everything is printed before crashing.
setbuf(stdout, 0);

#if defined(_WIN32)
_set_abort_behavior(1, _WRITE_ABORT_MSG);
#endif

signal(SIGILL, CrashCatcher);
signal(SIGTRAP, CrashCatcher);
signal(SIGABRT, CrashCatcher);
signal(SIGFPE, CrashCatcher);
signal(SIGBUS, CrashCatcher);
signal(SIGSEGV, CrashCatcher);
#if defined(_WIN32)
AddVectoredExceptionHandler(TRUE, VectoredCrashHandler);
#else
signal(SIGTRAP, CrashCatcher);
signal(SIGBUS, CrashCatcher);
signal(SIGSYS, CrashCatcher);
#endif
}

Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public func getFloat32(_ x: Float32) -> Float32 { return _opaqueIdentity(x) }
@inline(never)
public func getFloat64(_ x: Float64) -> Float64 { return _opaqueIdentity(x) }

#if arch(i386) || arch(x86_64)
#if !os(Windows) && (arch(i386) || arch(x86_64))
@inline(never)
public func getFloat80(_ x: Float80) -> Float80 { return _opaqueIdentity(x) }
#endif
Expand Down
44 changes: 42 additions & 2 deletions stdlib/private/StdlibUnittest/RaceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ import SwiftPrivateThreadExtras
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
import Glibc
#elseif os(Windows)
import MSVCRT
import WinSDK
#endif

#if _runtime(_ObjC)
Expand Down Expand Up @@ -508,6 +511,36 @@ func _workerThreadOneTrial<RT>(
}

/// One-shot sleep in one thread, allowing interrupt by another.
#if os(Windows)
class _InterruptibleSleep {
let event: HANDLE?
var completed: Bool = false

init() {
self.event = CreateEventW(nil, TRUE, FALSE, nil)
precondition(self.event != nil)
}

deinit {
CloseHandle(self.event)
}

func sleep(durationInSeconds duration: Int) {
guard completed == false else { return }

let result: DWORD = WaitForSingleObject(event, DWORD(duration * 1000))
precondition(result == WAIT_OBJECT_0)

completed = true
}

func wake() {
guard completed == false else { return }
let result: BOOL = SetEvent(self.event)
precondition(result == TRUE)
}
}
#else
class _InterruptibleSleep {
let writeEnd: CInt
let readEnd: CInt
Expand Down Expand Up @@ -550,6 +583,13 @@ class _InterruptibleSleep {
precondition(ret >= 0)
}
}
#endif

#if os(Windows)
typealias ThreadHandle = HANDLE
#else
typealias ThreadHandle = pthread_t
#endif

public func runRaceTest<RT : RaceTestWithPerTrialData>(
_: RT.Type,
Expand Down Expand Up @@ -600,8 +640,8 @@ public func runRaceTest<RT : RaceTestWithPerTrialData>(
_ = timeoutReached.fetchAndAdd(1)
}

var testTids = [pthread_t]()
var alarmTid: pthread_t
var testTids = [ThreadHandle]()
var alarmTid: ThreadHandle

// Create the master thread.
do {
Expand Down
4 changes: 4 additions & 0 deletions stdlib/private/StdlibUnittest/StdlibCoreExtras.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import SwiftPrivateLibcExtras
import Darwin
#elseif os(Linux) || os(FreeBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
import Glibc
#elseif os(Windows)
import MSVCRT
#endif

#if _runtime(_ObjC)
Expand Down Expand Up @@ -73,6 +75,7 @@ func findSubstring(_ string: String, _ substring: String) -> String.Index? {
#endif
}

#if !os(Windows)
public func createTemporaryFile(
_ fileNamePrefix: String, _ fileNameSuffix: String, _ contents: String
) -> String {
Expand All @@ -95,6 +98,7 @@ public func createTemporaryFile(
}
return fileName
}
#endif

public final class Box<T> {
public init(_ value: T) { self.value = value }
Expand Down
Loading