dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.99k stars 4.03k forks source link

InvalidProgramException: Common Language Runtime detected an invalid program. #61055

Closed colejohnson66 closed 2 years ago

colejohnson66 commented 2 years ago

This used to not happen, but I recently updated my SDK and now I can't work on my program. In debug mode, the exception results. However, in release mode, it launches, but I'm left with a black window. The documentation for this exception implies it's a bug in the compiler.

I've tried dotnet clean, and even deleted the bin and obj folders, but it did not fix the issue.

Version Used: 17.0 (SDK 6.0.202)

Steps to Reproduce:

  1. Clone this repo: colejohnson66/iDecryptIt
  2. Build and debug.

Expected Behavior: I can debug my program.

Actual Behavior: I get an exception.

Stack Trace:

System.InvalidProgramException: Common Language Runtime detected an invalid program.
   at iDecryptIt.ViewModels.MainWindowViewModel..ctor()
   at iDecryptIt.App.OnFrameworkInitializationCompleted() in C:\Users\Cole\Desktop\iDecryptIt\iDecryptIt\App.axaml.cs:line 43
   at Avalonia.Controls.AppBuilderBase`1.Setup() in /_/src/Avalonia.Controls/AppBuilderBase.cs:line 312
   at Avalonia.Controls.AppBuilderBase`1.SetupWithLifetime(IApplicationLifetime lifetime) in /_/src/Avalonia.Controls/AppBuilderBase.cs:line 179
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime[T](T builder, String[] args, ShutdownMode shutdownMode) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 208
   at iDecryptIt.Program.Main(String[] args) in C:\Users\Cole\Desktop\iDecryptIt\iDecryptIt\Program.cs:line 37
colejohnson66 commented 2 years ago

JetBrains dotPeek refuses to decompile the constructor:

