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

(net9.0-ios preview 3) SRE not available when interpreter is enabled #101840

Closed
rdavisau opened this issue May 3, 2024 · 6 comments · Fixed by xamarin/xamarin-macios#20696
Closed

Comments

@rdavisau
Copy link

rdavisau commented May 3, 2024

Description

Regression? from net8.0-ios

Operations involving SRE are expected to fail when interpreter is disabled. However, in net9.0-ios operations fail even when the interpreter is enabled, with the exceptionSystem.PlatformNotSupportedException: Dynamic code generation is not supported on this platform.

edit: updated based on the guided investigation in the issue

Operations involving SRE are expected to fail when interpreter is disabled. However, in net9.0-ios operations fail even when the interpreter is enabled, if <MTouchInterpreter> property is also specified, with the exceptionSystem.PlatformNotSupportedException: Dynamic code generation is not supported on this platform.

Reproduction Steps

Clone and run net8/net9 scripts: https://github.com/rdavisau/ios-net9-sre-repro
Or

  1. Create new net9.0-ios project
  2. Enable interpreter <UseInterpreter>true</UseInterpreter> (edit: and <MTouchInterpreter>-all</MTouchInterpreter>)
  3. Add AssemblyBuilder.DefineDynamicAssembly(new AssemblyName("Emit"), AssemblyBuilderAccess.Run) before end of AppDelegate.FinishedLaunching
  4. Run on simulator or device, in debug or release

Expected behavior

Application will launch

Actual behavior

Application crashes:

System.PlatformNotSupportedException: Dynamic code generation is not supported on this platform.
   at System.Reflection.Emit.AssemblyBuilder.ThrowDynamicCodeNotSupported()
   at System.Reflection.Emit.AssemblyBuilder.EnsureDynamicCodeSupported()
   at System.Reflection.Emit.RuntimeAssemblyBuilder..ctor(AssemblyName n, AssemblyBuilderAccess access)
   at System.Reflection.Emit.AssemblyBuilder.DefineDynamicAssembly(AssemblyName name, AssemblyBuilderAccess access)
   at SRE.AppDelegate.FinishedLaunching(UIApplication application, NSDictionary launchOptions) in /Users/rdavis/Source/ios-net9-sre-repro/AppDelegate.cs:line 19

Regression?

Yes - working in net8.0-ios and earlier. Unsure whether it worked in net9 p2/p1

Known Workarounds

No response

Configuration

dotnet --info

.NET SDK:
 Version:           9.0.100-preview.3.24204.13
 Commit:            81f61d8290
 Workload version:  9.0.100-manifests.f4f7fc79
 MSBuild version:   17.11.0-preview-24178-16+7ca3c98fa

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  14.1
 OS Platform: Darwin
 RID:         osx-arm64
 Base Path:   /usr/local/share/dotnet/sdk/9.0.100-preview.3.24204.13/

.NET workloads installed:
 [ios]
   Installation Source: SDK 9.0.100-preview.3
   Manifest Version:    17.2.9433-net9-p3/9.0.100-preview.3
   Manifest Path:       /usr/local/share/dotnet/sdk-manifests/9.0.100-preview.3/microsoft.net.sdk.ios/17.2.9433-net9-p3/WorkloadManifest.json
   Install Type:        FileBased

 [maui]
   Installation Source: SDK 9.0.100-preview.3
   Manifest Version:    9.0.0-preview.3.10457/9.0.100-preview.3
   Manifest Path:       /usr/local/share/dotnet/sdk-manifests/9.0.100-preview.3/microsoft.net.sdk.maui/9.0.0-preview.3.10457/WorkloadManifest.json
   Install Type:        FileBased


Host:
  Version:      9.0.0-preview.3.24172.9
  Architecture: arm64
  Commit:       9e6ba1f68c

