From 900f794c8d12cf29ecfc72341872fe8e0cf23b4d Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Wed, 17 Aug 2022 14:21:21 -0700 Subject: [PATCH 1/7] enable test --- src/tests/readytorun/tests/main.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/tests/readytorun/tests/main.cs b/src/tests/readytorun/tests/main.cs index 02d94a5939378a..b1bb93fc900388 100644 --- a/src/tests/readytorun/tests/main.cs +++ b/src/tests/readytorun/tests/main.cs @@ -513,9 +513,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(); From d70b37d5bad270c58062e58820e7080cddf37e42 Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Wed, 17 Aug 2022 15:16:12 -0700 Subject: [PATCH 2/7] flat layout is ok if it is not a file --- src/coreclr/vm/peimagelayout.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/vm/peimagelayout.cpp b/src/coreclr/vm/peimagelayout.cpp index 2cf519425da2a9..0a19d3fa29686b 100644 --- a/src/coreclr/vm/peimagelayout.cpp +++ b/src/coreclr/vm/peimagelayout.cpp @@ -85,7 +85,7 @@ 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()) From 7b5aa65c0f9a3aacb0738df748496eccca052cce Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Wed, 17 Aug 2022 16:21:18 -0700 Subject: [PATCH 3/7] do not convert non-file layouts on OSX --- src/coreclr/vm/peimagelayout.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/coreclr/vm/peimagelayout.cpp b/src/coreclr/vm/peimagelayout.cpp index 0a19d3fa29686b..71c8a9b05d7d6b 100644 --- a/src/coreclr/vm/peimagelayout.cpp +++ b/src/coreclr/vm/peimagelayout.cpp @@ -94,6 +94,17 @@ PEImageLayout* PEImageLayout::LoadConverted(PEImage* pOwner) return pFlat.Extract(); } +#ifdef TARGET_OSX + // We need to allocate executable memory on OSX in order to do relocation of R2R code. + // Converted layout currently uses VirtualAlloc, so it will not work. + // Do not convert byte array images on OSX for now (that will disable R2R) + // TODO: consider relaxing this in the future. + if (!pOwner->IsFile()) + { + return pFlat.Extract(); + } +#endif + return new ConvertedImageLayout(pFlat); } From 29f97af92d15d5e5eddd519c3ddd739c1becdcb7 Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Thu, 18 Aug 2022 16:45:54 -0700 Subject: [PATCH 4/7] run some code when loading from byte array --- src/tests/readytorun/tests/main.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/tests/readytorun/tests/main.cs b/src/tests/readytorun/tests/main.cs index b1bb93fc900388..b8a94e8ce2b239 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 somehting 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)] From ccdba92c8796d6d541155373b77b4df103422d89 Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Fri, 19 Aug 2022 08:56:42 -0700 Subject: [PATCH 5/7] use flat layout on all platforms --- src/coreclr/vm/peimagelayout.cpp | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/coreclr/vm/peimagelayout.cpp b/src/coreclr/vm/peimagelayout.cpp index 71c8a9b05d7d6b..e3ff495c00d00e 100644 --- a/src/coreclr/vm/peimagelayout.cpp +++ b/src/coreclr/vm/peimagelayout.cpp @@ -88,24 +88,15 @@ PEImageLayout* PEImageLayout::LoadConverted(PEImage* pOwner) _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); } -#ifdef TARGET_OSX - // We need to allocate executable memory on OSX in order to do relocation of R2R code. - // Converted layout currently uses VirtualAlloc, so it will not work. - // Do not convert byte array images on OSX for now (that will disable R2R) - // TODO: consider relaxing this in the future. - if (!pOwner->IsFile()) - { - return pFlat.Extract(); - } -#endif - - return new ConvertedImageLayout(pFlat); + // we can use flat layout for this + return pFlat.Extract(); } PEImageLayout* PEImageLayout::Load(PEImage* pOwner, HRESULT* loadFailure) @@ -459,7 +450,7 @@ ConvertedImageLayout::ConvertedImageLayout(FlatImageLayout* source) IfFailThrow(Init(loadedImage)); - if (IsNativeMachineFormat() && g_fAllowNativeImages) + if (pOwner->IsFile() && IsNativeMachineFormat() && g_fAllowNativeImages) { // Do base relocation and exception hookup, if necessary. // otherwise R2R will be disabled for this image. From f5714fa86a144949db4d8b9e1d0bd65ee66a7d12 Mon Sep 17 00:00:00 2001 From: Vladimir Sadov Date: Fri, 19 Aug 2022 08:57:28 -0700 Subject: [PATCH 6/7] Suggestion from PR feedback Co-authored-by: Jan Vorlicek --- src/tests/readytorun/tests/main.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tests/readytorun/tests/main.cs b/src/tests/readytorun/tests/main.cs index b8a94e8ce2b239..04314860d48ca0 100644 --- a/src/tests/readytorun/tests/main.cs +++ b/src/tests/readytorun/tests/main.cs @@ -417,7 +417,7 @@ static void RVAFieldTest() // public constructor, so we run something when loading from byte array in the test below public Program() { - // do somehting in the constructor to see if it works + // do something in the constructor to see if it works TestVirtualMethodCalls(); } From c1ec495eae6d54a5f4d0825a96752b649c83451a Mon Sep 17 00:00:00 2001 From: vsadov <8218165+VSadov@users.noreply.github.com> Date: Fri, 19 Aug 2022 09:14:16 -0700 Subject: [PATCH 7/7] typo --- src/coreclr/vm/peimagelayout.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/coreclr/vm/peimagelayout.cpp b/src/coreclr/vm/peimagelayout.cpp index e3ff495c00d00e..792a3176eb129c 100644 --- a/src/coreclr/vm/peimagelayout.cpp +++ b/src/coreclr/vm/peimagelayout.cpp @@ -450,7 +450,7 @@ ConvertedImageLayout::ConvertedImageLayout(FlatImageLayout* source) IfFailThrow(Init(loadedImage)); - if (pOwner->IsFile() && 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.