Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PublishAot fails with CodeGenerationFailedException/InvalidOperationException at TypeSystemHelpers.ContainsSignatureVariables #98821

Closed
tmds opened this issue Feb 22, 2024 · 3 comments · Fixed by #98836

Comments

@tmds
Copy link
Member

tmds commented Feb 22, 2024

I'm working on making a library NativeAOT compatible.

I tried to compile an application that uses the library with PublishAot and the ILCompiler is throwing an exception:

  System.AggregateException: One or more errors occurred. (Code generation failed for method '[Tmds.DBus.Protocol]Tmds.DBus.Protocol.TypeModel.GetTypeAlignment<__Canon>()')
   ---> ILCompiler.CodeGenerationFailedException: Code generation failed for method '[Tmds.DBus.Protocol]Tmds.DBus.Protocol.TypeModel.GetTypeAlignment<__Canon>()'
   ---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
     at Internal.TypeSystem.TypeSystemHelpers.ContainsSignatureVariables(TypeDesc, Boolean) + 0x192
     at Internal.TypeSystem.TypeSystemHelpers.ContainsSignatureVariables(TypeDesc, Boolean) + 0x138
     at ILCompiler.FeatureSwitchManager.TryExpandTypeEquality(MethodIL, Byte[], FeatureSwitchManager.OpcodeFlags[], Int32, String, Int32&) + 0x22f
     at ILCompiler.FeatureSwitchManager.TryGetConstantArgument(MethodIL, Byte[], FeatureSwitchManager.OpcodeFlags[], Int32, Int32, Int32&) + 0x7b4
     at ILCompiler.FeatureSwitchManager.GetMethodILWithInlinedSubstitutions(MethodIL) + 0x983
     at ILCompiler.FeatureSwitchManager.GetMethodIL(MethodDesc) + 0x65
     at ILCompiler.Compilation.CombinedILProvider.GetMethodIL(MethodDesc) + 0x1f
     at ILCompiler.Compilation.ILCache.CreateValueFromKey(MethodDesc) + 0x3a
     at Internal.TypeSystem.LockFreeReaderHashtable`2.CreateValueAndEnsureValueIsInTable(TKey) + 0x14
     at ILCompiler.Compilation.GetMethodIL(MethodDesc) + 0xc6
     at Internal.IL.ILImporter..ctor(ILScanner, MethodDesc, MethodIL) + 0x60
     at ILCompiler.ILScanner.CompileSingleMethod(ScannedMethodNode methodCodeNodeNeedingCode) + 0x46
     --- End of inner exception stack trace ---
     at ILCompiler.ILScanner.CompileSingleMethod(ScannedMethodNode methodCodeNodeNeedingCode) + 0x221
     at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.<ForWorker>b__1(RangeWorker& currentWorker, Int64 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) + 0x292
  --- End of stack trace from previous location ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception) + 0x13
     at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.<ForWorker>b__1(RangeWorker& currentWorker, Int64 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) + 0x456
     at System.Threading.Tasks.TaskReplicator.Replica.Execute() + 0x68
     --- End of inner exception stack trace ---
     at System.Threading.Tasks.TaskReplicator.Run[TState](TaskReplicator.ReplicatableUserAction`1, ParallelOptions, Boolean) + 0x15a
     at System.Threading.Tasks.Parallel.ForWorker[TLocal,TInt](TInt, TInt, ParallelOptions, Action`1, Action`2, Func`4, Func`1, Action`1) + 0x23e
  --- End of stack trace from previous location ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
     at System.Threading.Tasks.Parallel.ThrowSingleCancellationExceptionOrOtherException(ICollection, CancellationToken, Exception) + 0x31
     at System.Threading.Tasks.Parallel.ForWorker[TLocal,TInt](TInt, TInt, ParallelOptions, Action`1, Action`2, Func`4, Func`1, Action`1) + 0x437
     at ILCompiler.ILScanner.CompileMultiThreaded(List`1) + 0x1eb
     at ILCompiler.ILScanner.ComputeDependencyNodeDependencies(List`1 obj) + 0x178
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ComputeMarkedNodes() + 0x97
     at ILCompiler.ILScanner.ILCompiler.IILScanner.Scan() + 0x19
     at ILCompiler.Program.<Run>g__RunScanner|4_0(Program.<>c__DisplayClass4_0&) + 0x19a
     at ILCompiler.Program.Run() + 0x25c0
     at ILCompiler.ILCompilerRootCommand.<>c__DisplayClass221_0.<.ctor>b__0(ParseResult result) + 0x310

To reproduce:

git clone https://github.com/tmds/Tmds.DBus
cd Tmds.DBus/samples/MediaPlayerRemote/
dotnet publish /p:PublishAot=true /p:TargetFramework=net8.0 /p:TargetFrameworks=net8.0

There are a lot of warnings when I publish the app which I still need to address.
It could be that the exception goes away when I've done that.

$ dotnet --version
8.0.101

cc @MichalStrehovsky

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Feb 22, 2024
@ghost
Copy link

ghost commented Feb 22, 2024

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

Issue Details

I'm working on making a library NativeAOT compatible.

