diff --git a/Sources/System/MachPort.swift b/Sources/System/MachPort.swift index 04d2cc5..223757b 100644 --- a/Sources/System/MachPort.swift +++ b/Sources/System/MachPort.swift @@ -7,7 +7,7 @@ See https://swift.org/LICENSE.txt for license information */ -#if false && swift(>=5.8) && $MoveOnly && (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) +#if swift(>=5.9) && (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) import Darwin.Mach @@ -48,11 +48,13 @@ public enum Mach { /// /// This initializer makes a syscall to guard the right. public init(name: mach_port_name_t) { - precondition(name != mach_port_name_t(MACH_PORT_NULL), "Mach.Port cannot be initialized with MACH_PORT_NULL") + precondition(name != mach_port_name_t(MACH_PORT_NULL), + "Mach.Port cannot be initialized with MACH_PORT_NULL") self._name = name if RightType.self == ReceiveRight.self { - precondition(name != 0xFFFFFFFF /* MACH_PORT_DEAD */, "Receive rights cannot be dead names") + precondition(name != 0xFFFFFFFF /* MACH_PORT_DEAD */, + "Receive rights cannot be dead names") let secret = mach_port_context_t(arc4random()) _machPrecondition(mach_port_guard(mach_task_self_, name, secret, 0)) @@ -81,17 +83,18 @@ public enum Mach { } deinit { - if _name == 0xFFFFFFFF /* MACH_PORT_DEAD */ { - precondition(RightType.self != ReceiveRight.self, "Receive rights cannot be dead names") - _machPrecondition(mach_port_mod_refs(mach_task_self_, _name, MACH_PORT_RIGHT_DEAD_NAME, -1)) + if RightType.self == ReceiveRight.self { + precondition(_name != 0xFFFFFFFF /* MACH_PORT_DEAD */, + "Receive rights cannot be dead names") + _machPrecondition( + mach_port_destruct(mach_task_self_, _name, 0, _context) + ) } else { - if RightType.self == ReceiveRight.self { - _machPrecondition(mach_port_destruct(mach_task_self_, _name, -1, _context)) - } else if RightType.self == SendRight.self { - _machPrecondition(mach_port_mod_refs(mach_task_self_, _name, MACH_PORT_RIGHT_SEND, -1)) - } else if RightType.self == SendOnceRight.self { - _machPrecondition(mach_port_mod_refs(mach_task_self_, _name, MACH_PORT_RIGHT_SEND_ONCE, -1)) - } + assert( + RightType.self == SendRight.self || + RightType.self == SendOnceRight.self + ) + _machPrecondition(mach_port_deallocate(mach_task_self_, _name)) } } } @@ -135,6 +138,8 @@ extension Mach.Port where RightType == Mach.ReceiveRight { /// The underlying port right will be automatically deallocated when /// the Mach.Port object is destroyed. public init(name: mach_port_name_t, context: mach_port_context_t) { + precondition(name != mach_port_name_t(MACH_PORT_NULL), + "Mach.Port cannot be initialized with MACH_PORT_NULL") self._name = name self._context = context } @@ -147,9 +152,9 @@ extension Mach.Port where RightType == Mach.ReceiveRight { @inlinable public init() { var storage: mach_port_name_t = mach_port_name_t(MACH_PORT_NULL) - withUnsafeMutablePointer(to: &storage) { storage in - _machPrecondition(mach_port_allocate(mach_task_self_, MACH_PORT_RIGHT_RECEIVE, storage)) - } + _machPrecondition( + mach_port_allocate(mach_task_self_, MACH_PORT_RIGHT_RECEIVE, &storage) + ) // name-only init will guard ReceiveRights self.init(name: storage) @@ -167,9 +172,11 @@ extension Mach.Port where RightType == Mach.ReceiveRight { /// After this function completes, the Mach.Port is destroyed and no longer /// usable. @inlinable - public __consuming func relinquish( + public consuming func relinquish( ) -> (name: mach_port_name_t, context: mach_port_context_t) { - return (name: _name, context: _context) + let destructured = (name: _name, context: _context) + discard self + return destructured } /// Remove guard and transfer ownership of the underlying port right to @@ -187,9 +194,10 @@ extension Mach.Port where RightType == Mach.ReceiveRight { /// Mach.ReceiveRights. Use relinquish() to avoid the syscall and extract /// the context value along with the port name. @inlinable - public __consuming func unguardAndRelinquish() -> mach_port_name_t { - _machPrecondition(mach_port_unguard(mach_task_self_, _name, _context)) - return _name + public consuming func unguardAndRelinquish() -> mach_port_name_t { + let (name, context) = self.relinquish() + _machPrecondition(mach_port_unguard(mach_task_self_, name, context)) + return name } /// Borrow access to the port name in a block that can perform @@ -220,17 +228,15 @@ extension Mach.Port where RightType == Mach.ReceiveRight { var newRight: mach_port_name_t = mach_port_name_t(MACH_PORT_NULL) var newRightType: mach_port_type_t = MACH_PORT_TYPE_NONE - withUnsafeMutablePointer(to: &newRight) { newRight in - withUnsafeMutablePointer(to: &newRightType) { newRightType in - _machPrecondition( - mach_port_extract_right(mach_task_self_, - _name, - mach_msg_type_name_t(MACH_MSG_TYPE_MAKE_SEND_ONCE), - newRight, - newRightType) - ) - } - } + _machPrecondition( + mach_port_extract_right( + mach_task_self_, + _name, + mach_msg_type_name_t(MACH_MSG_TYPE_MAKE_SEND_ONCE), + &newRight, + &newRightType + ) + ) // The value of newRight is validated by the Mach.Port initializer precondition(newRightType == MACH_MSG_TYPE_MOVE_SEND_ONCE) @@ -249,7 +255,11 @@ extension Mach.Port where RightType == Mach.ReceiveRight { let how = MACH_MSG_TYPE_MAKE_SEND // name is the same because send and recv rights are coalesced - _machPrecondition(mach_port_insert_right(mach_task_self_, _name, _name, mach_msg_type_name_t(how))) + _machPrecondition( + mach_port_insert_right( + mach_task_self_, _name, _name, mach_msg_type_name_t(how) + ) + ) return Mach.Port(name: _name) } @@ -261,11 +271,19 @@ extension Mach.Port where RightType == Mach.ReceiveRight { public var makeSendCount: mach_port_mscount_t { get { var status: mach_port_status = mach_port_status() - var size: mach_msg_type_number_t = mach_msg_type_number_t(MemoryLayout.size / MemoryLayout.size) - withUnsafeMutablePointer(to: &size) { size in - withUnsafeMutablePointer(to: &status) { status in - let info = UnsafeMutableRawPointer(status).bindMemory(to: integer_t.self, capacity: 1) - _machPrecondition(mach_port_get_attributes(mach_task_self_, _name, MACH_PORT_RECEIVE_STATUS, info, size)) + var size = mach_msg_type_number_t( + MemoryLayout.size / MemoryLayout.size + ) + + withUnsafeMutablePointer(to: &status) { + let status = UnsafeMutableBufferPointer(start: $0, count: 1) + status.withMemoryRebound(to: integer_t.self) { + let info = $0.baseAddress + _machPrecondition( + mach_port_get_attributes( + mach_task_self_, _name, MACH_PORT_RECEIVE_STATUS, info, &size + ) + ) } } return status.mps_mscount @@ -288,8 +306,10 @@ extension Mach.Port where RightType == Mach.SendRight { /// After this function completes, the Mach.Port is destroyed and no longer /// usable. @inlinable - public __consuming func relinquish() -> mach_port_name_t { - return _name + public consuming func relinquish() -> mach_port_name_t { + let name = _name + discard self + return name } /// Create another send right from a given send right. @@ -304,8 +324,10 @@ extension Mach.Port where RightType == Mach.SendRight { let how = MACH_MSG_TYPE_COPY_SEND // name is the same because send rights are coalesced - let kr = mach_port_insert_right(mach_task_self_, _name, _name, mach_msg_type_name_t(how)) - if kr == KERN_INVALID_CAPABILITY { + let kr = mach_port_insert_right( + mach_task_self_, _name, _name, mach_msg_type_name_t(how) + ) + if kr == KERN_INVALID_NAME || kr == KERN_INVALID_CAPABILITY { throw Mach.PortRightError.deadName } _machPrecondition(kr) @@ -326,8 +348,10 @@ extension Mach.Port where RightType == Mach.SendOnceRight { /// After this function completes, the Mach.Port is destroyed and no longer /// usable. @inlinable - public __consuming func relinquish() -> mach_port_name_t { - return _name + public consuming func relinquish() -> mach_port_name_t { + let name = _name + discard self + return name } } diff --git a/Tests/SystemTests/MachPortTests.swift b/Tests/SystemTests/MachPortTests.swift index 31a654c..07b7410 100644 --- a/Tests/SystemTests/MachPortTests.swift +++ b/Tests/SystemTests/MachPortTests.swift @@ -7,7 +7,7 @@ See https://swift.org/LICENSE.txt for license information */ -#if false && swift(>=5.8) && $MoveOnly && (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) +#if swift(>=5.9) && (os(macOS) || os(iOS) || os(watchOS) || os(tvOS)) import XCTest import Darwin.Mach @@ -20,33 +20,37 @@ import System final class MachPortTests: XCTestCase { func refCountForMachPortName(name:mach_port_name_t, kind:mach_port_right_t) -> mach_port_urefs_t { - var refCount:mach_port_urefs_t = 0 - withUnsafeMutablePointer(to: &refCount) { refCount in - let kr = mach_port_get_refs(mach_task_self_, name, kind, refCount) - assert(kr == KERN_SUCCESS) + var refCount:mach_port_urefs_t = .max + let kr = mach_port_get_refs(mach_task_self_, name, kind, &refCount) + if kr == KERN_INVALID_NAME { + refCount = 0 + } else { + XCTAssertEqual(kr, KERN_SUCCESS) } return refCount } func scopedReceiveRight(name:mach_port_name_t) -> mach_port_urefs_t { - _ = Mach.Port(name:name) // this should automatically deallocate when going out of scope + let right = Mach.Port(name:name) // this should automatically deallocate when going out of scope + defer { _ = right } return refCountForMachPortName(name:name, kind:MACH_PORT_RIGHT_RECEIVE) } - func testRecieveRightDeallocation() throws { - var name:mach_port_name_t = 0 // Never read - withUnsafeMutablePointer(to:&name) { name in - let kr = mach_port_allocate(mach_task_self_, MACH_PORT_RIGHT_RECEIVE, name) - assert(kr == KERN_SUCCESS) - } + func testReceiveRightDeallocation() throws { + var name: mach_port_name_t = 0xFFFFFFFF + let kr = mach_port_allocate(mach_task_self_, MACH_PORT_RIGHT_RECEIVE, &name) + XCTAssertEqual(kr, KERN_SUCCESS) - XCTAssert(name != 0xFFFFFFFF) + XCTAssertNotEqual(name, 0xFFFFFFFF) - let one = scopedReceiveRight(name:name) - let zero = refCountForMachPortName(name:name, kind: MACH_PORT_RIGHT_RECEIVE) + let originalCount = refCountForMachPortName(name: name, kind: MACH_PORT_RIGHT_RECEIVE) + XCTAssertEqual(originalCount, 1) - XCTAssert(one == 1); - XCTAssert(zero == 0); + let incrementedCount = scopedReceiveRight(name:name) + XCTAssertEqual(incrementedCount, 1); + + let deallocated = refCountForMachPortName(name:name, kind: MACH_PORT_RIGHT_RECEIVE) + XCTAssertEqual(deallocated, 0); } func consumeSendRightAutomatically(name:mach_port_name_t) -> mach_port_urefs_t { @@ -61,11 +65,11 @@ final class MachPortTests: XCTestCase { let recv = Mach.Port() recv.withBorrowedName { name in let kr = mach_port_insert_right(mach_task_self_, name, name, mach_msg_type_name_t(MACH_MSG_TYPE_MAKE_SEND)) - XCTAssert(kr == KERN_SUCCESS) + XCTAssertEqual(kr, KERN_SUCCESS) let one = consumeSendRightAutomatically(name:name) - XCTAssert(one == 1); + XCTAssertEqual(one, 1); let zero = refCountForMachPortName(name:name, kind:MACH_PORT_RIGHT_SEND) - XCTAssert(zero == 0); + XCTAssertEqual(zero, 0); } } @@ -77,29 +81,70 @@ final class MachPortTests: XCTestCase { let one = send.withBorrowedName { name in return self.refCountForMachPortName(name:name, kind:MACH_PORT_RIGHT_SEND) } - XCTAssert(one == 1) + XCTAssertEqual(one, 1) return send.relinquish() })() let stillOne = refCountForMachPortName(name:name, kind:MACH_PORT_RIGHT_SEND) - XCTAssert(stillOne == 1) + XCTAssertEqual(stillOne, 1) + + recv.withBorrowedName { + let alsoOne = refCountForMachPortName(name: $0, kind: MACH_PORT_RIGHT_RECEIVE) + XCTAssertEqual(alsoOne, 1) + } + } + + func testSendOnceRightRelinquishment() throws { + let recv = Mach.Port() + + let name = ({ + let send = recv.makeSendOnceRight() + let one = send.withBorrowedName { name in + return self.refCountForMachPortName(name: name, kind: MACH_PORT_RIGHT_SEND_ONCE) + } + XCTAssertEqual(one, 1) + + return send.relinquish() + })() + + let stillOne = refCountForMachPortName(name: name, kind: MACH_PORT_RIGHT_SEND_ONCE) + XCTAssertEqual(stillOne, 1) + + recv.withBorrowedName { + let alsoOne = refCountForMachPortName(name: $0, kind: MACH_PORT_RIGHT_RECEIVE) + XCTAssertEqual(alsoOne, 1) + } + } + + func testReceiveRightRelinquishment() throws { + let recv = Mach.Port() + + let one = recv.withBorrowedName { + self.refCountForMachPortName(name: $0, kind: MACH_PORT_RIGHT_RECEIVE) + } + XCTAssertEqual(one, 1) + + let name = recv.unguardAndRelinquish() + + let stillOne = refCountForMachPortName(name: name, kind: MACH_PORT_RIGHT_RECEIVE) + XCTAssertEqual(stillOne, 1) } func testMakeSendCountSettable() throws { var recv = Mach.Port() - XCTAssert(recv.makeSendCount == 0) + XCTAssertEqual(recv.makeSendCount, 0) recv.makeSendCount = 7 - XCTAssert(recv.makeSendCount == 7) + XCTAssertEqual(recv.makeSendCount, 7) } func makeSendRight() throws -> Mach.Port { let recv = Mach.Port() let zero = recv.makeSendCount - XCTAssert(zero == 0) + XCTAssertEqual(zero, 0) let send = recv.makeSendRight() let one = recv.makeSendCount - XCTAssert(one == 1) + XCTAssertEqual(one, 1) return send } @@ -110,10 +155,10 @@ final class MachPortTests: XCTestCase { func testMakeSendOnceDoesntIncrementMakeSendCount() throws { let recv = Mach.Port() let zero = recv.makeSendCount - XCTAssert(zero == 0) + XCTAssertEqual(zero, 0) _ = recv.makeSendOnceRight() let same = recv.makeSendCount - XCTAssert(same == zero) + XCTAssertEqual(same, zero) } func testMakeSendOnceIsUnique() throws { @@ -121,8 +166,7 @@ final class MachPortTests: XCTestCase { let once = recv.makeSendOnceRight() recv.withBorrowedName { rname in once.withBorrowedName { oname in - print(oname, rname) - XCTAssert(oname != rname) + XCTAssertNotEqual(oname, rname) } } } @@ -130,15 +174,68 @@ final class MachPortTests: XCTestCase { func testCopySend() throws { let recv = Mach.Port() let zero = recv.makeSendCount - XCTAssert(zero == 0) + XCTAssertEqual(zero, 0) let send = recv.makeSendRight() let one = recv.makeSendCount - XCTAssert(one == 1) + XCTAssertEqual(one, 1) _ = try send.copySendRight() let same = recv.makeSendCount - XCTAssert(same == one) + XCTAssertEqual(same, one) } + + func testCopyDeadName() throws { + let recv = Mach.Port() + let send = recv.makeSendRight() + _ = consume recv // and turn `send` into a dead name + XCTAssertThrowsError( + _ = try send.copySendRight(), + "Copying a dead name should throw" + ) { error in + XCTAssertEqual( + error as! Mach.PortRightError, Mach.PortRightError.deadName + ) + } + } + + func testCopyDeadName2() throws { + let send = Mach.Port(name: 0xffffffff) + XCTAssertThrowsError( + _ = try send.copySendRight(), + "Copying a dead name should throw" + ) { error in + XCTAssertEqual( + error as! Mach.PortRightError, Mach.PortRightError.deadName + ) + } + } + + func testMakeReceiveRightFromExistingName() throws { + var name = mach_port_name_t(MACH_PORT_NULL) + var kr = mach_port_allocate(mach_task_self_, MACH_PORT_RIGHT_RECEIVE, &name) + XCTAssertEqual(kr, KERN_SUCCESS) + XCTAssertNotEqual(name, mach_port_name_t(MACH_PORT_NULL)) + let context = mach_port_context_t(arc4random()) + kr = mach_port_guard(mach_task_self_, name, context, 0) + XCTAssertEqual(kr, KERN_SUCCESS) + + let right = Mach.Port(name: name, context: context) + right.withBorrowedName { + XCTAssertEqual(name, $0) + XCTAssertEqual(context, $1) + } + } + + func testDeinitDeadSendRights() throws { + let recv = Mach.Port() + let send = recv.makeSendRight() + let send1 = recv.makeSendOnceRight() + + _ = consume recv + // `send` and `send1` have become dead names + _ = consume send + _ = consume send1 + } } #endif