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