Bug 36566 - MethodInfo.MakeGenericMethod(...).Invoke causes "Attempting to JIT compile method" exception only when not using project option "Enable Device Specific Builds"
Summary: MethodInfo.MakeGenericMethod(...).Invoke causes "Attempting to JIT compile me...
Alias: None
Product: iOS
Classification: Xamarin
Component: Mono runtime / AOT compiler ()
Version: XI 9.2
Hardware: PC Mac OS
: High normal
Target Milestone: C6SR1
Assignee: Zoltan Varga
Depends on:
Reported: 2015-12-03 21:59 UTC by Jon Goldberger [MSFT]
Modified: 2015-12-17 11:54 UTC (History)
8 users (show)

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

Test Project (11.67 KB, application/zip)
2015-12-03 21:59 UTC, Jon Goldberger [MSFT]

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:

Description Jon Goldberger [MSFT] 2015-12-03 21:59:29 UTC
Created attachment 14111 [details]
Test Project

## Description

MethodInfo.MakeGenericMethod(...).Invoke causes "Attempting to JIT compile method" exception only when not using project option "Enable Device Specific Builds" and on device only.

## Steps to reproduce

1. Open the attached test project

2. Open the Project Options->iOS Build settings and uncheck the "Enable device-specific builds" option for the Debug|iPhone Configuration|Platform

3. Deploy to an iOS device using Debug configuration. (Tested with iPad air 2 and iPhone 5s, both running iOS 9.1)

4. Open the Application Output pane so you can see console output.

4. Tap the TEST ME button

Expected result: No exception output to console. 

Actual result: The following exception occurs and is written to the console:
>System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ExecutionEngineException: Attempting to JIT compile method '(wrapper runtime-invoke) <Module>:runtime_invoke_bool_Nullable`1<Guid> (object,intptr,intptr,intptr)' while running with --aot-only. See http://docs.xamarin.com/ios/about/limitations for more information.

>  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
>  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00038] in /Users/builder/data/lanes/2506/eb4c1ef1/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:295 
  --- End of inner exception stack trace ---
>  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00050] in /Users/builder/data/lanes/2506/eb4c1ef1/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/corlib/System.Reflection/MonoMethod.cs:303 
>  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) [0x00000] in /Users/builder/data/lanes/2506/eb4c1ef1/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/external/referencesource/mscorlib/system/reflection/methodbase.cs:277 
>  at Test.InvokeTestCrash (System.Object obj, System.Type memberType) [0x0004f] in /Users/apple/Downloads/InvokeTest/InvokeTest/Test.cs:31 
>  at InvokeTest.ViewController.<ViewDidLoad>m__0 (System.Object sender, System.EventArgs e) [0x0005d] in /Users/apple/Downloads/InvokeTest/InvokeTest/ViewController.cs:38 

## Regression status

Customer reported this only occurring after updating to Cycle 6 (Xamarin 4), but I saw this exception in C5 builds as well: - Exception occurs only without "Enable Device Specific Builds" selected - Exception occurs with or without "Enable Device Specific Builds" selected - Exception occurs with or without "Enable Device Specific Builds" selected - "Enable Device Specific Builds" does not seem to work with this version, but exception occurred when not selected. 

So as far back as, this exception occurs. 

## Version info:

=== Xamarin Studio ===

Version 5.10.1 (build 3)
Installation UUID: 964c531b-d928-456b-a9ae-e1f82266b360
	Mono 4.2.1 (explicit/6dd2d0d)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 402010102

=== Xamarin.Profiler ===

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

=== Xamarin.Android ===

Version: (Business Edition)
Android SDK: /Users/apple/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)

SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.0.1
SDK Build Tools Version: 23.0.2

Java SDK: /usr
java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Apple Developer Tools ===

Xcode 7.1.1 (9081)
Build 7B1005

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: eb4c1ef
Branch: master
Build date: 2015-12-01 02:12:30-0500

=== Xamarin.Mac ===

Version: (Business Edition)

=== Build Information ===

Release ID: 510010003
Git revision: f2021a209d66d49cbc0649a6d968b29040e57807
Build date: 2015-12-01 10:43:40-05
Xamarin addins: dfd4f5103e8951edbc8ac24480b53b53c55e04ff
Build lane: monodevelop-lion-cycle6-baseline

