Bug 30448

Summary: Cecil loads referenced assemblies from GAC specific to the running application, not the referencing library
Product: [Mono] Class Libraries Reporter: Piotr Zierhoffer <piotr.zierhoffer>
Component: WindowsBaseAssignee: Marek Safar <masafa>
Status: RESOLVED FIXED    
Severity: normal CC: masafa, mono-bugs+mono, piotr.zierhoffer
Priority: ---    
Version: master   
Target Milestone: Untriaged   
Hardware: PC   
OS: Linux   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Failing source code
The project with failing DLL
Updated, much simpler test case

Description Piotr Zierhoffer 2015-05-26 11:11:34 UTC
Created attachment 11330 [details]
Failing source code

I have a code that analyses .NET DLLs with Cecil. One of this DLL is Xwt, containing a struct Xwt.Size with ValueSerializerAttribute defined.

I have bisected Mono and verified that commit 830bf66a13ea18dd8c6f67203d9a70b20349f1d6 breaks my logic - Resolve returns null. Before this commit everything works fine (and this commit description seems relevant).

As my test case requires Xwt.dll (compiled by me with the same Mono, tested many versions), I post both analyzer code and an archive with it.

I have tried to extract a simpler case from this DLL, but failed, anything simpler works for me.

Steps to reproduce the problem:
1. Compile Program.cs in Debug or Release (Cecil library is fetched by NuGet)
2. Go to Debug/Release directory
3. Run

Expected results:
List of analyzed types and attributes in form similar to:

[...]
Xwt.PasswordEntry: 1 attributes
	Xwt.Backends.BackendTypeAttribute
Xwt.PasswordEntry/WidgetBackendHost: 0 attributes
Xwt.PreviewTextInputEventArgs: 0 attributes
Xwt.Widget: 1 attributes
	Xwt.Backends.BackendTypeAttribute
[...]


Actual results:
A beginning of this list, ending with NRE (thrown from code):

[...]
Xwt.Point: 0 attributes
Xwt.Size: 2 attributes
	System.ComponentModel.TypeConverterAttribute
	System.Windows.Markup.ValueSerializerAttribute

Unhandled Exception:
System.NullReferenceException: Resolve returned null!
  at test31.MainClass.Main (System.String[] args) [...]
Comment 1 Piotr Zierhoffer 2015-05-26 11:14:32 UTC
Created attachment 11331 [details]
The project with failing DLL
Comment 2 Piotr Zierhoffer 2015-06-08 07:25:21 UTC
I believe I have found the actual issue.

The Xwt library was compiled against framework 4.0, and the app - 4.5.

ValueSerializerAttribute was moved from System.Xaml to System between this version.

So, TypeRef in Xwt.dll pointed to System.Xaml.dll, but Cecil opened the file from the current GAC - 4.5. Of course, there was no such type found, so Resolve failed.

It works on Windows/.NET, so I believe this is mono-specific code (BaseAssemblyResolver.GetAssemblyInGac is platform-specific).
Comment 3 Piotr Zierhoffer 2015-06-08 07:32:13 UTC
I believe I have found the actual issue.

The Xwt library was compiled against framework 4.0, and the app - 4.5.

ValueSerializerAttribute was moved from System.Xaml to System between this version.

So, TypeRef in Xwt.dll pointed to System.Xaml.dll, but Cecil opened the file from the current GAC - 4.5. Of course, there was no such type found, so Resolve failed.

It works on Windows/.NET, so I believe this is mono-specific code (BaseAssemblyResolver.GetAssemblyInGac is platform-specific).
Comment 4 Piotr Zierhoffer 2015-06-08 08:10:19 UTC
Created attachment 11504 [details]
Updated, much simpler test case

This does not reference Xwt, but a simple library with attribute. The library is compiled against framework 4.0, the app - 4.5.
Comment 5 Marek Safar 2015-06-08 08:23:59 UTC
Fixed in master