[test] Test SRE of a class with two methods with the same name.
authorAleksey Kliger <aleksey@xamarin.com>
Thu, 8 Jun 2017 21:44:54 +0000 (17:44 -0400)
committerAleksey Kliger <aleksey@xamarin.com>
Thu, 8 Jun 2017 21:47:42 +0000 (17:47 -0400)
Regression test for https://bugzilla.xamarin.com/show_bug.cgi?id=57222

Although methods aren't affected by this bug, I added a regression test for
this case anyway in case the implementation changes.

Currently MethodBuilder.RuntimeResolve() goes through a different codepath - it
calls RuntimeType.GetMethod (MethodInfo) which calls into an icall which looks
for a method with a matching token rather than a matching name.

mcs/class/corlib/Test/System.Reflection.Emit/TypeBuilderTest.cs

index 56eef2e51c9db31071ff20cdf643787b1c187bd7..86f87defcf7b78bf32dc068e20857b9351e79c19 100644 (file)
@@ -11364,5 +11364,33 @@ namespace MonoTests.System.Reflection.Emit
                        dynamicAssembly.Save (fileName);
                }
 
+               [Test]
+               public void MethodsWithSameNameAndSig () {
+                       // https://bugzilla.xamarin.com/show_bug.cgi?id=57222
+                       string fileName = CreateTempAssembly ();
+
+                       var assemblyName = new AssemblyName { Name = "test" };
+                       var dynamicAssembly = AssemblyBuilder.DefineDynamicAssembly (assemblyName, AssemblyBuilderAccess.RunAndSave);
+                       var dynamicModule = dynamicAssembly.DefineDynamicModule (assemblyName.Name, fileName);
+                       var typeBuilder = dynamicModule.DefineType ("type1", TypeAttributes.Public | TypeAttributes.Class);
+
+                       var mainMethod = typeBuilder.DefineMethod ("Main", MethodAttributes.Public | MethodAttributes.Static, typeof (int), new Type[0]);
+                       var mainMethodIl = mainMethod.GetILGenerator ();
+
+                       var m1 = typeBuilder.DefineMethod ("X", MethodAttributes.Private | MethodAttributes.Static, typeof (void), new Type [0]);
+                       var m2 = typeBuilder.DefineMethod ("X", MethodAttributes.Private | MethodAttributes.Static, typeof (void), new Type [0]);
+                       m1.GetILGenerator ().Emit (OpCodes.Ret);
+                       m2.GetILGenerator ().Emit (OpCodes.Ret);
+
+                       mainMethodIl.Emit (OpCodes.Call, m1);
+                       mainMethodIl.Emit (OpCodes.Call, m2);
+                       mainMethodIl.Emit (OpCodes.Ldc_I4_0);
+                       mainMethodIl.Emit (OpCodes.Ret);
+
+                       typeBuilder.CreateType ();
+                       dynamicAssembly.SetEntryPoint (mainMethod);
+
+                       dynamicAssembly.Save (fileName);
+               }
        }
 }