Bug 28918

Summary: MTOUCH: error MT0000: Unexpected error - Please file a bug report at http://bugzilla.xamarin.com
Product: iOS Reporter: Herman Schoenfeld <herman>
Component: GeneralAssignee: Sebastien Pouliot <sebastien>
Status: VERIFIED FIXED    
Severity: normal CC: buulioni, edward.elliott, kale.plush.work, mono-bugs+monotouch, ntindall, parmendrak, robert.waggott, rolf, sebastien, shrutis, udhams
Priority: Normal    
Version: XI 8.10   
Target Milestone: (C7)   
Hardware: Other   
OS: Other   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Full build log for issue
Simple test project

Description Herman Schoenfeld 2015-04-09 04:07:39 UTC
Created attachment 10685 [details]
Full build log for issue

After migrating from Xamarin Classic to Xamarin Unified, I've encountered an MT0000 error. Below is the summary. Attached is the build log.

SUMMARY
=======
MTOUCH: error MT0000: Unexpected error - Please file a bug report at http://bugzilla.xamarin.com
		Mono.Cecil.ResolutionException: Failed to resolve System.Security.Permissions.SecurityPermissionFlag
		  at Mono.Cecil.Mixin.CheckedResolve (Mono.Cecil.TypeReference self) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.SignatureReader.ReadCustomAttributeEnum (Mono.Cecil.TypeReference enum_type) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.SignatureReader.ReadCustomAttributeElementValue (Mono.Cecil.TypeReference type) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.SignatureReader.ReadCustomAttributeElement (Mono.Cecil.TypeReference type) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.SignatureReader.ReadCustomAttributeFixedArgument (Mono.Cecil.TypeReference type) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.SignatureReader.ReadCustomAttributeNamedArgument (Mono.Collections.Generic.Collection`1& fields, Mono.Collections.Generic.Collection`1& properties) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.SignatureReader.ReadCustomAttributeNamedArguments (UInt16 count, Mono.Collections.Generic.Collection`1& fields, Mono.Collections.Generic.Collection`1& properties) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.SignatureReader.ReadSecurityAttribute () [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.MetadataReader.ReadSecurityDeclarationSignature (Mono.Cecil.SecurityDeclaration declaration) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.SecurityDeclaration.<Resolve>m__1 (Mono.Cecil.SecurityDeclaration declaration, Mono.Cecil.MetadataReader reader) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.ModuleDefinition.Read[SecurityDeclaration,SecurityDeclaration] (Mono.Cecil.SecurityDeclaration item, System.Func`3 read) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.SecurityDeclaration.Resolve () [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.SecurityDeclaration.get_SecurityAttributes () [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.ImmediateModuleReader.ReadSecurityDeclarations (ISecurityDeclarationProvider provider) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.ImmediateModuleReader.ReadMethods (Mono.Cecil.TypeDefinition type) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.ImmediateModuleReader.ReadType (Mono.Cecil.TypeDefinition type) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.ImmediateModuleReader.ReadTypes (Mono.Collections.Generic.Collection`1 types) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.ImmediateModuleReader.ReadModule (Mono.Cecil.ModuleDefinition module) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.ModuleWriter.WriteModuleTo (Mono.Cecil.ModuleDefinition module, System.IO.Stream stream, Mono.Cecil.WriterParameters parameters) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.ModuleDefinition.Write (System.IO.Stream stream, Mono.Cecil.WriterParameters parameters) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.ModuleDefinition.Write (System.String fileName, Mono.Cecil.WriterParameters parameters) [0x00000] in <filename unknown>:0 
		  at Mono.Cecil.AssemblyDefinition.Write (System.String fileName, Mono.Cecil.WriterParameters parameters) [0x00000] in <filename unknown>:0 
		  at MTouch.SaveAssembly (Mono.Cecil.AssemblyDefinition assembly, System.String destination) [0x00000] in <filename unknown>:0 
		  at MonoTouch.Assembly.RemoveResources (Boolean remove_resources, System.String target_directory, System.String nores_directory) [0x00000] in <filename unknown>:0 
		  at MonoTouch.Target.ProcessAssemblies () [0x00000] in <filename unknown>:0 
		  at MonoTouch.Application.BuildApp () [0x00000] in <filename unknown>:0 
		  at MonoTouch.Application.Build () [0x00000] in <filename unknown>:0 
		  at MTouch.Main2 (System.String[] args) [0x00000] in <filename unknown>:0 
		  at MTouch.Main (System.String[] args) [0x00000] in <filename unknown>:0 
	Task "MTouch" execution -- FAILED



=== Xamarin Studio ===

Version 5.9 (build 388)
Installation UUID: aad8e2c1-53aa-4766-af0b-57b0ba105a0e
Runtime:
	Mono 4.0.0 ((detached/21d849b)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400000121

=== Xamarin.Android ===

Version: 5.1.0.100 (Business Edition)
Android SDK: /Users/Developer/Library/Developer/Xamarin/android-sdk-mac_x86
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.4   (API level 19)
Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

=== Xamarin Android Player ===

Not Installed

=== Apple Developer Tools ===

Xcode 6.2 (6776)
Build 6C131e

=== Xamarin.Mac ===

Version: 2.0.0.197 (Business Edition)

=== Xamarin.iOS ===

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

=== Build Information ===

Release ID: 509000388
Git revision: 027c5efa6f92249fd2e614535965159c0a7c0fe6
Build date: 2015-04-01 19:12:53-04
Xamarin addins: a6779639267bbe31df3fd6958be4ecae82665b7e

=== Operating System ===

Mac OS X 10.10.2
Darwin 192-168-1-5.tpgi.com.au 14.1.0 Darwin Kernel Version 14.1.0
    Thu Feb 26 19:26:47 PST 2015
    root:xnu-2782.10.73~1/RELEASE_X86_64 x86_64
Comment 1 Rolf Bjarne Kvinge [MSFT] 2015-04-09 04:10:28 UTC
Can you give us access to (or share with us) the project so that we can reproduce it ourselves?
Comment 2 Herman Schoenfeld 2015-04-09 04:28:18 UTC
I've asked the client and they are not comfortable opening up the entire source code to a 3rd party without any NDA's in place. We're upgrading to Xamarin Unified (from Classic) due to another bug in Classic that still lives on here unresolved (#24098). So to ask them now to open up the source is fairly unreasonable, according to them.

So, as they (and I) are paying business subscribers of Xamarin iOS, are you able to resolve this issue in the usual way ISV's solve such issues by reviewing logs and performing your own investigations? 

My guess is that it's an AOT failure on the System.Security.Permissions.SecurityPermissionFlag type.

The main project is compiled with --nolinkaway if that helps.

Thanks
Comment 3 Rolf Bjarne Kvinge [MSFT] 2015-04-09 04:35:25 UTC
This particular error can be very hard to track down without a test case.

However we can probably reproduce this with only the compiled assemblies instead of the source code; so can you instead zip up the directory /Users/Developer/Documents/Development/SH2/SH2.Mobile.iOS/obj/iPhone/Debug/mtouch-cache and send it to us?
Comment 4 Sebastien Pouliot 2015-04-09 11:36:23 UTC
>		Xamarin.iOS 8.10.0 Business Edition using framework: 

Did the same error occurred in the stable channel (XI 8.8.x) ? 8.10 is an alpha build.
Comment 7 Rolf Bjarne Kvinge [MSFT] 2015-04-10 06:04:50 UTC
I can reproduce this (the problematic assembly seems to be Sphere10.Framework.iOS.dll), thanks for the test case.
Comment 8 Rolf Bjarne Kvinge [MSFT] 2015-04-10 06:59:49 UTC
Sebastien, the problem is that the System.Security.Permissions.SecurityPermissionFlag type is linked away, but then used in a (security) attribute (using the "typename as string" encoding), and when resolving that attribute later the exception is thrown:

> $ monodis --typedef Link/mscorlib.dll|grep SecurityPermissionFlag
1723: System.Security.Permissions.SecurityPermissionFlag (flist=6561, mlist=15599, flags=0x2101, extends=0x1ebc)
> $ monodis --typedef PreBuild/mscorlib.dll|grep SecurityPermissionFlag
> $ monodis --typedef Build/mscorlib.dll|grep SecurityPermissionFlag
Comment 10 Sebastien Pouliot 2015-04-10 10:03:49 UTC
@Herman thanks for the data!

@Rolf thanks for the analysis. Custom attributes encoding wrt System.Type vs (lack of) assembly reference can be tricky - even more when PCL facades are thrown in.

OTOH the `--nolinkaway` argument is present so that type should not have been removed from mscorlib.dll - but I'm not sure it's "linked away" or simply not marked. I'll check that first.
Comment 11 Sebastien Pouliot 2015-04-10 10:54:22 UTC
`--nolinkaway` works fine (i.e. the related linker step is not called) so the linker does not forcefully remove `SecurityPermissionFlag`, however it does not preserve it either (that's a bug).


@Herman, what happens if you add the line:

> typeof (System.Security.Permissions.SecurityPermissionFlag).ToString ();

to your Main method ? that could be a quick workaround for this issue (I'll be sure once I created my own test case).
Comment 12 Sebastien Pouliot 2015-04-10 11:18:57 UTC
Actually it was faster to confirm that with your provided binaries first. I added a `preserve.xml` file and the option `--xml=preserve.xml` and the build continued up to the AOT compiler*.

$ cat preserve.xml 
<linker>
	<assembly fullname="mscorlib">
		<type fullname="System.Security.Permissions.SecurityPermissionFlag" />
    </assembly>
</linker>

* there was a different error at AOT compilation but that could be because the builds were interrupted before (and the cache might not be valid).
Comment 13 Sebastien Pouliot 2015-04-10 12:48:40 UTC
> Custom attributes encoding

That was close but not quite it. Security declarations uses a similar but not identical (and not the same metadata table) format as custom attributes. 

That means that another possible workaround would be to comment the [SecurityPermission] declaration on:

> Sphere10.Framework.Threading.SynchronizationCallback::Invoke()

which won't affect runtime execution on Xamarin.iOS (or any other Xamarin products as CAS is not supported by Mono).
Comment 14 Sebastien Pouliot 2015-04-10 15:41:45 UTC
Fixed in maccore/master ebdd9734786c405a7e05111d081a84ae56b7608c

QA: unit tests added in the same revision

@Herman I'm fairly sure either workaround will be fine (unless theres' another issue). Please let us know if they don't work by updating the bug report (new build logs and mtouch-cache directory). Thanks!
Comment 15 Herman Schoenfeld 2015-04-14 21:10:04 UTC
Thank you for your prompt response.
Comment 16 Sebastien Pouliot 2015-04-23 09:30:14 UTC
The original fix was reverted (it could cause issues) and changed to mark the security declarations (more proper and general).

Fixed mono/master 669572d6c946fca04b9aa7999d2939b6017fb2c6
Bumped in maccore/master d311a837e84dd85927b64e93a7f57e6ef22e7ed0
Comment 17 Sebastien Pouliot 2015-04-29 11:20:56 UTC
*** Bug 29521 has been marked as a duplicate of this bug. ***
Comment 18 Sebastien Pouliot 2015-04-30 11:27:39 UTC
*** Bug 29606 has been marked as a duplicate of this bug. ***
Comment 19 Rolf Bjarne Kvinge [MSFT] 2015-04-30 13:23:55 UTC
*** Bug 29577 has been marked as a duplicate of this bug. ***
Comment 20 Sebastien Pouliot 2015-04-30 23:23:11 UTC
*** Bug 29627 has been marked as a duplicate of this bug. ***
Comment 21 Sebastien Pouliot 2015-04-30 23:27:17 UTC
As a immediate workaround you can try:

1.  removing the security declaration from your source; or 

2. try adding `--linkskip=mscorlib` to your "Additional mtouch arguments" in the iOS builds section of your project options; or

3. add a `typeof (X).ToSting ();` in your FinishedLaunching method, where X is the type being reported as missing, e.g.

> typeof (System.Security.Permissions.FileIOPermissionAccess).ToString ();
Comment 22 Rolf Bjarne Kvinge [MSFT] 2015-05-01 02:29:28 UTC
Created attachment 11007 [details]
Simple test project

QA: to reproduce, just unzip this attachment and try to build the project.
Comment 23 Shruti 2015-05-01 07:39:28 UTC
I have reproduced this issue with X.iOS 8.10.0.272 . Screencast regarding same : http://www.screencast.com/t/mTLsZnbcIfL

Also checked this issue with patch available with card X.iOS 8.11.0.651 and it is working fine. Here is the screencast for same : http://www.screencast.com/t/rqmIb23bc5Sc

Environment Info : 
=== Xamarin Studio ===

Version 5.9 (build 436)
Installation UUID: 67eaf3b4-f8a7-4ab4-a9df-3197350ca5dc
Runtime:
	Mono 4.0.1 ((detached/373d684)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 400000009

=== Apple Developer Tools ===

Xcode 6.3 (7569)
Build 6D570

=== Xamarin.iOS ===

Version: 8.11.0.651 (Enterprise Edition)
Hash: 4891f88
Branch: master
Build date: 2015-04-30 12:54:57-0400

=== Xamarin.Android ===

Version: 5.1.0.119 (Enterprise Edition)
Android SDK: /Users/ixamarin78/Desktop/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)

=== Xamarin Android Player ===

Version: Unknown version
Location: /Applications/Xamarin Android Player.app

=== Xamarin.Mac ===

Version: 2.0.0.273 (Enterprise Edition)

=== Build Information ===

Release ID: 509000436
Git revision: 7e8ba4cbccf7b188ce5e80a90fdc4acdedb5a83f
Build date: 2015-04-28 12:01:46-04
Xamarin addins: 21fd95f743742c5237e22023996c2800ce3f8db3

=== Operating System ===

Mac OS X 10.10.3
Darwin XamnewiMac.local 14.3.0 Darwin Kernel Version 14.3.0
    Mon Mar 23 11:59:05 PDT 2015
    root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64
Comment 24 Rolf Bjarne Kvinge [MSFT] 2015-05-01 08:35:47 UTC
Backported.

maccore/macios-cycle5: d153c6020a3a85c339984380ccbe8606aff22c68
Comment 25 Sebastien Pouliot 2015-05-04 17:30:57 UTC
*** Bug 29714 has been marked as a duplicate of this bug. ***
Comment 26 Sebastien Pouliot 2015-11-03 19:12:56 UTC
The original fix (better) is back in master (for C7) since the issue was found and fixed (#35372). The extra mark (2nd fix) is still present but should not be needed.

Moving back to RESOLVED|FIXED so QA can test/confirm this again in C7.
Comment 27 Parmendra Kumar 2016-05-20 17:15:44 UTC
I have checked this issue with latest C7 build and observed that I am successfully able to run attached sample. I am not getting any error message.

Screencast: http://www.screencast.com/t/4dbMjO8zZ

Hence marked this issue Verified Fixed.

EnvironmentInfo:
Xamarin Studio 6.0 (build 5165)
Mono 4.4.0 (mono-4.4.0-branch/f3253a0) (64-bit)
Xcode 7.2 (9548)
Xamarin.iOS: 9.8.0.318 (Xamarin Enterprise)
Xamarin.Android : 6.1.0.56 (Xamarin Enterprise)
Mac OS X 10.10.5