Skip to content

Commit 7c3fb38

Browse files
authored
Fix the TypeLoadException CPAOT compilation bucket (dotnet#7360)
I have fixed this by constructing TypeFixupSignature instances using a new helper method TypeSignature (much like what we're doing for methods) which calls the CheckCanGenerateEEType method upfront. This properly throws the exception to JIT so that it gets caught in compileMethodInternal and just suppresses jitting of a particular method rather than tearing down the entire compilation process. Based on Michal's suggestion I'm proposing to remove the check from the constructors of AvailableType and ExternalTypeNode as with my change it becomes duplicative and it ultimately causes more harm than good by crashing the compiler when hitting an invalid type (as happens e.g. in negative CoreCLR typeloader tests). Thanks Tomas
1 parent b27534e commit 7c3fb38

File tree

4 files changed

+35
-27
lines changed

4 files changed

+35
-27
lines changed

src/ILCompiler.ReadyToRun/src/Compiler/DependencyAnalysis/ExternalTypeNode.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ class ExternalTypeNode : DependencyNodeCore<NodeFactory>, IEETypeNode
1818
public ExternalTypeNode(NodeFactory factory, TypeDesc type)
1919
{
2020
_type = type;
21-
22-
//
23-
// This check encodes rules specific to CoreRT. Ie, no function pointer classes allowed.
24-
// Eventually we will hit situations where this check fails when it shouldn't and we'll need to
25-
// split the logic. It's a good sanity check for the time being though.
26-
//
27-
EETypeNode.CheckCanGenerateEEType(factory, type);
2821
}
2922

3023
public TypeDesc Type => _type;

src/ILCompiler.ReadyToRun/src/Compiler/DependencyAnalysis/ReadyToRun/AvailableType.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ public class AvailableType : DependencyNodeCore<NodeFactory>, IEETypeNode
1818
public AvailableType(NodeFactory factory, TypeDesc type)
1919
{
2020
_type = type;
21-
22-
//
23-
// This check encodes rules specific to CoreRT. Ie, no function pointer classes allowed.
24-
// Eventually we will hit situations where this check fails when it shouldn't and we'll need to
25-
// split the logic. It's a good sanity check for the time being though.
26-
//
27-
EETypeNode.CheckCanGenerateEEType(factory, type);
2821
}
2922

3023
public TypeDesc Type => _type;

src/ILCompiler.ReadyToRun/src/Compiler/DependencyAnalysis/ReadyToRunCodegenNodeFactory.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,28 @@ public MethodFixupSignature MethodSignature(
295295
return signature;
296296
}
297297

298+
private readonly Dictionary<ReadyToRunFixupKind, Dictionary<TypeDesc, TypeFixupSignature>> _typeSignatures =
299+
new Dictionary<ReadyToRunFixupKind, Dictionary<TypeDesc, TypeFixupSignature>>();
300+
301+
public TypeFixupSignature TypeSignature(ReadyToRunFixupKind fixupKind, TypeDesc typeDesc, SignatureContext signatureContext)
302+
{
303+
Dictionary<TypeDesc, TypeFixupSignature> perFixupKindMap;
304+
if (!_typeSignatures.TryGetValue(fixupKind, out perFixupKindMap))
305+
{
306+
perFixupKindMap = new Dictionary<TypeDesc, TypeFixupSignature>();
307+
_typeSignatures.Add(fixupKind, perFixupKindMap);
308+
}
309+
310+
TypeFixupSignature signature;
311+
if (!perFixupKindMap.TryGetValue(typeDesc, out signature))
312+
{
313+
EETypeNode.CheckCanGenerateEEType(this, typeDesc);
314+
signature = new TypeFixupSignature(fixupKind, typeDesc, signatureContext);
315+
perFixupKindMap.Add(typeDesc, signature);
316+
}
317+
return signature;
318+
}
319+
298320
public override void AttachToDependencyGraph(DependencyAnalyzerBase<NodeFactory> graph)
299321
{
300322
Header = new HeaderNode(Target);
@@ -493,7 +515,7 @@ protected override ISymbolNode CreateGenericLookupFromDictionaryNode(ReadyToRunG
493515
this,
494516
HelperImports,
495517
GetGenericStaticHelper(helperKey.HelperId),
496-
new TypeFixupSignature(
518+
TypeSignature(
497519
ReadyToRunFixupKind.READYTORUN_FIXUP_Invalid,
498520
(TypeDesc)helperKey.Target,
499521
InputModuleContext));
@@ -505,7 +527,7 @@ protected override ISymbolNode CreateGenericLookupFromTypeNode(ReadyToRunGeneric
505527
this,
506528
HelperImports,
507529
GetGenericStaticHelper(helperKey.HelperId),
508-
new TypeFixupSignature(
530+
TypeSignature(
509531
ReadyToRunFixupKind.READYTORUN_FIXUP_Invalid,
510532
(TypeDesc)helperKey.Target,
511533
InputModuleContext));

src/ILCompiler.ReadyToRun/src/Compiler/DependencyAnalysis/ReadyToRunSymbolNodeFactory.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private ISymbolNode CreateGCStaticBaseHelper(TypeDesc type, SignatureContext sig
150150
_codegenNodeFactory,
151151
_codegenNodeFactory.HelperImports,
152152
ILCompiler.DependencyAnalysis.ReadyToRun.ReadyToRunHelper.READYTORUN_HELPER_DelayLoad_Helper,
153-
new TypeFixupSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_StaticBaseGC, type, signatureContext));
153+
_codegenNodeFactory.TypeSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_StaticBaseGC, type, signatureContext));
154154
}
155155

156156
private ISymbolNode CreateNonGCStaticBaseHelper(TypeDesc type, SignatureContext signatureContext)
@@ -159,7 +159,7 @@ private ISymbolNode CreateNonGCStaticBaseHelper(TypeDesc type, SignatureContext
159159
_codegenNodeFactory,
160160
_codegenNodeFactory.HelperImports,
161161
ILCompiler.DependencyAnalysis.ReadyToRun.ReadyToRunHelper.READYTORUN_HELPER_DelayLoad_Helper,
162-
new TypeFixupSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_StaticBaseNonGC, type, signatureContext));
162+
_codegenNodeFactory.TypeSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_StaticBaseNonGC, type, signatureContext));
163163
}
164164

