Bug 58711 - Linker fails randomly when processing a specific set dependencies
Summary: Linker fails randomly when processing a specific set dependencies
Status: VERIFIED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: Linker ()
Version: 8.0 (15.4)
Hardware: PC Windows
: High major
Target Milestone: 15.4
Assignee: Radek Doulik
URL:
: 58797 ()
Depends on:
Blocks:
 
Reported: 2017-08-11 02:38 UTC by Jerome Laban
Modified: 2017-09-01 00:14 UTC (History)
7 users (show)

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


Attachments
repro solution (29.81 KB, application/x-zip-compressed)
2017-08-11 02:38 UTC, Jerome Laban
Details
versions (3.56 KB, text/plain)
2017-08-11 02:39 UTC, Jerome Laban
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 Jerome Laban 2017-08-11 02:38:35 UTC
Created attachment 24134 [details]
repro solution

Using Xamarin 4.7.0.827 (Xamarin.Android 7.5.0.3), the attached solution fails to deploy in release configuration with the error below. Each compilation seems to produce a different method to be analyzed error.

Disabling the linker works around this issue, but the linker is essential for large apps.

2>Mono.Linker.MarkException: Error processing method: 'System.Void Xamarin.Forms.Platform.Android.ViewRenderer`2::set_Control(TNativeView)' in assembly: 'Xamarin.Forms.Platform.Android.dll' ---> System.ArgumentNullException: Value cannot be null.
2>Parameter name: instruction
2>   at Mono.Cecil.Cil.InstructionOffset..ctor(Instruction instruction)
2>   at Mono.Cecil.Cil.CodeReader.ReadScope(ScopeDebugInformation scope)
2>   at Mono.Cecil.Cil.CodeReader.ReadScopes(Collection`1 scopes)
2>   at Mono.Cecil.Cil.CodeReader.ReadScope(ScopeDebugInformation scope)
2>   at Mono.Cecil.Cil.CodeReader.ReadScopes(Collection`1 scopes)
2>   at Mono.Cecil.Cil.CodeReader.ReadScope(ScopeDebugInformation scope)
2>   at Mono.Cecil.Cil.CodeReader.ReadDebugInfo()
2>   at Mono.Cecil.Cil.CodeReader.ReadMethodBody()
2>   at Mono.Cecil.Cil.CodeReader.ReadMethodBody(MethodDefinition method)
2>   at Mono.Cecil.MethodDefinition.<>c.<get_Body>b__41_0(MethodDefinition method, MetadataReader reader)
2>   at Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TRet& variable, TItem item, Func`3 read)
2>   at Mono.Cecil.MethodDefinition.get_Body()
2>   at Mono.Linker.Steps.MarkStep.ProcessMethod(MethodDefinition method)
2>   at Mono.Linker.Steps.MarkStep.ProcessQueue()
2>   --- End of inner exception stack trace ---
2>   at Mono.Linker.Steps.MarkStep.ProcessQueue()
2>   at Mono.Linker.Steps.MarkStep.Process()
2>   at Mono.Linker.Steps.MarkStep.Process(LinkContext context)
2>   at Mono.Linker.Pipeline.Process(LinkContext context)
2>   at MonoDroid.Tuner.Linker.Process(LinkerOptions options, LinkContext& context)
2>   at Xamarin.Android.Tasks.LinkAssemblies.Execute(DirectoryAssemblyResolver res)
2>   at Xamarin.Android.Tasks.LinkAssemblies.Execute()
2>   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
2>   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()
2>Done building project "App9.csproj" -- FAILED.
2>Build FAILED.
2>
Comment 1 Jerome Laban 2017-08-11 02:39:04 UTC
Created attachment 24135 [details]
versions

Added installed tools versions.
Comment 2 Jon Douglas [MSFT] 2017-08-11 09:24:08 UTC
I was able to reproduce using the same version information in https://bugzilla.xamarin.com/show_bug.cgi?id=58711#c1 with the reproduction in https://bugzilla.xamarin.com/show_bug.cgi?id=58711#c0

Full Diagnostic Build Output here: https://gist.github.com/JonDouglas/67d9ae242bf5bf4a4c337235e43d9576

I did notice the Linker settings in your sample project is set to be "Sdk and User Assemblies" which is not the Release configuration default of "SDK Assemblies only". Point aside, it seems that the linker is failing here:

https://github.com/mono/linker/blob/5d72aac5e056c35887a54c66a0ed79d203943165/linker/Mono.Linker.Steps/MarkStep.cs#L182-L195

which is calling into ProcessMethod:

https://github.com/mono/linker/blob/5d72aac5e056c35887a54c66a0ed79d203943165/linker/Mono.Linker.Steps/MarkStep.cs#L1336-L1390

which then calls into ReadScope where I believe it's failing:

https://github.com/mono/cecil/blob/master/Mono.Cecil.Cil/CodeReader.cs#L162-L169

I also tested Xamarin.Android 7.3.1.2(current stable), and it seems to build and deploy just fine. Thus I believe the linker is failing when processing some Xamarin.Forms methods.

*Version Information:

Microsoft Visual Studio Enterprise 2017 Preview (Preview)
Version 15.3.0 Preview 7.0
VisualStudio.15.Preview/15.3.0-pre.7.0+26730.0
Microsoft .NET Framework
Version 4.7.02046

Xamarin   4.7.0.827 (ddd8750)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android SDK   7.5.0.3 (3e66e8e)
Xamarin.Android Reference Assemblies and MSBuild support.

Xamarin.iOS and Xamarin.Mac SDK   10.14.0.4 (c70e3b5)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.
Comment 3 Jerome Laban 2017-08-11 12:04:42 UTC
Indeed, this is not the default, yet I'm almost never using this default as part of production applications.

I'm frequently reminded that bugs reported don't use the default settings, yet those are valid issues, regardless of the default. The product provides this option, meaning it's a supported option...


Also, the failure point is pretty much anything above a certain threshold of assemblies, as I hit this bug with a project that contains none of the assemblies that are referenced in the sample.
Comment 4 Jon Douglas [MSFT] 2017-08-11 16:29:08 UTC
(In reply to Jerome Laban from comment #3)
> Indeed, this is not the default, yet I'm almost never using this default as
> part of production applications.
> 
> I'm frequently reminded that bugs reported don't use the default settings,
> yet those are valid issues, regardless of the default. The product provides
> this option, meaning it's a supported option...
> 
> 
> Also, the failure point is pretty much anything above a certain threshold of
> assemblies, as I hit this bug with a project that contains none of the
> assemblies that are referenced in the sample.

I was merely making a small note to indicate to anyone else looking over this bug report that it isn't the default configuration settings and if they were to make another reproduction, they would need to change the linker setting. This is just a bookkeeping note that you can ignore.

We will look into this shortly as I've already confirmed it.
Comment 5 Jerome Laban 2017-08-11 17:21:22 UTC
Thank you :)
Comment 6 Eazhilarasi 2017-08-15 14:03:52 UTC
Hi,
I am finding the same 'LinkAssemblies' failed unexpectedly problem while trying to Build the project on Release mode - 'Sdk Assemblies Only' Linking option.

System.NotSupportedException: Specified method is not supported.
3>   at Mono.Cecil.Cil.InstructionOffset.get_Offset()
3>   at Mono.Cecil.Mdb.MdbWriter.WriteScope(ScopeDebugInformation scope, MethodDebugInformation info)
3>   at Mono.Cecil.Mdb.MdbWriter.WriteScopes(Collection`1 scopes, MethodDebugInformation info)
3>   at Mono.Cecil.Mdb.MdbWriter.WriteScope(ScopeDebugInformation scope, MethodDebugInformation info)
3>   at Mono.Cecil.Mdb.MdbWriter.WriteScopes(Collection`1 scopes, MethodDebugInformation info)
3>   at Mono.Cecil.Mdb.MdbWriter.WriteRootScope(ScopeDebugInformation scope, MethodDebugInformation info)
3>   at Mono.Cecil.Mdb.MdbWriter.Write(MethodDebugInformation info)
3>   at Mono.Cecil.Cil.CodeWriter.WriteResolvedMethodBody(MethodDefinition method)
3>   at Mono.Cecil.Cil.CodeWriter.WriteMethodBody(MethodDefinition method)
3>   at Mono.Cecil.MetadataBuilder.AddMethod(MethodDefinition method)
3>   at Mono.Cecil.MetadataBuilder.AddMethods(TypeDefinition type)
3>   at Mono.Cecil.MetadataBuilder.AddType(TypeDefinition type)
3>   at Mono.Cecil.MetadataBuilder.AddNestedTypes(TypeDefinition type)
3>   at Mono.Cecil.MetadataBuilder.AddType(TypeDefinition type)
3>   at Mono.Cecil.MetadataBuilder.AddTypes()
3>   at Mono.Cecil.MetadataBuilder.BuildTypes()
3>   at Mono.Cecil.MetadataBuilder.BuildModule()
3>   at Mono.Cecil.MetadataBuilder.BuildMetadata()
3>   at Mono.Cecil.ModuleWriter.<>c.<BuildMetadata>b__2_0(MetadataBuilder builder, MetadataReader _)
3>   at Mono.Cecil.ModuleDefinition.Read[TItem,TRet](TItem item, Func`3 read)
3>   at Mono.Cecil.ModuleWriter.BuildMetadata(ModuleDefinition module, MetadataBuilder metadata)
3>   at Mono.Cecil.ModuleWriter.Write(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters)
3>   at Mono.Cecil.ModuleWriter.WriteModule(ModuleDefinition module, Disposable`1 stream, WriterParameters parameters)
3>   at Mono.Cecil.ModuleDefinition.Write(String fileName, WriterParameters parameters)
3>   at Mono.Linker.Steps.OutputStep.OutputAssembly(AssemblyDefinition assembly)
3>   at Mono.Linker.Steps.OutputStep.ProcessAssembly(AssemblyDefinition assembly)
3>   at Mono.Linker.Steps.BaseStep.Process(LinkContext context)
3>   at Mono.Linker.Pipeline.Process(LinkContext context)
3>   at MonoDroid.Tuner.Linker.Process(LinkerOptions options, LinkContext& context)
3>   at Xamarin.Android.Tasks.LinkAssemblies.Execute(DirectoryAssemblyResolver res)
3>   at Xamarin.Android.Tasks.LinkAssemblies.Execute()
3>   at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
3>   at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()
3>Done building project "SampleAppToTestRelease.Droid.csproj" -- FAILED.
3>Build FAILED.
Comment 7 Jon Douglas [MSFT] 2017-08-16 17:43:02 UTC
*** Bug 58797 has been marked as a duplicate of this bug. ***
Comment 8 Brendan Zagaeski (Xamarin Team, assistant) 2017-08-17 20:38:32 UTC
The fix for non-public Bug 57675 is:

https://github.com/mono/cecil/commit/16d23978e7084c910b47f84092a5c546d880ae48


This commit _was_ added into the d15-3 branch of Xamarin.Android:
https://github.com/xamarin/xamarin-android/commit/832f9e6615187455144b5fd26da8a4f4809c7749


But it has _not_ yet been added into the d15-4 branch as of Xamarin.Android 7.5.0.3 (monodroid/d15-4 3e66e8e) (which is based on xamarin-android/d15-4 506d354)

xamarin-android/d15-4 506d354:
https://github.com/xamarin/xamarin-android/tree/506d3546b53f908558f7509849968d0c45040012/external

... brings us to Java.Interop cfc2ef3:
https://github.com/xamarin/java.interop/tree/cfc2ef32ecfbd47dc83efd848a079c975ea87745/external

... brings us to Mono.Cecil c76ba7b, which most importantly for this bug does _not_ include the "fix reading scope start" change to CodeReader.cs from non-public Bug 57675:
https://github.com/mono/cecil/commits/c76ba7b410447fa37093150cb7bc772cba28a0ae/Mono.Cecil.Cil/CodeReader.cs
Comment 9 Radek Doulik 2017-08-18 07:17:38 UTC
This bug was fixed in xamarin-android/d15-4 by https://github.com/xamarin/xamarin-android/commit/00288c93b285e1aea346a0ec0b7f6c5bdb843c9f
Comment 11 Brendan Zagaeski (Xamarin Team, assistant) 2017-09-01 00:14:17 UTC
## Verification status: verified fixed in Xamarin.Android 7.5.0-13 (d15-4/14c149)

GOOD: Xamarin.Android 7.5.0-13 (d15-4/14c149)
BAD:  Xamarin.Android 7.5.0-3  (d15-4/3e66e8e)




## Steps followed to test

1. Extract the test case from Comment 0.

2. Open the solution in Visual Studio for Mac to restore the NuGet packages.

3. Build the project in the Release configuration.

4. Add some little change to a source file like adding `var x = 1;` in the `MainActivity.OnCreate()` override. 

5. Build the project again in the Release configuration (without cleaning).




## GOOD results with Xamarin.Android 7.5.0-13 (d15-4/14c149)

There are no warnings or errors during the `LinkAssemblies` task.




## BAD results with Xamarin.Android 7.5.0-3 (d15-4/3e66e8e)

The build fails as described in Comment 0.  Top of the call stack from the build output:

> error MSB4018: The "LinkAssemblies" task failed unexpectedly.
> error MSB4018: Mono.Linker.MarkException: Error processing method: 'System.Void Xamarin.Forms.Platform.Android.ViewRenderer`2::set_Control(TNativeView)' in assembly: 'Xamarin.Forms.Platform.Android.dll' ---> System.ArgumentNullException: Value cannot be null.
> error MSB4018: Parameter name: instruction
> error MSB4018:   at Mono.Cecil.Cil.InstructionOffset..ctor (Mono.Cecil.Cil.Instruction instruction) [0x00021] in /Users/builder/data/lanes/5147/3e66e8e4/source/xamarin-android/external/Java.Interop/external/cecil/Mono.Cecil.Cil/Symbols.cs:200 
> error MSB4018:   at Mono.Cecil.Cil.CodeReader.ReadScope (Mono.Cecil.Cil.ScopeDebugInformation scope) [0x00015] in /Users/builder/data/lanes/5147/3e66e8e4/source/xamarin-android/external/Java.Interop/external/cecil/Mono.Cecil.Cil/CodeReader.cs:163 
> error MSB4018:   at Mono.Cecil.Cil.CodeReader.ReadScopes (Mono.Collections.Generic.Collection`1[T] scopes) [0x00004] in /Users/builder/data/lanes/5147/3e66e8e4/source/xamarin-android/external/Java.Interop/external/cecil/Mono.Cecil.Cil/CodeReader.cs:157 
> error MSB4018:   at Mono.Cecil.Cil.CodeReader.ReadScope (Mono.Cecil.Cil.ScopeDebugInformation scope) [0x000af] in /Users/builder/data/lanes/5147/3e66e8e4/source/xamarin-android/external/Java.Interop/external/cecil/Mono.Cecil.Cil/CodeReader.cs:180 
> error MSB4018:   at Mono.Cecil.Cil.CodeReader.ReadScopes (Mono.Collections.Generic.Collection`1[T] scopes) [0x00004] in /Users/builder/data/lanes/5147/3e66e8e4/source/xamarin-android/external/Java.Interop/external/cecil/Mono.Cecil.Cil/CodeReader.cs:157 



## Additional testing environment info (brief)

Visual Studio 2017 for Mac (7.1.0.1297)
Mono MDK 5.2.0.215 (d15-3/da80840)

macOS 10.12.6