Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ios/tvos] ARM64 simulator JIT Hack and CMAKE_SYSTEM_VARIANT iOSSimulator #52764

Closed
4 changes: 4 additions & 0 deletions eng/native/build-commons.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ build_native()
cmakeArgs="-DCMAKE_SYSTEM_VARIANT=MacCatalyst $cmakeArgs"
fi

if [[ "$targetOS" == "iOSSimulator" || "$targetOS" == "tvOSSimulator" ]]; then
cmakeArgs="-DCMAKE_SYSTEM_VARIANT=$targetOS $cmakeArgs"
fi

if [[ "$__UseNinja" == 1 ]]; then
generator="ninja"
buildTool="$(command -v ninja || command -v ninja-build)"
Expand Down
3 changes: 3 additions & 0 deletions src/mono/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "iOS" OR CMAKE_SYSTEM_NAME STREQUAL "tvOS")
add_definitions("-DSMALL_CONFIG")
add_definitions("-D_XOPEN_SOURCE")
add_definitions("-DHAVE_LARGE_FILE_SUPPORT=1")
if (CMAKE_SYSTEM_VARIANT STREQUAL "iOSSimulator" OR CMAKE_SYSTEM_VARIANT STREQUAL "tvOSSimulator")
set (HOST_DARWIN_SIMULATOR 1)
endif()
elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(HOST_LINUX 1)
add_definitions(-D_GNU_SOURCE -D_REENTRANT)
Expand Down
3 changes: 3 additions & 0 deletions src/mono/cmake/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@
/* Host Platform is Mac Catalyst */
#cmakedefine HOST_MACCAT 1

/* Host Platform is iOS or tvOS simulator */
#cmakedefine HOST_DARWIN_SIMULATOR 1

/* Use classic Windows API support */
#cmakedefine HAVE_CLASSIC_WINAPI_SUPPORT 1

Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/utils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ set(utils_common_sources
mono-logger.c
mono-logger-internals.h
mono-codeman.c
write-protect.c
write-protect.h
mono-counters.c
mono-compiler.h
mono-dl.c
Expand Down
29 changes: 17 additions & 12 deletions src/mono/mono/utils/mono-codeman.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ static void* mono_code_manager_heap;

#include <mono/utils/mono-os-mutex.h>
#include <mono/utils/mono-tls.h>
#include <mono/utils/write-protect.h>

static uintptr_t code_memory_used = 0;
static size_t dynamic_code_alloc_count;
Expand Down Expand Up @@ -652,6 +653,16 @@ mono_code_manager_size (MonoCodeManager *cman, int *used_size)
return size;
}

#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
#define JIT_WRITE_PROTECT(enable) pthread_jit_write_protect_np ((enable))
#define JIT_WRITE_PROTECT_AVAIL() (__builtin_available (macOS 11, *))
#define USE_WRITE_PROTECT 1
#elif (defined(HOST_IOS) || defined(HOST_TVOS)) && defined(HOST_DARWIN_SIMULATOR) && defined(HOST_ARM64)
#define USE_WRITE_PROTECT 1
#define JIT_WRITE_PROTECT(enable) mono_jit_write_protect ((enable))
#define JIT_WRITE_PROTECT_AVAIL() 1
#endif

/*
* mono_codeman_enable_write ():
*
Expand All @@ -661,16 +672,13 @@ mono_code_manager_size (MonoCodeManager *cman, int *used_size)
void
mono_codeman_enable_write (void)
{
#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
if (__builtin_available (macOS 11, *)) {
#ifdef USE_WRITE_PROTECT
if (JIT_WRITE_PROTECT_AVAIL()) {
int level = GPOINTER_TO_INT (mono_native_tls_get_value (write_level_tls_id));
level ++;
mono_native_tls_set_value (write_level_tls_id, GINT_TO_POINTER (level));
pthread_jit_write_protect_np (0);
JIT_WRITE_PROTECT (0);
}
#elif defined(HOST_MACCAT) && defined(__aarch64__)
/* JITing in Catalyst apps is not allowed on Apple Silicon. */
g_assert_not_reached ();
#endif
}

Expand All @@ -683,17 +691,14 @@ mono_codeman_enable_write (void)
void
mono_codeman_disable_write (void)
{
#ifdef HAVE_PTHREAD_JIT_WRITE_PROTECT_NP
if (__builtin_available (macOS 11, *)) {
#ifdef USE_WRITE_PROTECT
if (JIT_WRITE_PROTECT_AVAIL()) {
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
int level = GPOINTER_TO_INT (mono_native_tls_get_value (write_level_tls_id));
g_assert (level);
level --;
mono_native_tls_set_value (write_level_tls_id, GINT_TO_POINTER (level));
if (level == 0)
pthread_jit_write_protect_np (1);
JIT_WRITE_PROTECT (1);
}
#elif defined(HOST_MACCAT) && defined(__aarch64__)
/* JITing in Catalyst apps is not allowed on Apple Silicon. */
g_assert_not_reached ();
#endif
}
27 changes: 27 additions & 0 deletions src/mono/mono/utils/write-protect.c
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_IOS) || defined(HOST_TVOS)) && defined (HOST_DARWIN_SIMULATOR) && defined (HOST_ARM64)

/* 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 /* (defined(HOST_IOS) || defined(HOST_TVOS)) && defined (HOST_DARWIN_SIMULATOR) && defined (HOST_ARM64) */

MONO_EMPTY_SOURCE_FILE (write_protect);

#endif
16 changes: 16 additions & 0 deletions src/mono/mono/utils/write-protect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* \file
*/

#ifndef __MONO_WRITE_PROTECT_H__
#define __MONO_WRITE_PROTECT_H__

#include <mono/utils/mono-publib.h>

#if (defined(HOST_IOS) || defined(HOST_TVOS)) && defined (HOST_DARWIN_SIMULATOR) && defined (HOST_ARM64)
mono_jit_write_protect (int enabled);

#endif /* (defined(HOST_IOS) || defined(HOST_TVOS)) && defined (HOST_DARWIN_SIMULATOR) && defined (HOST_ARM64) */

#endif /* __MONO_WRITE_PROTECT_H__ */