Bug 36471 - Assembly loading bug?
Summary: Assembly loading bug?
Alias: None
Product: Runtime
Classification: Mono
Component: Tools ()
Version: 4.2.0 (C6)
Hardware: PC Linux
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2015-12-02 16:33 UTC by Tom
Modified: 2015-12-04 12:24 UTC (History)
2 users (show)

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

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 GitHub or Developer Community 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:

Description Tom 2015-12-02 16:33:47 UTC
I haven't really figured out what's going on here, but I have an assembly that will load in certain contexts but not others.

System:  Ubuntu 14.04 64-bit, running Mono built from commit 8269971fd1901aa3771a0d0198ffd39672779452 (but the same problem has been seen on version 4.2.1 from the mono-project.com repository).  You'll need a NuGet executable from somewhere and a relatively recent MonoDevelop installation.

Steps to reproduce:

$ git clone https://github.com/tomkcook/T4CSTest.git
$ cd T4CSTest
$ mono NuGet.exe restore T4CSTest.sln
$ xbuild T4CSTest/T4CSTest.csproj
$ xbuild Templates/Templates.csproj
$ cd Templates/bin/Debug
$ mono /path/to/TextTransform.exe ../../T4Template.tt
Invalid type Microsoft.CodeAnalysis.SyntaxTree for instance field Microsoft.CodeAnalysis.SyntaxNode:_syntaxTree
Processing '../../T4Template.tt' failed.
(0,0): ERROR Error running transform: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeLoadException: Failure has occurred while loading a type.
  at T4CSTest.Utilities.GetFirstNamespace (System.String code) <0x409fd130 + 0x0002f> in <filename unknown>:0 
  at Microsoft.VisualStudio.TextTemplating.GeneratedTextTransformation23aa00dc.TransformText () <0x409fcc10 + 0x00087> in <filename unknown>:0 
  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) <0x7f849f9b7d70 + 0x000a3> in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) <0x7f849f9b7d70 + 0x000f1> in <filename unknown>:0 
  at System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) <0x7f849f7c46a0 + 0x0002a> in <filename unknown>:0 
  at Mono.TextTemplating.CompiledTemplate.Process () <0x409fc160 + 0x0036f> in <filename unknown>:0 

However, the following works:

$ cp /path/to/TextTransform.exe .
$ cp /path/to/Mono.TextTemplating.dll .
$ mono TextTransform.exe ../../T4Template.tt

I've poked around a little bit in mono/metadata/class.c and mono_class_set_failure_from_loader_error is being called from mono_field_resolve_type for the field Microsoft.CodeAnalysis.SyntaxTree:lazySha1Checksum, which is of type System.Collections.ImmutableArray<byte>, because mono_metadata_parse_type_full for this generic type is failing.

I think any further diagnosis is going to require running mono under gdb, which I may get to later this week.  I'm hoping someone can tell me what's going wrong in the meantime!
Comment 1 Tom 2015-12-02 16:54:03 UTC
Additional information:

I've set a breakpoint at metadata.c:1689, and gdb reports that this is the content of the error variable:

(gdb) p error
$9 = {error_code = 3, hidden_0 = 1, hidden_1 = {0xe3bc40, 0xe3a490, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 
    0x526806 <mono_class_from_name_checked_aux+358>, 0x7fffffffc020, 0x122, 0x7fffffffc020}, 
  hidden_2 = "Could not resolve type with token 01000034\000\000\000\000\000\000\315u@\366\377\177\000\000\200\231\343\000\000\000\000\000\006hR\000\000\000\000\000\021\002\000\002\000\000\000\000\331\001\000\000\000\000\000\000\210l\336\000\000\000\000\000\030\000\000\000\000\000\000\000\360g\336\000\000\000\000\000_PT\000\000\000\000\000\240\302\377\377\377\177\000"}

I've tried pushing down into do_mono_metadata_parse_type but the optimizer has got to it too much for the debugger to tell me much useful and I've run out of time for today.
Comment 2 Tom 2015-12-03 13:40:10 UTC
I've chased this through as far as mono_class_from_name_checked_aux, which returns NULL for the type System.Collections.Immutable.ImmutableArray`1 at class.c line 7822.  But I'm not at all clear where in the code preceding this line that type should have been found.

search_modules returns NULL because image->tables[MONO_TABLE_FILE] is empty, but I'm not sure if that's expected.

The point where the working one (using TextTransform.exe in the current directory) and the one that doesn't work (using TextTransform.exe in another directory) diverge is that the lookup of nspace_table at class.c:7797 returns a NULL pointer in the failing case but a valid hash table in the working case.
Comment 3 Tom 2015-12-04 12:24:49 UTC
Never mind, this was MonoDevelop's TextTransform doing very strange things in AssemblyResolve event handlers.  See https://github.com/mono/monodevelop/pull/1166.