Merge pull request #620 from knocte/monoservice_playground
[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
27 public enum AnEnum {
28         A = 0,
29         B= 1
30 }
31
32 public sealed class Tests3 {
33         public static void M1 () {
34         }
35
36         static void M2 () {
37         }
38
39         public void M3 () {
40         }
41
42         void M4 () {
43         }
44
45 }
46
47 public static class Tests4 {
48         static Tests4 () {
49         }
50 }
51
52 public class AAttribute : Attribute {
53         public int afield;
54 }
55
56 public class BAttribute : AAttribute {
57         public int bfield;
58 }
59
60 [DebuggerDisplay ("Tests", Name="FOO", Target=typeof (int))]
61 [DebuggerTypeProxy (typeof (Tests))]
62 [BAttribute (afield = 1, bfield = 2)]
63 public class Tests2 {
64         [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
65         public int field_j;
66         public static int static_field_j;
67
68         [DebuggerBrowsableAttribute (DebuggerBrowsableState.Collapsed)]
69         public int AProperty {
70                 get {
71                         return 0;
72                 }
73         }
74
75         public void invoke () {
76         }
77 }
78
79 public struct AStruct {
80         public int i;
81         public string s;
82         public byte k;
83         public IntPtr j;
84
85         [MethodImplAttribute (MethodImplOptions.NoInlining)]
86         public int foo (int val) {
87                 return val;
88         }
89
90         [MethodImplAttribute (MethodImplOptions.NoInlining)]
91         public static int static_foo (int val) {
92                 return val;
93         }
94
95         [MethodImplAttribute (MethodImplOptions.NoInlining)]
96         public int invoke_return_int () {
97                 return i;
98         }
99
100         [MethodImplAttribute (MethodImplOptions.NoInlining)]
101         public static int invoke_static () {
102                 return 5;
103         }
104
105         [MethodImplAttribute (MethodImplOptions.NoInlining)]
106         public IntPtr invoke_return_intptr () {
107                 return j;
108         }
109 }
110
111 public class GClass<T> {
112         public T field;
113         public static T static_field;
114
115         [MethodImplAttribute (MethodImplOptions.NoInlining)]
116         public GClass () {
117         }
118
119         [MethodImplAttribute (MethodImplOptions.NoInlining)]
120         public void bp<T2> () {
121         }
122 }
123
124 public struct GStruct<T> {
125         public T i;
126
127         public int j;
128
129         [MethodImplAttribute (MethodImplOptions.NoInlining)]
130         public int invoke_return_int () {
131                 return j;
132         }
133 }
134
135 interface ITest
136 {
137         void Foo ();
138         void Bar ();
139 }
140
141 interface ITest<T>
142 {
143         void Foo ();
144         void Bar ();
145 }
146
147 class TestIfaces : ITest
148 {
149         void ITest.Foo () {
150         }
151
152         void ITest.Bar () {
153         }
154
155         TestIfaces<int> Baz () {
156                 return null;
157         }
158 }
159
160 class TestIfaces<T> : ITest<T>
161 {
162         void ITest<T>.Foo () {
163         }
164
165         void ITest<T>.Bar () {
166         }
167 }
168
169 public class Tests : TestsBase
170 {
171 #pragma warning disable 0414
172         int field_i;
173         string field_s;
174         AnEnum field_enum;
175         bool field_bool1, field_bool2;
176         char field_char;
177         byte field_byte;
178         sbyte field_sbyte;
179         short field_short;
180         ushort field_ushort;
181         long field_long;
182         ulong field_ulong;
183         float field_float;
184         double field_double;
185         Thread field_class;
186         IntPtr field_intptr;
187         int? field_nullable;
188         static int static_i = 55;
189         static string static_s = "A";
190         public const int literal_i = 56;
191         public const string literal_s = "B";
192         public object child;
193         public AStruct field_struct;
194         public object field_boxed_struct;
195         public GStruct<int> generic_field_struct;
196         [ThreadStatic]
197         public static int tls_i;
198         public static bool is_attached = Debugger.IsAttached;
199
200 #pragma warning restore 0414
201
202         public class NestedClass {
203         }
204
205         public int IntProperty {
206                 get {
207                         return field_i;
208                 }
209                 set {
210                         field_i = value;
211                 }
212         }
213
214         public int ReadOnlyProperty {
215                 get {
216                         return field_i;
217                 }
218         }
219
220         public int this [int index] {
221                 get {
222                         return field_i;
223                 }
224         }
225
226         public static int Main (String[] args) {
227                 tls_i = 42;
228
229                 if (args.Length > 0 && args [0] == "suspend-test")
230                         /* This contains an infinite loop, so execute it conditionally */
231                         suspend ();
232                 if (args.Length >0 && args [0] == "unhandled-exception") {
233                         unhandled_exception ();
234                         return 0;
235                 }
236                 if (args.Length >0 && args [0] == "unhandled-exception-user") {
237                         unhandled_exception_user ();
238                         return 0;
239                 }
240                 breakpoints ();
241                 single_stepping ();
242                 arguments ();
243                 objects ();
244                 objrefs ();
245                 vtypes ();
246                 locals ();
247                 line_numbers ();
248                 type_info ();
249                 assembly_load ();
250                 invoke ();
251                 exceptions ();
252                 exception_filter ();
253                 threads ();
254                 dynamic_methods ();
255                 user ();
256                 type_load ();
257                 regress ();
258                 gc_suspend ();
259                 if (args.Length > 0 && args [0] == "domain-test")
260                         /* This takes a lot of time, so execute it conditionally */
261                         domains ();
262                 if (args.Length > 0 && args [0] == "ref-emit-test")
263                         ref_emit ();
264                 if (args.Length > 0 && args [0] == "frames-in-native")
265                         frames_in_native ();
266                 if (args.Length > 0 && args [0] == "invoke-single-threaded")
267                         new Tests ().invoke_single_threaded ();
268                 return 3;
269         }
270
271         public static void breakpoints () {
272                 /* Call these early so it is JITted by the time a breakpoint is placed on it */
273                 bp3 ();
274                 bp7<int> ();
275                 bp7<string> ();
276
277                 bp1 ();
278                 bp2 ();
279                 bp3 ();
280                 bp4 ();
281                 bp4 ();
282                 bp4 ();
283                 bp5 ();
284                 bp6<string> (new GClass <int> ());
285                 bp7<int> ();
286                 bp7<string> ();
287         }
288
289         [MethodImplAttribute (MethodImplOptions.NoInlining)]
290         public static void bp1 () {
291         }
292
293         [MethodImplAttribute (MethodImplOptions.NoInlining)]
294         public static void bp2 () {
295         }
296
297         [MethodImplAttribute (MethodImplOptions.NoInlining)]
298         public static void bp3 () {
299         }
300
301         [MethodImplAttribute (MethodImplOptions.NoInlining)]
302         public static void bp4 () {
303         }
304
305         [MethodImplAttribute (MethodImplOptions.NoInlining)]
306         public static void bp5 () {
307         }
308
309         [MethodImplAttribute (MethodImplOptions.NoInlining)]
310         public static void bp6<T> (GClass<int> gc) {
311                 gc.bp<int> ();
312         }
313
314         [MethodImplAttribute (MethodImplOptions.NoInlining)]
315         public static void bp7<T> () {
316         }
317
318         [MethodImplAttribute (MethodImplOptions.NoInlining)]
319         public static void single_stepping () {
320                 bool b = true;
321                 ss1 ();
322                 ss2 ();
323                 ss3 ();
324                 ss3_2 ();
325                 ss4 ();
326                 ss5 (new int [] { 1, 2, 3 }, new Func<int, bool> (is_even));
327                 try {
328                         ss6 (b);
329                 } catch {
330                 }
331                 ss7 ();
332                 ss_regress_654694 ();
333         }
334
335         [MethodImplAttribute (MethodImplOptions.NoInlining)]
336         public static void ss1 () {
337         }
338
339         [MethodImplAttribute (MethodImplOptions.NoInlining)]
340         public static void ss2 () {
341         }
342
343         [MethodImplAttribute (MethodImplOptions.NoInlining)]
344         public static int ss3 () {
345                 int sum = 0;
346
347                 for (int i = 0; i < 10; ++i)
348                         sum += i;
349
350                 return sum;
351         }
352
353         [MethodImplAttribute (MethodImplOptions.NoInlining)]
354         public static void ss3_2 () {
355                 ss3_2_2 ();
356         }
357
358         [MethodImplAttribute (MethodImplOptions.NoInlining)]
359         public static void ss3_2_2 () {
360         }
361
362         [MethodImplAttribute (MethodImplOptions.NoInlining)]
363         public static int ss4 () {
364                 ss1 (); ss1 ();
365                 ss2 ();
366                 return 0;
367         }
368
369         [MethodImplAttribute (MethodImplOptions.NoInlining)]
370         public static void ss5 (int[] arr, Func<int, bool> selector) {
371                 // Call into linq which calls back into this assembly
372                 arr.Count (selector);
373         }
374
375         [MethodImplAttribute (MethodImplOptions.NoInlining)]
376         public static void ss6 (bool b) {
377                 if (b) {
378                         ss6_2 ();
379                         throw new Exception ();
380                 }
381         }
382
383         [MethodImplAttribute (MethodImplOptions.NoInlining)]
384         public static void ss6_2 () {
385         }
386
387         [MethodImplAttribute (MethodImplOptions.NoInlining)]
388         public static void ss7 () {
389                 try {
390                         ss7_2 ();
391                         ss7_3 ();
392                 } catch {
393                 }
394                 ss7_2 ();
395         }
396
397         [MethodImplAttribute (MethodImplOptions.NoInlining)]
398         public static void ss7_2 () {
399         }
400
401         [MethodImplAttribute (MethodImplOptions.NoInlining)]
402         public static void ss7_3 () {
403                 throw new Exception ();
404         }
405
406         [MethodImplAttribute (MethodImplOptions.NoInlining)]
407         public static bool is_even (int i) {
408                 return i % 2 == 0;
409         }
410
411         /*
412                 lock (static_s) {
413                         Console.WriteLine ("HIT!");
414                 }
415                 return 0;
416         }
417         */
418
419         [MethodImplAttribute (MethodImplOptions.NoInlining)]
420         public static void arguments () {
421                 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));
422                 int i = 42;
423                 arg2 ("FOO", null, "BLA", ref i, new GClass <int> { field = 42 }, new object ());
424                 Tests t = new Tests () { field_i = 42, field_s = "S" };
425                 t.arg3 ("BLA");
426         }
427
428         [MethodImplAttribute (MethodImplOptions.NoInlining)]
429         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) {
430                 return (int)(sb + b + (bl ? 0 : 1) + s + us + (int)c + i + ui + l + (long)ul + f + d + (int)ip + (int)uip);
431         }
432
433         [MethodImplAttribute (MethodImplOptions.NoInlining)]
434         public static string arg2 (string s, string s3, object o, ref int i, GClass <int> gc, object o2) {
435                 return s + (s3 != null ? "" : "") + o + i + gc.field + o2;
436         }
437
438         [MethodImplAttribute (MethodImplOptions.NoInlining)]
439         public object arg3 (string s) {
440                 return s + s + s + s + this;
441         }
442
443         [MethodImplAttribute (MethodImplOptions.NoInlining)]
444         public static void objects () {
445                 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 };
446                 t.o1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
447                 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 });
448         }
449
450         [MethodImplAttribute (MethodImplOptions.NoInlining)]
451         public object o1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
452                 if (t == null || gc1 == null || gc2 == null)
453                         return null;
454                 else
455                         return this;
456         }
457
458         [MethodImplAttribute (MethodImplOptions.NoInlining)]
459         public static string o2 (string[] s2, int[] s3, int[,] s4, int[,] s5, IList<int> s6) {
460                 return s2 [0] + s3 [0] + s4 [0, 0] + s6 [0];
461         }
462
463         [MethodImplAttribute (MethodImplOptions.NoInlining)]
464         public static void objrefs () {
465                 Tests t = new Tests () {};
466                 set_child (t);
467                 t.objrefs1 ();
468                 t.child = null;
469                 GC.Collect ();
470                 objrefs2 ();
471         }
472
473         [MethodImplAttribute (MethodImplOptions.NoInlining)]
474         public static void set_child (Tests t) {
475                 t.child = new Tests ();
476         }
477
478         [MethodImplAttribute (MethodImplOptions.NoInlining)]
479         public void objrefs1 () {
480         }
481
482         [MethodImplAttribute (MethodImplOptions.NoInlining)]
483         public static void objrefs2 () {
484         }
485
486         public static void vtypes () {
487                 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 }};
488                 AStruct s = new AStruct { i = 44, s = "T", k = 45 };
489                 AStruct[] arr = new AStruct[] { 
490                         new AStruct () { i = 1, s = "S1" },
491                         new AStruct () { i = 2, s = "S2" } };
492                 t.vtypes1 (s, arr);
493                 vtypes2 (s);
494                 vtypes3 (s);
495         }
496
497         [MethodImplAttribute (MethodImplOptions.NoInlining)]
498         public object vtypes1 (AStruct s, AStruct[] arr) {
499                 if (arr != null)
500                         return this;
501                 else
502                         return null;
503         }
504
505         [MethodImplAttribute (MethodImplOptions.NoInlining)]
506         public static void vtypes2 (AStruct s) {
507                 s.foo (5);
508         }
509
510         [MethodImplAttribute (MethodImplOptions.NoInlining)]
511         public static void vtypes3 (AStruct s) {
512                 AStruct.static_foo (5);
513         }
514
515         [MethodImplAttribute (MethodImplOptions.NoInlining)]
516         public static void locals () {
517                 string s = null;
518                 locals1 (null);
519                 locals2<string> (null, 5, "ABC", ref s);
520                 locals3 ();
521                 locals6 ();
522         }
523
524         [MethodImplAttribute (MethodImplOptions.NoInlining)]
525         static void locals11 (double a, ref double b) {
526         }
527
528         [MethodImplAttribute (MethodImplOptions.NoInlining)]
529         public static void locals1 (string[] args) {
530                 long foo = 42;
531
532                 double ri = 1;
533                 locals11 (b: ref ri, a: ri);
534
535                 for (int j = 0; j < 10; ++j) {
536                         foo ++;
537                 }
538         }
539
540         [MethodImplAttribute (MethodImplOptions.NoInlining)]
541         [StateMachine (typeof (int))]
542         public static void locals2<T> (string[] args, int arg, T t, ref string rs) {
543                 long i = 42;
544                 string s = "AB";
545
546                 for (int j = 0; j < 10; ++j) {
547                         if (s != null)
548                                 i ++;
549                         if (t != null)
550                                 i ++;
551                 }
552                 rs = "A";
553         }
554
555         [MethodImplAttribute (MethodImplOptions.NoInlining)]
556         public static void locals3 () {
557                 string s = "B";
558                 s.ToString ();
559
560                 {
561                         long i = 42;
562                         i ++;
563                         locals4 ();
564                 }
565                 {
566                         string i = "A";
567                         i.ToString ();
568                         locals5 ();
569                 }
570                 {
571                         long j = 42;
572                         j ++;
573                 }
574         }
575
576         [MethodImplAttribute (MethodImplOptions.NoInlining)]
577         public static void locals4 () {
578         }
579
580         [MethodImplAttribute (MethodImplOptions.NoInlining)]
581         public static void locals5 () {
582         }
583
584         [MethodImplAttribute (MethodImplOptions.NoInlining)]
585         public static void locals6 () {
586                 int i = 0;
587                 int j = 0;
588                 for (i = 0; i < 10; ++i)
589                         j ++;
590                 sbyte sb = 0;
591                 for (i = 0; i < 10; ++i)
592                         sb ++;
593                 locals6_1 ();
594                 locals6_2 (j);
595                 locals6_3 ();
596                 locals6_4 (j);
597                 locals6_5 ();
598                 locals6_6 (sb);
599         }
600
601         [MethodImplAttribute (MethodImplOptions.NoInlining)]
602         public static void locals6_1 () {
603         }
604
605         [MethodImplAttribute (MethodImplOptions.NoInlining)]
606         public static void locals6_2 (int arg) {
607         }
608
609         [MethodImplAttribute (MethodImplOptions.NoInlining)]
610         public static void locals6_3 () {
611                 // Clobber all registers
612                 int sum = 0, i, j, k, l, m;
613                 for (i = 0; i < 100; ++i)
614                         sum ++;
615                 for (j = 0; j < 100; ++j)
616                         sum ++;
617                 for (k = 0; k < 100; ++k)
618                         sum ++;
619                 for (l = 0; l < 100; ++l)
620                         sum ++;
621                 for (m = 0; m < 100; ++m)
622                         sum ++;
623         }
624
625         [MethodImplAttribute (MethodImplOptions.NoInlining)]
626         public static void locals6_4 (int arg) {
627         }
628
629         [MethodImplAttribute (MethodImplOptions.NoInlining)]
630         public static void locals6_5 () {
631         }
632
633         [MethodImplAttribute (MethodImplOptions.NoInlining)]
634         public static void locals6_6 (int arg) {
635         }
636
637         [MethodImplAttribute (MethodImplOptions.NoInlining)]
638         public static void line_numbers () {
639                 LineNumbers.ln1 ();
640         }
641
642         [MethodImplAttribute (MethodImplOptions.NoInlining)]
643         public static void suspend () {
644                 long i = 5;
645
646                 while (true) {
647                         i ++;
648                 }
649         }
650
651         [MethodImplAttribute (MethodImplOptions.NoInlining)]
652         public static void type_info () {
653                 Tests t = new Tests () { field_i = 42, field_s = "S", base_field_i = 43, base_field_s = "T", field_enum = AnEnum.B };
654                 t.ti1 (new Tests2 () { field_j = 43 }, new GClass <int> { field = 42 }, new GClass <string> { field = "FOO" });
655                 int val = 0;
656                 unsafe {
657                         AStruct s = new AStruct () { i = 42, s = "S", k = 43 };
658
659                         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);
660                 }
661         }
662
663         [MethodImplAttribute (MethodImplOptions.NoInlining)]
664         public object ti1 (Tests2 t, GClass <int> gc1, GClass <string> gc2) {
665                 if (t == null || gc1 == null || gc2 == null)
666                         return null;
667                 else
668                         return this;
669         }
670
671         [MethodImplAttribute (MethodImplOptions.NoInlining)]
672         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) {
673                 return s2 [0] + s3 [0] + s4 [0, 0];
674         }
675
676         [MethodImplAttribute (MethodImplOptions.NoInlining)]
677         public static void assembly_load () {
678                 assembly_load_2 ();
679         }
680
681         [MethodImplAttribute (MethodImplOptions.NoInlining)]
682         public static void assembly_load_2 () {
683                 // This will load System.dll while holding the loader lock
684                 new Foo ();
685         }
686
687         [MethodImplAttribute (MethodImplOptions.NoInlining)]
688         public static void invoke () {
689                 new Tests ().invoke1 (new Tests2 (), new AStruct () { i = 42, j = (IntPtr)43 }, new GStruct<int> { j = 42 });
690                 new Tests ().invoke_ex ();
691         }
692
693         [MethodImplAttribute (MethodImplOptions.NoInlining)]
694         public void invoke1 (Tests2 t, AStruct s, GStruct<int> g) {
695                 invoke2 ();
696         }
697
698         [MethodImplAttribute (MethodImplOptions.NoInlining)]
699         public void invoke2 () {
700         }
701
702         [MethodImplAttribute (MethodImplOptions.NoInlining)]
703         public void invoke_ex () {
704                 invoke_ex_inner ();
705         }
706
707         [MethodImplAttribute (MethodImplOptions.NoInlining)]
708         public void invoke_ex_inner () {
709                 try {
710                         throw new Exception ();
711                 } catch {
712                 }
713         }
714
715         int counter;
716
717         [MethodImplAttribute (MethodImplOptions.NoInlining)]
718         public void invoke_single_threaded () {
719                 // Spawn a thread incrementing a counter
720                 bool finished = false;
721
722                 new Thread (delegate () {
723                                 while (!finished)
724                                         counter ++;
725                 }).Start ();
726
727                 Thread.Sleep (100);
728
729                 invoke_single_threaded_2 ();
730
731                 finished = true;
732         }
733
734         [MethodImplAttribute (MethodImplOptions.NoInlining)]
735         public void invoke_single_threaded_2 () {
736         }
737
738         public void invoke_return_void () {
739         }
740
741         public string invoke_return_ref () {
742                 return "ABC";
743         }
744
745         public object invoke_return_null () {
746                 return null;
747         }
748
749         public int invoke_return_primitive () {
750                 return 42;
751         }
752
753         public int? invoke_return_nullable () {
754                 return 42;
755         }
756
757         public int? invoke_return_nullable_null () {
758                 return null;
759         }
760
761         public void invoke_type_load () {
762                 new Class3 ();
763         }
764
765         class Class3 {
766         }
767
768         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) {
769                 return ub + sb + ss + us + i + ui + l + (long)ul + (int)c + (b ? 1 : 0) + (int)f + (int)d;
770         }
771
772         public int invoke_pass_primitive2 (bool b) {
773                 return b ? 1 : 0;
774         }
775
776         public string invoke_pass_ref (string s) {
777                 return s;
778         }
779
780         public static string invoke_static_pass_ref (string s) {
781                 return s;
782         }
783
784         public static void invoke_static_return_void () {
785         }
786
787         public static void invoke_throws () {
788                 throw new Exception ();
789         }
790
791         [MethodImplAttribute (MethodImplOptions.NoInlining)]
792         public static void exceptions () {
793                 try {
794                         throw new OverflowException ();
795                 } catch (Exception) {
796                 }
797                 try {
798                         throw new OverflowException ();
799                 } catch (Exception) {
800                 }
801                 try {
802                         throw new ArgumentException ();
803                 } catch (Exception) {
804                 }
805                 try {
806                         throw new OverflowException ();
807                 } catch (Exception) {
808                 }
809
810                 object o = null;
811                 try {
812                         o.GetType ();
813                 } catch (Exception) {
814                 }
815
816                 try {
817                         exceptions2 ();
818                 } catch (Exception) {
819                 }
820         }
821
822         [MethodImplAttribute (MethodImplOptions.NoInlining)]
823         public static void unhandled_exception () {
824                 ThreadPool.QueueUserWorkItem (delegate {
825                                 throw new InvalidOperationException ();
826                         });
827                 Thread.Sleep (10000);
828         }
829
830         [MethodImplAttribute (MethodImplOptions.NoInlining)]
831         public static void unhandled_exception_user () {
832 #if NET_4_5
833                 System.Threading.Tasks.Task.Factory.StartNew (() => {
834                                 Throw ();
835                         });
836                 Thread.Sleep (10000);
837 #endif
838         }
839
840         [MethodImplAttribute (MethodImplOptions.NoInlining)]
841         public static void Throw () {
842                 throw new Exception ();
843         }
844
845         internal static Delegate create_filter_delegate (Delegate dlg, MethodInfo filter_method)
846         {
847                 if (dlg == null)
848                         throw new ArgumentNullException ();
849                 if (dlg.Target != null)
850                         throw new ArgumentException ();
851                 if (dlg.Method == null)
852                         throw new ArgumentException ();
853
854                 var ret_type = dlg.Method.ReturnType;
855                 var param_types = dlg.Method.GetParameters ().Select (x => x.ParameterType).ToArray ();
856
857                 var dynamic = new DynamicMethod (Guid.NewGuid ().ToString (), ret_type, param_types, typeof (object), true);
858                 var ig = dynamic.GetILGenerator ();
859
860                 LocalBuilder retval = null;
861                 if (ret_type != typeof (void))
862                         retval = ig.DeclareLocal (ret_type);
863
864                 var label = ig.BeginExceptionBlock ();
865
866                 for (int i = 0; i < param_types.Length; i++)
867                         ig.Emit (OpCodes.Ldarg, i);
868                 ig.Emit (OpCodes.Call, dlg.Method);
869
870                 if (retval != null)
871                         ig.Emit (OpCodes.Stloc, retval);
872
873                 ig.Emit (OpCodes.Leave, label);
874
875                 ig.BeginExceptFilterBlock ();
876
877                 ig.Emit (OpCodes.Call, filter_method);
878
879                 ig.BeginCatchBlock (null);
880
881                 ig.Emit (OpCodes.Pop);
882
883                 ig.EndExceptionBlock ();
884
885                 if (retval != null)
886                         ig.Emit (OpCodes.Ldloc, retval);
887
888                 ig.Emit (OpCodes.Ret);
889
890                 return dynamic.CreateDelegate (dlg.GetType ());
891         }
892
893         [MethodImplAttribute (MethodImplOptions.NoInlining)]
894         static void exception_filter_method () {
895                 throw new InvalidOperationException ();
896         }
897
898         [MethodImplAttribute (MethodImplOptions.NoInlining)]
899         static int exception_filter_filter (Exception exc) {
900                 return 1;
901         }
902
903         [MethodImplAttribute (MethodImplOptions.NoInlining)]
904         public static void exception_filter () {
905                 var method = typeof (Tests).GetMethod (
906                         "exception_filter_method", BindingFlags.NonPublic | BindingFlags.Static);
907                 var filter_method = typeof (Tests).GetMethod (
908                         "exception_filter_filter", BindingFlags.NonPublic | BindingFlags.Static);
909
910                 var dlg = Delegate.CreateDelegate (typeof (Action), method);
911
912                 var wrapper = (Action) create_filter_delegate (dlg, filter_method);
913
914                 wrapper ();
915         }
916
917         [MethodImplAttribute (MethodImplOptions.NoInlining)]
918         public static bool return_true () {
919                 return true;
920         }
921
922         [MethodImplAttribute (MethodImplOptions.NoInlining)]
923         public static void exceptions2 () {
924                 if (return_true ())
925                         throw new Exception ();
926                 Console.WriteLine ();
927         }
928
929         [MethodImplAttribute (MethodImplOptions.NoInlining)]
930         public static void threads () {
931                 Thread t = new Thread (delegate () {});
932
933                 t.Start ();
934                 t.Join ();
935         }
936
937         [MethodImplAttribute (MethodImplOptions.NoInlining)]
938         public static void domains () {
939                 AppDomain domain = AppDomain.CreateDomain ("domain");
940
941                 CrossDomain o = (CrossDomain)domain.CreateInstanceAndUnwrap (
942                                    typeof (CrossDomain).Assembly.FullName, "CrossDomain");
943
944                 domains_2 (o, new CrossDomain ());
945
946                 o.invoke_2 ();
947
948                 o.invoke ();
949
950                 o.invoke_2 ();
951
952                 AppDomain.Unload (domain);
953
954                 domains_3 ();
955         }
956
957         [MethodImplAttribute (MethodImplOptions.NoInlining)]
958         public static void domains_2 (object o, object o2) {
959         }
960
961         [MethodImplAttribute (MethodImplOptions.NoInlining)]
962         public static void domains_3 () {
963         }
964
965         [MethodImplAttribute (MethodImplOptions.NoInlining)]
966         public static void invoke_in_domain () {
967         }
968
969         [MethodImplAttribute (MethodImplOptions.NoInlining)]
970         public static void invoke_in_domain_2 () {
971         }
972
973         [MethodImplAttribute (MethodImplOptions.NoInlining)]
974         public static void dynamic_methods () {
975                 var m = new DynamicMethod ("dyn_method", typeof (void), new Type []  { typeof (int) }, typeof (object).Module);
976                 var ig = m.GetILGenerator ();
977
978                 ig.Emit (OpCodes.Ldstr, "FOO");
979                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("dyn_call"));
980                 ig.Emit (OpCodes.Ret);
981
982                 var del = (Action<int>)m.CreateDelegate (typeof (Action<int>));
983
984                 del (0);
985         }
986
987         public static void dyn_call (string s) {
988         }
989
990         [MethodImplAttribute (MethodImplOptions.NoInlining)]
991         public static void ref_emit () {
992                 AssemblyName assemblyName = new AssemblyName ();
993                 assemblyName.Name = "foo";
994
995                 AssemblyBuilder assembly =
996                         Thread.GetDomain ().DefineDynamicAssembly (
997                                                                                                            assemblyName, AssemblyBuilderAccess.RunAndSave);
998
999                 ModuleBuilder module = assembly.DefineDynamicModule ("foo.dll");
1000
1001                 TypeBuilder tb = module.DefineType ("foo", TypeAttributes.Public, typeof (object));
1002                 MethodBuilder mb = tb.DefineMethod ("ref_emit_method", MethodAttributes.Public|MethodAttributes.Static, CallingConventions.Standard, typeof (void), new Type [] { });
1003                 ILGenerator ig = mb.GetILGenerator ();
1004                 ig.Emit (OpCodes.Ldstr, "FOO");
1005                 ig.Emit (OpCodes.Call, typeof (Tests).GetMethod ("ref_emit_call"));
1006                 ig.Emit (OpCodes.Ret);
1007
1008                 Type t = tb.CreateType ();
1009
1010                 t.GetMethod ("ref_emit_method").Invoke (null, null);
1011         }
1012
1013         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1014         public static void ref_emit_call (string s) {
1015         }
1016
1017         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1018         public static void frames_in_native () {
1019                 Thread.Sleep (500);
1020                 object mon = new object ();
1021                 ThreadPool.QueueUserWorkItem (delegate {
1022                                 frames_in_native_2 ();
1023                                 lock (mon) {
1024                                         Monitor.Pulse (mon);
1025                                 }
1026                         });
1027                 lock (mon) {
1028                         Monitor.Wait (mon);
1029                 }
1030         }
1031
1032         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1033         static void frames_in_native_2 () {
1034                 frames_in_native_3 ();
1035         }
1036
1037         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1038         static void frames_in_native_3 () {
1039         }
1040
1041         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1042         public static void string_call (string s) {
1043         }
1044
1045         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1046         public static void ss_regress_654694 () {
1047                 if (true) {
1048                         string h = "hi";
1049                         string_call (h);
1050                 }
1051         }
1052
1053         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1054         public static void user () {
1055                 Debugger.Break ();
1056
1057                 Debugger.Log (5, Debugger.IsLogging () ? "A" : "", "B");
1058         }
1059
1060         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1061         public static void type_load () {
1062                 type_load_2 ();
1063         }
1064
1065         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1066         static void type_load_2 () {
1067                 var c1 = new Dictionary<int, int> ();
1068                 c1.ToString ();
1069                 var c = new TypeLoadClass ();
1070                 c.ToString ();
1071                 var c2 = new TypeLoadClass2 ();
1072                 c2.ToString ();
1073         }
1074
1075         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1076         public static void regress () {
1077                 regress_2755 (DateTime.Now);
1078         }
1079
1080         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1081         public static unsafe void regress_2755 (DateTime d) {
1082                 int* buffer = stackalloc int [128];
1083
1084                 regress_2755_2 ();
1085
1086                 int sum = 0;
1087                 for (int i = 0; i < 128; ++i)
1088                         sum += buffer [i];
1089
1090                 regress_2755_3 (sum);
1091         }
1092
1093         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1094         public static void regress_2755_2 () {
1095         }
1096
1097         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1098         public static void regress_2755_3 (int sum) {
1099         }
1100
1101         static object gc_suspend_field;
1102
1103         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1104         static unsafe void set_gc_suspend_field () {
1105                 set_gc_suspend_field_2 ();
1106                 // Clear stack
1107                 int* buffer = stackalloc int [4096];
1108         }
1109
1110         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1111         static void set_gc_suspend_field_2 () {
1112                 gc_suspend_field = new object ();
1113         }
1114
1115         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1116         static void gc_suspend_1 () {
1117         }
1118
1119         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1120         public static void gc_suspend_invoke () {
1121                 gc_suspend_field = null;
1122                 GC.Collect ();
1123                 GC.WaitForPendingFinalizers ();
1124         }
1125
1126         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1127         public static void gc_suspend () {
1128                 set_gc_suspend_field ();
1129                 gc_suspend_1 ();
1130         }
1131 }
1132
1133 class TypeLoadClass {
1134 }
1135
1136 class TypeLoadClass2 {
1137 }
1138
1139 public class CrossDomain : MarshalByRefObject
1140 {
1141         public void invoke () {
1142                 Tests.invoke_in_domain ();
1143         }
1144
1145         public void invoke_2 () {
1146                 Tests.invoke_in_domain_2 ();
1147         }
1148
1149         public int invoke_3 () {
1150                 return 42;
1151         }
1152 }       
1153
1154 public class Foo
1155 {
1156         public ProcessStartInfo info;
1157 }
1158
1159 // Class used for line number info testing, don't change its layout
1160 public class LineNumbers
1161 {
1162         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1163         public static void ln1 () {
1164                 // Column 3
1165                 ln2 ();
1166                 ln3 ();
1167         }
1168
1169         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1170         public static void ln2 () {
1171         }
1172
1173         [MethodImplAttribute (MethodImplOptions.NoInlining)]
1174         public static void ln3 () {
1175 #pragma warning disable 0219
1176                 int i = 5;
1177 #pragma warning restore 0219
1178                 #line 55 "FOO"
1179         }
1180 }