Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / tests / stack-overflow.cs
1 using System;
2 using System.Collections.Generic;
3
4 public class Tests {
5
6         struct A1 {
7                 public long a1, a2, a3, a4;
8         }
9
10         struct A2 {
11                 public A1 a1, a2, a3, a4;
12         }
13
14         struct A3 {
15                 public A2 a1, a2, a3, a4;
16         }
17
18         struct A4 {
19                 public A3 a1, a2, a3, a4;
20         }
21
22         struct A5 {
23                 public A4 a1, a2, a3, a4;
24         }
25
26         public static int foo () {
27                 A5 a5;
28
29                 /* Prevent a5 from being optimized away */
30                 a5 = new A5 ();
31                 a5.a1.a1.a1.a1.a1 = 5;
32
33                 return foo () + 1;
34         }
35
36         // call an icall so we have a big chance to hit the
37         // stack overflow in unmanaged code
38         static void Recurse () {
39                 Type t = typeof (Dictionary<,>);
40                 t.GetGenericArguments ();
41                 Recurse ();
42         }
43
44         public static int Main () {
45                 // Try overflow in managed code
46                 int count = 0;
47                 try {
48                         foo ();
49                 }
50                 catch (StackOverflowException) {
51                         Console.WriteLine ("Stack overflow caught.");
52                         count ++;
53                 }
54                 if (count != 1)
55                         return 1;
56
57                 // Try overflow in unmanaged code
58                 count = 0;
59                 try {
60                         Recurse ();
61                 } catch (Exception ex) {
62                         Console.WriteLine ("Handled: {0}", ex.Message);
63                         count++;
64                 }
65                 // Check that the stack protection is properly restored
66                 try {
67                         Recurse ();
68                 } catch (Exception ex) {
69                         Console.WriteLine ("Again: {0}", ex.Message);
70                         count++;
71                 }
72                 if (count != 2)
73                         return 2;
74
75                 return 0;
76         }
77 }