Bug 32282 - Certain C# code when compiled by csc.exe causes "Argument is out of range" at "MonoTouch.Tuner.OptimizeGeneratedCodeSubStep.ProcessCalls()" during linking (possibly related to generics and certain attributes)
Summary: Certain C# code when compiled by csc.exe causes "Argument is out of range" at...
Status: VERIFIED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Tools ()
Version: XI 8.10
Hardware: PC All
: Normal normal
Target Milestone: 8.13 (C6 alpha)
Assignee: Sebastien Pouliot
URL:
: 32792 ()
Depends on:
Blocks:
 
Reported: 2015-07-21 21:22 UTC by psa2ry7rgq
Modified: 2015-08-14 06:45 UTC (History)
6 users (show)

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


Attachments
Test case (21.37 KB, application/zip)
2015-07-21 21:22 UTC, psa2ry7rgq
Details
Diagnostic build output, additional version info (11.02 KB, application/zip)
2015-07-21 21:23 UTC, psa2ry7rgq
Details
The problematic .dll file by itself (4.50 KB, application/x-msdownload)
2015-07-21 21:55 UTC, Brendan Zagaeski (Xamarin Team, assistant)
Details


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:
VERIFIED FIXED

Description psa2ry7rgq 2015-07-21 21:22:21 UTC
Created attachment 12143 [details]
Test case

Certain C# code when compiled by csc.exe causes "Argument is out of range" at "MonoTouch.Tuner.OptimizeGeneratedCodeSubStep.ProcessCalls()" during linking (possibly related to generics and certain attributes)




## Regression status: not a recent regression

BAD: Xamarin.iOS 8.10.4.10 (c57a2c7)
BAD: Xamarin.iOS 8.9.1.3   (f7736a4)


Although this is not technically a regression, the new support for iOS bindings projects in XVS that was added in Cycle 5 means that this issue might now be easier to hit. It seems that the generated code for certain kinds of methods in the `ApiDefinition.cs` file will always hit this error. Additionally the project template for iOS bindings projects has `[assembly:LinkerSafe]` included in the `AssemblyInfo.cs`.




## Steps to reproduce

Attempt to build the attached test case in the "Debug|iPhoneSimulator" configuration (on either Mac or Windows).




## About the test case


- This is a new "Visual C# -> iOS -> iPhone -> Single View App" plus a new "Visual C# -> iOS -> Class Library" created in Visual Studio.


- The app project references a pre-compiled version of the class library, compiled on Windows using `csc.exe`. Recompiling the class library using Mono's `mcs` on Mac stops the problem.


- The class library intentionally has the `[assembly: LinkerSafe]` attribute set in `AssemblyInfo.cs`. The "Debug|iPhoneSimulator" configuration has linking set to `SdkOnly`. The problem happens when attempting to link the class library.


- This test case might seem a bit contrived at first glance. The small "Class1" type in the class library is in fact a trimmed down version of a `.g.cs` file that was generated by an iOS binding project containing the following lines:

> [BaseType(typeof(NSObject))]
> interface Class1
> {
>     [Export("userAlert")]
>     UIAlertView UserAlert { get; set; }
> }




## Results (excerpt from diagnostic build output)

