diff --git a/src/coreclr/vm/peimagelayout.cpp b/src/coreclr/vm/peimagelayout.cpp index 2cf519425da2a9..792a3176eb129c 100644 --- a/src/coreclr/vm/peimagelayout.cpp +++ b/src/coreclr/vm/peimagelayout.cpp @@ -85,16 +85,18 @@ 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(!pFlat->HasReadyToRunHeader()); + _ASSERTE(!pOwner->IsFile() || !pFlat->HasReadyToRunHeader()); #endif - if (!pFlat->HasReadyToRunHeader() && !pFlat->HasWriteableSections()) + // ignore R2R if the image is not a file. + if ((pFlat->HasReadyToRunHeader() && pOwner->IsFile()) || + pFlat->HasWriteableSections()) { - // we can use flat layout for this - return pFlat.Extract(); + return new ConvertedImageLayout(pFlat); } - return new ConvertedImageLayout(pFlat); + // we can use flat layout for this + return pFlat.Extract(); } PEImageLayout* PEImageLayout::Load(PEImage* pOwner, HRESULT* loadFailure) @@ -448,7 +450,7 @@ ConvertedImageLayout::ConvertedImageLayout(FlatImageLayout* source) IfFailThrow(Init(loadedImage)); - if (IsNativeMachineFormat() && g_fAllowNativeImages) + if (m_pOwner->IsFile() && IsNativeMachineFormat() && g_fAllowNativeImages) { // Do base relocation and exception hookup, if necessary. // otherwise R2R will be disabled for this image. diff --git a/src/tests/readytorun/tests/main.cs b/src/tests/readytorun/tests/main.cs index 02d94a5939378a..04314860d48ca0 100644 --- a/src/tests/readytorun/tests/main.cs +++ b/src/tests/readytorun/tests/main.cs @@ -414,6 +414,13 @@ static void RVAFieldTest() Assert.AreEqual(value[i], (byte)(9 - i)); } + // public constructor, so we run something when loading from byte array in the test below + public Program() + { + // do something in the constructor to see if it works + TestVirtualMethodCalls(); + } + static void TestLoadR2RImageFromByteArray() { Assembly assembly1 = typeof(Program).Assembly; @@ -422,6 +429,8 @@ static void TestLoadR2RImageFromByteArray() Assembly assembly2 = Assembly.Load(array); Assert.AreEqual(assembly2.FullName, assembly1.FullName); + + assembly2.CreateInstance("Program"); } [MethodImplAttribute(MethodImplOptions.NoInlining)] @@ -513,9 +522,8 @@ static void RunAllTests() Console.WriteLine("RVAFieldTest"); RVAFieldTest(); -// Disable for https://github.com/dotnet/runtime/issues/71507 -// Console.WriteLine("TestLoadR2RImageFromByteArray"); -// TestLoadR2RImageFromByteArray(); + Console.WriteLine("TestLoadR2RImageFromByteArray"); + TestLoadR2RImageFromByteArray(); Console.WriteLine("TestILBodyChange"); TestILBodyChange();