From d0ffdac224be85e53ef4dfd426e52bd60db37760 Mon Sep 17 00:00:00 2001 From: Tomas Date: Fri, 13 Aug 2021 20:23:17 +0200 Subject: [PATCH] Harvest signatures of methods being compiled by the token resolver Fixes: https://github.com/dotnet/runtime/issues/56971 I believe this is the primary fix for the issue; when a type only ever occurs in method signatures in an assembly, never in the implementation IL, JIT never calls resolveToken for it and so it doesn't make it to the module token resolver table. My investigation revealed additional potential problems in the product around exported type resolution and the IL linker; I described them in the issue thread but I believe this to be both the most appropriate and the safest primary fix. Thanks Tomas --- .../DependencyAnalysis/ReadyToRun/ModuleTokenResolver.cs | 5 +++++ .../JitInterface/CorInfoImpl.ReadyToRun.cs | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/ModuleTokenResolver.cs b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/ModuleTokenResolver.cs index 86d507ab57af8d..d15ffc25c501cb 100644 --- a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/ModuleTokenResolver.cs +++ b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/Compiler/DependencyAnalysis/ReadyToRun/ModuleTokenResolver.cs @@ -143,6 +143,11 @@ public void AddModuleTokenForMethod(MethodDesc method, ModuleToken token) AddModuleTokenForType(owningType, new ModuleToken(token.Module, owningTypeHandle)); memberRef.DecodeMethodSignature(new TokenResolverProvider(this, token.Module), this); } + if (token.TokenType == CorTokenType.mdtMethodDef) + { + MethodDefinition methodDef = token.MetadataReader.GetMethodDefinition((MethodDefinitionHandle)token.Handle); + methodDef.DecodeSignature(new TokenResolverProvider(this, token.Module), this); + } } private void AddModuleTokenForFieldReference(TypeDesc owningType, ModuleToken token) diff --git a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs index 6a325a5844c816..2bedb907e3c9b8 100644 --- a/src/coreclr/tools/aot/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs +++ b/src/coreclr/tools/aot/ILCompiler.ReadyToRun/JitInterface/CorInfoImpl.ReadyToRun.cs @@ -471,6 +471,12 @@ public void CompileMethod(MethodWithGCInfo methodCodeNodeNeedingCode, Logger log { if (!FunctionJustThrows(methodIL)) { + if (MethodBeingCompiled.GetTypicalMethodDefinition() is EcmaMethod ecmaMethod) + { + // Harvest the method being compiled for the purpose of populating the type resolver + var resolver = _compilation.NodeFactory.Resolver; + resolver.AddModuleTokenForMethod(MethodBeingCompiled, new ModuleToken(ecmaMethod.Module, ecmaMethod.Handle)); + } CompileMethodInternal(methodCodeNodeNeedingCode, methodIL); codeGotPublished = true; }