/
appdomain-thread-abort.cs
175 lines (155 loc) · 3.73 KB
/
appdomain-thread-abort.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Threading;
using System.Runtime.Remoting;
public class JustSomeClass {
}
public class Test : MarshalByRefObject {
ThreadAbortException exc;
public JustSomeClass other;
public void doThrow (int n, object state) {
if (n <= 0)
Thread.CurrentThread.Abort (state);
else
doThrow (n - 1, state);
}
public void abortProxy () {
doThrow (10, this);
}
public void abortOther () {
other = new JustSomeClass ();
doThrow (10, other);
}
public void abortString () {
try {
doThrow (10, "bla");
} catch (ThreadAbortException e) {
exc = e;
}
}
public void abortOtherIndirect (Test test) {
test.abortOther ();
}
public object getState () {
return exc.ExceptionState;
}
public int getInt () {
return 123;
}
}
public class main {
public static int Main (string [] args) {
AppDomain domain = AppDomain.CreateDomain ("newdomain");
Test test = (Test) domain.CreateInstanceAndUnwrap (typeof (Test).Assembly.FullName, typeof (Test).FullName);
bool didAbort;
Test testHere = new Test ();
if (!RemotingServices.IsTransparentProxy (test)) {
Console.WriteLine ("test is no proxy");
return 5;
}
try {
test.abortOtherIndirect (testHere);
} catch (ThreadAbortException e) {
object state = e.ExceptionState;
Thread.ResetAbort ();
if ((JustSomeClass)state != testHere.other) {
Console.WriteLine ("other class not preserved in state");
return 16;
}
}
try {
didAbort = false;
test.abortString ();
} catch (ThreadAbortException e) {
object state;
state = e.ExceptionState;
Thread.ResetAbort ();
didAbort = true;
if (state == null) {
Console.WriteLine ("state is null");
return 13;
} else {
if (RemotingServices.IsTransparentProxy (state)) {
Console.WriteLine ("state is proxy");
return 1;
}
if (!((string)state).Equals ("bla")) {
Console.WriteLine ("state is wrong: " + (string)state);
return 2;
}
}
if (RemotingServices.IsTransparentProxy (e)) {
Console.WriteLine ("exception is proxy");
return 3;
}
if (test.getState () != null) {
Console.WriteLine ("have state");
return 12;
}
}
if (!didAbort) {
Console.WriteLine ("no abort");
return 4;
}
try {
didAbort = false;
test.abortProxy ();
} catch (ThreadAbortException e) {
object state;
state = e.ExceptionState;
Thread.ResetAbort ();
didAbort = true;
if (state == null) {
Console.WriteLine ("state is null");
return 14;
} else {
if (!RemotingServices.IsTransparentProxy (state)) {
Console.WriteLine ("state is not proxy");
return 6;
}
if (((Test)state).getInt () != 123) {
Console.WriteLine ("state doesn't work");
return 15;
}
}
if (RemotingServices.IsTransparentProxy (e)) {
Console.WriteLine ("exception is proxy");
return 7;
}
}
if (!didAbort) {
Console.WriteLine ("no abort");
return 8;
}
try {
didAbort = false;
test.abortOther ();
} catch (ThreadAbortException e) {
object state = null;
bool stateExc = false;
didAbort = true;
try {
state = e.ExceptionState;
Console.WriteLine ("have state");
} catch (Exception) {
stateExc = true;
/* FIXME: if we put this after the try/catch, mono
quietly quits */
Thread.ResetAbort ();
}
if (!stateExc) {
Console.WriteLine ("no state exception");
return 9;
}
if (RemotingServices.IsTransparentProxy (e)) {
Console.WriteLine ("exception is proxy");
return 10;
}
}
if (!didAbort) {
Console.WriteLine ("no abort");
return 11;
}
Console.WriteLine ("done");
return 0;
}
}