.NET SDKs installed:
  6.0.417 [/usr/local/share/dotnet/sdk]
  7.0.404 [/usr/local/share/dotnet/sdk]
  8.0.100 [/usr/local/share/dotnet/sdk]
  9.0.100-preview.3.24204.13 [/usr/local/share/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.25 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.14 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 9.0.0-preview.3.24172.13 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.25 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.14 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 9.0.0-preview.3.24172.9 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label May 3, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 3, 2024
@vitek-karas vitek-karas added the os-ios Apple iOS label May 3, 2024
@vitek-karas
Copy link
Member

@simonrozsival could you please try this? It looks like the IsDynamicCodeSupported feature switch is disabled in this case, for some reason (at least based on the callstack).

@simonrozsival
Copy link
Member

@vitek-karas @rolfbjarne this regression is probably caused by xamarin/xamarin-macios#19812. We now set the DynamicCodeSupport to false whenever MtouchInterpreter is set to any value. I'm not sure if that's the correct behavior.

In this case the project has the following setting:

<UseInterpreter>true</UseInterpreter>
<MtouchInterpreter>-all</MtouchInterpreter>

When MtouchInterpreter is set, UseInterpreter is completely ignored. I think that when MtouchInterpreter is -all, it should keep DynamicCodeSupport around.

@rdavisau please try removing both of these properties from your .NET 9 project file. I believe the default configuration is the configuration you're trying to express with these settings.

Copy link
Contributor

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

@rdavisau
Copy link
Author

rdavisau commented May 3, 2024

Thanks @simonrozsival, I've given a few combinations a try - altering the .NET 9 project file:

No interpreter related properties, or only <MtouchInterpreter>-all</MtouchInterpreter>:

Crashes with

*** Terminating app due to uncaught exception 'System.PlatformNotSupportedException', reason: 'PlatformNotSupported_ReflectionEmit (System.PlatformNotSupportedException)
   at System.Reflection.Emit.AssemblyBuilder.EnsureDynamicCodeSupported()
   at System.Reflection.Emit.RuntimeAssemblyBuilder..ctor(AssemblyName , AssemblyBuilderAccess )
   at System.Reflection.Emit.AssemblyBuilder.DefineDynamicAssembly(AssemblyName , AssemblyBuilderAccess )
   at SRE.AppDelegate.EmitDown()
   at SRE.AppDelegate.FinishedLaunching(UIApplication application, NSDictionary launchOptions)
   at SRE.AppDelegate.__Registrar_Callbacks__.callback_2_SRE_AppDelegate_FinishedLaunching(IntPtr pobj, IntPtr sel, IntPtr p0, IntPtr p1, IntPtr* exception_gchandle)
'
*** First throw call stack:
(0x1a48dbf24 0x19c772018 0x10538602c 0x10552334c 0x1a6fa3b00 0x1a6fa103c 0x1a6e6fe24 0x1a6e23070 0x1a6d16930 0x1a6ee8698 0x1a6ee83a8 0x1a6c784c0 0x1a6c45e4c 0x1a6c45774 0x1a6c45120 0x1a6c44760 0x1a6d17ea8 0x1a6d166e0 0x1a6d16038 0x1a6de0db0 0x1<…>

Only <UseInterpreter>true</UseInterpreter>:

Works*

--

Although <UseInterpreter> "works", the output is an app package where all the assemblies are interpreted. That's the expected behaviour for just that property, but the configuration that the combination of the two properties should (used to) express is something like "all assemblies are aot'd at compile time, and code generated at runtime is interpreted". The aot'ing of assemblies does seem to occur when the two properties are specified (sod with just <UseInterpreter> is 3.7mb, with both flags 9.8mb), but the runtime generated code isn't able to be interpreted because of that DynamicCodeSupport check, I guess.

(I've added edits to the issue to mention the <MTouchInterpreter> property)

@vcsjones vcsjones removed the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label May 4, 2024
@rolfbjarne
Copy link
Member

@vitek-karas @rolfbjarne this regression is probably caused by xamarin/xamarin-macios#19812. We now set the DynamicCodeSupport to false whenever MtouchInterpreter is set to any value. I'm not sure if that's the correct behavior.

Yes, looks like I got the boolean condition reversed.

Adding this to your csproj should work in the meantime:

<PropertyGroup>
    <DynamicCodeSupport>true</DynamicCodeSupport>
</PropertyGroup>

and then set MtouchInterpreter to your desired value.

rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this issue May 6, 2024
…upported or not. Fixes #dotnet/runtime@101840.

Dynamic code is not supported if the interpreter is not enabled, and the
interpreter is not enabled if the MtouchInterpreter property is *empty*.

This regression was introduced in xamarin#19812.

Fixes dotnet/runtime#101840.
rolfbjarne added a commit to xamarin/xamarin-macios that referenced this issue May 7, 2024
…upported or not. Fixes #dotnet/runtime@101840. (#20563)

Dynamic code is not supported if the interpreter is not enabled, and the
interpreter is not enabled if the MtouchInterpreter property is *empty*.

This regression was introduced in #19812.

Fixes dotnet/runtime#101840.
@rolfbjarne
Copy link
Member

This has been fixed, and the fix will be in preview 5.

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label May 7, 2024
rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this issue Jun 6, 2024
…or not. Fixes #dotnet/runtime@101840. (xamarin#20563)

Dynamic code is not supported if the interpreter is not enabled, and the
interpreter is not enabled if the MtouchInterpreter property is *empty*.

This regression was introduced in xamarin#19812.

Fixes dotnet/runtime#101840.
@github-actions github-actions bot locked and limited conversation to collaborators Jun 7, 2024
rolfbjarne added a commit to xamarin/xamarin-macios that referenced this issue Jun 7, 2024
…or not. Fixes #dotnet/runtime@101840. (#20563) (#20696)

Dynamic code is not supported if the interpreter is not enabled, and the
interpreter is not enabled if the MtouchInterpreter property is *empty*.

This regression was introduced in #19812.

Fixes dotnet/runtime#101840.
rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this issue Jun 17, 2024
…or not. Fixes #dotnet/runtime@101840. (xamarin#20563) (xamarin#20696)

Dynamic code is not supported if the interpreter is not enabled, and the
interpreter is not enabled if the MtouchInterpreter property is *empty*.

This regression was introduced in xamarin#19812.

Fixes dotnet/runtime#101840.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
6 participants