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 */
236 if (args.Length > 0 && args [0] == "domain-test")
237 /* This takes a lot of time, so execute it conditionally */
239 if (args.Length > 0 && args [0] == "ref-emit-test")
241 if (args.Length > 0 && args [0] == "frames-in-native")
243 if (args.Length >0 && args [0] == "invoke-single-threaded")
244 new Tests ().invoke_single_threaded ();
248 public static void breakpoints () {
249 /* Call these early so it is JITted by the time a breakpoint is placed on it */
261 bp6<string> (new GClass <int> ());
266 [MethodImplAttribute (MethodImplOptions.NoInlining)]
267 public static void bp1 () {
270 [MethodImplAttribute (MethodImplOptions.NoInlining)]
271 public static void bp2 () {
274 [MethodImplAttribute (MethodImplOptions.NoInlining)]
275 public static void bp3 () {
278 [MethodImplAttribute (MethodImplOptions.NoInlining)]
279 public static void bp4 () {
282 [MethodImplAttribute (MethodImplOptions.NoInlining)]
283 public static void bp5 () {
286 [MethodImplAttribute (MethodImplOptions.NoInlining)]
287 public static void bp6<T> (GClass<int> gc) {
291 [MethodImplAttribute (MethodImplOptions.NoInlining)]
292 public static void bp7<T> () {
295 [MethodImplAttribute (MethodImplOptions.NoInlining)]
296 public static void single_stepping () {
303 ss5 (new int [] { 1, 2, 3 }, new Func<int, bool> (is_even));
308 ss_regress_654694 ();
311 [MethodImplAttribute (MethodImplOptions.NoInlining)]
312 public static void ss1 () {
315 [MethodImplAttribute (MethodImplOptions.NoInlining)]
316 public static void ss2 () {
319 [MethodImplAttribute (MethodImplOptions.NoInlining)]
320 public static int ss3 () {
323 for (int i = 0; i < 10; ++i)
329 [MethodImplAttribute (MethodImplOptions.NoInlining)]
330 public static void ss3_2 () {
334 [MethodImplAttribute (MethodImplOptions.NoInlining)]
335 public static void ss3_2_2 () {
338 [MethodImplAttribute (MethodImplOptions.NoInlining)]
339 public static int ss4 () {
345 [MethodImplAttribute (MethodImplOptions.NoInlining)]
346 public static void ss5 (int[] arr, Func<int, bool> selector) {
347 // Call into linq which calls back into this assembly
348 arr.Count (selector);
351 [MethodImplAttribute (MethodImplOptions.NoInlining)]
352 public static void ss6 (bool b) {
355 throw new Exception ();
359 [MethodImplAttribute (MethodImplOptions.NoInlining)]
360 public static void ss7 () {
363 [MethodImplAttribute (MethodImplOptions.NoInlining)]
364 public static bool is_even (int i) {
370 Console.WriteLine ("HIT!");
376 [MethodImplAttribute (MethodImplOptions.NoInlining)]
377 public static void arguments () {
378 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));
380 arg2 ("FOO", null, "BLA", ref i, new GClass <int> { field = 42 }, new object ());
381 Tests t = new Tests () { field_i = 42, field_s = "S" };
385 [MethodImplAttribute (MethodImplOptions.NoInlining)]
386 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) {
387 return (int)(sb + b + (bl ? 0 : 1) + s + us + (int)c + i + ui + l + (long)ul + f + d + (int)ip + (int)uip);
390 [MethodImplAttribute (MethodImplOptions.NoInlining)]
391 public static string arg2 (string s, string s3, object o, ref int i, GClass <int> gc, object o2) {
392 return s + (s3 != null ? "" : "") + o + i + gc.field + o2;
395 [MethodImplAttribute (MethodImplOptions.NoInlining)]
396 public object arg3 (string s) {
397 return s + s + s + s + this;
400 [MethodImplAttribute (MethodImplOptions.NoInlining)]
401 public static void objects () {
402 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) };
403 t.o1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
404 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 });
407 [MethodImplAttribute (MethodImplOptions.NoInlining)]
408 public object o1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
409 if (t == null || gc1 == null || gc2 == null)
415 [MethodImplAttribute (MethodImplOptions.NoInlining)]
416 public static string o2 (string[] s2, int[] s3, int[,] s4, int[,] s5, IList<int> s6) {
417 return s2 [0] + s3 [0] + s4 [0, 0] + s6 [0];
420 [MethodImplAttribute (MethodImplOptions.NoInlining)]
421 public static void objrefs () {
422 Tests t = new Tests () {};
430 [MethodImplAttribute (MethodImplOptions.NoInlining)]
431 public static void set_child (Tests t) {
432 t.child = new Tests ();
435 [MethodImplAttribute (MethodImplOptions.NoInlining)]
436 public void objrefs1 () {
439 [MethodImplAttribute (MethodImplOptions.NoInlining)]
440 public static void objrefs2 () {
443 public static void vtypes () {
444 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 }};
445 AStruct s = new AStruct { i = 44, s = "T", k = 45 };
446 AStruct[] arr = new AStruct[] {
447 new AStruct () { i = 1, s = "S1" },
448 new AStruct () { i = 2, s = "S2" } };
454 [MethodImplAttribute (MethodImplOptions.NoInlining)]
455 public object vtypes1 (AStruct s, AStruct[] arr) {
462 [MethodImplAttribute (MethodImplOptions.NoInlining)]
463 public static void vtypes2 (AStruct s) {
467 [MethodImplAttribute (MethodImplOptions.NoInlining)]
468 public static void vtypes3 (AStruct s) {
469 AStruct.static_foo (5);
472 [MethodImplAttribute (MethodImplOptions.NoInlining)]
473 public static void locals () {
476 locals2<string> (null, 5, "ABC", ref s);
481 [MethodImplAttribute (MethodImplOptions.NoInlining)]
482 public static void locals1 (string[] args) {
485 for (int j = 0; j < 10; ++j) {
490 [MethodImplAttribute (MethodImplOptions.NoInlining)]
491 public static void locals2<T> (string[] args, int arg, T t, ref string rs) {
495 for (int j = 0; j < 10; ++j) {
504 [MethodImplAttribute (MethodImplOptions.NoInlining)]
505 public static void locals3 () {
525 [MethodImplAttribute (MethodImplOptions.NoInlining)]
526 public static void locals4 () {
529 [MethodImplAttribute (MethodImplOptions.NoInlining)]
530 public static void locals5 () {
533 [MethodImplAttribute (MethodImplOptions.NoInlining)]
534 public static void locals6 () {
537 for (i = 0; i < 10; ++i)
540 for (i = 0; i < 10; ++i)
550 [MethodImplAttribute (MethodImplOptions.NoInlining)]
551 public static void locals6_1 () {
554 [MethodImplAttribute (MethodImplOptions.NoInlining)]
555 public static void locals6_2 (int arg) {
558 [MethodImplAttribute (MethodImplOptions.NoInlining)]
559 public static void locals6_3 () {
560 // Clobber all registers
561 int sum = 0, i, j, k, l, m;
562 for (i = 0; i < 100; ++i)
564 for (j = 0; j < 100; ++j)
566 for (k = 0; k < 100; ++k)
568 for (l = 0; l < 100; ++l)
570 for (m = 0; m < 100; ++m)
574 [MethodImplAttribute (MethodImplOptions.NoInlining)]
575 public static void locals6_4 (int arg) {
578 [MethodImplAttribute (MethodImplOptions.NoInlining)]
579 public static void locals6_5 () {
582 [MethodImplAttribute (MethodImplOptions.NoInlining)]
583 public static void locals6_6 (int arg) {
586 [MethodImplAttribute (MethodImplOptions.NoInlining)]
587 public static void line_numbers () {
591 [MethodImplAttribute (MethodImplOptions.NoInlining)]
592 public static void suspend () {
600 [MethodImplAttribute (MethodImplOptions.NoInlining)]
601 public static void type_info () {
602 Tests t = new Tests () { field_i = 42, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B };
603 t.ti1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
606 AStruct s = new AStruct () { i = 42, s = "S", k = 43 };
608 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);
612 [MethodImplAttribute (MethodImplOptions.NoInlining)]
613 public object ti1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
614 if (t == null || gc1 == null || gc2 == null)
620 [MethodImplAttribute (MethodImplOptions.NoInlining)]
621 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) {
622 return s2 [0] + s3 [0] + s4 [0, 0];
625 [MethodImplAttribute (MethodImplOptions.NoInlining)]
626 public static void assembly_load () {
630 [MethodImplAttribute (MethodImplOptions.NoInlining)]
631 public static void assembly_load_2 () {
632 // This will load System.dll while holding the loader lock
636 [MethodImplAttribute (MethodImplOptions.NoInlining)]
637 public static void invoke () {
638 new Tests ().invoke1 (new Tests2 (), new AStruct () { i = 42, j = (IntPtr)43 }, new GStruct<int> { j = 42 });
639 new Tests ().invoke_ex ();
642 [MethodImplAttribute (MethodImplOptions.NoInlining)]
643 public void invoke1 (Tests2 t, AStruct s, GStruct<int> g) {
647 [MethodImplAttribute (MethodImplOptions.NoInlining)]
648 public void invoke2 () {
651 [MethodImplAttribute (MethodImplOptions.NoInlining)]
652 public void invoke_ex () {
656 [MethodImplAttribute (MethodImplOptions.NoInlining)]
657 public void invoke_ex_inner () {
659 throw new Exception ();
666 [MethodImplAttribute (MethodImplOptions.NoInlining)]
667 public void invoke_single_threaded () {
668 // Spawn a thread incrementing a counter
669 bool finished = false;
671 new Thread (delegate () {
678 invoke_single_threaded_2 ();
683 [MethodImplAttribute (MethodImplOptions.NoInlining)]
684 public void invoke_single_threaded_2 () {
687 public void invoke_return_void () {
690 public string invoke_return_ref () {
694 public object invoke_return_null () {
698 public int invoke_return_primitive () {
702 public int? invoke_return_nullable () {
706 public int? invoke_return_nullable_null () {
710 public void invoke_type_load () {
717 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) {
718 return ub + sb + ss + us + i + ui + l + (long)ul + (int)c + (b ? 1 : 0) + (int)f + (int)d;
721 public int invoke_pass_primitive2 (bool b) {
725 public string invoke_pass_ref (string s) {
729 public static string invoke_static_pass_ref (string s) {
733 public static void invoke_static_return_void () {
736 public static void invoke_throws () {
737 throw new Exception ();
740 [MethodImplAttribute (MethodImplOptions.NoInlining)]
741 public static void exceptions () {
743 throw new OverflowException ();
744 } catch (Exception) {
747 throw new OverflowException ();
748 } catch (Exception) {
751 throw new ArgumentException ();
752 } catch (Exception) {
755 throw new OverflowException ();
756 } catch (Exception) {
762 } catch (Exception) {
767 } catch (Exception) {
771 internal static Delegate create_filter_delegate (Delegate dlg, MethodInfo filter_method)
774 throw new ArgumentNullException ();
775 if (dlg.Target != null)
776 throw new ArgumentException ();
777 if (dlg.Method == null)
778 throw new ArgumentException ();
780 var ret_type = dlg.Method.ReturnType;
781 var param_types = dlg.Method.GetParameters ().Select (x => x.ParameterType).ToArray ();
783 var dynamic = new DynamicMethod (Guid.NewGuid ().ToString (), ret_type, param_types, typeof (object), true);
784 var ig = dynamic.GetILGenerator ();
786 LocalBuilder retval = null;
787 if (ret_type != typeof (void))
788 retval = ig.DeclareLocal (ret_type);
790 var label = ig.BeginExceptionBlock ();
792 for (int i = 0; i < param_types.Length; i++)
793 ig.Emit (OpCodes.Ldarg, i);
794 ig.Emit (OpCodes.Call, dlg.Method);
797 ig.Emit (OpCodes.Stloc, retval);
799 ig.Emit (OpCodes.Leave, label);
801 ig.BeginExceptFilterBlock ();
803 ig.Emit (OpCodes.Call, filter_method);
805 ig.BeginCatchBlock (null);
807 ig.Emit (OpCodes.Pop);
809 ig.EndExceptionBlock ();
812 ig.Emit (OpCodes.Ldloc, retval);
814 ig.Emit (OpCodes.Ret);
816 return dynamic.CreateDelegate (dlg.GetType ());
819 [MethodImplAttribute (MethodImplOptions.NoInlining)]
820 static void exception_filter_method () {
821 throw new InvalidOperationException ();
824 [MethodImplAttribute (MethodImplOptions.NoInlining)]
825 static int exception_filter_filter (Exception exc) {
829 [MethodImplAttribute (MethodImplOptions.NoInlining)]
830 public static void exception_filter () {
831 var method = typeof (Tests).GetMethod (
832 "exception_filter_method", BindingFlags.NonPublic | BindingFlags.Static);
833 var filter_method = typeof (Tests).GetMethod (
834 "exception_filter_filter", BindingFlags.NonPublic | BindingFlags.Static);
836 var dlg = Delegate.CreateDelegate (typeof (Action), method);
838 var wrapper = (Action) create_filter_delegate (dlg, filter_method);
843 [MethodImplAttribute (MethodImplOptions.NoInlining)]
844 public static bool return_true () {
848 [MethodImplAttribute (MethodImplOptions.NoInlining)]
849 public static void exceptions2 () {
851 throw new Exception ();
852 Console.WriteLine ();
855 [MethodImplAttribute (MethodImplOptions.NoInlining)]
856 public static void threads () {
857 Thread t = new Thread (delegate () {});
863 [MethodImplAttribute (MethodImplOptions.NoInlining)]
864 public static void domains () {
865 AppDomain domain = AppDomain.CreateDomain ("domain");
867 CrossDomain o = (CrossDomain)domain.CreateInstanceAndUnwrap (
868 typeof (CrossDomain).Assembly.FullName, "CrossDomain");
876 AppDomain.Unload (domain);
881 [MethodImplAttribute (MethodImplOptions.NoInlining)]
882 public static void domains_2 () {
885 [MethodImplAttribute (MethodImplOptions.NoInlining)]
886 public static void invoke_in_domain () {
889 [MethodImplAttribute (MethodImplOptions.NoInlining)]
890 public static void invoke_in_domain_2 () {
893 [MethodImplAttribute (MethodImplOptions.NoInlining)]
894 public static void dynamic_methods () {
895 var m = new DynamicMethod ("dyn_method", typeof (void), new Type [] { typeof (int) }, typeof (object).Module);
896 var ig = m.GetILGenerator ();
898 ig.Emit (OpCodes.Ldstr, "FOO");
899 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("dyn_call"));
900 ig.Emit (OpCodes.Ret);
902 var del = (Action<int>)m.CreateDelegate (typeof (Action<int>));
907 public static void dyn_call (string s) {
910 [MethodImplAttribute (MethodImplOptions.NoInlining)]
911 public static void ref_emit () {
912 AssemblyName assemblyName = new AssemblyName ();
913 assemblyName.Name = "foo";
915 AssemblyBuilder assembly =
916 Thread.GetDomain ().DefineDynamicAssembly (
917 assemblyName, AssemblyBuilderAccess.RunAndSave);
919 ModuleBuilder module = assembly.DefineDynamicModule ("foo.dll");
921 TypeBuilder tb = module.DefineType ("foo", TypeAttributes.Public, typeof (object));
922 MethodBuilder mb = tb.DefineMethod ("ref_emit_method", MethodAttributes.Public|MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type [] { });
923 ILGenerator ig = mb.GetILGenerator ();
924 ig.Emit (OpCodes.Ldstr, "FOO");
925 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("ref_emit_call"));
926 ig.Emit (OpCodes.Ret);
928 Type t = tb.CreateType ();
930 t.GetMethod ("ref_emit_method").Invoke (null, null);
933 [MethodImplAttribute (MethodImplOptions.NoInlining)]
934 public static void ref_emit_call (string s) {
937 [MethodImplAttribute (MethodImplOptions.NoInlining)]
938 public static void frames_in_native () {
942 [MethodImplAttribute (MethodImplOptions.NoInlining)]
943 public static void string_call (string s) {
946 [MethodImplAttribute (MethodImplOptions.NoInlining)]
947 public static void ss_regress_654694 () {
954 [MethodImplAttribute (MethodImplOptions.NoInlining)]
955 public static void user () {
958 Debugger.Log (5, Debugger.IsLogging () ? "A" : "", "B");
961 [MethodImplAttribute (MethodImplOptions.NoInlining)]
962 public static void type_load () {
966 [MethodImplAttribute (MethodImplOptions.NoInlining)]
967 static void type_load_2 () {
968 var c1 = new Dictionary<int, int> ();
970 var c = new TypeLoadClass ();
972 var c2 = new TypeLoadClass2 ();
976 [MethodImplAttribute (MethodImplOptions.NoInlining)]
977 public static void regress () {
978 regress_2755 (DateTime.Now);
981 [MethodImplAttribute (MethodImplOptions.NoInlining)]
982 public static unsafe void regress_2755 (DateTime d) {
983 int* buffer = stackalloc int [128];
988 for (int i = 0; i < 128; ++i)
991 regress_2755_3 (sum);
994 [MethodImplAttribute (MethodImplOptions.NoInlining)]
995 public static void regress_2755_2 () {
998 [MethodImplAttribute (MethodImplOptions.NoInlining)]
999 public static void regress_2755_3 (int sum) {
1003 class TypeLoadClass {
1006 class TypeLoadClass2 {
1009 public class CrossDomain : MarshalByRefObject
1011 public void invoke () {
1012 Tests.invoke_in_domain ();
1015 public void invoke_2 () {
1016 Tests.invoke_in_domain_2 ();
1022 public ProcessStartInfo info;
1025 // Class used for line number info testing, don't change its layout
1026 public class LineNumbers
1028 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1029 public static void ln1 () {
1034 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1035 public static void ln2 () {
1038 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1039 public static void ln3 () {
1040 #pragma warning disable 0219
1042 #pragma warning restore 0219