Bug 57851 - Assertion if using ResolveEventHandler to find custom attributes
Summary: Assertion if using ResolveEventHandler to find custom attributes
Alias: None
Product: Runtime
Classification: Mono
Component: Reflection ()
Version: 5.0 (2017-02)
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Aleksey Kliger
Depends on:
Reported: 2017-06-28 23:42 UTC by Aleksey Kliger
Modified: 2017-06-30 20:15 UTC (History)
1 user (show)

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

reproduction example (1.91 KB, application/zip)
2017-06-28 23:42 UTC, Aleksey Kliger

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 Aleksey Kliger 2017-06-28 23:42:30 UTC
Created attachment 23187 [details]
reproduction example

If a ResolveEventHandler is installed while we are looking for the arguments of custom attributes, we may call ono_assembly_metadata_foreach_custom_attr  on a dynamic assembly, which triggers an assertion:

* Assertion at custom-attrs.c:1947, condition `!image_is_dynamic (image)' not met

To reproduce:
  1. download and unzip the attached file.
  2. run make

Expected Output:
  Some manner of exception

Actual Output:
  the assert from above.
Comment 1 Aleksey Kliger 2017-06-28 23:45:21 UTC
The function mono_assembly_metadata_foreach_custom_attr is called by the loader to look for ReflectionAssemblyAttribute attributes.  It did not expect to be called with a dynamic assembly (as the ResolveEventHandler in the example does).

The solution is to check if the assembly is dynamic and if so go through its attributes  using the normal MonoCustomAttrInfo* APIs.  (I think this is okay to do because the dynamic assembly will have all of its MonoClasses existing already so it won't trigger another loader event.)
Comment 2 Aleksey Kliger 2017-06-29 15:50:48 UTC
Hm. Interestingly, .NET Framework (and .NET Core 2.0) seems to ignore ReferenceAssemblyAttribute when it's set on an AssemblyBuilder and happily runs the generated assembly.  So we should really do two things:
  1. (bugfix) Not even bother checking for the ReferenceAssemblyAttribute when the assembly is dynamic.
  2. (feature enhancement) Teach mono_assembly_metadata_foreach_custom_attr to iterate over dynamic assemblies.