Bug 36292

Summary: Assignment operator (+=, ++, etc.) on generic items crashes with System.InvalidProgramException: Invalid IL code
Product: iOS Reporter: Alexandre Pepin <alexandre.pepin>
Component: Mono runtime / AOT compilerAssignee: Zoltan Varga <vargaz>
Severity: major CC: brendan.zagaeski, eric.goulet, kumpera, masafa, mono-bugs+monotouch, sebastien
Priority: High    
Version: XI 9.2   
Target Milestone: C6SR1   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: testcase
testcase source
Test case for iOS

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. ***