Fix crossgen segfault
authorSteve MacLean <Steve.MacLean@Microsoft.com>
Fri, 30 Nov 2018 23:15:26 +0000 (18:15 -0500)
committerSteve MacLean <stmaclea@microsoft.com>
Sat, 1 Dec 2018 01:37:45 +0000 (20:37 -0500)
src/vm/ceeload.cpp

index c6911ed..9ff186e 100644 (file)
@@ -3103,8 +3103,49 @@ void Module::SetDomainFile(DomainFile *pDomainFile)
 
     DomainLocalModule* pModuleData = 0;
 
-    pModuleData = this->m_ModuleID;
-    LOG((LF_CLASSLOADER, LL_INFO10, "STATICS: Allocation not needed for ngened non shared module %s in Appdomain %08x\n"));
+    // Do we need to allocate memory for the non GC statics?
+    if (m_ModuleID == NULL)
+    {
+        // Allocate memory for the module statics.
+        LoaderAllocator *pLoaderAllocator = NULL;
+        if (GetAssembly()->IsCollectible())
+        {
+            pLoaderAllocator = GetAssembly()->GetLoaderAllocator();
+        }
+        else
+        {
+            pLoaderAllocator = pDomainFile->GetAppDomain()->GetLoaderAllocator();
+        }
+
+        SIZE_T size = GetDomainLocalModuleSize();
+
+        LOG((LF_CLASSLOADER, LL_INFO10, "STATICS: Allocating %i bytes for precomputed statics in module %S in LoaderAllocator %p\n",
+            size, this->GetDebugName(), pLoaderAllocator));
+
+        // We guarantee alignment for 64-bit regular statics on 32-bit platforms even without FEATURE_64BIT_ALIGNMENT for performance reasons.
+
+        _ASSERTE(size >= DomainLocalModule::OffsetOfDataBlob());
+
+        pModuleData = (DomainLocalModule*)(void*)
+            pLoaderAllocator->GetHighFrequencyHeap()->AllocAlignedMem(
+                size, MAX_PRIMITIVE_FIELD_SIZE);
+
+        // Note: Memory allocated on loader heap is zero filled
+        // memset(pModuleData, 0, size);
+
+        // Verify that the space is really zero initialized
+        _ASSERTE(pModuleData->GetPrecomputedGCStaticsBasePointer() == NULL);
+
+        // If the module was loaded as domain-specific, then we need to assign
+        // this module a domain-neutral module ID.
+        pModuleData->m_ModuleIndex = Module::AllocateModuleIndex();
+        m_ModuleIndex = pModuleData->m_ModuleIndex;
+    }
+    else
+    {
+        pModuleData = this->m_ModuleID;
+        LOG((LF_CLASSLOADER, LL_INFO10, "STATICS: Allocation not needed for ngened non shared module %s in Appdomain %08x\n"));
+    }
 
     // Non shared case, module points directly to the statics. In ngen case
     // m_pDomainModule is already set for the non shared case