dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.16k stars 4.72k forks source link

ILCompiler: Unable to cast object of type 'Internal.TypeSystem.SignatureTypeVariable' to type 'Internal.TypeSystem.DefType'. #85865

Closed filipnavara closed 1 year ago

filipnavara commented 1 year ago
System.AggregateException: One or more errors occurred. (Code generation failed for method '[Microsoft.iOS]UIKit.UIGestureRecognizer+Callback`1+__Registrar_Callbacks__.callback_2515_UIKit_UIGestureRecognizer_Callback_1_Activated(native int,native int,native int,native int*)')
 ---> ILCompiler.CodeGenerationFailedException: Code generation failed for method '[Microsoft.iOS]UIKit.UIGestureRecognizer+Callback`1+__Registrar_Callbacks__.callback_2515_UIKit_UIGestureRecognizer_Callback_1_Activated(native int,native int,native int,native int*)'
 ---> System.InvalidCastException: Unable to cast object of type 'Internal.TypeSystem.SignatureTypeVariable' to type 'Internal.TypeSystem.DefType'.
   at ILCompiler.CompilerTypeSystemContext.EnsureLoadableTypeUncached(TypeDesc) in /_/src/coreclr/tools/Common/Compiler/CompilerTypeSystemContext.Validation.cs:line 134
   at Internal.TypeSystem.LockFreeReaderHashtable`2.CreateValueAndEnsureValueIsInTable(TKey) in /_/src/coreclr/tools/Common/TypeSystem/Common/Utilities/LockFreeReaderHashtable.cs:line 562
   at Internal.JitInterface.CorInfoImpl.resolveToken(CORINFO_RESOLVED_TOKEN&) in /_/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs:line 1798
   at Internal.JitInterface.CorInfoImpl._resolveToken(IntPtr, IntPtr*, CORINFO_RESOLVED_TOKEN*) in /_/src/coreclr/tools/Common/JitInterface/CorInfoImpl_generated.cs:line 445
   --- End of inner exception stack trace ---
   at Internal.JitInterface.CorInfoImpl.CompileMethodInternal(IMethodNode, MethodIL) in /_/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs:line 383
   at Internal.JitInterface.CorInfoImpl.CompileMethod(MethodCodeNode, MethodIL ) in /_/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs:line 62
   at ILCompiler.RyuJitCompilation.CompileSingleMethod(CorInfoImpl, MethodCodeNode) in /_/src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/RyuJitCompilation.cs:line 190
   at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.<ForWorker>b__1(RangeWorker&, Int32, Boolean& )
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.<ForWorker>b__1(RangeWorker&, Int32, Boolean& )
   at System.Threading.Tasks.TaskReplicator.Replica.Execute()
   --- End of inner exception stack trace ---
   at System.Threading.Tasks.TaskReplicator.Run[TState](ReplicatableUserAction`1, ParallelOptions, Boolean)
   at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32, Int32, ParallelOptions, Action`1, Action`2, Func`4, Func`1, Action`1)
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Parallel.ThrowSingleCancellationExceptionOrOtherException(ICollection, CancellationToken, Exception)
   at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32, Int32, ParallelOptions, Action`1, Action`2, Func`4, Func`1, Action`1)
   at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1, ParallelOptions, Action`1, Action`2, Action`3, Func`4, Func`5, Func`1, Action`1)
   at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1, ParallelOptions, Action`1)
   at ILCompiler.RyuJitCompilation.CompileMultiThreaded(List`1) in /_/src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/RyuJitCompilation.cs:line 150
   at ILCompiler.RyuJitCompilation.ComputeDependencyNodeDependencies(List`1) in /_/src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/RyuJitCompilation.cs:line 140
   at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ComputeMarkedNodes() in /_/src/coreclr/tools/aot/ILCompiler.DependencyAnalysisFramework/DependencyAnalyzer.cs:line 316
   at ILCompiler.RyuJitCompilation.CompileInternal(String, ObjectDumper) in /_/src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/RyuJitCompilation.cs:line 88
   at ILCompiler.Compilation.ILCompiler.ICompilation.Compile(String, ObjectDumper) in /_/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Compilation.cs:line 523
   at ILCompiler.Program.Run() in /_/src/coreclr/tools/aot/ILCompiler/Program.cs:line 532
   at ILCompiler.ILCompilerRootCommand.<>c__DisplayClass206_0.<.ctor>b__0(InvocationContext) in /_/src/coreclr/tools/aot/ILCompiler/ILCompilerRootCommand.cs:line 275

