Do not use /dev/zero to mmap anonymous memory (#48140)
authorVladimir Sadov <vsadov@microsoft.com>
Thu, 11 Feb 2021 13:47:35 +0000 (05:47 -0800)
committerGitHub <noreply@github.com>
Thu, 11 Feb 2021 13:47:35 +0000 (05:47 -0800)
* return void

* Always use MAP_ANON

* remove HAVE_MMAP_DEV_ZERO config

src/coreclr/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.CoreCLR.cs
src/coreclr/pal/src/config.h.in
src/coreclr/pal/src/configure.cmake
src/coreclr/pal/src/map/map.cpp

index c5fb0f2..2b992fe 100644 (file)
@@ -19,7 +19,7 @@ namespace System.Runtime.Loader
 
         [RequiresUnreferencedCode("Types and members the loaded assembly depends on might be removed")]
         [DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
-        private static extern IntPtr LoadFromStream(IntPtr ptrNativeAssemblyLoadContext, IntPtr ptrAssemblyArray, int iAssemblyArrayLen, IntPtr ptrSymbols, int iSymbolArrayLen, ObjectHandleOnStack retAssembly);
+        private static extern void LoadFromStream(IntPtr ptrNativeAssemblyLoadContext, IntPtr ptrAssemblyArray, int iAssemblyArrayLen, IntPtr ptrSymbols, int iSymbolArrayLen, ObjectHandleOnStack retAssembly);
 
         [DllImport(RuntimeHelpers.QCall, CharSet = CharSet.Unicode)]
         internal static extern void InternalSetProfileRoot(string directoryPath);
index 10f281b..c684213 100644 (file)
 #cmakedefine01 HAVE_CLOCK_GETTIME_NSEC_NP
 #cmakedefine01 HAVE_CLOCK_THREAD_CPUTIME
 #cmakedefine01 HAVE_PTHREAD_CONDATTR_SETCLOCK
-#cmakedefine01 HAVE_MMAP_DEV_ZERO
 #cmakedefine01 MMAP_ANON_IGNORES_PROTECTION
 #cmakedefine01 ONE_SHARED_MAPPING_PER_FILEREGION_PER_PROCESS
 #cmakedefine01 PTHREAD_CREATE_MODIFIES_ERRNO
index af9836c..28f8d70 100644 (file)
@@ -478,26 +478,6 @@ int main()
 set(CMAKE_REQUIRED_LIBRARIES)
 
 check_cxx_source_runs("
-#include <stdlib.h>
-#include <sys/types.h>
-#include <sys/mman.h>
-#include <fcntl.h>
-
-int main(void) {
-  int devzero;
-  void *retval;
-
-  devzero = open(\"/dev/zero\", O_RDWR);
-  if (-1 == devzero) {
-    exit(1);
-  }
-  retval = mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, devzero, 0);
-  if (retval == (void *)-1) {
-    exit(1);
-  }
-  exit(0);
-}" HAVE_MMAP_DEV_ZERO)
-check_cxx_source_runs("
 #include <sys/types.h>
 #include <sys/mman.h>
 #include <signal.h>
index 0243bd9..af97934 100644 (file)
@@ -102,12 +102,10 @@ MAPmmapAndRecord(
     LPVOID *ppvBaseAddress
     );
 
-#if !HAVE_MMAP_DEV_ZERO
 /* We need MAP_ANON. However on some platforms like HP-UX, it is defined as MAP_ANONYMOUS */
 #if !defined(MAP_ANON) && defined(MAP_ANONYMOUS)
 #define MAP_ANON MAP_ANONYMOUS
 #endif
-#endif
 
 void
 FileMappingCleanupRoutine(
@@ -477,22 +475,7 @@ CorUnix::InternalCreateFileMapping(
             goto ExitInternalCreateFileMapping;
         }
 
-#if HAVE_MMAP_DEV_ZERO
-
-        UnixFd = InternalOpen(pImmutableData->lpFileName, O_RDWR | O_CLOEXEC);
-        if ( -1 == UnixFd )
-        {
-            ERROR( "Unable to open the file.\n");
-            palError = ERROR_INTERNAL_ERROR;
-            goto ExitInternalCreateFileMapping;
-        }
-
-#else //!HAVE_MMAP_DEV_ZERO
-
         UnixFd = -1;  /* will pass MAP_ANON to mmap() instead */
-
-#endif //!HAVE_MMAP_DEV_ZERO
-
     }
     else
     {
@@ -1057,13 +1040,11 @@ CorUnix::InternalMapViewOfFile(
     if (FILE_MAP_COPY == dwDesiredAccess)
     {
         int flags = MAP_PRIVATE;
-
-#if !HAVE_MMAP_DEV_ZERO
         if (pProcessLocalData->UnixFd == -1)
         {
             flags |= MAP_ANON;
         }
-#endif
+
         pvBaseAddress = mmap(
             NULL,
             dwNumberOfBytesToMap,
@@ -1079,13 +1060,10 @@ CorUnix::InternalMapViewOfFile(
         if (prot != -1)
         {
             int flags = MAP_SHARED;
-
-#if !HAVE_MMAP_DEV_ZERO
             if (pProcessLocalData->UnixFd == -1)
             {
                 flags |= MAP_ANON;
             }
-#endif
 
             pvBaseAddress = mmap(
                 NULL,