Bug 36292 - Assignment operator (+=, ++, etc.) on generic items crashes with System.InvalidProgramException: Invalid IL code
Summary: Assignment operator (+=, ++, etc.) on generic items crashes with System.Inval...
Alias: None
Product: iOS
Classification: Xamarin
Component: Mono runtime / AOT compiler ()
Version: XI 9.2
Hardware: Macintosh Mac OS
: High major
Target Milestone: C6SR1
Assignee: Zoltan Varga
: 31883 ()
Depends on:
Reported: 2015-11-26 14:50 UTC by Alexandre Pepin
Modified: 2016-01-13 17:14 UTC (History)
6 users (show)

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

testcase (5.00 KB, application/x-msdownload)
2015-11-27 11:27 UTC, Zoltan Varga
testcase source (370 bytes, application/octet-stream)
2015-11-27 11:30 UTC, Zoltan Varga
Test case for iOS (16.31 KB, application/zip)
2015-12-16 00:14 UTC, Brendan Zagaeski (Xamarin Team, assistant)

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 Alexandre Pepin 2015-11-26 14:50:49 UTC
# Steps to reproduce
Add this code on iPad project

public override void ViewDidLoad()
	IDump d = new Dump();

public interface IDump
	int Index { get; set; }

public class Dump : IDump
	public int Index { get; set; }	

public void AddIndex<T>(T m) where T : IDump

# Expected behavior
To not crash and increment the index

# Actual behavior
It crashes with different exception depending if we are on the simulator or on the device.
On the iPhoneSimulator : System.InvalidProgramException: Invalid IL code in App1.ViewController:AddIndex<T_REF> (T_REF): IL_0003: stloc.0
On the iPad device : System.ExecutionEngineException: Attempting to JIT compile method 'App1.ViewController:AddIndex<App1.ViewController/IDump> (App1.ViewController/IDump)' while running with --aot-only. See http://docs.xamarin.com/ios/about/limitations for more information.

Change "m.Index++" to "m.Index = m.Index + 1"

# Supplemental info (logs, images, videos)
This wasn't a problem before we upgraded to Xamarin 4
It only occurs in Visual Studio 2015. It doesn't crash in Visual Studio 2013

# Test environment (full version information)
Microsoft Visual Studio Professional 2015
Version 14.0.23107.0 D14REL
Microsoft .NET Framework
Version 4.6.00081
Installed Version: Professional
Xamarin (f860fe4)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.
Xamarin.Android (3efa14c)
Visual Studio plugin to enable development for Xamarin.Android.
Xamarin.iOS (3c0ec35)
Visual Studio extension to enable development for Xamarin.iOS.
Comment 1 Sebastien Pouliot 2015-11-26 15:39:38 UTC
It looks like newer MS csc generates code that the mono runtime does not like.

workaround: until a fix is available in a service release either compile with an older VS or on the Mac
Comment 2 Zoltan Varga 2015-11-27 11:27:06 UTC
Created attachment 14017 [details]
Comment 3 Zoltan Varga 2015-11-27 11:30:50 UTC
Created attachment 14018 [details]
testcase source
Comment 4 Zoltan Varga 2015-12-02 03:17:47 UTC
Fixed by the mono bump in maccore 897bdc1f0b882bf00e4b49a65dd7e2a41a23eeea.
Comment 5 Brendan Zagaeski (Xamarin Team, assistant) 2015-12-16 00:14:43 UTC
Created attachment 14308 [details]
Test case for iOS

## Verification status: verified fixed on master development builds

BAD:  Xamarin.iOS   (dab1554) + XamarinVS (019d80f)
BAD:  Xamarin.iOS (66129b2) + XamarinVS (019d80f)
GOOD: Xamarin.iOS (7c8f909) + XamarinVS (019d80f)

(The "GOOD" build is taken from 2 commits after the commit mentioned in Comment 4.)

## Steps to verify

Build and run the attached test case in the "Debug|iPhoneSimulator" configuration using VS 2015 on Windows.

## "BAD" Results

The app crashes due to the unhandled exception described in Comment 0.

### Top of the stack trace from the simulator's console log

> Unhandled Exception:
> System.InvalidProgramException: Invalid IL code in UnifiedSingleViewIphone1.ViewController/Tests:AddIndex<T_REF> (T_REF): IL_0003: stloc.0   
>   at UnifiedSingleViewIphone1.ViewController+Tests.Run () [0x00007] in C:\source\InvalidIL_VS2015_CSC\UnifiedSingleViewIphone1\ViewController.cs:33 
>   at UnifiedSingleViewIphone1.ViewController.ViewDidLoad () [0x00008] in C:\source\InvalidIL_VS2015_CSC\UnifiedSingleViewIphone1\ViewController.cs:17 

## "GOOD" Results

The app successfully launches with no exceptions and shows the expected empty ViewController.
Comment 6 Brendan Zagaeski (Xamarin Team, assistant) 2015-12-16 00:15:33 UTC
## Side note: actual steps that I followed to verify

1. My installed VS 2015 RC was expired. To save the time it would take me to get the IDE updated, I instead just built from the command line:

> C:\>"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" /p:ServerAddress= /p:ServerUser="Mac User" /p:Configuration=Debug /p:Platform="iPhoneSimulator" C:\source\InvalidIL_VS2015_CSC\UnifiedSingleViewIphone1.sln /t:Build

2. Then I launched the app on simulator from the command line on the Mac build host:

> /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/bin/mtouch --launchsim=UnifiedSingleViewIphone1/ec5f7c07ac2ab4a817a61f95775a2630/bin/iPhoneSimulator/Debug/UnifiedSingleViewIphone1.app -sdk 9.1 -device v2:udid=FF7D5965-0E89-4E27-9BEC-FE33A8213F7D

(You would need to modify the UDID to match one of the simulators on your machine. For example, you can look up the ID under "Windows -> Devices" in Xcode.)
Comment 7 Brendan Zagaeski (Xamarin Team, assistant) 2016-01-07 17:06:13 UTC
## Note to the Xamarin QA team to help with testing

Interestingly I cannot replicate the problem using the test case from Comment 5 on Visual Studio 2015 Update 1, but I can still replicate the problem using my old VS 2015 RC installation.

So it would seem that VS 2015 Update 1 does not generate the "interesting" IL code any more, at least for this precise test case. Accordingly, any further verifications of the bug will probably need to be run against VS 2015 RTM (or older) rather than VS 2015 Update 1.
Comment 8 Zoltan Varga 2016-01-13 17:14:57 UTC
*** Bug 31883 has been marked as a duplicate of this bug. ***