Skip to content

Starting App on iOS fails with System.ExecutingEngineException #80853

Closed

Description

@rolfbjarne

From @jhimes144 on Sun, 08 Jan 2023 04:51:56 GMT

This is an issue I have raised over in Avalonia here AvaloniaUI/Avalonia#9934
To the best of our knowledge, the below method GetStringNative should not have any problem with AOT, and everything works fine on Android with AOT enabled. So far I've reproduced this on two separate code bases.

Steps to Reproduce

  1. Create a new dotnet solution using the Avalonia xplat template. See https://github.com/AvaloniaUI/avalonia-dotnet-templates
  2. Insert needed CodesignKey in project file for the ios project
  3. Build and run the ios project on a physical ios device

Expected Behavior

The app runs

Actual Behavior

The following exception is raised on app startup

System.ExecutionEngineException: Attempting to JIT compile method '(wrapper managed-to-native) intptr Avalonia.OpenGL.GlBasicInfoInterface:wrapper_native_indirect_0x283931e40 (intptr&,int)' while running in aot-only mode. See https://docs.microsoft.com/xamarin/ios/internals/limitations for more information.

  at Avalonia.OpenGL.GlBasicInfoInterface.GetStringNative(Int32 v) in /_/src/Avalonia.OpenGL/DevGenerators/Generator.GetProcAddressInitializationGenerator/globalAvalonia.OpenGL.GlBasicInfoInterface.cs:13
  at Avalonia.OpenGL.GlBasicInfoInterface.GetString(Int32 v) in /_/src/Avalonia.OpenGL/GlBasicInfoInterface.cs:27
  at Avalonia.OpenGL.GlBasicInfoInterface.GetExtensions() in /_/src/Avalonia.OpenGL/GlBasicInfoInterface.cs:43
  at Avalonia.OpenGL.GlInterface.GlContextInfo.Create(GlVersion version, Func`2 getProcAddress) in /_/src/Avalonia.OpenGL/GlInterface.cs:33
  at Avalonia.OpenGL.GlInterface..ctor(GlVersion version, Func`2 getProcAddress) in /_/src/Avalonia.OpenGL/GlInterface.cs:48
  at Avalonia.iOS.GlContext..ctor() in /_/src/iOS/Avalonia.iOS/EaglDisplay.cs:30
  at Avalonia.iOS.EaglFeature..ctor() in /_/src/iOS/Avalonia.iOS/EaglDisplay.cs:17
  at Avalonia.iOS.Platform.Register() in /_/src/iOS/Avalonia.iOS/Platform.cs:34
  at at Avalonia.Controls.AppBuilderBase`1[[Avalonia.AppBuilder, Avalonia.Controls, Version=11.0.0.0, Culture=neutral, PublicKeyToken=c8d484a7012f9a8b]].Setup()
  at at Avalonia.Controls.AppBuilderBase`1[[Avalonia.AppBuilder, Avalonia.Controls, Version=11.0.0.0, Culture=neutral, PublicKeyToken=c8d484a7012f9a8b]].SetupWithLifetime(IApplicationLifetime lifetime)
  at at Avalonia.iOS.AvaloniaAppDelegate`1[[testapp.App, testapp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].FinishedLaunching(UIApplication application, NSDictionary launchOptions)

I have tried adding <ResolveAssemblyConflicts>true</ResolveAssemblyConflicts>, different linker settings, and downgrading the project to .net 6. All attempts failed with same error.

Environment

Visual Studio Community 2022 for Mac
Version 17.4.2 (build 17)
Installation UUID: 04eb36b5-8ade-4881-9f2c-f51e231033e1

Runtime
.NET 6.0.12 (64-bit)
Architecture: X64

Roslyn (Language Service)
4.4.0-6.22578.12+3c6ab8e1715e5b080fb7bb77070810ab71e09387

NuGet
Version: 6.3.1.1

.NET SDK (x64)
SDK: /usr/local/share/dotnet/sdk/7.0.101/Sdks
SDK Versions:
	7.0.101
	6.0.404
MSBuild SDKs: /Applications/Visual Studio.app/Contents/MonoBundle/MSBuild/Current/bin/Sdks

.NET Runtime (x64)
Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
	7.0.1
	6.0.12

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

Updater
Version: 11

Xamarin.Android
Version: 13.1.0.1 (Visual Studio Community)
Commit: xamarin-android/d17-4/13ba222
Android SDK: /Users/jhimes/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		13.0 (API level 33)

SDK Command-line Tools Version: 7.0
SDK Platform Tools Version: 33.0.2
SDK Build Tools Version: 32.0.0

Build Information: 
Mono: a96bde9
Java.Interop: xamarin/java.interop/d17-4@fcc33ce2
SQLite: xamarin/sqlite/3.39.3@23e1ae7
Xamarin.Android Tools: xamarin/xamarin-android-tools/main@0be567a

Microsoft Build of OpenJDK
Java SDK: /Library/Java/JavaVirtualMachines/microsoft-11.jdk
11.0.12
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Eclipse Temurin JDK
Java SDK: /Library/Java/JavaVirtualMachines/temurin-8.jdk
1.8.0.302
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Android SDK Manager
Version: 17.4.0.54
Hash: 6eabb9e
Branch: remotes/origin/d17-4
Build date: 2022-12-13 04:22:32 UTC

Android Device Manager
Version: 0.0.0.1206
Hash: 886af39
Branch: 886af39
Build date: 2022-12-13 04:22:32 UTC

Apple Developer Tools
Xcode 14.2 (21534)
Build 14C18

Xamarin.Mac
Version: 9.0.0.27 (Visual Studio Community)
Hash: 933c6c2c9
Branch: xcode14.1
Build date: 2022-11-22 02:00:36-0500

Xamarin.iOS
Version: 16.1.1.27 (Visual Studio Community)
Hash: 933c6c2c9
Branch: xcode14.1
Build date: 2022-11-22 02:00:37-0500

Xamarin Designer
Version: 17.4.0.136
Hash: d49c9ff6d3
Branch: remotes/origin/d17-4
Build date: 2022-12-13 04:22:27 UTC

Build Information
Release ID: 1704020017
Git revision: 7ef5413f2eec3351ce648085f619fc29fd8cb647
Build date: 2022-12-13 04:20:10+00
Build branch: release-17.4
Build lane: release-17.4

Operating System
Mac OS X 12.6.1
Darwin 21.6.0 Darwin Kernel Version 21.6.0
    Thu Sep 29 20:12:57 PDT 2022
    root:xnu-8020.240.7~1/RELEASE_X86_64 x86_64


Copied from original issue dotnet/macios#17159

Activity

  1. rolfbjarne commented on Jan 19, 2023

    @rolfbjarne
    MemberAuthor

    From @rolfbjarne on Thu, 19 Jan 2023 15:12:23 GMT

    I can reproduce this with the provided sample and running on an iOS device.

    Using ILSpy on this method at Avalonia.OpenGL.GlBasicInfoInterface.GetStringNative(Int32 v) shows:

    .field private method unmanaged stdcall native int *(int32) _addr_GetStringNative
    
    .method public hidebysig 
    	instance native int GetStringNative (
    		int32 v
    	) cil managed 
    {
    	.custom instance void Avalonia.SourceGenerator.GetProcAddressAttribute::.ctor(string) = (
    		01 00 0b 67 6c 47 65 74 53 74 72 69 6e 67 00 00
    	)
    	// Method begins at RVA 0x2330
    	// Header size: 12
    	// Code size: 15 (0xf)
    	.maxstack 2
    	.locals init (
    		[0] method unmanaged stdcall native int *(int32)
    	)
    
    	IL_0000: ldarg.0
    	IL_0001: ldfld method unmanaged stdcall native int *(int32) Avalonia.OpenGL.GlBasicInfoInterface::_addr_GetStringNative
    	IL_0006: stloc.0
    	IL_0007: ldarg.1
    	IL_0008: ldloc.0
    	IL_0009: calli unmanaged stdcall native int(int32)
    	IL_000e: ret
    } // end of method GlBasicInfoInterface::GetStringNative

    and nothing in particular stands out to me, so I'm moving to dotnet/runtime.

  2. ghost added
    untriaged
    on Jan 19, 2023
  3. steveisok commented on Jan 19, 2023

    @steveisok
    Member
  4. JayToltTech commented on May 6, 2023

    @JayToltTech

    FYI, this makes 'hello world' on iOS fail. For me, this implies that .NET on iOS isn't anyone's priority, given that the most basic of apps fails to launch and it's not being investigated.

  5. jhimes144 commented on May 6, 2023

    @jhimes144

    FYI, this makes 'hello world' on iOS fail. For me, this implies that .NET on iOS isn't anyone's priority, given that the most basic of apps fails to launch and it's not being investigated.

    Yup. This issue hasn't received any activity for almost 4 months.

  6. steveisok commented on May 6, 2023

    @steveisok
    Member

    @jhimes144 on android, was full aot being used and you didn't hit this problem? If not, it's very likely the code path was still using jit.

  7. jhimes144 commented on May 7, 2023

    @jhimes144

    @steveisok Full aot was/is being used on android and the problem is not encountered.

  8. vargaz commented on May 7, 2023

    @vargaz
    Contributor

    This particular code pattern, when a function pointer is returned from native code and later called using calli is not supported by full-aot, don't think it ever was. The reason is that the runtime needs to generate a managed-to-native wrapper to call the function pointer, and the wrapper needs to embed the address which is called which is a runtime constant, so it cannot be generated at AOT time.

    Enabling the interpreter might fix the problem.

  9. jhimes144 commented on May 8, 2023

    @jhimes144

    @maxkatz6 Tagging you here

  10. maxkatz6 commented on May 8, 2023

    @maxkatz6
    Contributor

    @vargaz thanks for the explanation.
    To summarize, this scenario isn't supported in mono aot currently? I understand that function pointer isn't known in compile time, but it still possible to generate trampoline function with this runtime pointer and parameters.

    Either way, we will look into possible workarounds.
    But as far as I know the same approach is also used in other OpenGL wrappers for dotnet like Silk.NET

  11. 34 remaining items

  12. steveisok commented on Jun 6, 2023

    @steveisok
    Member

    @danmoseley Thanks for confirming, that is what I found and installed. Then when I deployed my Avalonia-based iOS app, I still go the crash.

    So either the crash is unfixed, or I didn't deploy the new runtime, which led to my question, should I rebuild Avalonia too? Thanks!

    We landed the fix in main, but the backport to 7 hasn't made it yet. Can you try a .net 8 nightly sdk instead?

  13. nref commented on Jun 6, 2023

    @nref

    Having trouble building with net8.0.

    Process so far:

    • Installed dotnet-sdk-8.0.100-preview.6.23305.4-win-x64.exe,
    • Restarted Visual Studio (a few times)
    • Changed TargetFramework from net7.0-ios to net8.0-ios. Didn't touch RuntimeIdentifier, that caused other errors.
    • Re-paired with the mac which then installed the matching runtime.
    • Re-paired again
    • Error 2>C:\Program Files\dotnet\packs\Microsoft.iOS.Sdk\16.4.8531-net8-p6\targets\Xamarin.Shared.Sdk.targets(1039,4): error MSB4057: The target "_ComputeManagedAssemblyToLink" does not exist in the project.

    Googling around points to the linker. MTouchLink=None had no effect.
    Out of time for now.

  14. vargaz commented on Jun 6, 2023

    @vargaz
    Contributor

    The backport PR is:
    #86934

  15. rolfbjarne commented on Jun 6, 2023

    @rolfbjarne
    MemberAuthor
    • Error 2>C:\Program Files\dotnet\packs\Microsoft.iOS.Sdk\16.4.8531-net8-p6\targets\Xamarin.Shared.Sdk.targets(1039,4): error MSB4057: The target "_ComputeManagedAssemblyToLink" does not exist in the project.

    I believe you can work around this by adding this to your csproj:

    <PropertyGroup>
        <_RequiresILLinkPack>true</_RequiresILLinkPack>
    </PropertyGroup>
  16. nref commented on Jun 6, 2023

    @nref

    Thanks @rolfbjarne .

    I added that and now am getting

    2>C:\Program Files\dotnet\packs\Microsoft.iOS.Windows.Sdk\16.4.8531-net8-p6\tools\msbuild\iOS\Xamarin.iOS.Common.After.targets(321,3): error : The illink assembly doesn't exist: ''.

  17. rolfbjarne commented on Jun 6, 2023

    @rolfbjarne
    MemberAuthor

    @slater1 can you try directly from the Mac?

  18. nref commented on Jun 6, 2023

    @nref

    Ok, sure. After following the steps below, on the mac a vanilla avalonia.xplat project builds and runs in the iOS simulator:

    Steps:

    • Installed dotnet-sdk-8.0.100-preview.4.23260.5-osx-arm64.pkg on the mac, then:
    dotnet new install Avalonia.Templates
    dotnet new avalonia.xplat -o test
    cd test
    # manually update ./test.iOS/test.iOS.csproj TFM from `net7.0-iOS` to `net8.0-iOS`
    sudo dotnet workload restore # installs iOS workload etc
    dotnet build
    dotnet run --project ./test.iOS
    

    image

    Not sure what this means for me using Visual Studio from Windows. Later today I can try this with a real iPhone.

  19. ghost removed
    in-pr
    on Jun 9, 2023
  20. DrWenz commented on Jun 21, 2023

    @DrWenz

    i'm facing to the same problem since days, on Simulator it runs perfectly.
    I need to put my app in AppStore, what can i do?

  21. nref commented on Jun 21, 2023

    @nref

    @DrWenz One workaround is to use the mono interpreter.

      <PropertyGroup>
        <UseInterpreter>True</UseInterpreter>
      </PropertyGroup>

    This worked for me to get my Avalonia iOS app running on real devices. Interpreter is slower sometimes than AOT so be sure to performance test your app.

  22. DrWenz commented on Jun 21, 2023

    @DrWenz

    @DrWenz One workaround is to use the mono interpreter.

      <PropertyGroup>
        <UseInterpreter>True</UseInterpreter>
      </PropertyGroup>
    

    This worked for me to get my Avalonia iOS app running on real devices. Interpreter is slower sometimes than AOT so be sure to performance test your app.

    Thx @slater1 works like charm :) saved my day!

  23. SamMonoRT commented on Aug 3, 2023

    @SamMonoRT
    Member

    Based on above comments and backported PR merging, closing this issue. Please re-open if you still encounter the crashes.

  24. ghost locked as resolved and limited conversation to collaborators on Sep 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions