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;
173 static int static_i = 55;
174 static string static_s = "A";
175 public const int literal_i = 56;
176 public const string literal_s = "B";
178 public AStruct field_struct;
179 public object field_boxed_struct;
180 public GStruct<int> generic_field_struct;
182 public static int tls_i;
183 public static bool is_attached = Debugger.IsAttached;
185 #pragma warning restore 0414
187 public class NestedClass {
190 public int IntProperty {
199 public int ReadOnlyProperty {
205 public int this [int index] {
211 public static int Main (String[] args) {
214 if (args.Length > 0 && args [0] == "suspend-test")
215 /* This contains an infinite loop, so execute it conditionally */
235 if (args.Length > 0 && args [0] == "domain-test")
236 /* This takes a lot of time, so execute it conditionally */
238 if (args.Length > 0 && args [0] == "ref-emit-test")
240 if (args.Length > 0 && args [0] == "frames-in-native")
242 if (args.Length >0 && args [0] == "invoke-single-threaded")
243 new Tests ().invoke_single_threaded ();
247 public static void breakpoints () {
248 /* Call these early so it is JITted by the time a breakpoint is placed on it */
260 bp6<string> (new GClass <int> ());
265 [MethodImplAttribute (MethodImplOptions.NoInlining)]
266 public static void bp1 () {
269 [MethodImplAttribute (MethodImplOptions.NoInlining)]
270 public static void bp2 () {
273 [MethodImplAttribute (MethodImplOptions.NoInlining)]
274 public static void bp3 () {
277 [MethodImplAttribute (MethodImplOptions.NoInlining)]
278 public static void bp4 () {
281 [MethodImplAttribute (MethodImplOptions.NoInlining)]
282 public static void bp5 () {
285 [MethodImplAttribute (MethodImplOptions.NoInlining)]
286 public static void bp6<T> (GClass<int> gc) {
290 [MethodImplAttribute (MethodImplOptions.NoInlining)]
291 public static void bp7<T> () {
294 [MethodImplAttribute (MethodImplOptions.NoInlining)]
295 public static void single_stepping () {
302 ss5 (new int [] { 1, 2, 3 }, new Func<int, bool> (is_even));
307 ss_regress_654694 ();
310 [MethodImplAttribute (MethodImplOptions.NoInlining)]
311 public static void ss1 () {
314 [MethodImplAttribute (MethodImplOptions.NoInlining)]
315 public static void ss2 () {
318 [MethodImplAttribute (MethodImplOptions.NoInlining)]
319 public static int ss3 () {
322 for (int i = 0; i < 10; ++i)
328 [MethodImplAttribute (MethodImplOptions.NoInlining)]
329 public static void ss3_2 () {
333 [MethodImplAttribute (MethodImplOptions.NoInlining)]
334 public static void ss3_2_2 () {
337 [MethodImplAttribute (MethodImplOptions.NoInlining)]
338 public static int ss4 () {
344 [MethodImplAttribute (MethodImplOptions.NoInlining)]
345 public static void ss5 (int[] arr, Func<int, bool> selector) {
346 // Call into linq which calls back into this assembly
347 arr.Count (selector);
350 [MethodImplAttribute (MethodImplOptions.NoInlining)]
351 public static void ss6 (bool b) {
354 throw new Exception ();
358 [MethodImplAttribute (MethodImplOptions.NoInlining)]
359 public static void ss7 () {
362 [MethodImplAttribute (MethodImplOptions.NoInlining)]
363 public static bool is_even (int i) {
369 Console.WriteLine ("HIT!");
375 [MethodImplAttribute (MethodImplOptions.NoInlining)]
376 public static void arguments () {
377 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));
379 arg2 ("FOO", null, "BLA", ref i, new GClass <int> { field = 42 }, new object ());
380 Tests t = new Tests () { field_i = 42, field_s = "S" };
384 [MethodImplAttribute (MethodImplOptions.NoInlining)]
385 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) {
386 return (int)(sb + b + (bl ? 0 : 1) + s + us + (int)c + i + ui + l + (long)ul + f + d + (int)ip + (int)uip);
389 [MethodImplAttribute (MethodImplOptions.NoInlining)]
390 public static string arg2 (string s, string s3, object o, ref int i, GClass <int> gc, object o2) {
391 return s + (s3 != null ? "" : "") + o + i + gc.field + o2;
394 [MethodImplAttribute (MethodImplOptions.NoInlining)]
395 public object arg3 (string s) {
396 return s + s + s + s + this;
399 [MethodImplAttribute (MethodImplOptions.NoInlining)]
400 public static void objects () {
401 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) };
402 t.o1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
403 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 });
406 [MethodImplAttribute (MethodImplOptions.NoInlining)]
407 public object o1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
408 if (t == null || gc1 == null || gc2 == null)
414 [MethodImplAttribute (MethodImplOptions.NoInlining)]
415 public static string o2 (string[] s2, int[] s3, int[,] s4, int[,] s5, IList<int> s6) {
416 return s2 [0] + s3 [0] + s4 [0, 0] + s6 [0];
419 [MethodImplAttribute (MethodImplOptions.NoInlining)]
420 public static void objrefs () {
421 Tests t = new Tests () {};
429 [MethodImplAttribute (MethodImplOptions.NoInlining)]
430 public static void set_child (Tests t) {
431 t.child = new Tests ();
434 [MethodImplAttribute (MethodImplOptions.NoInlining)]
435 public void objrefs1 () {
438 [MethodImplAttribute (MethodImplOptions.NoInlining)]
439 public static void objrefs2 () {
442 public static void vtypes () {
443 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 }};
444 AStruct s = new AStruct { i = 44, s = "T", k = 45 };
445 AStruct[] arr = new AStruct[] {
446 new AStruct () { i = 1, s = "S1" },
447 new AStruct () { i = 2, s = "S2" } };
453 [MethodImplAttribute (MethodImplOptions.NoInlining)]
454 public object vtypes1 (AStruct s, AStruct[] arr) {
461 [MethodImplAttribute (MethodImplOptions.NoInlining)]
462 public static void vtypes2 (AStruct s) {
466 [MethodImplAttribute (MethodImplOptions.NoInlining)]
467 public static void vtypes3 (AStruct s) {
468 AStruct.static_foo (5);
471 [MethodImplAttribute (MethodImplOptions.NoInlining)]
472 public static void locals () {
475 locals2<string> (null, 5, "ABC", ref s);
480 [MethodImplAttribute (MethodImplOptions.NoInlining)]
481 public static void locals1 (string[] args) {
484 for (int j = 0; j < 10; ++j) {
489 [MethodImplAttribute (MethodImplOptions.NoInlining)]
490 public static void locals2<T> (string[] args, int arg, T t, ref string rs) {
494 for (int j = 0; j < 10; ++j) {
503 [MethodImplAttribute (MethodImplOptions.NoInlining)]
504 public static void locals3 () {
524 [MethodImplAttribute (MethodImplOptions.NoInlining)]
525 public static void locals4 () {
528 [MethodImplAttribute (MethodImplOptions.NoInlining)]
529 public static void locals5 () {
532 [MethodImplAttribute (MethodImplOptions.NoInlining)]
533 public static void locals6 () {
536 for (i = 0; i < 10; ++i)
539 for (i = 0; i < 10; ++i)
549 [MethodImplAttribute (MethodImplOptions.NoInlining)]
550 public static void locals6_1 () {
553 [MethodImplAttribute (MethodImplOptions.NoInlining)]
554 public static void locals6_2 (int arg) {
557 [MethodImplAttribute (MethodImplOptions.NoInlining)]
558 public static void locals6_3 () {
559 // Clobber all registers
560 int sum = 0, i, j, k, l, m;
561 for (i = 0; i < 100; ++i)
563 for (j = 0; j < 100; ++j)
565 for (k = 0; k < 100; ++k)
567 for (l = 0; l < 100; ++l)
569 for (m = 0; m < 100; ++m)
573 [MethodImplAttribute (MethodImplOptions.NoInlining)]
574 public static void locals6_4 (int arg) {
577 [MethodImplAttribute (MethodImplOptions.NoInlining)]
578 public static void locals6_5 () {
581 [MethodImplAttribute (MethodImplOptions.NoInlining)]
582 public static void locals6_6 (int arg) {
585 [MethodImplAttribute (MethodImplOptions.NoInlining)]
586 public static void line_numbers () {
590 [MethodImplAttribute (MethodImplOptions.NoInlining)]
591 public static void suspend () {
599 [MethodImplAttribute (MethodImplOptions.NoInlining)]
600 public static void type_info () {
601 Tests t = new Tests () { field_i = 42, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B };
602 t.ti1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
605 AStruct s = new AStruct () { i = 42, s = "S", k = 43 };
607 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);
611 [MethodImplAttribute (MethodImplOptions.NoInlining)]
612 public object ti1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
613 if (t == null || gc1 == null || gc2 == null)
619 [MethodImplAttribute (MethodImplOptions.NoInlining)]
620 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) {
621 return s2 [0] + s3 [0] + s4 [0, 0];
624 [MethodImplAttribute (MethodImplOptions.NoInlining)]
625 public static void assembly_load () {
629 [MethodImplAttribute (MethodImplOptions.NoInlining)]
630 public static void assembly_load_2 () {
631 // This will load System.dll while holding the loader lock
635 [MethodImplAttribute (MethodImplOptions.NoInlining)]
636 public static void invoke () {
637 new Tests ().invoke1 (new Tests2 (), new AStruct () { i = 42, j = (IntPtr)43 }, new GStruct<int> { j = 42 });
638 new Tests ().invoke_ex ();
641 [MethodImplAttribute (MethodImplOptions.NoInlining)]
642 public void invoke1 (Tests2 t, AStruct s, GStruct<int> g) {
646 [MethodImplAttribute (MethodImplOptions.NoInlining)]
647 public void invoke2 () {
650 [MethodImplAttribute (MethodImplOptions.NoInlining)]
651 public void invoke_ex () {
655 [MethodImplAttribute (MethodImplOptions.NoInlining)]
656 public void invoke_ex_inner () {
658 throw new Exception ();
665 [MethodImplAttribute (MethodImplOptions.NoInlining)]
666 public void invoke_single_threaded () {
667 // Spawn a thread incrementing a counter
668 bool finished = false;
670 new Thread (delegate () {
677 invoke_single_threaded_2 ();
682 [MethodImplAttribute (MethodImplOptions.NoInlining)]
683 public void invoke_single_threaded_2 () {
686 public void invoke_return_void () {
689 public string invoke_return_ref () {
693 public object invoke_return_null () {
697 public int invoke_return_primitive () {
701 public int? invoke_return_nullable () {
705 public int? invoke_return_nullable_null () {
709 public void invoke_type_load () {
716 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) {
717 return ub + sb + ss + us + i + ui + l + (long)ul + (int)c + (b ? 1 : 0) + (int)f + (int)d;
720 public int invoke_pass_primitive2 (bool b) {
724 public string invoke_pass_ref (string s) {
728 public static string invoke_static_pass_ref (string s) {
732 public static void invoke_static_return_void () {
735 public static void invoke_throws () {
736 throw new Exception ();
739 [MethodImplAttribute (MethodImplOptions.NoInlining)]
740 public static void exceptions () {
742 throw new OverflowException ();
743 } catch (Exception) {
746 throw new OverflowException ();
747 } catch (Exception) {
750 throw new ArgumentException ();
751 } catch (Exception) {
754 throw new OverflowException ();
755 } catch (Exception) {
761 } catch (Exception) {
766 } catch (Exception) {
770 internal static Delegate create_filter_delegate (Delegate dlg, MethodInfo filter_method)
773 throw new ArgumentNullException ();
774 if (dlg.Target != null)
775 throw new ArgumentException ();
776 if (dlg.Method == null)
777 throw new ArgumentException ();
779 var ret_type = dlg.Method.ReturnType;
780 var param_types = dlg.Method.GetParameters ().Select (x => x.ParameterType).ToArray ();
782 var dynamic = new DynamicMethod (Guid.NewGuid ().ToString (), ret_type, param_types, typeof (object), true);
783 var ig = dynamic.GetILGenerator ();
785 LocalBuilder retval = null;
786 if (ret_type != typeof (void))
787 retval = ig.DeclareLocal (ret_type);
789 var label = ig.BeginExceptionBlock ();
791 for (int i = 0; i < param_types.Length; i++)
792 ig.Emit (OpCodes.Ldarg, i);
793 ig.Emit (OpCodes.Call, dlg.Method);
796 ig.Emit (OpCodes.Stloc, retval);
798 ig.Emit (OpCodes.Leave, label);
800 ig.BeginExceptFilterBlock ();
802 ig.Emit (OpCodes.Call, filter_method);
804 ig.BeginCatchBlock (null);
806 ig.Emit (OpCodes.Pop);
808 ig.EndExceptionBlock ();
811 ig.Emit (OpCodes.Ldloc, retval);
813 ig.Emit (OpCodes.Ret);
815 return dynamic.CreateDelegate (dlg.GetType ());
818 [MethodImplAttribute (MethodImplOptions.NoInlining)]
819 static void exception_filter_method () {
820 throw new InvalidOperationException ();
823 [MethodImplAttribute (MethodImplOptions.NoInlining)]
824 static int exception_filter_filter (Exception exc) {
828 [MethodImplAttribute (MethodImplOptions.NoInlining)]
829 public static void exception_filter () {
830 var method = typeof (Tests).GetMethod (
831 "exception_filter_method", BindingFlags.NonPublic | BindingFlags.Static);
832 var filter_method = typeof (Tests).GetMethod (
833 "exception_filter_filter", BindingFlags.NonPublic | BindingFlags.Static);
835 var dlg = Delegate.CreateDelegate (typeof (Action), method);
837 var wrapper = (Action) create_filter_delegate (dlg, filter_method);
842 [MethodImplAttribute (MethodImplOptions.NoInlining)]
843 public static bool return_true () {
847 [MethodImplAttribute (MethodImplOptions.NoInlining)]
848 public static void exceptions2 () {
850 throw new Exception ();
851 Console.WriteLine ();
854 [MethodImplAttribute (MethodImplOptions.NoInlining)]
855 public static void threads () {
856 Thread t = new Thread (delegate () {});
862 [MethodImplAttribute (MethodImplOptions.NoInlining)]
863 public static void domains () {
864 AppDomain domain = AppDomain.CreateDomain ("domain");
866 CrossDomain o = (CrossDomain)domain.CreateInstanceAndUnwrap (
867 typeof (CrossDomain).Assembly.FullName, "CrossDomain");
875 AppDomain.Unload (domain);
880 [MethodImplAttribute (MethodImplOptions.NoInlining)]
881 public static void domains_2 () {
884 [MethodImplAttribute (MethodImplOptions.NoInlining)]
885 public static void invoke_in_domain () {
888 [MethodImplAttribute (MethodImplOptions.NoInlining)]
889 public static void invoke_in_domain_2 () {
892 [MethodImplAttribute (MethodImplOptions.NoInlining)]
893 public static void dynamic_methods () {
894 var m = new DynamicMethod ("dyn_method", typeof (void), new Type [] { typeof (int) }, typeof (object).Module);
895 var ig = m.GetILGenerator ();
897 ig.Emit (OpCodes.Ldstr, "FOO");
898 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("dyn_call"));
899 ig.Emit (OpCodes.Ret);
901 var del = (Action<int>)m.CreateDelegate (typeof (Action<int>));
906 public static void dyn_call (string s) {
909 [MethodImplAttribute (MethodImplOptions.NoInlining)]
910 public static void ref_emit () {
911 AssemblyName assemblyName = new AssemblyName ();
912 assemblyName.Name = "foo";
914 AssemblyBuilder assembly =
915 Thread.GetDomain ().DefineDynamicAssembly (
916 assemblyName, AssemblyBuilderAccess.RunAndSave);
918 ModuleBuilder module = assembly.DefineDynamicModule ("foo.dll");
920 TypeBuilder tb = module.DefineType ("foo", TypeAttributes.Public, typeof (object));
921 MethodBuilder mb = tb.DefineMethod ("ref_emit_method", MethodAttributes.Public|MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type [] { });
922 ILGenerator ig = mb.GetILGenerator ();
923 ig.Emit (OpCodes.Ldstr, "FOO");
924 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("ref_emit_call"));
925 ig.Emit (OpCodes.Ret);
927 Type t = tb.CreateType ();
929 t.GetMethod ("ref_emit_method").Invoke (null, null);
932 [MethodImplAttribute (MethodImplOptions.NoInlining)]
933 public static void ref_emit_call (string s) {
936 [MethodImplAttribute (MethodImplOptions.NoInlining)]
937 public static void frames_in_native () {
941 [MethodImplAttribute (MethodImplOptions.NoInlining)]
942 public static void string_call (string s) {
945 [MethodImplAttribute (MethodImplOptions.NoInlining)]
946 public static void ss_regress_654694 () {
953 [MethodImplAttribute (MethodImplOptions.NoInlining)]
954 public static void user () {
957 Debugger.Log (5, Debugger.IsLogging () ? "A" : "", "B");
960 [MethodImplAttribute (MethodImplOptions.NoInlining)]
961 public static void type_load () {
965 [MethodImplAttribute (MethodImplOptions.NoInlining)]
966 static void type_load_2 () {
967 var c1 = new Dictionary<int, int> ();
969 var c = new TypeLoadClass ();
971 var c2 = new TypeLoadClass2 ();
975 [MethodImplAttribute (MethodImplOptions.NoInlining)]
976 public static void regress () {
977 regress_2755 (DateTime.Now);
980 [MethodImplAttribute (MethodImplOptions.NoInlining)]
981 public static unsafe void regress_2755 (DateTime d) {
982 int* buffer = stackalloc int [128];
987 for (int i = 0; i < 128; ++i)
990 regress_2755_3 (sum);
993 [MethodImplAttribute (MethodImplOptions.NoInlining)]
994 public static void regress_2755_2 () {
997 [MethodImplAttribute (MethodImplOptions.NoInlining)]
998 public static void regress_2755_3 (int sum) {
1002 class TypeLoadClass {
1005 class TypeLoadClass2 {
1008 public class CrossDomain : MarshalByRefObject
1010 public void invoke () {
1011 Tests.invoke_in_domain ();
1014 public void invoke_2 () {
1015 Tests.invoke_in_domain_2 ();
1021 public ProcessStartInfo info;
1024 // Class used for line number info testing, don't change its layout
1025 public class LineNumbers
1027 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1028 public static void ln1 () {
1033 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1034 public static void ln2 () {
1037 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1038 public static void ln3 () {
1039 #pragma warning disable 0219
1041 #pragma warning restore 0219