> Linking SDK only for assembly /private/tmp/Working/LinkerArgumentOutOfRangeGenericCollection/UnifiedSingleViewIphone1/bin/iPhoneSimulator/Debug//UnifiedSingleViewIphone1.exe into /private/tmp/Working/LinkerArgumentOutOfRangeGenericCollection/UnifiedSingleViewIphone1/obj/iPhoneSimulator/Debug/mtouch-cache/PreBuild
> MTOUCH: error MT2001: Could not link assemblies. Reason: Argument is out of range.
> --- inner exception
> System.ArgumentOutOfRangeException: Argument is out of range.
>   at Mono.Collections.Generic.Collection`1[Mono.Cecil.Cil.Instruction].get_Item (Int32 index) [0x00000] in <filename unknown>:0 
>   at MonoTouch.Tuner.OptimizeGeneratedCodeSubStep.ProcessCalls (Mono.Cecil.MethodDefinition caller, Int32 i) [0x00000] in <filename unknown>:0 
>   at MonoTouch.Tuner.OptimizeGeneratedCodeSubStep.ProcessMethod (Mono.Cecil.MethodDefinition method) [0x00000] in <filename unknown>:0 
>   at Xamarin.Linker.CoreOptimizeGeneratedCode.ProcessMethods (IEnumerable`1 c) [0x00000] in <filename unknown>:0 
>   at Xamarin.Linker.CoreOptimizeGeneratedCode.ProcessType (Mono.Cecil.TypeDefinition type) [0x00000] in <filename unknown>:0 
>   at MonoTouch.Tuner.OptimizeGeneratedCodeSubStep.ProcessType (Mono.Cecil.TypeDefinition type) [0x00000] in <filename unknown>:0 
>   at Mono.Tuner.SubStepDispatcher.DispatchType (Mono.Cecil.TypeDefinition type) [0x00000] in <filename unknown>:0 
>   at Mono.Tuner.SubStepDispatcher.BrowseTypes (ICollection types) [0x00000] in <filename unknown>:0 
>   at Mono.Tuner.SubStepDispatcher.BrowseAssemblies (IEnumerable`1 assemblies) [0x00000] in <filename unknown>:0 
>   at Mono.Tuner.SubStepDispatcher.Process (Mono.Linker.LinkContext context) [0x00000] in <filename unknown>:0 
>   at Mono.Linker.Pipeline.Process (Mono.Linker.LinkContext context) [0x00000] in <filename unknown>:0 
>   at MonoTouch.Tuner.Linker.Process (MonoTouch.Tuner.LinkerOptions options, MonoTouch.Tuner.MonoTouchLinkContext& context, System.Collections.Generic.List`1& assemblies) [0x00000] in <filename unknown>:0
Comment 1 psa2ry7rgq 2015-07-21 21:23:35 UTC
Created attachment 12144 [details]
Diagnostic build output, additional version info
Comment 3 Sebastien Pouliot 2015-07-21 21:38:31 UTC
@Brendan can you attach the binary .dll ?
Comment 4 Brendan Zagaeski (Xamarin Team, assistant) 2015-07-21 21:55:37 UTC
Created attachment 12145 [details]
The problematic .dll file by itself
Comment 5 Brendan Zagaeski (Xamarin Team, assistant) 2015-07-21 21:56:48 UTC
Updating status per comment 4.
Comment 6 Sebastien Pouliot 2015-08-05 10:50:02 UTC
*** Bug 32792 has been marked as a duplicate of this bug. ***
Comment 8 Sebastien Pouliot 2015-08-13 14:41:57 UTC
csc debug IL is very sub-optimal, e.g.

            IL_002f: stloc.2
            IL_0030: ldloc.2

and that was not handled by the binding optimizer - mostly because, like you noted, we did not support CSC until very recently to create bindings.

A quick around around should be to ask the C# compiler to output optimized code, which does not present debugging and, one the scale of build a remove project, should not negligibly affect build times.

@PJ: can you ask 360logica to add building binding projects/samples from VS in their test plan now that this is a supported feature ?

Fixed in maccore/master f6f3ed45bd3b3b7bf1251efcbefb02aa5b9c705e
Comment 9 Udham Singh 2015-08-14 06:45:40 UTC
I have checked this issue with latest master XI build monotouch-8.13.0.113_454bb59d4f8e89fbdddd66ff5e9b335321875f26 and observed that this issue has been fixed.

To verify this issue first I checked this issue with C5SR3 build monotouch-8.10.4.49_7e598ef6cef7fd68709be477681606c76ee01c62 and able to reproduce this issue.

Hence I am closing this issue.

Screencast : http://www.screencast.com/t/GSgQPBRc4E

Environment Info : 

=== Xamarin Studio ===

Version 5.9.5 (build 9)
Installation UUID: ce927b2a-2c07-44c5-b186-09cfdafba6dc
Runtime:
	Mono 4.0.3 ((detached/d6946b4)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400030020

=== Apple Developer Tools ===

Xcode 6.2 (6776)
Build 6C131e

=== Xamarin.iOS ===

Version: 8.13.0.113 (Enterprise Edition)
Hash: 454bb59
Branch: master
Build date: 2015-08-13 23:40:20-0400

=== Build Information ===

Release ID: 509050009
Git revision: b99cb19a1d3b15d8980c4616f13f837457e5b292
Build date: 2015-07-28 02:01:44-04
Xamarin addins: d53d6eceb828422f3d4aff8547f2f6bab6c9ea58

=== Operating System ===

Mac OS X 10.9.5
Darwin Xamarin76s-Mac-mini.local 13.4.0 Darwin Kernel Version 13.4.0
    Sun Aug 17 19:50:11 PDT 2014
    root:xnu-2422.115.4~1/RELEASE_X86_64 x86_64