Remove allocations from Dns.* (dotnet/corefx#41061)
[mono-project.git] / mono / tests / stack-overflow.cs
blobd71f9d0719de9a0df9e8f67755a52d7f5481d7ab
1 using System;
2 using System.Collections.Generic;
4 public class Tests {
6 struct A1 {
7 public long a1, a2, a3, a4;
10 struct A2 {
11 public A1 a1, a2, a3, a4;
14 struct A3 {
15 public A2 a1, a2, a3, a4;
18 struct A4 {
19 public A3 a1, a2, a3, a4;
22 struct A5 {
23 public A4 a1, a2, a3, a4;
26 public static int foo () {
27 A5 a5;
29 /* Prevent a5 from being optimized away */
30 a5 = new A5 ();
31 a5.a1.a1.a1.a1.a1 = 5;
33 return foo () + 1;
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 ();
44 public static int Main () {
45 // Try overflow in managed code
46 int count = 0;
47 try {
48 foo ();
50 catch (StackOverflowException) {
51 Console.WriteLine ("Stack overflow caught.");
52 count ++;
54 if (count != 1)
55 return 1;
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++;
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++;
72 if (count != 2)
73 return 2;
75 return 0;