`mach_task_self_` and Swift 6

I was working through some of my code, and building with

swift build -Xswiftc -strict-concurrency=complete

and I found that my use of mach_task_self_ is not safe in Swift 6:

reference to var 'mach_task_self_' is not concurrency-safe because it involves shared mutable state; this is an error in Swift 6

It seems a solve would be to use the @preconcurrency attribute in my Darwin import:

@preconcurrency import Darwin

While this works, I find it a bit odd, as the documentation seems to indicate this attribute is meant to be a stopgap measure. To have to apply it to a core module feels strange to me.

Perhaps I shouldn't expect low-level system API's to conform to Swift's new concurrency model. But I also feel like mach_task_self_ would be used a lot, to the point where everyone just having @preconcurrency behind their import makes its intended use as a stopgap measure make less sense.

It seems there is a functional implementation of mach_task_self_ in the XNU kernel syscalls. But it doesn't appear to actually be usable immediately from Swift.

To note, this "involves share mutable state" bit also happens with bootstrap_port. And trying to use

task_get_special_port(
        mach_task_self_, TASK_BOOTSTRAP_PORT, ...)

just moves the issue back to mach_task_self_.

Are there any plans on allowing these Mach API's (and similar) be used in Swift 6 and beyond that go beyond simply guidance to add the @preconcurrency attribute?

1 Like

Are there any plans on allowing these Mach API's … ?

These are Apple specific, and that limits my ability to talk about The Future™. My general advice for Apple APIs is that, if you’re hitting compatibility problems with Swift concurrency, you should file a bug against the API.

Please post any bug numbers, just for the record.

In the meantime, I think that @preconcurrency is a reasonable workaround.

Oh, and two more things:

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

1 Like

Oh hi Quinn! Thank you for your response. I understand that you can't talk about everything.

Also, to note, yes I understand that the low level Mach API's are fairly dangerous. To note, I'm not using them in a production user-facing app. I'm really actually only using them as part of my own support framework for Mach messages as part of my reverse-engineering toolkit here:

Although, it looks like Apple might be getting into that as well, based on your links. I'd be interested to see where they take it!

Thanks again for your response!

I'm not using them in a production user-facing app.

Those are the words I like to hear!

And thanks for the pointer to Kass. Every now and again I need to do something silly with Mach messaging, and I’ll keep it in mind.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

2 Likes