Bug 28617

Summary: Linker-modified System.Net.Http.Primitives.dll causes ArgumentOutOfRangeException during mono-cil-strip
Product: iOS Reporter: Brendan Zagaeski (Xamarin Support) <brendan.zagaeski>
Component: GeneralAssignee: Sebastien Pouliot <sebastien>
Status: VERIFIED FIXED    
Severity: normal CC: asimk, mono-bugs+monotouch, owen.stewart, sebastien
Priority: Normal    
Version: XI 8.8.0   
Target Milestone: 8.10   
Hardware: PC   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Test case
Diagnostic build output

Description Brendan Zagaeski (Xamarin Support) 2015-04-01 03:50:58 UTC
Created attachment 10581 [details]
Test case

Linker-modified System.Net.Http.Primitives.dll causes ArgumentOutOfRangeException during mono-cil-strip


This is almost identical to bug 25344 (see in particular bug 25344, comment 7), _but_ this particular test case only fails when building on Mac.

(The fact that the `System.Net.Http.Primitives.dll` is not copied into the `PreBuild` folder in VS might be _correct_ (see bug 20589, comment 7), but in any case, that's a separate issue.)

Sysem.Net.Http.Primitives.dll is part of the Microsoft.Net.Http ("Microsoft HTTP Client Libraries") NuGet package.



## Steps to reproduce


1. Open the attached test case in Xamarin Studio to restore the packages.


2. Build the "UnifiedSingleViewIphone1" project in the "Release|iPhone" configuration (either in Xamarin Studio or using `xbuild` on the command line).



## Results

The build fails while stripping the `System.Net.Http.Primitives.dll` assembly:


> /Library/Frameworks/Mono.framework/Versions/Current/bin/mono /Library/Frameworks/Mono.framework/Versions/Current/lib/mono/4.5/mono-cil-strip.exe /private/tmp/HttpClientPCLLinkerStripperError/UnifiedSingleViewIphone1/obj/iPhone/Release/mtouch-cache/64/Build/System.Net.Http.Primitives.dll /private/tmp/HttpClientPCLLinkerStripperError/UnifiedSingleViewIphone1/bin/iPhone/Release/UnifiedSingleViewIphone1.app/.monotouch-64/System.Net.Http.Primitives.dll 
> Mono CIL Stripper
> 
> Error: System.ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.
> Parameter name: index
> 2
>   at System.Collections.ArrayList.ThrowNewArgumentOutOfRangeException (System.String name, System.Object actual, System.String message) [0x00000] in <filename unknown>:0 
>   at System.Collections.ArrayList.get_Item (Int32 index) [0x00000] in <filename unknown>:0 
>   at System.Collections.CollectionBase.System.Collections.IList.get_Item (Int32 index) [0x00000] in <filename unknown>:0 
>   at Mono.Cecil.AssemblyNameReferenceCollection.get_Item (Int32 index) [0x00000] in <filename unknown>:0 
>   at Mono.Cecil.ReflectionReader.GetExportedTypeScope (MetadataToken scope) [0x00000] in <filename unknown>:0 
>   at Mono.Cecil.ReflectionReader.VisitExternTypeCollection (Mono.Cecil.ExternTypeCollection externs) [0x00000] in <filename unknown>:0 
>   at Mono.Cecil.AggressiveReflectionReader.ReadExternTypes () [0x00000] in <filename unknown>:0 
>   at Mono.Cecil.AggressiveReflectionReader.VisitTypeDefinitionCollection (Mono.Cecil.TypeDefinitionCollection types) [0x00000] in <filename unknown>:0 
>   at Mono.Cecil.ReflectionReader.VisitModuleDefinition (Mono.Cecil.ModuleDefinition mod) [0x00000] in <filename unknown>:0 
>   at Mono.Cecil.StructureReader.TerminateAssemblyDefinition (Mono.Cecil.AssemblyDefinition asm) [0x00000] in <filename unknown>:0 
>   at Mono.Cecil.AssemblyDefinition.Accept (IReflectionStructureVisitor visitor) [0x00000] in <filename unknown>:0 
>   at Mono.Cecil.AssemblyFactory.GetAssembly (Mono.Cecil.Binary.ImageReader irv, Boolean manifestOnly) [0x00000] in <filename unknown>:0 
>   at Mono.Cecil.AssemblyFactory.GetAssembly (Mono.Cecil.Binary.ImageReader reader) [0x00000] in <filename unknown>:0 
>   at Mono.Cecil.AssemblyFactory.GetAssembly (System.String file) [0x00000] in <filename unknown>:0 
>   at Mono.CilStripper.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0 




## Partial workaround?

Remove "-v -v -v -v" under "project options -> Build -> iOS Build -> General [tab] -> Additional mtouch arguments".



### Result after workaround

The build completes successfully. Note that if you manually run `mono-cil-strip.exe` on `.../64/Build/System.Net.Http.Primitives.dll` after the build completes, it will still produce the "ArgumentOutOfRangeException".




## Classic API, mdtool build engine

Building the included Classic API test case with mdtool build engine completes successfully, but the build output still shows the "ArgumentOutOfRangeException".




## Version information

### OS X 10.9.5, MacBook Air

Mono 3.12.1 (detached/0849ec7)

Xcode 6.1.1 (6611), Build 6A2008a

Xamarin.iOS, 8.8.2.4 (Business Edition)
Hash: 981acb3
Build date: 2015-03-25 14:21:05-0400
Comment 1 Brendan Zagaeski (Xamarin Support) 2015-04-01 03:51:49 UTC
Created attachment 10582 [details]
Diagnostic build output
Comment 2 Brendan Zagaeski (Xamarin Support) 2015-04-01 03:54:12 UTC
I'd guess that this is _not_ a regression, or at least not a recent regression.
Comment 3 asimk 2015-04-01 07:01:29 UTC
I have checked this issue with sample app attached in bug description and able to reproduce this issue. I follow the steps and observed that I am getting "Error:System.ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count.Parameter name: index" error while building "UnifiedSingleViewIphone1" project in the "Release|iPhone" configuration.

Screencast: http://www.screencast.com/t/8d43FVHXHtdy
Ide Log: https://gist.github.com/saurabh360/34a8a7718252be19d994
iOS Designer Log: https://gist.github.com/saurabh360/01d864bbceed2653e8b5
Build Output: https://gist.github.com/saurabh360/b0fe49f648b72414b8e6

Environment Info:
=== Xamarin Studio ===

Version 5.8.2 (build 7)
Installation UUID: d6f15b80-470e-4e50-9aba-38a45c556680
Runtime:
	Mono 3.12.1 ((detached/0849ec7)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312010003

=== Apple Developer Tools ===

Xcode 6.3 (7557.5)
Build 6D554n

=== Xamarin.iOS ===

Version: 8.8.2.4 (Enterprise Edition)
Hash: 981acb3
Branch: 
Build date: 2015-03-25 14:21:05-0400

=== Xamarin.Android ===

Version: 4.20.1.0 (Enterprise Edition)
Android SDK: /Users/360logica/Desktop/Anddk/android-sdk-macosx
	Supported Android versions:
		2.1    (API level 7)
		2.2    (API level 8)
		2.3    (API level 10)
		3.1    (API level 12)
		4.0    (API level 14)
		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)
		4.4.87 (API level 20)
		5.0    (API level 21)
Java SDK: /usr
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

=== Xamarin Android Player ===

Not Installed

=== Xamarin.Mac ===

Version: 1.12.0.14 (Enterprise Edition)

=== Build Information ===

Release ID: 508020007
Git revision: 067ed06873402cf780f8a0899bb332c2a1c0af22
Build date: 2015-03-27 14:17:13-04
Xamarin addins: 02510c4990dee3475c282b3182d588975a63ff07

=== Operating System ===

Mac OS X 10.10.0
Darwin 360Logicas-MacBook-Pro.local 14.0.0 Darwin Kernel Version 14.0.0
    Fri Sep 19 00:26:44 PDT 2014
    root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64
Comment 4 Sebastien Pouliot 2015-04-01 09:33:45 UTC
IIRC mono-cil-strip errors are ignored (will confirm) but I think the msbuild task (that call mtouch) might not happy to see the error in the logs and stop the execution. 

That log is only visible (to the developer and the msbuild task) if verbosity is high enough.
Comment 5 Sebastien Pouliot 2015-04-01 10:15:52 UTC
It's a uncommon, but bad, mix of conditions:

a. there something wrong while stripping the assembly (to be investigated);

b. mono-cil-strip returns 0 on error/exception (it should not, needs to be fixed);

c. Extra (> 1) verbosity makes the error show in the console (when error code is 0). That logic is fine but it should not be executed if mono-cil-strip reported its failure correctly;

d. msbuild see the error in the logs and (even if mtouch returns 0 aborts). Not sure if this is a correct behaviour or not. In this case it's useful (but I can imagine cases where it would be harmful). It's not the the mtouch-task logic (it's in a base class) so it's gonna be replaced with MS code shortly (not worth investigating).
Comment 6 Sebastien Pouliot 2015-04-02 15:47:58 UTC
Linker (a) is fixed in mono/master 7934663bb834e35e0f9d192d8574fc903695bcdf

In general PCL facade assemblies are SDK code - but here it's part of the NuGet so it's considered user code. That meant some Scope were not updated correctly, which in turns made cil-strip unhappy.

mono-cil-strip (b) fixed in mono/master deda3beb52b7efa5be794c6e68f08ff6928028e1
Comment 7 Sebastien Pouliot 2015-04-02 16:49:21 UTC
Fixed backported to mono-4.0.0-branch
maccore/master bumped mono in 6a1a6093fb01c7afa2e6c9b4d531d2f38d69ade7
maccore/master bumped mono in 001724a75de3a1bf614d9ca05ce2ffbe2eb3e373
Comment 8 Brendan Zagaeski (Xamarin Support) 2015-04-02 16:57:06 UTC
Excellent. Many thanks for the quick investigation!
Comment 9 asimk 2015-04-03 09:35:42 UTC
I have checked this issue and I am able to deploy attached application on Simulator successfully. Hence closing this issue.

Environment Info:
=== Xamarin Studio ===

Version 5.9 (build 394)
Installation UUID: d6f15b80-470e-4e50-9aba-38a45c556680
Runtime:
 Mono 4.0.0 ((detached/21d849b)
 GTK+ 2.24.23 (Raleigh theme)

 Package version: 400000121

=== Xamarin.Android ===

Version: 5.1.0.101 (Enterprise Edition)
Android SDK: /Users/360logica/Desktop/Anddk/android-sdk-macosx
 Supported Android versions:
  2.3    (API level 10)
  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)
  4.4.87 (API level 20)
  5.0    (API level 21)
Java SDK: /usr
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

=== Apple Developer Tools ===

Xcode 6.3 (7557.5)
Build 6D554n

=== Xamarin.iOS ===

Version: 8.10.0.197 (Enterprise Edition)
Hash: 4dac6bf
Branch: master
Build date: 2015-04-01 21:55:11-0400

=== Xamarin.Mac ===

Version: 2.0.0.197 (Enterprise Edition)

=== Build Information ===

Release ID: 509000394
Git revision: 1080ff00106be1822023e662a3b7c633c69babd7
Build date: 2015-04-02 11:25:35-04
Xamarin addins: 2a368c4c7fb7978244131a061361ae2cbf5dc7c2

=== Operating System ===

Mac OS X 10.10.0
Darwin 360Logicas-MacBook-Pro.local 14.0.0 Darwin Kernel Version 14.0.0
    Fri Sep 19 00:26:44 PDT 2014
    root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64