Skip to content

Fix usage of pthread_jit_write_protect_np() on macOS and iOS

Nirbheek Chauhan requested to merge nirbheek/orc:pthread-jit-write-usage into main

The API is not available on iOS at all, and is only available on macOS starting from macOS 11, as can be seen in pthread/pthread.h in the Xcode SDK:

__API_AVAILABLE(macos(11.0))
__API_UNAVAILABLE(ios, tvos, watchos, driverkit)
void pthread_jit_write_protect_np(int enabled);

__API_AVAILABLE(macos(11.0))
__API_UNAVAILABLE(ios, tvos, watchos, driverkit)
int pthread_jit_write_protect_supported_np(void);

The configuration check for this is actually wrong. We should detect availability of the API at compile time and use it conditionally at runtime. The code now checks the following cases:

  1. Are we building for macOS?
  2. Do we have a new-enough SDK that defines MAC_OS_VERSION_11_0 and hence has pthread_jit_write_* available?
  3. Is the maximum macOS version allowed at least macOS 11.0, so we have a possibility of using this API at runtime?
  4. Are we running on macOS 11.0 or newer?

Also: we need to ensure that pthread_jit_write_protect_supported_np() actually returns true before using the API, because if you're shipping an app, you need to set com.apple.security.cs.allow-jit in your app otherwise the API won't work if you've opted in to Hardened Runtime. See: https://developer.apple.com/documentation/apple-silicon/porting-just-in-time-compilers-to-apple-silicon

Fixes #44 (closed)

I will open an MR on the cerbero side too.

Merge request reports