Skip to content

Commit 372ada0

Browse files
authored
test: add handling for Wasm/WASI (swiftlang#39519)
This change adds support for WASI in stdlib tests. Some tests that expect a crash to happen had to be disabled, since there's currently no way to observe such crash from a WASI host.
1 parent 680698e commit 372ada0

35 files changed

+179
-13
lines changed

stdlib/private/StdlibUnittest/RaceTest.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ import SwiftPrivateThreadExtras
4343
import Darwin
4444
#elseif canImport(Glibc)
4545
import Glibc
46+
#elseif os(WASI)
47+
import WASILibc
4648
#elseif os(Windows)
4749
import CRT
4850
import WinSDK
@@ -335,6 +337,36 @@ public func evaluateObservationsAllEqual<T : Equatable>(_ observations: [T])
335337
return .pass
336338
}
337339

340+
// WebAssembly/WASI doesn't support multi-threading yet
341+
#if os(WASI)
342+
public func runRaceTest<RT : RaceTestWithPerTrialData>(
343+
_: RT.Type,
344+
trials: Int,
345+
timeoutInSeconds: Int? = nil,
346+
threads: Int? = nil
347+
) {}
348+
public func runRaceTest<RT : RaceTestWithPerTrialData>(
349+
_ test: RT.Type,
350+
operations: Int,
351+
timeoutInSeconds: Int? = nil,
352+
threads: Int? = nil
353+
) {}
354+
public func consumeCPU(units amountOfWork: Int) {}
355+
public func runRaceTest(
356+
trials: Int,
357+
timeoutInSeconds: Int? = nil,
358+
threads: Int? = nil,
359+
invoking body: @escaping () -> Void
360+
) {}
361+
362+
public func runRaceTest(
363+
operations: Int,
364+
timeoutInSeconds: Int? = nil,
365+
threads: Int? = nil,
366+
invoking body: @escaping () -> Void
367+
) {}
368+
#else
369+
338370
struct _RaceTestAggregatedEvaluations : CustomStringConvertible {
339371
var passCount: Int = 0
340372
var passInterestingCount = [String: Int]()
@@ -756,3 +788,4 @@ public func runRaceTest(
756788
timeoutInSeconds: timeoutInSeconds, threads: threads)
757789
}
758790

791+
#endif // os(WASI)

stdlib/private/StdlibUnittest/StdlibCoreExtras.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import SwiftPrivateLibcExtras
1616
import Darwin
1717
#elseif canImport(Glibc)
1818
import Glibc
19+
#elseif os(WASI)
20+
import WASILibc
1921
#elseif os(Windows)
2022
import CRT
2123
#endif

stdlib/private/StdlibUnittest/SymbolLookup.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import Darwin
1515
#elseif canImport(Glibc)
1616
import Glibc
17+
#elseif os(WASI)
18+
import WASILibc
1719
#elseif os(Windows)
1820
import CRT
1921
import WinSDK
@@ -23,7 +25,7 @@
2325

2426
#if canImport(Darwin) || os(OpenBSD)
2527
let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2)
26-
#elseif os(Linux)
28+
#elseif os(Linux) || os(WASI)
2729
let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: 0)
2830
#elseif os(Android)
2931
#if arch(arm) || arch(i386)

stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import SwiftPrivate
1515
import Darwin
1616
#elseif canImport(Glibc)
1717
import Glibc
18+
#elseif os(WASI)
19+
import WASILibc
1820
#elseif os(Windows)
1921
import CRT
2022
import WinSDK

stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import SwiftPrivate
1515
import Darwin
1616
#elseif canImport(Glibc)
1717
import Glibc
18+
#elseif os(WASI)
19+
import WASILibc
1820
#elseif os(Windows)
1921
import CRT
2022
#endif

stdlib/private/SwiftPrivateThreadExtras/SwiftPrivateThreadExtras.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import Darwin
2020
#elseif canImport(Glibc)
2121
import Glibc
22+
#elseif os(WASI)
23+
import WASILibc
2224
#elseif os(Windows)
2325
import CRT
2426
import WinSDK
@@ -98,6 +100,9 @@ public func _stdlib_thread_create_block<Argument, Result>(
98100
} else {
99101
return (0, ThreadHandle(bitPattern: threadID))
100102
}
103+
#elseif os(WASI)
104+
// WASI environment has a only single thread
105+
return (0, nil)
101106
#else
102107
var threadID = _make_pthread_t()
103108
let result = pthread_create(&threadID, nil,
@@ -129,6 +134,9 @@ public func _stdlib_thread_join<Result>(
129134
}
130135
}
131136
return (CInt(result), value)
137+
#elseif os(WASI)
138+
// WASI environment has a only single thread
139+
return (0, nil)
132140
#else
133141
var threadResultRawPtr: UnsafeMutableRawPointer?
134142
let result = pthread_join(thread, &threadResultRawPtr)

