Unify the FlushProcessWriteBuffers mechanism for macOS arm64 / x64 (#63301)
authorgithub-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Thu, 6 Jan 2022 18:46:06 +0000 (10:46 -0800)
committerGitHub <noreply@github.com>
Thu, 6 Jan 2022 18:46:06 +0000 (10:46 -0800)
The macOS arm64 was using a new mechanism to implement FlushProcessWriteBuffers
because the original one based on a wired memory page doesn't work on arm64.
We have seen people hitting problems with the wired memory page allocation on
x64 in the past due to the fact that wired memory is a scarce resource and
in some cases, e.g. in presence of apps that greedily grab all of the available
wired memory pages, we were unable to initialize coreclr and execute any .NET
application.

This change switches x64 to the same mechanism as arm64 to prevent that issue
from happening and updates the minimum supported x64 macOS to 10.14. That's the
version required for the new APIs the change is using. Apple stopped supporting
10.13 a year ago anyways.

Co-authored-by: Jan Vorlicek <janvorli@microsoft.com>
eng/native/configurecompiler.cmake
src/coreclr/gc/unix/gcenv.unix.cpp
src/coreclr/pal/src/thread/process.cpp

index 502d432..c6f4a6a 100644 (file)
@@ -420,7 +420,7 @@ if (CLR_CMAKE_HOST_UNIX)
         set(CMAKE_OSX_DEPLOYMENT_TARGET "11.0")
         add_compile_options(-arch arm64)
       elseif(CLR_CMAKE_HOST_ARCH_AMD64)
-        set(CMAKE_OSX_DEPLOYMENT_TARGET "10.13")
+      set(CMAKE_OSX_DEPLOYMENT_TARGET "10.14")
         add_compile_options(-arch x86_64)
       else()
         clr_unknown_arch()
index 2a37c17..6319c51 100644 (file)
@@ -67,7 +67,6 @@
 #include <mach/mach_port.h>
 #include <mach/mach_host.h>
 
-#if defined(HOST_ARM64)
 #include <mach/task.h>
 #include <mach/vm_map.h>
 extern "C"
@@ -84,7 +83,6 @@ extern "C"
             abort();                                                        \
         }                                                                   \
     } while (false)
-#endif // defined(HOST_ARM64)
 
 #endif // __APPLE__
 
@@ -372,7 +370,7 @@ bool GCToOSInterface::Initialize()
     {
         s_flushUsingMemBarrier = TRUE;
     }
-#if !(defined(TARGET_OSX) && defined(HOST_ARM64))
+#ifndef TARGET_OSX
     else
     {
         assert(g_helperPage == 0);
@@ -404,7 +402,7 @@ bool GCToOSInterface::Initialize()
             return false;
         }
     }
-#endif // !(defined(TARGET_OSX) && defined(HOST_ARM64))
+#endif // !TARGET_OSX
 
     InitializeCGroup();
 
@@ -544,7 +542,7 @@ void GCToOSInterface::FlushProcessWriteBuffers()
         status = pthread_mutex_unlock(&g_flushProcessWriteBuffersMutex);
         assert(status == 0 && "Failed to unlock the flushProcessWriteBuffersMutex lock");
     }
-#if defined(TARGET_OSX) && defined(HOST_ARM64)
+#ifdef TARGET_OSX
     else
     {
         mach_msg_type_number_t cThreads;
@@ -570,7 +568,7 @@ void GCToOSInterface::FlushProcessWriteBuffers()
         machret = vm_deallocate(mach_task_self(), (vm_address_t)pThreads, cThreads * sizeof(thread_act_t));
         CHECK_MACH("vm_deallocate()", machret);
     }
-#endif // defined(TARGET_OSX) && defined(HOST_ARM64)
+#endif // TARGET_OSX
 }
 
 // Break into a debugger. Uses a compiler intrinsic if one is available,
index 2b349a6..f31449f 100644 (file)
@@ -85,7 +85,6 @@ SET_DEFAULT_DEBUG_CHANNEL(PROCESS); // some headers have code with asserts, so d
 #include <libproc.h>
 #include <sys/sysctl.h>
 #include <sys/posix_sem.h>
-#if defined(HOST_ARM64)
 #include <mach/task.h>
 #include <mach/vm_map.h>
 extern "C"
@@ -103,7 +102,6 @@ extern "C"
         }                                                                   \
     } while (false)
 
-#endif // defined(HOST_ARM64)
 #endif // __APPLE__
 
 #ifdef __NetBSD__
@@ -3472,7 +3470,7 @@ InitializeFlushProcessWriteBuffers()
         }
     }
 
-#if defined(TARGET_OSX) && defined(HOST_ARM64)
+#ifdef TARGET_OSX
     return TRUE;
 #else
     s_helperPage = static_cast<int*>(mmap(0, GetVirtualPageSize(), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0));
@@ -3502,7 +3500,7 @@ InitializeFlushProcessWriteBuffers()
     }
 
     return status == 0;
-#endif // defined(TARGET_OSX) && defined(HOST_ARM64)
+#endif // TARGET_OSX
 }
 
 #define FATAL_ASSERT(e, msg) \
@@ -3552,7 +3550,7 @@ FlushProcessWriteBuffers()
         status = pthread_mutex_unlock(&flushProcessWriteBuffersMutex);
         FATAL_ASSERT(status == 0, "Failed to unlock the flushProcessWriteBuffersMutex lock");
     }
-#if defined(TARGET_OSX) && defined(HOST_ARM64)
+#ifdef TARGET_OSX
     else
     {
         mach_msg_type_number_t cThreads;
@@ -3578,7 +3576,7 @@ FlushProcessWriteBuffers()
         machret = vm_deallocate(mach_task_self(), (vm_address_t)pThreads, cThreads * sizeof(thread_act_t));
         CHECK_MACH("vm_deallocate()", machret);
     }
-#endif // defined(TARGET_OSX) && defined(HOST_ARM64)
+#endif // TARGET_OSX
 }
 
 /*++