Crashing test in mono_class_init() from a MonoGenericClass.

[#35375](https://bugzilla.xamarin.com/show_bug.cgi?id=35375)
This commit is contained in:
Aleksey Kliger committed 2015-11-11 11:09:41 -05:00
1 parent 59f8e6a2bf
commit b503b87a03
1 file changed
+82
@@ -201,6 +201,88 @@ namespace MonoTests.System.Reflection.Emit
Assert.AreSame (expected, t.BaseType, "#1");
}
[Test]
public void GenericClassFromStaleTypeBuilderDoesNotClassInit ()
{
// interface JJJ<T> {
// abstract void W (x : T)
// }
MethodInfo winfo = null;
TypeBuilder ib = null;
Type ic = null;
Type icreated = null;
{
ib = module.DefineType ("Foo.JJJ`1",
TypeAttributes.Public
| TypeAttributes.Interface
| TypeAttributes.Abstract);
String[] gens = { "T" };
GenericTypeParameterBuilder[] gbs = ib.DefineGenericParameters (gens);
var gb = gbs[0];
winfo = ib.DefineMethod ("W",
MethodAttributes.Public |
MethodAttributes.Abstract |
MethodAttributes.Virtual,
CallingConventions.HasThis,
typeof(void),
new Type[] { gb });
icreated = ib.CreateType();
}
// class SSS : JJJ<char> {
// bool wasCalled;
// void JJJ.W (x : T) { wasCalled = true; return; }
// }
TypeBuilder tb = null;
MethodBuilder mb = null;
{
tb = module.DefineType ("Foo.SSS",
TypeAttributes.Public,
null,
new Type[]{ icreated.MakeGenericType(typeof(char)) });
var wasCalledField = tb.DefineField ("wasCalled",
typeof(bool),
FieldAttributes.Public);
mb = tb.DefineMethod ("W_impl",
MethodAttributes.Public | MethodAttributes.Virtual,
CallingConventions.HasThis,
typeof (void),
new Type[] { typeof (char) });
{
var il = mb.GetILGenerator ();
il.Emit (OpCodes.Ldarg_0); // this
il.Emit (OpCodes.Ldc_I4_1);
il.Emit (OpCodes.Stfld, wasCalledField); // this.wasCalled = true
il.Emit (OpCodes.Ret);
}
}
ic = ib.MakeGenericType(typeof (char)); // this is a MonoGenericMethod
var mintf = TypeBuilder.GetMethod(ic, winfo);
// the next line causes mono_class_init() to
// be called on JJJ<char> when we try to setup
// the vtable for SSS
tb.DefineMethodOverride(mb, mintf);
var result = tb.CreateType();
// o = new SSS()
object o = Activator.CreateInstance(result);
Assert.IsNotNull(o, "#1");
// ((JJJ<char>)o).W('a');
var m = icreated.MakeGenericType(typeof(char)).GetMethod("W", BindingFlags.Public | BindingFlags.Instance);
Assert.IsNotNull(m, "#2");
m.Invoke(o, new object[] {'a'});
var f = result.GetField("wasCalled", BindingFlags.Public | BindingFlags.Instance);
Assert.IsNotNull(f, "#3");
var wasCalledVal = f.GetValue(o);
Assert.IsInstanceOfType (typeof(Boolean), wasCalledVal, "#4");
Assert.AreEqual (wasCalledVal, true, "#5");
}
}
}