stdlib/private/SwiftPrivateThreadExtras/ThreadBarriers.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import Darwin
1515
#elseif canImport(Glibc)
1616
import Glibc
17+
#elseif os(WASI)
18+
import WASILibc
1719
#elseif os(Windows)
1820
import CRT
1921
import WinSDK
@@ -36,6 +38,8 @@ public struct _stdlib_thread_barrier_t {
3638
#elseif os(Cygwin) || os(FreeBSD) || os(OpenBSD)
3739
var mutex: UnsafeMutablePointer<pthread_mutex_t?>?
3840
var cond: UnsafeMutablePointer<pthread_cond_t?>?
41+
#elseif os(WASI)
42+
// pthread is currently not available on WASI
3943
#else
4044
var mutex: UnsafeMutablePointer<pthread_mutex_t>?
4145
var cond: UnsafeMutablePointer<pthread_cond_t>?
@@ -67,6 +71,8 @@ public func _stdlib_thread_barrier_init(
6771

6872
barrier.pointee.cond = UnsafeMutablePointer.allocate(capacity: 1)
6973
InitializeConditionVariable(barrier.pointee.cond!)
74+
#elseif os(WASI)
75+
// WASI environment has only a single thread
7076
#else
7177
barrier.pointee.mutex = UnsafeMutablePointer.allocate(capacity: 1)
7278
barrier.pointee.cond = UnsafeMutablePointer.allocate(capacity: 1)
@@ -82,7 +88,7 @@ public func _stdlib_thread_barrier_init(
8288
return 0
8389
}
8490

85-
#if !os(Windows)
91+
#if !os(Windows) && !os(WASI)
8692
private func _stdlib_thread_barrier_mutex_and_cond_init(_ barrier: UnsafeMutablePointer<_stdlib_thread_barrier_t>) -> CInt {
8793
guard pthread_mutex_init(barrier.pointee.mutex!, nil) == 0 else {
8894
return -1
@@ -101,17 +107,22 @@ public func _stdlib_thread_barrier_destroy(
101107
#if os(Windows)
102108
// Condition Variables do not need to be explicitly destroyed
103109
// Mutexes do not need to be explicitly destroyed
110+
#elseif os(WASI)
111+
// WASI environment has only a single thread
104112
#else
105113
guard pthread_cond_destroy(barrier.pointee.cond!) == 0 &&
106114
pthread_mutex_destroy(barrier.pointee.mutex!) == 0 else {
107115
fatalError("_stdlib_thread_barrier_destroy() failed")
108116
}
109117
#endif
118+
119+
#if !os(WASI)
110120
barrier.pointee.cond!.deinitialize(count: 1)
111121
barrier.pointee.cond!.deallocate()
112122

113123
barrier.pointee.mutex!.deinitialize(count: 1)
114124
barrier.pointee.mutex!.deallocate()
125+
#endif
115126

116127
return
117128
}
@@ -121,6 +132,8 @@ public func _stdlib_thread_barrier_wait(
121132
) -> CInt {
122133
#if os(Windows)
123134
AcquireSRWLockExclusive(barrier.pointee.mutex!)
135+
#elseif os(WASI)
136+
// WASI environment has only a single thread
124137
#else
125138
if pthread_mutex_lock(barrier.pointee.mutex!) != 0 {
126139
return -1
@@ -135,6 +148,8 @@ public func _stdlib_thread_barrier_wait(
135148
return -1
136149
}
137150
ReleaseSRWLockExclusive(barrier.pointee.mutex!)
151+
#elseif os(WASI)
152+
// WASI environment has a only single thread
138153
#else
139154
if pthread_cond_wait(barrier.pointee.cond!, barrier.pointee.mutex!) != 0 {
140155
return -1
@@ -152,6 +167,8 @@ public func _stdlib_thread_barrier_wait(
152167
#if os(Windows)
153168
WakeAllConditionVariable(barrier.pointee.cond!)
154169
ReleaseSRWLockExclusive(barrier.pointee.mutex!)
170+
#elseif os(WASI)
171+
// WASI environment has a only single thread
155172
#else
156173
if pthread_cond_broadcast(barrier.pointee.cond!) != 0 {
157174
return -1

stdlib/public/Concurrency/Actor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ static bool isExecutingOnMainThread() {
299299
}
300300

301301
return __initialPthread == GetCurrentThread();
302+
#elif defined(__wasi__)
303+
return true;
302304
#else
303305
return pthread_main_np() == 1;
304306
#endif

stdlib/public/Concurrency/AsyncLet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "TaskPrivate.h"
2727
#include "Debug.h"
2828

29-
#if !defined(_WIN32)
29+
#if !defined(_WIN32) && !defined(__wasi__)
3030
#include <dlfcn.h>
3131
#endif
3232

stdlib/public/Concurrency/Task.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#include <dispatch/dispatch.h>
3333
#endif
3434

35-
#if !defined(_WIN32)
35+
#if !defined(_WIN32) && !defined(__wasi__)
3636
#include <dlfcn.h>
3737
#endif
3838

stdlib/public/Concurrency/TaskGroup.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#include <dispatch/dispatch.h>
3939
#endif
4040

41-
#if !defined(_WIN32)
41+
#if !defined(_WIN32) && !defined(__wasi__)
4242
#include <dlfcn.h>
4343
#endif
4444

stdlib/public/Concurrency/ThreadSanitizer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
#include "TaskPrivate.h"
1818

19-
// Thread Sanitizer is not supported on Windows.
20-
#if defined(_WIN32)
19+
// Thread Sanitizer is not supported on Windows or WASI.
20+
#if defined(_WIN32) || defined(__wasi__)
2121
void swift::_swift_tsan_acquire(void *addr) {}
2222
void swift::_swift_tsan_release(void *addr) {}
2323
#else

stdlib/public/Differentiation/TgmathDerivatives.swift.gyb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import Swift
1818
import Darwin.C.tgmath
1919
#elseif os(Linux) || os(FreeBSD) || os(OpenBSD) || os(PS4) || os(Android) || os(Cygwin) || os(Haiku)
2020
import Glibc
21+
#elseif os(WASI)
22+
import WASILibc
2123
#elseif os(Windows)
2224
import CRT
2325
#else

stdlib/tools/swift-reflection-test/swift-reflection-test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <stdio.h>
3030
#include <stdlib.h>
3131
#include <string.h>
32-
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
32+
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) || defined(__wasi__)
3333
#include <unistd.h>
3434
#elif defined(_WIN32)
3535
#include <io.h>

test/ClangImporter/clang_builtins.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import Darwin
55
#elseif canImport(Glibc)
66
import Glibc
7+
#elseif os(WASI)
8+
import WASILibc
79
#elseif os(Windows)
810
import CRT
911
#else

test/IRGen/builtin_math.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import Darwin
55
#elseif canImport(Glibc)
66
import Glibc
7+
#elseif os(WASI)
8+
import WASILibc
79
#elseif os(Windows)
810
import CRT
911
#else

test/stdlib/Character.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ UnicodeScalarTests.test("UInt8(ascii: UnicodeScalar)") {
358358
}
359359
}
360360

361+
#if !os(WASI)
362+
// Trap tests aren't available on WASI.
361363
UnicodeScalarTests.test("UInt8(ascii: UnicodeScalar)/non-ASCII should trap")
362364
.skip(.custom(
363365
{ _isFastAssertConfiguration() },
@@ -367,6 +369,7 @@ UnicodeScalarTests.test("UInt8(ascii: UnicodeScalar)/non-ASCII should trap")
367369
expectCrashLater()
368370
_blackHole(UInt8(ascii: us))
369371
}
372+
#endif
370373

371374
UnicodeScalarTests.test("UInt32(_: UnicodeScalar),UInt64(_: UnicodeScalar)") {
372375
for us in baseScalars {

test/stdlib/Error.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ ErrorTests.test("default domain and code") {
118118

119119
enum SillyError: Error { case JazzHands }
120120

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

144147
ErrorTests.test("try?") {
145148
var value = try? { () throws -> Int in return 1 }()

test/stdlib/FloatConstants.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import Darwin
55
#elseif canImport(Glibc)
66
import Glibc
7+
#elseif os(WASI)
8+
import WASILibc
79
#elseif os(Windows)
810
import CRT
911
#else

0 commit comments

Comments
 (0)