=== Operating System ===

Mac OS X 10.11.1
Darwin Jons-iMac.local 15.0.0 Darwin Kernel Version 15.0.0
    Sat Sep 19 15:53:46 PDT 2015
    root:xnu-3247.10.11~1/RELEASE_X86_64 x86_64
Comment 3 Sebastien Pouliot 2015-12-04 02:49:24 UTC
@Zoltan, the devices listed in the description are all 64bits devices.

I suspect that the bug that only affect one (64 bits) arch but might not happen for the device specific build.

Now that should not happen but there was a bug (just fixed in [1] for SR1, c.c. Jeff for confirmation) that would produce a 32 bits builds for a 64 bits device (which is not the optimal device build, but it would hide the bug when selected).

If that's correct then it would mean a 64-bits only issue, 32 bits is fine.

[1] maccore/cycle6-xi 6898dcbb6f6249ede382c78ddeaa4eab2ca919ec
Comment 4 Zoltan Varga 2015-12-04 20:04:31 UTC
	public static bool GetHasValue<T>(T? value) where T : struct
		return value.HasValue;

	public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
		Guid? nullableGuid = Guid.NewGuid();

		var m = typeof(AppDelegate).GetMethod("GetHasValue", BindingFlags.Static | BindingFlags.Public);

		Type type = typeof (Guid?).GetGenericArguments () [0];
		m.MakeGenericMethod (new Type[] {type}).Invoke (null, new object[] { nullableGuid });
                return true;
Comment 5 Zoltan Varga 2015-12-04 22:47:25 UTC
Fixed by mono 2bf0d19a48809f566b59408356a00acc507c991d/mono-extensions 1de9b72bdd71f37b620f37a5d06defa2493a6f35.
Comment 7 Zoltan Varga 2015-12-05 01:53:06 UTC
The commits in comment #5 can be backported to cycle6. They are low risk.
Comment 9 Shruti 2015-12-08 11:30:59 UTC
Reproduce Status:
I have reproduced this issue using code snippet given in comment(4) with XI Getting exception after deploying app on 64 bit device. 
Application output: https://gist.github.com/Shruti360/72936e10427c3da0724a
Screencast: http://www.screencast.com/t/bggVevcA
Device: 64 bit device 5S

Verify Status:

Verified this issue with latest master build. I am not getting any exception after deploying app on 64 bit device using code given in comment(4).
Screencast: http://www.screencast.com/t/aHMTaEBThE
Environment Info:https://gist.github.com/Shruti360/c505e1676822cc3f8c6e

I will close this issue after verifying on Release build.
Comment 10 Jon Goldberger [MSFT] 2015-12-10 00:19:40 UTC
Is there any way to workaround this issue until the fix is released?
Comment 11 Zoltan Varga 2015-12-10 00:25:56 UTC
Avoid using nullable types and MethodInfo.Invoke together.
Comment 12 Francisco 2015-12-11 12:05:46 UTC
Hi guys.

I am having this problem in a production environment, the main feature of the app is not working properly because of this bug and our customer is becoming more nervous.

Please, I ask you if you can set to this fix some priority in the next release.

Comment 13 Brendan Zagaeski (Xamarin Team, assistant) 2015-12-11 16:27:34 UTC
I am updating the target milestone to C6SR1 to match the tentative internal timeline for this bug. That tentative timeline suggests that the fix for this issue will be included in a future preview version of "Cycle 6 – Service Release 1," perhaps as soon as next week.
Comment 14 Francisco 2015-12-16 16:38:13 UTC

do you already know when will be released? :)

Thanks again.
Comment 15 Shruti 2015-12-17 11:54:20 UTC
I have checked this issue with latest C6SR1 monotouch- using code snippet given in comment(4) and observed that Now no exception is raising on deploying the app on 64 bit device. 

Screencast: http://www.screencast.com/t/Fot6wVDGsVFo
Device: 5S
Environment Info:https://gist.github.com/shrutis360/3d269a8dede0da9640ad

Hence, Closing this issue.