-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[mono] Work around arm64 MacCatalyst unavailable JITing API (#51669)
* To [JIT on Apple Silicon](https://developer.apple.com/documentation/apple-silicon/porting-just-in-time-compilers-to-apple-silicon) the application must call `pthread_jit_write_protect_np (0)` before writing to a code page and then `pthread_jit_write_protect_np (1)` before executing JITed code. * Catalyst apps are Mac apps that get to use iOS frameworks (as well as some mac frameworks). The API access is guarded by `__API_AVAILABLE` and `__API_UNAVAILABLE` macros in Apple framework headers. If an API is not explicitly marked for catalyst, clang will follow the `ios` availability. Unfortunately, `pthread_jit_write_protect_np` is marked like this: ```c __API_AVAILABLE(macos(11.0)) __API_UNAVAILABLE(ios, tvos, watchos) void pthread_jit_write_protect_np(int enabled); ``` So a Catalyst app running on arm64 cannot call it. It will get a compile-time error. ``` src/mono/mono/utils/mono-codeman.c:669:3: error: 'pthread_jit_write_protect_np' is unavailable: not available on macCatalyst pthread_jit_write_protect_np (0); ^ /Applications/Xcode_12.4.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX11.1.sdk/usr/include/pthread.h:561:6: note: 'pthread_jit_write_protect_np' has been explicitly marked unavailable here void pthread_jit_write_protect_np(int enabled); ^ ``` --- Turns out the symbol `pthread_jit_write_protect_np` is actually present in the Catalyst framework libraries, and it does the right thing. This PR works around the unfortunate `pthread.h` declaration by not using it. We create a new .c file that included our own declaration ```c void pthread_jit_write_protect_np(int enabled); ``` And then a new `mono_jit_write_protect` function that calls the pthread function. --- Another option would be to use something like ``` #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wpartial-availability" ... #pragma clang diagnostic pop ``` around the offending callsites. But neither ignoring `-Wpartial-availability` nor `-Wunguarded-availability-new` seem to have any effect on the compilation error. --- Caveat: It's not yet clear whether this is ok to do in a retail (App Store) app. --- * [mono] define HOST_MACCAT not HOST_MACCATALYST for Mac Catalyst The C code already has HOST_MACCAT ifdefs. HOST_MACCATALYST isn't used.
- Loading branch information
1 parent
df61348
commit a9f1207
Showing
7 changed files
with
66 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* \file | ||
*/ | ||
|
||
#include "config.h" | ||
|
||
#include "mono-compiler.h" | ||
#include "write-protect.h" | ||
|
||
#if defined (HOST_MACCAT) && defined (__aarch64__) | ||
|
||
/* our own declaration of pthread_jit_write_protect_np so that we don't see the __API_UNAVAILABLE__ header */ | ||
void | ||
pthread_jit_write_protect_np (int enabled); | ||
|
||
|
||
void | ||
mono_jit_write_protect (int enabled) | ||
{ | ||
pthread_jit_write_protect_np (enabled); | ||
} | ||
|
||
#else | ||
|
||
MONO_EMPTY_SOURCE_FILE (write_protect); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* \file | ||
*/ | ||
|
||
#ifndef __MONO_WRITE_PROTECT_H__ | ||
#define __MONO_WRITE_PROTECT_H__ | ||
|
||
#include <mono/utils/mono-publib.h> | ||
|
||
#if defined (HOST_MACCAT) && defined (__aarch64__) | ||
|
||
void | ||
mono_jit_write_protect (int enabled); | ||
|
||
#endif /* defined (HOST_MACCAT) && defined (__aarch64__) */ | ||
|
||
#endif /* __MONO_WRITE_PROTECT_H__ */ | ||
|