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

iOS: Attempting to JIT compile method '(wrapper dynamic-method) string object:lambda_method1 (System.Runtime.CompilerServices.Closure)' while running in aot-only mode. #47112

Closed
rolfbjarne opened this issue Jan 18, 2021 · 9 comments
Assignees
Milestone

Comments

@rolfbjarne
Copy link
Member

rolfbjarne commented Jan 18, 2021

Description

The following code throws an exception at runtime when running on an iOS device:

var foos = new string [] { "hi", "bye" };
string f = foos.AsQueryable ().First ();

Exception:

System.ExecutionEngineException: Attempting to JIT compile method '(wrapper dynamic-method) string object:lambda_method1 (System.Runtime.CompilerServices.Closure)' while running in aot-only mode. See https://docs.microsoft.com/xamarin/ios/internals/limitations for more information.
    at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure, Boolean allowClosed)
    at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure)
    at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method)
    at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target)
    at System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate()
    at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
    at System.Linq.Expressions.Expression`1[[System.Func`1[[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Compile()
    at System.Linq.EnumerableExecutor`1[[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Execute()
    at System.Linq.EnumerableQuery`1[[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].System.Linq.IQueryProvider.Execute[String](Expression expression)
    at System.Linq.Queryable.First[String](IQueryable`1 source)
    at MySingleView.AppDelegate.FinishedLaunching(UIApplication app, NSDictionary options) in /Users/rolf/work/maccore/onedotnet/xamarin-macios/tests/dotnet/MySingleView/AppDelegate.cs:line 27

Repro:

git clone https://github.com/rolfbjarne/xamarin-macios
cd xamarin-macios
git checkout dotnet-attempting-to-jit-compile-method-lamba_method1
./configure --enable-dotnet
make reset
make all -j8
make install -j8
make mysingleview -C tests/dotnet

Complete output

Configuration

.NET 6.0.100-alpha.1.21060.3

Binlog:
MySingleView.binlog.zip

Regression?

No (new feature)

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Linq.Expressions untriaged New issue has not been triaged by the area owner labels Jan 18, 2021
@ghost
Copy link

ghost commented Jan 18, 2021

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

Issue Details

Description

The following code throws an exception at runtime when running on an iOS device:

var foos = new string [] { "hi", "bye" };
string f = foos.AsQueryable ().First ();

Exception:

System.ExecutionEngineException: Attempting to JIT compile method '(wrapper dynamic-method) string object:lambda_method1 (System.Runtime.CompilerServices.Closure)' while running in aot-only mode. See https://docs.microsoft.com/xamarin/ios/internals/limitations for more information.
    at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure, Boolean allowClosed)
    at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method, Boolean throwOnBindFailure)
    at System.Delegate.CreateDelegate(Type type, Object firstArgument, MethodInfo method)
    at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type delegateType, Object target)
    at System.Linq.Expressions.Compiler.LambdaCompiler.CreateDelegate()
    at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
    at System.Linq.Expressions.Expression`1[[System.Func`1[[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Compile()
    at System.Linq.EnumerableExecutor`1[[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Execute()
    at System.Linq.EnumerableQuery`1[[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].System.Linq.IQueryProvider.Execute[String](Expression expression)
    at System.Linq.Queryable.First[String](IQueryable`1 source)
    at MySingleView.AppDelegate.FinishedLaunching(UIApplication app, NSDictionary options) in /Users/rolf/work/maccore/onedotnet/xamarin-macios/tests/dotnet/MySingleView/AppDelegate.cs:line 27

Repro:

git clone https://github.com/rolfbjarne/xamarin-macios
cd xamarin-macios
git checkout dotnet-attempting-to-jit-compile-method-lamba_method1
./configure --enable-dotnet --enable-xamarin
make reset
make all -j8
make install -j8
make mysingleview -C tests/dotnet

Complete output

Configuration

.NET 6.0.100-alpha.1.21060.3

Binlog:
MySingleView.binlog.zip

Regression?

No (new feature)

Author: rolfbjarne
Assignees: -
Labels:

area-System.Linq.Expressions, untriaged

Milestone: -

@EgorBo
Copy link
Member

EgorBo commented Jan 18, 2021

I suspect LambdaCompiler should always prefer interpretation instead of compilation on iOS.

https://github.com/dotnet/runtime/blob/master/src/libraries/System.Linq.Expressions/src/System.Linq.Expressions.csproj#L6-L9

@marek-safar marek-safar added os-ios Apple iOS and removed untriaged New issue has not been triaged by the area owner labels Jan 20, 2021
@marek-safar marek-safar added this to the 6.0.0 milestone Jan 20, 2021
@steveisok
Copy link
Member

@MaximLipnin See if you can reproduce and give Egor's suggestion a try after.

@MaximLipnin
Copy link
Contributor

I faced a lot of library test failures with similar error in in #50510. I have no any ios device at hand so I'll try re-enabling System.Linq.Expresions test suite in the PR and see any related CI failures.

@steveisok
Copy link
Member

@MaximLipnin try testing against the simulator with aot enabled. You should be able to take Rolf's example and alter the functional test to repro.

@MaximLipnin
Copy link
Contributor

It's reproducible against the simulator when executing

1. ./build.sh -os iOSSimulator -arch x64 -c Release -s mono+libs
2. ./dotnet.sh build /t:Test /p:TargetOS=iOSSimulator /p:TargetArchitecture=x64 /p:Configuration=Release /p:RunAOTCompilation=true  /p:EnableAggressiveTrimming=true src/tests/FunctionalTests/iOS/Simulator/AOT/iOS.Simulator.Aot.Test.csproj

@EgorBo What would be the right way to apply your suggestion? When trying to set <IsInterpreting>true</IsInterpreting>
I get the following error:

/Users/maxim/.nuget/packages/microsoft.dotnet.apicompat/6.0.0-beta.21321.1/build/Microsoft.DotNet.ApiCompat.targets(145,5): error : TypesMustExist : Type 'System.Linq.Expressions.ExpressionCreator<TDelegate>' does not exist in the reference but it does exist in the implementation. [/Users/maxim/dev/runtime/src/libraries/System.Linq.Expressions/src/System.Linq.Expressions.csproj]
/Users/maxim/.nuget/packages/microsoft.dotnet.apicompat/6.0.0-beta.21321.1/build/Microsoft.DotNet.ApiCompat.targets(159,5): error : MatchingRefApiCompat failed - The reference assembly doesn't match all the APIs in the implementation for '/Users/maxim/dev/runtime/artifacts/bin/System.Linq.Expressions/net6.0-Release/System.Linq.Expressions.dll'. To address either fix errors in the reference assembly (referenced as implementation in compat errors for this reverse compat check), add the issues to the baseline file '/Users/maxim/dev/runtime/src/libraries/System.Linq.Expressions/src/MatchingRefApiCompatBaseline.txt' or disable this check by setting RunMatchingRefApiCompat=false in this project. [/Users/maxim/dev/runtime/src/libraries/System.Linq.Expressions/src/System.Linq.Expressions.csproj]

The System.Linq.Expressions.ExpressionCreator type is not in the reference assembly.

@steveisok
Copy link
Member

@ViktorHofer Not quite sure who to ask, but would you happen to know the answer to Maxim's question?

@EgorBo
Copy link
Member

EgorBo commented Jun 30, 2021

@MaximLipnin I guess nobody tested "true" for that condition and it doesn't expose the same public APIs when it's set.
if the only error AppCompat found is

error : TypesMustExist : Type 'System.Linq.Expressions.ExpressionCreator<TDelegate>' does not exist in the reference but it does exist in the implementation. [/Users/maxim/dev/runtime/src/libraries/System.Linq.Expressions/src/System.Linq.Expressions.csproj]

that should not be difficult to fix

@ViktorHofer
Copy link
Member

I guess nobody tested "true" for that condition and it doesn't expose the same public APIs when it's set.
if the only error AppCompat found is

Right, we had that option enabled on some configurations (I believe uap) which don't exist anymore. Nobody noticed that the API surface diverged when using the interpreter mode.

steveisok pushed a commit that referenced this issue Jul 9, 2021
This is an attempt to address #47112 based on Egor's suggestion.

The changes:

- set IsInterpreting MSBuild property to 'true' in case of iOS;
- made System.Linq.Expressions.ExpressionCreator class internal static in order not to introduce new public type; otherwise it throws Type 'System.Linq.Expressions.ExpressionCreator<TDelegate>' does not exist in the reference but it does exist in the implementation.
-updated the iOS simulator functional test for AOT to verify the fix
- added PlatformDetection.IsLinqExpressionsBuiltWithIsInterpretingOnly which checks whether the System.Linq.Expressions is built with IsInterpreting property set to true. To do so, it uses reflection to verify the Expression.Accept method doesn't exist.
- disabled some failing library tests using ActiveIssue + PlatformDetection.
- updated the invariant functional test for the iOS simulator (b/c of Allow restricting cultures creation with any arbitrary names in Globalization Invariant Mode #54247)

Co-authored-by: Stephen Toub <stoub@microsoft.com>
@ghost ghost locked as resolved and limited conversation to collaborators Aug 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants