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