165165
private ISymbolNode CreateThreadGcStaticBaseHelper(TypeDesc type, SignatureContext signatureContext)
@@ -168,7 +168,7 @@ private ISymbolNode CreateThreadGcStaticBaseHelper(TypeDesc type, SignatureConte
168168
_codegenNodeFactory,
169169
_codegenNodeFactory.HelperImports,
170170
ILCompiler.DependencyAnalysis.ReadyToRun.ReadyToRunHelper.READYTORUN_HELPER_DelayLoad_Helper,
171-
new TypeFixupSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_ThreadStaticBaseGC, type, signatureContext));
171+
_codegenNodeFactory.TypeSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_ThreadStaticBaseGC, type, signatureContext));
172172
}
173173

174174
private ISymbolNode CreateThreadNonGcStaticBaseHelper(TypeDesc type, SignatureContext signatureContext)
@@ -177,7 +177,7 @@ private ISymbolNode CreateThreadNonGcStaticBaseHelper(TypeDesc type, SignatureCo
177177
_codegenNodeFactory,
178178
_codegenNodeFactory.HelperImports,
179179
ILCompiler.DependencyAnalysis.ReadyToRun.ReadyToRunHelper.READYTORUN_HELPER_DelayLoad_Helper,
180-
new TypeFixupSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_ThreadStaticBaseNonGC, type, signatureContext));
180+
_codegenNodeFactory.TypeSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_ThreadStaticBaseNonGC, type, signatureContext));
181181
}
182182