I tried to compile an application that uses the library with PublishAot and the ILCompiler is throwing an exception:

  System.AggregateException: One or more errors occurred. (Code generation failed for method '[Tmds.DBus.Protocol]Tmds.DBus.Protocol.TypeModel.GetTypeAlignment<__Canon>()')
   ---> ILCompiler.CodeGenerationFailedException: Code generation failed for method '[Tmds.DBus.Protocol]Tmds.DBus.Protocol.TypeModel.GetTypeAlignment<__Canon>()'
   ---> System.InvalidOperationException: Operation is not valid due to the current state of the object.
     at Internal.TypeSystem.TypeSystemHelpers.ContainsSignatureVariables(TypeDesc, Boolean) + 0x192
     at Internal.TypeSystem.TypeSystemHelpers.ContainsSignatureVariables(TypeDesc, Boolean) + 0x138
     at ILCompiler.FeatureSwitchManager.TryExpandTypeEquality(MethodIL, Byte[], FeatureSwitchManager.OpcodeFlags[], Int32, String, Int32&) + 0x22f
     at ILCompiler.FeatureSwitchManager.TryGetConstantArgument(MethodIL, Byte[], FeatureSwitchManager.OpcodeFlags[], Int32, Int32, Int32&) + 0x7b4
     at ILCompiler.FeatureSwitchManager.GetMethodILWithInlinedSubstitutions(MethodIL) + 0x983
     at ILCompiler.FeatureSwitchManager.GetMethodIL(MethodDesc) + 0x65
     at ILCompiler.Compilation.CombinedILProvider.GetMethodIL(MethodDesc) + 0x1f
     at ILCompiler.Compilation.ILCache.CreateValueFromKey(MethodDesc) + 0x3a
     at Internal.TypeSystem.LockFreeReaderHashtable`2.CreateValueAndEnsureValueIsInTable(TKey) + 0x14
     at ILCompiler.Compilation.GetMethodIL(MethodDesc) + 0xc6
     at Internal.IL.ILImporter..ctor(ILScanner, MethodDesc, MethodIL) + 0x60
     at ILCompiler.ILScanner.CompileSingleMethod(ScannedMethodNode methodCodeNodeNeedingCode) + 0x46
     --- End of inner exception stack trace ---
     at ILCompiler.ILScanner.CompileSingleMethod(ScannedMethodNode methodCodeNodeNeedingCode) + 0x221
     at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.<ForWorker>b__1(RangeWorker& currentWorker, Int64 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) + 0x292
  --- End of stack trace from previous location ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception) + 0x13
     at System.Threading.Tasks.Parallel.<>c__DisplayClass19_0`2.<ForWorker>b__1(RangeWorker& currentWorker, Int64 timeout, Boolean& replicationDelegateYieldedBeforeCompletion) + 0x456
     at System.Threading.Tasks.TaskReplicator.Replica.Execute() + 0x68
     --- End of inner exception stack trace ---
     at System.Threading.Tasks.TaskReplicator.Run[TState](TaskReplicator.ReplicatableUserAction`1, ParallelOptions, Boolean) + 0x15a
     at System.Threading.Tasks.Parallel.ForWorker[TLocal,TInt](TInt, TInt, ParallelOptions, Action`1, Action`2, Func`4, Func`1, Action`1) + 0x23e
  --- End of stack trace from previous location ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
     at System.Threading.Tasks.Parallel.ThrowSingleCancellationExceptionOrOtherException(ICollection, CancellationToken, Exception) + 0x31
     at System.Threading.Tasks.Parallel.ForWorker[TLocal,TInt](TInt, TInt, ParallelOptions, Action`1, Action`2, Func`4, Func`1, Action`1) + 0x437
     at ILCompiler.ILScanner.CompileMultiThreaded(List`1) + 0x1eb
     at ILCompiler.ILScanner.ComputeDependencyNodeDependencies(List`1 obj) + 0x178
     at ILCompiler.DependencyAnalysisFramework.DependencyAnalyzer`2.ComputeMarkedNodes() + 0x97
     at ILCompiler.ILScanner.ILCompiler.IILScanner.Scan() + 0x19
     at ILCompiler.Program.<Run>g__RunScanner|4_0(Program.<>c__DisplayClass4_0&) + 0x19a
     at ILCompiler.Program.Run() + 0x25c0
     at ILCompiler.ILCompilerRootCommand.<>c__DisplayClass221_0.<.ctor>b__0(ParseResult result) + 0x310

To reproduce:

git clone https://github.com/tmds/Tmds.DBus
cd Tmds.DBus/samples/MediaPlayerRemote/
dotnet publish /p:PublishAot=true /p:TargetFramework=net8.0 /p:TargetFrameworks=net8.0

There are a lot of warnings when I publish the app which I still need to address.
It could be that the exception goes away when I've done that.

$ dotnet --version
8.0.101

cc @MichalStrehovsky

Author: tmds
Assignees: -
Labels:

untriaged, area-NativeAOT-coreclr

Milestone: -

@tmds tmds removed the untriaged New issue has not been triaged by the area owner label Feb 22, 2024
@jkotas
Copy link
Member

jkotas commented Feb 22, 2024

https://github.com/tmds/Tmds.DBus/blob/f201319123ff733718df0e6598d25559a9be9fb0/src/Tmds.DBus.Protocol/TypeModel.cs#L72-L81

This code looks like a bug. I think it should be type == typeof(Struct<>).

We should not crash on buggy code like this, but fixing this bug in your library is likely going to make the crash go away.

@tmds
Copy link
Member Author

tmds commented Feb 22, 2024

This code looks like a bug. I think it should be type == typeof(Struct<>).

@jkotas that's indeed a bug, and also what is causing the crash. Thanks!!

I'll leave this open to track improving the compiler so it doesn't crash for this.
Feel free to close if you want to track it elsewhere.

@jkotas jkotas modified the milestones: 8.0.x, 9.0.0 Feb 22, 2024
MichalStrehovsky added a commit to MichalStrehovsky/runtime that referenced this issue Feb 22, 2024
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Feb 22, 2024
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Feb 23, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Mar 24, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants
@tmds @jkotas and others