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