Skip to content

[Mono] App crashes with BadImageFormatException: Method has no body #101074

Closed
@simonrozsival

Description

@simonrozsival

Description

In a .NET MAUI app with AOT enabled on MacCatalyst, we're getting an exception System.BadImageFormatException: Method has no body. It seems there is some problem invoking a method delegate when the delegate is a virtual method. The app behaves correctly when running with the interpreter.

The bug occurs in a MAUI app but there doesn't seem to be anything

/cc @BrzVlad @brianrob @vitek-karas

Reproduction Steps

  1. Create a new MAUI app:
dotnet new maui -o MethodHasNoBodyRepro
  1. Add reference to System.Reactive
dotnet add package System.Reactive
  1. Update project file to AOT the whole project:
<PropertyGroup>
    <MtouchInterpreter>-all</MtouchInterpreter>
    <MtouchLink>None</MtouchLink>
</PropertyGroup>
  1. Add the following code to MainPage.xaml.cs:
public MainPage()
{
    InitializeComponent();
    
    var subject = new Subject<object>();
    subject.Subscribe(static x => Console.WriteLine($"OnNext({x})"));
    
    Observable.FromEventPattern(h => StateChanged += h, h => StateChanged -= h)
	  .Subscribe(subject.OnNext); // this one crashes
	  // .Subscribe(x => subject.OnNext(x)); // this one is fine
  
    try
    {
	  StateChanged?.Invoke(this, EventArgs.Empty);
    }
    catch (Exception ex)
    {
	  Console.WriteLine(ex);
	  throw;
    }
}

private event EventHandler? StateChanged;
  1. Build and run the app for MacCatalyst:
dotnet build -c Release -f net8.0-maccatalyst -r maccatalyst-arm64 MethodHasNoBodyRepro.csproj
./bin/Release/net8.0-maccatalyst/maccatalyst-arm64/MethodHasNoBodyRepro.app/Contents/MacOS/MethodHasNoBodyRepro

Expected behavior

The app should print OnNext(...) in console

Actual behavior

The app crashes and prints the following stack trace:

System.BadImageFormatException: Method has no body
IO_FileName_Name, System.Reactive
at System.Reactive.AnonymousSafeObserver`1[[System.Reactive.EventPattern`1[[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Reactive, Version=6.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263]].OnNext(EventPattern`1 value)
at System.Reactive.Subjects.Subject`1[[System.Reactive.EventPattern`1[[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Reactive, Version=6.0.0.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263]].OnNext(EventPattern`1 value)
at System.Reactive.Linq.ObservableImpl.FromEventPattern.Impl`2.<>c__DisplayClass3_0[[System.EventHandler, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].<GetHandler>b__0(Object sender, Object eventArgs)
at MethodHasNoBodyRepro.MainPage..ctor()

Regression?

No response

Known Workarounds

  • Changing .Subscribe(subject.OnNext) to .Subscribe(x => subject.OnNext(x)) fixes the problem
  • Disabling AOT fixes the problem

Configuration

  • .NET 8.0.202
  • .NET MAUI 8.0.7
  • macOS 14.4.1
  • arm64

Other information

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions