Skip to content

test: fix stdlib tests when targeting WebAssembly/WASI #39519

New issue

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

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

Already on GitHub? # to your account

Merged
merged 1 commit into from
Jan 12, 2022
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
33 changes: 33 additions & 0 deletions stdlib/private/StdlibUnittest/RaceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import SwiftPrivateThreadExtras
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import CRT
import WinSDK
Expand Down Expand Up @@ -335,6 +337,36 @@ public func evaluateObservationsAllEqual<T : Equatable>(_ observations: [T])
return .pass
}

// WebAssembly/WASI doesn't support multi-threading yet
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we not just mark the test as UNSUPPORTED on OS=wasi instead?

Copy link
Contributor Author

@MaxDesiatov MaxDesiatov Sep 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a lit test. Can any test in StdlibUnittest be marked as UNSUPPORTED?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess compnerd said we can put unsupported statements for each test case, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, not sure what you mean here, could you share an example?

#if os(WASI)
public func runRaceTest<RT : RaceTestWithPerTrialData>(
_: RT.Type,
trials: Int,
timeoutInSeconds: Int? = nil,
threads: Int? = nil
) {}
public func runRaceTest<RT : RaceTestWithPerTrialData>(
_ test: RT.Type,
operations: Int,
timeoutInSeconds: Int? = nil,
threads: Int? = nil
) {}
public func consumeCPU(units amountOfWork: Int) {}
public func runRaceTest(
trials: Int,
timeoutInSeconds: Int? = nil,
threads: Int? = nil,
invoking body: @escaping () -> Void
) {}

public func runRaceTest(
operations: Int,
timeoutInSeconds: Int? = nil,
threads: Int? = nil,
invoking body: @escaping () -> Void
) {}
#else

struct _RaceTestAggregatedEvaluations : CustomStringConvertible {
var passCount: Int = 0
var passInterestingCount = [String: Int]()
Expand Down Expand Up @@ -756,3 +788,4 @@ public func runRaceTest(
timeoutInSeconds: timeoutInSeconds, threads: threads)
}

#endif // os(WASI)
2 changes: 2 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 canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import CRT
#endif
Expand Down
4 changes: 3 additions & 1 deletion stdlib/private/StdlibUnittest/SymbolLookup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import CRT
import WinSDK
Expand All @@ -23,7 +25,7 @@

