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 [StateMachine (typeof (int))]
516 public static void locals2<T> (string[] args, int arg, T t, ref string rs) {
520 for (int j = 0; j < 10; ++j) {
529 [MethodImplAttribute (MethodImplOptions.NoInlining)]
530 public static void locals3 () {
550 [MethodImplAttribute (MethodImplOptions.NoInlining)]
551 public static void locals4 () {
554 [MethodImplAttribute (MethodImplOptions.NoInlining)]
555 public static void locals5 () {
558 [MethodImplAttribute (MethodImplOptions.NoInlining)]
559 public static void locals6 () {
562 for (i = 0; i < 10; ++i)
565 for (i = 0; i < 10; ++i)
575 [MethodImplAttribute (MethodImplOptions.NoInlining)]
576 public static void locals6_1 () {
579 [MethodImplAttribute (MethodImplOptions.NoInlining)]
580 public static void locals6_2 (int arg) {
583 [MethodImplAttribute (MethodImplOptions.NoInlining)]
584 public static void locals6_3 () {
585 // Clobber all registers
586 int sum = 0, i, j, k, l, m;
587 for (i = 0; i < 100; ++i)
589 for (j = 0; j < 100; ++j)
591 for (k = 0; k < 100; ++k)
593 for (l = 0; l < 100; ++l)
595 for (m = 0; m < 100; ++m)
599 [MethodImplAttribute (MethodImplOptions.NoInlining)]
600 public static void locals6_4 (int arg) {
603 [MethodImplAttribute (MethodImplOptions.NoInlining)]
604 public static void locals6_5 () {
607 [MethodImplAttribute (MethodImplOptions.NoInlining)]
608 public static void locals6_6 (int arg) {
611 [MethodImplAttribute (MethodImplOptions.NoInlining)]
612 public static void line_numbers () {
616 [MethodImplAttribute (MethodImplOptions.NoInlining)]
617 public static void suspend () {
625 [MethodImplAttribute (MethodImplOptions.NoInlining)]
626 public static void type_info () {
627 Tests t = new Tests () { field_i = 42, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B };
628 t.ti1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
631 AStruct s = new AStruct () { i = 42, s = "S", k = 43 };
633 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);
637 [MethodImplAttribute (MethodImplOptions.NoInlining)]
638 public object ti1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
639 if (t == null || gc1 == null || gc2 == null)
645 [MethodImplAttribute (MethodImplOptions.NoInlining)]
646 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) {
647 return s2 [0] + s3 [0] + s4 [0, 0];
650 [MethodImplAttribute (MethodImplOptions.NoInlining)]
651 public static void assembly_load () {
655 [MethodImplAttribute (MethodImplOptions.NoInlining)]
656 public static void assembly_load_2 () {
657 // This will load System.dll while holding the loader lock
661 [MethodImplAttribute (MethodImplOptions.NoInlining)]
662 public static void invoke () {
663 new Tests ().invoke1 (new Tests2 (), new AStruct () { i = 42, j = (IntPtr)43 }, new GStruct<int> { j = 42 });
664 new Tests ().invoke_ex ();
667 [MethodImplAttribute (MethodImplOptions.NoInlining)]
668 public void invoke1 (Tests2 t, AStruct s, GStruct<int> g) {
672 [MethodImplAttribute (MethodImplOptions.NoInlining)]
673 public void invoke2 () {
676 [MethodImplAttribute (MethodImplOptions.NoInlining)]
677 public void invoke_ex () {
681 [MethodImplAttribute (MethodImplOptions.NoInlining)]
682 public void invoke_ex_inner () {
684 throw new Exception ();
691 [MethodImplAttribute (MethodImplOptions.NoInlining)]
692 public void invoke_single_threaded () {
693 // Spawn a thread incrementing a counter
694 bool finished = false;
696 new Thread (delegate () {
703 invoke_single_threaded_2 ();
708 [MethodImplAttribute (MethodImplOptions.NoInlining)]
709 public void invoke_single_threaded_2 () {
712 public void invoke_return_void () {
715 public string invoke_return_ref () {
719 public object invoke_return_null () {
723 public int invoke_return_primitive () {
727 public int? invoke_return_nullable () {
731 public int? invoke_return_nullable_null () {
735 public void invoke_type_load () {
742 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) {
743 return ub + sb + ss + us + i + ui + l + (long)ul + (int)c + (b ? 1 : 0) + (int)f + (int)d;
746 public int invoke_pass_primitive2 (bool b) {
750 public string invoke_pass_ref (string s) {
754 public static string invoke_static_pass_ref (string s) {
758 public static void invoke_static_return_void () {
761 public static void invoke_throws () {
762 throw new Exception ();
765 [MethodImplAttribute (MethodImplOptions.NoInlining)]
766 public static void exceptions () {
768 throw new OverflowException ();
769 } catch (Exception) {
772 throw new OverflowException ();
773 } catch (Exception) {
776 throw new ArgumentException ();
777 } catch (Exception) {
780 throw new OverflowException ();
781 } catch (Exception) {
787 } catch (Exception) {
792 } catch (Exception) {
796 [MethodImplAttribute (MethodImplOptions.NoInlining)]
797 public static void unhandled_exception () {
798 ThreadPool.QueueUserWorkItem (delegate {
799 throw new InvalidOperationException ();
801 Thread.Sleep (10000);
804 internal static Delegate create_filter_delegate (Delegate dlg, MethodInfo filter_method)
807 throw new ArgumentNullException ();
808 if (dlg.Target != null)
809 throw new ArgumentException ();
810 if (dlg.Method == null)
811 throw new ArgumentException ();
813 var ret_type = dlg.Method.ReturnType;
814 var param_types = dlg.Method.GetParameters ().Select (x => x.ParameterType).ToArray ();
816 var dynamic = new DynamicMethod (Guid.NewGuid ().ToString (), ret_type, param_types, typeof (object), true);
817 var ig = dynamic.GetILGenerator ();
819 LocalBuilder retval = null;
820 if (ret_type != typeof (void))
821 retval = ig.DeclareLocal (ret_type);
823 var label = ig.BeginExceptionBlock ();
825 for (int i = 0; i < param_types.Length; i++)
826 ig.Emit (OpCodes.Ldarg, i);
827 ig.Emit (OpCodes.Call, dlg.Method);
830 ig.Emit (OpCodes.Stloc, retval);
832 ig.Emit (OpCodes.Leave, label);
834 ig.BeginExceptFilterBlock ();
836 ig.Emit (OpCodes.Call, filter_method);
838 ig.BeginCatchBlock (null);
840 ig.Emit (OpCodes.Pop);
842 ig.EndExceptionBlock ();
845 ig.Emit (OpCodes.Ldloc, retval);
847 ig.Emit (OpCodes.Ret);
849 return dynamic.CreateDelegate (dlg.GetType ());
852 [MethodImplAttribute (MethodImplOptions.NoInlining)]
853 static void exception_filter_method () {
854 throw new InvalidOperationException ();
857 [MethodImplAttribute (MethodImplOptions.NoInlining)]
858 static int exception_filter_filter (Exception exc) {
862 [MethodImplAttribute (MethodImplOptions.NoInlining)]
863 public static void exception_filter () {
864 var method = typeof (Tests).GetMethod (
865 "exception_filter_method", BindingFlags.NonPublic | BindingFlags.Static);
866 var filter_method = typeof (Tests).GetMethod (
867 "exception_filter_filter", BindingFlags.NonPublic | BindingFlags.Static);
869 var dlg = Delegate.CreateDelegate (typeof (Action), method);
871 var wrapper = (Action) create_filter_delegate (dlg, filter_method);
876 [MethodImplAttribute (MethodImplOptions.NoInlining)]
877 public static bool return_true () {
881 [MethodImplAttribute (MethodImplOptions.NoInlining)]
882 public static void exceptions2 () {
884 throw new Exception ();
885 Console.WriteLine ();
888 [MethodImplAttribute (MethodImplOptions.NoInlining)]
889 public static void threads () {
890 Thread t = new Thread (delegate () {});
896 [MethodImplAttribute (MethodImplOptions.NoInlining)]
897 public static void domains () {
898 AppDomain domain = AppDomain.CreateDomain ("domain");
900 CrossDomain o = (CrossDomain)domain.CreateInstanceAndUnwrap (
901 typeof (CrossDomain).Assembly.FullName, "CrossDomain");
903 domains_2 (o, new CrossDomain ());
911 AppDomain.Unload (domain);
916 [MethodImplAttribute (MethodImplOptions.NoInlining)]
917 public static void domains_2 (object o, object o2) {
920 [MethodImplAttribute (MethodImplOptions.NoInlining)]
921 public static void domains_3 () {
924 [MethodImplAttribute (MethodImplOptions.NoInlining)]
925 public static void invoke_in_domain () {
928 [MethodImplAttribute (MethodImplOptions.NoInlining)]
929 public static void invoke_in_domain_2 () {
932 [MethodImplAttribute (MethodImplOptions.NoInlining)]
933 public static void dynamic_methods () {
934 var m = new DynamicMethod ("dyn_method", typeof (void), new Type [] { typeof (int) }, typeof (object).Module);
935 var ig = m.GetILGenerator ();
937 ig.Emit (OpCodes.Ldstr, "FOO");
938 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("dyn_call"));
939 ig.Emit (OpCodes.Ret);
941 var del = (Action<int>)m.CreateDelegate (typeof (Action<int>));
946 public static void dyn_call (string s) {
949 [MethodImplAttribute (MethodImplOptions.NoInlining)]
950 public static void ref_emit () {
951 AssemblyName assemblyName = new AssemblyName ();
952 assemblyName.Name = "foo";
954 AssemblyBuilder assembly =
955 Thread.GetDomain ().DefineDynamicAssembly (
956 assemblyName, AssemblyBuilderAccess.RunAndSave);
958 ModuleBuilder module = assembly.DefineDynamicModule ("foo.dll");
960 TypeBuilder tb = module.DefineType ("foo", TypeAttributes.Public, typeof (object));
961 MethodBuilder mb = tb.DefineMethod ("ref_emit_method", MethodAttributes.Public|MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type [] { });
962 ILGenerator ig = mb.GetILGenerator ();
963 ig.Emit (OpCodes.Ldstr, "FOO");
964 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("ref_emit_call"));
965 ig.Emit (OpCodes.Ret);
967 Type t = tb.CreateType ();
969 t.GetMethod ("ref_emit_method").Invoke (null, null);
972 [MethodImplAttribute (MethodImplOptions.NoInlining)]
973 public static void ref_emit_call (string s) {
976 [MethodImplAttribute (MethodImplOptions.NoInlining)]
977 public static void frames_in_native () {
981 [MethodImplAttribute (MethodImplOptions.NoInlining)]
982 public static void string_call (string s) {
985 [MethodImplAttribute (MethodImplOptions.NoInlining)]
986 public static void ss_regress_654694 () {
993 [MethodImplAttribute (MethodImplOptions.NoInlining)]
994 public static void user () {
997 Debugger.Log (5, Debugger.IsLogging () ? "A" : "", "B");
1000 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1001 public static void type_load () {
1005 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1006 static void type_load_2 () {
1007 var c1 = new Dictionary<int, int> ();
1009 var c = new TypeLoadClass ();
1011 var c2 = new TypeLoadClass2 ();
1015 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1016 public static void regress () {
1017 regress_2755 (DateTime.Now);
1020 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1021 public static unsafe void regress_2755 (DateTime d) {
1022 int* buffer = stackalloc int [128];
1027 for (int i = 0; i < 128; ++i)
1030 regress_2755_3 (sum);
1033 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1034 public static void regress_2755_2 () {
1037 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1038 public static void regress_2755_3 (int sum) {
1042 class TypeLoadClass {
1045 class TypeLoadClass2 {
1048 public class CrossDomain : MarshalByRefObject
1050 public void invoke () {
1051 Tests.invoke_in_domain ();
1054 public void invoke_2 () {
1055 Tests.invoke_in_domain_2 ();
1058 public int invoke_3 () {
1065 public ProcessStartInfo info;
1068 // Class used for line number info testing, don't change its layout
1069 public class LineNumbers
1071 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1072 public static void ln1 () {
1078 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1079 public static void ln2 () {
1082 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1083 public static void ln3 () {
1084 #pragma warning disable 0219
1086 #pragma warning restore 0219