/
appdomain-thread-abort.cs
252 lines (213 loc) · 5.33 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
using System;
using System.Threading;
using System.Runtime.Remoting;
using System.Reflection;
public class JustSomeClass {
}
public class Test2 : ContextBoundObject
{
public void Run () {
Thread.CurrentThread.Abort ();
}
}
public class Test1 : MarshalByRefObject
{
public bool Run () {
AppDomain d = AppDomain.CreateDomain ("foo2");
var t2 = (Test2)d.CreateInstanceAndUnwrap (Assembly.GetExecutingAssembly().FullName, "Test2");
try {
t2.Run ();
} catch (ThreadAbortException ex) {
Thread.ResetAbort ();
return true;
}
return false;
}
}
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 static class Tests
{
static AppDomain domain = AppDomain.CreateDomain ("newdomain");
public static int test_0_abort_other_indirect ()
{
Test test = (Test) domain.CreateInstanceAndUnwrap (typeof (Test).Assembly.FullName, typeof (Test).FullName);
Test testHere = new Test ();
if (!RemotingServices.IsTransparentProxy (test)) {
Console.WriteLine ("test is no proxy");
return 1;
}
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 2;
}
}
return 0;
}
public static int test_0_abort_string ()
{
Test test = (Test) domain.CreateInstanceAndUnwrap (typeof (Test).Assembly.FullName, typeof (Test).FullName);
if (!RemotingServices.IsTransparentProxy (test)) {
Console.WriteLine ("test is no proxy");
return 1;
}
try {
test.abortString ();
Console.WriteLine ("no abort");
return 2;
} catch (ThreadAbortException e) {
object state;
state = e.ExceptionState;
Thread.ResetAbort ();
if (state == null) {
Console.WriteLine ("state is null");
return 3;
} else {
if (RemotingServices.IsTransparentProxy (state)) {
Console.WriteLine ("state is proxy");
return 4;
}
if (!((string)state).Equals ("bla")) {
Console.WriteLine ("state is wrong: " + (string)state);
return 5;
}
}
if (RemotingServices.IsTransparentProxy (e)) {
Console.WriteLine ("exception is proxy");
return 6;
}
if (test.getState () != null) {
Console.WriteLine ("have state");
return 7;
}
}
return 0;
}
public static int test_0_abort_proxy ()
{
Test test = (Test) domain.CreateInstanceAndUnwrap (typeof (Test).Assembly.FullName, typeof (Test).FullName);
if (!RemotingServices.IsTransparentProxy (test)) {
Console.WriteLine ("test is no proxy");
return 1;
}
try {
test.abortProxy ();
Console.WriteLine ("no abort");
return 2;
} catch (ThreadAbortException e) {
object state;
state = e.ExceptionState;
Thread.ResetAbort ();
if (state == null) {
Console.WriteLine ("state is null");
return 3;
} else {
if (!RemotingServices.IsTransparentProxy (state)) {
Console.WriteLine ("state is not proxy");
return 4;
}
if (((Test)state).getInt () != 123) {
Console.WriteLine ("state doesn't work");
return 5;
}
}
if (RemotingServices.IsTransparentProxy (e)) {
Console.WriteLine ("exception is proxy");
return 6;
}
}
return 0;
}
public static int test_0_abort_other ()
{
Test test = (Test) domain.CreateInstanceAndUnwrap (typeof (Test).Assembly.FullName, typeof (Test).FullName);
if (!RemotingServices.IsTransparentProxy (test)) {
Console.WriteLine ("test is no proxy");
return 1;
}
try {
test.abortOther ();
Console.WriteLine ("no abort");
return 2;
} catch (ThreadAbortException e) {
object state = null;
bool stateExc = false;
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 3;
}
if (RemotingServices.IsTransparentProxy (e)) {
Console.WriteLine ("exception is proxy");
return 4;
}
}
return 0;
}
public static int test_0_abort_in_thread ()
{
// #539394
// Calling Thread.Abort () from a remoting call throws a ThreadAbortException which
// cannot be caught because the exception handling code is confused by the domain
// transitions
bool res = false;
Thread thread = new Thread (delegate () {
AppDomain d = AppDomain.CreateDomain ("foo");
var t = (Test1)d.CreateInstanceAndUnwrap (Assembly.GetExecutingAssembly().FullName, "Test1");
res = t.Run ();
});
thread.Start ();
thread.Join ();
if (!res)
return 1;
return 0;
}
public static int Main (string [] args)
{
return TestDriver.RunTests (typeof (Tests), args);
}
}