diff --git a/src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs b/src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs index 6b0aa4658a1a51..cc0a5d97554e33 100644 --- a/src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs +++ b/src/coreclr/src/tools/crossgen2/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs @@ -1979,11 +1979,23 @@ private bool pInvokeMarshalingRequired(CORINFO_METHOD_STRUCT_* handle, CORINFO_S return false; } - MethodIL stubIL = _compilation.GetMethodIL(method); - if (stubIL == null) + MethodIL stubIL = null; + try { - // This is the case of a PInvoke method that requires marshallers, which we can't use in this compilation - Debug.Assert(!_compilation.NodeFactory.CompilationModuleGroup.GeneratesPInvoke(method)); + stubIL = _compilation.GetMethodIL(method); + if (stubIL == null) + { + // This is the case of a PInvoke method that requires marshallers, which we can't use in this compilation + Debug.Assert(!_compilation.NodeFactory.CompilationModuleGroup.GeneratesPInvoke(method)); + return true; + } + } + catch (RequiresRuntimeJitException) + { + // The PInvoke IL emitter will throw for known unsupported scenario. We cannot propagate the exception here since + // this interface call might be used to check if a certain pinvoke can be inlined in the caller. Throwing means that the + // caller will not get compiled. Instead, we'll return true to let the JIT know that it cannot inline the pinvoke, and + // the actual pinvoke call will be handled by a stub that we create and compile in the runtime. return true; }