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