2 using System.Reflection;
3 using System.Runtime.CompilerServices;
6 * Regression tests for the mono JIT.
8 * Each test needs to be of the form:
10 * public static int test_<result>_<name> ();
12 * where <result> is an integer (the value that needs to be returned by
13 * the method to make it pass.
14 * <name> is a user-displayed name used to identify the test.
16 * The tests can be driven in two ways:
17 * *) running the program directly: Main() uses reflection to find and invoke
18 * the test methods (this is useful mostly to check that the tests are correct)
19 * *) with the --regression switch of the jit (this is the preferred way since
20 * all the tests will be run with optimizations on and off)
22 * The reflection logic could be moved to a .dll since we need at least another
23 * regression test file written in IL code to have better control on how
35 public static int Main (string[] args) {
36 return TestDriver.RunTests (typeof (Tests), args);
40 public static int test_0_catch () {
41 Exception x = new Exception ();
45 } catch (Exception e) {
52 public static int test_0_finally_without_exc () {
57 } catch (Exception e) {
66 public static int test_0_finally () {
70 throw new Exception ();
71 } catch (Exception e) {
79 public static int test_0_nested_finally () {
94 public static int test_0_byte_cast () {
107 } catch (OverflowException) {
121 } catch (OverflowException) {
136 } catch (OverflowException) {
150 } catch (OverflowException) {
164 } catch (OverflowException) {
178 } catch (OverflowException) {
192 } catch (OverflowException) {
206 } catch (OverflowException) {
220 } catch (OverflowException) {
234 } catch (OverflowException) {
248 } catch (OverflowException) {
262 } catch (OverflowException) {
277 catch (OverflowException) {
288 public static int test_0_sbyte_cast () {
300 } catch (OverflowException) {
314 } catch (OverflowException) {
328 } catch (OverflowException) {
342 } catch (OverflowException) {
356 } catch (OverflowException) {
370 } catch (OverflowException) {
384 } catch (OverflowException) {
398 } catch (OverflowException) {
412 } catch (OverflowException) {
426 } catch (OverflowException) {
440 } catch (OverflowException) {
454 } catch (OverflowException) {
468 } catch (OverflowException) {
482 } catch (OverflowException) {
496 } catch (OverflowException) {
510 } catch (OverflowException) {
524 } catch (OverflowException) {
538 } catch (OverflowException) {
552 } catch (OverflowException) {
566 } catch (OverflowException) {
581 catch (OverflowException) {
592 public static int test_0_ushort_cast () {
600 a = System.UInt16.MaxValue;
605 } catch (OverflowException) {
617 } catch (OverflowException) {
624 a = System.UInt16.MaxValue + 1;
629 } catch (OverflowException) {
641 } catch (OverflowException) {
653 } catch (OverflowException) {
660 double d = System.UInt16.MaxValue;
665 } catch (OverflowException) {
677 } catch (OverflowException) {
684 double d = System.UInt16.MaxValue + 1.0;
689 } catch (OverflowException) {
696 l = System.UInt16.MaxValue;
701 } catch (OverflowException) {
713 } catch (OverflowException) {
720 l = System.UInt16.MaxValue + 1;
725 } catch (OverflowException) {
737 } catch (OverflowException) {
749 } catch (OverflowException) {
758 public static int test_0_short_cast () {
765 a = System.UInt16.MaxValue;
770 } catch (OverflowException) {
782 } catch (OverflowException) {
789 a = System.Int16.MaxValue + 1;
794 } catch (OverflowException) {
801 a = System.Int16.MinValue - 1;
806 } catch (OverflowException) {
818 } catch (OverflowException) {
825 a = System.Int16.MinValue;
830 } catch (OverflowException) {
837 a = System.Int16.MaxValue;
842 } catch (OverflowException) {
849 a = System.Int16.MaxValue + 1;
854 } catch (OverflowException) {
861 double d = System.Int16.MaxValue;
866 } catch (OverflowException) {
873 double d = System.Int16.MinValue;
878 } catch (OverflowException) {
885 double d = System.Int16.MaxValue + 1.0;
890 } catch (OverflowException) {
897 double d = System.Int16.MinValue - 1.0;
902 } catch (OverflowException) {
909 l = System.Int16.MaxValue + 1;
914 } catch (OverflowException) {
921 l = System.Int16.MaxValue;
926 } catch (OverflowException) {
933 l = System.Int16.MinValue - 1;
938 } catch (OverflowException) {
946 l = System.Int16.MinValue;
951 } catch (OverflowException) {
958 l = 0x00000000ffffffff;
963 } catch (OverflowException) {
975 } catch (OverflowException) {
984 public static int test_0_int_cast () {
990 double d = System.Int32.MaxValue + 1.0;
995 } catch (OverflowException) {
1002 double d = System.Int32.MaxValue;
1007 } catch (OverflowException) {
1015 double d = System.Int32.MinValue;
1020 } catch (OverflowException) {
1028 double d = System.Int32.MinValue - 1.0;
1033 } catch (OverflowException) {
1040 l = System.Int32.MaxValue + (long)1;
1045 } catch (OverflowException) {
1052 l = System.Int32.MaxValue;
1057 } catch (OverflowException) {
1065 l = System.Int32.MinValue;
1070 } catch (OverflowException) {
1078 l = System.Int32.MinValue - (long)1;
1083 } catch (OverflowException) {
1090 uint ui = System.UInt32.MaxValue;
1096 catch (OverflowException) {
1103 ulong ul = (long)(System.Int32.MaxValue) + 1;
1109 catch (OverflowException) {
1116 ulong ul = UInt64.MaxValue;
1122 catch (OverflowException) {
1139 public static int test_0_uint_cast () {
1145 double d = System.UInt32.MaxValue;
1150 } catch (OverflowException) {
1157 double d = System.UInt32.MaxValue + 1.0;
1162 } catch (OverflowException) {
1169 double d = System.UInt32.MinValue;
1174 } catch (OverflowException) {
1181 double d = System.UInt32.MinValue - 1.0;
1186 } catch (OverflowException) {
1193 l = System.UInt32.MaxValue;
1198 } catch (OverflowException) {
1205 l = System.UInt32.MaxValue + (long)1;
1210 } catch (OverflowException) {
1217 l = System.UInt32.MinValue;
1222 } catch (OverflowException) {
1229 l = System.UInt32.MinValue - (long)1;
1234 } catch (OverflowException) {
1247 catch (OverflowException) {
1264 public static int test_0_long_cast () {
1267 * These tests depend on properties of x86 fp arithmetic so they won't work
1268 * on other platforms.
1275 double d = System.Int64.MaxValue - 512.0;
1280 } catch (OverflowException) {
1287 double d = System.Int64.MaxValue - 513.0;
1292 } catch (OverflowException) {
1299 double d = System.Int64.MinValue - 1024.0;
1304 } catch (OverflowException) {
1311 double d = System.Int64.MinValue - 1025.0;
1316 } catch (OverflowException) {
1334 public static int test_0_ulong_cast () {
1339 * These tests depend on properties of x86 fp arithmetic so they won't work
1340 * on other platforms.
1345 double d = System.UInt64.MaxValue - 1024.0;
1350 } catch (OverflowException) {
1357 double d = System.UInt64.MaxValue - 1025.0;
1362 } catch (OverflowException) {
1375 } catch (OverflowException) {
1387 } catch (OverflowException) {
1408 catch (OverflowException) {
1415 int i = Int32.MinValue;
1421 catch (OverflowException) {
1430 public static int test_0_simple_double_casts () {
1432 double d = 0xffffffff;
1434 if ((uint)d != 4294967295)
1438 * These tests depend on properties of x86 fp arithmetic so they won't work
1439 * on other platforms.
1442 d = 0xffffffffffffffff;
1456 if ((ushort)d != 0xffff)
1459 if ((byte)d != 0xff)
1465 [Category ("NaClDisable")]
1466 public static int test_0_div_zero () {
1475 } catch (DivideByZeroException) {
1484 } catch (DivideByZeroException) {
1495 } catch (DivideByZeroException) {
1496 /* wrong exception */
1497 } catch (OverflowException) {
1508 } catch (DivideByZeroException) {
1509 /* wrong exception */
1510 } catch (OverflowException) {
1519 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1520 static void dummy () {
1523 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1524 static int div_zero_llvm_inner (int i) {
1526 // This call make use avoid the 'handler without invoke' restriction in the llvm backend
1529 } catch (Exception ex) {
1534 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1535 static long div_zero_llvm_inner_long (long l) {
1539 } catch (Exception ex) {
1544 public static int test_0_div_zero_llvm () {
1545 long r = div_zero_llvm_inner (0);
1548 r = div_zero_llvm_inner_long (0);
1554 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1555 static int div_overflow_llvm_inner (int i) {
1558 return Int32.MinValue / i;
1559 } catch (Exception ex) {
1564 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1565 static long div_overflow_llvm_inner_long (long l) {
1568 return Int64.MinValue / l;
1569 } catch (Exception ex) {
1574 public static int test_0_div_overflow_llvm () {
1575 long r = div_overflow_llvm_inner (-1);
1578 r = div_overflow_llvm_inner_long ((long)-1);
1584 public static int return_55 () {
1588 public static int test_0_cfold_div_zero () {
1589 // Test that constant folding doesn't cause division by zero exceptions
1590 if (return_55 () != return_55 ()) {
1609 public static int test_0_udiv_zero () {
1618 } catch (DivideByZeroException) {
1627 } catch (DivideByZeroException) {
1636 [Category ("NaClDisable")]
1637 public static int test_0_long_div_zero () {
1646 } catch (DivideByZeroException) {
1655 } catch (DivideByZeroException) {
1666 } catch (DivideByZeroException) {
1667 /* wrong exception */
1668 } catch (ArithmeticException) {
1679 } catch (DivideByZeroException) {
1680 /* wrong exception */
1681 } catch (ArithmeticException) {
1690 public static int test_0_ulong_div_zero () {
1699 } catch (DivideByZeroException) {
1708 } catch (DivideByZeroException) {
1717 public static int test_0_float_div_zero () {
1726 } catch (DivideByZeroException) {
1735 } catch (DivideByZeroException) {
1744 public static int test_0_invalid_unbox () {
1747 object o = "Some string";
1751 // Illegal conversion; o contains a string not an int
1753 } catch (Exception e) {
1761 // Test that double[] can't be cast to double (bug #46027)
1762 public static int test_0_invalid_unbox_arrays () {
1763 double[] d1 = { 1.0 };
1764 double[][] d2 = { d1 };
1768 foreach (double d in a) {
1772 catch (InvalidCastException e) {
1777 /* bug# 42190, at least mcs generates a leave for the return that
1778 * jumps out of multiple exception clauses: we used to execute just
1779 * one enclosing finally block.
1781 public static int finally_level;
1782 static void do_something () {
1795 public static int test_2_multiple_finally_clauses () {
1798 if (finally_level == 1)
1803 public static int test_3_checked_cast_un () {
1804 ulong i = 0x8000000034000000;
1808 checked { j = (long)i; }
1809 } catch (OverflowException) {
1818 public static int test_4_checked_cast () {
1822 unchecked { i = (long)0x8000000034000000;};
1824 checked { j = (ulong)i; }
1825 } catch (OverflowException) {
1834 static readonly int[] mul_dim_results = new int[] {
1835 0, 0, 0, 1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6, 0, 7, 0, 8,
1836 1, 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 8,
1840 5, 0, 5, 1, 5, 2, 5, 3, 5, 4, 5, 5, 5, 6, 5, 7, 5, 8,
1841 6, 0, 6, 1, 6, 2, 6, 3, 6, 4, 6, 5, 6, 6, 6, 7, 6, 8,
1842 7, 0, 7, 1, 7, 2, 7, 3, 7, 4, 7, 5, 7, 6, 7, 7, 7, 8,
1845 public static int test_0_multi_dim_array_access () {
1846 int [,] a = System.Array.CreateInstance (typeof (int),
1847 new int [] {3,6}, new int [] {2,2 }) as int[,];
1850 for (x = 0; x < 8; ++x) {
1851 for (y = 0; y < 9; ++y) {
1852 bool got_ex = false;
1859 if (result_idx >= mul_dim_results.Length)
1861 if (mul_dim_results [result_idx] != x || mul_dim_results [result_idx + 1] != y) {
1862 return result_idx + 1;
1868 if (result_idx == mul_dim_results.Length)
1873 static void helper_out_obj (out object o) {
1874 o = (object)"buddy";
1877 static void helper_out_string (out string o) {
1881 public static int test_2_array_mismatch () {
1882 string[] a = { "hello", "world" };
1884 bool passed = false;
1887 helper_out_obj (out b [1]);
1888 } catch (ArrayTypeMismatchException) {
1893 helper_out_string (out a [1]);
1894 if (a [1] != "buddy")
1899 public static int test_0_ovf1 () {
1904 ulong a = UInt64.MaxValue - 1;
1913 public static int test_1_ovf2 () {
1918 ulong a = UInt64.MaxValue;
1927 public static int test_0_ovf3 () {
1930 long a = Int64.MaxValue - 1;
1941 public static int test_1_ovf4 () {
1944 long a = Int64.MaxValue;
1955 public static int test_0_ovf5 () {
1958 ulong a = UInt64.MaxValue - 1;
1969 public static int test_1_ovf6 () {
1972 ulong a = UInt64.MaxValue;
1983 public static int test_0_ovf7 () {
1986 long a = Int64.MinValue + 1;
1997 public static int test_1_ovf8 () {
2000 long a = Int64.MinValue;
2011 public static int test_0_ovf9 () {
2014 ulong a = UInt64.MinValue + 1;
2025 public static int test_1_ovf10 () {
2028 ulong a = UInt64.MinValue;
2039 public static int test_0_ovf11 () {
2042 int a = Int32.MinValue + 1;
2053 public static int test_1_ovf12 () {
2056 int a = Int32.MinValue;
2067 public static int test_0_ovf13 () {
2081 public static int test_1_ovf14 () {
2095 public static int test_0_ovf15 () {
2109 public static int test_1_ovf16 () {
2123 public static int test_0_ovf17 () {
2135 public static int test_0_ovf18 () {
2149 public static int test_1_ovf19 () {
2163 public static int test_0_ovf20 () {
2168 ulong a = 0xffffffffff;
2169 ulong t = a*0x0ffffff;
2177 public static int test_1_ovf21 () {
2180 ulong a = 0xffffffffff;
2183 ulong t = a*0x0fffffff;
2191 public static int test_1_ovf22 () {
2194 long a = Int64.MinValue;
2206 public static int test_1_ovf23 () {
2210 long b = Int64.MinValue;
2222 public static int i;
2225 throw new Exception ("Ugh!");
2228 public static int DoSomething () {
2233 [Category ("!INTERPRETER")]
2234 public static int test_0_exception_in_cctor () {
2236 Broken.DoSomething ();
2238 catch (TypeInitializationException) {
2239 // This will only happen once even if --regression is used
2244 public static int test_5_regalloc () {
2248 for (i = 0; i < 10; ++i) {
2250 throw new Exception ();
2258 // Check that variables written in catch clauses are volatile
2261 throw new Exception ();
2272 throw new Exception ();
2286 public static void rethrow () {
2288 throw new ApplicationException();
2289 } catch (ApplicationException) {
2291 throw new OverflowException();
2292 } catch (Exception) {
2298 // Test that a rethrow rethrows the correct exception
2299 public static int test_0_rethrow_nested () {
2302 } catch (OverflowException) {
2304 } catch (Exception) {
2310 [MethodImplAttribute (MethodImplOptions.NoInlining)]
2311 public static void rethrow1 () {
2312 throw new Exception ();
2315 [MethodImplAttribute (MethodImplOptions.NoInlining)]
2316 public static void rethrow2 () {
2318 /* This disables tailcall opts */
2319 Console.WriteLine ();
2322 [Category ("!INTERPRETER")]
2323 [Category ("!BITCODE")]
2324 public static int test_0_rethrow_stacktrace () {
2325 // Check that rethrowing an exception preserves the original stack trace
2330 catch (Exception ex) {
2331 // Check that each catch clause has its own exception variable
2332 // If not, the throw below will overwrite the exception used
2335 throw new DivideByZeroException ();
2337 catch (Exception foo) {
2343 catch (Exception ex) {
2344 if (ex.StackTrace.IndexOf ("rethrow2") != -1)
2352 class Face : IFace {}
2354 public static int test_1_array_mismatch_2 () {
2356 object [] o = new Face [1];
2359 } catch (ArrayTypeMismatchException) {
2364 public static int test_1_array_mismatch_3 () {
2366 object [] o = new IFace [1];
2369 } catch (ArrayTypeMismatchException) {
2374 public static int test_1_array_mismatch_4 () {
2376 object [][] o = new Face [5] [];
2377 o [0] = new object [5];
2380 } catch (ArrayTypeMismatchException) {
2385 public static int test_0_array_size () {
2390 int[,] mem2 = new int [Int32.MaxValue, Int32.MaxValue];
2392 catch (OutOfMemoryException e) {
2402 int i, j, k, l, m, n;
2405 static IntPtr[] addr;
2407 static unsafe void throw_func (int i, S s) {
2408 addr [i] = new IntPtr (&i);
2409 throw new Exception ();
2412 /* Test that arguments are correctly popped off the stack during unwinding */
2413 /* FIXME: Fails on x86 when llvm is enabled (#5432) */
2415 public static int test_0_stack_unwind () {
2416 addr = new IntPtr [1000];
2418 for (int j = 0; j < 1000; j++) {
2425 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2429 static unsafe void get_sp (int i) {
2430 addr [i] = new IntPtr (&i);
2433 /* Test that the arguments to the throw trampoline are correctly popped off the stack */
2434 public static int test_0_throw_unwind () {
2435 addr = new IntPtr [1000];
2437 for (int j = 0; j < 1000; j++) {
2440 throw new Exception ();
2445 return (addr [0].ToInt64 () - addr [100].ToInt64 () < 100) ? 0 : 1;
2448 public static int test_0_regress_73242 () {
2449 int [] arr = new int [10];
2450 for (int i = 0; i < 10; ++i)
2453 throw new Exception ();
2460 public static int test_0_nullref () {
2464 } catch (NullReferenceException e) {
2470 public int amethod () {
2474 public static int test_0_nonvirt_nullref_at_clause_start () {
2475 ExceptionTests t = null;
2478 } catch (NullReferenceException) {
2485 public static int throw_only () {
2486 throw new Exception ();
2489 [MethodImpl(MethodImplOptions.NoInlining)]
2490 public static int throw_only2 () {
2491 return throw_only ();
2494 public static int test_0_inline_throw_only () {
2496 return throw_only2 ();
2498 catch (Exception ex) {
2503 public static string GetText (string s) {
2507 public static int throw_only_gettext () {
2508 throw new Exception (GetText ("FOO"));
2511 public static int test_0_inline_throw_only_gettext () {
2514 o = throw_only_gettext ();
2516 catch (Exception ex) {
2520 return o != null ? 0 : 1;
2524 public static int test_0_throw_to_branch_opt_outer_clause () {
2529 string [] files = new string[1];
2531 string s = files[2];
2538 return (i == 1) ? 0 : 1;
2542 public static int test_0_try_inside_finally_cmov_opt () {
2543 bool Reconect = false;
2545 object o = new object ();
2549 catch (Exception ExCon) {
2555 catch (Exception Last) {
2559 if (Reconect == true) {
2562 catch (Exception ex) {
2570 public static int test_0_inline_throw () {
2579 // for llvm, the end bblock is unreachable
2580 public static int inline_throw1 (int i) {
2582 throw new Exception ();
2584 return inline_throw2 (i);
2587 public static int inline_throw2 (int i) {
2588 throw new Exception ();
2592 public static int test_0_lmf_filter () {
2594 // The invoke calls a runtime-invoke wrapper which has a filter clause
2596 typeof (ExceptionTests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2598 typeof (Tests).GetMethod ("lmf_filter").Invoke (null, new object [] { });
2600 } catch (TargetInvocationException) {
2605 public static void lmf_filter () {
2610 throw new NotImplementedException ();
2614 public static void Connect () {
2616 throw new Exception();
2619 public static void Stop () {
2627 private static void do_raise () {
2628 throw new System.Exception ();
2631 private static int int_func (int i) {
2636 public static int test_8_local_deadce_causes () {
2643 } catch (System.Exception) {
2649 public static int test_0_except_opt_two_clauses () {
2652 uint ui = (uint)size;
2655 uint v = ui * (uint)4;
2657 } catch (OverflowException e) {
2659 } catch (Exception) {
2668 public virtual long Method()
2670 throw new Exception();
2675 public static int test_100_long_vars_in_clauses_initlocals_opt () {
2676 Child c = new Child();
2686 public object AnObj;
2689 public static void DoSomething (ref object o) {
2692 public static int test_0_ldflda_null () {
2696 DoSomething (ref a.AnObj);
2697 } catch (NullReferenceException) {
2708 public static Foo* pFoo;
2711 /* MS.NET doesn't seem to throw in this case */
2712 public unsafe static int test_0_ldflda_null_pointer () {
2713 int* pi = &Foo.pFoo->i;
2718 static int test_0_try_clause_in_finally_clause_regalloc () {
2719 // Fill up registers with values
2720 object a = new object ();
2721 object[] arr1 = new object [1];
2722 object[] arr2 = new object [1];
2723 object[] arr3 = new object [1];
2724 object[] arr4 = new object [1];
2725 object[] arr5 = new object [1];
2727 for (int i = 0; i < 10; ++i)
2729 for (int i = 0; i < 10; ++i)
2731 for (int i = 0; i < 10; ++i)
2733 for (int i = 0; i < 10; ++i)
2735 for (int i = 0; i < 10; ++i)
2740 try_clause_in_finally_clause_regalloc_inner (out res);
2741 } catch (Exception) {
2746 public static object Throw () {
2747 for (int i = 0; i < 10; ++i)
2749 throw new Exception ();
2752 static void try_clause_in_finally_clause_regalloc_inner (out int res) {
2758 } catch (Exception) {
2759 /* Make sure this doesn't branch to the finally */
2760 throw new DivideByZeroException ();
2763 /* Make sure o is register allocated */
2776 } catch (DivideByZeroException) {
2781 public static bool t_1835_inner () {
2783 if (a) throw new Exception();
2787 [MethodImpl(MethodImplOptions.NoInlining)]
2788 public static bool t_1835_inner_2 () {
2789 bool b = t_1835_inner ();
2793 public static int test_0_inline_retval_throw_in_branch_1835 () {
2802 static bool finally_called = false;
2804 static void regress_30472 (int a, int b) {
2809 finally_called = true;
2814 public static int test_0_regress_30472 () {
2815 finally_called = false;
2817 regress_30472 (Int32.MaxValue - 1, 2);
2818 } catch (Exception ex) {
2820 return finally_called ? 0 : 1;
2823 static int array_len_1 = 1;
2825 public static int test_0_bounds_check_negative_constant () {
2827 byte[] arr = new byte [array_len_1];
2833 byte[] arr = new byte [array_len_1];
2841 public static int test_0_string_bounds_check_negative_constant () {
2853 class ExceptionTests : Tests