Imported Upstream version 5.10.0.47

Former-commit-id: d0813289fa2d35e1f8ed77530acb4fb1df441bc0
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2018-01-24 17:04:36 +00:00
parent 88ff76fe28
commit e46a49ecf1
5927 changed files with 226314 additions and 129848 deletions

View File

@ -116,7 +116,6 @@ namespace MonoTests.System.Reflection.Emit
}
[Test]
[Category("NotWorking")]
public void TestGlobalMethods ()
{
AssemblyBuilder builder = genAssembly ();
@ -859,5 +858,332 @@ namespace MonoTests.System.Reflection.Emit
Assert.AreEqual ("t1&", module.GetType ("t1&").FullName);
Assert.AreEqual ("t1[]&", module.GetType ("t1[]&").FullName);
}
[AttributeUsage(AttributeTargets.All)]
public class MyAttribute : Attribute {
public String Contents;
public MyAttribute (String contents)
{
this.Contents = contents;
}
}
[Test]
public void GetMethodsBeforeInstantiation ()
{
AssemblyBuilder assm = AssemblyBuilder.DefineDynamicAssembly (new AssemblyName ("Name"), AssemblyBuilderAccess.Run);
ModuleBuilder module = assm.DefineDynamicModule ("Module");
// Added to make sure fields and methods not mixed up by getters
FieldBuilder fieldBuilder = module.DefineInitializedData ("GlobalField", new byte[4], FieldAttributes.Public);
MethodBuilder method = module.DefinePInvokeMethod ("printf", "libc.so",
MethodAttributes.PinvokeImpl | MethodAttributes.Static | MethodAttributes.Public,
CallingConventions.Standard, typeof (void), new Type [] { typeof (string) }, CallingConvention.Winapi,
CharSet.Auto);
method.SetImplementationFlags (MethodImplAttributes.PreserveSig |
method.GetMethodImplementationFlags ());
module.CreateGlobalFunctions ();
// Make sure method is defined, but field is not
Assert.AreEqual (1, module.GetMethods (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance).Length);
}
[Test]
public void GetFieldsBeforeInstantiation ()
{
AssemblyBuilder assm = AssemblyBuilder.DefineDynamicAssembly (new AssemblyName ("Name"), AssemblyBuilderAccess.Run);
ModuleBuilder module = assm.DefineDynamicModule ("Module");
FieldBuilder fieldBuilder = module.DefineInitializedData ("GlobalField", new byte[4], FieldAttributes.Public);
module.CreateGlobalFunctions ();
var fieldG = module.GetField (fieldBuilder.Name);
Assert.IsNotNull (fieldG);
Assert.AreEqual (1, module.GetFields (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance).Length);
}
[Test]
public void GetCustomAttributesBeforeInstantiation ()
{
AssemblyBuilder assm = AssemblyBuilder.DefineDynamicAssembly (new AssemblyName ("Name"), AssemblyBuilderAccess.Run);
ModuleBuilder module = assm.DefineDynamicModule ("Module");
module.CreateGlobalFunctions ();
ConstructorInfo ctor = typeof(MyAttribute).GetConstructor (new Type [] {typeof(String)});
ctor.GetHashCode ();
CustomAttributeBuilder cab = new CustomAttributeBuilder (ctor, new object [] {"hi"});
module.SetCustomAttribute (cab);
Assert.AreEqual (1, module.GetCustomAttributes (false).Length);
Assert.AreEqual (typeof (MyAttribute), ((MyAttribute) module.GetCustomAttributes (false)[0]).GetType ());
Assert.AreEqual ("hi", ((MyAttribute) module.GetCustomAttributes (false)[0]).Contents);
}
[Test]
public void GetCustomAttributesIgnoresArg ()
{
AssemblyBuilder assm = AssemblyBuilder.DefineDynamicAssembly (new AssemblyName ("Name"), AssemblyBuilderAccess.Run);
ModuleBuilder module = assm.DefineDynamicModule ("Module");
module.CreateGlobalFunctions ();
ConstructorInfo ctor = typeof(MyAttribute).GetConstructor (new Type [] {typeof(String)});
ctor.GetHashCode ();
CustomAttributeBuilder cab = new CustomAttributeBuilder (ctor, new object [] {"hi"});
module.SetCustomAttribute (cab);
var first = module.GetCustomAttributes (false);
var second = module.GetCustomAttributes (true);
Assert.AreEqual (first.Length, second.Length);
for (int i=0; i < first.Length; i++)
Assert.AreEqual (first [i].GetType (), second [i].GetType ());
Assert.AreEqual ("hi", ((MyAttribute) first [0]).Contents);
Assert.AreEqual ("hi", ((MyAttribute) second [0]).Contents);
}
[Test]
public void GetCustomAttributesThrowsUnbakedAttributeType ()
{
AssemblyBuilder assm = AssemblyBuilder.DefineDynamicAssembly (new AssemblyName ("Name"), AssemblyBuilderAccess.Run);
ModuleBuilder module = assm.DefineDynamicModule ("Module");
TypeBuilder tb = module.DefineType ("foo");
module.CreateGlobalFunctions ();
ConstructorInfo ctor = typeof(MyAttribute).GetConstructor (new Type [] {typeof(String)});
ctor.GetHashCode ();
CustomAttributeBuilder cab = new CustomAttributeBuilder (ctor, new object [] {"hi"});
module.SetCustomAttribute (cab);
try {
module.GetCustomAttributes (tb, false);
}
catch (InvalidOperationException e) {
// Correct behavior
return;
}
Assert.Fail ("Supposed to throw");
}
[Test]
public void GetExternalTypeBuilderCAttr ()
{
AssemblyBuilder assm = AssemblyBuilder.DefineDynamicAssembly (new AssemblyName ("Name"), AssemblyBuilderAccess.Run);
ModuleBuilder module = assm.DefineDynamicModule ("Module");
ModuleBuilder module_two = assm.DefineDynamicModule ("ModuleTwo");
TypeBuilder tb = module_two.DefineType ("foo");
ConstructorInfo ctor = tb.DefineConstructor (MethodAttributes.Public, CallingConventions.Standard, Type.EmptyTypes);
CustomAttributeBuilder cab = new CustomAttributeBuilder (ctor, Array.Empty<object> ());
// Set the custom attribute to have a type builder from another module
module.SetCustomAttribute (cab);
module.CreateGlobalFunctions ();
try {
module.GetCustomAttributes (false);
}
catch (NotSupportedException e) {
// Correct behavior
return;
}
Assert.Fail ("Supposed to throw");
}
[Test]
public void GetFieldsNoGlobalType ()
{
AssemblyBuilder assm = AssemblyBuilder.DefineDynamicAssembly (new AssemblyName ("Name"), AssemblyBuilderAccess.Run);
ModuleBuilder module = assm.DefineDynamicModule ("Module");
FieldBuilder fieldBuilder = module.DefineInitializedData ("GlobalField", new byte[4], FieldAttributes.Public);
try {
module.GetFields (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
}
catch (InvalidOperationException e) {
// Correct behavior
return;
}
Assert.Fail ("Supposed to throw");
}
[Test]
public void GetFieldNoGlobalType ()
{
AssemblyBuilder assm = AssemblyBuilder.DefineDynamicAssembly (new AssemblyName ("Name"), AssemblyBuilderAccess.Run);
ModuleBuilder module = assm.DefineDynamicModule ("Module");
FieldBuilder fieldBuilder = module.DefineInitializedData ("GlobalField", new byte[4], FieldAttributes.Public);
try {
module.GetField (fieldBuilder.Name);
}
catch (InvalidOperationException e) {
// Correct behavior
return;
}
Assert.Fail ("Supposed to throw");
}
[Test]
public void GetMethodsNoGlobalType ()
{
AssemblyBuilder assm = AssemblyBuilder.DefineDynamicAssembly (new AssemblyName ("Name"), AssemblyBuilderAccess.Run);
ModuleBuilder module = assm.DefineDynamicModule ("Module");
FieldBuilder fieldBuilder = module.DefineInitializedData ("GlobalField", new byte[4], FieldAttributes.Public);
MethodBuilder method = module.DefinePInvokeMethod ("printf", "libc.so",
MethodAttributes.PinvokeImpl | MethodAttributes.Static | MethodAttributes.Public,
CallingConventions.Standard, typeof (void), new Type [] { typeof (string) }, CallingConvention.Winapi,
CharSet.Auto);
method.SetImplementationFlags (MethodImplAttributes.PreserveSig |
method.GetMethodImplementationFlags ());
try {
module.GetMethods (BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance);
}
catch (InvalidOperationException e) {
// Correct behavior
return;
}
Assert.Fail ("Supposed to throw");
}
[Test]
public void GetMetadataToken ()
{
AssemblyBuilder assm = AssemblyBuilder.DefineDynamicAssembly (new AssemblyName ("Name"), AssemblyBuilderAccess.Run);
ModuleBuilder module = assm.DefineDynamicModule ("Module");
module.CreateGlobalFunctions ();
Assert.AreEqual (0, module.MetadataToken);
}
[Test]
public void SaveMemberRefGtd () {
// Ensure that the a memberref token is emitted for a
// field or for a method of a gtd in the same dynamic assembly.
// Regression test for GitHub #6192
// class T {
// public string F () {
// int i = new C<int>().Foo (42).field1;
// return i.ToString();
// }
// public string F2 () {
// int i = new C<int>().Bar (42);
// return i.ToString ();
// }
// }
// class C<X> {
// public X field1;
// public C<X> Foo (X x) {
// this.field1 = x;
// return this;
// }
// public X Bar (X x) {
// return this.Foo (x).field1;
// }
//}
AssemblyName an = genAssemblyName ();
AssemblyBuilder ab = Thread.GetDomain ().DefineDynamicAssembly (an, AssemblyBuilderAccess.RunAndSave, tempDir);
ModuleBuilder modulebuilder = ab.DefineDynamicModule (an.Name, an.Name + ".dll");
var tb = modulebuilder.DefineType ("T", TypeAttributes.Public);
var il_gen = tb.DefineMethod ("F", MethodAttributes.Public, typeof(string), null).GetILGenerator ();
var il_gen2 = tb.DefineMethod ("F2", MethodAttributes.Public, typeof(string), null).GetILGenerator ();
var cbuilder = modulebuilder.DefineType ("C", TypeAttributes.Public);
var genericParams = cbuilder.DefineGenericParameters ("X");
var field1builder = cbuilder.DefineField ("field1", genericParams[0], FieldAttributes.Public);
var cOfX = cbuilder.MakeGenericType(genericParams);
var fooBuilder = cbuilder.DefineMethod ("Foo",
MethodAttributes.Public,
cOfX,
new Type [] { genericParams[0] });
var cdefaultCtor = cbuilder.DefineDefaultConstructor (MethodAttributes.Public);
var fooIL = fooBuilder.GetILGenerator ();
fooIL.Emit (OpCodes.Ldarg_0);
fooIL.Emit (OpCodes.Ldarg_1);
// Emit (Stfld, field1builder) must generate a memberref token, not fielddef.
fooIL.Emit (OpCodes.Stfld, field1builder);
fooIL.Emit (OpCodes.Ldarg_0);
fooIL.Emit (OpCodes.Ret);
var barBuilder = cbuilder.DefineMethod ("Bar",
MethodAttributes.Public,
genericParams [0],
new Type [] { genericParams [0] });
var barIL = barBuilder.GetILGenerator ();
barIL.Emit (OpCodes.Ldarg_0);
barIL.Emit (OpCodes.Ldarg_1);
// Emit (Call, fooBuilder) must generate a memberref token, not a methoddef.
barIL.Emit (OpCodes.Call, fooBuilder);
barIL.Emit (OpCodes.Ldfld, field1builder);
barIL.Emit (OpCodes.Ret);
var cOfInt32 = cbuilder.MakeGenericType (new Type [] { typeof (int) });
var fooOfInt32 = TypeBuilder.GetMethod (cOfInt32, fooBuilder);
var cfield1OfInt32 = TypeBuilder.GetField (cOfInt32, field1builder);
var intToString = typeof(int).GetMethod ("ToString", Type.EmptyTypes);
var ilocal = il_gen.DeclareLocal (typeof(int));
il_gen.Emit (OpCodes.Newobj, TypeBuilder.GetConstructor (cOfInt32, cdefaultCtor));
il_gen.Emit (OpCodes.Ldc_I4, 42);
il_gen.Emit (OpCodes.Call, fooOfInt32);
il_gen.Emit (OpCodes.Ldfld, cfield1OfInt32);
il_gen.Emit (OpCodes.Stloc, ilocal);
il_gen.Emit (OpCodes.Ldloca, ilocal);
il_gen.Emit (OpCodes.Call, intToString);
il_gen.Emit (OpCodes.Ret);
var i2local = il_gen2.DeclareLocal (typeof (int));
var barOfInt32 = TypeBuilder.GetMethod (cOfInt32, barBuilder);
il_gen2.Emit (OpCodes.Newobj, TypeBuilder.GetConstructor (cOfInt32, cdefaultCtor));
il_gen2.Emit (OpCodes.Ldc_I4, 17);
il_gen2.Emit (OpCodes.Call, barOfInt32);
il_gen2.Emit (OpCodes.Stloc, i2local);
il_gen2.Emit (OpCodes.Ldloca, i2local);
il_gen2.Emit (OpCodes.Call, intToString);
il_gen2.Emit (OpCodes.Ret);
cbuilder.CreateType ();
tb.CreateType ();
ab.Save (an.Name + ".dll");
/* Yes the test really needs to roundtrip through SRE.Save().
* The regression is in the token fixup code on the saving codepath.
*/
var assm = Assembly.LoadFrom (Path.Combine (tempDir, an.Name + ".dll"));
var baked = assm.GetType ("T");
var x = Activator.CreateInstance (baked);
var m = baked.GetMethod ("F");
var s = m.Invoke (x, null);
Assert.AreEqual ("42", s);
var m2 = baked.GetMethod ("F2");
var s2 = m2.Invoke (x, null);
Assert.AreEqual ("17", s2);
}
}
}