diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index d543be2..ec43b5b 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -9,7 +9,10 @@ jobs: name: Test uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main with: - linux_exclude_swift_versions: '[{"swift_version": "5.8"}]' + linux_os_versions: '["jammy", "focal"]' + enable_macos_checks: false + macos_xcode_versions: '["16.3"]' + soundness: name: Soundness uses: swiftlang/github-workflows/.github/workflows/soundness.yml@main diff --git a/Sources/CSystem/include/CSystemWASI.h b/Sources/CSystem/include/CSystemWASI.h index 9877853..1c8cd0f 100644 --- a/Sources/CSystem/include/CSystemWASI.h +++ b/Sources/CSystem/include/CSystemWASI.h @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2024 Apple Inc. and the Swift System project authors + Copyright (c) 2024 - 2025 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -11,8 +11,10 @@ #if __wasi__ +#include #include #include +#include // For NAME_MAX // wasi-libc defines the following constants in a way that Clang Importer can't // understand, so we need to expose them manually. @@ -28,4 +30,37 @@ static inline int32_t _getConst_O_WRONLY(void) { return O_WRONLY; } static inline int32_t _getConst_EWOULDBLOCK(void) { return EWOULDBLOCK; } static inline int32_t _getConst_EOPNOTSUPP(void) { return EOPNOTSUPP; } +static inline uint8_t _getConst_DT_DIR(void) { return DT_DIR; } + +// Modified dirent struct that can be imported to Swift +struct _system_dirent { + ino_t d_ino; + unsigned char d_type; + // char d_name[] cannot be imported to Swift + char d_name[NAME_MAX + 1]; +}; + +// Convert WASI dirent with d_name[] to _system_dirent +static inline +struct _system_dirent * +_system_dirent_from_wasi_dirent(const struct dirent *wasi_dirent) { + + // Match readdir behavior and use thread-local storage for the converted dirent + static __thread struct _system_dirent _converted_dirent; + + if (wasi_dirent == NULL) { + return NULL; + } + + memset(&_converted_dirent, 0, sizeof(struct _system_dirent)); + + _converted_dirent.d_ino = wasi_dirent->d_ino; + _converted_dirent.d_type = wasi_dirent->d_type; + + strncpy(_converted_dirent.d_name, wasi_dirent->d_name, NAME_MAX); + _converted_dirent.d_name[NAME_MAX] = '\0'; + + return &_converted_dirent; +} + #endif diff --git a/Sources/CSystem/include/io_uring.h b/Sources/CSystem/include/io_uring.h index 0068f50..d8287b1 100644 --- a/Sources/CSystem/include/io_uring.h +++ b/Sources/CSystem/include/io_uring.h @@ -1,10 +1,195 @@ #include #include #include - #include + +#define __SWIFT_IORING_SQE_FALLBACK_STRUCT { \ + __u8 opcode; \ + __u8 flags; \ + __u16 ioprio; \ + __s32 fd; \ + union { \ + __u64 off; \ + __u64 addr2; \ + struct { \ + __u32 cmd_op; \ + __u32 __pad1; \ + }; \ + }; \ + union { \ + __u64 addr; \ + __u64 splice_off_in; \ + struct { \ + __u32 level; \ + __u32 optname; \ + }; \ + }; \ + __u32 len; \ + union { \ + __kernel_rwf_t rw_flags; \ + __u32 fsync_flags; \ + __u16 poll_events; \ + __u32 poll32_events; \ + __u32 sync_range_flags; \ + __u32 msg_flags; \ + __u32 timeout_flags; \ + __u32 accept_flags; \ + __u32 cancel_flags; \ + __u32 open_flags; \ + __u32 statx_flags; \ + __u32 fadvise_advice; \ + __u32 splice_flags; \ + __u32 rename_flags; \ + __u32 unlink_flags; \ + __u32 hardlink_flags; \ + __u32 xattr_flags; \ + __u32 msg_ring_flags; \ + __u32 uring_cmd_flags; \ + __u32 waitid_flags; \ + __u32 futex_flags; \ + __u32 install_fd_flags; \ + __u32 nop_flags; \ + }; \ + __u64 user_data; \ + union { \ + __u16 buf_index; \ + __u16 buf_group; \ + } __attribute__((packed)); \ + __u16 personality; \ + union { \ + __s32 splice_fd_in; \ + __u32 file_index; \ + __u32 optlen; \ + struct { \ + __u16 addr_len; \ + __u16 __pad3[1]; \ + }; \ + }; \ + union { \ + struct { \ + __u64 addr3; \ + __u64 __pad2[1]; \ + }; \ + __u64 optval; \ + __u8 cmd[0]; \ + }; \ +} + +#if __has_include() #include +#ifdef IORING_TIMEOUT_BOOTTIME +// Kernel version >= 5.15, io_uring_sqe has file_index +// and all current Swift operations are supported. +#define __SWIFT_IORING_SUPPORTED true +typedef struct io_uring_sqe swift_io_uring_sqe; +#else +// io_uring_sqe is missing properties that IORequest expects. +// This configuration is not supported for now. +// +// Define a fallback struct to avoid build errors, but IORing +// will throw ENOTSUP on initialization. +#define __SWIFT_IORING_SUPPORTED false +typedef struct __SWIFT_IORING_SQE_FALLBACK_STRUCT swift_io_uring_sqe; +#endif + +// We can define more specific availability later + +#ifdef IORING_FEAT_RW_CUR_POS +// Kernel version >= 5.6, io_uring_sqe has open_flags +#endif + +#ifdef IORING_FEAT_NODROP +// Kernel version >= 5.5, io_uring_sqe has cancel_flags +#endif + +#else +// Minimal fallback definitions when linux/io_uring.h is not available (e.g. static SDK) +#include + +#define __SWIFT_IORING_SUPPORTED false + +#define IORING_OFF_SQ_RING 0ULL +#define IORING_OFF_CQ_RING 0x8000000ULL +#define IORING_OFF_SQES 0x10000000ULL + +#define IORING_ENTER_GETEVENTS (1U << 0) + +#define IORING_FEAT_SINGLE_MMAP (1U << 0) +#define IORING_FEAT_NODROP (1U << 1) +#define IORING_FEAT_SUBMIT_STABLE (1U << 2) +#define IORING_FEAT_RW_CUR_POS (1U << 3) +#define IORING_FEAT_CUR_PERSONALITY (1U << 4) +#define IORING_FEAT_FAST_POLL (1U << 5) +#define IORING_FEAT_POLL_32BITS (1U << 6) +#define IORING_FEAT_SQPOLL_NONFIXED (1U << 7) +#define IORING_FEAT_EXT_ARG (1U << 8) +#define IORING_FEAT_NATIVE_WORKERS (1U << 9) +#define IORING_FEAT_RSRC_TAGS (1U << 10) +#define IORING_FEAT_CQE_SKIP (1U << 11) +#define IORING_FEAT_LINKED_FILE (1U << 12) +#define IORING_FEAT_REG_REG_RING (1U << 13) +#define IORING_FEAT_RECVSEND_BUNDLE (1U << 14) +#define IORING_FEAT_MIN_TIMEOUT (1U << 15) +#define IORING_FEAT_RW_ATTR (1U << 16) +#define IORING_FEAT_NO_IOWAIT (1U << 17) + +typedef uint8_t __u8; +typedef uint16_t __u16; +typedef uint32_t __u32; +typedef uint64_t __u64; +typedef int32_t __s32; + +#ifndef __kernel_rwf_t +typedef int __kernel_rwf_t; +#endif + +typedef struct __SWIFT_IORING_SQE_FALLBACK_STRUCT swift_io_uring_sqe; + +struct io_uring_cqe { + __u64 user_data; + __s32 res; + __u32 flags; +}; + +struct io_sqring_offsets { + __u32 head; + __u32 tail; + __u32 ring_mask; + __u32 ring_entries; + __u32 flags; + __u32 dropped; + __u32 array; + __u32 resv1; + __u64 user_addr; +}; + +struct io_cqring_offsets { + __u32 head; + __u32 tail; + __u32 ring_mask; + __u32 ring_entries; + __u32 overflow; + __u32 cqes; + __u32 flags; + __u32 resv1; + __u64 user_addr; +}; + +struct io_uring_params { + __u32 sq_entries; + __u32 cq_entries; + __u32 flags; + __u32 sq_thread_cpu; + __u32 sq_thread_idle; + __u32 features; + __u32 wq_fd; + __u32 resv[3]; + struct io_sqring_offsets sq_off; + struct io_cqring_offsets cq_off; +}; +#endif // __has_include() + #ifndef SWIFT_IORING_C_WRAPPER #define SWIFT_IORING_C_WRAPPER diff --git a/Sources/System/FileOperations.swift b/Sources/System/FileOperations.swift index fc9fc93..2a8509b 100644 --- a/Sources/System/FileOperations.swift +++ b/Sources/System/FileOperations.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2025 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -509,6 +509,7 @@ extension FileDescriptor { } } +#if !os(WASI) // WASI has no umask extension FilePermissions { /// The file creation permission mask (aka "umask"). /// @@ -549,3 +550,4 @@ extension FilePermissions { return system_umask(mode) } } +#endif diff --git a/Sources/System/IORing/IORing.swift b/Sources/System/IORing/IORing.swift index c676f78..3e54c78 100644 --- a/Sources/System/IORing/IORing.swift +++ b/Sources/System/IORing/IORing.swift @@ -10,6 +10,10 @@ import Musl #endif import Synchronization +private var ioringSupported: Bool { + __SWIFT_IORING_SUPPORTED != 0 +} + //This was #defines in older headers, so we redeclare it to get a consistent import internal enum RegistrationOps: UInt32 { case registerBuffers = 0 @@ -72,7 +76,7 @@ extension UnsafeMutableRawBufferPointer { @inline(__always) @inlinable internal func _tryWriteRequest( _ request: __owned RawIORequest, ring: inout SQRing, - submissionQueueEntries: UnsafeMutableBufferPointer + submissionQueueEntries: UnsafeMutableBufferPointer ) -> Bool { @@ -151,9 +155,9 @@ internal func _flushQueue(ring: borrowing SQRing) -> UInt32 { @inlinable internal func _getSubmissionEntry( - ring: inout SQRing, submissionQueueEntries: UnsafeMutableBufferPointer + ring: inout SQRing, submissionQueueEntries: UnsafeMutableBufferPointer ) -> UnsafeMutablePointer< - io_uring_sqe + swift_io_uring_sqe >? { let next = ring.userTail &+ 1 //this is expected to wrap @@ -196,7 +200,7 @@ private func setUpRing( throw err } - if params.features & IORING_FEAT_NODROP == 0 + if params.features & IORing.Features.nonDroppingCompletions.rawValue == 0 { close(ringDescriptor) throw Errno.invalidArgument @@ -223,7 +227,7 @@ private func setUpRing( /* prot: */ PROT_READ | PROT_WRITE, /* flags: */ MAP_SHARED | MAP_POPULATE, /* fd: */ ringDescriptor, - /* offset: */ __off_t(IORING_OFF_SQ_RING) + /* offset: */ off_t(IORING_OFF_SQ_RING) ) if ringPtr == MAP_FAILED { @@ -238,7 +242,7 @@ private func setUpRing( /* prot: */ PROT_READ | PROT_WRITE, /* flags: */ MAP_SHARED | MAP_POPULATE, /* fd: */ ringDescriptor, - /* offset: */ __off_t(IORING_OFF_SQ_RING) + /* offset: */ off_t(IORING_OFF_SQ_RING) ) if sqPtr == MAP_FAILED { @@ -253,7 +257,7 @@ private func setUpRing( /* prot: */ PROT_READ | PROT_WRITE, /* flags: */ MAP_SHARED | MAP_POPULATE, /* fd: */ ringDescriptor, - /* offset: */ __off_t(IORING_OFF_CQ_RING) + /* offset: */ off_t(IORING_OFF_CQ_RING) ) if cqPtr == MAP_FAILED { @@ -266,11 +270,11 @@ private func setUpRing( // map the submission queue let sqes = mmap( /* addr: */ nil, - /* len: */ Int(params.sq_entries) * MemoryLayout.size, + /* len: */ Int(params.sq_entries) * MemoryLayout.size, /* prot: */ PROT_READ | PROT_WRITE, /* flags: */ MAP_SHARED | MAP_POPULATE, /* fd: */ ringDescriptor, - /* offset: */ __off_t(IORING_OFF_SQES) + /* offset: */ off_t(IORING_OFF_SQES) ) if sqes == MAP_FAILED { @@ -307,7 +311,7 @@ public struct IORing: ~Copyable { @usableFromInline let completionRing: CQRing - @usableFromInline let submissionQueueEntries: UnsafeMutableBufferPointer + @usableFromInline let submissionQueueEntries: UnsafeMutableBufferPointer // kept around for unmap / cleanup. TODO: we can save a few words of memory by figuring out how to handle cleanup for non-IORING_FEAT_SINGLE_MMAP better let ringSize: Int @@ -366,76 +370,90 @@ public struct IORing: ~Copyable { /// Initializes an IORing with enough space for `queueDepth` prepared requests and completed operations public init(queueDepth: UInt32, flags: SetupFlags = []) throws(Errno) { + guard ioringSupported else { + throw Errno.notSupported + } + let (params, tmpRingDescriptor, tmpRingPtr, tmpRingSize, tmpSQPtr, tmpSQSize, tmpCQPtr, tmpCQSize, sqes) = try setUpRing(queueDepth: queueDepth, flags: flags) // All throws need to be before initializing ivars here to avoid // "error: conditional initialization or destruction of noncopyable types is not supported; // this variable must be consistently in an initialized or uninitialized state through every code path" - features = Features(rawValue: params.features) - ringDescriptor = tmpRingDescriptor - ringPtr = tmpRingPtr - ringSize = tmpRingSize - submissionRingPtr = tmpSQPtr - submissionRingSize = tmpSQSize - completionRingPtr = tmpCQPtr - completionRingSize = tmpCQSize - - _registeredFiles = [] - _registeredBuffers = [] - submissionRing = SQRing( + + // Pre-compute values to avoid accessing partially initialized state + let ringBasePtr = tmpRingPtr ?? tmpSQPtr! + let completionBasePtr = tmpRingPtr ?? tmpCQPtr! + + let submissionRing = SQRing( kernelHead: UnsafePointer>( - (ringPtr ?? submissionRingPtr!).advanced(by: params.sq_off.head) + ringBasePtr.advanced(by: params.sq_off.head) .assumingMemoryBound(to: Atomic.self) ), kernelTail: UnsafePointer>( - (ringPtr ?? submissionRingPtr!).advanced(by: params.sq_off.tail) + ringBasePtr.advanced(by: params.sq_off.tail) .assumingMemoryBound(to: Atomic.self) ), userTail: 0, // no requests yet - ringMask: (ringPtr ?? submissionRingPtr!).advanced(by: params.sq_off.ring_mask) + ringMask: ringBasePtr.advanced(by: params.sq_off.ring_mask) .assumingMemoryBound(to: UInt32.self).pointee, flags: UnsafePointer>( - (ringPtr ?? submissionRingPtr!).advanced(by: params.sq_off.flags) + ringBasePtr.advanced(by: params.sq_off.flags) .assumingMemoryBound(to: Atomic.self) ), array: UnsafeMutableBufferPointer( - start: (ringPtr ?? submissionRingPtr!).advanced(by: params.sq_off.array) + start: ringBasePtr.advanced(by: params.sq_off.array) .assumingMemoryBound(to: UInt32.self), count: Int( - (ringPtr ?? submissionRingPtr!).advanced(by: params.sq_off.ring_entries) + ringBasePtr.advanced(by: params.sq_off.ring_entries) .assumingMemoryBound(to: UInt32.self).pointee) ) ) - - // fill submission ring array with 1:1 map to underlying SQEs - for i in 0 ..< submissionRing.array.count { - submissionRing.array[i] = UInt32(i) - } - - submissionQueueEntries = UnsafeMutableBufferPointer( - start: sqes.assumingMemoryBound(to: io_uring_sqe.self), - count: Int(params.sq_entries) - ) - - completionRing = CQRing( + + let completionRing = CQRing( kernelHead: UnsafePointer>( - (ringPtr ?? completionRingPtr!).advanced(by: params.cq_off.head) + completionBasePtr.advanced(by: params.cq_off.head) .assumingMemoryBound(to: Atomic.self) ), kernelTail: UnsafePointer>( - (ringPtr ?? completionRingPtr!).advanced(by: params.cq_off.tail) + completionBasePtr.advanced(by: params.cq_off.tail) .assumingMemoryBound(to: Atomic.self) ), - ringMask: (ringPtr ?? completionRingPtr!).advanced(by: params.cq_off.ring_mask) + ringMask: completionBasePtr.advanced(by: params.cq_off.ring_mask) .assumingMemoryBound(to: UInt32.self).pointee, cqes: UnsafeBufferPointer( - start: (ringPtr ?? completionRingPtr!).advanced(by: params.cq_off.cqes) + start: completionBasePtr.advanced(by: params.cq_off.cqes) .assumingMemoryBound(to: io_uring_cqe.self), count: Int( - (ringPtr ?? completionRingPtr!).advanced(by: params.cq_off.ring_entries) + completionBasePtr.advanced(by: params.cq_off.ring_entries) .assumingMemoryBound(to: UInt32.self).pointee) ) ) + + let submissionQueueEntries = UnsafeMutableBufferPointer( + start: sqes.assumingMemoryBound(to: swift_io_uring_sqe.self), + count: Int(params.sq_entries) + ) + + // Now initialize all stored properties + self.features = Features(rawValue: params.features) + self.ringDescriptor = tmpRingDescriptor + self.ringPtr = tmpRingPtr + self.ringSize = tmpRingSize + self.submissionRingPtr = tmpSQPtr + self.submissionRingSize = tmpSQSize + self.completionRingPtr = tmpCQPtr + self.completionRingSize = tmpCQSize + self._registeredFiles = [] + self._registeredBuffers = [] + self.submissionRing = submissionRing + self.completionRing = completionRing + self.submissionQueueEntries = submissionQueueEntries self.ringFlags = params.flags + + // fill submission ring array with 1:1 map to underlying SQEs + // (happens after all properties are initialized) + for i in 0 ..< self.submissionRing.array.count { + self.submissionRing.array[i] = UInt32(i) + } } @inlinable @@ -858,7 +876,7 @@ public struct IORing: ~Copyable { } munmap( UnsafeMutableRawPointer(submissionQueueEntries.baseAddress!), - submissionQueueEntries.count * MemoryLayout.size + submissionQueueEntries.count * MemoryLayout.size ) close(ringDescriptor) } diff --git a/Sources/System/IORing/RawIORequest.swift b/Sources/System/IORing/RawIORequest.swift index dea378f..4958a48 100644 --- a/Sources/System/IORing/RawIORequest.swift +++ b/Sources/System/IORing/RawIORequest.swift @@ -5,11 +5,13 @@ import CSystem @usableFromInline internal struct RawIORequest: ~Copyable { - @usableFromInline var rawValue: io_uring_sqe + // swift_io_uring_sqe is a typedef of io_uring_sqe on platforms where + // IORing is supported (currently requires kernel version >= 5.15). + @usableFromInline var rawValue: swift_io_uring_sqe @usableFromInline var path: FilePath? //buffer owner for the path pointer that the sqe may have @inlinable public init() { - self.rawValue = io_uring_sqe() + self.rawValue = swift_io_uring_sqe() } } @@ -176,8 +178,8 @@ extension RawIORequest { @inlinable static func withTimeoutRequest( - linkedTo opEntry: UnsafeMutablePointer, - in timeoutEntry: UnsafeMutablePointer, + linkedTo opEntry: UnsafeMutablePointer, + in timeoutEntry: UnsafeMutablePointer, duration: Duration, flags: TimeOutFlags, work: () throws -> R) rethrows -> R { diff --git a/Sources/System/Internals/Syscalls.swift b/Sources/System/Internals/Syscalls.swift index 1627273..f6eb533 100644 --- a/Sources/System/Internals/Syscalls.swift +++ b/Sources/System/Internals/Syscalls.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 - 2024 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2025 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -14,6 +14,7 @@ import Glibc #elseif canImport(Musl) import Musl #elseif canImport(WASILibc) +import CSystem import WASILibc #elseif os(Windows) import ucrt @@ -189,9 +190,14 @@ internal func system_confstr( #if !os(Windows) internal let SYSTEM_AT_REMOVE_DIR = AT_REMOVEDIR +#if os(WASI) +internal let SYSTEM_DT_DIR = _getConst_DT_DIR() +internal typealias system_dirent = _system_dirent +#else internal let SYSTEM_DT_DIR = DT_DIR internal typealias system_dirent = dirent -#if os(Linux) || os(Android) || os(FreeBSD) || os(OpenBSD) +#endif +#if os(Linux) || os(Android) || os(FreeBSD) || os(OpenBSD) || os(WASI) internal typealias system_DIRPtr = OpaquePointer #else internal typealias system_DIRPtr = UnsafeMutablePointer @@ -216,8 +222,12 @@ internal func system_fdopendir( internal func system_readdir( _ dir: system_DIRPtr -) -> UnsafeMutablePointer? { +) -> UnsafeMutablePointer? { + #if os(WASI) + return _system_dirent_from_wasi_dirent(readdir(dir)) + #else return readdir(dir) + #endif } internal func system_rewinddir( @@ -246,11 +256,13 @@ internal func system_openat( } #endif +#if !os(WASI) // WASI has no umask internal func system_umask( _ mode: CInterop.Mode ) -> CInterop.Mode { return umask(mode) } +#endif internal func system_getenv( _ name: UnsafePointer diff --git a/Tests/SystemTests/ErrnoTest.swift b/Tests/SystemTests/ErrnoTest.swift index da09657..5f4551b 100644 --- a/Tests/SystemTests/ErrnoTest.swift +++ b/Tests/SystemTests/ErrnoTest.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2025 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -36,7 +36,7 @@ final class ErrnoTest: XCTestCase { XCTAssert(ENOMEM == Errno.noMemory.rawValue) XCTAssert(EACCES == Errno.permissionDenied.rawValue) XCTAssert(EFAULT == Errno.badAddress.rawValue) -#if !os(Windows) +#if !os(Windows) && !os(WASI) XCTAssert(ENOTBLK == Errno.notBlockDevice.rawValue) #endif XCTAssert(EBUSY == Errno.resourceBusy.rawValue) @@ -74,9 +74,11 @@ final class ErrnoTest: XCTestCase { XCTAssert(WSAEOPNOTSUPP == Errno.notSupported.rawValue) XCTAssert(WSAEPFNOSUPPORT == Errno.protocolFamilyNotSupported.rawValue) #else - XCTAssert(ESOCKTNOSUPPORT == Errno.socketTypeNotSupported.rawValue) XCTAssert(ENOTSUP == Errno.notSupported.rawValue) +#if !os(WASI) + XCTAssert(ESOCKTNOSUPPORT == Errno.socketTypeNotSupported.rawValue) XCTAssert(EPFNOSUPPORT == Errno.protocolFamilyNotSupported.rawValue) +#endif #endif XCTAssert(EAFNOSUPPORT == Errno.addressFamilyNotSupported.rawValue) XCTAssert(EADDRINUSE == Errno.addressInUse.rawValue) @@ -91,7 +93,7 @@ final class ErrnoTest: XCTestCase { XCTAssert(ENOTCONN == Errno.socketNotConnected.rawValue) #if os(Windows) XCTAssert(WSAESHUTDOWN == Errno.socketShutdown.rawValue) -#else +#elseif !os(WASI) XCTAssert(ESHUTDOWN == Errno.socketShutdown.rawValue) #endif XCTAssert(ETIMEDOUT == Errno.timedOut.rawValue) @@ -100,7 +102,7 @@ final class ErrnoTest: XCTestCase { XCTAssert(ENAMETOOLONG == Errno.fileNameTooLong.rawValue) #if os(Windows) XCTAssert(WSAEHOSTDOWN == Errno.hostIsDown.rawValue) -#else +#elseif !os(WASI) XCTAssert(EHOSTDOWN == Errno.hostIsDown.rawValue) #endif XCTAssert(EHOSTUNREACH == Errno.noRouteToHost.rawValue) @@ -115,7 +117,9 @@ final class ErrnoTest: XCTestCase { XCTAssert(WSAEDQUOT == Errno.diskQuotaExceeded.rawValue) XCTAssert(WSAESTALE == Errno.staleNFSFileHandle.rawValue) #else +#if !os(WASI) XCTAssert(EUSERS == Errno.tooManyUsers.rawValue) +#endif XCTAssert(EDQUOT == Errno.diskQuotaExceeded.rawValue) XCTAssert(ESTALE == Errno.staleNFSFileHandle.rawValue) #endif @@ -171,7 +175,7 @@ final class ErrnoTest: XCTestCase { XCTAssert(EPROTO == Errno.protocolError.rawValue) #endif -#if !os(Windows) && !os(FreeBSD) +#if !os(Windows) && !os(FreeBSD) && !os(WASI) XCTAssert(ENODATA == Errno.noData.rawValue) XCTAssert(ENOSR == Errno.noStreamResources.rawValue) XCTAssert(ENOSTR == Errno.notStream.rawValue) @@ -181,11 +185,13 @@ final class ErrnoTest: XCTestCase { XCTAssert(EOPNOTSUPP == Errno.notSupportedOnSocket.rawValue) // From headers but not man page +#if !os(WASI) // Would need to use _getConst func from CSystem XCTAssert(EWOULDBLOCK == Errno.wouldBlock.rawValue) +#endif #if os(Windows) XCTAssert(WSAETOOMANYREFS == Errno.tooManyReferences.rawValue) XCTAssert(WSAEREMOTE == Errno.tooManyRemoteLevels.rawValue) -#else +#elseif !os(WASI) XCTAssert(ETOOMANYREFS == Errno.tooManyReferences.rawValue) XCTAssert(EREMOTE == Errno.tooManyRemoteLevels.rawValue) #endif diff --git a/Tests/SystemTests/FileOperationsTest.swift b/Tests/SystemTests/FileOperationsTest.swift index 18adde3..479e503 100644 --- a/Tests/SystemTests/FileOperationsTest.swift +++ b/Tests/SystemTests/FileOperationsTest.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2025 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -16,10 +16,13 @@ import XCTest #endif #if canImport(Android) import Android +#elseif os(WASI) +import CSystem #endif @available(/*System 0.0.1: macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0*/iOS 8, *) final class FileOperationsTest: XCTestCase { + #if !os(WASI) // Would need to use _getConst funcs from CSystem func testSyscalls() { let fd = FileDescriptor(rawValue: 1) @@ -88,6 +91,7 @@ final class FileOperationsTest: XCTestCase { for test in syscallTestCases { test.runAllTests() } } + #endif // !os(WASI) func testWriteFromEmptyBuffer() throws { #if os(Windows) @@ -153,6 +157,7 @@ final class FileOperationsTest: XCTestCase { // TODO: Test writeAll, writeAll(toAbsoluteOffset), closeAfter } + #if !os(WASI) // WASI has no pipe func testAdHocPipe() throws { // Ad-hoc test testing `Pipe` functionality. // We cannot test `Pipe` using `MockTestCase` because it calls `pipe` with a pointer to an array local to the `Pipe`, the address of which we do not know prior to invoking `Pipe`. @@ -171,6 +176,7 @@ final class FileOperationsTest: XCTestCase { } } } + #endif func testAdHocOpen() { // Ad-hoc test touching a file system. @@ -211,8 +217,13 @@ final class FileOperationsTest: XCTestCase { func testGithubIssues() { // https://github.com/apple/swift-system/issues/26 + #if os(WASI) + let openOptions = _getConst_O_WRONLY() | _getConst_O_CREAT() + #else + let openOptions = O_WRONLY | O_CREAT + #endif let issue26 = MockTestCase( - name: "open", .interruptable, "a path", O_WRONLY | O_CREAT, 0o020 + name: "open", .interruptable, "a path", openOptions, 0o020 ) { retryOnInterrupt in _ = try FileDescriptor.open( @@ -221,7 +232,6 @@ final class FileOperationsTest: XCTestCase { retryOnInterrupt: retryOnInterrupt) } issue26.runAllTests() - } func testResizeFile() throws { diff --git a/Tests/SystemTests/FileTypesTest.swift b/Tests/SystemTests/FileTypesTest.swift index 5258709..a818dfb 100644 --- a/Tests/SystemTests/FileTypesTest.swift +++ b/Tests/SystemTests/FileTypesTest.swift @@ -1,7 +1,7 @@ /* This source file is part of the Swift System open source project - Copyright (c) 2020 - 2021 Apple Inc. and the Swift System project authors + Copyright (c) 2020 - 2025 Apple Inc. and the Swift System project authors Licensed under Apache License v2.0 with Runtime Library Exception See https://swift.org/LICENSE.txt for license information @@ -32,6 +32,7 @@ final class FileDescriptorTest: XCTestCase { XCTAssertEqual(O_WRONLY, FileDescriptor.AccessMode.writeOnly.rawValue) XCTAssertEqual(O_RDWR, FileDescriptor.AccessMode.readWrite.rawValue) +#if !os(WASI) // Would need to use _getConst funcs from CSystem #if !os(Windows) XCTAssertEqual(O_NONBLOCK, FileDescriptor.OpenOptions.nonBlocking.rawValue) #endif @@ -39,6 +40,7 @@ final class FileDescriptorTest: XCTestCase { XCTAssertEqual(O_CREAT, FileDescriptor.OpenOptions.create.rawValue) XCTAssertEqual(O_TRUNC, FileDescriptor.OpenOptions.truncate.rawValue) XCTAssertEqual(O_EXCL, FileDescriptor.OpenOptions.exclusiveCreate.rawValue) +#endif // !os(WASI #if !os(Windows) XCTAssertEqual(O_NOFOLLOW, FileDescriptor.OpenOptions.noFollow.rawValue) XCTAssertEqual(O_CLOEXEC, FileDescriptor.OpenOptions.closeOnExec.rawValue)