Skip to content

Attempting to JIT compile method while running in aot-only mode #12416

@beeradmoore

Description

@beeradmoore

Using ImageSharp in a Xamarin project, it was recently reported of a crash using on iOS device in release mode. I am able to replicate this with the attached project. The problem goes away if you enable mono interpreter (can also repro in debug mode if you disable mono interpreter which is the default for release, not default for debug).

Looking around it appears we should not enable the mono interpreter for release code, but there was initially no issues with apps being approved with this enabled.

From what I understand the exception is throw while trying to build (build meaning not AOT?) the ProcessRgbaScaneline method, this is called in ProcessDefilterdScanline.

Without doing a deep dive on the library myself and taking it on the word of the project creator there is no dynamic/reflected code being executed in this library so the issue is with the Xamarin side of things.

So I guess the questions I have are:

  1. Is it safe to use enable mono interpreter in a production app, if so what art the possible drawbacks?
  2. Is this revealing an issue with the compiler, or is there something that was overlooked in ImageSharp code which is a no-no for Xamarin.iOS apps?
  3. Is it possible and is there anything on the roadmap to improve handling of generics or is this as good as it gets?

(I was unable to try with MAUI and .NET 6 to see if this is still a problem because those have once again decided to not work on my computer again)

Steps to Reproduce

  1. Create new Xamarin.Forms project.
  2. Add the SixLabors.ImageSharp nuget package. (I added it to all projects but it didn't seem to make a difference for this issue)
  3. Add a png image to the Xamarin.Forms project
  4. Load that image into a byte array and try load the image like below.
byte[] imageData = null;
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MainPage)).Assembly;
using (var stream = assembly.GetManifestResourceStream("ImageSharpTest.Resources.icon.png"))
{
    using (var memoryStream = new MemoryStream())
    {
        stream.CopyTo(memoryStream);
        imageData = memoryStream.ToArray();
    }
}

var imageDecoder = new SixLabors.ImageSharp.Formats.Png.PngDecoder();
var image = SixLabors.ImageSharp.Image.Load(imageData, imageDecoder);
DisplayAlert("Loaded image size", $"{image.Width}x{image.Height}", "Okay");

Expected Behavior

App displays an alert showing the width and height of the image.

Actual Behavior

(iOS debug+release build on device only, with mono interpreter disabled)
App crashes with the following error