public MainWindowViewModel()
{
  // ISSUE: unable to decompile the method.
}
Generated IL ```cil .method public hidebysig specialname rtspecialname instance void .ctor() cil managed { .maxstack 6 IL_0000: ldarg.0 // this IL_0001: ldc.i4.0 IL_0002: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_DecryptingRootFS(bool) IL_0007: ldarg.0 // this IL_0008: ldstr "" IL_000d: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_RootFSInput(string) IL_0012: ldarg.0 // this IL_0013: ldstr "" IL_0018: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_RootFSOutput(string) IL_001d: ldarg.0 // this IL_001e: ldstr "" IL_0023: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_RootFSKey(string) IL_0028: ldarg.0 // this IL_0029: ldstr "" IL_002e: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_DecryptInput(string) IL_0033: ldarg.0 // this IL_0034: ldstr "" IL_0039: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_DecryptOutput(string) IL_003e: ldarg.0 // this IL_003f: ldstr "" IL_0044: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_DecryptIV(string) IL_0049: ldarg.0 // this IL_004a: ldstr "" IL_004f: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_DecryptKey(string) IL_0054: ldarg.0 // this IL_0055: ldstr "" IL_005a: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_ExtractInput(string) IL_005f: ldarg.0 // this IL_0060: ldstr "" IL_0065: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_ExtractOutput(string) IL_006a: ldarg.0 // this IL_006b: ldsfld class [System.ObjectModel]System.Collections.ObjectModel.ReadOnlyDictionary`2> [iDecryptIt.Shared]iDecryptIt.Shared.Device::MappingGroupToDevices IL_0070: callvirt instance class [System.ObjectModel]System.Collections.ObjectModel.ReadOnlyDictionary`2/KeyCollection*/> class [System.ObjectModel]System.Collections.ObjectModel.ReadOnlyDictionary`2>::get_Keys() IL_0075: newobj instance void class [System.ObjectModel]System.Collections.ObjectModel.ObservableCollection`1::.ctor(class [System.Runtime]System.Collections.Generic.IEnumerable`1) IL_007a: stfld class [System.ObjectModel]System.Collections.ObjectModel.ObservableCollection`1 iDecryptIt.ViewModels.MainWindowViewModel::'k__BackingField' IL_007f: ldarg.0 // this IL_0080: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_VKGroupSelectedItem(valuetype [System.Runtime]System.Nullable`1) IL_0085: initobj valuetype [System.Runtime]System.Nullable`1 IL_008b: ldarg.0 // this IL_008c: ldc.i4.0 IL_008d: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_VKModelEnabled(bool) IL_0092: ldarg.0 // this IL_0093: newobj instance void class [System.ObjectModel]System.Collections.ObjectModel.ObservableCollection`1::.ctor() IL_0098: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_VKModelList(class [System.ObjectModel]System.Collections.ObjectModel.ObservableCollection`1) IL_009d: ldarg.0 // this IL_009e: ldnull IL_009f: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_VKModelSelectedItem(class [iDecryptIt.Shared]iDecryptIt.Shared.Device) IL_00a4: ldarg.0 // this IL_00a5: ldc.i4.0 IL_00a6: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_VKBuildEnabled(bool) IL_00ab: ldarg.0 // this IL_00ac: newobj instance void class [System.ObjectModel]System.Collections.ObjectModel.ObservableCollection`1::.ctor() IL_00b1: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_VKBuildList(class [System.ObjectModel]System.Collections.ObjectModel.ObservableCollection`1) IL_00b6: ldarg.0 // this IL_00b7: ldnull IL_00b8: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_VKBuildSelectedItem(string) IL_00bd: ldarg.0 // this IL_00be: ldstr "" IL_00c3: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_KeysHeading(string) IL_00c8: ldarg.0 // this IL_00c9: newobj instance void class [System.ObjectModel]System.Collections.ObjectModel.ObservableCollection`1::.ctor() IL_00ce: dup IL_00cf: ldc.i4.1 IL_00d0: ldstr "" IL_00d5: ldc.i4.1 IL_00d6: ldnull IL_00d7: ldstr "" IL_00dc: newobj instance void iDecryptIt.Models.FirmwareItemModel::.ctor(valuetype [iDecryptIt.Shared]iDecryptIt.Shared.FirmwareItemType, string, bool, string, string) IL_00e1: callvirt instance void class [System.Runtime]System.Collections.ObjectModel.Collection`1::Add(!0/*class iDecryptIt.Models.FirmwareItemModel*/) IL_00e6: nop IL_00e7: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_KeyEntries(class [System.ObjectModel]System.Collections.ObjectModel.ObservableCollection`1) IL_00ec: ldarg.0 // this IL_00ed: call instance void iDecryptIt.ViewModels.ViewModelBase::.ctor() IL_00f2: nop IL_00f3: nop IL_00f4: ldarg.0 // this IL_00f5: ldarg.0 // this IL_00f6: ldftn instance void iDecryptIt.ViewModels.MainWindowViewModel::OnDecryptingRootFSSwitch() IL_00fc: newobj instance void [System.Runtime]System.Action::.ctor(object, native int) IL_0101: ldnull IL_0102: ldnull IL_0103: call class [ReactiveUI]ReactiveUI.ReactiveCommand`2 [ReactiveUI]ReactiveUI.ReactiveCommand::Create(class [System.Runtime]System.Action, class [System.Runtime]System.IObservable`1, class [System.Reactive]System.Reactive.Concurrency.IScheduler) IL_0108: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_DecryptingRootFSSwitchCommand(class [ReactiveUI]ReactiveUI.ReactiveCommand`2) IL_010d: nop IL_010e: ldarg.0 // this IL_010f: ldarg.0 // this IL_0110: ldftn instance void iDecryptIt.ViewModels.MainWindowViewModel::OnRootFSOpen(string) IL_0116: newobj instance void class [System.Runtime]System.Action`1::.ctor(object, native int) IL_011b: ldnull IL_011c: ldnull IL_011d: call class [ReactiveUI]ReactiveUI.ReactiveCommand`2 [ReactiveUI]ReactiveUI.ReactiveCommand::Create(class [System.Runtime]System.Action`1, class [System.Runtime]System.IObservable`1, class [System.Reactive]System.Reactive.Concurrency.IScheduler) IL_0122: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_RootFSOpenCommand(class [ReactiveUI]ReactiveUI.ReactiveCommand`2) IL_0127: nop IL_0128: ldarg.0 // this IL_0129: ldarg.0 // this IL_012a: ldftn instance void iDecryptIt.ViewModels.MainWindowViewModel::OnRootFSCopyKey() IL_0130: newobj instance void [System.Runtime]System.Action::.ctor(object, native int) IL_0135: ldnull IL_0136: ldnull IL_0137: call class [ReactiveUI]ReactiveUI.ReactiveCommand`2 [ReactiveUI]ReactiveUI.ReactiveCommand::Create(class [System.Runtime]System.Action, class [System.Runtime]System.IObservable`1, class [System.Reactive]System.Reactive.Concurrency.IScheduler) IL_013c: stfld class [ReactiveUI]ReactiveUI.ReactiveCommand`2 iDecryptIt.ViewModels.MainWindowViewModel::'k__BackingField' IL_0141: ldarg.0 // this IL_0142: ldarg.0 // this IL_0143: ldftn instance void iDecryptIt.ViewModels.MainWindowViewModel::OnDecryptOpen(string) IL_0149: newobj instance void class [System.Runtime]System.Action`1::.ctor(object, native int) IL_014e: ldnull IL_014f: ldnull IL_0150: call class [ReactiveUI]ReactiveUI.ReactiveCommand`2 [ReactiveUI]ReactiveUI.ReactiveCommand::Create(class [System.Runtime]System.Action`1, class [System.Runtime]System.IObservable`1, class [System.Reactive]System.Reactive.Concurrency.IScheduler) IL_0155: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_DecryptOpenCommand(class [ReactiveUI]ReactiveUI.ReactiveCommand`2) IL_015a: nop IL_015b: ldarg.0 // this IL_015c: ldarg.0 // this IL_015d: ldftn instance void iDecryptIt.ViewModels.MainWindowViewModel::OnDecrypt() IL_0163: newobj instance void [System.Runtime]System.Action::.ctor(object, native int) IL_0168: ldnull IL_0169: ldnull IL_016a: call class [ReactiveUI]ReactiveUI.ReactiveCommand`2 [ReactiveUI]ReactiveUI.ReactiveCommand::Create(class [System.Runtime]System.Action, class [System.Runtime]System.IObservable`1, class [System.Reactive]System.Reactive.Concurrency.IScheduler) IL_016f: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_DecryptCommand(class [ReactiveUI]ReactiveUI.ReactiveCommand`2) IL_0174: nop IL_0175: ldarg.0 // this IL_0176: ldarg.0 // this IL_0177: ldftn instance void iDecryptIt.ViewModels.MainWindowViewModel::OnExtractOpen(string) IL_017d: newobj instance void class [System.Runtime]System.Action`1::.ctor(object, native int) IL_0182: ldnull IL_0183: ldnull IL_0184: call class [ReactiveUI]ReactiveUI.ReactiveCommand`2 [ReactiveUI]ReactiveUI.ReactiveCommand::Create(class [System.Runtime]System.Action`1, class [System.Runtime]System.IObservable`1, class [System.Reactive]System.Reactive.Concurrency.IScheduler) IL_0189: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_ExtractOpenCommand(class [ReactiveUI]ReactiveUI.ReactiveCommand`2) IL_018e: nop IL_018f: ldarg.0 // this IL_0190: ldarg.0 // this IL_0191: ldftn instance void iDecryptIt.ViewModels.MainWindowViewModel::OnExtract() IL_0197: newobj instance void [System.Runtime]System.Action::.ctor(object, native int) IL_019c: ldnull IL_019d: ldnull IL_019e: call class [ReactiveUI]ReactiveUI.ReactiveCommand`2 [ReactiveUI]ReactiveUI.ReactiveCommand::Create(class [System.Runtime]System.Action, class [System.Runtime]System.IObservable`1, class [System.Reactive]System.Reactive.Concurrency.IScheduler) IL_01a3: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_ExtractCommand(class [ReactiveUI]ReactiveUI.ReactiveCommand`2) IL_01a8: nop IL_01a9: ldarg.0 // this IL_01aa: ldarg.0 // this IL_01ab: ldftn instance void iDecryptIt.ViewModels.MainWindowViewModel::OnViewKeys() IL_01b1: newobj instance void [System.Runtime]System.Action::.ctor(object, native int) IL_01b6: ldnull IL_01b7: ldnull IL_01b8: call class [ReactiveUI]ReactiveUI.ReactiveCommand`2 [ReactiveUI]ReactiveUI.ReactiveCommand::Create(class [System.Runtime]System.Action, class [System.Runtime]System.IObservable`1, class [System.Reactive]System.Reactive.Concurrency.IScheduler) IL_01bd: call instance void iDecryptIt.ViewModels.MainWindowViewModel::set_ViewKeysCommand(class [ReactiveUI]ReactiveUI.ReactiveCommand`2) IL_01c2: nop IL_01c3: ldarg.0 // this IL_01c4: newobj instance void [System.Reactive]System.Reactive.Disposables.CompositeDisposable::.ctor() IL_01c9: stfld class [System.Reactive]System.Reactive.Disposables.CompositeDisposable iDecryptIt.ViewModels.MainWindowViewModel::_disposables IL_01ce: ldarg.0 // this IL_01cf: ldarg.0 // this IL_01d0: ldfld class [System.Reactive]System.Reactive.Disposables.CompositeDisposable iDecryptIt.ViewModels.MainWindowViewModel::_disposables IL_01d5: call instance void iDecryptIt.ViewModels.MainWindowViewModel::Subscribe(class [System.Reactive]System.Reactive.Disposables.CompositeDisposable) IL_01da: nop IL_01db: ret } // end of method MainWindowViewModel::.ctor ```
cston commented 2 years ago

@colejohnson66, does peverify.exe report any errors for the assembly that might indicate the source of the issue? Thanks.

colejohnson66 commented 2 years ago

@cston Running peverify produces this output:

C:\Users\Cole\Desktop\iDecryptIt\iDecryptIt\bin\Debug\net6.0>peverify iDecryptIt.dll

Microsoft (R) .NET Framework PE Verifier.  Version  4.0.30319.0
Copyright (c) Microsoft Corporation.  All rights reserved.

[MD]: Error: MethodImpl's Decl (token=0x0a0000e6) and Body (token=0x06000091) method signatures do not match. [token:0x19000003]
1 Error(s) Verifying C:\Users\Cole\Desktop\iDecryptIt\iDecryptIt\bin\Debug\net6.0\iDecryptIt.dll

If needed, I can zip up some files (or the entire output directory) and upload it somewhere.

jaredpar commented 2 years ago

@colejohnson66

I took a quick look at your repo and noticed that you're using Fody as a part of your build. That is an IL weaving tool that, if it has bugs, can lead to invalid IL and the exceptions you posted here. Looking at the ctor in question nothing stands out as a potential issue to me, the constructor is very straight forward. It leads me to suspect this is actually an issue in the Fody package.

Can you try removing the ReactiveUI.Fody package and see if the constructor continues to generate invalid IL?

colejohnson66 commented 2 years ago

Yes. Removing ReactiveUI.Fody did fix the issue. That doesn't explain why this wasn't an issue previously. I have not updated the NuGet package at all; I've been using ReactiveUI.Fody 17.1.50 since the start of the project and haven't had issues...

This is weird. If I checkout a commit I know was working, it still crashes? Even if I build it using the 6.0.100 SDK. I've tried clearing my NuGet cache and deleting the bin and obj directories...

jaredpar commented 2 years ago

Closing as this does not appear to be a Roslyn issue but instead an issue with ReactiveUI.Fody. I suggest filing an issue on their repository to track down this particular problem.

Unfortunately I don't have a lot of insight into why this particular error would start showing up recently. Generally I expect an issue like this to happen when you update compilers and they produce a subtly different IL pattern that Fody wasn't expecting or there is an update to Fody, direct or indirect, that brings in a new version that has a bug.

colejohnson66 commented 2 years ago

Ah. I've figured it out. You're correct; this is an issue with ReactiveUI.Fody that only occurs with Nullable<T> objects: https://github.com/reactiveui/ReactiveUI/issues/2688

Specifically, setting this line:

[Reactive] public DeviceGroup? VKGroupSelectedItem { get; set; } = null;