Add test for dynamic method resurrection.
authorRodrigo Kumpera <kumpera@gmail.com>
Tue, 1 Feb 2011 17:08:36 +0000 (18:08 +0100)
committerRodrigo Kumpera <kumpera@gmail.com>
Tue, 1 Feb 2011 17:08:57 +0000 (18:08 +0100)
mono/tests/Makefile.am
mono/tests/dynamic-method-resurrection.cs [new file with mode: 0644]

index 0e9c429ee2ace9a3a674aec31d6802a7ec0d3a42..a7145476017caa9a4618fcb55ee9db3d971d96b5 100644 (file)
@@ -371,7 +371,8 @@ BASE_TEST_CS_SRC=           \
        bug-575941.cs   \
        bug-599469.cs   \
        bug-389886-3.cs \
-       monitor.cs
+       monitor.cs      \
+       dynamic-method-resurrection.cs
 
 TEST_CS_SRC_DIST=      \
        $(BASE_TEST_CS_SRC)     \
diff --git a/mono/tests/dynamic-method-resurrection.cs b/mono/tests/dynamic-method-resurrection.cs
new file mode 100644 (file)
index 0000000..bd7bc5f
--- /dev/null
@@ -0,0 +1,63 @@
+using System;
+using System.Reflection;
+using System.Reflection.Emit;
+using System.Runtime.InteropServices;
+using System.Runtime.CompilerServices;
+
+delegate int Getter ();
+
+class Host {
+
+       static int Field = 42;
+
+       Getter g;
+
+       public Host (Getter g) {
+               this.g = g;
+       }
+
+       ~Host () {
+               Program.resed = g;
+       }
+}
+
+
+class Program {
+               internal static Getter resed;
+               static int result;
+               static void DoStuff ()
+        {
+                       DynamicMethod method = new DynamicMethod ("GetField",
+                        typeof (int), new Type [0], Type.GetType ("Host"));
+
+                       ILGenerator il = method.GetILGenerator ();
+                       il.Emit (OpCodes.Ldsfld, typeof (Host).GetField ("Field", BindingFlags.Static | BindingFlags.NonPublic));
+                       il.Emit (OpCodes.Ret);
+
+                       var g = (Getter)method.CreateDelegate (typeof (Getter));                        
+                       new Host (g);
+        }
+
+               static bool CheckStuff () {
+                       if (resed == null)
+                               return false;
+                       Program.result = resed ();
+                       resed = null;
+                       return true;
+               }
+
+               public static int Main ()
+        {
+                       int cnt = 5;
+                       DoStuff ();
+                       do {
+                               if (CheckStuff ())
+                                       break;
+                               GC.Collect ();
+                               GC.WaitForPendingFinalizers ();
+                       } while (cnt-- > 0);
+                       GC.Collect ();
+                       GC.WaitForPendingFinalizers ();
+                       return result == 42 ? 0 : 1;
+               }
+}