4 * Application program used by the debugger tests.
7 using System.Runtime.CompilerServices;
8 using System.Reflection;
9 using System.Reflection.Emit;
10 using System.Diagnostics;
11 using System.Threading;
12 using System.Collections.Generic;
15 public class TestsBase
17 #pragma warning disable 0414
18 #pragma warning disable 0169
19 public int base_field_i;
20 public string base_field_s;
21 static int base_static_i = 57;
22 static string base_static_s = "C";
23 #pragma warning restore 0414
24 #pragma warning restore 0169
32 public sealed class Tests3 {
33 public static void M1 () {
47 [DebuggerDisplay ("Tests", Name="FOO", Target=typeof (int))]
48 [DebuggerTypeProxy (typeof (Tests))]
50 [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
52 public static int static_field_j;
54 [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
55 public int AProperty {
61 public void invoke () {
65 public struct AStruct {
71 [MethodImplAttribute (MethodImplOptions.NoInlining)]
72 public int foo (int val) {
76 [MethodImplAttribute (MethodImplOptions.NoInlining)]
77 public static int static_foo (int val) {
81 [MethodImplAttribute (MethodImplOptions.NoInlining)]
82 public int invoke_return_int () {
86 [MethodImplAttribute (MethodImplOptions.NoInlining)]
87 public static int invoke_static () {
91 [MethodImplAttribute (MethodImplOptions.NoInlining)]
92 public IntPtr invoke_return_intptr () {
97 public class GClass<T> {
99 public static T static_field;
101 [MethodImplAttribute (MethodImplOptions.NoInlining)]
105 [MethodImplAttribute (MethodImplOptions.NoInlining)]
106 public void bp<T2> () {
110 public struct GStruct<T> {
115 [MethodImplAttribute (MethodImplOptions.NoInlining)]
116 public int invoke_return_int () {
133 class TestIfaces : ITest
141 TestIfaces<int> Baz () {
146 class TestIfaces<T> : ITest<T>
148 void ITest<T>.Foo () {
151 void ITest<T>.Bar () {
155 public class Tests : TestsBase
157 #pragma warning disable 0414
161 bool field_bool1, field_bool2;
174 static int static_i = 55;
175 static string static_s = "A";
176 public const int literal_i = 56;
177 public const string literal_s = "B";
179 public AStruct field_struct;
180 public object field_boxed_struct;
181 public GStruct<int> generic_field_struct;
183 public static int tls_i;
184 public static bool is_attached = Debugger.IsAttached;
186 #pragma warning restore 0414
188 public class NestedClass {
191 public int IntProperty {
200 public int ReadOnlyProperty {
206 public int this [int index] {
212 public static int Main (String[] args) {
215 if (args.Length > 0 && args [0] == "suspend-test")
216 /* This contains an infinite loop, so execute it conditionally */
218 if (args.Length >0 && args [0] == "unhandled-exception") {
219 unhandled_exception ();
240 if (args.Length > 0 && args [0] == "domain-test")
241 /* This takes a lot of time, so execute it conditionally */
243 if (args.Length > 0 && args [0] == "ref-emit-test")
245 if (args.Length > 0 && args [0] == "frames-in-native")
247 if (args.Length >0 && args [0] == "invoke-single-threaded")
248 new Tests ().invoke_single_threaded ();
252 public static void breakpoints () {
253 /* Call these early so it is JITted by the time a breakpoint is placed on it */
265 bp6<string> (new GClass <int> ());
270 [MethodImplAttribute (MethodImplOptions.NoInlining)]
271 public static void bp1 () {
274 [MethodImplAttribute (MethodImplOptions.NoInlining)]
275 public static void bp2 () {
278 [MethodImplAttribute (MethodImplOptions.NoInlining)]
279 public static void bp3 () {
282 [MethodImplAttribute (MethodImplOptions.NoInlining)]
283 public static void bp4 () {
286 [MethodImplAttribute (MethodImplOptions.NoInlining)]
287 public static void bp5 () {
290 [MethodImplAttribute (MethodImplOptions.NoInlining)]
291 public static void bp6<T> (GClass<int> gc) {
295 [MethodImplAttribute (MethodImplOptions.NoInlining)]
296 public static void bp7<T> () {
299 [MethodImplAttribute (MethodImplOptions.NoInlining)]
300 public static void single_stepping () {
307 ss5 (new int [] { 1, 2, 3 }, new Func<int, bool> (is_even));
313 ss_regress_654694 ();
316 [MethodImplAttribute (MethodImplOptions.NoInlining)]
317 public static void ss1 () {
320 [MethodImplAttribute (MethodImplOptions.NoInlining)]
321 public static void ss2 () {
324 [MethodImplAttribute (MethodImplOptions.NoInlining)]
325 public static int ss3 () {
328 for (int i = 0; i < 10; ++i)
334 [MethodImplAttribute (MethodImplOptions.NoInlining)]
335 public static void ss3_2 () {
339 [MethodImplAttribute (MethodImplOptions.NoInlining)]
340 public static void ss3_2_2 () {
343 [MethodImplAttribute (MethodImplOptions.NoInlining)]
344 public static int ss4 () {
350 [MethodImplAttribute (MethodImplOptions.NoInlining)]
351 public static void ss5 (int[] arr, Func<int, bool> selector) {
352 // Call into linq which calls back into this assembly
353 arr.Count (selector);
356 [MethodImplAttribute (MethodImplOptions.NoInlining)]
357 public static void ss6 (bool b) {
360 throw new Exception ();
364 [MethodImplAttribute (MethodImplOptions.NoInlining)]
365 public static void ss6_2 () {
368 [MethodImplAttribute (MethodImplOptions.NoInlining)]
369 public static void ss7 () {
378 [MethodImplAttribute (MethodImplOptions.NoInlining)]
379 public static void ss7_2 () {
382 [MethodImplAttribute (MethodImplOptions.NoInlining)]
383 public static void ss7_3 () {
384 throw new Exception ();
387 [MethodImplAttribute (MethodImplOptions.NoInlining)]
388 public static bool is_even (int i) {
394 Console.WriteLine ("HIT!");
400 [MethodImplAttribute (MethodImplOptions.NoInlining)]
401 public static void arguments () {
402 arg1 (SByte.MaxValue - 5, Byte.MaxValue - 5, true, Int16.MaxValue - 5, UInt16.MaxValue - 5, 'F', Int32.MaxValue - 5, UInt32.MaxValue - 5, Int64.MaxValue - 5, UInt64.MaxValue - 5, 1.2345f, 6.78910, new IntPtr (Int32.MaxValue - 5), new UIntPtr (UInt32.MaxValue - 5));
404 arg2 ("FOO", null, "BLA", ref i, new GClass <int> { field = 42 }, new object ());
405 Tests t = new Tests () { field_i = 42, field_s = "S" };
409 [MethodImplAttribute (MethodImplOptions.NoInlining)]
410 public static int arg1 (sbyte sb, byte b, bool bl, short s, ushort us, char c, int i, uint ui, long l, ulong ul, float f, double d, IntPtr ip, UIntPtr uip) {
411 return (int)(sb + b + (bl ? 0 : 1) + s + us + (int)c + i + ui + l + (long)ul + f + d + (int)ip + (int)uip);
414 [MethodImplAttribute (MethodImplOptions.NoInlining)]
415 public static string arg2 (string s, string s3, object o, ref int i, GClass <int> gc, object o2) {
416 return s + (s3 != null ? "" : "") + o + i + gc.field + o2;
419 [MethodImplAttribute (MethodImplOptions.NoInlining)]
420 public object arg3 (string s) {
421 return s + s + s + s + this;
424 [MethodImplAttribute (MethodImplOptions.NoInlining)]
425 public static void objects () {
426 Tests t = new Tests () { field_i = 42, field_bool1 = true, field_bool2 = false, field_char = 'A', field_byte = 129, field_sbyte = -33, field_short = Int16.MaxValue - 5, field_ushort = UInt16.MaxValue - 5, field_long = Int64.MaxValue - 5, field_ulong = UInt64.MaxValue - 5, field_float = 3.14f, field_double = 3.14f, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B, field_class = null, field_intptr = new IntPtr (Int32.MaxValue - 5) };
427 t.o1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
428 o2 (new string [] { "BAR", "BAZ" }, new int[] { 42, 43 }, new int [,] { { 1, 2 }, { 3, 4 }}, (int[,])Array.CreateInstance (typeof (int), new int [] { 2, 2}, new int [] { 1, 3}), new int[] { 0 });
431 [MethodImplAttribute (MethodImplOptions.NoInlining)]
432 public object o1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
433 if (t == null || gc1 == null || gc2 == null)
439 [MethodImplAttribute (MethodImplOptions.NoInlining)]
440 public static string o2 (string[] s2, int[] s3, int[,] s4, int[,] s5, IList<int> s6) {
441 return s2 [0] + s3 [0] + s4 [0, 0] + s6 [0];
444 [MethodImplAttribute (MethodImplOptions.NoInlining)]
445 public static void objrefs () {
446 Tests t = new Tests () {};
454 [MethodImplAttribute (MethodImplOptions.NoInlining)]
455 public static void set_child (Tests t) {
456 t.child = new Tests ();
459 [MethodImplAttribute (MethodImplOptions.NoInlining)]
460 public void objrefs1 () {
463 [MethodImplAttribute (MethodImplOptions.NoInlining)]
464 public static void objrefs2 () {
467 public static void vtypes () {
468 Tests t = new Tests () { field_struct = new AStruct () { i = 42, s = "S", k = 43 }, generic_field_struct = new GStruct<int> () { i = 42 }, field_boxed_struct = new AStruct () { i = 42 }};
469 AStruct s = new AStruct { i = 44, s = "T", k = 45 };
470 AStruct[] arr = new AStruct[] {
471 new AStruct () { i = 1, s = "S1" },
472 new AStruct () { i = 2, s = "S2" } };
478 [MethodImplAttribute (MethodImplOptions.NoInlining)]
479 public object vtypes1 (AStruct s, AStruct[] arr) {
486 [MethodImplAttribute (MethodImplOptions.NoInlining)]
487 public static void vtypes2 (AStruct s) {
491 [MethodImplAttribute (MethodImplOptions.NoInlining)]
492 public static void vtypes3 (AStruct s) {
493 AStruct.static_foo (5);
496 [MethodImplAttribute (MethodImplOptions.NoInlining)]
497 public static void locals () {
500 locals2<string> (null, 5, "ABC", ref s);
505 [MethodImplAttribute (MethodImplOptions.NoInlining)]
506 public static void locals1 (string[] args) {
509 for (int j = 0; j < 10; ++j) {
514 [MethodImplAttribute (MethodImplOptions.NoInlining)]
515 public static void locals2<T> (string[] args, int arg, T t, ref string rs) {
519 for (int j = 0; j < 10; ++j) {
528 [MethodImplAttribute (MethodImplOptions.NoInlining)]
529 public static void locals3 () {
549 [MethodImplAttribute (MethodImplOptions.NoInlining)]
550 public static void locals4 () {
553 [MethodImplAttribute (MethodImplOptions.NoInlining)]
554 public static void locals5 () {
557 [MethodImplAttribute (MethodImplOptions.NoInlining)]
558 public static void locals6 () {
561 for (i = 0; i < 10; ++i)
564 for (i = 0; i < 10; ++i)
574 [MethodImplAttribute (MethodImplOptions.NoInlining)]
575 public static void locals6_1 () {
578 [MethodImplAttribute (MethodImplOptions.NoInlining)]
579 public static void locals6_2 (int arg) {
582 [MethodImplAttribute (MethodImplOptions.NoInlining)]
583 public static void locals6_3 () {
584 // Clobber all registers
585 int sum = 0, i, j, k, l, m;
586 for (i = 0; i < 100; ++i)
588 for (j = 0; j < 100; ++j)
590 for (k = 0; k < 100; ++k)
592 for (l = 0; l < 100; ++l)
594 for (m = 0; m < 100; ++m)
598 [MethodImplAttribute (MethodImplOptions.NoInlining)]
599 public static void locals6_4 (int arg) {
602 [MethodImplAttribute (MethodImplOptions.NoInlining)]
603 public static void locals6_5 () {
606 [MethodImplAttribute (MethodImplOptions.NoInlining)]
607 public static void locals6_6 (int arg) {
610 [MethodImplAttribute (MethodImplOptions.NoInlining)]
611 public static void line_numbers () {
615 [MethodImplAttribute (MethodImplOptions.NoInlining)]
616 public static void suspend () {
624 [MethodImplAttribute (MethodImplOptions.NoInlining)]
625 public static void type_info () {
626 Tests t = new Tests () { field_i = 42, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B };
627 t.ti1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
630 AStruct s = new AStruct () { i = 42, s = "S", k = 43 };
632 ti2 (new string [] { "BAR", "BAZ" }, new int[] { 42, 43 }, new int [,] { { 1, 2 }, { 3, 4 }}, ref val, (int*)IntPtr.Zero, 5, s, new Tests (), new Tests2 (), new GClass <int> (), AnEnum.B);
636 [MethodImplAttribute (MethodImplOptions.NoInlining)]
637 public object ti1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
638 if (t == null || gc1 == null || gc2 == null)
644 [MethodImplAttribute (MethodImplOptions.NoInlining)]
645 public static unsafe string ti2 (string[] s2, int[] s3, int[,] s4, ref int ri, int* ptr, int i, AStruct s, Tests t, Tests2 t2, GClass<int> g, AnEnum ae) {
646 return s2 [0] + s3 [0] + s4 [0, 0];
649 [MethodImplAttribute (MethodImplOptions.NoInlining)]
650 public static void assembly_load () {
654 [MethodImplAttribute (MethodImplOptions.NoInlining)]
655 public static void assembly_load_2 () {
656 // This will load System.dll while holding the loader lock
660 [MethodImplAttribute (MethodImplOptions.NoInlining)]
661 public static void invoke () {
662 new Tests ().invoke1 (new Tests2 (), new AStruct () { i = 42, j = (IntPtr)43 }, new GStruct<int> { j = 42 });
663 new Tests ().invoke_ex ();
666 [MethodImplAttribute (MethodImplOptions.NoInlining)]
667 public void invoke1 (Tests2 t, AStruct s, GStruct<int> g) {
671 [MethodImplAttribute (MethodImplOptions.NoInlining)]
672 public void invoke2 () {
675 [MethodImplAttribute (MethodImplOptions.NoInlining)]
676 public void invoke_ex () {
680 [MethodImplAttribute (MethodImplOptions.NoInlining)]
681 public void invoke_ex_inner () {
683 throw new Exception ();
690 [MethodImplAttribute (MethodImplOptions.NoInlining)]
691 public void invoke_single_threaded () {
692 // Spawn a thread incrementing a counter
693 bool finished = false;
695 new Thread (delegate () {
702 invoke_single_threaded_2 ();
707 [MethodImplAttribute (MethodImplOptions.NoInlining)]
708 public void invoke_single_threaded_2 () {
711 public void invoke_return_void () {
714 public string invoke_return_ref () {
718 public object invoke_return_null () {
722 public int invoke_return_primitive () {
726 public int? invoke_return_nullable () {
730 public int? invoke_return_nullable_null () {
734 public void invoke_type_load () {
741 public long invoke_pass_primitive (byte ub, sbyte sb, short ss, ushort us, int i, uint ui, long l, ulong ul, char c, bool b, float f, double d) {
742 return ub + sb + ss + us + i + ui + l + (long)ul + (int)c + (b ? 1 : 0) + (int)f + (int)d;
745 public int invoke_pass_primitive2 (bool b) {
749 public string invoke_pass_ref (string s) {
753 public static string invoke_static_pass_ref (string s) {
757 public static void invoke_static_return_void () {
760 public static void invoke_throws () {
761 throw new Exception ();
764 [MethodImplAttribute (MethodImplOptions.NoInlining)]
765 public static void exceptions () {
767 throw new OverflowException ();
768 } catch (Exception) {
771 throw new OverflowException ();
772 } catch (Exception) {
775 throw new ArgumentException ();
776 } catch (Exception) {
779 throw new OverflowException ();
780 } catch (Exception) {
786 } catch (Exception) {
791 } catch (Exception) {
795 [MethodImplAttribute (MethodImplOptions.NoInlining)]
796 public static void unhandled_exception () {
797 ThreadPool.QueueUserWorkItem (delegate {
798 throw new InvalidOperationException ();
800 Thread.Sleep (10000);
803 internal static Delegate create_filter_delegate (Delegate dlg, MethodInfo filter_method)
806 throw new ArgumentNullException ();
807 if (dlg.Target != null)
808 throw new ArgumentException ();
809 if (dlg.Method == null)
810 throw new ArgumentException ();
812 var ret_type = dlg.Method.ReturnType;
813 var param_types = dlg.Method.GetParameters ().Select (x => x.ParameterType).ToArray ();
815 var dynamic = new DynamicMethod (Guid.NewGuid ().ToString (), ret_type, param_types, typeof (object), true);
816 var ig = dynamic.GetILGenerator ();
818 LocalBuilder retval = null;
819 if (ret_type != typeof (void))
820 retval = ig.DeclareLocal (ret_type);
822 var label = ig.BeginExceptionBlock ();
824 for (int i = 0; i < param_types.Length; i++)
825 ig.Emit (OpCodes.Ldarg, i);
826 ig.Emit (OpCodes.Call, dlg.Method);
829 ig.Emit (OpCodes.Stloc, retval);
831 ig.Emit (OpCodes.Leave, label);
833 ig.BeginExceptFilterBlock ();
835 ig.Emit (OpCodes.Call, filter_method);
837 ig.BeginCatchBlock (null);
839 ig.Emit (OpCodes.Pop);
841 ig.EndExceptionBlock ();
844 ig.Emit (OpCodes.Ldloc, retval);
846 ig.Emit (OpCodes.Ret);
848 return dynamic.CreateDelegate (dlg.GetType ());
851 [MethodImplAttribute (MethodImplOptions.NoInlining)]
852 static void exception_filter_method () {
853 throw new InvalidOperationException ();
856 [MethodImplAttribute (MethodImplOptions.NoInlining)]
857 static int exception_filter_filter (Exception exc) {
861 [MethodImplAttribute (MethodImplOptions.NoInlining)]
862 public static void exception_filter () {
863 var method = typeof (Tests).GetMethod (
864 "exception_filter_method", BindingFlags.NonPublic | BindingFlags.Static);
865 var filter_method = typeof (Tests).GetMethod (
866 "exception_filter_filter", BindingFlags.NonPublic | BindingFlags.Static);
868 var dlg = Delegate.CreateDelegate (typeof (Action), method);
870 var wrapper = (Action) create_filter_delegate (dlg, filter_method);
875 [MethodImplAttribute (MethodImplOptions.NoInlining)]
876 public static bool return_true () {
880 [MethodImplAttribute (MethodImplOptions.NoInlining)]
881 public static void exceptions2 () {
883 throw new Exception ();
884 Console.WriteLine ();
887 [MethodImplAttribute (MethodImplOptions.NoInlining)]
888 public static void threads () {
889 Thread t = new Thread (delegate () {});
895 [MethodImplAttribute (MethodImplOptions.NoInlining)]
896 public static void domains () {
897 AppDomain domain = AppDomain.CreateDomain ("domain");
899 CrossDomain o = (CrossDomain)domain.CreateInstanceAndUnwrap (
900 typeof (CrossDomain).Assembly.FullName, "CrossDomain");
902 domains_2 (o, new CrossDomain ());
910 AppDomain.Unload (domain);
915 [MethodImplAttribute (MethodImplOptions.NoInlining)]
916 public static void domains_2 (object o, object o2) {
919 [MethodImplAttribute (MethodImplOptions.NoInlining)]
920 public static void domains_3 () {
923 [MethodImplAttribute (MethodImplOptions.NoInlining)]
924 public static void invoke_in_domain () {
927 [MethodImplAttribute (MethodImplOptions.NoInlining)]
928 public static void invoke_in_domain_2 () {
931 [MethodImplAttribute (MethodImplOptions.NoInlining)]
932 public static void dynamic_methods () {
933 var m = new DynamicMethod ("dyn_method", typeof (void), new Type [] { typeof (int) }, typeof (object).Module);
934 var ig = m.GetILGenerator ();
936 ig.Emit (OpCodes.Ldstr, "FOO");
937 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("dyn_call"));
938 ig.Emit (OpCodes.Ret);
940 var del = (Action<int>)m.CreateDelegate (typeof (Action<int>));
945 public static void dyn_call (string s) {
948 [MethodImplAttribute (MethodImplOptions.NoInlining)]
949 public static void ref_emit () {
950 AssemblyName assemblyName = new AssemblyName ();
951 assemblyName.Name = "foo";
953 AssemblyBuilder assembly =
954 Thread.GetDomain ().DefineDynamicAssembly (
955 assemblyName, AssemblyBuilderAccess.RunAndSave);
957 ModuleBuilder module = assembly.DefineDynamicModule ("foo.dll");
959 TypeBuilder tb = module.DefineType ("foo", TypeAttributes.Public, typeof (object));
960 MethodBuilder mb = tb.DefineMethod ("ref_emit_method", MethodAttributes.Public|MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type [] { });
961 ILGenerator ig = mb.GetILGenerator ();
962 ig.Emit (OpCodes.Ldstr, "FOO");
963 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("ref_emit_call"));
964 ig.Emit (OpCodes.Ret);
966 Type t = tb.CreateType ();
968 t.GetMethod ("ref_emit_method").Invoke (null, null);
971 [MethodImplAttribute (MethodImplOptions.NoInlining)]
972 public static void ref_emit_call (string s) {
975 [MethodImplAttribute (MethodImplOptions.NoInlining)]
976 public static void frames_in_native () {
980 [MethodImplAttribute (MethodImplOptions.NoInlining)]
981 public static void string_call (string s) {
984 [MethodImplAttribute (MethodImplOptions.NoInlining)]
985 public static void ss_regress_654694 () {
992 [MethodImplAttribute (MethodImplOptions.NoInlining)]
993 public static void user () {
996 Debugger.Log (5, Debugger.IsLogging () ? "A" : "", "B");
999 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1000 public static void type_load () {
1004 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1005 static void type_load_2 () {
1006 var c1 = new Dictionary<int, int> ();
1008 var c = new TypeLoadClass ();
1010 var c2 = new TypeLoadClass2 ();
1014 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1015 public static void regress () {
1016 regress_2755 (DateTime.Now);
1019 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1020 public static unsafe void regress_2755 (DateTime d) {
1021 int* buffer = stackalloc int [128];
1026 for (int i = 0; i < 128; ++i)
1029 regress_2755_3 (sum);
1032 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1033 public static void regress_2755_2 () {
1036 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1037 public static void regress_2755_3 (int sum) {
1041 class TypeLoadClass {
1044 class TypeLoadClass2 {
1047 public class CrossDomain : MarshalByRefObject
1049 public void invoke () {
1050 Tests.invoke_in_domain ();
1053 public void invoke_2 () {
1054 Tests.invoke_in_domain_2 ();
1057 public int invoke_3 () {
1064 public ProcessStartInfo info;
1067 // Class used for line number info testing, don't change its layout
1068 public class LineNumbers
1070 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1071 public static void ln1 () {
1077 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1078 public static void ln2 () {
1081 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1082 public static void ln3 () {
1083 #pragma warning disable 0219
1085 #pragma warning restore 0219