Skip to content

Commit 9c25d81

Browse files
Merge pull request #1346 from Unity-Technologies/apple-silicon-fixes
Fix JIT on macOS Big Sur beta 6 when running on Apple silicon
2 parents bf9a6fb + 38cd777 commit 9c25d81

File tree

5 files changed

+37
-5
lines changed

5 files changed

+37
-5
lines changed

external/buildscripts/Build.bee.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private static void RegisterOSXArtifacts()
7575

7676
Artifacts.Add("MacBuildEnvironment",
7777
new Tuple<string, string>(
78-
"mac-toolchain-11_0/12.0-12A8158a_2b346a6c93e9c82250a1d88c02f24a5dea9f0bd1aa2ef44109896a44800e8c68.zip",
78+
"mac-toolchain-11_0/12.2-12B5018i_351c773fb8a192039fe0f0e962314b888102b0718c734ad54f3906c0caeed1c9.zip",
7979
"unity-internal"));
8080

8181
Artifacts.Add("mono-build-tools-extra",

external/buildscripts/build_all_osx.pl

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110

111111
for my $file ('mono')
112112
{
113-
MergeIntoFatBinary("$distDirSourceBinX64/$file", "$distDirSourceBinARM64/$file", "$distDirDestinationBin/$file");
113+
MergeIntoFatBinary("$distDirSourceBinX64/$file", "$distDirSourceBinARM64/$file", "$distDirDestinationBin/$file", 1);
114114
}
115115

116116
for my $file ('pedump')
@@ -126,7 +126,7 @@
126126

127127
for my $file ('libMonoPosixHelper.dylib')
128128
{
129-
MergeIntoFatBinary("$embedDirSourceX64/$file", "$embedDirSourceARM64/$file", "$distDirDestinationLib/$file");
129+
MergeIntoFatBinary("$embedDirSourceX64/$file", "$embedDirSourceARM64/$file", "$distDirDestinationLib/$file", 0);
130130
}
131131

132132
if ($buildMachine)
@@ -174,8 +174,17 @@ sub CopyEmbedRuntimeBinaries
174174

175175
sub MergeIntoFatBinary
176176
{
177-
my ($binary1, $binary2, $binaryOutput) = @_;
177+
my ($binary1, $binary2, $binaryOutput, $isExe) = @_;
178178

179179
print(">>> Merging '$binary1' and '$binary2' into '$binaryOutput'\n\n");
180180
system('lipo', "$binary1", "$binary2", "-create", "-output", "$binaryOutput") eq 0 or die("Failed to run lipo!");
181-
}
181+
182+
if ($isExe)
183+
{
184+
system("codesign", "--entitlements", $buildscriptsdir . "/entitlements.plist", "-s", "-", "$binaryOutput") eq 0 or die("Failed to codesign $binaryOutput!");
185+
}
186+
else
187+
{
188+
system("codesign", "-s", "-", "$binaryOutput") eq 0 or die("Failed to codesign $binaryOutput!");
189+
}
190+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>com.apple.security.cs.allow-jit</key>
6+
<true/>
7+
</dict>
8+
</plist>
9+

mono/utils/dlmalloc.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,14 @@ extern void* sbrk(ptrdiff_t);
13141314
#define MAP_ANONYMOUS MAP_ANON
13151315
#endif /* MAP_ANON */
13161316
#ifdef MAP_ANONYMOUS
1317+
1318+
#if defined(__APPLE__) && defined(__arm64__)
1319+
/* macOS on ARM64 requires using MAP_JIT in order to allocate executable memory. */
1320+
#define MMAP_FLAGS (MAP_PRIVATE|MAP_ANONYMOUS|MAP_JIT)
1321+
#else
13171322
#define MMAP_FLAGS (MAP_PRIVATE|MAP_ANONYMOUS)
1323+
#endif
1324+
13181325
#define CALL_MMAP(s) mmap(0, (s), MMAP_PROT, MMAP_FLAGS, -1, 0)
13191326
#else /* MAP_ANONYMOUS */
13201327
/*

mono/utils/mono-mmap.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ mono_valloc (void *addr, size_t length, int flags, MonoMemAccountType type)
228228
mflags |= MAP_ANONYMOUS;
229229
mflags |= MAP_PRIVATE;
230230

231+
#if defined(__APPLE__) && defined(__arm64__)
232+
/* macOS on ARM64 requires using MAP_JIT in order
233+
to allocate executable memory. */
234+
if (type == MONO_MEM_ACCOUNT_CODE)
235+
mflags |= MAP_JIT;
236+
#endif
237+
231238
BEGIN_CRITICAL_SECTION;
232239
ptr = mmap (addr, length, prot, mflags, -1, 0);
233240
if (ptr == MAP_FAILED) {

0 commit comments

Comments
 (0)