From 0eab74045014e5dafef4a620b11aa039a5370727 Mon Sep 17 00:00:00 2001 From: RKS Date: Mon, 5 Oct 2026 22:39:32 -0400 Subject: [PATCH 1/4] [IO_URING] Accept immutable buffers for unregistered writes --- Sources/System/IORing/IORequest.swift | 20 ++++++++++ Tests/SystemTests/IORequestTests.swift | 52 ++++++++++++++++++++++++++ Tests/SystemTests/IORingTests.swift | 24 ++++++++++++ 3 files changed, 96 insertions(+) diff --git a/Sources/System/IORing/IORequest.swift b/Sources/System/IORing/IORequest.swift index a5c4c40..4414cad 100644 --- a/Sources/System/IORing/IORequest.swift +++ b/Sources/System/IORing/IORequest.swift @@ -385,6 +385,26 @@ extension IORing.Request { ) } + @inlinable public static func write( + _ buffer: UnsafeRawBufferPointer, + into file: IORing.RegisteredFile, + at offset: UInt64 = 0, + context: UInt64 = 0 + ) -> IORing.Request { + // The shared request encoder uses a mutable pointer, but writes only read the buffer. + write(UnsafeMutableRawBufferPointer(mutating: buffer), into: file, at: offset, context: context) + } + + @inlinable public static func write( + _ buffer: UnsafeRawBufferPointer, + into file: FileDescriptor, + at offset: UInt64 = 0, + context: UInt64 = 0 + ) -> IORing.Request { + // The shared request encoder uses a mutable pointer, but writes only read the buffer. + write(UnsafeMutableRawBufferPointer(mutating: buffer), into: file, at: offset, context: context) + } + @inlinable public static func close( _ file: FileDescriptor, context: UInt64 = 0 diff --git a/Tests/SystemTests/IORequestTests.swift b/Tests/SystemTests/IORequestTests.swift index 773a90c..2e1c98e 100644 --- a/Tests/SystemTests/IORequestTests.swift +++ b/Tests/SystemTests/IORequestTests.swift @@ -2,6 +2,7 @@ #if os(Linux) import XCTest +import CSystem #if SYSTEM_PACKAGE @testable import SystemPackage @@ -21,6 +22,57 @@ func requestBytes(_ request: consuming RawIORequest) -> [UInt8] { // that were generated with liburing or manually written out, // which are known to work correctly. final class IORequestTests: XCTestCase { + func testWriteUnregisteredImmutableBuffer() { + let bytes: [UInt8] = [1, 2, 3] + bytes.withUnsafeBytes { buffer in + let request = IORing.Request.write( + buffer, into: FileDescriptor(rawValue: 7), at: 17, context: 42 + ).makeRawRequest(pathBuffers: PendingPathBuffers(reservedCapacity: 0)) + XCTAssertEqual(request.rawValue.opcode, 23) + XCTAssertEqual(request.rawValue.fd, 7) + XCTAssertEqual(request.rawValue.flags, 0) + XCTAssertEqual(request.rawValue.addr, UInt64(UInt(bitPattern: buffer.baseAddress))) + XCTAssertEqual(request.rawValue.len, 3) + XCTAssertEqual(request.rawValue.off, 17) + XCTAssertEqual(request.rawValue.user_data, 42) + } + } + + func testWriteUnregisteredImmutableBufferToRegisteredFile() { + let bytes: [UInt8] = [1, 2, 3] + bytes.withUnsafeBytes { buffer in + let file = IORing.RegisteredFile(resource: 7, index: 3) + let request = IORing.Request.write( + buffer, into: file, at: 17, context: 42 + ).makeRawRequest(pathBuffers: PendingPathBuffers(reservedCapacity: 0)) + XCTAssertEqual(request.rawValue.opcode, 23) + XCTAssertEqual(request.rawValue.fd, 3) + XCTAssertEqual(request.rawValue.flags, 1) + XCTAssertEqual(request.rawValue.addr, UInt64(UInt(bitPattern: buffer.baseAddress))) + XCTAssertEqual(request.rawValue.len, 3) + XCTAssertEqual(request.rawValue.off, 17) + XCTAssertEqual(request.rawValue.user_data, 42) + } + } + + func testMutableWriteFunctionReferences() { + let write: (UnsafeMutableRawBufferPointer, FileDescriptor, UInt64, UInt64) -> IORing.Request = + IORing.Request.write + let writeRegistered: (UnsafeMutableRawBufferPointer, IORing.RegisteredFile, UInt64, UInt64) -> IORing.Request = + IORing.Request.write + var bytes: [UInt8] = [1, 2, 3] + bytes.withUnsafeMutableBytes { buffer in + let request = write(buffer, FileDescriptor(rawValue: 7), 17, 42) + .makeRawRequest(pathBuffers: PendingPathBuffers(reservedCapacity: 0)) + let registered = writeRegistered(buffer, IORing.RegisteredFile(resource: 7, index: 3), 17, 42) + .makeRawRequest(pathBuffers: PendingPathBuffers(reservedCapacity: 0)) + XCTAssertEqual(request.rawValue.fd, 7) + XCTAssertEqual(request.rawValue.len, 3) + XCTAssertEqual(registered.rawValue.fd, 3) + XCTAssertEqual(registered.rawValue.flags, 1) + } + } + func testNop() { let req = IORing.Request.nop().makeRawRequest( pathBuffers: PendingPathBuffers(reservedCapacity: 0)) diff --git a/Tests/SystemTests/IORingTests.swift b/Tests/SystemTests/IORingTests.swift index 799e591..dd0d1b8 100644 --- a/Tests/SystemTests/IORingTests.swift +++ b/Tests/SystemTests/IORingTests.swift @@ -125,6 +125,30 @@ final class IORingTests: XCTestCase { return ring } + func testWriteImmutableUnregisteredBuffer() throws { + try XCTSkipIf(!uringEnabled, failureMessage) + var ring = try IORing(queueDepth: 1) + let (readFD, writeFD) = try FileDescriptor.pipe() + defer { + try? readFD.close() + try? writeFD.close() + } + let bytes: [UInt8] = [1, 2, 3] + try bytes.withUnsafeBytes { buffer in + XCTAssertTrue(try ring.submit(linkedRequests: .write(buffer, into: writeFD, context: 42))) + let completion = try ring.blockingConsumeCompletion(timeout: .seconds(1)) + XCTAssertEqual(completion.context, 42) + guard completion.result == 3 else { + XCTFail("Expected three bytes written, got \(completion.result)") + return + } + var received = [UInt8](repeating: 0, count: 3) + let count = try received.withUnsafeMutableBytes { try readFD.read(into: $0) } + XCTAssertEqual(count, 3) + XCTAssertEqual(received, bytes) + } + } + func testUndersizedSubmissionQueue() throws { try XCTSkipIf(!uringEnabled, failureMessage) var ring: IORing = try IORing(queueDepth: 1) From b832f4134a3fdb2b6c6be5c1e854aac22ec661e8 Mon Sep 17 00:00:00 2001 From: RKS Date: Mon, 5 Oct 2026 22:48:56 -0400 Subject: [PATCH 2/4] [IO_URING] Keep test ring cleanup within buffer lifetime --- Tests/SystemTests/IORingTests.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Tests/SystemTests/IORingTests.swift b/Tests/SystemTests/IORingTests.swift index dd0d1b8..cc1120a 100644 --- a/Tests/SystemTests/IORingTests.swift +++ b/Tests/SystemTests/IORingTests.swift @@ -127,14 +127,14 @@ final class IORingTests: XCTestCase { func testWriteImmutableUnregisteredBuffer() throws { try XCTSkipIf(!uringEnabled, failureMessage) - var ring = try IORing(queueDepth: 1) - let (readFD, writeFD) = try FileDescriptor.pipe() - defer { - try? readFD.close() - try? writeFD.close() - } let bytes: [UInt8] = [1, 2, 3] try bytes.withUnsafeBytes { buffer in + var ring = try IORing(queueDepth: 1) + let (readFD, writeFD) = try FileDescriptor.pipe() + defer { + try? readFD.close() + try? writeFD.close() + } XCTAssertTrue(try ring.submit(linkedRequests: .write(buffer, into: writeFD, context: 42))) let completion = try ring.blockingConsumeCompletion(timeout: .seconds(1)) XCTAssertEqual(completion.context, 42) From 92481cc05652e561623eb32c1411d5d2e4b7fd4d Mon Sep 17 00:00:00 2001 From: RKS Date: Tue, 6 Oct 2026 12:39:41 -0400 Subject: [PATCH 3/4] [IO_URING] Document immutable write requests --- Sources/System/IORing/IORequest.swift | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Sources/System/IORing/IORequest.swift b/Sources/System/IORing/IORequest.swift index 49a98cc..7c25c56 100644 --- a/Sources/System/IORing/IORequest.swift +++ b/Sources/System/IORing/IORequest.swift @@ -389,6 +389,21 @@ extension IORing.Request { ) } + /// Creates a request to write an unregistered buffer to a registered file. + /// + /// The request does not copy the buffer's contents. Keep its storage alive + /// and unchanged until the operation completes. The write only reads from + /// the buffer; it does not modify its contents. + /// + /// - Parameters: + /// - buffer: The bytes to write. + /// - file: The registered file to write to. + /// - offset: The byte offset in the file at which to begin writing. + /// Ignored for non-seekable files. Defaults to `0`. + /// - context: An application-specific value passed through to the + /// completion event. Defaults to `0`. + /// - Returns: An I/O ring write request. A successful completion's result + /// is the number of bytes written, which may be less than the buffer's count. @inlinable public static func write( _ buffer: UnsafeRawBufferPointer, into file: IORing.RegisteredFile, @@ -399,6 +414,21 @@ extension IORing.Request { write(UnsafeMutableRawBufferPointer(mutating: buffer), into: file, at: offset, context: context) } + /// Creates a request to write an unregistered buffer to a file descriptor. + /// + /// The request does not copy the buffer's contents. Keep its storage alive + /// and unchanged until the operation completes. The write only reads from + /// the buffer; it does not modify its contents. + /// + /// - Parameters: + /// - buffer: The bytes to write. + /// - file: The file descriptor to write to. + /// - offset: The byte offset in the file at which to begin writing. + /// Ignored for non-seekable files. Defaults to `0`. + /// - context: An application-specific value passed through to the + /// completion event. Defaults to `0`. + /// - Returns: An I/O ring write request. A successful completion's result + /// is the number of bytes written, which may be less than the buffer's count. @inlinable public static func write( _ buffer: UnsafeRawBufferPointer, into file: FileDescriptor, From a51425f6d82fc84ce0b1f580e73d62363916a5aa Mon Sep 17 00:00:00 2001 From: RKS Date: Fri, 16 Oct 2026 22:42:35 -0400 Subject: [PATCH 4/4] Disfavor deprecated mutable write overloads --- Sources/System/IORing/IORequest.swift | 12 ++++++++++-- Tests/SystemTests/IORequestTests.swift | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Sources/System/IORing/IORequest.swift b/Sources/System/IORing/IORequest.swift index 7c25c56..ac46c3f 100644 --- a/Sources/System/IORing/IORequest.swift +++ b/Sources/System/IORing/IORequest.swift @@ -368,6 +368,8 @@ extension IORing.Request { .init(core: .write(file: file, buffer: buffer, offset: offset, context: context)) } + @available(*, deprecated, message: "Use the overload accepting UnsafeRawBufferPointer instead.") + @_disfavoredOverload @inlinable public static func write( _ buffer: UnsafeMutableRawBufferPointer, into file: IORing.RegisteredFile, @@ -378,6 +380,8 @@ extension IORing.Request { file: file, buffer: buffer, offset: offset, context: context)) } + @available(*, deprecated, message: "Use the overload accepting UnsafeRawBufferPointer instead.") + @_disfavoredOverload @inlinable public static func write( _ buffer: UnsafeMutableRawBufferPointer, into file: FileDescriptor, @@ -411,7 +415,9 @@ extension IORing.Request { context: UInt64 = 0 ) -> IORing.Request { // The shared request encoder uses a mutable pointer, but writes only read the buffer. - write(UnsafeMutableRawBufferPointer(mutating: buffer), into: file, at: offset, context: context) + .init(core: .writeUnregisteredSlot( + file: file, buffer: UnsafeMutableRawBufferPointer(mutating: buffer), offset: offset, context: context + )) } /// Creates a request to write an unregistered buffer to a file descriptor. @@ -436,7 +442,9 @@ extension IORing.Request { context: UInt64 = 0 ) -> IORing.Request { // The shared request encoder uses a mutable pointer, but writes only read the buffer. - write(UnsafeMutableRawBufferPointer(mutating: buffer), into: file, at: offset, context: context) + .init(core: .writeUnregistered( + file: file, buffer: UnsafeMutableRawBufferPointer(mutating: buffer), offset: offset, context: context + )) } @inlinable public static func close( diff --git a/Tests/SystemTests/IORequestTests.swift b/Tests/SystemTests/IORequestTests.swift index a1f5d41..c7f28bb 100644 --- a/Tests/SystemTests/IORequestTests.swift +++ b/Tests/SystemTests/IORequestTests.swift @@ -56,7 +56,7 @@ final class IORequestTests: XCTestCase { } func testInferredRebasedWriteBuffer() { - var bytes: [UInt8] = [1, 2, 3]; + var bytes: [UInt8] = [1, 2, 3] bytes.withUnsafeMutableBytes { buffer in let written = 1 let file = FileDescriptor(rawValue: 7)