[runtime] Read fields out of TransparentProxies correctly in debugger agent
[mono.git] / mcs / class / Mono.Debugger.Soft / Test / dtest-app.cs
1 /*
2  * dtest-app.cs:
3  *
4  *   Application program used by the debugger tests.
5  */
6 using System;
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;
13 using System.Linq;
14
15 public class TestsBase
16 {
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
25
26         public virtual string virtual_method () {
27                 return "V1";
28         }
29 }
30
31 public enum AnEnum {
32         A = 0,
33         B= 1
34 }
35
36 public sealed class Tests3 {
37         public static void M1 () {
38         }
39
40         static void M2 () {
41         }
42
43         public void M3 () {
44         }
45
46         void M4 () {
47         }
48
49 }
50
51 public static class Tests4 {
52         static Tests4 () {
53         }
54 }
55
56 public class AAttribute : Attribute {
57         public int afield;
58 }
59
60 public class BAttribute : AAttribute {
61         public int bfield;
62 }
63
64 [DebuggerDisplay ("Tests", Name="FOO", Target=typeof (int))]
65 [DebuggerTypeProxy (typeof (Tests))]
66 [BAttribute (afield = 1, bfield = 2)]
67 public class Tests2 {
68         [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
69         public int field_j;
70         public static int static_field_j;
71
72         [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
73         public int AProperty {
74                 get {
75                         return 0;
76                 }
77         }
78
79         public void invoke () {
80         }
81 }
82
83 public struct AStruct : ITest2 {
84         public int i;
85         public string s;
86         public byte k;
87         public IntPtr j;
88         public int l;
89 /*
90         public AStruct () {
91                 i = 0;
92                 s = null;
93                 k = 0;
94                 j = IntPtr.Zero;
95                 l = 0;
96         }
97 */
98         public AStruct (int arg) {
99                 i = arg;
100                 s = null;
101                 k = 0;
102                 j = IntPtr.Zero;
103                 l = 0;
104         }
105
106         [MethodImplAttribute (MethodImplOptions.NoInlining)]
107         public int foo (int val) {
108                 return val;
109         }
110
111         [MethodImplAttribute (MethodImplOptions.NoInlining)]
112         public static int static_foo (int val) {
113                 return val;
114         }
115
116         [MethodImplAttribute (MethodImplOptions.NoInlining)]
117         public int invoke_return_int () {
118                 return i;
119         }
120
121         [MethodImplAttribute (MethodImplOptions.NoInlining)]
122         public static int invoke_static () {
123                 return 5;
124         }
125
126         [MethodImplAttribute (MethodImplOptions.NoInlining)]
127         public IntPtr invoke_return_intptr () {
128                 return j;
129         }
130
131         [MethodImplAttribute (MethodImplOptions.NoInlining)]
132         public void invoke_mutate () {
133                 l = 5;
134         }
135
136         public int invoke_iface () {
137                 return i;
138         }
139
140         public override string ToString () {
141                 return i.ToString ();
142         }
143 }
144
145 public class GClass<T> {
146         public T field;
147         public static T static_field;
148
149         [MethodImplAttribute (MethodImplOptions.NoInlining)]
150         public GClass () {
151         }
152
153         [MethodImplAttribute (MethodImplOptions.NoInlining)]
154         public void bp<T2> () {
155         }
156 }
157
158 public struct GStruct<T> {
159         public T i;
160
161         public int j;
162
163         [MethodImplAttribute (MethodImplOptions.NoInlining)]
164         public int invoke_return_int () {
165                 return j;
166         }
167 }
168
169 public struct NestedStruct {
170         NestedInner nested1, nested2;
171 }
172
173 public struct NestedInner {
174 }
175
176 public interface IRecStruct {
177         void foo (object o);
178 }
179
180 struct RecStruct : IRecStruct {
181         public object o;
182
183         public void foo (object o) {
184                 this.o = o;
185         }
186 }
187
188 interface ITest
189 {
190         void Foo ();
191         void Bar ();
192 }
193
194 interface ITest<T>
195 {
196         void Foo ();
197         void Bar ();
198 }
199
200 class TestIfaces : ITest
201 {
202         void ITest.Foo () {
203         }
204
205         void ITest.Bar () {
206         }
207
208         TestIfaces<int> Baz () {
209                 return null;
210         }
211 }
212
213 class TestIfaces<T> : ITest<T>
214 {
215         void ITest<T>.Foo () {
216         }
217
218         void ITest<T>.Bar () {
219         }
220 }
221
222 public interface ITest2
223 {
224         int invoke_iface ();
225 }
226
227 public class Tests : TestsBase, ITest2
228 {
229 #pragma warning disable 0414
230         int field_i;
231         string field_s;
232         AnEnum field_enum;
233         bool field_bool1, field_bool2;
234         char field_char;
235         byte field_byte;
236         sbyte field_sbyte;
237         short field_short;
238         ushort field_ushort;
239         long field_long;
240         ulong field_ulong;
241         float field_float;
242         double field_double;
243         Thread field_class;
244         IntPtr field_intptr;
245         int? field_nullable;
246         static int static_i = 55;
247         static string static_s = "A";
248         public const int literal_i = 56;
249         public const string literal_s = "B";
250         public object child;
251         public AStruct field_struct;
252         public object field_boxed_struct;
253         public GStruct<int> generic_field_struct;
254         public KeyValuePair<int, object> boxed_struct_field;
255         [ThreadStatic]
256         public static int tls_i;
257         public static bool is_attached = Debugger.IsAttached;
258         public NestedStruct nested_struct;
259
260 #pragma warning restore 0414
261
262         public class NestedClass {
263         }
264
265         public int IntProperty {
266                 get {
267                         return field_i;
268                 }
269                 set {
270                         field_i = value;
271                 }
272         }
273
274         public int ReadOnlyProperty {
275                 get {
276                         return field_i;
277                 }
278         }
279
280         public int this [int index] {
281                 get {
282                         return field_i;
283                 }
284         }
285
286         public static void wait_one ()
287         {
288                 ManualResetEvent evt = new ManualResetEvent (false);
289                 evt.WaitOne ();
290         }
291
292         public static int Main (String[] args) {
293                 tls_i = 42;
294
295                 if (args.Length > 0 && args [0] == "suspend-test")
296                         /* This contains an infinite loop, so execute it conditionally */
297                         suspend ();
298                 if (args.Length >0 && args [0] == "unhandled-exception") {
299                         unhandled_exception ();
300                         return 0;
301                 }
302                 if (args.Length >0 && args [0] == "unhandled-exception-endinvoke") {
303                         unhandled_exception_endinvoke ();
304                         return 0;
305                 }
306                 if (args.Length >0 && args [0] == "unhandled-exception-user") {
307                         unhandled_exception_user ();
308                         return 0;
309                 }
310                 if (args.Length >0 && args [0] == "wait-one") {
311                         wait_one ();
312                         return 0;
313                 }
314                 breakpoints ();
315                 single_stepping ();
316                 arguments ();
317                 objects ();
318                 objrefs ();
319                 vtypes ();
320                 locals ();
321                 line_numbers ();
322                 type_info ();
323                 assembly_load ();
324                 invoke ();
325                 exceptions ();
326                 exception_filter ();
327                 threads ();
328                 dynamic_methods ();
329                 user ();
330                 type_load ();
331                 regress ();
332                 gc_suspend ();
333                 set_ip ();
334                 step_filters ();
335                 local_reflect ();
336                 if (args.Length > 0 && args [0] == "domain-test")
337                         /* This takes a lot of time, so execute it conditionally */
338                         domains ();
339                 if (args.Length > 0 && args [0] == "ref-emit-test")
340                         ref_emit ();
341                 if (args.Length > 0 && args [0] == "frames-in-native")
342                         frames_in_native ();
343                 if (args.Length > 0 && args [0] == "invoke-single-threaded")
344                         new Tests ().invoke_single_threaded ();
345                 if (args.Length > 0 && args [0] == "invoke-abort")
346                         new Tests ().invoke_abort ();
347                 new Tests ().evaluate_method ();
348                 return 3;
349         }
350
351         public static void local_reflect () {
352                 //Breakpoint line below, and reflect someField via ObjectMirror;
353                 LocalReflectClass.RunMe ();
354         }
355
356         public static void breakpoints () {
357                 /* Call these early so it is JITted by the time a breakpoint is placed on it */
358                 bp3 ();
359                 bp7<int> ();
360                 bp7<string> ();
361
362                 bp1 ();
363                 bp2 ();
364                 bp3 ();
365                 bp4 ();
366                 bp4 ();
367                 bp4 ();
368                 bp5 ();
369                 bp6<string> (new GClass <int> ());
370                 bp7<int> ();
371                 bp7<string> ();
372         }
373
374         [MethodImplAttribute (MethodImplOptions.NoInlining)]
375         public static void bp1 () {
376         }
377
378         [MethodImplAttribute (MethodImplOptions.NoInlining)]
379         public static void bp2 () {
380         }
381
382         [MethodImplAttribute (MethodImplOptions.NoInlining)]
383         public static void bp3 () {
384         }
385
386         [MethodImplAttribute (MethodImplOptions.NoInlining)]
387         public static void bp4 () {
388         }
389
390         [MethodImplAttribute (MethodImplOptions.NoInlining)]
391         public static void bp5 () {
392         }
393
394         [MethodImplAttribute (MethodImplOptions.NoInlining)]
395         public static void bp6<T> (GClass<int> gc) {
396                 gc.bp<int> ();
397         }
398
399         [MethodImplAttribute (MethodImplOptions.NoInlining)]
400         public static void bp7<T> () {
401         }
402
403         [MethodImplAttribute (MethodImplOptions.NoInlining)]
404         public static void single_stepping () {
405                 bool b = true;
406                 ss1 ();
407                 ss2 ();
408                 ss3 ();
409                 ss3_2 ();
410                 ss4 ();
411                 ss5 (new int [] { 1, 2, 3 }, new Func<int, bool> (is_even));
412                 try {
413                         ss6 (b);
414                 } catch {
415                 }
416                 ss7 ();
417                 ss_nested ();
418                 ss_regress_654694 ();
419                 ss_step_through ();
420                 ss_non_user_code ();
421                 ss_recursive (1);
422                 ss_fp_clobber ();
423         }
424
425         [MethodImplAttribute (MethodImplOptions.NoInlining)]
426         public static void ss1 () {
427         }
428
429         [MethodImplAttribute (MethodImplOptions.NoInlining)]
430         public static void ss2 () {
431         }
432
433         [MethodImplAttribute (MethodImplOptions.NoInlining)]
434         public static int ss3 () {
435                 int sum = 0;
436
437                 for (int i = 0; i < 10; ++i)
438                         sum += i;
439
440                 return sum;
441         }
442
443         [MethodImplAttribute (MethodImplOptions.NoInlining)]
444         public static void ss3_2 () {
445                 ss3_2_2 ();
446         }
447
448         [MethodImplAttribute (MethodImplOptions.NoInlining)]
449         public static void ss3_2_2 () {
450         }
451
452         [MethodImplAttribute (MethodImplOptions.NoInlining)]
453         public static int ss4 () {
454                 ss1 (); ss1 ();
455                 ss2 ();
456                 return 0;
457         }
458
459         [MethodImplAttribute (MethodImplOptions.NoInlining)]
460         public static void ss5 (int[] arr, Func<int, bool> selector) {
461                 // Call into linq which calls back into this assembly
462                 arr.Count (selector);
463         }
464
465         [MethodImplAttribute (MethodImplOptions.NoInlining)]
466         public static void ss6 (bool b) {
467                 if (b) {
468                         ss6_2 ();
469                         throw new Exception ();
470                 }
471         }
472
473         [MethodImplAttribute (MethodImplOptions.NoInlining)]
474         public static void ss6_2 () {
475         }
476
477         [MethodImplAttribute (MethodImplOptions.NoInlining)]
478         public static void ss7 () {
479                 try {
480                         ss7_2 ();
481                         ss7_3 ();
482                 } catch {
483                 }
484                 ss7_2 ();
485         }
486
487         [MethodImplAttribute (MethodImplOptions.NoInlining)]
488         public static void ss7_2 () {
489         }
490
491         [MethodImplAttribute (MethodImplOptions.NoInlining)]
492         public static void ss7_3 () {
493                 throw new Exception ();
494         }
495
496         [MethodImplAttribute (MethodImplOptions.NoInlining)]
497         public static void ss_nested () {
498                 ss_nested_1 (ss_nested_2 ());
499                 ss_nested_1 (ss_nested_2 ());
500                 ss_nested_3 ();
501         }
502
503         [MethodImplAttribute (MethodImplOptions.NoInlining)]
504         public static void ss_nested_1 (int i) {
505         }
506
507         [MethodImplAttribute (MethodImplOptions.NoInlining)]
508         public static int ss_nested_2 () {
509                 return 0;
510         }
511
512         [MethodImplAttribute (MethodImplOptions.NoInlining)]
513         public static void ss_nested_3 () {
514         }
515
516         [MethodImplAttribute (MethodImplOptions.NoInlining)]
517         public static void ss_step_through () {
518                 step_through_1 ();
519                 StepThroughClass.step_through_2 ();
520                 step_through_3 ();
521         }
522
523         [DebuggerStepThrough]
524         [MethodImplAttribute (MethodImplOptions.NoInlining)]
525         public static void step_through_1 () {
526         }
527
528         [DebuggerStepThrough]
529         class StepThroughClass {
530                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
531                 public static void step_through_2 () {
532                 }
533         }
534
535         [DebuggerStepThrough]
536         [MethodImplAttribute (MethodImplOptions.NoInlining)]
537         public static void step_through_3 () {
538         }
539
540         [MethodImplAttribute (MethodImplOptions.NoInlining)]
541         public static void ss_non_user_code () {
542                 non_user_code_1 ();
543                 StepNonUserCodeClass.non_user_code_2 ();
544                 non_user_code_3 ();
545         }
546
547         [DebuggerNonUserCode]
548         [MethodImplAttribute (MethodImplOptions.NoInlining)]
549         public static void non_user_code_1 () {
550         }
551
552         [DebuggerNonUserCode]
553         class StepNonUserCodeClass {
554                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
555                 public static void non_user_code_2 () {
556                 }
557         }
558
559         [DebuggerNonUserCode]
560         [MethodImplAttribute (MethodImplOptions.NoInlining)]
561         public static void non_user_code_3 () {
562         }
563
564         [MethodImplAttribute (MethodImplOptions.NoInlining)]
565         public static void ss_recursive (int n) {
566                 if (n == 10)
567                         return;
568                 ss_recursive (n + 1);
569         }
570
571         [MethodImplAttribute (MethodImplOptions.NoInlining)]
572         public static void ss_fp_clobber () {
573                 double v = ss_fp_clobber_1 (5.0);
574                 ss_fp_clobber_2 (v);
575         }
576
577         [MethodImplAttribute (MethodImplOptions.NoInlining)]
578         public static double ss_fp_clobber_1 (double d) {
579                 return d + 2.0;
580         }
581
582         [MethodImplAttribute (MethodImplOptions.NoInlining)]
583         public static void ss_fp_clobber_2 (double d) {
584         }
585
586         [MethodImplAttribute (MethodImplOptions.NoInlining)]
587         public static bool is_even (int i) {
588                 return i % 2 == 0;
589         }
590
591         /*
592                 lock (static_s) {
593                         Console.WriteLine ("HIT!");
594                 }
595                 return 0;
596         }
597         */
598
599         [MethodImplAttribute (MethodImplOptions.NoInlining)]
600         public static void arguments () {
601                 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));
602                 int i = 42;
603                 arg2 ("FOO", null, "BLA", ref i, new GClass <int> { field = 42 }, new object (), '\0'.ToString () + "A");
604                 Tests t = new Tests () { field_i = 42, field_s = "S" };
605                 t.arg3 ("BLA");
606         }
607
608         [MethodImplAttribute (MethodImplOptions.NoInlining)]
609         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) {
610                 return (int)(sb + b + (bl ? 0 : 1) + s + us + (int)c + i + ui + l + (long)ul + f + d + (int)ip + (int)uip);
611         }
612
613         [MethodImplAttribute (MethodImplOptions.NoInlining)]
614         public static string arg2 (string s, string s3, object o, ref int i, GClass <int> gc, object o2, string s4) {
615                 return s + (s3 != null ? "" : "") + o + i + gc.field + o2;
616         }
617
618         [MethodImplAttribute (MethodImplOptions.NoInlining)]
619         public object arg3 (string s) {
620                 return s + s + s + s + this;
621         }
622
623         [MethodImplAttribute (MethodImplOptions.NoInlining)]
624         public static void objects () {
625                 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), field_nullable = null };
626                 t.o1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
627                 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 });
628         }
629
630         [MethodImplAttribute (MethodImplOptions.NoInlining)]
631         public object o1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
632                 if (t == null || gc1 == null || gc2 == null)
633                         return null;
634                 else
635                         return this;
636         }
637
638         [MethodImplAttribute (MethodImplOptions.NoInlining)]
639         public static string o2 (string[] s2, int[] s3, int[,] s4, int[,] s5, IList<int> s6) {
640                 return s2 [0] + s3 [0] + s4 [0, 0] + s6 [0];
641         }
642
643         [MethodImplAttribute (MethodImplOptions.NoInlining)]
644         public static void objrefs () {
645                 Tests t = new Tests () {};
646                 set_child (t);
647                 t.objrefs1 ();
648                 t.child = null;
649                 GC.Collect ();
650                 objrefs2 ();
651         }
652
653         [MethodImplAttribute (MethodImplOptions.NoInlining)]
654         public static void set_child (Tests t) {
655                 t.child = new Tests ();
656         }
657
658         [MethodImplAttribute (MethodImplOptions.NoInlining)]
659         public void objrefs1 () {
660         }
661
662         [MethodImplAttribute (MethodImplOptions.NoInlining)]
663         public static void objrefs2 () {
664         }
665
666         public static void vtypes () {
667                 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 }, boxed_struct_field = new KeyValuePair<int, object> (1, (long)42 ) };
668                 AStruct s = new AStruct { i = 44, s = "T", k = 45 };
669                 AStruct[] arr = new AStruct[] { 
670                         new AStruct () { i = 1, s = "S1" },
671                         new AStruct () { i = 2, s = "S2" } };
672                 TypedReference typedref = __makeref (s);
673                 t.vtypes1 (s, arr, typedref);
674                 vtypes2 (s);
675                 vtypes3 (s);
676                 vtypes4 ();
677         }
678
679         [MethodImplAttribute (MethodImplOptions.NoInlining)]
680         public object vtypes1 (AStruct s, AStruct[] arr, TypedReference typedref) {
681                 if (arr != null)
682                         return this;
683                 else
684                         return null;
685         }
686
687         [MethodImplAttribute (MethodImplOptions.NoInlining)]
688         public static void vtypes2 (AStruct s) {
689                 s.foo (5);
690         }
691
692         [MethodImplAttribute (MethodImplOptions.NoInlining)]
693         public static void vtypes3 (AStruct s) {
694                 AStruct.static_foo (5);
695         }
696
697         [MethodImplAttribute (MethodImplOptions.NoInlining)]
698         public static void vtypes4_2 (IRecStruct o) {
699         }
700
701         [MethodImplAttribute (MethodImplOptions.NoInlining)]
702         public static void vtypes4 () {
703                 IRecStruct s = new RecStruct ();
704                 s.foo (s);
705                 vtypes4_2 (s);
706         }
707
708         [MethodImplAttribute (MethodImplOptions.NoInlining)]
709         public static void locals () {
710                 string s = null;
711                 var astruct = new AStruct () { i = 42 };
712                 locals1 (null);
713                 locals2<string> (null, 5, "ABC", ref s, ref astruct);
714                 locals3 ();
715                 locals6 ();
716                 locals7<int> (22);
717         }
718
719         [MethodImplAttribute (MethodImplOptions.NoInlining)]
720         static void locals11 (double a, ref double b) {
721         }
722
723         [MethodImplAttribute (MethodImplOptions.NoInlining)]
724         public static void locals1 (string[] args) {
725                 long foo = 42;
726
727                 double ri = 1;
728                 locals11 (b: ref ri, a: ri);
729
730                 for (int j = 0; j < 10; ++j) {
731                         foo ++;
732                 }
733         }
734
735         [MethodImplAttribute (MethodImplOptions.NoInlining)]
736 #if NET_4_5
737         [StateMachine (typeof (int))]
738 #endif
739         public static void locals2<T> (string[] args, int arg, T t, ref string rs, ref AStruct astruct) {
740                 long i = 42;
741                 string s = "AB";
742
743                 for (int j = 0; j < 10; ++j) {
744                         if (s != null)
745                                 i ++;
746                         if (t != null)
747                                 i ++;
748                         astruct = new AStruct ();
749                 }
750                 rs = "A";
751                 List<int> alist = new List<int> () { 12 };
752         }
753
754
755         [MethodImplAttribute (MethodImplOptions.NoInlining)]
756         public static void locals3 () {
757                 string s = "B";
758                 s.ToString ();
759
760                 {
761                         long i = 42;
762                         i ++;
763                         locals4 ();
764                 }
765                 {
766                         string i = "A";
767                         i.ToString ();
768                         locals5 ();
769                 }
770                 {
771                         long j = 42;
772                         j ++;
773                 }
774         }
775
776         [MethodImplAttribute (MethodImplOptions.NoInlining)]
777         public static void locals4 () {
778         }
779
780         [MethodImplAttribute (MethodImplOptions.NoInlining)]
781         public static void locals5 () {
782         }
783
784         [MethodImplAttribute (MethodImplOptions.NoInlining)]
785         public static void locals6 () {
786                 int i = 0;
787                 int j = 0;
788                 for (i = 0; i < 10; ++i)
789                         j ++;
790                 sbyte sb = 0;
791                 for (i = 0; i < 10; ++i)
792                         sb ++;
793                 locals6_1 ();
794                 locals6_2 (j);
795                 locals6_3 ();
796                 locals6_4 (j);
797                 locals6_5 ();
798                 locals6_6 (sb);
799         }
800
801         [MethodImplAttribute (MethodImplOptions.NoInlining)]
802         public static void locals6_1 () {
803         }
804
805         [MethodImplAttribute (MethodImplOptions.NoInlining)]
806         public static void locals6_2 (int arg) {
807         }
808
809         [MethodImplAttribute (MethodImplOptions.NoInlining)]
810         public static void locals6_3 () {
811                 // Clobber all registers
812                 int sum = 0, i, j, k, l, m;
813                 for (i = 0; i < 100; ++i)
814                         sum ++;
815                 for (j = 0; j < 100; ++j)
816                         sum ++;
817                 for (k = 0; k < 100; ++k)
818                         sum ++;
819                 for (l = 0; l < 100; ++l)
820                         sum ++;
821                 for (m = 0; m < 100; ++m)
822                         sum ++;
823         }
824
825         [MethodImplAttribute (MethodImplOptions.NoInlining)]
826         public static void locals6_4 (int arg) {
827         }
828
829         [MethodImplAttribute (MethodImplOptions.NoInlining)]
830         public static void locals6_5 () {
831         }
832
833         [MethodImplAttribute (MethodImplOptions.NoInlining)]
834         public static void locals6_6 (int arg) {
835         }
836
837         [MethodImplAttribute (MethodImplOptions.NoInlining)]
838         public static void locals7<T> (T arg) {
839                 T t = arg;
840                 T t2 = t;
841         }
842
843         [MethodImplAttribute (MethodImplOptions.NoInlining)]
844         public static void line_numbers () {
845                 LineNumbers.ln1 ();
846         }
847
848         [MethodImplAttribute (MethodImplOptions.NoInlining)]
849         public static void suspend () {
850                 long i = 5;
851
852                 while (true) {
853                         i ++;
854                 }
855         }
856
857         struct TypedRefTest {
858                 public int MaxValue;
859         }
860
861         [MethodImplAttribute (MethodImplOptions.NoInlining)]
862         public static void type_info () {
863                 Tests t = new Tests () { field_i = 42, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B };
864                 t.ti1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
865                 int val = 0;
866                 unsafe {
867                         AStruct s = new AStruct () { i = 42, s = "S", k = 43 };
868                         TypedRefTest reftest = new TypedRefTest () { MaxValue = 12 };
869                         TypedReference typedref = __makeref (reftest);
870                         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, typedref);
871                 }
872         }
873
874         [MethodImplAttribute (MethodImplOptions.NoInlining)]
875         public object ti1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
876                 if (t == null || gc1 == null || gc2 == null)
877                         return null;
878                 else
879                         return this;
880         }
881
882         [MethodImplAttribute (MethodImplOptions.NoInlining)]
883         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, TypedReference typedref) {
884                 return s2 [0] + s3 [0] + s4 [0, 0];
885         }
886
887         [MethodImplAttribute (MethodImplOptions.NoInlining)]
888         public static void assembly_load () {
889                 assembly_load_2 ();
890         }
891
892         [MethodImplAttribute (MethodImplOptions.NoInlining)]
893         public static void assembly_load_2 () {
894                 // This will load System.dll while holding the loader lock
895                 new Foo ();
896         }
897
898         [MethodImplAttribute (MethodImplOptions.NoInlining)]
899         public static void invoke () {
900                 new Tests ().invoke1 (new Tests2 (), new AStruct () { i = 42, j = (IntPtr)43 }, new GStruct<int> { j = 42 });
901                 new Tests ().invoke_ex ();
902         }
903
904         [MethodImplAttribute (MethodImplOptions.NoInlining)]
905         public void invoke1 (Tests2 t, AStruct s, GStruct<int> g) {
906                 invoke2 ();
907         }
908
909         [MethodImplAttribute (MethodImplOptions.NoInlining)]
910         public void invoke2 () {
911         }
912
913         [MethodImplAttribute (MethodImplOptions.NoInlining)]
914         public void invoke_ex () {
915                 invoke_ex_inner ();
916         }
917
918         [MethodImplAttribute (MethodImplOptions.NoInlining)]
919         public void invoke_ex_inner () {
920                 try {
921                         throw new Exception ();
922                 } catch {
923                 }
924         }
925
926         int counter;
927
928         [MethodImplAttribute (MethodImplOptions.NoInlining)]
929         public void invoke_single_threaded () {
930                 // Spawn a thread incrementing a counter
931                 bool finished = false;
932
933                 new Thread (delegate () {
934                                 while (!finished)
935                                         counter ++;
936                 }).Start ();
937
938                 Thread.Sleep (100);
939
940                 invoke_single_threaded_2 ();
941
942                 finished = true;
943         }
944
945         [MethodImplAttribute (MethodImplOptions.NoInlining)]
946         public void invoke_single_threaded_2 () {
947         }
948
949         [MethodImplAttribute (MethodImplOptions.NoInlining)]
950         public void invoke_abort () {
951         }
952
953         [MethodImplAttribute (MethodImplOptions.NoInlining)]
954         public void invoke_abort_2 () {
955                 Thread.Sleep (1000000);
956         }
957
958         public void invoke_return_void () {
959         }
960
961         public string invoke_return_ref () {
962                 return "ABC";
963         }
964
965         public object invoke_return_null () {
966                 return null;
967         }
968
969         public int invoke_return_primitive () {
970                 return 42;
971         }
972
973         public int? invoke_return_nullable () {
974                 return 42;
975         }
976
977         public int? invoke_return_nullable_null () {
978                 return null;
979         }
980
981         public void invoke_type_load () {
982                 new Class3 ();
983         }
984
985         class Class3 {
986         }
987
988         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) {
989                 return ub + sb + ss + us + i + ui + l + (long)ul + (int)c + (b ? 1 : 0) + (int)f + (int)d;
990         }
991
992         public int invoke_pass_primitive2 (bool b) {
993                 return b ? 1 : 0;
994         }
995
996         public string invoke_pass_ref (string s) {
997                 return s;
998         }
999
1000         public static string invoke_static_pass_ref (string s) {
1001                 return s;
1002         }
1003
1004         public static void invoke_static_return_void () {
1005         }
1006
1007         public static void invoke_throws () {
1008                 throw new Exception ();
1009         }
1010
1011         public int invoke_iface () {
1012                 return 42;
1013         }
1014
1015         public void invoke_out (out int foo, out int[] arr) {
1016                 foo = 5;
1017                 arr = new int [10];
1018         }
1019
1020         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1021         public static void exceptions () {
1022                 try {
1023                         throw new OverflowException ();
1024                 } catch (Exception) {
1025                 }
1026                 try {
1027                         throw new OverflowException ();
1028                 } catch (Exception) {
1029                 }
1030                 try {
1031                         throw new ArgumentException ();
1032                 } catch (Exception) {
1033                 }
1034                 try {
1035                         throw new OverflowException ();
1036                 } catch (Exception) {
1037                 }
1038                 // no subclasses
1039                 try {
1040                         throw new OverflowException ();
1041                 } catch (Exception) {
1042                 }
1043                 try {
1044                         throw new Exception ();
1045                 } catch (Exception) {
1046                 }
1047
1048                 object o = null;
1049                 try {
1050                         o.GetType ();
1051                 } catch (Exception) {
1052                 }
1053
1054                 try {
1055                         exceptions2 ();
1056                 } catch (Exception) {
1057                 }
1058         }
1059
1060         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1061         public static void unhandled_exception () {
1062                 ThreadPool.QueueUserWorkItem (delegate {
1063                                 throw new InvalidOperationException ();
1064                         });
1065                 Thread.Sleep (10000);
1066         }
1067
1068         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1069         public static void unhandled_exception_endinvoke_2 () {
1070         }
1071
1072         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1073         public static void unhandled_exception_endinvoke () {
1074                         Action action = new Action (() => 
1075                         {
1076                                 throw new Exception ("thrown");
1077                         });
1078                         action.BeginInvoke ((ar) => {
1079                                 try {
1080                                         action.EndInvoke (ar);
1081                                 } catch (Exception ex) {
1082                                         //Console.WriteLine (ex);
1083                                 }
1084                         }, null);
1085                 Thread.Sleep (1000);
1086                 unhandled_exception_endinvoke_2 ();
1087         }
1088
1089         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1090         public static void unhandled_exception_user () {
1091 #if NET_4_5
1092                 System.Threading.Tasks.Task.Factory.StartNew (() => {
1093                                 Throw ();
1094                         });
1095                 Thread.Sleep (10000);
1096 #endif
1097         }
1098
1099         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1100         public static void Throw () {
1101                 throw new Exception ();
1102         }
1103
1104         internal static Delegate create_filter_delegate (Delegate dlg, MethodInfo filter_method)
1105         {
1106                 if (dlg == null)
1107                         throw new ArgumentNullException ();
1108                 if (dlg.Target != null)
1109                         throw new ArgumentException ();
1110                 if (dlg.Method == null)
1111                         throw new ArgumentException ();
1112
1113                 var ret_type = dlg.Method.ReturnType;
1114                 var param_types = dlg.Method.GetParameters ().Select (x => x.ParameterType).ToArray ();
1115
1116                 var dynamic = new DynamicMethod (Guid.NewGuid ().ToString (), ret_type, param_types, typeof (object), true);
1117                 var ig = dynamic.GetILGenerator ();
1118
1119                 LocalBuilder retval = null;
1120                 if (ret_type != typeof (void))
1121                         retval = ig.DeclareLocal (ret_type);
1122
1123                 var label = ig.BeginExceptionBlock ();
1124
1125                 for (int i = 0; i < param_types.Length; i++)
1126                         ig.Emit (OpCodes.Ldarg, i);
1127                 ig.Emit (OpCodes.Call, dlg.Method);
1128
1129                 if (retval != null)
1130                         ig.Emit (OpCodes.Stloc, retval);
1131
1132                 ig.Emit (OpCodes.Leave, label);
1133
1134                 ig.BeginExceptFilterBlock ();
1135
1136                 ig.Emit (OpCodes.Call, filter_method);
1137
1138                 ig.BeginCatchBlock (null);
1139
1140                 ig.Emit (OpCodes.Pop);
1141
1142                 ig.EndExceptionBlock ();
1143
1144                 if (retval != null)
1145                         ig.Emit (OpCodes.Ldloc, retval);
1146
1147                 ig.Emit (OpCodes.Ret);
1148
1149                 return dynamic.CreateDelegate (dlg.GetType ());
1150         }
1151
1152         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1153         static void exception_filter_method () {
1154                 throw new InvalidOperationException ();
1155         }
1156
1157         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1158         static int exception_filter_filter (Exception exc) {
1159                 return 1;
1160         }
1161
1162         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1163         public static void exception_filter () {
1164                 var method = typeof (Tests).GetMethod (
1165                         "exception_filter_method", BindingFlags.NonPublic | BindingFlags.Static);
1166                 var filter_method = typeof (Tests).GetMethod (
1167                         "exception_filter_filter", BindingFlags.NonPublic | BindingFlags.Static);
1168
1169                 var dlg = Delegate.CreateDelegate (typeof (Action), method);
1170
1171                 var wrapper = (Action) create_filter_delegate (dlg, filter_method);
1172
1173                 wrapper ();
1174         }
1175
1176         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1177         public static bool return_true () {
1178                 return true;
1179         }
1180
1181         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1182         public static void exceptions2 () {
1183                 if (return_true ())
1184                         throw new Exception ();
1185                 Console.WriteLine ();
1186         }
1187
1188         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1189         public static void threads () {
1190                 Thread t = new Thread (delegate () {});
1191
1192                 t.Start ();
1193                 t.Join ();
1194         }
1195
1196         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1197         public static void domains () {
1198                 AppDomain domain = AppDomain.CreateDomain ("domain");
1199
1200                 CrossDomain o = (CrossDomain)domain.CreateInstanceAndUnwrap (
1201                                    typeof (CrossDomain).Assembly.FullName, "CrossDomain");
1202
1203                 domains_print_across (o);
1204
1205                 domains_2 (o, new CrossDomain ());
1206
1207                 o.invoke_2 ();
1208
1209                 o.invoke ();
1210
1211                 o.invoke_2 ();
1212
1213                 AppDomain.Unload (domain);
1214
1215                 domains_3 ();
1216
1217                 typeof (Tests).GetMethod ("called_from_invoke").Invoke (null, null);
1218         }
1219
1220         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1221         public static void called_from_invoke () {
1222         }
1223
1224         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1225         public static void domains_2 (object o, object o2) {
1226         }
1227
1228         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1229         public static void domains_print_across (object o) {
1230         }
1231
1232         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1233         public static void domains_3 () {
1234         }
1235
1236         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1237         public static void invoke_in_domain () {
1238         }
1239
1240         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1241         public static void invoke_in_domain_2 () {
1242         }
1243
1244         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1245         public static void dynamic_methods () {
1246                 var m = new DynamicMethod ("dyn_method", typeof (void), new Type []  { typeof (int) }, typeof (object).Module);
1247                 var ig = m.GetILGenerator ();
1248
1249                 ig.Emit (OpCodes.Ldstr, "FOO");
1250                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("dyn_call"));
1251                 ig.Emit (OpCodes.Ret);
1252
1253                 var del = (Action<int>)m.CreateDelegate (typeof (Action<int>));
1254
1255                 del (0);
1256         }
1257
1258         public static void dyn_call (string s) {
1259         }
1260
1261         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1262         public static void ref_emit () {
1263                 AssemblyName assemblyName = new AssemblyName ();
1264                 assemblyName.Name = "foo";
1265
1266                 AssemblyBuilder assembly =
1267                         Thread.GetDomain ().DefineDynamicAssembly (
1268                                                                                                            assemblyName, AssemblyBuilderAccess.RunAndSave);
1269
1270                 ModuleBuilder module = assembly.DefineDynamicModule ("foo.dll");
1271
1272                 TypeBuilder tb = module.DefineType ("foo", TypeAttributes.Public, typeof (object));
1273                 MethodBuilder mb = tb.DefineMethod ("ref_emit_method", MethodAttributes.Public|MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type [] { });
1274                 ILGenerator ig = mb.GetILGenerator ();
1275                 ig.Emit (OpCodes.Ldstr, "FOO");
1276                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("ref_emit_call"));
1277                 ig.Emit (OpCodes.Ret);
1278
1279                 Type t = tb.CreateType ();
1280
1281                 t.GetMethod ("ref_emit_method").Invoke (null, null);
1282         }
1283
1284         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1285         public static void ref_emit_call (string s) {
1286         }
1287
1288         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1289         public static void frames_in_native () {
1290                 Thread.Sleep (500);
1291                 var evt = new ManualResetEvent (false);
1292                 
1293                 object mon = new object ();
1294                 ThreadPool.QueueUserWorkItem (delegate {
1295                                 frames_in_native_2 ();
1296                                 evt.Set ();
1297                         });
1298                 evt.WaitOne ();
1299         }
1300
1301         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1302         static void frames_in_native_2 () {
1303                 frames_in_native_3 ();
1304         }
1305
1306         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1307         static void frames_in_native_3 () {
1308         }
1309
1310         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1311         public static void string_call (string s) {
1312         }
1313
1314         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1315         public static void ss_regress_654694 () {
1316                 if (true) {
1317                         string h = "hi";
1318                         string_call (h);
1319                 }
1320         }
1321
1322         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1323         public static void user () {
1324                 Debugger.Break ();
1325
1326                 Debugger.Log (5, Debugger.IsLogging () ? "A" : "", "B");
1327         }
1328
1329         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1330         public static void type_load () {
1331                 type_load_2 ();
1332         }
1333
1334         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1335         static void type_load_2 () {
1336                 var c1 = new Dictionary<int, int> ();
1337                 c1.ToString ();
1338                 var c = new TypeLoadClass ();
1339                 c.ToString ();
1340                 var c2 = new TypeLoadClass2 ();
1341                 c2.ToString ();
1342         }
1343
1344         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1345         public static void regress () {
1346                 regress_2755 (DateTime.Now);
1347         }
1348
1349         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1350         public static unsafe void regress_2755 (DateTime d) {
1351                 int* buffer = stackalloc int [128];
1352
1353                 regress_2755_2 ();
1354
1355                 int sum = 0;
1356                 for (int i = 0; i < 128; ++i)
1357                         sum += buffer [i];
1358
1359                 regress_2755_3 (sum);
1360         }
1361
1362         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1363         public static void regress_2755_2 () {
1364         }
1365
1366         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1367         public static void regress_2755_3 (int sum) {
1368         }
1369
1370         static object gc_suspend_field;
1371
1372         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1373         static unsafe void set_gc_suspend_field () {
1374                 set_gc_suspend_field_2 ();
1375                 // Clear stack
1376                 int* buffer = stackalloc int [4096];
1377         }
1378
1379         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1380         static void set_gc_suspend_field_2 () {
1381                 gc_suspend_field = new object ();
1382         }
1383
1384         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1385         static void gc_suspend_1 () {
1386         }
1387
1388         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1389         public static void gc_suspend_invoke () {
1390                 gc_suspend_field = null;
1391                 GC.Collect ();
1392                 GC.WaitForPendingFinalizers ();
1393         }
1394
1395         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1396         public static void gc_suspend () {
1397                 set_gc_suspend_field ();
1398                 gc_suspend_1 ();
1399         }
1400
1401         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1402         public static void generic_method<T> () where T : class {
1403         }
1404
1405         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1406         public void evaluate_method_2 () {
1407         }
1408
1409         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1410         public void evaluate_method () {
1411                 field_i = 42;
1412                 evaluate_method_2 ();
1413         }
1414
1415         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1416         static void set_ip_1 () {
1417         }
1418
1419         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1420         static void set_ip_2 () {
1421         }
1422
1423         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1424         public static void set_ip () {
1425                 int i = 0, j;
1426
1427                 i ++;
1428                 i ++;
1429                 set_ip_1 ();
1430                 i ++;
1431                 j = 5;
1432                 set_ip_2 ();
1433         }
1434
1435         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1436         public static void step_filters () {
1437                 ClassWithCctor.cctor_filter ();
1438         }
1439
1440         class ClassWithCctor {
1441                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1442                 static ClassWithCctor () {
1443                         int i = 1;
1444                         int j = 2;
1445                 }
1446
1447                 [MethodImplAttribute (MethodImplOptions.NoInlining)]
1448                 public static void cctor_filter () {
1449                 }
1450         }
1451
1452         public override string virtual_method () {
1453                 return "V2";
1454         }
1455 }
1456
1457 class TypeLoadClass {
1458 }
1459
1460 class TypeLoadClass2 {
1461 }
1462
1463 public class SentinelClass : MarshalByRefObject {
1464 }
1465
1466 public class CrossDomain : MarshalByRefObject
1467 {
1468         SentinelClass printMe = new SentinelClass ();
1469
1470         public void invoke () {
1471                 Tests.invoke_in_domain ();
1472         }
1473
1474         public void invoke_2 () {
1475                 Tests.invoke_in_domain_2 ();
1476         }
1477
1478         public int invoke_3 () {
1479                 return 42;
1480         }
1481 }       
1482
1483 public class Foo
1484 {
1485         public ProcessStartInfo info;
1486 }
1487
1488 // Class used for line number info testing, don't change its layout
1489 public class LineNumbers
1490 {
1491         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1492         public static void ln1 () {
1493                 // Column 3
1494                 ln2 ();
1495                 ln3 ();
1496         }
1497
1498         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1499         public static void ln2 () {
1500         }
1501
1502         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1503         public static void ln3 () {
1504 #pragma warning disable 0219
1505                 int i = 5;
1506 #pragma warning restore 0219
1507                 #line 55 "FOO"
1508         }
1509 }
1510
1511 class LocalReflectClass
1512 {
1513         public static void RunMe ()
1514         {
1515                 var reflectMe = new someClass ();
1516                 reflectMe.someMethod ();
1517         }
1518
1519         class someClass : ContextBoundObject
1520         {
1521                 public object someField;
1522
1523                 public void someMethod ()
1524                 {
1525                 }
1526         }
1527 }
1528
1529