#if canImport(Darwin) || os(OpenBSD)
let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2)
#elseif os(Linux)
#elseif os(Linux) || os(WASI)
let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: 0)
#elseif os(Android)
#if arch(arm) || arch(i386)
Expand Down
2 changes: 2 additions & 0 deletions stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import SwiftPrivate
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import CRT
import WinSDK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import SwiftPrivate
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import CRT
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import CRT
import WinSDK
Expand Down Expand Up @@ -98,6 +100,9 @@ public func _stdlib_thread_create_block<Argument, Result>(
} else {
return (0, ThreadHandle(bitPattern: threadID))
}
#elseif os(WASI)
// WASI environment has a only single thread
return (0, nil)
#else
var threadID = _make_pthread_t()
let result = pthread_create(&threadID, nil,
Expand Down Expand Up @@ -129,6 +134,9 @@ public func _stdlib_thread_join<Result>(
}
}
return (CInt(result), value)
#elseif os(WASI)
// WASI environment has a only single thread
return (0, nil)
#else
var threadResultRawPtr: UnsafeMutableRawPointer?
let result = pthread_join(thread, &threadResultRawPtr)
Expand Down
19 changes: 18 additions & 1 deletion stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import CRT
import WinSDK
Expand All @@ -36,6 +38,8 @@ public struct _stdlib_thread_barrier_t {
#elseif os(Cygwin) || os(FreeBSD) || os(OpenBSD)
var mutex: UnsafeMutablePointer<pthread_mutex_t?>?
var cond: UnsafeMutablePointer<pthread_cond_t?>?
#elseif os(WASI)
// pthread is currently not available on WASI
#else
var mutex: UnsafeMutablePointer<pthread_mutex_t>?
var cond: UnsafeMutablePointer<pthread_cond_t>?
Expand Down Expand Up @@ -67,6 +71,8 @@ public func _stdlib_thread_barrier_init(

barrier.pointee.cond = UnsafeMutablePointer.allocate(capacity: 1)
InitializeConditionVariable(barrier.pointee.cond!)
#elseif os(WASI)
// WASI environment has only a single thread
#else
barrier.pointee.mutex = UnsafeMutablePointer.allocate(capacity: 1)
barrier.pointee.cond = UnsafeMutablePointer.allocate(capacity: 1)
Expand All @@ -82,7 +88,7 @@ public func _stdlib_thread_barrier_init(
return 0
}

#if !os(Windows)
#if !os(Windows) && !os(WASI)
private func _stdlib_thread_barrier_mutex_and_cond_init(_ barrier: UnsafeMutablePointer<_stdlib_thread_barrier_t>) -> CInt {
guard pthread_mutex_init(barrier.pointee.mutex!, nil) == 0 else {
return -1
Expand All @@ -101,17 +107,22 @@ public func _stdlib_thread_barrier_destroy(
#if os(Windows)
// Condition Variables do not need to be explicitly destroyed
// Mutexes do not need to be explicitly destroyed
#elseif os(WASI)
// WASI environment has only a single thread
#else
guard pthread_cond_destroy(barrier.pointee.cond!) == 0 &&
pthread_mutex_destroy(barrier.pointee.mutex!) == 0 else {
fatalError("_stdlib_thread_barrier_destroy() failed")
}
#endif

#if !os(WASI)
barrier.pointee.cond!.deinitialize(count: 1)
barrier.pointee.cond!.deallocate()

barrier.pointee.mutex!.deinitialize(count: 1)
barrier.pointee.mutex!.deallocate()
#endif

return
}
Expand All @@ -121,6 +132,8 @@ public func _stdlib_thread_barrier_wait(
) -> CInt {
#if os(Windows)
AcquireSRWLockExclusive(barrier.pointee.mutex!)
#elseif os(WASI)
// WASI environment has only a single thread
#else
if pthread_mutex_lock(barrier.pointee.mutex!) != 0 {
return -1
Expand All @@ -135,6 +148,8 @@ public func _stdlib_thread_barrier_wait(
return -1
}
ReleaseSRWLockExclusive(barrier.pointee.mutex!)
#elseif os(WASI)
// WASI environment has a only single thread
#else
if pthread_cond_wait(barrier.pointee.cond!, barrier.pointee.mutex!) != 0 {
return -1
Expand All @@ -152,6 +167,8 @@ public func _stdlib_thread_barrier_wait(
#if os(Windows)
WakeAllConditionVariable(barrier.pointee.cond!)
ReleaseSRWLockExclusive(barrier.pointee.mutex!)
#elseif os(WASI)
// WASI environment has a only single thread
#else
if pthread_cond_broadcast(barrier.pointee.cond!) != 0 {
return -1
Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/Concurrency/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ static bool isExecutingOnMainThread() {
}

return __initialPthread == GetCurrentThread();
#elif defined(__wasi__)
return true;
#else
return pthread_main_np() == 1;
#endif
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Concurrency/AsyncLet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "TaskPrivate.h"
#include "Debug.h"

#if !defined(_WIN32)
#if !defined(_WIN32) && !defined(__wasi__)
#include <dlfcn.h>
#endif

Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Concurrency/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <dispatch/dispatch.h>
#endif

#if !defined(_WIN32)
#if !defined(_WIN32) && !defined(__wasi__)
#include <dlfcn.h>
#endif

Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Concurrency/TaskGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include <dispatch/dispatch.h>
#endif

#if !defined(_WIN32)
#if !defined(_WIN32) && !defined(__wasi__)
#include <dlfcn.h>
#endif

Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/Concurrency/ThreadSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@

#include "TaskPrivate.h"

// Thread Sanitizer is not supported on Windows.
#if defined(_WIN32)
// Thread Sanitizer is not supported on Windows or WASI.
#if defined(_WIN32) || defined(__wasi__)
void swift::_swift_tsan_acquire(void *addr) {}
void swift::_swift_tsan_release(void *addr) {}
#else
Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/Differentiation/TgmathDerivatives.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import Swift
import Darwin.C.tgmath
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import CRT
#else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__wasi__)
#include <unistd.h>
#elif defined(_WIN32)
#include <io.h>
Expand Down
2 changes: 2 additions & 0 deletions test/ClangImporter/clang_builtins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import CRT
#else
Expand Down
2 changes: 2 additions & 0 deletions test/IRGen/builtin_math.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import CRT
#else
Expand Down
3 changes: 3 additions & 0 deletions test/stdlib/Character.swift
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ UnicodeScalarTests.test("UInt8(ascii: UnicodeScalar)") {
}
}

#if !os(WASI)
// Trap tests aren't available on WASI.
UnicodeScalarTests.test("UInt8(ascii: UnicodeScalar)/non-ASCII should trap")
.skip(.custom(
{ _isFastAssertConfiguration() },
Expand All @@ -378,6 +380,7 @@ UnicodeScalarTests.test("UInt8(ascii: UnicodeScalar)/non-ASCII should trap")
expectCrashLater()
_blackHole(UInt8(ascii: us))
}
#endif

UnicodeScalarTests.test("UInt32(_: UnicodeScalar),UInt64(_: UnicodeScalar)") {
for us in baseScalars {
Expand Down
3 changes: 3 additions & 0 deletions test/stdlib/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ ErrorTests.test("default domain and code") {

enum SillyError: Error { case JazzHands }

#if !os(WASI)
// Trap tests aren't available on WASI.
ErrorTests.test("try!")
.skip(.custom({ _isFastAssertConfiguration() },
reason: "trap is not guaranteed to happen in -Ounchecked"))
Expand All @@ -140,6 +142,7 @@ ErrorTests.test("try!/location")
expectCrashLater()
let _: () = try! { throw SillyError.JazzHands }()
}
#endif

ErrorTests.test("try?") {
var value = try? { () throws -> Int in return 1 }()
Expand Down
2 changes: 2 additions & 0 deletions test/stdlib/FloatConstants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif os(WASI)
import WASILibc
#elseif os(Windows)
import CRT
#else
Expand Down
Loading