Introduce a diagnostic switch to turn off the use of file mapping in PE loader. ...
authorVladimir Sadov <vsadov@microsoft.com>
Tue, 4 Apr 2023 03:29:57 +0000 (20:29 -0700)
committerGitHub <noreply@github.com>
Tue, 4 Apr 2023 03:29:57 +0000 (20:29 -0700)
* Introduce a diagnostic switch to turn off use of file mapping in PE loader.

* change default to off

* fix condition on Unix

src/coreclr/inc/clrconfigvalues.h
src/coreclr/vm/peimagelayout.cpp
src/coreclr/vm/peimagelayout.h

index 9dd74d914ad74620eb94a6e68437ab92fa6ac843..94a6c8984252f6355dce68041d39c34d993d95b2 100644 (file)
@@ -130,6 +130,11 @@ RETAIL_CONFIG_DWORD_INFO(INTERNAL_JitPitchMaxVal, W("JitPitchMaxVal"), (DWORD)0x
 ///
 CONFIG_DWORD_INFO(INTERNAL_GetAssemblyIfLoadedIgnoreRidMap, W("GetAssemblyIfLoadedIgnoreRidMap"), 0, "Used to force loader to ignore assemblies cached in the rid-map")
 
+///
+/// PE Loader
+///
+RETAIL_CONFIG_DWORD_INFO(INTERNAL_PELoader_DisableMapping, W("PELoader_DisableMapping"), 0, "Disable file mapping when performing non-OS layout.")
+
 ///
 /// Conditional breakpoints
 ///
index 09ac710c1cf5f113e6d7eb76d53cacc2f4b4875f..dcbe0f9db2ba52003f636ef2f9df0ea14f64713c 100644 (file)
@@ -58,7 +58,7 @@ PEImageLayout* PEImageLayout::CreateFromHMODULE(HMODULE hModule, PEImage* pOwner
 }
 #endif
 
-PEImageLayout* PEImageLayout::LoadConverted(PEImage* pOwner)
+PEImageLayout* PEImageLayout::LoadConverted(PEImage* pOwner, bool disableMapping)
 {
     STANDARD_VM_CONTRACT;
 
@@ -85,14 +85,14 @@ PEImageLayout* PEImageLayout::LoadConverted(PEImage* pOwner)
     // ConvertedImageLayout may be able to handle them, but the fact that we were unable to
     // load directly implies that MAPMapPEFile could not consume what crossgen produced.
     // that is suspicious, one or another might have a bug.
-    _ASSERTE(!pOwner->IsFile() || !pFlat->HasReadyToRunHeader());
+    _ASSERTE(!pOwner->IsFile() || !pFlat->HasReadyToRunHeader() || disableMapping);
 #endif
 
     // ignore R2R if the image is not a file.
     if ((pFlat->HasReadyToRunHeader() && pOwner->IsFile()) ||
         pFlat->HasWriteableSections())
     {
-        return new ConvertedImageLayout(pFlat);
+        return new ConvertedImageLayout(pFlat, disableMapping);
     }
 
     // we can use flat layout for this
@@ -103,6 +103,8 @@ PEImageLayout* PEImageLayout::Load(PEImage* pOwner, HRESULT* loadFailure)
 {
     STANDARD_VM_CONTRACT;
 
+    bool disableMapping = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_PELoader_DisableMapping);
+
     if (pOwner->IsFile())
     {
         if (!pOwner->IsInBundle()
@@ -111,19 +113,26 @@ PEImageLayout* PEImageLayout::Load(PEImage* pOwner, HRESULT* loadFailure)
 #endif
             )
         {
-            PEImageLayoutHolder pAlloc(new LoadedImageLayout(pOwner, loadFailure));
-            if (pAlloc->GetBase() != NULL)
-                return pAlloc.Extract();
+#if defined(TARGET_UNIX)
+            if (!disableMapping)
+#endif
+            {
+
+                PEImageLayoutHolder pAlloc(new LoadedImageLayout(pOwner, loadFailure));
+                if (pAlloc->GetBase() != NULL)
+                    return pAlloc.Extract();
 
 #if TARGET_WINDOWS
-            // OS loader is always right. If a file cannot be loaded, do not try any further.
-            // Even if we may be able to load it, we do not want to support such files.
-            return NULL;
+                // For regular PE files always use OS loader on Windows.
+                // If a file cannot be loaded, do not try any further.
+                // Even if we may be able to load it, we do not want to support such files.
+                return NULL;
 #endif
+            }
         }
     }
 
-    return PEImageLayout::LoadConverted(pOwner);
+    return PEImageLayout::LoadConverted(pOwner, disableMapping);
 }
 
 PEImageLayout* PEImageLayout::LoadFlat(PEImage* pOwner)
@@ -411,7 +420,7 @@ void ConvertedImageLayout::FreeImageParts()
     }
 }
 
-ConvertedImageLayout::ConvertedImageLayout(FlatImageLayout* source)
+ConvertedImageLayout::ConvertedImageLayout(FlatImageLayout* source, bool disableMapping)
 {
     CONTRACTL
     {
@@ -432,14 +441,17 @@ ConvertedImageLayout::ConvertedImageLayout(FlatImageLayout* source)
     LOG((LF_LOADER, LL_INFO100, "PEImage: Opening manually mapped stream\n"));
 
 #ifdef TARGET_WINDOWS
-    loadedImage = source->LoadImageByMappingParts(this->m_imageParts);
-    if (loadedImage == NULL)
+    if (!disableMapping)
     {
-        FreeImageParts();
-    }
-    else
-    {
-        relocationMustWriteCopy = true;
+        loadedImage = source->LoadImageByMappingParts(this->m_imageParts);
+        if (loadedImage == NULL)
+        {
+            FreeImageParts();
+        }
+        else
+        {
+            relocationMustWriteCopy = true;
+        }
     }
 #endif //TARGET_WINDOWS
 
index 73039057cd8666e2c61b9573a96718492b90da2c..64afafba136a3d69f722bddb573b47a6e584c358 100644 (file)
@@ -50,7 +50,7 @@ public:
 #endif
     static PEImageLayout* Load(PEImage* pOwner, HRESULT* loadFailure);
     static PEImageLayout* LoadFlat(PEImage* pOwner);
-    static PEImageLayout* LoadConverted(PEImage* pOwner);
+    static PEImageLayout* LoadConverted(PEImage* pOwner, bool disableMapping);
     static PEImageLayout* LoadNative(LPCWSTR fullPath);
 #endif
     PEImageLayout();
@@ -104,7 +104,7 @@ class ConvertedImageLayout: public PEImageLayout
 public:
     static const int MAX_PARTS = 16;
 #ifndef DACCESS_COMPILE
-    ConvertedImageLayout(FlatImageLayout* source);
+    ConvertedImageLayout(FlatImageLayout* source, bool disableMapping);
     virtual ~ConvertedImageLayout();
     void  FreeImageParts();
 #endif