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 (show other bugs)
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)

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

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

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.

Note You need to log in before you can comment on or make changes to this bug.