Bug 58424 - Xamarin iOS : EF Core crash when I add row to table with nullable type or with a lot of columns
Summary: Xamarin iOS : EF Core crash when I add row to table with nullable type or wit...
Status: RESOLVED DUPLICATE of bug 59184
Alias: None
Product: iOS
Classification: Xamarin
Component: Mono runtime / AOT compiler ()
Version: XI 10.12 (d15-3)
Hardware: Macintosh Mac OS
: --- blocker
Target Milestone: Future Cycle (TBD)
Assignee: Zoltan Varga
URL:
Depends on:
Blocks:
 
Reported: 2017-07-26 23:04 UTC by Diego Vega
Modified: 2017-12-31 15:56 UTC (History)
9 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED DUPLICATE of bug 59184

Description Diego Vega 2017-07-26 23:04:45 UTC
Below is a copy of the original issue as reported at https://github.com/aspnet/EntityFramework/issues/9249 by https://github.com/Thetyne. Repro code is provided below as a github repo. From the EF Core perspective, we would like to know if this is expected, if there anything Xamarin, EF Core or the user can do to overcome it. Please feel free to follow up on the original issue on github or on email.
			  
EntityFramework crash when I insert a row inside table. it crash only on iOS Device (iPod Touch and iPad mini 4).

I have try successfully onAndroid Device and iOS Simulator.
But when i use on iOS device, it fall to Xamarin iOS limitation.

First way : when i have a nullable type inside TrainingDayRow table (exemple : int? unit)
```
Exception message: 
Stack trace:

{System.ExecutionEngineException: Attempting to JIT compile method '(wrapper runtime-invoke) <Module>:runtime_invoke_void__this___object_int_int_int_int_DateTime_DateTime_DateTime_Nullable`1<int> (object,intptr,intptr,intptr)' while running in aot-only mode. See https://developer.xamarin.com/guides/ios/advanced_topics/limitations/ for more information.
at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.12.0.14/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:661 
 --- End of stack trace from previous location where exception was thrown ---
```

Second way : Have a table with a lot of column (here on TrainingExerciseRow with 14 columns)
```
Exception message: 
Stack trace:

