Implement mono_gc_alloc_fixed on Boehm to be uncollectable. This matches SGen behavio...
[mono.git] / mono / tests / abort-cctor.cs
1 using System;
2 using System.Diagnostics;
3 using System.Threading;
4 using System.Runtime.CompilerServices;
5
6 class Driver
7 {
8         public static ManualResetEvent mre1 = new ManualResetEvent (false);
9         public static ManualResetEvent mre2 = new ManualResetEvent (false);
10
11         class StaticConstructor1
12         {
13                 internal static bool gotToEnd, caughtException;
14                 static StaticConstructor1 ()
15                 {
16                         try {
17                                 Console.WriteLine ("StaticConstructor1.StaticConstructor1 (1)");
18                                 Driver.mre1.Set ();
19                                 var sw = Stopwatch.StartNew ();
20                                 Thread.Sleep (1000);
21                                 sw.Stop ();
22                                 typeof (string).GetMethods ();
23                                 //XXX we assume that if we slept less than 900ms we got aborted
24                                 if (sw.ElapsedMilliseconds < 900)
25                                         throw new Exception ("Bad abort broke our sleep");
26                                 Console.WriteLine ("StaticConstructor1.StaticConstructor1 (2) waited {0}", sw.ElapsedMilliseconds);
27                                 gotToEnd = true;
28                         } catch (Exception e) {
29                                 caughtException = true;
30                                 throw;
31                         }
32                 }
33
34                 public static void Init ()
35                 {
36                         Console.WriteLine ("StaticConstructor1.Init");
37                 }
38         }
39
40         [MethodImplAttribute (MethodImplOptions.NoInlining)]
41         static void IsStaticConstructor1Viable () {
42                 new StaticConstructor1 ();
43                 Console.WriteLine ("Did it get to the end? {0} Did it catch an exception {1}", StaticConstructor1.gotToEnd, StaticConstructor1.caughtException);
44                 if (!StaticConstructor1.gotToEnd) /* the TAE must not land during a .cctor */
45                         Environment.Exit (1);
46                 if (StaticConstructor1.caughtException)
47                         Environment.Exit (1);
48                         
49         }
50
51         static void Test1 ()
52         {
53                 Console.WriteLine ("Test 1:");
54
55                 Driver.mre1.Reset ();
56                 Driver.mre2.Reset ();
57
58                 Thread thread = new Thread (() => {
59                         try {
60                                 StaticConstructor1.Init ();
61                         } catch (Exception e) {
62                                 Console.WriteLine ("StaticConstructor1::init caught exception {0}", e);
63
64                                 if (!(e is ThreadAbortException))
65                                         throw;
66                         }
67                 });
68
69                 thread.Start ();
70
71                 Driver.mre1.WaitOne ();
72
73                 // The ThreadAbortException should land while in
74                 // the StaticConstructor1.cctor. The exception should
75                 // be queued, and be rethrown when exiting the cctor.
76                 thread.Abort ();
77
78                 thread.Join ();
79
80                 //is StaticConstructor1 viable?
81                 try {
82                         IsStaticConstructor1Viable ();
83                         Console.WriteLine ("StaticConstructor1 is viable"); /* a TAE doesn't make a type unusable */
84                 } catch (TypeInitializationException  e) {
85                         Console.WriteLine ("StaticConstructor1 not viable");
86                         Environment.Exit (1);
87                 }
88         }
89
90         class StaticConstructor2Exception : Exception {}
91
92         class StaticConstructor2
93         {
94                 static StaticConstructor2 ()
95                 {
96                         Console.WriteLine ("StaticConstructor2.StaticConstructor2 (1)");
97                         Driver.mre1.Set ();
98                         throw new StaticConstructor2Exception ();
99                         /* Unreachable */
100                         Driver.mre2.Set ();
101                         Console.WriteLine ("StaticConstructor2.StaticConstructor2 (2)");
102                 }
103
104                 public static void Init ()
105                 {
106                         Console.WriteLine ("StaticConstructor2.Init");
107                 }
108         }
109
110         [MethodImplAttribute (MethodImplOptions.NoInlining)]
111         static void IsStaticConstructor2Viable () {
112                 new StaticConstructor2 ();
113         }
114
115
116         static void Test2 ()
117         {
118                 Console.WriteLine ("Test 2:");
119
120                 Driver.mre1.Reset ();
121                 Driver.mre2.Reset ();
122
123                 Thread thread = new Thread (() => {
124                         try {
125                                 StaticConstructor2.Init ();
126                         } catch (TypeInitializationException e) {
127                                 Console.WriteLine (e);
128
129                                 if (!(e.InnerException is StaticConstructor2Exception))
130                                         throw;
131                         }
132                 });
133
134                 thread.Start ();
135
136                 Driver.mre1.WaitOne ();
137
138                 // A InvalidOperationException should be thrown while in
139                 // the StaticConstructor2.cctor. The exception should
140                 // be wrapped in a TypeInitializationException.
141
142                 if (Driver.mre2.WaitOne (500)) {
143                         /* We shouldn't reach Driver.mre.Set () in StaticConstructor2.cctor */
144                         Environment.Exit (1);
145                 }
146
147                 thread.Join ();
148
149                 //is StaticConstructor2 viable?
150                 try {
151                         IsStaticConstructor2Viable ();
152                         Console.WriteLine ("StaticConstructor2 is viable");
153                         /* A regular exception escaping the .cctor makes the type not usable */
154                         Environment.Exit (1);
155                 } catch (TypeInitializationException e) {
156                         Console.WriteLine ("StaticConstructor2 not viable");
157                 }
158
159         }
160
161         class StaticConstructor3
162         {
163                 static StaticConstructor3 ()
164                 {
165                         Console.WriteLine ("StaticConstructor3.StaticConstructor3 (1)");
166                         Driver.mre1.Set ();
167                         Thread.CurrentThread.Abort ();
168                         /* Unreachable */
169                         Driver.mre2.Set ();
170                         Console.WriteLine ("StaticConstructor3.StaticConstructor3 (2)");
171                         Environment.Exit (1);
172                 }
173
174                 public static void Init ()
175                 {
176                         Console.WriteLine ("StaticConstructor3.Init");
177                 }
178         }
179
180         [MethodImplAttribute (MethodImplOptions.NoInlining)]
181         static void IsStaticConstructor3Viable () {
182                 new StaticConstructor3 ();
183         }
184
185         static void Test3 ()
186         {
187                 Console.WriteLine ("Test 3:");
188
189                 Driver.mre1.Reset ();
190                 Driver.mre2.Reset ();
191
192                 Thread thread = new Thread (() => {
193                         try {
194                                 StaticConstructor3.Init ();
195                                 Console.WriteLine ("cctor3 didn't throw?!?!");
196                                 /* StaticConstructor3 self aborted */
197                                 Environment.Exit (1);
198                         } catch (ThreadAbortException e) {
199                                 Console.WriteLine ("TEST 3: aborted {0}", e);
200                         }
201                 });
202
203                 thread.Start ();
204
205                 Driver.mre1.WaitOne ();
206
207                 // A InvalidOperationException should be thrown while in
208                 // the StaticConstructor2.cctor. The exception should
209                 // be wrapped in a TypeInitializationException.
210
211                 thread.Join ();
212
213                 //is StaticConstructor2 viable?
214                 try {
215                         IsStaticConstructor3Viable ();
216                         Console.WriteLine ("StaticConstructor3 is viable");
217                         /* A regular exception escaping the .cctor makes the type not usable */
218                         Environment.Exit (1);
219                 } catch (TypeInitializationException e) {
220                         Console.WriteLine ("StaticConstructor3 not viable");
221                 }
222         }
223
224
225
226
227
228         class StaticConstructor4
229         {
230                 internal static bool gotToEnd, caughtException;
231
232                 static StaticConstructor4 ()
233                 {
234                         try {
235                                 Console.WriteLine ("StaticConstructor4.StaticConstructor4 (1)");
236                                 Driver.mre1.Set ();
237                                 var sw = Stopwatch.StartNew ();
238                                 Thread.Sleep (1000);
239                                 sw.Stop ();
240                                 typeof (string).GetMethods ();
241                                 //XXX we assume that if we slept less than 900ms we got aborted
242                                 if (sw.ElapsedMilliseconds < 900)
243                                         throw new Exception ("Bad abort broke our sleep");
244                                 Console.WriteLine ("StaticConstructor4.StaticConstructor4 (2) waited {0}", sw.ElapsedMilliseconds);
245                                 gotToEnd = true;
246                         } catch (Exception e) {
247                                 caughtException = true;
248                                 throw;
249                         }       
250                 }
251
252                 public static void Init ()
253                 {
254                         Console.WriteLine ("StaticConstructor4.Init");
255                 }
256         }
257
258         static bool got_to_the_end_of_the_finally = false;
259
260         [MethodImplAttribute (MethodImplOptions.NoInlining)]
261         static void IsStaticConstructor4Viable () {
262                 new StaticConstructor4 ();
263                 Console.WriteLine ("IsStaticConstructor4Viable: Did it get to the end? {0} Did it catch an exception {1} and end of the finally block {2}", StaticConstructor4.gotToEnd, StaticConstructor4.caughtException, got_to_the_end_of_the_finally);
264                 if (!StaticConstructor4.gotToEnd) /* the TAE must not land during a .cctor */
265                         Environment.Exit (1);
266                 if (StaticConstructor4.caughtException)
267                         Environment.Exit (1);
268         }
269
270         static void Test4 ()
271         {
272                 Console.WriteLine ("Test 4:");
273
274                 Driver.mre1.Reset ();
275                 Driver.mre2.Reset ();
276
277                 Thread thread = new Thread (() => {
278                         try {
279
280                                 try {
281                                 } finally {
282                                         StaticConstructor4.Init ();
283                                         Console.WriteLine ("Test 4: After the cctor");
284                                         got_to_the_end_of_the_finally = true;
285                                 }
286                         } catch (Exception e) {
287                                 Console.WriteLine ("StaticConstructor4::init caught exception {0}", e);
288                                 if (!(e is ThreadAbortException))
289                                         throw;
290                                 if (!got_to_the_end_of_the_finally)
291                                         throw new Exception ("Test 4: did not get to the end of the cctor");
292                         }
293                 });
294
295                 thread.Start ();
296
297                 Driver.mre1.WaitOne ();
298
299                 // The ThreadAbortException should land while in
300                 // the StaticConstructor4.cctor. The exception should
301                 // be queued, and be rethrown when exiting the cctor.
302                 thread.Abort ();
303
304                 thread.Join ();
305
306                 if (!got_to_the_end_of_the_finally) { 
307                         Console.WriteLine ("Did not get to the end of test 4 cctor");
308                         Environment.Exit (1);
309                 }
310
311                 //is StaticConstructor4viable?
312                 try {
313                         IsStaticConstructor4Viable ();
314                         Console.WriteLine ("StaticConstructor4 is viable"); /* a TAE doesn't make a type unusable */
315                 } catch (TypeInitializationException  e) {
316                         Console.WriteLine ("StaticConstructor4 not viable");
317                         Environment.Exit (1);
318                 }
319         }
320
321
322
323         public static int Main ()
324         {
325                 Test1 ();
326                 Test2 ();
327                 Test3 ();
328                 Test4 ();
329                 Console.WriteLine ("done, all things good");
330                 return 0;
331         }
332 }