3 using System.Reflection;
4 using System.Runtime.InteropServices;
5 using System.Runtime.CompilerServices;
8 * Regression tests for the mono JIT.
10 * Each test needs to be of the form:
12 * static int test_<result>_<name> ();
14 * where <result> is an integer (the value that needs to be returned by
15 * the method to make it pass.
16 * <name> is a user-displayed name used to identify the test.
18 * The tests can be driven in two ways:
19 * *) running the program directly: Main() uses reflection to find and invoke
20 * the test methods (this is useful mostly to check that the tests are correct)
21 * *) with the --regression switch of the jit (this is the preferred way since
22 * all the tests will be run with optimizations on and off)
24 * The reflection logic could be moved to a .dll since we need at least another
25 * regression test file written in IL code to have better control on how
59 public void populate ()
69 return one == 1 && two == 2 &&
70 three == 3 && four == 4 &&
71 five == 5 && six == 6 &&
72 seven == 7 && eight == 8 &&
73 nine == 9 && ten == 10;
79 public Sample (int v) {
84 [StructLayout ( LayoutKind.Explicit )]
85 struct StructWithBigOffsets {
86 [ FieldOffset(10000) ] public byte b;
87 [ FieldOffset(10001) ] public sbyte sb;
88 [ FieldOffset(11000) ] public short s;
89 [ FieldOffset(11002) ] public ushort us;
90 [ FieldOffset(12000) ] public uint i;
91 [ FieldOffset(12004) ] public int si;
92 [ FieldOffset(13000) ] public long l;
93 [ FieldOffset(14000) ] public float f;
94 [ FieldOffset(15000) ] public double d;
104 public long a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v;
108 public Alpha a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v;
112 public Beta a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v;
118 return TestDriver.RunTests (typeof (Tests));
121 public static int test_0_return () {
125 s.c = (short)(s.a + s.b);
130 public static int test_0_string_access () {
137 public static int test_0_string_virtual_call () {
139 string s2 = s.ToString ();
145 public static int test_0_iface_call () {
147 object o = ((ICloneable)s).Clone ();
151 public static int test_5_newobj () {
152 Sample s = new Sample (5);
156 public static int test_4_box () {
161 public static int test_0_enum_unbox () {
162 SampleEnum x = SampleEnum.A;
172 static Simple get_simple (int v) {
173 Simple r = new Simple ();
176 r.c = (short)(v + 2);
182 public static int test_3_return_struct () {
183 Simple v = get_simple (1);
196 public virtual Simple v_get_simple (int v)
198 return get_simple (v);
201 public static int test_2_return_struct_virtual () {
202 Tests t = new Tests ();
203 Simple v = t.v_get_simple (2);
216 static int receive_simple (int a, Simple v, int b) {
232 public static int test_5_pass_struct () {
233 Simple v = get_simple (1);
234 if (receive_simple (7, v, 9) != 0)
236 if (receive_simple (7, get_simple (1), 9) != 0)
242 public static int test_5_pass_static_struct () {
243 s_v = get_simple (1);
244 if (receive_simple (7, s_v, 9) != 0)
249 // Test alignment of small structs
251 static Small get_small (byte v) {
252 Small r = new Small ();
255 r.b2 = (byte)(v + 1);
260 static Small return_small (Small s) {
264 static int receive_small (int a, Small v, int b) {
272 static int receive_small_sparc_many_args (int a, int a2, int a3, int a4, int a5, int a6, Small v, int b) {
280 public static int test_5_pass_small_struct () {
281 Small v = get_small (1);
282 if (receive_small (7, v, 9) != 0)
284 if (receive_small (7, get_small (1), 9) != 0)
286 if (receive_small_sparc_many_args (1, 2, 3, 4, 5, 6, v, 9) != 0)
288 v = return_small (v);
296 // 64-bits, 32-bit aligned
302 static int check_struct1(struct1 x) {
310 static int pass_struct1(int a, int b, struct1 x) {
315 return check_struct1(x);
318 static int pass_struct1(int a, struct1 x) {
321 return check_struct1(x);
324 static int pass_struct1(struct1 x) {
325 return check_struct1(x);
328 public static int test_0_struct1_args () {
334 if ((r = check_struct1(x)) != 0)
336 if ((r = pass_struct1(x)) != 0)
338 if ((r = pass_struct1(3, x)) != 0)
340 if ((r = pass_struct1(3, 4, x)) != 0)
345 // 64-bits, 64-bit aligned
350 static int check_struct2(struct2 x) {
356 static int pass_struct2(int a, int b, int c, struct2 x) {
363 return check_struct2(x);
366 static int pass_struct2(int a, int b, struct2 x) {
371 return check_struct2(x);
374 static int pass_struct2(int a, struct2 x) {
377 return check_struct2(x);
380 static int pass_struct2(struct2 x) {
381 return check_struct2(x);
384 public static int test_0_struct2_args () {
389 if ((r = check_struct2(x)) != 0)
391 if ((r = pass_struct2(x)) != 0)
393 if ((r = pass_struct2(3, x)) != 0)
395 if ((r = pass_struct2(3, 4, x)) != 0)
397 if ((r = pass_struct2(3, 4, 5, x)) != 0)
404 public long i, j, k, l;
407 static int pass_struct3 (int i, int j, int k, int l, int m, int n, int o, int p, Struct3 s, int q) {
408 if (s.i + s.j + s.k + s.l != 10)
414 public static int test_0_struct3_args () {
415 Struct3 s = new Struct3 ();
421 return pass_struct3 (1, 2, 3, 4, 5, 6, 7, 8, s, 9);
424 // Struct with unaligned size on 64 bit machines
426 public int i, j, k, l, m;
427 public int i1, i2, i3, i4, i5, i6;
430 static int pass_struct4 (Struct4 s) {
431 if (s.i + s.j + s.k + s.l + s.m != 15)
437 public static int test_0_struct4_args () {
438 Struct4 s = new Struct4 ();
445 return pass_struct4 (s);
453 public AStruct (int i) {
457 public override int GetHashCode () {
462 // Test that vtypes are unboxed during a virtual call
463 public static int test_44_unbox_trampoline () {
464 AStruct s = new AStruct (44);
466 return o.GetHashCode ();
469 public static int test_0_unbox_trampoline2 () {
473 if (i.ToString () != "12")
475 if (((Int32)o).ToString () != "12")
477 if (o.ToString () != "12")
482 // Test fields with big offsets
483 public static int test_0_fields_with_big_offsets () {
484 StructWithBigOffsets s = new StructWithBigOffsets ();
485 StructWithBigOffsets s2 = new StructWithBigOffsets ();
511 if (s2.i != 0xdeadbeef)
513 if (s2.l != 0xcafebabe)
532 int buf_length, buf_offset;
540 public long Seek (long position) {
542 /* interaction between the register allocator and
543 * allocating arguments to registers */
544 if (pos >= buf_start && pos <= buf_start + buf_length) {
545 buf_offset = (int) (pos - buf_start);
553 public static int test_0_seektest () {
554 TestRegA t = new TestRegA ();
555 return (int)t.Seek (0);
558 class Super : ICloneable {
559 public virtual object Clone () {
566 public static int test_0_null_cast () {
574 public static int test_0_super_cast () {
575 Duper d = new Duper ();
589 d = (Duper)(object)sup;
596 public static int test_0_super_cast_array () {
597 Duper[] d = new Duper [0];
608 if (!(d is Object[]))
611 d = (Duper[])(object[])sup;
618 public static int test_0_multi_array_cast () {
619 Duper[,] d = new Duper [1, 1];
623 o [0, 0] = new Super ();
626 catch (ArrayTypeMismatchException) {
632 public static int test_0_vector_array_cast () {
633 Array arr1 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0});
634 Array arr2 = Array.CreateInstance (typeof (int), new int[] {1}, new int[] {10});
636 if (arr1.GetType () != typeof (int[]))
639 if (arr2.GetType () == typeof (int[]))
650 catch (InvalidCastException) {
656 int [,] [] arr3 = new int [1, 1] [];
658 int [,] [] arr4 = (int [,] [])o;
663 public static int test_0_enum_array_cast () {
664 TypeCode[] tc = new TypeCode [0];
673 if (a is ValueType[])
687 vta = (ValueType[])a;
704 public static int test_0_more_cast_corner_cases () {
705 ValueType[] vta = new ValueType [0];
706 Enum[] ea = new Enum [0];
711 if (!(a is object[]))
713 if (!(a is ValueType[]))
718 if (!(a is object[]))
720 if (!(a is ValueType[]))
782 public static int test_0_cast_iface_array () {
783 object o = new ICloneable [0];
784 object o2 = new Duper [0];
788 if (!(o is object[]))
790 if (!(o2 is ICloneable[]))
804 t = (ICloneable[])o2;
820 if (!(o is ICloneable[]))
823 /* add tests for interfaces that 'inherit' interfaces */
827 private static int[] daysmonthleap = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
829 private static int AbsoluteDays (int year, int month, int day)
832 int[] days = daysmonthleap;
835 return ((day-1) + temp + (365* (year-1)) + ((year-1)/4) - ((year-1)/100) + ((year-1)/400));
838 public static int test_719162_complex_div () {
839 int adays = AbsoluteDays (1970, 1, 1);
843 delegate int GetIntDel ();
845 static int return4 () {
853 public static int test_2_static_delegate () {
854 GetIntDel del = new GetIntDel (return4);
861 public static int test_2_instance_delegate () {
862 Tests t = new Tests ();
863 GetIntDel del = new GetIntDel (t.return5);
870 public static int test_1_store_decimal () {
871 decimal[,] a = {{1}};
878 public static int test_2_intptr_stobj () {
879 System.IntPtr [] arr = { new System.IntPtr () };
881 if (arr [0] != (System.IntPtr)0)
886 static int llmult (int a, int b, int c, int d) {
887 return a + b + c + d;
891 * Test that evaluation of complex arguments does not overwrite the
892 * arguments already in outgoing registers.
894 public static int test_155_regalloc () {
900 int[] arr = new int [5];
902 return llmult (arr [c + d], 150, 5, 0);
905 static bool large_struct_test (Large a, Large b, Large c, Large d)
907 if (!a.check ()) return false;
908 if (!b.check ()) return false;
909 if (!c.check ()) return false;
910 if (!d.check ()) return false;
914 public static int test_2_large_struct_pass ()
925 if (large_struct_test (a, b, c, d))
930 public static unsafe int test_0_pin_string () {
932 fixed (char *c = x) {
939 public static int my_flags;
940 public static int test_0_and_cmp_static ()
943 /* various forms of test [mem], imm */
945 my_flags = 0x01020304;
947 if ((my_flags & 0x01020304) == 0)
950 if ((my_flags & 0x00000304) == 0)
953 if ((my_flags & 0x00000004) == 0)
956 if ((my_flags & 0x00000300) == 0)
959 if ((my_flags & 0x00020000) == 0)
962 if ((my_flags & 0x01000000) == 0)
969 public static int test_0_byte_compares ()
975 if (!(b < System.Byte.MaxValue))
978 if (!(b <= System.Byte.MaxValue))
984 public static int test_71_long_shift_right () {
985 ulong value = 38654838087;
987 byte [] buffer = new byte [1];
988 buffer [x] = ((byte)(value >> x));
993 public static int test_0_addsub_mem ()
1010 public static int test_0_sh32_mem ()
1012 y = 0x0102130405060708;
1015 if (y != 0x01021304)
1018 y = 0x0102130405060708;
1021 if (y != 0x0506070800000000)
1024 x = 0x0102130405060708;
1027 if (x != 0x0506070800000000)
1034 static uint dum_de_dum = 1;
1035 public static int test_0_long_arg_opt ()
1037 return Foo (0x1234567887654321, dum_de_dum);
1040 static int Foo (ulong x, ulong y)
1042 if (x != 0x1234567887654321)
1051 public static int test_0_long_ret_opt ()
1054 if (x != 0x1234567887654321)
1065 return 0x1234567887654321;
1073 /* from bug# 71515 */
1074 static int counter = 0;
1075 static bool WriteStuff () {
1079 public static int test_0_cond_branch_side_effects () {
1089 public static int arg_only_written (string file_name, int[]
1091 if (file_name == null)
1100 public static int[] foo () {
1104 public static void bar (int i) {
1108 public static int test_0_arg_only_written ()
1110 return arg_only_written ("md.in", null);
1113 static long position = 0;
1115 public static int test_4_static_inc_long () {
1123 return (int)position;
1128 public FooStruct (long l) {
1132 public static int test_0_calls_opcode_emulation () {
1133 // Test that emulated opcodes do not clobber arguments already in
1137 new FooStruct (val * 10000);
1142 public static int test_0_intrins_string_length () {
1145 return (s.Length == 3) ? 0 : 1;
1148 public static int test_0_intrins_string_chars () {
1151 return (s [0] == 'A' && s [1] == 'B' && s [2] == 'C') ? 0 : 1;
1154 public static int test_0_intrins_object_gettype () {
1157 return (o.GetType () == typeof (int)) ? 0 : 1;
1160 public static int test_0_intrins_object_gethashcode () {
1161 object o = new Object ();
1163 return (o.GetHashCode () == o.GetHashCode ()) ? 0 : 1;
1169 public static int test_0_intrins_object_ctor () {
1170 object o = new FooClass ();
1172 return (o != null) ? 0 : 1;
1175 public static int test_0_intrins_array_rank () {
1176 int[,] a = new int [10, 10];
1178 return (a.Rank == 2) ? 0 : 1;
1181 public static int test_0_intrins_array_length () {
1182 int[,] a = new int [10, 10];
1185 return (a2.Length == 100) ? 0 : 1;
1188 public static int test_0_intrins_runtimehelpers_offset_to_string_data () {
1189 int i = RuntimeHelpers.OffsetToStringData;
1194 public static int test_0_intrins_string_setchar () {
1195 StringBuilder sb = new StringBuilder ("ABC");
1199 return sb.ToString () == "ADC" ? 0 : 1;
1203 bool allowLocation = true;
1207 public static int test_0_regress_78990_unaligned_structs () {
1213 public static unsafe int test_97_negative_index () {
1214 char[] arr = new char[] {'a', 'b'};
1215 fixed (char *p = arr) {
1223 public static int test_0_unsigned_right_shift_imm0 () {
1225 byte[] data = new byte[256];
1226 for (int i = 0; i < 1; i ++)
1227 temp = (uint)(data[temp >> 24] | data[temp >> 0]);
1232 public virtual int foo () {
1237 sealed class Bar2 : Foo2 {
1238 public override int foo () {
1243 public static int test_0_abcrem_check_this_removal () {
1244 Bar2 b = new Bar2 ();
1246 // The check_this generated here by the JIT should be removed
1252 static int invoke_twice (Bar2 b) {
1254 // The check_this generated here by the JIT should be removed
1260 public static int test_0_abcrem_check_this_removal2 () {
1261 Bar2 b = new Bar2 ();
1269 public static int test_0_array_access_64_bit () {
1270 int[] arr2 = new int [10];
1271 for (int i = 0; i < 10; ++i)
1273 string s = "ABCDEFGH";
1275 byte[] arr = new byte [4];
1281 int len = arr [0] | (arr [1] << 8) | (arr [2] << 16) | (arr [3] << 24);
1282 int len2 = - (len + 2);
1284 // Test array and string access with a 32 bit value whose upper 32 bits are
1287 if (arr2 [len2] != 2)
1289 if (s [len2] != 'C')
1294 public static float return_float () {
1298 public static int test_0_float_return_spill () {
1299 // The return value of return_float () is spilled because of the
1301 object o = return_float ();
1302 float f = return_float ();
1303 return (float)o == f ? 0 : 1;
1307 public static float pi = 3.14f;
1309 public float float_field;
1312 public static int test_0_ldsfld_soft_float () {
1313 if (R4Holder.pi == 3.14f)
1319 public static int test_0_ldfld_stfld_soft_float () {
1320 R4Holder h = new R4Holder ();
1321 h.float_field = 3.14f;
1323 if (h.float_field == 3.14f)
1329 class R4HolderRemote : MarshalByRefObject {
1330 public static float pi = 3.14f;
1332 public float float_field;
1335 public static int test_0_ldfld_stfld_soft_float_remote () {
1336 R4HolderRemote h = new R4HolderRemote ();
1337 h.float_field = 3.14f;
1339 if (h.float_field == 3.14f)
1345 public static int test_0_locals_soft_float () {
1361 static float pass_vtype_return_float (AStruct2 s) {
1362 return s.i + s.j == 6 ? 1.0f : -1.0f;
1365 public static int test_0_vtype_arg_soft_float () {
1366 return pass_vtype_return_float (new AStruct2 () { i = 2, j = 4 }) > 0.0 ? 0 : 1;
1369 static int range_check_strlen (int i, string s) {
1370 if (i < 0 || i > s.Length)
1376 public static int test_0_range_check_opt () {
1377 if (range_check_strlen (0, "A") != 0)
1379 if (range_check_strlen (1, "A") != 0)
1381 if (range_check_strlen (2, "A") != 1)
1383 if (range_check_strlen (-100, "A") != 1)
1388 static int test_0_array_get_set_soft_float () {
1389 float[,] arr = new float [2, 2];
1391 return arr [0, 0] == 256f ? 0 : 1;
1395 struct Bug506915 { public int val; }
1396 static int test_2_ldobj_stobj_optization ()
1399 var a = new Bug506915 ();
1400 var b = new Bug506915 ();
1401 if (i.GetHashCode () == 99)
1403 var array = new Bug506915 [2];
1405 array [1] = (i == 0) ? a : array [0];
1407 return array [1].val;
1410 /* mcs can't compile this (#646744) */
1412 static void InitMe (out Gamma noMercyWithTheStack) {
1413 noMercyWithTheStack = new Gamma ();
1416 static int FunNoInline () {
1418 if (x > 344 && x < 22)
1423 static float DoNothingButDontInline (float a, int b) {
1426 else if (b < 0 && b > 10)
1432 * The local register allocator emits loadr8_membase and storer8_membase
1433 * to do spilling. This code is generated after mono_arch_lowering_pass so
1434 * mono_arch_output_basic_block must know how to deal with big offsets.
1435 * This only happens because the call in middle forces the temp for "(float)obj"
1438 public static int test_0_float_load_and_store_with_big_offset ()
1441 Gamma noMercyWithTheStack;
1444 InitMe (out noMercyWithTheStack);
1446 res = DoNothingButDontInline ((float)obj, FunNoInline ());
1458 static int vtype_phi (VTypePhi v1, VTypePhi v2, bool first) {
1459 VTypePhi v = first ? v1 : v2;
1464 static int test_0_vtype_phi ()
1466 VTypePhi v1 = new VTypePhi () { i = 1 };
1467 VTypePhi v2 = new VTypePhi () { i = 2 };
1469 if (vtype_phi (v1, v2, true) != 1)
1471 if (vtype_phi (v1, v2, false) != 2)
1477 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1478 static void UseValue (int index)
1482 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1483 static bool IsFalse ()
1488 static int test_0_llvm_moving_faulting_loads ()
1490 int[] indexes = null;
1493 indexes = new int[0];
1496 while (IsFalse ()) {
1497 UseValue (indexes[0]);
1498 UseValue (indexes[0]);
1504 public static bool flag;
1508 internal static B[] d;
1515 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1516 static int regress_679467_inner () {
1525 * FIXME: This fails with AOT #703317.
1528 static int test_0_multiple_cctor_calls_regress_679467 () {
1530 return regress_679467_inner ();
1534 static int test_0_char_ctor () {
1535 string s = new String (new char[] { 'A', 'B' }, 0, 1);
1539 static object mInstance = null;
1541 [MethodImpl(MethodImplOptions.Synchronized)]
1542 public static object getInstance() {
1543 if (mInstance == null)
1544 mInstance = new object();
1548 static int test_0_synchronized () {
1558 public static BStruct foo () {
1559 return new BStruct () { t = typeof (T) };
1563 delegate BStruct ADelegate ();
1565 static int test_0_regress_10601 () {
1566 var act = (ADelegate)(Del<string>.foo);
1568 if (b.t != typeof (string))
1573 static int test_0_regress_11058 () {
1574 int foo = -252674008;
1575 int foo2 = (int)(foo ^ 0xF0F0F0F0); // = 28888
1576 var arr = new byte[foo2].Length;
1580 public static void do_throw () {
1581 throw new Exception ();
1584 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1585 static void empty () {
1589 public static int test_0_llvm_inline_throw () {
1592 } catch (Exception ex) {