{System.ExecutionEngineException: Attempting to JIT compile method '(wrapper runtime-invoke) <Module>:runtime_invoke_void__this___object_int_int_int_int_DateTime_DateTime_DateTime_int (object,intptr,intptr,intptr)' while running in aot-only mode. See https://developer.xamarin.com/guides/ios/advanced_topics/limitations/ for more information.
at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
 at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.12.0.14/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:661 
--- End of stack trace from previous location where exception was thrown ---
```

### Steps to reproduce
Clone my Github source and just click to "+".

Source code
[https://github.com/Thetyne/XamarinTodo](https://github.com/Thetyne/XamarinTodo)

### Further technical details
EntityFrameworkCore 2.0.0-preview2-final
NetStandard 2.0.0.0-preview2-25401-01
Database Provider: Microsoft.EntityFrameworkCore.Sqlite
Operating system: iOS/Macosx
IDE: Visual Studio Community 2017 for Mac Version 7.1 Preview (7.1 build 1281)
Comment 1 Marek Safar 2017-07-27 10:02:26 UTC
It looks like our AOT vtype generic heuristic is not able to catch this case

Zoltan, thoughts on that?
Comment 2 Zoltan Varga 2017-07-28 03:24:58 UTC
How do I compile this project ?
Comment 4 Manuel de la Peña [MSFT] 2017-08-02 15:56:25 UTC
Setting it to assigned to keep the bug backlog clean. @Zoltan I suppose you'll take care of the bug from here.
Comment 5 William Gilmour 2017-08-09 14:43:38 UTC
I can confirm I also have an issue with this. When loading or creating a model with a Nullable field using EFCore, it fails with an exception: 

System.ExecutionEngineException: Attempting to JIT compile method '(wrapper runtime-invoke) <Module>:runtime_invoke_void__this___int_byte_byte_byte_byte_byte_object_object_object_object_object_object_object_object_object_object_Nullable1<int> (object,intptr,intptr,intptr)' while running in aot-only mode. See https://developer.xamarin.com/guides/ios/advanced_topics/limitations/ for more information.
at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
  at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00002] in /Library/Frameworks/Xamarin.iOS.framework/Versions/10.10.0.36/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:661 
--- End of stack trace from previous location where exception was thrown ---

And as mentioned in the original issue from Github, it only happens on the actual iOS device, it works fine on the simulator.
Comment 6 Zoltan Varga 2017-08-09 17:25:08 UTC
How do I compile the test project ?
Comment 7 William Gilmour 2017-08-10 17:38:47 UTC
I actually couldn't get the project linked in by Diego working either, so I created my own one: https://github.com/ns-wgilmour/EFCoreNullables, which produces the errors on my device. 

It should just simply be a case of pull, restore packages, compile and go, using Visual Studio Community for Mac version 7.0.1, and Xamarin.iOS 10.10.0.36.

Doing this little test project seems to indicate that it may actually be just the number of columns that is causing the problem, as I can seem to create types with Nullables and a small number (3, including primary and foreign key) of fields no problem.
Comment 8 Zoltan Varga 2017-08-11 01:33:01 UTC
Can't reproduce using the test case, does it also happen if you set the supported architectures to 'ARM64' ?
Comment 9 William Gilmour 2017-08-11 12:01:15 UTC
No it still produces the errors with the ARM64 architectures. The only things I can think of are what version of Mono are you using? And are you sure you're running the test cases on an actual iOS device? They do not appear in the iOS Simulator. 

This is my full About output:
=== Visual Studio Community 2017 for Mac ===

Version 7.0.1 (build 24)
Installation UUID: dc90ad27-f19b-46de-84ed-cdc179b236c3
Runtime:
	Mono 5.0.1.1 (2017-02/5077205) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 500010001

=== NuGet ===

Version: 4.0.0.2323

=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet
SDK: /usr/local/share/dotnet/sdk/1.0.1/Sdks
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.0.1/lib/mono/msbuild/15.0/bin/Sdks

=== Xamarin.Profiler ===

'/Applications/Xamarin Profiler.app' not found

=== Xamarin.Android ===

Version: 7.3.1.2 (Visual Studio Community)
Android SDK: /Users/Will/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		6.0 (API level 23)

SDK Tools Version: 25.1.2
SDK Platform Tools Version: 24.0.0
SDK Build Tools Version: 23.0.2

Java SDK: /usr
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Xamarin Inspector ===

Version: 1.2.2
Hash: b71b035
Branch: d15-1
Build date: Fri, 21 Apr 2017 17:57:12 GMT

=== Apple Developer Tools ===

Xcode 8.3.3 (12175.1)
Build 8E3004b

=== Xamarin.iOS ===

Version: 10.10.0.36 (Visual Studio Community)
Hash: d2270eec
Branch: d15-2
Build date: 2017-05-22 16:30:53-0400

=== Xamarin.Mac ===

Version: 3.4.0.36 (Visual Studio Community)

=== Build Information ===

Release ID: 700010024
Git revision: 7ab1ca2ced6f584e56b7a0d4d321d00775cd95c9
Build date: 2017-05-19 05:44:51-04
Xamarin addins: 08d17158f3365beee5e60f67999e607cce4b3f93
Build lane: monodevelop-lion-d15-2

=== Operating System ===

Mac OS X 10.12.4
Darwin 16.5.0 Darwin Kernel Version 16.5.0
    Fri Mar  3 16:52:33 PST 2017
    root:xnu-3789.51.2~3/RELEASE_X86_64 x86_64
Comment 10 Thomas Williams 2017-08-29 15:30:05 UTC
Hello, we are getting the same issue using EF Core on iOS with Xamarin Forms. Works fine in the simulator but crashes on the device. This is a huge blocking point for us, is there any workaround or eta?
Comment 11 Zoltan Varga 2017-08-29 19:00:59 UTC
Can anyone else reproduce using the testcase ?
Comment 12 Rolf Bjarne Kvinge [MSFT] 2017-08-30 10:32:34 UTC
@Zoltan, yes I can reproduce, but the test project from comment #7 doesn't crash, since it catches the ExecutionEngineExceptions (and prints them out).

Some of the application output looks like this:

// ----- NullableMultiFields ----- //

First BigFoos length: 0
Create BigFoo without related Foo: Attempting to JIT compile method '(wrapper runtime-invoke) <Module>:runtime_invoke_void__this___int_object_object_object_object_object_object_object_object_object_object_object_object_object_Nullable`1<int>_object (object,intptr,intptr,intptr)' while running in aot-only mode. See https://developer.xamarin.com/guides/ios/advanced_topics/limitations/ for more information.

Create BigFoo with related Foo: Attempting to JIT compile method '(wrapper runtime-invoke) <Module>:runtime_invoke_void__this___int_object_object_object_object_object_object_object_object_object_object_object_object_object_Nullable`1<int>_object (object,intptr,intptr,intptr)' while running in aot-only mode. See https://developer.xamarin.com/guides/ios/advanced_topics/limitations/ for more information.

BigFoo with related Foo manually created
Second BigFoo load after manual create: Attempting to JIT compile method '(wrapper runtime-invoke) <Module>:runtime_invoke_void__this___int_object_object_object_object_object_object_object_object_object_object_object_object_object_Nullable`1<int>_object (object,intptr,intptr,intptr)' while running in aot-only mode. See https://developer.xamarin.com/guides/ios/advanced_topics/limitations/ for more information.

I reproduced with our xcode9 preview (since that's what I had installed).
Comment 13 Zoltan Varga 2017-08-30 12:17:53 UTC
I can reproduce, it tries to do a method invoke on this method:
"Microsoft.EntityFrameworkCore.ChangeTracking.Internal.Snapshot`20<int, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string, string>:.ctor (int,string,string,string,string,string,string,string,string,string,string,string,string,string,string,string,string,string,string,string)"

xamarin.ios currently has some limits on what methods can be dynamically invoked, this method has too arguments.
Comment 14 Zoltan Varga 2017-08-30 12:18:07 UTC
too many arguments.
Comment 15 Diego Vega 2017-08-30 12:26:17 UTC
EF Core uses up to 30 generic arguments in this type. See https://github.com/aspnet/EntityFrameworkCore/blob/dev/src/EFCore/ChangeTracking/Internal/Snapshot.cs

How hard would it be to increase the number of arguments Xamarin.iOS can handle?
Comment 16 Diego Vega 2017-08-30 12:27:05 UTC
Moreover, what is the current maximum?
Comment 17 Thomas Williams 2017-09-02 22:30:03 UTC
When this is fixed, is there an eta for release as this is delaying our release for iOS.
Comment 18 Zoltan Varga 2017-09-03 03:24:54 UTC
The limit for parameters is about 14.
Comment 19 Zoltan Varga 2017-09-03 04:52:04 UTC
It will take some time to fix this since it is low level code and it needs to work on multiple platforms. It might be better to try to rewrite the c# code to use less arguments.
Comment 20 Diego Vega 2017-09-03 05:19:19 UTC
Thanks Zoltan. We will discuss if thi is feasible in our next triage. We certainly have a threshold over which we use a different mechanism, so it is possible that we could just tweak it to make EF Core work within the current limit.

It would be great if we could have an exact number to work with.
Comment 21 Zoltan Varga 2017-09-03 05:46:45 UTC
On arm64, its 8+6=14, or arm its 4+10=14. This includes the this pointer as well.
Comment 22 Zoltan Varga 2017-09-24 08:44:46 UTC
Tracked in #59184.

*** This bug has been marked as a duplicate of bug 59184 ***
Comment 23 gidanmx2 2017-12-31 15:56:25 UTC
I'have the same problem, when table have more than 13 colums I get this error:

Attempting to JIT compile method '(wrapper runtime-invoke) <Module>:runtime_invoke_void__this___object_byte_object_object_object_object_object_object_object_object_object_int_object_byte (object,intptr,intptr,intptr)' while running in aot-only mode. See https://developer.xamarin.com/guides/ios/advanced_topics/limitations/ for more information.

When is released the fix?