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"
akoeplinger marked this conversation as resolved.
Show resolved Hide resolved
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
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
19 changes: 13 additions & 6 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 @@ -668,9 +669,11 @@ mono_codeman_enable_write (void)
mono_native_tls_set_value (write_level_tls_id, GINT_TO_POINTER (level));
pthread_jit_write_protect_np (0);
lambdageek marked this conversation as resolved.
Show resolved Hide resolved
}
#elif defined(HOST_MACCAT) && defined(__aarch64__)
/* JITing in Catalyst apps is not allowed on Apple Silicon. */
g_assert_not_reached ();
#elif (defined(HOST_IOS) || defined(HOST_TVOS)) && defined(HOST_DARWIN_SIMULATOR) && defined(__aarch64__)
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));
mono_jit_write_protect (0);
#endif
}

Expand All @@ -692,8 +695,12 @@ mono_codeman_disable_write (void)
if (level == 0)
pthread_jit_write_protect_np (1);
}
#elif defined(HOST_MACCAT) && defined(__aarch64__)
/* JITing in Catalyst apps is not allowed on Apple Silicon. */
g_assert_not_reached ();
#elif (defined(HOST_IOS) || defined(HOST_TVOS)) && defined(HOST_DARWIN_SIMULATOR) && defined(__aarch64__)
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)
mono_jit_write_protect (1);
#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 (__aarch64__)
lambdageek marked this conversation as resolved.
Show resolved Hide resolved

/* 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
18 changes: 18 additions & 0 deletions src/mono/mono/utils/write-protect.h
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_IOS) || defined(HOST_TVOS)) && defined (HOST_DARWIN_SIMULATOR) && defined (__aarch64__)

void
mono_jit_write_protect (int enabled);

#endif /* defined (HOST_DARWIN_SIMULATOR) && defined (__aarch64__) */

#endif /* __MONO_WRITE_PROTECT_H__ */