mirror of
https://github.com/apple/swift-system
synced 2026-11-22 04:30:46 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
94090e1c5c | ||
|
|
b6a7776360 | ||
|
|
0ea6743c17 | ||
|
|
086219bf62 | ||
|
|
11d9d4a78e | ||
|
|
0c11feadd4 | ||
|
|
2410aed341 | ||
|
|
9ec41a667c |
No files matched your search
@@ -1,8 +1,5 @@
|
||||
name: Pull request
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
types: [opened, reopened, synchronize]
|
||||
@@ -12,50 +9,18 @@ jobs:
|
||||
name: Test
|
||||
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main
|
||||
with:
|
||||
swift_flags: "-Xbuild-tools-swiftc -DSYSTEM_CI"
|
||||
enable_linux_checks: true
|
||||
linux_os_versions: '["noble", "jammy", "focal"]'
|
||||
linux_exclude_swift_versions: |
|
||||
[
|
||||
{"os_version": "noble", "swift_version": "5.9"},
|
||||
{"os_version": "noble", "swift_version": "5.10"},
|
||||
{"os_version": "focal", "swift_version": "nightly-6.2"},
|
||||
{"os_version": "focal", "swift_version": "6.2"},
|
||||
{"os_version": "focal", "swift_version": "nightly-6.3"},
|
||||
{"os_version": "focal", "swift_version": "6.3"},
|
||||
{"os_version": "focal", "swift_version": "nightly-main"},
|
||||
]
|
||||
linux_os_versions: '["jammy", "focal"]'
|
||||
enable_macos_checks: true
|
||||
macos_exclude_xcode_versions: '[]'
|
||||
enable_windows_checks: true
|
||||
windows_exclude_swift_versions: |
|
||||
# FIXME: https://github.com/swiftlang/github-workflows/pull/140
|
||||
# Xcode 16.0 and 16.1 are not actually available
|
||||
macos_exclude_xcode_versions: |
|
||||
[
|
||||
{"swift_version": "5.9"},
|
||||
{"swift_version": "5.10"}
|
||||
{"xcode_version": "16.0"},
|
||||
{"xcode_version": "16.1"},
|
||||
]
|
||||
swift_flags: "-Xbuild-tools-swiftc -DSYSTEM_CI"
|
||||
enable_linux_static_sdk_build: true
|
||||
linux_static_sdk_exclude_swift_versions: |
|
||||
[
|
||||
{"os_version": "focal", "swift_version": "nightly-6.2"},
|
||||
{"os_version": "focal", "swift_version": "6.2"},
|
||||
{"os_version": "focal", "swift_version": "nightly-6.3"},
|
||||
{"os_version": "focal", "swift_version": "6.3"},
|
||||
{"os_version": "focal", "swift_version": "nightly-main"},
|
||||
]
|
||||
enable_wasm_sdk_build: true
|
||||
wasm_exclude_swift_versions: |
|
||||
[
|
||||
{"os_version": "focal", "swift_version": "nightly-6.2"},
|
||||
{"os_version": "focal", "swift_version": "6.2"},
|
||||
{"os_version": "focal", "swift_version": "nightly-6.3"},
|
||||
{"os_version": "focal", "swift_version": "6.3"},
|
||||
{"os_version": "focal", "swift_version": "nightly-main"},
|
||||
]
|
||||
enable_android_sdk_build: true
|
||||
android_exclude_swift_versions: |
|
||||
[
|
||||
{"os_version": "focal", "swift_version": "nightly-main"},
|
||||
]
|
||||
|
||||
build-abi-stable:
|
||||
name: Build ABI Stable
|
||||
@@ -66,7 +31,13 @@ jobs:
|
||||
enable_windows_checks: false
|
||||
# Only build
|
||||
macos_build_command: "xcrun swift build --build-tests"
|
||||
macos_exclude_xcode_versions: '[]'
|
||||
# FIXME: https://github.com/swiftlang/github-workflows/pull/140
|
||||
# Xcode 16.0 and 16.1 are not actually available
|
||||
macos_exclude_xcode_versions: |
|
||||
[
|
||||
{"xcode_version": "16.0"},
|
||||
{"xcode_version": "16.1"},
|
||||
]
|
||||
# Enable availability to match ABI stable verion of system.
|
||||
swift_flags: "-Xbuild-tools-swiftc -DSYSTEM_CI -Xbuild-tools-swiftc -DSYSTEM_ABI_STABLE"
|
||||
|
||||
|
||||
@@ -134,13 +134,11 @@ typedef struct __SWIFT_IORING_SQE_FALLBACK_STRUCT swift_io_uring_sqe;
|
||||
#define IORING_FEAT_RW_ATTR (1U << 16)
|
||||
#define IORING_FEAT_NO_IOWAIT (1U << 17)
|
||||
|
||||
#if !defined(_ASM_GENERIC_INT_LL64_H) && !defined(_ASM_GENERIC_INT_L64_H) && !defined(_UAPI_ASM_GENERIC_INT_LL64_H) && !defined(_UAPI_ASM_GENERIC_INT_L64_H)
|
||||
typedef uint8_t __u8;
|
||||
typedef uint16_t __u16;
|
||||
typedef uint32_t __u32;
|
||||
typedef uint64_t __u64;
|
||||
typedef int32_t __s32;
|
||||
#endif
|
||||
|
||||
#ifndef __kernel_rwf_t
|
||||
typedef int __kernel_rwf_t;
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
/// occurred.
|
||||
@frozen
|
||||
@available(System 0.0.1, *)
|
||||
public struct Errno: RawRepresentable, Error, Hashable, Codable {
|
||||
public struct Errno: RawRepresentable, Error, Hashable {
|
||||
/// The raw C error number.
|
||||
@_alwaysEmitIntoClient
|
||||
public let rawValue: CInt
|
||||
@@ -160,7 +160,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable {
|
||||
/// No child processes.
|
||||
///
|
||||
/// A `wait(2)` or `waitpid(2)` function was executed
|
||||
/// by a process that doesn't have any existing child processes
|
||||
/// by a process that dosn't have any existing child processes
|
||||
/// or whose child processes are all already being waited for.
|
||||
///
|
||||
/// The corresponding C error is `ECHILD`.
|
||||
@@ -1562,6 +1562,8 @@ extension Errno: CustomStringConvertible, CustomDebugStringConvertible {
|
||||
public var debugDescription: String { self.description }
|
||||
}
|
||||
|
||||
#if !$Embedded
|
||||
|
||||
@available(System 0.0.1, *)
|
||||
extension Errno {
|
||||
@_alwaysEmitIntoClient
|
||||
@@ -1571,3 +1573,13 @@ extension Errno {
|
||||
}
|
||||
}
|
||||
|
||||
@available(System 0.0.1, *)
|
||||
extension Errno: Codable {}
|
||||
|
||||
public typealias SystemError = any Error
|
||||
|
||||
#else
|
||||
|
||||
public typealias SystemError = Errno
|
||||
|
||||
#endif // !$Embedded
|
||||
@@ -15,7 +15,7 @@
|
||||
/// in the same way as you manage a raw C file handle.
|
||||
@frozen
|
||||
@available(System 0.0.1, *)
|
||||
public struct FileDescriptor: RawRepresentable, Hashable, Codable {
|
||||
public struct FileDescriptor: RawRepresentable, Hashable {
|
||||
/// The raw C file handle.
|
||||
@_alwaysEmitIntoClient
|
||||
public let rawValue: CInt
|
||||
@@ -46,7 +46,7 @@ extension FileDescriptor {
|
||||
/// The desired read and write access for a newly opened file.
|
||||
@frozen
|
||||
@available(System 0.0.1, *)
|
||||
public struct AccessMode: RawRepresentable, Sendable, Hashable, Codable {
|
||||
public struct AccessMode: RawRepresentable, Sendable, Hashable {
|
||||
/// The raw C access mode.
|
||||
@_alwaysEmitIntoClient
|
||||
public var rawValue: CInt
|
||||
@@ -89,7 +89,7 @@ extension FileDescriptor {
|
||||
/// Options that specify behavior for a newly-opened file.
|
||||
@frozen
|
||||
@available(System 0.0.1, *)
|
||||
public struct OpenOptions: OptionSet, Sendable, Hashable, Codable {
|
||||
public struct OpenOptions: OptionSet, Sendable, Hashable {
|
||||
/// The raw C options.
|
||||
@_alwaysEmitIntoClient
|
||||
public var rawValue: CInt
|
||||
@@ -327,7 +327,7 @@ extension FileDescriptor {
|
||||
/// Options for specifying what a file descriptor's offset is relative to.
|
||||
@frozen
|
||||
@available(System 0.0.1, *)
|
||||
public struct SeekOrigin: RawRepresentable, Sendable, Hashable, Codable {
|
||||
public struct SeekOrigin: RawRepresentable, Sendable, Hashable {
|
||||
/// The raw C value.
|
||||
@_alwaysEmitIntoClient
|
||||
public var rawValue: CInt
|
||||
@@ -402,6 +402,22 @@ extension FileDescriptor {
|
||||
}
|
||||
}
|
||||
|
||||
#if !$Embedded
|
||||
|
||||
@available(System 0.0.1, *)
|
||||
extension FileDescriptor: Codable {}
|
||||
|
||||
@available(System 0.0.1, *)
|
||||
extension FileDescriptor.AccessMode: Codable {}
|
||||
|
||||
@available(System 0.0.1, *)
|
||||
extension FileDescriptor.OpenOptions: Codable {}
|
||||
|
||||
@available(System 0.0.1, *)
|
||||
extension FileDescriptor.SeekOrigin: Codable {}
|
||||
|
||||
#endif // !$Embedded
|
||||
|
||||
@available(System 0.0.1, *)
|
||||
extension FileDescriptor.AccessMode
|
||||
: CustomStringConvertible, CustomDebugStringConvertible
|
||||
|
||||
@@ -30,24 +30,24 @@ extension FileDescriptor {
|
||||
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
|
||||
permissions: FilePermissions? = nil,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws -> FileDescriptor {
|
||||
) throws(SystemError) -> FileDescriptor {
|
||||
#if !os(Windows)
|
||||
return try path.withCString {
|
||||
return try path.withCString { (cString) throws(SystemError) in
|
||||
try FileDescriptor.open(
|
||||
$0, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt)
|
||||
cString, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt)
|
||||
}
|
||||
#else
|
||||
return try path.withPlatformString {
|
||||
#else
|
||||
return try path.withPlatformString { (platformString) throws(SystemError) in
|
||||
try FileDescriptor.open(
|
||||
$0, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt)
|
||||
platformString, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if !os(Windows)
|
||||
// On Darwin, `CInterop.PlatformChar` is less available than
|
||||
#if !os(Windows)
|
||||
// On Darwin, `CInterop.PlatformChar` is less available than
|
||||
// `FileDescriptor.open`, so we need to use `CChar` instead.
|
||||
|
||||
|
||||
/// Opens or creates a file for reading or writing.
|
||||
///
|
||||
/// - Parameters:
|
||||
@@ -69,7 +69,7 @@ extension FileDescriptor {
|
||||
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
|
||||
permissions: FilePermissions? = nil,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws -> FileDescriptor {
|
||||
) throws(SystemError) -> FileDescriptor {
|
||||
try FileDescriptor._open(
|
||||
path, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
@@ -116,7 +116,7 @@ extension FileDescriptor {
|
||||
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
|
||||
permissions: FilePermissions? = nil,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws -> FileDescriptor {
|
||||
) throws(SystemError) -> FileDescriptor {
|
||||
try FileDescriptor._open(
|
||||
path, mode, options: options, permissions: permissions, retryOnInterrupt: retryOnInterrupt
|
||||
).get()
|
||||
@@ -149,7 +149,7 @@ extension FileDescriptor {
|
||||
///
|
||||
/// The corresponding C function is `close`.
|
||||
@_alwaysEmitIntoClient
|
||||
public func close() throws { try _close().get() }
|
||||
public func close() throws(SystemError) { try _close().get() }
|
||||
|
||||
@usableFromInline
|
||||
internal func _close() -> Result<(), Errno> {
|
||||
@@ -169,7 +169,7 @@ extension FileDescriptor {
|
||||
@discardableResult
|
||||
public func seek(
|
||||
offset: Int64, from whence: FileDescriptor.SeekOrigin
|
||||
) throws -> Int64 {
|
||||
) throws(SystemError) -> Int64 {
|
||||
try _seek(offset: offset, from: whence).get()
|
||||
}
|
||||
|
||||
@@ -187,7 +187,7 @@ extension FileDescriptor {
|
||||
@available(*, unavailable, renamed: "seek")
|
||||
public func lseek(
|
||||
offset: Int64, from whence: FileDescriptor.SeekOrigin
|
||||
) throws -> Int64 {
|
||||
) throws(SystemError) -> Int64 {
|
||||
try seek(offset: offset, from: whence)
|
||||
}
|
||||
|
||||
@@ -214,7 +214,7 @@ extension FileDescriptor {
|
||||
public func read(
|
||||
into buffer: UnsafeMutableRawBufferPointer,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws -> Int {
|
||||
) throws(SystemError) -> Int {
|
||||
try _read(into: buffer, retryOnInterrupt: retryOnInterrupt).get()
|
||||
}
|
||||
|
||||
@@ -222,7 +222,7 @@ extension FileDescriptor {
|
||||
internal func _read(
|
||||
into buffer: UnsafeMutableRawBufferPointer,
|
||||
retryOnInterrupt: Bool
|
||||
) throws -> Result<Int, Errno> {
|
||||
) throws(SystemError) -> Result<Int, Errno> {
|
||||
valueOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
system_read(self.rawValue, buffer.baseAddress, buffer.count)
|
||||
}
|
||||
@@ -251,7 +251,7 @@ extension FileDescriptor {
|
||||
fromAbsoluteOffset offset: Int64,
|
||||
into buffer: UnsafeMutableRawBufferPointer,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws -> Int {
|
||||
) throws(SystemError) -> Int {
|
||||
try _read(
|
||||
fromAbsoluteOffset: offset,
|
||||
into: buffer,
|
||||
@@ -276,7 +276,7 @@ extension FileDescriptor {
|
||||
fromAbsoluteOffset offset: Int64,
|
||||
into buffer: UnsafeMutableRawBufferPointer,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws -> Int {
|
||||
) throws(SystemError) -> Int {
|
||||
try read(
|
||||
fromAbsoluteOffset: offset,
|
||||
into: buffer,
|
||||
@@ -303,7 +303,7 @@ extension FileDescriptor {
|
||||
public func write(
|
||||
_ buffer: UnsafeRawBufferPointer,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws -> Int {
|
||||
) throws(SystemError) -> Int {
|
||||
try _write(buffer, retryOnInterrupt: retryOnInterrupt).get()
|
||||
}
|
||||
|
||||
@@ -337,7 +337,7 @@ extension FileDescriptor {
|
||||
toAbsoluteOffset offset: Int64,
|
||||
_ buffer: UnsafeRawBufferPointer,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws -> Int {
|
||||
) throws(SystemError) -> Int {
|
||||
try _write(toAbsoluteOffset: offset, buffer, retryOnInterrupt: retryOnInterrupt).get()
|
||||
}
|
||||
|
||||
@@ -359,7 +359,7 @@ extension FileDescriptor {
|
||||
toAbsoluteOffset offset: Int64,
|
||||
into buffer: UnsafeRawBufferPointer,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws -> Int {
|
||||
) throws(SystemError) -> Int {
|
||||
try write(
|
||||
toAbsoluteOffset: offset,
|
||||
buffer,
|
||||
@@ -402,7 +402,7 @@ extension FileDescriptor {
|
||||
public func duplicate(
|
||||
as target: FileDescriptor? = nil,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws -> FileDescriptor {
|
||||
) throws(SystemError) -> FileDescriptor {
|
||||
try _duplicate(as: target, retryOnInterrupt: retryOnInterrupt).get()
|
||||
}
|
||||
|
||||
@@ -411,7 +411,7 @@ extension FileDescriptor {
|
||||
internal func _duplicate(
|
||||
as target: FileDescriptor?,
|
||||
retryOnInterrupt: Bool
|
||||
) throws -> Result<FileDescriptor, Errno> {
|
||||
) throws(SystemError) -> Result<FileDescriptor, Errno> {
|
||||
valueOrErrno(retryOnInterrupt: retryOnInterrupt) {
|
||||
if let target = target {
|
||||
return system_dup2(self.rawValue, target.rawValue)
|
||||
@@ -422,13 +422,13 @@ extension FileDescriptor {
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "duplicate")
|
||||
public func dup() throws -> FileDescriptor {
|
||||
public func dup() throws(SystemError) -> FileDescriptor {
|
||||
fatalError("Not implemented")
|
||||
}
|
||||
|
||||
@_alwaysEmitIntoClient
|
||||
@available(*, unavailable, renamed: "duplicate")
|
||||
public func dup2() throws -> FileDescriptor {
|
||||
public func dup2() throws(SystemError) -> FileDescriptor {
|
||||
fatalError("Not implemented")
|
||||
}
|
||||
}
|
||||
@@ -444,7 +444,7 @@ extension FileDescriptor {
|
||||
/// The corresponding C function is `pipe`.
|
||||
@_alwaysEmitIntoClient
|
||||
@available(System 1.1.0, *)
|
||||
public static func pipe() throws -> (readEnd: FileDescriptor, writeEnd: FileDescriptor) {
|
||||
public static func pipe() throws(SystemError) -> (readEnd: FileDescriptor, writeEnd: FileDescriptor) {
|
||||
try _pipe().get()
|
||||
}
|
||||
|
||||
@@ -490,7 +490,7 @@ extension FileDescriptor {
|
||||
public func resize(
|
||||
to newSize: Int64,
|
||||
retryOnInterrupt: Bool = true
|
||||
) throws {
|
||||
) throws(SystemError) {
|
||||
try _resize(
|
||||
to: newSize,
|
||||
retryOnInterrupt: retryOnInterrupt
|
||||
@@ -537,7 +537,7 @@ extension FilePermissions {
|
||||
/// afterwards, because of the way reading the creation mask works.
|
||||
internal static func withCreationMask<R>(
|
||||
_ permissions: FilePermissions,
|
||||
body: () throws -> R
|
||||
body: () throws(SystemError) -> R
|
||||
) rethrows -> R {
|
||||
let oldMask = _umask(permissions.rawValue)
|
||||
defer {
|
||||
|
||||
@@ -68,6 +68,8 @@ extension FilePath {
|
||||
@available(System 0.0.1, *)
|
||||
extension FilePath: Hashable {}
|
||||
|
||||
#if !$Embedded
|
||||
|
||||
@available(System 0.0.1, *)
|
||||
extension FilePath: Codable {
|
||||
// Encoder is synthesized; it probably should have been explicit and used
|
||||
@@ -88,3 +90,5 @@ extension FilePath: Codable {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // !$Embedded
|
||||
@@ -128,6 +128,8 @@ extension SystemString {
|
||||
}
|
||||
}
|
||||
|
||||
#if !$Embedded
|
||||
|
||||
// Unifying protocol for common functionality between roots, components,
|
||||
// and views onto SystemString and FilePath.
|
||||
internal protocol _StrSlice: _PlatformStringable, Hashable, Codable {
|
||||
@@ -138,6 +140,20 @@ internal protocol _StrSlice: _PlatformStringable, Hashable, Codable {
|
||||
|
||||
func _invariantCheck()
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
internal protocol _StrSlice: _PlatformStringable, Hashable {
|
||||
var _storage: SystemString { get }
|
||||
var _range: Range<SystemString.Index> { get }
|
||||
|
||||
init?(_ str: SystemString)
|
||||
|
||||
func _invariantCheck()
|
||||
}
|
||||
|
||||
#endif // !$Embedded
|
||||
|
||||
extension _StrSlice {
|
||||
internal var _slice: Slice<SystemString> {
|
||||
Slice(base: _storage, bounds: _range)
|
||||
|
||||
@@ -64,7 +64,7 @@ extension FilePath {
|
||||
}
|
||||
|
||||
#if !os(Windows)
|
||||
// Note: This function should have been opaque, but it shipped as
|
||||
// Note: This function should have been opaque, but it shipped as
|
||||
// `@_alwaysEmitIntoClient` in macOS 12/iOS 15, and now it is stuck
|
||||
// this way forever. (Or until the language provides a way for us
|
||||
// to declare separate availability for a function's exported symbol
|
||||
@@ -84,7 +84,7 @@ extension FilePath {
|
||||
/// Don't try to store the pointer for later use.
|
||||
@_alwaysEmitIntoClient
|
||||
public func withPlatformString<Result>(
|
||||
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
|
||||
_ body: (UnsafePointer<CInterop.PlatformChar>) throws(SystemError) -> Result
|
||||
) rethrows -> Result {
|
||||
return try withCString(body)
|
||||
}
|
||||
@@ -102,7 +102,7 @@ extension FilePath {
|
||||
/// only during the execution of this method.
|
||||
/// Don't try to store the pointer for later use.
|
||||
public func withPlatformString<Result>(
|
||||
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
|
||||
_ body: (UnsafePointer<CInterop.PlatformChar>) throws(SystemError) -> Result
|
||||
) rethrows -> Result {
|
||||
return try _withPlatformString(body)
|
||||
}
|
||||
@@ -595,8 +595,8 @@ extension FilePath {
|
||||
/// For backwards compatibility only. This function is equivalent to
|
||||
/// the preferred `withPlatformString`.
|
||||
public func withCString<Result>(
|
||||
_ body: (UnsafePointer<CChar>) throws -> Result
|
||||
) rethrows -> Result {
|
||||
_ body: (UnsafePointer<CChar>) throws(SystemError) -> Result
|
||||
) throws(SystemError) -> Result {
|
||||
return try _withPlatformString(body)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,8 +20,8 @@
|
||||
/// deletes the directory and all of its contents before returning.
|
||||
internal func withTemporaryFilePath<R>(
|
||||
basename: FilePath.Component,
|
||||
_ body: (FilePath) throws -> R
|
||||
) throws -> R {
|
||||
_ body: (FilePath) throws(SystemError) -> R
|
||||
) throws(SystemError) -> R {
|
||||
let temporaryDir = try createUniqueTemporaryDirectory(basename: basename)
|
||||
defer {
|
||||
try? _recursiveRemove(at: temporaryDir)
|
||||
@@ -44,9 +44,9 @@ fileprivate let base64 = Array<UInt8>(
|
||||
///
|
||||
/// This function will throw if there is an error, except if the error
|
||||
/// is that the directory exists, in which case it returns `false`.
|
||||
fileprivate func makeLockedDownDirectory(at path: FilePath) throws -> Bool {
|
||||
return try path.withPlatformString {
|
||||
if system_mkdir($0, 0o700) == 0 {
|
||||
fileprivate func makeLockedDownDirectory(at path: FilePath) throws(SystemError) -> Bool {
|
||||
return try path.withPlatformString { (platformString) throws(SystemError) in
|
||||
if system_mkdir(platformString, 0o700) == 0 {
|
||||
return true
|
||||
}
|
||||
let err = system_errno
|
||||
@@ -83,7 +83,7 @@ fileprivate func createRandomString(length: Int) -> String {
|
||||
/// string of characters.
|
||||
fileprivate func createUniqueTemporaryDirectory(
|
||||
basename: FilePath.Component
|
||||
) throws -> FilePath {
|
||||
) throws(SystemError) -> FilePath {
|
||||
var tempDir = try _getTemporaryDirectory()
|
||||
tempDir.append(basename)
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
#if !os(Windows)
|
||||
|
||||
/// Get the path to the system temporary directory.
|
||||
internal func _getTemporaryDirectory() throws -> FilePath {
|
||||
internal func _getTemporaryDirectory() throws(SystemError) -> FilePath {
|
||||
guard let tmp = system_getenv("TMPDIR") else {
|
||||
return "/tmp"
|
||||
}
|
||||
@@ -26,22 +26,22 @@ internal func _getTemporaryDirectory() throws -> FilePath {
|
||||
/// Removes a directory completely, including all of its contents.
|
||||
internal func _recursiveRemove(
|
||||
at path: FilePath
|
||||
) throws {
|
||||
) throws(SystemError) {
|
||||
let dirfd = try FileDescriptor.open(path, .readOnly, options: .directory)
|
||||
defer {
|
||||
try? dirfd.close()
|
||||
}
|
||||
|
||||
let dot: (CInterop.PlatformChar, CInterop.PlatformChar) = (46, 0)
|
||||
try withUnsafeBytes(of: dot) {
|
||||
try withUnsafeBytes(of: dot) { (bytes) throws(SystemError) in
|
||||
try recursiveRemove(
|
||||
in: dirfd.rawValue,
|
||||
name: $0.assumingMemoryBound(to: CInterop.PlatformChar.self).baseAddress!
|
||||
name: bytes.assumingMemoryBound(to: CInterop.PlatformChar.self).baseAddress!
|
||||
)
|
||||
}
|
||||
|
||||
try path.withPlatformString {
|
||||
if system_rmdir($0) != 0 {
|
||||
try path.withPlatformString { (platformString) throws(SystemError) in
|
||||
if system_rmdir(platformString) != 0 {
|
||||
throw Errno.current
|
||||
}
|
||||
}
|
||||
@@ -81,8 +81,8 @@ fileprivate func impl_opendirat(
|
||||
fileprivate func forEachFile(
|
||||
in dirfd: CInt,
|
||||
subdir: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ body: (system_dirent) throws -> ()
|
||||
) throws {
|
||||
_ body: (system_dirent) throws(SystemError) -> ()
|
||||
) throws(SystemError) {
|
||||
guard let dir = impl_opendirat(dirfd, subdir) else {
|
||||
throw Errno.current
|
||||
}
|
||||
@@ -113,7 +113,7 @@ fileprivate func forEachFile(
|
||||
fileprivate func recursiveRemove(
|
||||
in dirfd: CInt,
|
||||
name: UnsafePointer<CInterop.PlatformChar>
|
||||
) throws {
|
||||
) throws(SystemError) {
|
||||
// First, deal with subdirectories
|
||||
try forEachFile(in: dirfd, subdir: name) { dirent in
|
||||
if dirent.d_type == SYSTEM_DT_DIR {
|
||||
|
||||
@@ -517,7 +517,7 @@ fileprivate func HRESULT_CODE(_ hr: HRESULT) -> DWORD {
|
||||
|
||||
@inline(__always)
|
||||
fileprivate func HRESULT_FACILITY(_ hr: HRESULT) -> DWORD {
|
||||
DWORD(hr >> 16) & 0x1fff
|
||||
DWORD(hr << 16) & 0x1fff
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
/// perms == [.ownerReadWrite, .groupRead, .otherRead] // true
|
||||
@frozen
|
||||
@available(System 0.0.1, *)
|
||||
public struct FilePermissions: OptionSet, Sendable, Hashable, Codable {
|
||||
public struct FilePermissions: OptionSet, Sendable, Hashable {
|
||||
/// The raw C file permissions.
|
||||
@_alwaysEmitIntoClient
|
||||
public let rawValue: CModeT
|
||||
@@ -172,3 +172,8 @@ extension FilePermissions
|
||||
/// A textual representation of the file permissions, suitable for debugging.
|
||||
public var debugDescription: String { self.description }
|
||||
}
|
||||
|
||||
#if !$Embedded
|
||||
@available(System 0.0.1, *)
|
||||
extension FilePermissions: Codable {}
|
||||
#endif
|
||||
@@ -320,7 +320,7 @@ extension IORing.Request {
|
||||
* ASYNC_CANCEL flags.
|
||||
*
|
||||
* IORING_ASYNC_CANCEL_ALL Cancel all requests that match the given key
|
||||
* IORING_ASYNC_CANCEL_FD Key off 'fd' for cancelation rather than the
|
||||
* IORING_ASYNC_CANCEL_FD Key off 'fd' for cancellation rather than the
|
||||
* request 'user_data'
|
||||
* IORING_ASYNC_CANCEL_ANY Match any request
|
||||
* IORING_ASYNC_CANCEL_FD_FIXED 'fd' passed in is a fixed descriptor
|
||||
@@ -388,9 +388,7 @@ extension IORing.Request {
|
||||
}
|
||||
|
||||
@inline(__always) @inlinable
|
||||
internal consuming func makeRawRequest(
|
||||
pathBuffers: PendingPathBuffers
|
||||
) -> RawIORequest {
|
||||
internal consuming func makeRawRequest() -> RawIORequest {
|
||||
var request = RawIORequest()
|
||||
switch extractCore() {
|
||||
case .nop:
|
||||
@@ -402,19 +400,27 @@ extension IORing.Request {
|
||||
request.operation = .openAt
|
||||
request.fileDescriptor = atDirectory
|
||||
request.rawValue.addr = UInt64(
|
||||
UInt(bitPattern: pathBuffers.pin(path)))
|
||||
UInt(
|
||||
bitPattern: path.withPlatformString { ptr in
|
||||
ptr //this is unsavory, but we keep it alive by storing path alongside it in the request
|
||||
}))
|
||||
request.rawValue.open_flags = UInt32(bitPattern: options.rawValue | mode.rawValue)
|
||||
request.rawValue.len = permissions?.rawValue ?? 0
|
||||
request.rawValue.file_index = UInt32(fileSlot.index + 1)
|
||||
request.path = path
|
||||
request.rawValue.user_data = context
|
||||
case .openat(
|
||||
let atDirectory, let path, let mode, let options, let permissions, let context):
|
||||
request.operation = .openAt
|
||||
request.fileDescriptor = atDirectory
|
||||
request.rawValue.addr = UInt64(
|
||||
UInt(bitPattern: pathBuffers.pin(path)))
|
||||
UInt(
|
||||
bitPattern: path.withPlatformString { ptr in
|
||||
ptr //this is unsavory, but we keep it alive by storing path alongside it in the request
|
||||
}))
|
||||
request.rawValue.open_flags = UInt32(bitPattern: options.rawValue | mode.rawValue)
|
||||
request.rawValue.len = permissions?.rawValue ?? 0
|
||||
request.path = path
|
||||
request.rawValue.user_data = context
|
||||
case .write(let file, let buffer, let offset, let context):
|
||||
request.operation = .writeFixed
|
||||
@@ -460,7 +466,12 @@ extension IORing.Request {
|
||||
request.operation = .unlinkAt
|
||||
request.fileDescriptor = atDirectory
|
||||
request.rawValue.addr = UInt64(
|
||||
UInt(bitPattern: pathBuffers.pin(path)))
|
||||
UInt(
|
||||
bitPattern: path.withPlatformString { ptr in
|
||||
ptr //this is unsavory, but we keep it alive by storing path alongside it in the request
|
||||
})
|
||||
)
|
||||
request.path = path
|
||||
request.rawValue.user_data = context
|
||||
case .cancelContext(let flags, let targetContext):
|
||||
request.operation = .asyncCancel
|
||||
|
||||
@@ -41,33 +41,6 @@ extension UnsafeMutableRawBufferPointer {
|
||||
}
|
||||
}
|
||||
|
||||
/// Owns the FilePaths whose interior pointers have been written into pending
|
||||
/// SQEs. The kernel copies SQE-referenced data out during io_uring_enter (due to
|
||||
/// IORING_FEAT_SUBMIT_STABLE), so the paths only need to live across the prepare->submit gap
|
||||
/// This is a class purely to avoid making methods calling `pin` mutating, which would be an API break
|
||||
@usableFromInline
|
||||
internal final class PendingPathBuffers {
|
||||
@usableFromInline
|
||||
var paths: [FilePath] = []
|
||||
|
||||
init(reservedCapacity: Int) {
|
||||
paths.reserveCapacity(reservedCapacity)
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
func pin(_ path: FilePath) -> UnsafePointer<CInterop.PlatformChar> {
|
||||
paths.append(path)
|
||||
// Safe to escape: `paths` keeps the FilePath (and its underlying
|
||||
// [SystemChar] buffer) alive until clear() or class deinit.
|
||||
return paths.last!.withPlatformString { $0 }
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
func clear() {
|
||||
paths.removeAll(keepingCapacity: true)
|
||||
}
|
||||
}
|
||||
|
||||
// all pointers in this struct reference kernel-visible memory
|
||||
@usableFromInline struct SQRing: ~Copyable {
|
||||
@usableFromInline let kernelHead: UnsafePointer<Atomic<UInt32>>
|
||||
@@ -227,14 +200,7 @@ private func setUpRing(
|
||||
throw err
|
||||
}
|
||||
|
||||
// We require IORING_FEAT_SUBMIT_STABLE so the kernel copies SQE-referenced
|
||||
// pathnames (and other inline data) out of userspace before io_uring_enter
|
||||
// returns; the path-buffer side store on IORing relies on this to free
|
||||
// path buffers as soon as a submission completes.
|
||||
let requiredFeatures =
|
||||
IORing.Features.nonDroppingCompletions.rawValue
|
||||
| IORing.Features.stableSubmissions.rawValue
|
||||
if params.features & requiredFeatures != requiredFeatures
|
||||
if params.features & IORing.Features.nonDroppingCompletions.rawValue == 0
|
||||
{
|
||||
close(ringDescriptor)
|
||||
throw Errno.invalidArgument
|
||||
@@ -358,8 +324,6 @@ public struct IORing: ~Copyable {
|
||||
@usableFromInline var _registeredFiles: [UInt32]
|
||||
@usableFromInline var _registeredBuffers: [iovec]
|
||||
|
||||
@usableFromInline let _pendingPathBuffers: PendingPathBuffers
|
||||
|
||||
var features = Features(rawValue: 0)
|
||||
|
||||
/// RegisteredResource is used via its typealiases, RegisteredFile and RegisteredBuffer. Registering file descriptors and buffers with the IORing allows for more efficient access to them.
|
||||
@@ -480,7 +444,6 @@ public struct IORing: ~Copyable {
|
||||
self.completionRingSize = tmpCQSize
|
||||
self._registeredFiles = []
|
||||
self._registeredBuffers = []
|
||||
self._pendingPathBuffers = PendingPathBuffers(reservedCapacity: Int(params.sq_entries))
|
||||
self.submissionRing = submissionRing
|
||||
self.completionRing = completionRing
|
||||
self.submissionQueueEntries = submissionQueueEntries
|
||||
@@ -802,10 +765,6 @@ public struct IORing: ~Copyable {
|
||||
@inlinable
|
||||
public func submitPreparedRequests() throws(Errno) {
|
||||
try _submitRequests(ring: submissionRing, ringDescriptor: ringDescriptor)
|
||||
// IORING_FEAT_SUBMIT_STABLE guarantees the kernel has copied any
|
||||
// SQE-referenced data (e.g. openat/unlinkAt pathnames) before
|
||||
// io_uring_enter returns; safe to release path storage now.
|
||||
_pendingPathBuffers.clear()
|
||||
}
|
||||
|
||||
/// Sends all prepared requests to the kernel for processing, and then dequeues at least `minimumCount` completions, waiting up to `timeout` for them to become available. `consumer` is called to process each completed IO operation as it becomes available.
|
||||
@@ -831,14 +790,9 @@ public struct IORing: ~Copyable {
|
||||
/// Attempts to prepare an IO request for submission to the kernel. Returns false if no space is available to enqueue the request
|
||||
@inlinable
|
||||
public mutating func prepare(request: __owned Request) -> Bool {
|
||||
guard _getRemainingSubmissionQueueCapacity(ring: submissionRing) >= 1 else {
|
||||
return false
|
||||
}
|
||||
var raw: RawIORequest? = request.makeRawRequest(pathBuffers: _pendingPathBuffers)
|
||||
let ok = _tryWriteRequest(
|
||||
var raw: RawIORequest? = request.makeRawRequest()
|
||||
return _tryWriteRequest(
|
||||
raw.take()!, ring: &submissionRing, submissionQueueEntries: submissionQueueEntries)
|
||||
assert(ok)
|
||||
return ok
|
||||
}
|
||||
|
||||
/// Attempts to prepare a chain of linked IO requests for submission to the kernel. Returns false if not enough space is available to enqueue the request. If any linked operation fails, subsequent operations will be canceled. Linked operations always execute in order.
|
||||
@@ -852,20 +806,18 @@ public struct IORing: ~Copyable {
|
||||
return false
|
||||
}
|
||||
let last = linkedRequests.last!
|
||||
var allAdded = true
|
||||
for req in linkedRequests.dropLast() {
|
||||
var raw = req.makeRawRequest(pathBuffers: _pendingPathBuffers)
|
||||
var raw = req.makeRawRequest()
|
||||
raw.linkToNextRequest()
|
||||
let successfullyAdded = _tryWriteRequest(
|
||||
raw, ring: &submissionRing, submissionQueueEntries: submissionQueueEntries)
|
||||
assert(successfullyAdded)
|
||||
allAdded = allAdded && successfullyAdded
|
||||
}
|
||||
let successfullyAdded = _tryWriteRequest(
|
||||
last.makeRawRequest(pathBuffers: _pendingPathBuffers), ring: &submissionRing,
|
||||
last.makeRawRequest(), ring: &submissionRing,
|
||||
submissionQueueEntries: submissionQueueEntries)
|
||||
assert(successfullyAdded)
|
||||
return allAdded && successfullyAdded
|
||||
return true
|
||||
}
|
||||
|
||||
/// Prepares a sequence of requests for submission to the ring. Returns false if the submission queue doesn't have enough available space.
|
||||
|
||||
@@ -8,6 +8,7 @@ internal struct RawIORequest: ~Copyable {
|
||||
// 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 = swift_io_uring_sqe()
|
||||
|
||||
@@ -116,9 +116,9 @@ internal func system_memset(
|
||||
|
||||
// Interop between String and platfrom string
|
||||
extension String {
|
||||
internal func _withPlatformString<Result>(
|
||||
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
|
||||
) rethrows -> Result {
|
||||
internal func _withPlatformString<Success, Failure: Error>(
|
||||
_ body: (UnsafePointer<CInterop.PlatformChar>) throws(Failure) -> Success
|
||||
) throws(Failure) -> Success {
|
||||
// Need to #if because CChar may be signed
|
||||
#if os(Windows)
|
||||
return try withCString(encodedAs: CInterop.PlatformUnicodeEncoding.self, body)
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
// enabled in testing builds of System currently, to minimize runtime overhead of release builds.
|
||||
//
|
||||
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
internal struct Trace {
|
||||
internal struct Entry {
|
||||
|
||||
@@ -115,12 +115,12 @@ extension MockingDriver {
|
||||
}
|
||||
}
|
||||
|
||||
#endif // ENABLE_MOCKING
|
||||
#endif // ENABLE_MOCKING && !$Embedded
|
||||
|
||||
@inline(__always)
|
||||
internal var mockingEnabled: Bool {
|
||||
// Fast constant-foldable check for release builds
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
return contextualMockingEnabled
|
||||
#else
|
||||
return false
|
||||
@@ -129,15 +129,15 @@ internal var mockingEnabled: Bool {
|
||||
|
||||
@inline(__always)
|
||||
internal var forceWindowsPaths: Bool? {
|
||||
#if !ENABLE_MOCKING
|
||||
return nil
|
||||
#else
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
return MockingDriver.forceWindowsPaths
|
||||
#else
|
||||
return nil
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
// Strip the mock_system prefix and the arg list suffix
|
||||
private func originalSyscallName(_ function: String) -> String {
|
||||
// `function` must be of format `system_<name>(<parameters>)`
|
||||
@@ -193,12 +193,12 @@ internal func _mockOffT(
|
||||
) -> _COffT {
|
||||
_COffT(mockImpl(name: name, path: path, args))
|
||||
}
|
||||
#endif // ENABLE_MOCKING
|
||||
#endif // ENABLE_MOCKING && !$Embedded
|
||||
|
||||
// Force paths to be treated as Windows syntactically if `enabled` is
|
||||
// true, and as POSIX syntactically if not.
|
||||
internal func _withWindowsPaths(enabled: Bool, _ body: () -> ()) {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
MockingDriver.withMockingEnabled { driver in
|
||||
driver.forceWindowsSyntaxForPaths = enabled
|
||||
body()
|
||||
|
||||
@@ -31,7 +31,7 @@ import Android
|
||||
internal func system_open(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>, _ oflag: Int32
|
||||
) -> CInt {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
if mockingEnabled {
|
||||
return _mock(path: path, oflag)
|
||||
}
|
||||
@@ -43,7 +43,7 @@ internal func system_open(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ oflag: Int32, _ mode: CInterop.Mode
|
||||
) -> CInt {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
if mockingEnabled {
|
||||
return _mock(path: path, oflag, mode)
|
||||
}
|
||||
@@ -53,7 +53,7 @@ internal func system_open(
|
||||
|
||||
// close
|
||||
internal func system_close(_ fd: Int32) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
if mockingEnabled { return _mock(fd) }
|
||||
#endif
|
||||
return close(fd)
|
||||
@@ -63,7 +63,7 @@ internal func system_close(_ fd: Int32) -> Int32 {
|
||||
internal func system_read(
|
||||
_ fd: Int32, _ buf: UnsafeMutableRawPointer?, _ nbyte: Int
|
||||
) -> Int {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
if mockingEnabled { return _mockInt(fd, buf, nbyte) }
|
||||
#endif
|
||||
return read(fd, buf, nbyte)
|
||||
@@ -73,7 +73,7 @@ internal func system_read(
|
||||
internal func system_pread(
|
||||
_ fd: Int32, _ buf: UnsafeMutableRawPointer?, _ nbyte: Int, _ offset: off_t
|
||||
) -> Int {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
if mockingEnabled { return _mockInt(fd, buf, nbyte, offset) }
|
||||
#endif
|
||||
#if os(Android)
|
||||
@@ -91,7 +91,7 @@ internal func system_pread(
|
||||
internal func system_lseek(
|
||||
_ fd: Int32, _ off: off_t, _ whence: Int32
|
||||
) -> off_t {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
if mockingEnabled { return _mockOffT(fd, off, whence) }
|
||||
#endif
|
||||
return lseek(fd, off, whence)
|
||||
@@ -101,7 +101,7 @@ internal func system_lseek(
|
||||
internal func system_write(
|
||||
_ fd: Int32, _ buf: UnsafeRawPointer?, _ nbyte: Int
|
||||
) -> Int {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
if mockingEnabled { return _mockInt(fd, buf, nbyte) }
|
||||
#endif
|
||||
return write(fd, buf, nbyte)
|
||||
@@ -111,7 +111,7 @@ internal func system_write(
|
||||
internal func system_pwrite(
|
||||
_ fd: Int32, _ buf: UnsafeRawPointer?, _ nbyte: Int, _ offset: off_t
|
||||
) -> Int {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
if mockingEnabled { return _mockInt(fd, buf, nbyte, offset) }
|
||||
#endif
|
||||
#if os(Android)
|
||||
@@ -127,14 +127,14 @@ internal func system_pwrite(
|
||||
|
||||
#if !os(WASI)
|
||||
internal func system_dup(_ fd: Int32) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
if mockingEnabled { return _mock(fd) }
|
||||
#endif
|
||||
return dup(fd)
|
||||
}
|
||||
|
||||
internal func system_dup2(_ fd: Int32, _ fd2: Int32) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
if mockingEnabled { return _mock(fd, fd2) }
|
||||
#endif
|
||||
return dup2(fd, fd2)
|
||||
@@ -143,7 +143,7 @@ internal func system_dup2(_ fd: Int32, _ fd2: Int32) -> Int32 {
|
||||
|
||||
#if !os(WASI)
|
||||
internal func system_pipe(_ fds: UnsafeMutablePointer<Int32>) -> CInt {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
if mockingEnabled { return _mock(fds) }
|
||||
#endif
|
||||
return pipe(fds)
|
||||
@@ -151,7 +151,7 @@ internal func system_pipe(_ fds: UnsafeMutablePointer<Int32>) -> CInt {
|
||||
#endif
|
||||
|
||||
internal func system_ftruncate(_ fd: Int32, _ length: off_t) -> Int32 {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
if mockingEnabled { return _mock(fd, length) }
|
||||
#endif
|
||||
return ftruncate(fd, length)
|
||||
@@ -161,7 +161,7 @@ internal func system_mkdir(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ mode: CInterop.Mode
|
||||
) -> CInt {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
if mockingEnabled { return _mock(path: path, mode) }
|
||||
#endif
|
||||
return mkdir(path, mode)
|
||||
@@ -170,7 +170,7 @@ internal func system_mkdir(
|
||||
internal func system_rmdir(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>
|
||||
) -> CInt {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
if mockingEnabled { return _mock(path: path) }
|
||||
#endif
|
||||
return rmdir(path)
|
||||
@@ -208,7 +208,7 @@ internal func system_unlinkat(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ flag: CInt
|
||||
) -> CInt {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
if mockingEnabled { return _mock(fd, path, flag) }
|
||||
#endif
|
||||
return unlinkat(fd, path, flag)
|
||||
@@ -247,7 +247,7 @@ internal func system_openat(
|
||||
_ path: UnsafePointer<CInterop.PlatformChar>,
|
||||
_ oflag: Int32
|
||||
) -> CInt {
|
||||
#if ENABLE_MOCKING
|
||||
#if ENABLE_MOCKING && !$Embedded
|
||||
if mockingEnabled {
|
||||
return _mock(fd, path, oflag)
|
||||
}
|
||||
|
||||
@@ -135,11 +135,7 @@ internal func dup(_ fd: Int32) -> Int32 {
|
||||
|
||||
@inline(__always)
|
||||
internal func dup2(_ fd: Int32, _ fd2: Int32) -> Int32 {
|
||||
// _dup2 returns 0 to indicate success.
|
||||
if _dup2(fd, fd2) == 0 {
|
||||
return fd2
|
||||
}
|
||||
return -1
|
||||
_dup2(fd, fd2)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
@@ -149,12 +145,6 @@ internal func pread(
|
||||
let handle: intptr_t = _get_osfhandle(fd)
|
||||
if handle == /* INVALID_HANDLE_VALUE */ -1 { ucrt._set_errno(EBADF); return -1 }
|
||||
|
||||
// Windows ReadFile accepts DWORD (32-bit) for buffer size, so validate nbyte doesn't exceed it
|
||||
if nbyte > Int(DWORD.max) {
|
||||
ucrt._set_errno(EINVAL)
|
||||
return -1
|
||||
}
|
||||
|
||||
// NOTE: this is a non-owning handle, do *not* call CloseHandle on it
|
||||
let hFile: HANDLE = HANDLE(bitPattern: handle)!
|
||||
|
||||
@@ -177,12 +167,6 @@ internal func pwrite(
|
||||
let handle: intptr_t = _get_osfhandle(fd)
|
||||
if handle == /* INVALID_HANDLE_VALUE */ -1 { ucrt._set_errno(EBADF); return -1 }
|
||||
|
||||
// Windows WriteFile accepts DWORD (32-bit) for buffer size, so validate nbyte doesn't exceed it
|
||||
if nbyte > Int(DWORD.max) {
|
||||
ucrt._set_errno(EINVAL)
|
||||
return -1
|
||||
}
|
||||
|
||||
// NOTE: this is a non-owning handle, do *not* call CloseHandle on it
|
||||
let hFile: HANDLE = HANDLE(bitPattern: handle)!
|
||||
|
||||
|
||||
@@ -158,8 +158,8 @@ extension String {
|
||||
/// only during the execution of this method.
|
||||
/// Don't try to store the pointer for later use.
|
||||
public func withPlatformString<Result>(
|
||||
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
|
||||
) rethrows -> Result {
|
||||
_ body: (UnsafePointer<CInterop.PlatformChar>) throws(SystemError) -> Result
|
||||
) throws(SystemError) -> Result {
|
||||
try _withPlatformString(body)
|
||||
}
|
||||
|
||||
@@ -189,8 +189,8 @@ extension CInterop.PlatformUnicodeEncoding.CodeUnit {
|
||||
|
||||
internal protocol _PlatformStringable {
|
||||
func _withPlatformString<Result>(
|
||||
_ body: (UnsafePointer<CInterop.PlatformChar>) throws -> Result
|
||||
) rethrows -> Result
|
||||
_ body: (UnsafePointer<CInterop.PlatformChar>) throws(SystemError) -> Result
|
||||
) throws(SystemError) -> Result
|
||||
|
||||
init?(_platformString: UnsafePointer<CInterop.PlatformChar>)
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
// A platform-native character representation, currently used for file paths
|
||||
internal struct SystemChar:
|
||||
RawRepresentable, Sendable, Comparable, Hashable, Codable {
|
||||
RawRepresentable, Sendable, Comparable, Hashable {
|
||||
internal typealias RawValue = CInterop.PlatformChar
|
||||
|
||||
internal var rawValue: RawValue
|
||||
@@ -23,6 +23,12 @@ internal struct SystemChar:
|
||||
}
|
||||
}
|
||||
|
||||
#if !$Embedded
|
||||
|
||||
extension SystemChar: Codable {}
|
||||
|
||||
#endif // !$Embedded
|
||||
|
||||
extension SystemChar {
|
||||
internal init(ascii: Unicode.Scalar) {
|
||||
self.init(rawValue: numericCast(UInt8(ascii: ascii)))
|
||||
@@ -104,7 +110,7 @@ extension SystemString {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
fileprivate func _invariantCheck() {
|
||||
#if DEBUG
|
||||
precondition(_invariantsSatisfied())
|
||||
@@ -173,10 +179,14 @@ extension SystemString: RangeReplaceableCollection {
|
||||
}
|
||||
}
|
||||
|
||||
extension SystemString: Hashable, Codable {
|
||||
extension SystemString: Hashable {}
|
||||
|
||||
#if !$Embedded
|
||||
|
||||
extension SystemString: Codable {
|
||||
// Encoder is synthesized; it probably should have been explicit and used
|
||||
// a single-value container, but making that change now is somewhat risky.
|
||||
|
||||
|
||||
// Decoder is written explicitly to ensure that we validate invariants on
|
||||
// untrusted input.
|
||||
public init(from decoder: any Decoder) throws {
|
||||
@@ -195,6 +205,8 @@ extension SystemString: Hashable, Codable {
|
||||
}
|
||||
}
|
||||
|
||||
#endif // !$Embedded
|
||||
|
||||
extension SystemString {
|
||||
|
||||
internal func withNullTerminatedSystemChars<T>(
|
||||
|
||||
@@ -43,6 +43,8 @@ internal func nothingOrErrno<I: FixedWidthInteger>(
|
||||
valueOrErrno(retryOnInterrupt: retryOnInterrupt, f).map { _ in () }
|
||||
}
|
||||
|
||||
#if !$Embedded
|
||||
|
||||
// Run a precondition for debug client builds
|
||||
internal func _debugPrecondition(
|
||||
_ condition: @autoclosure () -> Bool,
|
||||
@@ -56,6 +58,8 @@ internal func _debugPrecondition(
|
||||
}
|
||||
}
|
||||
|
||||
#endif // !$Embedded
|
||||
|
||||
extension OpaquePointer {
|
||||
internal var _isNULL: Bool {
|
||||
OpaquePointer(bitPattern: Int(bitPattern: self)) == nil
|
||||
|
||||
@@ -52,13 +52,13 @@ extension Slice where Element: Equatable {
|
||||
return self[...idx]
|
||||
}
|
||||
|
||||
// If `e` is present, eat up to first occurence of `e`
|
||||
// If `e` is present, eat up to first occurrence of `e`
|
||||
internal mutating func _eatUntil(_ e: Element) -> Slice? {
|
||||
guard let idx = self.firstIndex(of: e) else { return nil }
|
||||
return _eatUntil(idx)
|
||||
}
|
||||
|
||||
// If `e` is present, eat up to and through first occurence of `e`
|
||||
// If `e` is present, eat up to and through first occurrence of `e`
|
||||
internal mutating func _eatThrough(_ e: Element) -> Slice? {
|
||||
guard let idx = self.firstIndex(of: e) else { return nil }
|
||||
return _eatThrough(idx)
|
||||
|
||||
@@ -22,7 +22,7 @@ import CSystem
|
||||
|
||||
@available(System 0.0.1, *)
|
||||
final class FileOperationsTest: XCTestCase {
|
||||
#if !os(WASI) // Would need to use _getConst funcs from CSystem
|
||||
#if ENABLE_MOCKING && !os(WASI) // Would need to use _getConst funcs from CSystem
|
||||
func testSyscalls() {
|
||||
let fd = FileDescriptor(rawValue: 1)
|
||||
|
||||
@@ -91,7 +91,7 @@ final class FileOperationsTest: XCTestCase {
|
||||
|
||||
for test in syscallTestCases { test.runAllTests() }
|
||||
}
|
||||
#endif // !os(WASI)
|
||||
#endif // ENABLE_MOCKING && !os(WASI)
|
||||
|
||||
func testWriteFromEmptyBuffer() throws {
|
||||
#if os(Windows)
|
||||
@@ -215,6 +215,7 @@ final class FileOperationsTest: XCTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
#if ENABLE_MOCKING
|
||||
func testGithubIssues() {
|
||||
// https://github.com/apple/swift-system/issues/26
|
||||
#if os(WASI)
|
||||
@@ -233,6 +234,7 @@ final class FileOperationsTest: XCTestCase {
|
||||
}
|
||||
issue26.runAllTests()
|
||||
}
|
||||
#endif // ENABLE_MOCKING
|
||||
|
||||
func testResizeFile() throws {
|
||||
try withTemporaryFilePath(basename: "testResizeFile") { path in
|
||||
|
||||
@@ -242,53 +242,6 @@ final class FileOperationsTestWindows: XCTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Test that buffer sizes exceeding DWORD.max (4GB) are properly rejected
|
||||
func testBufferSizeLimit() throws {
|
||||
try withTemporaryFilePath(basename: "testBufferSizeLimit") { path in
|
||||
let fd = try FileDescriptor.open(
|
||||
path.appending("test.txt"),
|
||||
.readWrite,
|
||||
options: [.create, .truncate],
|
||||
permissions: .ownerReadWrite
|
||||
)
|
||||
defer { try? fd.close() }
|
||||
|
||||
// Write some data first
|
||||
try fd.writeAll("test data".utf8)
|
||||
|
||||
// Allocate a small buffer for testing
|
||||
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 1024, alignment: 1)
|
||||
defer { buffer.deallocate() }
|
||||
|
||||
// Test that a count exceeding DWORD.max (UInt32.max = 4,294,967,295) returns EINVAL
|
||||
// We use a buffer pointer but pass a count > DWORD.max
|
||||
let oversizedCount = Int(DWORD.max) + 1
|
||||
let oversizedBuffer = UnsafeMutableRawBufferPointer(
|
||||
start: buffer.baseAddress,
|
||||
count: oversizedCount
|
||||
)
|
||||
|
||||
// pread should fail with EINVAL
|
||||
do {
|
||||
_ = try fd.read(fromAbsoluteOffset: 0, into: oversizedBuffer)
|
||||
XCTFail("Expected EINVAL for buffer size exceeding DWORD.max")
|
||||
} catch let err as Errno {
|
||||
XCTAssertEqual(err, .invalidArgument, "Expected EINVAL, got \(err)")
|
||||
}
|
||||
|
||||
// pwrite should also fail with EINVAL
|
||||
do {
|
||||
_ = try fd.write(toAbsoluteOffset: 0, UnsafeRawBufferPointer(oversizedBuffer))
|
||||
XCTFail("Expected EINVAL for buffer size exceeding DWORD.max")
|
||||
} catch let err as Errno {
|
||||
XCTAssertEqual(err, .invalidArgument, "Expected EINVAL, got \(err)")
|
||||
}
|
||||
|
||||
// Verify that exactly DWORD.max works (if we had a buffer that large)
|
||||
// We can't easily test this without allocating 4GB, but the boundary is correct
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // os(Windows)
|
||||
@@ -7,6 +7,7 @@
|
||||
See https://swift.org/LICENSE.txt for license information
|
||||
*/
|
||||
|
||||
#if ENABLE_MOCKING
|
||||
import XCTest
|
||||
|
||||
#if SYSTEM_PACKAGE
|
||||
@@ -105,3 +106,4 @@ final class FilePathParsingTest: XCTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif // ENABLE_MOCKING
|
||||
@@ -7,6 +7,7 @@
|
||||
See https://swift.org/LICENSE.txt for license information
|
||||
*/
|
||||
|
||||
#if ENABLE_MOCKING
|
||||
import XCTest
|
||||
|
||||
#if SYSTEM_PACKAGE
|
||||
@@ -1238,3 +1239,4 @@ final class FilePathSyntaxTest: XCTestCase {
|
||||
}
|
||||
|
||||
}
|
||||
#endif // ENABLE_MOCKING
|
||||
@@ -22,8 +22,7 @@ func requestBytes(_ request: consuming RawIORequest) -> [UInt8] {
|
||||
// which are known to work correctly.
|
||||
final class IORequestTests: XCTestCase {
|
||||
func testNop() {
|
||||
let req = IORing.Request.nop().makeRawRequest(
|
||||
pathBuffers: PendingPathBuffers(reservedCapacity: 0))
|
||||
let req = IORing.Request.nop().makeRawRequest()
|
||||
let sourceBytes = requestBytes(req)
|
||||
// convenient property of nop: it's all zeros!
|
||||
// for some unknown reason, liburing sets the fd field to -1.
|
||||
|
||||
@@ -127,81 +127,6 @@ final class IORingTests: XCTestCase {
|
||||
efdBuf.deallocate()
|
||||
rawBuffer.deallocate()
|
||||
}
|
||||
|
||||
// Prior to the fix this is testing, prepare() escaped a pointer obtained
|
||||
// inside `path.withPlatformString { ... }` into the SQE and dropped the
|
||||
// owning FilePath before submit, so io_uring_enter handed the kernel a
|
||||
// dangling pointer. Here we deliberately let the FilePath go out of scope
|
||||
// between prepare and submit, then churn the heap to make UAFs observable.
|
||||
func testPathBufferLifetimeAcrossPrepareSubmit() throws {
|
||||
guard try uringEnabled() else { return }
|
||||
let (parent, _) = try makeHelloWorldFile()
|
||||
var ring = try IORing(queueDepth: 6)
|
||||
|
||||
func enqueueOpen() {
|
||||
let path: FilePath = "/tmp/IORingTests/test.txt"
|
||||
let ok = ring.prepare(request: .open(path, in: parent, mode: .readOnly))
|
||||
XCTAssertTrue(ok)
|
||||
}
|
||||
enqueueOpen()
|
||||
|
||||
var churn: [UnsafeMutableRawBufferPointer] = []
|
||||
for _ in 0..<256 {
|
||||
let buf = UnsafeMutableRawBufferPointer.allocate(byteCount: 64, alignment: 1)
|
||||
memset(buf.baseAddress!, 0x41, 64)
|
||||
churn.append(buf)
|
||||
}
|
||||
for buf in churn { buf.deallocate() }
|
||||
churn.removeAll()
|
||||
|
||||
try ring.submitPreparedRequests()
|
||||
let completion = try ring.blockingConsumeCompletion()
|
||||
XCTAssertGreaterThanOrEqual(
|
||||
completion.result, 0,
|
||||
"open should succeed; got \(completion.result)")
|
||||
|
||||
if completion.result >= 0 {
|
||||
let fd = FileDescriptor(rawValue: Int32(completion.result))
|
||||
try fd.close()
|
||||
}
|
||||
try cleanUpHelloWorldFile(parent)
|
||||
}
|
||||
|
||||
func testPathBufferLifetimeAcrossLinkedRequests() throws {
|
||||
guard try uringEnabled() else { return }
|
||||
let (parent, _) = try makeHelloWorldFile()
|
||||
let rawBuffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 13, alignment: 16)
|
||||
var ring = try setupTestRing(depth: 6, fileSlots: 1, buffers: [rawBuffer])
|
||||
|
||||
func enqueueLinkedReadback() {
|
||||
let path: FilePath = "/tmp/IORingTests/test.txt"
|
||||
let ok = ring.prepare(linkedRequests:
|
||||
.open(path, in: parent, into: ring.registeredFileSlots[0], mode: .readOnly),
|
||||
.read(ring.registeredFileSlots[0], into: ring.registeredBuffers[0]),
|
||||
.close(ring.registeredFileSlots[0]))
|
||||
XCTAssertTrue(ok)
|
||||
}
|
||||
enqueueLinkedReadback()
|
||||
|
||||
var churn: [UnsafeMutableRawBufferPointer] = []
|
||||
for _ in 0..<256 {
|
||||
let buf = UnsafeMutableRawBufferPointer.allocate(byteCount: 64, alignment: 1)
|
||||
memset(buf.baseAddress!, 0x41, 64)
|
||||
churn.append(buf)
|
||||
}
|
||||
for buf in churn { buf.deallocate() }
|
||||
|
||||
try ring.submitPreparedRequests()
|
||||
_ = try ring.blockingConsumeCompletion() // open
|
||||
_ = try ring.blockingConsumeCompletion() // read
|
||||
_ = try ring.blockingConsumeCompletion() // close
|
||||
|
||||
let result = String(cString: rawBuffer.assumingMemoryBound(to: CChar.self).baseAddress!)
|
||||
XCTAssertEqual(result, "Hello, World!")
|
||||
|
||||
try cleanUpHelloWorldFile(parent)
|
||||
rawBuffer.deallocate()
|
||||
}
|
||||
}
|
||||
#endif // os(Linux)
|
||||
#endif // compiler(>=6.2) && $Lifetimes
|
||||
@@ -7,6 +7,7 @@
|
||||
See https://swift.org/LICENSE.txt for license information
|
||||
*/
|
||||
|
||||
#if ENABLE_MOCKING
|
||||
import XCTest
|
||||
|
||||
#if SYSTEM_PACKAGE
|
||||
@@ -48,3 +49,4 @@ final class MockingTest: XCTestCase {
|
||||
XCTAssertFalse(mockingEnabled)
|
||||
}
|
||||
}
|
||||
#endif // ENABLE_MOCKING
|
||||
@@ -17,6 +17,7 @@ import XCTest
|
||||
|
||||
internal struct Wildcard: Hashable {}
|
||||
|
||||
#if ENABLE_MOCKING
|
||||
extension Trace.Entry {
|
||||
/// This implements `==` with wildcard matching.
|
||||
/// (`Entry` cannot conform to `Equatable`/`Hashable` this way because
|
||||
@@ -33,6 +34,7 @@ extension Trace.Entry {
|
||||
return true
|
||||
}
|
||||
}
|
||||
#endif // ENABLE_MOCKING
|
||||
|
||||
// To aid debugging, force failures to fatal error
|
||||
internal var forceFatalFailures = false
|
||||
@@ -81,6 +83,7 @@ extension TestCase {
|
||||
fail(message)
|
||||
}
|
||||
}
|
||||
#if ENABLE_MOCKING
|
||||
func expectMatch(
|
||||
_ expected: Trace.Entry?, _ actual: Trace.Entry?,
|
||||
_ message: String? = nil
|
||||
@@ -102,6 +105,7 @@ extension TestCase {
|
||||
fail(message)
|
||||
}
|
||||
}
|
||||
#endif // ENABLE_MOCKING
|
||||
func expectNil<T>(
|
||||
_ actual: T?,
|
||||
_ message: String? = nil
|
||||
@@ -142,6 +146,7 @@ extension TestCase {
|
||||
|
||||
}
|
||||
|
||||
#if ENABLE_MOCKING
|
||||
internal struct MockTestCase: TestCase {
|
||||
var file: StaticString
|
||||
var line: UInt
|
||||
@@ -241,6 +246,7 @@ internal struct MockTestCase: TestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif // ENABLE_MOCKING
|
||||
|
||||
internal func withWindowsPaths(enabled: Bool, _ body: () -> ()) {
|
||||
_withWindowsPaths(enabled: enabled, body)
|
||||
|
||||
Reference in new issue
Block a user