Bug 9562 - TypeLoadException on instantiating emitted generic type
Summary: TypeLoadException on instantiating emitted generic type
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: Reflection ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-01-14 20:55 UTC by Ben Bader
Modified: 2015-09-30 18:45 UTC (History)
6 users (show)

Tags: reflection,emit,generics
Is this bug a regression?: ---
Last known good build:


Attachments
A complete reproduction (2.23 KB, text/plain)
2013-01-14 20:55 UTC, Ben Bader
Details


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:
Status:
RESOLVED FIXED

Description Ben Bader 2013-01-14 20:55:00 UTC
Created attachment 3217 [details]
A complete reproduction

Briefly, instantiating a dynamically-generated generic type with a value-type parameter and a recursive private constructor blows up.

Specifically:
A new generic type is emmitted such that:
a) It has an open generic parameter
b) It has a constructor taking a boolean parameter which, when false, causes the ctor to invoke itself
c) It has a paramterless public constructor which invokes the private constructor

The type is instantiated as follows:
1) var concreteType = openType.MakeGenericType(typeof(int)); // Really, any value type
2) Activator.CreateInstance(concreteType, new object[0]);

This results in a TargetInvocationException whose InnerException is an erroneous TypeLoadException.

If the recursive constructor is called directly instead of being chained through the default constructor, code works.
If the generic parameter is a reference type code works.

A complete reproduction is attached.

Repro steps:
mcs Repro.cs && mono Repro.exe

Actual Results:

Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeLoadException: A type load exception has occurred.
  at ChildType`1[System.Int32]..ctor () [0x00000] in <filename unknown>:0 
  at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
  at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  at System.Activator.CreateInstance (System.Type type, BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) [0x00000] in <filename unknown>:0 
  at System.Activator.CreateInstance (System.Type type, System.Object[] args, System.Object[] activationAttributes) [0x00000] in <filename unknown>:0 
  at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in <filename unknown>:0 
  at Repro.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeLoadException: A type load exception has occurred.
  at ChildType`1[System.Int32]..ctor () [0x00000] in <filename unknown>:0 
  at (wrapper managed-to-native) System.Reflection.MonoCMethod:InternalInvoke (System.Reflection.MonoCMethod,object,object[],System.Exception&)
  at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at System.Reflection.MonoCMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0 
  at System.Activator.CreateInstance (System.Type type, BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) [0x00000] in <filename unknown>:0 
  at System.Activator.CreateInstance (System.Type type, System.Object[] args, System.Object[] activationAttributes) [0x00000] in <filename unknown>:0 
  at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in <filename unknown>:0 
  at Repro.Program.Main (System.String[] args) [0x00000] in <filename unknown>:0 


Expected Results:
"ChildType`1"

How often?
All the time.  Currently running Mono 2.10.9 on OS X 10.8.2.

Additional Info:
This exact code performs as expected in .NET 4.0 on Windows 7.
Comment 1 Zoltan Varga 2013-01-15 08:53:42 UTC
The testcase generates IL which calls 'cb' which is a constructor of an open generic type, and that is not valid IL, no idea why it works on windows.
The proper way to call it is:

			var t2 = typeBuilder.MakeGenericType (typeBuilder.GetGenericArguments ());
			
			il.Emit(OpCodes.Ldc_I4_1);
			il.Emit(OpCodes.Newobj, TypeBuilder.GetConstructor (t2, cb));
			il.Emit(OpCodes.Pop);
Comment 2 Rodrigo Kumpera 2013-08-21 11:06:31 UTC
Hi Zoltan,

This is a known quirk of SRE.

You must handle non-inflated methods/ctors in a special way when they are self-referencing.
Comment 3 Aleksey Kliger 2015-09-30 18:45:58 UTC
Works as expected in master and since at least Mono 3.2.7.