2008-08-22 Zoltan Varga <vargaz@gmail.com>
[mono.git] / mono / mini / exceptions.cs
index 406d50cc5cf0e6dbe94e7f94a7afecf6316ec7fd..6971d0e681a3b3174ef1c5a62717f986be9344bc 100644 (file)
@@ -1,12 +1,13 @@
 using System;
 using System.Reflection;
+using System.Runtime.CompilerServices;
 
 /*
  * Regression tests for the mono JIT.
  *
  * Each test needs to be of the form:
  *
- * static int test_<result>_<name> ();
+ * public static int test_<result>_<name> ();
  *
  * where <result> is an integer (the value that needs to be returned by
  * the method to make it pass.
@@ -25,11 +26,11 @@ using System.Reflection;
 
 class Tests {
 
-       static int Main () {
+       public static int Main () {
                return TestDriver.RunTests (typeof (Tests));
        }
 
-       static int test_0_catch () {
+       public static int test_0_catch () {
                Exception x = new Exception ();
                
                try {
@@ -41,7 +42,7 @@ class Tests {
                return 1;
        }
 
-       static int test_0_finally_without_exc () {
+       public static int test_0_finally_without_exc () {
                int x;
                
                try {
@@ -55,7 +56,7 @@ class Tests {
                return x;
        }
 
-       static int test_0_finally () {
+       public static int test_0_finally () {
                int x = 1;
                
                try {
@@ -68,7 +69,7 @@ class Tests {
                return x;
        }
 
-       static int test_0_nested_finally () {
+       public static int test_0_nested_finally () {
                int a;
 
                try {
@@ -83,7 +84,7 @@ class Tests {
                return a;
        }               
 
-       static int test_0_byte_cast () {
+       public static int test_0_byte_cast () {
                int a;
                long l;
                ulong ul;
@@ -277,7 +278,7 @@ class Tests {
                return 0;
        }
        
-       static int test_0_sbyte_cast () {
+       public static int test_0_sbyte_cast () {
                int a;
                long l;
                sbyte b = 0;
@@ -564,12 +565,13 @@ class Tests {
                        return -20;
 
                try {
-                       l = 0x00000000ffffffff;
+                       ulong ul = 128;
                        failed = true;
                        checked {
-                               b = (sbyte)l;
+                               b = (sbyte)ul;
                        }
-               } catch (OverflowException) {
+               }
+               catch (OverflowException) {
                        failed = false;
                }
                if (failed)
@@ -580,7 +582,7 @@ class Tests {
                return 0;
        }
 
-       static int test_0_ushort_cast () {
+       public static int test_0_ushort_cast () {
                int a;
                long l;
                ulong ul;
@@ -746,7 +748,7 @@ class Tests {
                return 0;
        }
        
-       static int test_0_short_cast () {
+       public static int test_0_short_cast () {
                int a;
                long l;
                short b;
@@ -945,10 +947,34 @@ class Tests {
                if (failed)
                        return 16;
 
+               try {
+                       l = 0x00000000ffffffff;
+                       failed = true;
+                       checked {
+                               b = (short)l;
+                       }
+               } catch (OverflowException) {
+                       failed = false;
+               }
+               if (failed)
+                       return 17;
+
+               try {
+                       ulong ul = 32768;
+                       failed = true;
+                       checked {
+                               b = (short)ul;
+                       }
+               } catch (OverflowException) {
+                       failed = false;
+               }
+               if (failed)
+                       return 18;
+
                return 0;
        }
        
-       static int test_0_int_cast () {
+       public static int test_0_int_cast () {
                int a;
                long l;
                bool failed;
@@ -1066,6 +1092,32 @@ class Tests {
                if (failed)
                        return 9;
 
+               try {
+                       ulong ul = (long)(System.Int32.MaxValue) + 1;
+                       failed = true;
+                       checked {
+                               a = (int)ul;
+                       }
+               }
+               catch (OverflowException) {
+                       failed = false;
+               }
+               if (failed)
+                       return 10;
+
+               try {
+                       ulong ul = UInt64.MaxValue;
+                       failed = true;
+                       checked {
+                               a = (int)ul;
+                       }
+               }
+               catch (OverflowException) {
+                       failed = false;
+               }
+               if (failed)
+                       return 11;
+
                {
                        int i; 
                        float f = 1.1f;
@@ -1077,7 +1129,7 @@ class Tests {
                return 0;
        }
 
-       static int test_0_uint_cast () {
+       public static int test_0_uint_cast () {
                uint a;
                long l;
                bool failed;
@@ -1202,7 +1254,13 @@ class Tests {
                return 0;
        }
        
-       static int test_0_long_cast () {
+       public static int test_0_long_cast () {
+
+               /*
+                * These tests depend on properties of x86 fp arithmetic so they won't work
+                * on other platforms.
+                */
+               /*
                long a;
                bool failed;
 
@@ -1230,7 +1288,6 @@ class Tests {
                if (failed)
                        return 2;
                
-
                try {
                        double d = System.Int64.MinValue - 1024.0;
                        failed = false;                 
@@ -1254,6 +1311,7 @@ class Tests {
                }
                if (failed)
                        return 4;
+               */
 
                {
                        long i; 
@@ -1266,10 +1324,16 @@ class Tests {
                return 0;
        }
 
-       static int test_0_ulong_cast () {
+       public static int test_0_ulong_cast () {
                ulong a;
                bool failed;
 
+               /*
+                * These tests depend on properties of x86 fp arithmetic so they won't work
+                * on other platforms.
+                */
+
+               /*
                try {
                        double d = System.UInt64.MaxValue - 1024.0;
                        failed = true;
@@ -1293,7 +1357,7 @@ class Tests {
                }
                if (failed)
                        return 2;
-               
+               */      
 
                try {
                        double d = 0;
@@ -1356,13 +1420,18 @@ class Tests {
                return 0;
        }
 
-       static int test_0_simple_double_casts () {
+       public static int test_0_simple_double_casts () {
 
                double d = 0xffffffff;
 
                if ((uint)d != 4294967295)
                        return 1;
 
+               /*
+                * These tests depend on properties of x86 fp arithmetic so they won't work
+                * on other platforms.
+                */
+               /*
                d = 0xffffffffffffffff;
 
                if ((ulong)d != 0)
@@ -1373,6 +1442,7 @@ class Tests {
                        
                if ((byte)d != 0)
                        return 4;
+               */
                        
                d = 0xffff;
 
@@ -1385,7 +1455,7 @@ class Tests {
                return 0;
        }
        
-       static int test_0_div_zero () {
+       public static int test_0_div_zero () {
                int d = 1;
                int q = 0;
                int val;
@@ -1438,11 +1508,11 @@ class Tests {
                return 0;
        }
 
-       static int return_55 () {
+       public static int return_55 () {
                return 55;
        }
 
-       static int test_0_cfold_div_zero () {
+       public static int test_0_cfold_div_zero () {
                // Test that constant folding doesn't cause division by zero exceptions
                if (return_55 () != return_55 ()) {
                        int d = 1;
@@ -1463,7 +1533,7 @@ class Tests {
                return 0;
        }
 
-       static int test_0_udiv_zero () {
+       public static int test_0_udiv_zero () {
                uint d = 1;
                uint q = 0;
                uint val;
@@ -1490,7 +1560,7 @@ class Tests {
                return 0;
        }
 
-       static int test_0_long_div_zero () {
+       public static int test_0_long_div_zero () {
                long d = 1;
                long q = 0;
                long val;
@@ -1543,7 +1613,7 @@ class Tests {
                return 0;
        }
 
-       static int test_0_ulong_div_zero () {
+       public static int test_0_ulong_div_zero () {
                ulong d = 1;
                ulong q = 0;
                ulong val;
@@ -1570,7 +1640,7 @@ class Tests {
                return 0;
        }
 
-       static int test_0_float_div_zero () {
+       public static int test_0_float_div_zero () {
                double d = 1;
                double q = 0;
                double val;
@@ -1597,7 +1667,7 @@ class Tests {
                return 0;
        }
 
-       static int test_0_invalid_unbox () {
+       public static int test_0_invalid_unbox () {
 
                int i = 123;
                object o = "Some string";
@@ -1615,7 +1685,7 @@ class Tests {
        }
 
        // Test that double[] can't be cast to double (bug #46027)
-       static int test_0_invalid_unbox_arrays () {
+       public static int test_0_invalid_unbox_arrays () {
                double[] d1 = { 1.0 };
                double[][] d2 = { d1 };
                Array a = d2;
@@ -1634,7 +1704,7 @@ class Tests {
         * jumps out of multiple exception clauses: we used to execute just 
         * one enclosing finally block.
         */
-       static int finally_level;
+       public static int finally_level;
        static void do_something () {
                int a = 0;
                try {
@@ -1648,7 +1718,7 @@ class Tests {
                }
        }
 
-       static int test_2_multiple_finally_clauses () {
+       public static int test_2_multiple_finally_clauses () {
                finally_level = 0;
                do_something ();
                if (finally_level == 1)
@@ -1656,7 +1726,7 @@ class Tests {
                return 0;
        }
 
-       static int test_3_checked_cast_un () {
+       public static int test_3_checked_cast_un () {
                 ulong i = 0x8000000034000000;
                 long j;
 
@@ -1671,7 +1741,7 @@ class Tests {
                return 3;
        }
        
-       static int test_4_checked_cast () {
+       public static int test_4_checked_cast () {
                 long i;
                 ulong j;
 
@@ -1698,7 +1768,7 @@ class Tests {
                7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
        };
 
-       static int test_0_multi_dim_array_access () {
+       public static int test_0_multi_dim_array_access () {
                int [,] a = System.Array.CreateInstance (typeof (int),
                        new int [] {3,6}, new int [] {2,2 }) as int[,];
                 int x, y;
@@ -1734,7 +1804,7 @@ class Tests {
                o = "buddy";
        }
 
-       static int test_2_array_mismatch () {
+       public static int test_2_array_mismatch () {
                string[] a = { "hello", "world" };
                object[] b = a;
                bool passed = false;
@@ -1752,245 +1822,330 @@ class Tests {
                return 2;
        }
 
-       static int test_0_ovf () {
-               int ocount = 0;
+       public static int test_0_ovf1 () {
+               int exception = 0;
                
                checked {
-
-                       ocount = 0;
                        try {
                                ulong a =  UInt64.MaxValue - 1;
                                ulong t = a++;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 0)
-                               return 1;
+               }
+               return exception;
+       }
 
-                       ocount = 0;
+       public static int test_1_ovf2 () {
+               int exception = 0;
+
+               checked {
                        try {
                                ulong a =  UInt64.MaxValue;
                                ulong t = a++;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 1)
-                               return 2;
+               }
+               return exception;
+       }
+
+       public static int test_0_ovf3 () {
+               int exception = 0;
 
-                       ocount = 0;
+               long a = Int64.MaxValue - 1;
+               checked {
                        try {
-                               long a = Int64.MaxValue - 1;
                                long t = a++;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 0)
-                               return 3;
+               }
+               return exception;
+       }
+
+       public static int test_1_ovf4 () {
+               int exception = 0;
 
+               long a = Int64.MaxValue;
+               checked {
                        try {
-                               long a = Int64.MaxValue;
                                long t = a++;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 1)
-                               return 4;
+               }
+               return exception;
+       }
+
+       public static int test_0_ovf5 () {
+               int exception = 0;
 
-                       ocount = 0;
+               ulong a = UInt64.MaxValue - 1;
+               checked {
                        try {
-                               ulong a = UInt64.MaxValue - 1;
                                ulong t = a++;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 0)
-                               return 5;
+               }
+               return exception;
+       }
 
+       public static int test_1_ovf6 () {
+               int exception = 0;
+
+               ulong a = UInt64.MaxValue;
+               checked {
                        try {
-                               ulong a = UInt64.MaxValue;
                                ulong t = a++;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 1)
-                               return 6;
+               }
+               return exception;
+       }
+
+       public static int test_0_ovf7 () {
+               int exception = 0;
 
-                       ocount = 0;
+               long a = Int64.MinValue + 1;
+               checked {
                        try {
-                               long a = Int64.MinValue + 1;
                                long t = a--;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 0)
-                               return 7;
+               }
+               return 0;
+       }
+
+       public static int test_1_ovf8 () {
+               int exception = 0;
 
-                       ocount = 0;
+               long a = Int64.MinValue;
+               checked {
                        try {
-                               long a = Int64.MinValue;
                                long t = a--;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 1)
-                               return 8;
+               }
+               return exception;
+       }
 
-                       ocount = 0;
+       public static int test_0_ovf9 () {
+               int exception = 0;
+
+               ulong a = UInt64.MinValue + 1;
+               checked {
                        try {
-                               ulong a = UInt64.MinValue + 1;
                                ulong t = a--;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 0)
-                               return 9;
+               }
+               return exception;
+       }
+
+       public static int test_1_ovf10 () {
+               int exception = 0;
 
-                       ocount = 0;
+               ulong a = UInt64.MinValue;
+               checked {
                        try {
-                               ulong a = UInt64.MinValue;
                                ulong t = a--;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 1)
-                               return 10;
+               }
+               return exception;
+       }
+
+       public static int test_0_ovf11 () {
+               int exception = 0;
 
-                       ocount = 0;
+               int a = Int32.MinValue + 1;
+               checked {
                        try {
-                               int a = Int32.MinValue + 1;
                                int t = a--;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 0)
-                               return 11;
+               }
+               return exception;
+       }
 
-                       ocount = 0;
+       public static int test_1_ovf12 () {
+               int exception = 0;
+
+               int a = Int32.MinValue;
+               checked {
                        try {
-                               int a = Int32.MinValue;
                                int t = a--;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 1)
-                               return 12;
+               }
+               return exception;
+       }
+
+       public static int test_0_ovf13 () {
+               int exception = 0;
 
-                       ocount = 0;
+               uint a = 1;
+               checked {
                        try {
-                               uint a = 1;
                                uint t = a--;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 0)
-                               return 13;
+               }
+               return exception;
+       }
 
-                       ocount = 0;
+       public static int test_1_ovf14 () {
+               int exception = 0;
+
+               uint a = 0;
+               checked {
                        try {
-                               uint a = 0;
                                uint t = a--;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 1)
-                               return 14;
+               }
+               return exception;
+       }
+
+       public static int test_0_ovf15 () {
+               int exception = 0;
 
-                       ocount = 0;
+               sbyte a = 126;
+               checked {
                        try {
-                               sbyte a = 126;
                                sbyte t = a++;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 0)
-                               return 15;
+               }
+               return exception;
+       }
 
-                       ocount = 0;
+       public static int test_1_ovf16 () {
+               int exception = 0;
+
+               sbyte a = 127;
+               checked {
                        try {
-                               sbyte a = 127;
                                sbyte t = a++;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 1)
-                               return 16;
+               }
+               return exception;
+       }
+
+       public static int test_0_ovf17 () {
+               int exception = 0;
 
-                       ocount = 0;
+               checked {
                        try {
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 0)
-                               return 17;
+               }
+               return exception;
+       }
+
+       public static int test_0_ovf18 () {
+               int exception = 0;
 
-                       ocount = 0;
+               int a = 1 << 29;
+               checked {
                        try {
-                               int a = 1 << 29;
                                int t = a*2;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 0)
-                               return 18;
+               }
+               return exception;
+       }
+
+       public static int test_1_ovf19 () {
+               int exception = 0;
 
-                       ocount = 0;
+               int a = 1 << 30;
+               checked {
                        try {
-                               int a = 1 << 30;
                                int t = a*2;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 1)
-                               return 19;
+               }
+               return exception;
+       }
 
-                       ocount = 0;
+       public static int test_0_ovf20 () {
+               int exception = 0;
+
+               checked {
                        try {
                                ulong a = 0xffffffffff;
                                ulong t = a*0x0ffffff;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 0)
-                               return 20;
+               }
+               return exception;
+       }
 
-                       ocount = 0;
+       public static int test_1_ovf21 () {
+               int exception = 0;
+
+               ulong a = 0xffffffffff;
+               checked {
                        try {
-                               ulong a = 0xffffffffff;
                                ulong t = a*0x0fffffff;
                        } catch {
-                               ocount++;
+                               exception = 1;
                        }
-                       if (ocount != 1)
-                               return 21;
+               }
+               return exception;
+       }
+
+       public static int test_1_ovf22 () {
+               int exception = 0;
 
-                       ocount = 0;
+               long a = Int64.MinValue;
+               long b = 10;
+               checked {
                        try {
-                               long a = Int64.MinValue;
-                               long b = 10;
                                long v = a * b;
                        } catch {
-                               ocount ++;
+                               exception = 1;
                        }
-                       if (ocount != 1)
-                               return 22;
+               }
+               return exception;
+       }
 
-                       ocount = 0;
+       public static int test_1_ovf23 () {
+               int exception = 0;
+
+               long a = 10;
+               long b = Int64.MinValue;
+               checked {
                        try {
-                               long a = 10;
-                               long b = Int64.MinValue;
                                long v = a * b;
                        } catch {
-                               ocount ++;
+                               exception = 1;
                        }
-                       if (ocount != 1)
-                               return 23;
                }
-               
-               return 0;
+               return exception;
        }
 
        class Broken {
-               static int i;
+               public static int i;
 
                static Broken () {
                        throw new Exception ("Ugh!");
@@ -2001,7 +2156,7 @@ class Tests {
                }
        }
 
-       static int test_0_exception_in_cctor () {
+       public static int test_0_exception_in_cctor () {
                try {
                        Broken.DoSomething ();
                }
@@ -2011,7 +2166,7 @@ class Tests {
                return 0;
        }
 
-       static int test_5_regalloc () {
+       public static int test_5_regalloc () {
                int i = 0;
 
                try {
@@ -2064,13 +2219,22 @@ class Tests {
                }
        }
 
-       static int test_0_rethrow_stacktrace () {
+       public static int test_0_rethrow_stacktrace () {
                // Check that rethrowing an exception preserves the original stack trace
                try {
                        try {
                                ThrowClass.rethrow2 ();
                        }
                        catch (Exception ex) {
+                               // Check that each catch clause has its own exception variable
+                               // If not, the throw below will overwrite the exception used
+                               // by the rethrow
+                               try {
+                                       throw new DivideByZeroException ();
+                               }
+                               catch (Exception foo) {
+                               }
+
                                throw;
                        }
                }
@@ -2085,7 +2249,7 @@ class Tests {
        interface IFace {}
        class Face : IFace {}
                
-       static int test_1_array_mismatch_2 () {
+       public static int test_1_array_mismatch_2 () {
                try {
                        object [] o = new Face [1];
                        o [0] = 1;
@@ -2095,7 +2259,7 @@ class Tests {
                }
        }
        
-       static int test_1_array_mismatch_3 () {
+       public static int test_1_array_mismatch_3 () {
                try {
                        object [] o = new IFace [1];
                        o [0] = 1;
@@ -2105,7 +2269,7 @@ class Tests {
                }
        }
        
-       static int test_1_array_mismatch_4 () {
+       public static int test_1_array_mismatch_4 () {
                try {
                        object [][] o = new Face [5] [];
                        o [0] = new object [5];
@@ -2116,7 +2280,7 @@ class Tests {
                }
        }
 
-       static int test_0_array_size () {
+       public static int test_0_array_size () {
                bool failed;
 
                try {
@@ -2141,5 +2305,103 @@ class Tests {
 
                return 0;
        }
+
+       struct S {
+               int i, j, k, l, m, n;
+       }
+
+       static IntPtr[] addr;
+
+       static unsafe void throw_func (int i, S s) {
+               addr [i] = new IntPtr (&i);
+               throw new Exception ();
+       }
+
+       /* Test that arguments are correctly popped off the stack during unwinding */
+       public static int test_0_stack_unwind () {
+               addr = new IntPtr [1000];
+               S s = new S ();
+               for (int j = 0; j < 1000; j++) {
+                       try {
+                               throw_func (j, s);
+                       }
+                       catch (Exception) {
+                       }
+               }
+               return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
+       }
+
+       public static int test_0_regress_73242 () {
+               int [] arr = new int [10];
+               for (int i = 0; i < 10; ++i)
+                       arr [i] = 0;
+               try {
+                       throw new Exception ();
+               }
+               catch {
+               }
+               return 0;
+    }
+
+       public static int test_0_nullref () {
+               try {
+                       Array foo = null;
+                       foo.Clone();
+               } catch (NullReferenceException e) {
+                       return 0;
+               }
+               return 1;
+       }
+
+       public int amethod () {
+               return 1;
+       }
+
+       public static int test_0_nonvirt_nullref_at_clause_start () {
+               Tests t = null;
+               try {
+                       t.amethod ();
+               } catch (NullReferenceException) {
+                       return 0;
+               }
+
+               return 1;
+       }
+
+       public static int throw_only () {
+               throw new Exception ();
+       }
+
+       [MethodImpl(MethodImplOptions.NoInlining)] 
+       public static int throw_only2 () {
+               return throw_only ();
+       }
+
+       public static int test_0_inline_throw_only () {
+               try {
+                       return throw_only2 ();
+               }
+               catch (Exception ex) {
+                       return 0;
+               }
+       }
+
+       // bug #78633
+       public static int test_0_throw_to_branch_opt_outer_clause () {
+               int i = 0;
+
+               try {
+                       try {
+                               string [] files = new string[1];
+
+                               string s = files[2];
+                       } finally {
+                               i ++;
+                       }
+               } catch {
+               }
+
+               return (i == 1) ? 0 : 1;
+       }               
 }