mirror of
https://github.com/apple/swift-system
synced 2026-11-26 04:57:54 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
669763cfd5 | ||
|
|
6fa5c8a6c1 | ||
|
|
2cae953181 | ||
|
|
7c6ad0fc39 | ||
|
|
13851a3421 | ||
|
|
8f8d80a4fe | ||
|
|
608a75b962 | ||
|
|
0dbbab4aa1 | ||
|
|
2eb655e0b7 | ||
|
|
07a3f090d7 | ||
|
|
dda2b535b7 | ||
|
|
9ddc89964b | ||
|
|
18a52c4729 | ||
|
|
1c1ad1d3f4 | ||
|
|
395a77f0aa | ||
|
|
b1618475cc | ||
|
|
890830fff1 | ||
|
|
a5f13c77b8 |
No files matched your search
@@ -1,5 +1,8 @@
|
|||||||
name: Pull request
|
name: Pull request
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
on:
|
on:
|
||||||
pull_request:
|
pull_request:
|
||||||
types: [opened, reopened, synchronize]
|
types: [opened, reopened, synchronize]
|
||||||
@@ -9,16 +12,50 @@ jobs:
|
|||||||
name: Test
|
name: Test
|
||||||
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main
|
uses: swiftlang/github-workflows/.github/workflows/swift_package_test.yml@main
|
||||||
with:
|
with:
|
||||||
linux_os_versions: '["jammy", "focal"]'
|
|
||||||
enable_macos_checks: true
|
|
||||||
# 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"},
|
|
||||||
]
|
|
||||||
swift_flags: "-Xbuild-tools-swiftc -DSYSTEM_CI"
|
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"},
|
||||||
|
]
|
||||||
|
enable_macos_checks: true
|
||||||
|
macos_exclude_xcode_versions: '[]'
|
||||||
|
enable_windows_checks: true
|
||||||
|
windows_exclude_swift_versions: |
|
||||||
|
[
|
||||||
|
{"swift_version": "5.9"},
|
||||||
|
{"swift_version": "5.10"}
|
||||||
|
]
|
||||||
|
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:
|
build-abi-stable:
|
||||||
name: Build ABI Stable
|
name: Build ABI Stable
|
||||||
@@ -29,13 +66,7 @@ jobs:
|
|||||||
enable_windows_checks: false
|
enable_windows_checks: false
|
||||||
# Only build
|
# Only build
|
||||||
macos_build_command: "xcrun swift build --build-tests"
|
macos_build_command: "xcrun swift build --build-tests"
|
||||||
# FIXME: https://github.com/swiftlang/github-workflows/pull/140
|
macos_exclude_xcode_versions: '[]'
|
||||||
# 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.
|
# Enable availability to match ABI stable verion of system.
|
||||||
swift_flags: "-Xbuild-tools-swiftc -DSYSTEM_CI -Xbuild-tools-swiftc -DSYSTEM_ABI_STABLE"
|
swift_flags: "-Xbuild-tools-swiftc -DSYSTEM_CI -Xbuild-tools-swiftc -DSYSTEM_ABI_STABLE"
|
||||||
|
|
||||||
|
|||||||
@@ -134,11 +134,13 @@ typedef struct __SWIFT_IORING_SQE_FALLBACK_STRUCT swift_io_uring_sqe;
|
|||||||
#define IORING_FEAT_RW_ATTR (1U << 16)
|
#define IORING_FEAT_RW_ATTR (1U << 16)
|
||||||
#define IORING_FEAT_NO_IOWAIT (1U << 17)
|
#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 uint8_t __u8;
|
||||||
typedef uint16_t __u16;
|
typedef uint16_t __u16;
|
||||||
typedef uint32_t __u32;
|
typedef uint32_t __u32;
|
||||||
typedef uint64_t __u64;
|
typedef uint64_t __u64;
|
||||||
typedef int32_t __s32;
|
typedef int32_t __s32;
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef __kernel_rwf_t
|
#ifndef __kernel_rwf_t
|
||||||
typedef int __kernel_rwf_t;
|
typedef int __kernel_rwf_t;
|
||||||
|
|||||||
@@ -160,7 +160,7 @@ public struct Errno: RawRepresentable, Error, Hashable, Codable {
|
|||||||
/// No child processes.
|
/// No child processes.
|
||||||
///
|
///
|
||||||
/// A `wait(2)` or `waitpid(2)` function was executed
|
/// A `wait(2)` or `waitpid(2)` function was executed
|
||||||
/// by a process that dosn't have any existing child processes
|
/// by a process that doesn't have any existing child processes
|
||||||
/// or whose child processes are all already being waited for.
|
/// or whose child processes are all already being waited for.
|
||||||
///
|
///
|
||||||
/// The corresponding C error is `ECHILD`.
|
/// The corresponding C error is `ECHILD`.
|
||||||
|
|||||||
@@ -517,7 +517,7 @@ fileprivate func HRESULT_CODE(_ hr: HRESULT) -> DWORD {
|
|||||||
|
|
||||||
@inline(__always)
|
@inline(__always)
|
||||||
fileprivate func HRESULT_FACILITY(_ hr: HRESULT) -> DWORD {
|
fileprivate func HRESULT_FACILITY(_ hr: HRESULT) -> DWORD {
|
||||||
DWORD(hr << 16) & 0x1fff
|
DWORD(hr >> 16) & 0x1fff
|
||||||
}
|
}
|
||||||
|
|
||||||
@inline(__always)
|
@inline(__always)
|
||||||
|
|||||||
@@ -388,7 +388,9 @@ extension IORing.Request {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@inline(__always) @inlinable
|
@inline(__always) @inlinable
|
||||||
internal consuming func makeRawRequest() -> RawIORequest {
|
internal consuming func makeRawRequest(
|
||||||
|
pathBuffers: PendingPathBuffers
|
||||||
|
) -> RawIORequest {
|
||||||
var request = RawIORequest()
|
var request = RawIORequest()
|
||||||
switch extractCore() {
|
switch extractCore() {
|
||||||
case .nop:
|
case .nop:
|
||||||
@@ -400,27 +402,19 @@ extension IORing.Request {
|
|||||||
request.operation = .openAt
|
request.operation = .openAt
|
||||||
request.fileDescriptor = atDirectory
|
request.fileDescriptor = atDirectory
|
||||||
request.rawValue.addr = UInt64(
|
request.rawValue.addr = UInt64(
|
||||||
UInt(
|
UInt(bitPattern: pathBuffers.pin(path)))
|
||||||
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.open_flags = UInt32(bitPattern: options.rawValue | mode.rawValue)
|
||||||
request.rawValue.len = permissions?.rawValue ?? 0
|
request.rawValue.len = permissions?.rawValue ?? 0
|
||||||
request.rawValue.file_index = UInt32(fileSlot.index + 1)
|
request.rawValue.file_index = UInt32(fileSlot.index + 1)
|
||||||
request.path = path
|
|
||||||
request.rawValue.user_data = context
|
request.rawValue.user_data = context
|
||||||
case .openat(
|
case .openat(
|
||||||
let atDirectory, let path, let mode, let options, let permissions, let context):
|
let atDirectory, let path, let mode, let options, let permissions, let context):
|
||||||
request.operation = .openAt
|
request.operation = .openAt
|
||||||
request.fileDescriptor = atDirectory
|
request.fileDescriptor = atDirectory
|
||||||
request.rawValue.addr = UInt64(
|
request.rawValue.addr = UInt64(
|
||||||
UInt(
|
UInt(bitPattern: pathBuffers.pin(path)))
|
||||||
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.open_flags = UInt32(bitPattern: options.rawValue | mode.rawValue)
|
||||||
request.rawValue.len = permissions?.rawValue ?? 0
|
request.rawValue.len = permissions?.rawValue ?? 0
|
||||||
request.path = path
|
|
||||||
request.rawValue.user_data = context
|
request.rawValue.user_data = context
|
||||||
case .write(let file, let buffer, let offset, let context):
|
case .write(let file, let buffer, let offset, let context):
|
||||||
request.operation = .writeFixed
|
request.operation = .writeFixed
|
||||||
@@ -466,12 +460,7 @@ extension IORing.Request {
|
|||||||
request.operation = .unlinkAt
|
request.operation = .unlinkAt
|
||||||
request.fileDescriptor = atDirectory
|
request.fileDescriptor = atDirectory
|
||||||
request.rawValue.addr = UInt64(
|
request.rawValue.addr = UInt64(
|
||||||
UInt(
|
UInt(bitPattern: pathBuffers.pin(path)))
|
||||||
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
|
request.rawValue.user_data = context
|
||||||
case .cancelContext(let flags, let targetContext):
|
case .cancelContext(let flags, let targetContext):
|
||||||
request.operation = .asyncCancel
|
request.operation = .asyncCancel
|
||||||
|
|||||||
@@ -41,6 +41,33 @@ 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
|
// all pointers in this struct reference kernel-visible memory
|
||||||
@usableFromInline struct SQRing: ~Copyable {
|
@usableFromInline struct SQRing: ~Copyable {
|
||||||
@usableFromInline let kernelHead: UnsafePointer<Atomic<UInt32>>
|
@usableFromInline let kernelHead: UnsafePointer<Atomic<UInt32>>
|
||||||
@@ -200,7 +227,14 @@ private func setUpRing(
|
|||||||
throw err
|
throw err
|
||||||
}
|
}
|
||||||
|
|
||||||
if params.features & IORing.Features.nonDroppingCompletions.rawValue == 0
|
// 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
|
||||||
{
|
{
|
||||||
close(ringDescriptor)
|
close(ringDescriptor)
|
||||||
throw Errno.invalidArgument
|
throw Errno.invalidArgument
|
||||||
@@ -324,6 +358,8 @@ public struct IORing: ~Copyable {
|
|||||||
@usableFromInline var _registeredFiles: [UInt32]
|
@usableFromInline var _registeredFiles: [UInt32]
|
||||||
@usableFromInline var _registeredBuffers: [iovec]
|
@usableFromInline var _registeredBuffers: [iovec]
|
||||||
|
|
||||||
|
@usableFromInline let _pendingPathBuffers: PendingPathBuffers
|
||||||
|
|
||||||
var features = Features(rawValue: 0)
|
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.
|
/// RegisteredResource is used via its typealiases, RegisteredFile and RegisteredBuffer. Registering file descriptors and buffers with the IORing allows for more efficient access to them.
|
||||||
@@ -444,6 +480,7 @@ public struct IORing: ~Copyable {
|
|||||||
self.completionRingSize = tmpCQSize
|
self.completionRingSize = tmpCQSize
|
||||||
self._registeredFiles = []
|
self._registeredFiles = []
|
||||||
self._registeredBuffers = []
|
self._registeredBuffers = []
|
||||||
|
self._pendingPathBuffers = PendingPathBuffers(reservedCapacity: Int(params.sq_entries))
|
||||||
self.submissionRing = submissionRing
|
self.submissionRing = submissionRing
|
||||||
self.completionRing = completionRing
|
self.completionRing = completionRing
|
||||||
self.submissionQueueEntries = submissionQueueEntries
|
self.submissionQueueEntries = submissionQueueEntries
|
||||||
@@ -765,6 +802,10 @@ public struct IORing: ~Copyable {
|
|||||||
@inlinable
|
@inlinable
|
||||||
public func submitPreparedRequests() throws(Errno) {
|
public func submitPreparedRequests() throws(Errno) {
|
||||||
try _submitRequests(ring: submissionRing, ringDescriptor: ringDescriptor)
|
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.
|
/// 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.
|
||||||
@@ -790,9 +831,14 @@ 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
|
/// Attempts to prepare an IO request for submission to the kernel. Returns false if no space is available to enqueue the request
|
||||||
@inlinable
|
@inlinable
|
||||||
public mutating func prepare(request: __owned Request) -> Bool {
|
public mutating func prepare(request: __owned Request) -> Bool {
|
||||||
var raw: RawIORequest? = request.makeRawRequest()
|
guard _getRemainingSubmissionQueueCapacity(ring: submissionRing) >= 1 else {
|
||||||
return _tryWriteRequest(
|
return false
|
||||||
|
}
|
||||||
|
var raw: RawIORequest? = request.makeRawRequest(pathBuffers: _pendingPathBuffers)
|
||||||
|
let ok = _tryWriteRequest(
|
||||||
raw.take()!, ring: &submissionRing, submissionQueueEntries: submissionQueueEntries)
|
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.
|
/// 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.
|
||||||
@@ -806,18 +852,20 @@ public struct IORing: ~Copyable {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
let last = linkedRequests.last!
|
let last = linkedRequests.last!
|
||||||
|
var allAdded = true
|
||||||
for req in linkedRequests.dropLast() {
|
for req in linkedRequests.dropLast() {
|
||||||
var raw = req.makeRawRequest()
|
var raw = req.makeRawRequest(pathBuffers: _pendingPathBuffers)
|
||||||
raw.linkToNextRequest()
|
raw.linkToNextRequest()
|
||||||
let successfullyAdded = _tryWriteRequest(
|
let successfullyAdded = _tryWriteRequest(
|
||||||
raw, ring: &submissionRing, submissionQueueEntries: submissionQueueEntries)
|
raw, ring: &submissionRing, submissionQueueEntries: submissionQueueEntries)
|
||||||
assert(successfullyAdded)
|
assert(successfullyAdded)
|
||||||
|
allAdded = allAdded && successfullyAdded
|
||||||
}
|
}
|
||||||
let successfullyAdded = _tryWriteRequest(
|
let successfullyAdded = _tryWriteRequest(
|
||||||
last.makeRawRequest(), ring: &submissionRing,
|
last.makeRawRequest(pathBuffers: _pendingPathBuffers), ring: &submissionRing,
|
||||||
submissionQueueEntries: submissionQueueEntries)
|
submissionQueueEntries: submissionQueueEntries)
|
||||||
assert(successfullyAdded)
|
assert(successfullyAdded)
|
||||||
return true
|
return allAdded && successfullyAdded
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Prepares a sequence of requests for submission to the ring. Returns false if the submission queue doesn't have enough available space.
|
/// Prepares a sequence of requests for submission to the ring. Returns false if the submission queue doesn't have enough available space.
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ internal struct RawIORequest: ~Copyable {
|
|||||||
// swift_io_uring_sqe is a typedef of io_uring_sqe on platforms where
|
// swift_io_uring_sqe is a typedef of io_uring_sqe on platforms where
|
||||||
// IORing is supported (currently requires kernel version >= 5.15).
|
// IORing is supported (currently requires kernel version >= 5.15).
|
||||||
@usableFromInline var rawValue: swift_io_uring_sqe
|
@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() {
|
@inlinable public init() {
|
||||||
self.rawValue = swift_io_uring_sqe()
|
self.rawValue = swift_io_uring_sqe()
|
||||||
|
|||||||
@@ -135,7 +135,11 @@ internal func dup(_ fd: Int32) -> Int32 {
|
|||||||
|
|
||||||
@inline(__always)
|
@inline(__always)
|
||||||
internal func dup2(_ fd: Int32, _ fd2: Int32) -> Int32 {
|
internal func dup2(_ fd: Int32, _ fd2: Int32) -> Int32 {
|
||||||
_dup2(fd, fd2)
|
// _dup2 returns 0 to indicate success.
|
||||||
|
if _dup2(fd, fd2) == 0 {
|
||||||
|
return fd2
|
||||||
|
}
|
||||||
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
@inline(__always)
|
@inline(__always)
|
||||||
@@ -145,6 +149,12 @@ internal func pread(
|
|||||||
let handle: intptr_t = _get_osfhandle(fd)
|
let handle: intptr_t = _get_osfhandle(fd)
|
||||||
if handle == /* INVALID_HANDLE_VALUE */ -1 { ucrt._set_errno(EBADF); return -1 }
|
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
|
// NOTE: this is a non-owning handle, do *not* call CloseHandle on it
|
||||||
let hFile: HANDLE = HANDLE(bitPattern: handle)!
|
let hFile: HANDLE = HANDLE(bitPattern: handle)!
|
||||||
|
|
||||||
@@ -167,6 +177,12 @@ internal func pwrite(
|
|||||||
let handle: intptr_t = _get_osfhandle(fd)
|
let handle: intptr_t = _get_osfhandle(fd)
|
||||||
if handle == /* INVALID_HANDLE_VALUE */ -1 { ucrt._set_errno(EBADF); return -1 }
|
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
|
// NOTE: this is a non-owning handle, do *not* call CloseHandle on it
|
||||||
let hFile: HANDLE = HANDLE(bitPattern: handle)!
|
let hFile: HANDLE = HANDLE(bitPattern: handle)!
|
||||||
|
|
||||||
|
|||||||
@@ -242,6 +242,53 @@ 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)
|
#endif // os(Windows)
|
||||||
@@ -22,7 +22,8 @@ func requestBytes(_ request: consuming RawIORequest) -> [UInt8] {
|
|||||||
// which are known to work correctly.
|
// which are known to work correctly.
|
||||||
final class IORequestTests: XCTestCase {
|
final class IORequestTests: XCTestCase {
|
||||||
func testNop() {
|
func testNop() {
|
||||||
let req = IORing.Request.nop().makeRawRequest()
|
let req = IORing.Request.nop().makeRawRequest(
|
||||||
|
pathBuffers: PendingPathBuffers(reservedCapacity: 0))
|
||||||
let sourceBytes = requestBytes(req)
|
let sourceBytes = requestBytes(req)
|
||||||
// convenient property of nop: it's all zeros!
|
// convenient property of nop: it's all zeros!
|
||||||
// for some unknown reason, liburing sets the fd field to -1.
|
// for some unknown reason, liburing sets the fd field to -1.
|
||||||
|
|||||||
@@ -127,6 +127,81 @@ final class IORingTests: XCTestCase {
|
|||||||
efdBuf.deallocate()
|
efdBuf.deallocate()
|
||||||
rawBuffer.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 // os(Linux)
|
||||||
#endif // compiler(>=6.2) && $Lifetimes
|
#endif // compiler(>=6.2) && $Lifetimes
|
||||||
Reference in new issue
Block a user