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 () {
1088 public static int arg_only_written (string file_name
, int[]
1090 if (file_name
== null)
1099 public static int[] foo () {
1103 public static void bar (int i
) {
1107 public static int test_0_arg_only_written ()
1109 return arg_only_written ("md.in", null);
1112 static long position
= 0;
1114 public static int test_4_static_inc_long () {
1122 return (int)position
;
1127 public FooStruct (long l
) {
1131 public static int test_0_calls_opcode_emulation () {
1132 // Test that emulated opcodes do not clobber arguments already in
1136 new FooStruct (val
* 10000);
1141 public static int test_0_intrins_string_length () {
1144 return (s
.Length
== 3) ? 0 : 1;
1147 public static int test_0_intrins_string_chars () {
1150 return (s
[0] == 'A' && s
[1] == 'B' && s
[2] == 'C') ? 0 : 1;
1153 public static int test_0_intrins_object_gettype () {
1156 return (o
.GetType () == typeof (int)) ? 0 : 1;
1159 public static int test_0_intrins_object_gethashcode () {
1160 object o
= new Object ();
1162 return (o
.GetHashCode () == o
.GetHashCode ()) ? 0 : 1;
1168 public static int test_0_intrins_object_ctor () {
1169 object o
= new FooClass ();
1171 return (o
!= null) ? 0 : 1;
1174 public static int test_0_intrins_array_rank () {
1175 int[,] a
= new int [10, 10];
1177 return (a
.Rank
== 2) ? 0 : 1;
1180 public static int test_0_intrins_array_length () {
1181 int[,] a
= new int [10, 10];
1184 return (a2
.Length
== 100) ? 0 : 1;
1187 public static int test_0_intrins_runtimehelpers_offset_to_string_data () {
1188 int i
= RuntimeHelpers
.OffsetToStringData
;
1193 public static int test_0_intrins_string_setchar () {
1194 StringBuilder sb
= new StringBuilder ("ABC");
1198 return sb
.ToString () == "ADC" ? 0 : 1;
1202 bool allowLocation
= true;
1206 public static int test_0_regress_78990_unaligned_structs () {
1212 public static unsafe int test_97_negative_index () {
1213 char[] arr
= new char[] {'a', 'b'}
;
1214 fixed (char *p
= arr
) {
1222 public static int test_0_unsigned_right_shift_imm0 () {
1224 byte[] data
= new byte[256];
1225 for (int i
= 0; i
< 1; i
++)
1226 temp
= (uint)(data
[temp
>> 24] | data
[temp
>> 0]);
1231 public virtual int foo () {
1236 sealed class Bar2
: Foo2
{
1237 public override int foo () {
1242 public static int test_0_abcrem_check_this_removal () {
1243 Bar2 b
= new Bar2 ();
1245 // The check_this generated here by the JIT should be removed
1251 static int invoke_twice (Bar2 b
) {
1253 // The check_this generated here by the JIT should be removed
1259 public static int test_0_abcrem_check_this_removal2 () {
1260 Bar2 b
= new Bar2 ();
1268 public static int test_0_array_access_64_bit () {
1269 int[] arr2
= new int [10];
1270 for (int i
= 0; i
< 10; ++i
)
1272 string s
= "ABCDEFGH";
1274 byte[] arr
= new byte [4];
1280 int len
= arr
[0] | (arr
[1] << 8) | (arr
[2] << 16) | (arr
[3] << 24);
1281 int len2
= - (len
+ 2);
1283 // Test array and string access with a 32 bit value whose upper 32 bits are
1286 if (arr2
[len2
] != 2)
1288 if (s
[len2
] != 'C')
1293 public static float return_float () {
1297 public static int test_0_float_return_spill () {
1298 // The return value of return_float () is spilled because of the
1300 object o
= return_float ();
1301 float f
= return_float ();
1302 return (float)o
== f
? 0 : 1;
1306 public static float pi
= 3.14f
;
1308 public float float_field
;
1311 public static int test_0_ldsfld_soft_float () {
1312 if (R4Holder
.pi
== 3.14f
)
1318 public static int test_0_ldfld_stfld_soft_float () {
1319 R4Holder h
= new R4Holder ();
1320 h
.float_field
= 3.14f
;
1322 if (h
.float_field
== 3.14f
)
1328 class R4HolderRemote
: MarshalByRefObject
{
1329 public static float pi
= 3.14f
;
1331 public float float_field
;
1334 public static int test_0_ldfld_stfld_soft_float_remote () {
1335 R4HolderRemote h
= new R4HolderRemote ();
1336 h
.float_field
= 3.14f
;
1338 if (h
.float_field
== 3.14f
)
1344 public static int test_0_locals_soft_float () {
1360 static float pass_vtype_return_float (AStruct2 s
) {
1361 return s
.i
+ s
.j
== 6 ? 1.0f
: -1.0f
;
1364 public static int test_0_vtype_arg_soft_float () {
1365 return pass_vtype_return_float (new AStruct2 () { i = 2, j = 4 }
) > 0.0 ? 0 : 1;
1368 static int range_check_strlen (int i
, string s
) {
1369 if (i
< 0 || i
> s
.Length
)
1375 public static int test_0_range_check_opt () {
1376 if (range_check_strlen (0, "A") != 0)
1378 if (range_check_strlen (1, "A") != 0)
1380 if (range_check_strlen (2, "A") != 1)
1382 if (range_check_strlen (-100, "A") != 1)
1387 static int test_0_array_get_set_soft_float () {
1388 float[,] arr
= new float [2, 2];
1390 return arr
[0, 0] == 256f
? 0 : 1;
1394 struct Bug506915 { public int val; }
1395 static int test_2_ldobj_stobj_optization ()
1398 var a
= new Bug506915 ();
1399 var b
= new Bug506915 ();
1400 if (i
.GetHashCode () == 99)
1402 var array
= new Bug506915
[2];
1404 array
[1] = (i
== 0) ? a
: array
[0];
1406 return array
[1].val
;
1409 /* mcs can't compile this (#646744) */
1411 static void InitMe (out Gamma noMercyWithTheStack
) {
1412 noMercyWithTheStack
= new Gamma ();
1415 static int FunNoInline () {
1417 if (x
> 344 && x
< 22)
1422 static float DoNothingButDontInline (float a
, int b
) {
1425 else if (b
< 0 && b
> 10)
1431 * The local register allocator emits loadr8_membase and storer8_membase
1432 * to do spilling. This code is generated after mono_arch_lowering_pass so
1433 * mono_arch_output_basic_block must know how to deal with big offsets.
1434 * This only happens because the call in middle forces the temp for "(float)obj"
1437 public static int test_0_float_load_and_store_with_big_offset ()
1440 Gamma noMercyWithTheStack
;
1443 InitMe (out noMercyWithTheStack
);
1445 res
= DoNothingButDontInline ((float)obj
, FunNoInline ());
1457 static int vtype_phi (VTypePhi v1
, VTypePhi v2
, bool first
) {
1458 VTypePhi v
= first
? v1
: v2
;
1463 static int test_0_vtype_phi ()
1465 VTypePhi v1
= new VTypePhi () { i = 1 }
;
1466 VTypePhi v2
= new VTypePhi () { i = 2 }
;
1468 if (vtype_phi (v1
, v2
, true) != 1)
1470 if (vtype_phi (v1
, v2
, false) != 2)
1476 [MethodImplAttribute (MethodImplOptions
.NoInlining
)]
1477 static void UseValue (int index
)
1481 [MethodImplAttribute (MethodImplOptions
.NoInlining
)]
1482 static bool IsFalse ()
1487 static int test_0_llvm_moving_faulting_loads ()
1489 int[] indexes
= null;
1492 indexes
= new int[0];
1495 while (IsFalse ()) {
1496 UseValue (indexes
[0]);
1497 UseValue (indexes
[0]);
1503 public static bool flag
;
1507 internal static B
[] d
;
1514 [MethodImplAttribute (MethodImplOptions
.NoInlining
)]
1515 static int regress_679467_inner () {
1524 * FIXME: This fails with AOT #703317.
1527 static int test_0_multiple_cctor_calls_regress_679467 () {
1529 return regress_679467_inner ();