From 1bb6619d7d24150f7b310f81b2fedb565461983d Mon Sep 17 00:00:00 2001 From: Gleb Balykov Date: Mon, 28 Jun 2021 12:53:58 +0300 Subject: [PATCH] Partially allow marshalling ILSTUB compilation for ndirect methods using crossgen2 if SPC.dll is in the same bubble AnsiStringMarshaller is disallowed because it incorrectly handles pointers returned from native code. --- .../ReadyToRunCompilationModuleGroupBase.cs | 9 +++++---- .../Interop/IL/Marshaller.ReadyToRun.cs | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/ReadyToRunCompilationModuleGroupBase.cs b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/ReadyToRunCompilationModuleGroupBase.cs index 08cdc64d09819d..12826165fc49c2 100644 --- a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/ReadyToRunCompilationModuleGroupBase.cs +++ b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/ReadyToRunCompilationModuleGroupBase.cs @@ -346,10 +346,8 @@ public sealed override bool GeneratesPInvoke(MethodDesc method) { // PInvokes depend on details of the core library, so for now only compile them if: // 1) We're compiling the core library module, or - // 2) We're compiling any module, and no marshalling is needed - // - // TODO Future: consider compiling PInvokes with complex marshalling in version bubble - // mode when the core library is included in the bubble. + // 2) We're compiling any module, and no marshalling is needed, or + // 3) We're compiling any module, and core library module is in the same bubble, and marhaller supports compilation Debug.Assert(method is EcmaMethod); @@ -361,6 +359,9 @@ public sealed override bool GeneratesPInvoke(MethodDesc method) if (((EcmaMethod)method).Module.Equals(method.Context.SystemModule)) return true; + if (_versionBubbleModuleSet.Contains(method.Context.SystemModule)) + return !Marshaller.IsMarshallingNotSupported(method); + return !Marshaller.IsMarshallingRequired(method); } diff --git a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Interop/IL/Marshaller.ReadyToRun.cs b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Interop/IL/Marshaller.ReadyToRun.cs index 636c698e8d5899..9016e6c6f3fdbc 100644 --- a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Interop/IL/Marshaller.ReadyToRun.cs +++ b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Interop/IL/Marshaller.ReadyToRun.cs @@ -141,5 +141,22 @@ public static bool IsMarshallingRequired(MethodSignature methodSig, ParameterMet return false; } + + public static bool IsMarshallingNotSupported(MethodDesc targetMethod) + { + Debug.Assert(targetMethod.IsPInvoke); + + var marshallers = GetMarshallersForMethod(targetMethod); + for (int i = 0; i < marshallers.Length; i++) + { + if (marshallers[i].GetType() == typeof(NotSupportedMarshaller) + // TODO: AnsiStringMarshaller can be allowed when it's logic is fixed, + // currently it leads to free(): invalid pointer + || marshallers[i].GetType() == typeof(AnsiStringMarshaller)) + return true; + } + + return false; + } } }