System.ExecutionEngineException: Attempting to JIT compile method 'void SixLabors.ImageSharp.Formats.Png.PngScanlineProcessor:ProcessRgbaScanline<SixLabors.ImageSharp.PixelFormats.Rgba32> (SixLabors.ImageSharp.Configuration,SixLabors.ImageSharp.Formats.Png.PngHeader&,System.ReadOnlySpan`1<byte>,System.Span`1<SixLabors.ImageSharp.PixelFormats.Rgba32>,int,int)' while running in aot-only mode. See https://docs.microsoft.com/xamarin/ios/internals/limitations for more information.

  at SixLabors.ImageSharp.Formats.Png.PngDecoderCore.ProcessDefilteredScanline[TPixel] (System.ReadOnlySpan`1[T] defilteredScanline, SixLabors.ImageSharp.ImageFrame`1[TPixel] pixels, SixLabors.ImageSharp.Formats.Png.PngMetadata pngMetadata) [0x0013b] in <fd7f20ccdc8d46db928374008bbc5166>:0 
  at (wrapper other) System.Object.gsharedvt_in()
  at (wrapper other) System.Object.gsharedvt_out()
  at SixLabors.ImageSharp.Formats.Png.PngDecoderCore.DecodePixelData[TPixel] (System.IO.Compression.DeflateStream compressedStream, SixLabors.ImageSharp.ImageFrame`1[TPixel] image, SixLabors.ImageSharp.Formats.Png.PngMetadata pngMetadata) [0x000df] in <fd7f20ccdc8d46db928374008bbc5166>:0 
  at (wrapper other) System.Object.gsharedvt_in()
  at (wrapper other) System.Object.gsharedvt_out()
  at SixLabors.ImageSharp.Formats.Png.PngDecoderCore.ReadScanlines[TPixel] (SixLabors.ImageSharp.Formats.Png.PngChunk chunk, SixLabors.ImageSharp.ImageFrame`1[TPixel] image, SixLabors.ImageSharp.Formats.Png.PngMetadata pngMetadata) [0x00047] in <fd7f20ccdc8d46db928374008bbc5166>:0 
  at (wrapper other) System.Object.gsharedvt_in()
  at (wrapper other) System.Object.gsharedvt_out()
  at SixLabors.ImageSharp.Formats.Png.PngDecoderCore.Decode[TPixel] (SixLabors.ImageSharp.IO.BufferedReadStream stream, System.Threading.CancellationToken cancellationToken) [0x00163] in <fd7f20ccdc8d46db928374008bbc5166>:0 
  at SixLabors.ImageSharp.Formats.ImageDecoderUtilities.Decode[TPixel] (SixLabors.ImageSharp.Formats.IImageDecoderInternals decoder, SixLabors.ImageSharp.Configuration configuration, System.IO.Stream stream, System.Func`3[T1,T2,TResult] largeImageExceptionFactory) [0x00008] in <fd7f20ccdc8d46db928374008bbc5166>:0 
  at SixLabors.ImageSharp.Formats.ImageDecoderUtilities.Decode[TPixel] (SixLabors.ImageSharp.Formats.IImageDecoderInternals decoder, SixLabors.ImageSharp.Configuration configuration, System.IO.Stream stream) [0x00000] in <fd7f20ccdc8d46db928374008bbc5166>:0 
  at SixLabors.ImageSharp.Formats.Png.PngDecoder.Decode[TPixel] (SixLabors.ImageSharp.Configuration configuration, System.IO.Stream stream) [0x00007] in <fd7f20ccdc8d46db928374008bbc5166>:0 
  at SixLabors.ImageSharp.Formats.Png.PngDecoder.Decode (SixLabors.ImageSharp.Configuration configuration, System.IO.Stream stream) [0x00000] in <fd7f20ccdc8d46db928374008bbc5166>:0 
  at SixLabors.ImageSharp.Image+<>c__DisplayClass120_0.<Load>b__0 (System.IO.Stream s) [0x00000] in <fd7f20ccdc8d46db928374008bbc5166>:0 
  at SixLabors.ImageSharp.Image.WithSeekableStream[T] (SixLabors.ImageSharp.Configuration configuration, System.IO.Stream stream, System.Func`2[T,TResult] action) [0x00041] in <fd7f20ccdc8d46db928374008bbc5166>:0 
  at SixLabors.ImageSharp.Image.Load (SixLabors.ImageSharp.Configuration configuration, System.IO.Stream stream, SixLabors.ImageSharp.Formats.IImageDecoder decoder) [0x00024] in <fd7f20ccdc8d46db928374008bbc5166>:0 
  at SixLabors.ImageSharp.Image.Load (SixLabors.ImageSharp.Configuration configuration, System.Byte[] data, SixLabors.ImageSharp.Formats.IImageDecoder decoder) [0x0000d] in <fd7f20ccdc8d46db928374008bbc5166>:0 
  at SixLabors.ImageSharp.Image.Load (System.Byte[] data, SixLabors.ImageSharp.Formats.IImageDecoder decoder) [0x00005] in <fd7f20ccdc8d46db928374008bbc5166>:0 
  at ImageSharpTest.MainPage.OnAppearing () [0x0006d] in /Users/bradmoore/Projects/ImageSharpTest/ImageSharpTest/MainPage.xaml.cs:36 
  at Xamarin.Forms.Page.SendAppearing () [0x00045] in D:\a\1\s\Xamarin.Forms.Core\Page.cs:452 
  at Xamarin.Forms.Platform.iOS.PageRenderer.ViewDidAppear (System.Boolean animated) [0x0004d] in D:\a\1\s\Xamarin.Forms.Platform.iOS\Renderers\PageRenderer.cs:215 
  at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:86 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:65 
  at ImageSharpTest.iOS.Application.Main (System.String[] args) [0x00001] in /Users/bradmoore/Projects/ImageSharpTest/ImageSharpTest.iOS/Main.cs:17 

Environment

=== Visual Studio Community 2019 for Mac ===