183183
private ISymbolNode CreateIsInstanceOfHelper(TypeDesc type, SignatureContext signatureContext)
@@ -186,7 +186,7 @@ private ISymbolNode CreateIsInstanceOfHelper(TypeDesc type, SignatureContext sig
186186
_codegenNodeFactory,
187187
_codegenNodeFactory.HelperImports,
188188
ILCompiler.DependencyAnalysis.ReadyToRun.ReadyToRunHelper.READYTORUN_HELPER_DelayLoad_Helper,
189-
new TypeFixupSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_IsInstanceOf, type, signatureContext));
189+
_codegenNodeFactory.TypeSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_IsInstanceOf, type, signatureContext));
190190
}
191191

192192
private ISymbolNode CreateCastClassHelper(TypeDesc type, SignatureContext signatureContext)
@@ -195,14 +195,14 @@ private ISymbolNode CreateCastClassHelper(TypeDesc type, SignatureContext signat
195195
_codegenNodeFactory,
196196
_codegenNodeFactory.HelperImports,
197197
ILCompiler.DependencyAnalysis.ReadyToRun.ReadyToRunHelper.READYTORUN_HELPER_DelayLoad_Helper_Obj,
198-
new TypeFixupSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_ChkCast, type, signatureContext));
198+
_codegenNodeFactory.TypeSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_ChkCast, type, signatureContext));
199199
}
200200

201201
private ISymbolNode CreateTypeHandleHelper(TypeDesc type, SignatureContext signatureContext)
202202
{
203203
return new PrecodeHelperImport(
204204
_codegenNodeFactory,
205-
new TypeFixupSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_TypeHandle, type, signatureContext));
205+
_codegenNodeFactory.TypeSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_TypeHandle, type, signatureContext));
206206
}
207207

208208
private ISymbolNode CreateMethodHandleHelper(MethodWithToken method, SignatureContext signatureContext)
@@ -255,14 +255,14 @@ private ISymbolNode CreateCctorTrigger(TypeDesc type, SignatureContext signature
255255
_codegenNodeFactory,
256256
_codegenNodeFactory.DispatchImports,
257257
ILCompiler.DependencyAnalysis.ReadyToRun.ReadyToRunHelper.READYTORUN_HELPER_DelayLoad_Helper,
258-
new TypeFixupSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_CctorTrigger, type, signatureContext));
258+
_codegenNodeFactory.TypeSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_CctorTrigger, type, signatureContext));
259259
}
260260

261261
private ISymbolNode CreateTypeDictionary(TypeDesc type, SignatureContext signatureContext)
262262
{
263263
return new PrecodeHelperImport(
264264
_codegenNodeFactory,
265-
new TypeFixupSignature(
265+
_codegenNodeFactory.TypeSignature(
266266
ReadyToRunFixupKind.READYTORUN_FIXUP_TypeDictionary,
267267
type,
268268
signatureContext));
@@ -323,7 +323,7 @@ public ISymbolNode FieldBaseOffset(TypeDesc typeDesc, SignatureContext signature
323323
{
324324
result = new PrecodeHelperImport(
325325
_codegenNodeFactory,
326-
new TypeFixupSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_FieldBaseOffset, typeDesc, signatureContext));
326+
_codegenNodeFactory.TypeSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_FieldBaseOffset, typeDesc, signatureContext));
327327
_fieldBaseOffsetCache.Add(typeDesc, result);
328328
}
329329
return result;
@@ -734,7 +734,7 @@ public ISymbolNode ConstructedTypeSymbol(TypeDesc type, SignatureContext signatu
734734
{
735735
symbol = new PrecodeHelperImport(
736736
_codegenNodeFactory,
737-
new TypeFixupSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_TypeDictionary, type, signatureContext));
737+
_codegenNodeFactory.TypeSignature(ReadyToRunFixupKind.READYTORUN_FIXUP_TypeDictionary, type, signatureContext));
738738
_constructedTypeSymbols.Add(type, symbol);
739739
}
740740
return symbol;

0 commit comments

Comments
 (0)