Repro: 3168759622_ios-naot.zip

ghost commented 1 year ago

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch See info in area-owners.md if you want to be subscribed.

Issue Details
``` System.AggregateException: One or more errors occurred. (Code generation failed for method '[Microsoft.iOS]UIKit.UIGestureRecognizer+Callback`1+__Registrar_Callbacks__.callback_2515_UIKit_UIGestureRecognizer_Callback_1_Activated(native int,native int,native int,native int*)') ---> ILCompiler.CodeGenerationFailedException: Code generation failed for method '[Microsoft.iOS]UIKit.UIGestureRecognizer+Callback`1+__Registrar_Callbacks__.callback_2515_UIKit_UIGestureRecognizer_Callback_1_Activated(native int,native int,native int,native int*)' ---> System.InvalidCastException: Unable to cast object of type 'Internal.TypeSystem.SignatureTypeVariable' to type 'Internal.TypeSystem.DefType'. at ILCompiler.CompilerTypeSystemContext.EnsureLoadableTypeUncached(TypeDesc) in /_/src/coreclr/tools/Common/Compiler/CompilerTypeSystemContext.Validation.cs:line 134 at Internal.TypeSystem.LockFreeReaderHashtable`2.CreateValueAndEnsureValueIsInTable(TKey) in /_/src/coreclr/tools/Common/TypeSystem/Common/Utilities/LockFreeReaderHashtable.cs:line 562 at Internal.JitInterface.CorInfoImpl.resolveToken(CORINFO_RESOLVED_TOKEN&) in /_/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs:line 1798 at Internal.JitInterface.CorInfoImpl._resolveToken(IntPtr, IntPtr*, CORINFO_RESOLVED_TOKEN*) in /_/src/coreclr/tools/Common/JitInterface/CorInfoImpl_generated.cs:line 445 --- End of inner exception stack trace --- at Internal.JitInterface.CorInfoImpl.CompileMethodInternal(IMethodNode, MethodIL) in /_/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs:line 383 at Internal.JitInterface.CorInfoImpl.CompileMethod(MethodCodeNode, MethodIL ) in /_/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs:line 62 at ILCompiler.RyuJitCompilation.CompileSingleMethod(CorInfoImpl, MethodCodeNode) in /_/src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/RyuJitCompilation.cs:line 190 at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.b__1(RangeWorker&, Int32, Boolean& ) --- End of stack trace from previous location --- at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.b__1(RangeWorker&, Int32, Boolean& ) at System.Threading.Tasks.TaskReplicator.Replica.Execute() --- End of inner exception stack trace --- at System.Threading.Tasks.TaskReplicator.Run[TState](ReplicatableUserAction`1, ParallelOptions, Boolean) at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32, Int32, ParallelOptions, Action`1, Action`2, Func`4, Func`1, Action`1) --- End of stack trace from previous location --- at System.Threading.Tasks.Parallel.ThrowSingleCancellationExceptionOrOtherException(ICollection, CancellationToken, Exception) at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32, Int32, ParallelOptions, Action`1, Action`2, Func`4, Func`1, Action`1) at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1, ParallelOptions, Action`1, Action`2, Action`3, Func`4, Func`5, Func`1, Action`1) at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1, ParallelOptions, Action`1) at ILCompiler.RyuJitCompilation.CompileMultiThreaded(List`1) in /_/src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/RyuJitCompilation.cs:line 150 at ILCompiler.RyuJitCompilation.ComputeDependencyNodeDependencies(List`1) in /_/src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/RyuJitCompilation.cs:line 140 at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ComputeMarkedNodes() in /_/src/coreclr/tools/aot/ILCompiler.DependencyAnalysisFramework/DependencyAnalyzer.cs:line 316 at ILCompiler.RyuJitCompilation.CompileInternal(String, ObjectDumper) in /_/src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/RyuJitCompilation.cs:line 88 at ILCompiler.Compilation.ILCompiler.ICompilation.Compile(String, ObjectDumper) in /_/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Compilation.cs:line 523 at ILCompiler.Program.Run() in /_/src/coreclr/tools/aot/ILCompiler/Program.cs:line 532 at ILCompiler.ILCompilerRootCommand.<>c__DisplayClass206_0.<.ctor>b__0(InvocationContext) in /_/src/coreclr/tools/aot/ILCompiler/ILCompilerRootCommand.cs:line 275 ``` Repro: [3168759622_ios-naot.zip](https://github.com/dotnet/runtime/files/11412213/3168759622_ios-naot.zip)
Author: filipnavara
Assignees: -
Labels: `area-CodeGen-coreclr`, `untriaged`
Milestone: -
ghost commented 1 year ago

Tagging subscribers to 'os-ios': @steveisok, @akoeplinger See info in area-owners.md if you want to be subscribed.

Issue Details
``` System.AggregateException: One or more errors occurred. (Code generation failed for method '[Microsoft.iOS]UIKit.UIGestureRecognizer+Callback`1+__Registrar_Callbacks__.callback_2515_UIKit_UIGestureRecognizer_Callback_1_Activated(native int,native int,native int,native int*)') ---> ILCompiler.CodeGenerationFailedException: Code generation failed for method '[Microsoft.iOS]UIKit.UIGestureRecognizer+Callback`1+__Registrar_Callbacks__.callback_2515_UIKit_UIGestureRecognizer_Callback_1_Activated(native int,native int,native int,native int*)' ---> System.InvalidCastException: Unable to cast object of type 'Internal.TypeSystem.SignatureTypeVariable' to type 'Internal.TypeSystem.DefType'. at ILCompiler.CompilerTypeSystemContext.EnsureLoadableTypeUncached(TypeDesc) in /_/src/coreclr/tools/Common/Compiler/CompilerTypeSystemContext.Validation.cs:line 134 at Internal.TypeSystem.LockFreeReaderHashtable`2.CreateValueAndEnsureValueIsInTable(TKey) in /_/src/coreclr/tools/Common/TypeSystem/Common/Utilities/LockFreeReaderHashtable.cs:line 562 at Internal.JitInterface.CorInfoImpl.resolveToken(CORINFO_RESOLVED_TOKEN&) in /_/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs:line 1798 at Internal.JitInterface.CorInfoImpl._resolveToken(IntPtr, IntPtr*, CORINFO_RESOLVED_TOKEN*) in /_/src/coreclr/tools/Common/JitInterface/CorInfoImpl_generated.cs:line 445 --- End of inner exception stack trace --- at Internal.JitInterface.CorInfoImpl.CompileMethodInternal(IMethodNode, MethodIL) in /_/src/coreclr/tools/Common/JitInterface/CorInfoImpl.cs:line 383 at Internal.JitInterface.CorInfoImpl.CompileMethod(MethodCodeNode, MethodIL ) in /_/src/coreclr/tools/aot/ILCompiler.RyuJit/JitInterface/CorInfoImpl.RyuJit.cs:line 62 at ILCompiler.RyuJitCompilation.CompileSingleMethod(CorInfoImpl, MethodCodeNode) in /_/src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/RyuJitCompilation.cs:line 190 at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.b__1(RangeWorker&, Int32, Boolean& ) --- End of stack trace from previous location --- at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`1.b__1(RangeWorker&, Int32, Boolean& ) at System.Threading.Tasks.TaskReplicator.Replica.Execute() --- End of inner exception stack trace --- at System.Threading.Tasks.TaskReplicator.Run[TState](ReplicatableUserAction`1, ParallelOptions, Boolean) at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32, Int32, ParallelOptions, Action`1, Action`2, Func`4, Func`1, Action`1) --- End of stack trace from previous location --- at System.Threading.Tasks.Parallel.ThrowSingleCancellationExceptionOrOtherException(ICollection, CancellationToken, Exception) at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32, Int32, ParallelOptions, Action`1, Action`2, Func`4, Func`1, Action`1) at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1, ParallelOptions, Action`1, Action`2, Action`3, Func`4, Func`5, Func`1, Action`1) at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1, ParallelOptions, Action`1) at ILCompiler.RyuJitCompilation.CompileMultiThreaded(List`1) in /_/src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/RyuJitCompilation.cs:line 150 at ILCompiler.RyuJitCompilation.ComputeDependencyNodeDependencies(List`1) in /_/src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/RyuJitCompilation.cs:line 140 at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ComputeMarkedNodes() in /_/src/coreclr/tools/aot/ILCompiler.DependencyAnalysisFramework/DependencyAnalyzer.cs:line 316 at ILCompiler.RyuJitCompilation.CompileInternal(String, ObjectDumper) in /_/src/coreclr/tools/aot/ILCompiler.RyuJit/Compiler/RyuJitCompilation.cs:line 88 at ILCompiler.Compilation.ILCompiler.ICompilation.Compile(String, ObjectDumper) in /_/src/coreclr/tools/aot/ILCompiler.Compiler/Compiler/Compilation.cs:line 523 at ILCompiler.Program.Run() in /_/src/coreclr/tools/aot/ILCompiler/Program.cs:line 532 at ILCompiler.ILCompilerRootCommand.<>c__DisplayClass206_0.<.ctor>b__0(InvocationContext) in /_/src/coreclr/tools/aot/ILCompiler/ILCompilerRootCommand.cs:line 275 ``` Repro: [3168759622_ios-naot.zip](https://github.com/dotnet/runtime/files/11412213/3168759622_ios-naot.zip)
Author: filipnavara
Assignees: -
Labels: `untriaged`, `os-ios`, `area-NativeAOT-coreclr`
Milestone: -
teo-tsirpanis commented 1 year ago

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas See info in area-owners.md if you want to be subscribed.

jkotas commented 1 year ago

This is bad IL. Generic variable token used non-generic method.

.class auto ansi nested assembly beforefieldinit Callback`1<(UIKit.UIGestureRecognizer) T>
         extends UIKit.UIGestureRecognizer/Token
{
    .class auto ansi sealed nested assembly __Registrar_Callbacks__
           extends [System.Private.CoreLib]System.Object

    {
      .method public hidebysig static void 
              callback_2515_UIKit_UIGestureRecognizer_Callback_1_Activated(native int pobj,
                                                                           native int sel,
                                                                           native int p0,
                                                                           native int* exception_gchandle) cil managed
...
          IL_0048:  unbox.any  !0 <- Reference to generic variable in non-generic method
...

Is the __Registrar_Callbacks__ code auto-generated by some tool? It looks like a bug in that tool.

filipnavara commented 1 year ago

Is the __Registrar_Callbacks__ code auto-generated by some tool? It looks like a bug in that tool.

Yes, it is - https://github.com/xamarin/xamarin-macios/pull/17828 - cc @rolfbjarne

filipnavara commented 1 year ago

It looks like it should have been unbox.any !T or nothing at all. In fact, the PR was updated meanwhile and the places which emitted the unbox.any now have a comment:

// We're calling the target method dynamically (using MethodBase.Invoke), so there's no
// need to check the type of the returned object, because MethodBase.Invoke will do type checks.
filipnavara commented 1 year ago

I rebuilt everything and looks like the latest version of the linked PR fixed the invalid IL that was generated.

Thanks for help!