Version 8.10.7 (build 17)
Installation UUID: 20fb50b3-7d66-4ddf-a724-9a931dba91c9
	GTK+ 2.24.23 (Raleigh theme)
	Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)

	Package version: 612000140

=== Mono Framework MDK ===

Runtime:
	Mono 6.12.0.140 (2020-02/51d876a041e) (64-bit)
	Package version: 612000140

=== Roslyn (Language Service) ===

3.10.0-4.21269.26+029847714208ebe49668667c60ea5b0a294e0fcb

=== NuGet ===

Version: 5.9.0.7134

=== .NET Core SDK ===

SDK: /usr/local/share/dotnet/sdk/6.0.100-preview.7.21379.14/Sdks
SDK Versions:
	6.0.100-preview.7.21379.14
	6.0.100-preview.6.21355.2
	6.0.100-preview.5.21302.13
	5.0.400
	5.0.302
	5.0.301
	5.0.203
	5.0.202
	5.0.201
	5.0.103
	5.0.102
	5.0.101
	3.1.412
	3.1.411
	3.1.410
	3.1.409
	3.1.408
	3.1.407
	3.1.406
	3.1.405
	3.1.404
MSBuild SDKs: /Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/Sdks

=== .NET Core Runtime ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
	6.0.0-preview.7.21377.19
	5.0.9
	5.0.8
	5.0.7
	5.0.6
	5.0.5
	5.0.4
	5.0.3
	5.0.2
	5.0.1
	3.1.18
	3.1.17
	3.1.16
	3.1.15
	3.1.14
	3.1.13
	3.1.12
	3.1.11
	3.1.10

=== .NET Core 3.1 SDK ===

SDK: 3.1.412

=== Xamarin.Profiler ===

Version: 1.6.15.68
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Updater ===

Version: 11

=== Apple Developer Tools ===

Xcode 12.5.1 (18212)
Build 12E507

=== Xamarin.Mac ===

Version: 7.14.0.24 (Visual Studio Community)
Hash: c4b89cddb
Branch: d16-10
Build date: 2021-06-15 22:03:00-0400

=== Xamarin.Android ===

Version: 11.3.0.4 (Visual Studio Community)
Commit: xamarin-android/d16-10/ae14caf
Android SDK: /Users/bradmoore/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		None installed

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 30.0.4
SDK Build Tools Version: 30.0.2

Build Information: 
Mono: b4a3858
Java.Interop: xamarin/java.interop/d16-10@f39db25
ProGuard: Guardsquare/proguard/v7.0.1@912d149
SQLite: xamarin/sqlite/3.35.4@85460d3
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-10@c5732a0

=== Microsoft OpenJDK for Mobile ===

Java SDK: /Users/bradmoore/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_1.8.0.25
1.8.0-25
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Android SDK Manager ===

Version: 16.10.0.12
Hash: e240b8c
Branch: remotes/origin/d16-10
Build date: 2021-08-03 17:48:52 UTC

=== Android Device Manager ===

Version: 16.10.0.14
Hash: e340248
Branch: remotes/origin/d16-10
Build date: 2021-08-03 17:49:10 UTC

=== Xamarin.iOS ===

Version: 14.20.0.24 (Visual Studio Community)
Hash: c4b89cddb
Branch: d16-10
Build date: 2021-06-15 22:03:01-0400

=== Xamarin Designer ===

Version: 16.10.0.119
Hash: 36a2d986f
Branch: remotes/origin/d16-10
Build date: 2021-06-02 19:41:34 UTC

=== Build Information ===

Release ID: 810070017
Git revision: 503b9b053dff2e7f136e96b362a629c199f78a42
Build date: 2021-08-05 12:48:29-04
Build branch: release-8.10

=== Operating System ===

Mac OS X 10.16.0
Darwin 20.6.0 Darwin Kernel Version 20.6.0
    Wed Jun 23 00:26:31 PDT 2021
    root:xnu-7195.141.2~5/RELEASE_X86_64 x86_64

=== Enabled user installed extensions ===

MFractor 4.4.8

Build Logs

Unsure if relevant, but can attach if required.

Example Project (If Possible)

ImageSharpTest.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIf an issue is a bug or a pull request a bug fixiOSIssues affecting iOS

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions