From 90ab3fbbe451625ff322c3575e04fadd63cdc1fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Tue, 25 Jun 2024 09:40:06 +0200 Subject: [PATCH] Improve resiliency when rooting broken assemblies Do a couple more upfront checks to trigger exceptions when it's recoverable. Fixes #103843. We still eventually crash when generating debug information since we don't have fine grained tracking of what parts of the type we looked at. The user would need to drop debug info generation, or stop feeding us garbage. --- .../Compiler/LibraryRootProvider.cs | 4 +--- .../Compiler/RootingServiceProvider.cs | 10 +++++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/LibraryRootProvider.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/LibraryRootProvider.cs index 00fb2285f85d5a..c47d26869319f9 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/LibraryRootProvider.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/LibraryRootProvider.cs @@ -99,9 +99,7 @@ public static void CheckCanGenerateMethod(MethodDesc method) private static void CheckTypeCanBeUsedInSignature(TypeDesc type) { - MetadataType defType = type as MetadataType; - - defType?.ComputeTypeContainsGCPointers(); + ((CompilerTypeSystemContext)type.Context).EnsureLoadableType(type); } } } diff --git a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/RootingServiceProvider.cs b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/RootingServiceProvider.cs index fc13ed9a298ee4..0f0b4d2a9c3333 100644 --- a/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/RootingServiceProvider.cs +++ b/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/RootingServiceProvider.cs @@ -46,7 +46,14 @@ public void AddCompilationRoot(TypeDesc type, string reason) public void AddReflectionRoot(TypeDesc type, string reason) { - _factory.TypeSystemContext.EnsureLoadableType(type); + TypeDesc lookedAtType = type; + do + { + _factory.TypeSystemContext.EnsureLoadableType(lookedAtType); + lookedAtType = (lookedAtType as MetadataType)?.ContainingType; + } + while (lookedAtType != null); + _rootAdder(_factory.ReflectedType(type), reason); } @@ -64,6 +71,7 @@ public void AddReflectionRoot(FieldDesc field, string reason) if (!_factory.MetadataManager.IsReflectionBlocked(field)) { _factory.TypeSystemContext.EnsureLoadableType(field.OwningType); + _factory.TypeSystemContext.EnsureLoadableType(field.FieldType); _rootAdder(_factory.ReflectedField(field), reason); } }