LiteDB icon indicating copy to clipboard operation
LiteDB copied to clipboard

[BUG] LiteDB 5.0.X crashes in iOS due to Emitting dynamic code

Open ryancheung opened this issue 3 years ago • 17 comments

Version

LiteDB 5.X/iOS/.NET 6.0

Describe the bug

The LiteDB 5.0.X crashes in .NET 6.0 ios but LiteDB 4.0.X works fine. The linq expressions used in 5.0.X seems not AOT friendly any more. The expression compiling of some of them trigger usage of System.Reflection.Emit.

Code to Reproduce

https://github.com/ryancheung/LiteDB-iOS-Crash-Repro

Expected behavior

It should not crash with emitting dynamic code while System.Linq.Expressions.Expression.Compile().

Screenshots/Stacktrace

Attempting to JIT compile method '(wrapper dynamic-method) LiteDB.BsonValue object:Thunk1ret_BsonValue_IEnumerable`1_BsonDocument_BsonValue_Collation_BsonDocument (System.Func`2<object[], object>,System.Collections.Generic.IEnumerable`1<LiteDB.BsonDocument>,LiteDB.BsonDocument,LiteDB.BsonValue,LiteDB.Collation,LiteDB.BsonDocument)' while running in aot-only mode. See https://docs.microsoft.com/xamarin/ios/internals/limitations for more information.
 (System.ExecutionEngineException)
   at System.Delegate.CreateDelegate(Type , Object , MethodInfo , Boolean , Boolean )
   at System.Delegate.CreateDelegate(Type , Object , MethodInfo , Boolean )
   at System.Delegate.CreateDelegate(Type , Object , MethodInfo )
   at System.Reflection.Emit.DynamicMethod.CreateDelegate(Type , Object )
   at System.Dynamic.Utils.DelegateHelpers.CreateObjectArrayDelegateRefEmit(Type , Func`2 )
   at System.Dynamic.Utils.DelegateHelpers.CreateObjectArrayDelegate(Type , Func`2 )
   at System.Linq.Expressions.Interpreter.LightLambda.MakeDelegate(Type )
   at System.Linq.Expressions.Interpreter.LightDelegateCreator.CreateDelegate(IStrongBox[] )
   at System.Linq.Expressions.Interpreter.LightDelegateCreator.CreateDelegate()
   at System.Linq.Expressions.Expression`1[[LiteDB.BsonExpressionScalarDelegate, LiteDB, Version=5.0.11.0, Culture=neutral, PublicKeyToken=4ee40123013c9f27]].Compile()
   at LiteDB.BsonExpression.<>c__DisplayClass70_0.<Compile>b__0(String s)
   at System.Collections.Concurrent.ConcurrentDictionary`2[[System.String, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[LiteDB.BsonExpressionScalarDelegate, LiteDB, Version=5.0.11.0, Culture=neutral, PublicKeyToken=4ee40123013c9f27]].GetOrAdd(String , Func`2 )
   at LiteDB.BsonExpression.Compile(BsonExpression expr, ExpressionContext context)
   at LiteDB.BsonExpression.ParseAndCompile(Tokenizer tokenizer, BsonExpressionParserMode mode, BsonDocument parameters, DocumentScope scope)
   at LiteDB.BsonExpression.Create(Tokenizer tokenizer, BsonExpressionParserMode mode, BsonDocument parameters)
   at LiteDB.BsonExpression.Create(String expression, BsonDocument parameters)
   at LiteDB.BsonExpression.Create(String expression)
   at LiteDB.BsonExpression..cctor()

ryancheung avatar Oct 13 '21 13:10 ryancheung

+1

Spent hours today trying to figure out why only iOS was crashing in my Maui blazor hybrid app, and finally figured out how to get the actual error and stack trace. It was this same issue. Too bad it's still broken almost a year later.

gvs55 avatar Jul 29 '22 07:07 gvs55

Does anybody found a solution?

7702244 avatar Dec 04 '22 17:12 7702244

Not quite a solution but a workaround. You can try to use interpreter-mode instead of LLVM. Comes with a performance hit but is so far the only workaronud I found. Add the following to your iOS-Release target in your csproj-file: <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0-ios|AnyCPU'"> <UseInterpreter>true</UseInterpreter> </PropertyGroup>

DDHSchmidt avatar Dec 19 '22 14:12 DDHSchmidt

We are also experiencing the same issue. Hope to see a fix soon

shmoe01 avatar May 02 '23 17:05 shmoe01

Just ran into this as well

Seems like an awfully important bug to be just... completely ignoring? LiteDB was always my go to for cross platform work, and missing iOS makes it pretty useless if I want to use something remotely recent.

lylerolleman avatar Aug 23 '23 21:08 lylerolleman

@lylerolleman Actually there has been some progress on Microsoft's side as well. As far as I understood there are supposed to be mitigations in .Net 8 preview 7 so that this exception shouldn't occur anymore. Unfortunately Preview 7 has other, intolerable errors for my project so I'm unable to test this until the next Preview of .Net 8 is released :/

DDHSchmidt avatar Aug 24 '23 08:08 DDHSchmidt

Not quite a solution but a workaround. You can try to use interpreter-mode instead of LLVM. Comes with a performance hit but is so far the only workaronud I found. Add the following to your iOS-Release target in your csproj-file: <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0-ios|AnyCPU'"> <UseInterpreter>true</UseInterpreter> </PropertyGroup>

It worked for me

olekssokol avatar Sep 23 '23 17:09 olekssokol

Not quite a solution but a workaround. You can try to use interpreter-mode instead of LLVM. Comes with a performance hit but is so far the only workaronud I found. Add the following to your iOS-Release target in your csproj-file: <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0-ios|AnyCPU'"> <UseInterpreter>true</UseInterpreter> </PropertyGroup>

It worked, thx

XhaiiCo avatar Oct 25 '23 07:10 XhaiiCo