1 /**
2 * \file
3 * Exception handling
4 *
5 * Authors:
6 * Paolo Molaro (lupus@ximian.com)
7 * Dietmar Maurer (dietmar@ximian.com)
8 * Dick Porter (dick@ximian.com)
9 * Miguel de Icaza (miguel@ximian.com)
10 *
11 * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
12 * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
13 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
14 */
15
16 #include <glib.h>
17 #include <config.h>
18 #include <mono/metadata/environment.h>
19 #include <mono/metadata/exception.h>
20 #include <mono/metadata/exception-internals.h>
21
22 #include <mono/metadata/object-internals.h>
23 #include <mono/metadata/metadata-internals.h>
24 #include <mono/metadata/appdomain.h>
25 #include <mono/metadata/mono-debug.h>
26 #include <mono/utils/mono-error-internals.h>
27 #include <mono/utils/mono-logger-internals.h>
28 #include <string.h>
29
30 #ifdef HAVE_EXECINFO_H
31 #include <execinfo.h>
32 #endif
33
34 static MonoUnhandledExceptionFunc unhandled_exception_hook = NULL;
35 static gpointer unhandled_exception_hook_data = NULL;
36
37 /**
38 * mono_exception_from_name:
39 * \param image the Mono image where to look for the class
40 * \param name_space the namespace for the class
41 * \param name class name
42 *
43 * Creates an exception of the given namespace/name class in the
44 * current domain.
45 *
46 * \returns the initialized exception instance.
47 */
48 MonoException *
mono_exception_from_name(MonoImage * image,const char * name_space,const char * name)49 mono_exception_from_name (MonoImage *image, const char *name_space,
50 const char *name)
51 {
52 return mono_exception_from_name_domain (mono_domain_get (), image, name_space, name);
53 }
54
55 /**
56 * mono_exception_from_name_domain:
57 * \param domain Domain where the return object will be created.
58 * \param image the Mono image where to look for the class
59 * \param name_space the namespace for the class
60 * \param name class name
61 *
62 * Creates an exception object of the given namespace/name class on
63 * the given domain.
64 *
65 * \returns the initialized exception instance.
66 */
67 MonoException *
mono_exception_from_name_domain(MonoDomain * domain,MonoImage * image,const char * name_space,const char * name)68 mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image,
69 const char* name_space, const char *name)
70 {
71 MonoError error;
72 MonoClass *klass;
73 MonoObject *o;
74 MonoDomain *caller_domain = mono_domain_get ();
75
76 klass = mono_class_load_from_name (image, name_space, name);
77
78 o = mono_object_new_checked (domain, klass, &error);
79 mono_error_assert_ok (&error);
80
81 if (domain != caller_domain)
82 mono_domain_set_internal (domain);
83 mono_runtime_object_init_checked (o, &error);
84 mono_error_assert_ok (&error);
85
86 if (domain != caller_domain)
87 mono_domain_set_internal (caller_domain);
88
89 return (MonoException *)o;
90 }
91
92
93 /**
94 * mono_exception_from_token:
95 * \param image the Mono image where to look for the class
96 * \param token The type token of the class
97 *
98 * Creates an exception of the type given by \p token.
99 *
100 * \returns the initialized exception instance.
101 */
102 MonoException *
mono_exception_from_token(MonoImage * image,guint32 token)103 mono_exception_from_token (MonoImage *image, guint32 token)
104 {
105 MonoError error;
106 MonoClass *klass;
107 MonoObject *o;
108
109 klass = mono_class_get_checked (image, token, &error);
110 mono_error_assert_ok (&error);
111
112 o = mono_object_new_checked (mono_domain_get (), klass, &error);
113 mono_error_assert_ok (&error);
114
115 mono_runtime_object_init_checked (o, &error);
116 mono_error_assert_ok (&error);
117
118 return (MonoException *)o;
119 }
120
121 static MonoException *
create_exception_two_strings(MonoClass * klass,MonoString * a1,MonoString * a2,MonoError * error)122 create_exception_two_strings (MonoClass *klass, MonoString *a1, MonoString *a2, MonoError *error)
123 {
124 MonoDomain *domain = mono_domain_get ();
125 MonoMethod *method = NULL;
126 MonoObject *o;
127 int count = 1;
128 gpointer args [2];
129 gpointer iter;
130 MonoMethod *m;
131
132 if (a2 != NULL)
133 count++;
134
135 o = mono_object_new_checked (domain, klass, error);
136 mono_error_assert_ok (error);
137
138 iter = NULL;
139 while ((m = mono_class_get_methods (klass, &iter))) {
140 MonoMethodSignature *sig;
141
142 if (strcmp (".ctor", mono_method_get_name (m)))
143 continue;
144 sig = mono_method_signature (m);
145 if (sig->param_count != count)
146 continue;
147
148 if (sig->params [0]->type != MONO_TYPE_STRING)
149 continue;
150 if (count == 2 && sig->params [1]->type != MONO_TYPE_STRING)
151 continue;
152 method = m;
153 break;
154 }
155
156 args [0] = a1;
157 args [1] = a2;
158
159 mono_runtime_invoke_checked (method, o, args, error);
160 return_val_if_nok (error, NULL);
161
162 return (MonoException *) o;
163 }
164
165 /**
166 * mono_exception_from_name_two_strings:
167 * \param image the Mono image where to look for the class
168 * \param name_space the namespace for the class
169 * \param name class name
170 * \param a1 first string argument to pass
171 * \param a2 second string argument to pass
172 *
173 * Creates an exception from a constructor that takes two string
174 * arguments.
175 *
176 * \returns the initialized exception instance.
177 */
178 MonoException *
mono_exception_from_name_two_strings(MonoImage * image,const char * name_space,const char * name,MonoString * a1,MonoString * a2)179 mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
180 const char *name, MonoString *a1, MonoString *a2)
181 {
182 MonoError error;
183 MonoException *ret;
184
185 ret = mono_exception_from_name_two_strings_checked (image, name_space, name, a1, a2, &error);
186 mono_error_cleanup (&error);
187 return ret;
188 }
189
190 /**
191 * mono_exception_from_name_two_strings_checked:
192 * \param image the Mono image where to look for the class
193 * \param name_space the namespace for the class
194 * \param name class name
195 * \param a1 first string argument to pass
196 * \param a2 second string argument to pass
197 * \param error set on error
198 *
199 * Creates an exception from a constructor that takes two string
200 * arguments.
201 *
202 * \returns the initialized exception instance. On failure returns
203 * NULL and sets \p error.
204 */
205 MonoException *
mono_exception_from_name_two_strings_checked(MonoImage * image,const char * name_space,const char * name,MonoString * a1,MonoString * a2,MonoError * error)206 mono_exception_from_name_two_strings_checked (MonoImage *image, const char *name_space,
207 const char *name, MonoString *a1, MonoString *a2,
208 MonoError *error)
209 {
210 MonoClass *klass;
211
212 error_init (error);
213 klass = mono_class_load_from_name (image, name_space, name);
214
215 return create_exception_two_strings (klass, a1, a2, error);
216 }
217
218 /**
219 * mono_exception_from_name_msg:
220 * \param image the Mono image where to look for the class
221 * \param name_space the namespace for the class
222 * \param name class name
223 * \param msg the message to embed inside the exception
224 *
225 * Creates an exception and initializes its message field.
226 *
227 * \returns the initialized exception instance.
228 */
229 MonoException *
mono_exception_from_name_msg(MonoImage * image,const char * name_space,const char * name,const char * msg)230 mono_exception_from_name_msg (MonoImage *image, const char *name_space,
231 const char *name, const char *msg)
232 {
233 MonoError error;
234 MonoException *ex;
235
236 ex = mono_exception_from_name (image, name_space, name);
237
238 if (msg) {
239 MonoString *msg_str = mono_string_new_checked (mono_object_get_domain ((MonoObject*)ex), msg, &error);
240 mono_error_assert_ok (&error);
241 MONO_OBJECT_SETREF (ex, message, msg_str);
242 }
243
244 return ex;
245 }
246
247 /**
248 * mono_exception_from_token_two_strings:
249 *
250 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
251 * IMAGE and TOKEN.
252 */
253 MonoException *
mono_exception_from_token_two_strings(MonoImage * image,guint32 token,MonoString * a1,MonoString * a2)254 mono_exception_from_token_two_strings (MonoImage *image, guint32 token,
255 MonoString *a1, MonoString *a2)
256 {
257 MonoError error;
258 MonoException *ret;
259 ret = mono_exception_from_token_two_strings_checked (image, token, a1, a2, &error);
260 mono_error_cleanup (&error);
261 return ret;
262 }
263
264 /**
265 * mono_exception_from_token_two_strings_checked:
266 *
267 * Same as mono_exception_from_name_two_strings, but lookup the exception class using
268 * IMAGE and TOKEN.
269 */
270 MonoException *
mono_exception_from_token_two_strings_checked(MonoImage * image,guint32 token,MonoString * a1,MonoString * a2,MonoError * error)271 mono_exception_from_token_two_strings_checked (MonoImage *image, guint32 token,
272 MonoString *a1, MonoString *a2,
273 MonoError *error)
274 {
275 MonoClass *klass;
276
277 error_init (error);
278
279 klass = mono_class_get_checked (image, token, error);
280 mono_error_assert_ok (error); /* FIXME handle the error. */
281
282 return create_exception_two_strings (klass, a1, a2, error);
283 }
284
285 /**
286 * mono_get_exception_divide_by_zero:
287 * \returns a new instance of the \c System.DivideByZeroException
288 */
289 MonoException *
mono_get_exception_divide_by_zero()290 mono_get_exception_divide_by_zero ()
291 {
292 return mono_exception_from_name (mono_get_corlib (), "System",
293 "DivideByZeroException");
294 }
295
296 /**
297 * mono_get_exception_security:
298 * \returns a new instance of the \c System.Security.SecurityException
299 */
300 MonoException *
mono_get_exception_security()301 mono_get_exception_security ()
302 {
303 return mono_exception_from_name (mono_get_corlib (), "System.Security",
304 "SecurityException");
305 }
306
307 /**
308 * mono_get_exception_thread_abort:
309 * \returns a new instance of the \c System.Threading.ThreadAbortException
310 */
311 MonoException *
mono_get_exception_thread_abort()312 mono_get_exception_thread_abort ()
313 {
314 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
315 "ThreadAbortException");
316 }
317
318 /**
319 * mono_get_exception_thread_interrupted:
320 * \returns a new instance of the \c System.Threading.ThreadInterruptedException
321 */
322 MonoException *
mono_get_exception_thread_interrupted()323 mono_get_exception_thread_interrupted ()
324 {
325 return mono_exception_from_name (mono_get_corlib (), "System.Threading",
326 "ThreadInterruptedException");
327 }
328
329 /**
330 * mono_get_exception_arithmetic:
331 * \returns a new instance of the \c System.ArithmeticException
332 */
333 MonoException *
mono_get_exception_arithmetic()334 mono_get_exception_arithmetic ()
335 {
336 return mono_exception_from_name (mono_get_corlib (), "System",
337 "ArithmeticException");
338 }
339
340 /**
341 * mono_get_exception_overflow:
342 * \returns a new instance of the \c System.OverflowException
343 */
344 MonoException *
mono_get_exception_overflow()345 mono_get_exception_overflow ()
346 {
347 return mono_exception_from_name (mono_get_corlib (), "System",
348 "OverflowException");
349 }
350
351 /**
352 * mono_get_exception_null_reference:
353 * \returns a new instance of the \c System.NullReferenceException
354 */
355 MonoException *
mono_get_exception_null_reference()356 mono_get_exception_null_reference ()
357 {
358 return mono_exception_from_name (mono_get_corlib (), "System",
359 "NullReferenceException");
360 }
361
362 /**
363 * mono_get_exception_execution_engine:
364 * \param msg the message to pass to the user
365 * \returns a new instance of the \c System.ExecutionEngineException
366 */
367 MonoException *
mono_get_exception_execution_engine(const char * msg)368 mono_get_exception_execution_engine (const char *msg)
369 {
370 return mono_exception_from_name_msg (mono_get_corlib (), "System", "ExecutionEngineException", msg);
371 }
372
373 /**
374 * mono_get_exception_serialization:
375 * \param msg the message to pass to the user
376 * \returns a new instance of the \c System.Runtime.Serialization.SerializationException
377 */
378 MonoException *
mono_get_exception_serialization(const char * msg)379 mono_get_exception_serialization (const char *msg)
380 {
381 return mono_exception_from_name_msg (mono_get_corlib (), "System.Runtime.Serialization", "SerializationException", msg);
382 }
383
384 /**
385 * mono_get_exception_invalid_cast:
386 * \returns a new instance of the \c System.InvalidCastException
387 */
388 MonoException *
mono_get_exception_invalid_cast()389 mono_get_exception_invalid_cast ()
390 {
391 return mono_exception_from_name (mono_get_corlib (), "System", "InvalidCastException");
392 }
393
394 /**
395 * mono_get_exception_invalid_operation:
396 * \param msg the message to pass to the user
397 * \returns a new instance of the \c System.InvalidOperationException
398 */
399 MonoException *
mono_get_exception_invalid_operation(const char * msg)400 mono_get_exception_invalid_operation (const char *msg)
401 {
402 return mono_exception_from_name_msg (mono_get_corlib (), "System",
403 "InvalidOperationException", msg);
404 }
405
406 /**
407 * mono_get_exception_index_out_of_range:
408 * \returns a new instance of the \c System.IndexOutOfRangeException
409 */
410 MonoException *
mono_get_exception_index_out_of_range()411 mono_get_exception_index_out_of_range ()
412 {
413 return mono_exception_from_name (mono_get_corlib (), "System",
414 "IndexOutOfRangeException");
415 }
416
417 /**
418 * mono_get_exception_array_type_mismatch:
419 * \returns a new instance of the \c System.ArrayTypeMismatchException
420 */
421 MonoException *
mono_get_exception_array_type_mismatch()422 mono_get_exception_array_type_mismatch ()
423 {
424 return mono_exception_from_name (mono_get_corlib (), "System",
425 "ArrayTypeMismatchException");
426 }
427
428 /**
429 * mono_get_exception_type_load:
430 * \param class_name the name of the class that could not be loaded
431 * \param assembly_name the assembly where the class was looked up.
432 * \returns a new instance of the \c System.TypeLoadException
433 */
434 MonoException *
mono_get_exception_type_load(MonoString * class_name,char * assembly_name)435 mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
436 {
437 MonoError error;
438 MonoString *s = NULL;
439 if (assembly_name) {
440 s = mono_string_new_checked (mono_domain_get (), assembly_name, &error);
441 mono_error_assert_ok (&error);
442 } else
443 s = mono_string_empty (mono_domain_get ());
444
445 MonoException *ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
446 "TypeLoadException", class_name, s, &error);
447 mono_error_assert_ok (&error);
448 return ret;
449 }
450
451 /**
452 * mono_get_exception_not_implemented:
453 * \param msg the message to pass to the user
454 * \returns a new instance of the \c System.NotImplementedException
455 */
456 MonoException *
mono_get_exception_not_implemented(const char * msg)457 mono_get_exception_not_implemented (const char *msg)
458 {
459 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotImplementedException", msg);
460 }
461
462 /**
463 * mono_get_exception_not_supported:
464 * \param msg the message to pass to the user
465 * \returns a new instance of the \c System.NotSupportedException
466 */
467 MonoException *
mono_get_exception_not_supported(const char * msg)468 mono_get_exception_not_supported (const char *msg)
469 {
470 return mono_exception_from_name_msg (mono_get_corlib (), "System", "NotSupportedException", msg);
471 }
472
473 /**
474 * mono_get_exception_missing_method:
475 * \param class_name the class where the lookup was performed.
476 * \param member_name the name of the missing method.
477 * \returns a new instance of the \c System.MissingMethodException
478 */
479 MonoException *
mono_get_exception_missing_method(const char * class_name,const char * member_name)480 mono_get_exception_missing_method (const char *class_name, const char *member_name)
481 {
482 MonoError error;
483 MonoString *s1 = mono_string_new_checked (mono_domain_get (), class_name, &error);
484 mono_error_assert_ok (&error);
485 MonoString *s2 = mono_string_new_checked (mono_domain_get (), member_name, &error);
486 mono_error_assert_ok (&error);
487
488 MonoException *ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
489 "MissingMethodException", s1, s2, &error);
490 mono_error_assert_ok (&error);
491 return ret;
492 }
493
494 /**
495 * mono_get_exception_missing_field:
496 * \param class_name the class where the lookup was performed
497 * \param member_name the name of the missing method.
498 * \returns a new instance of the \c System.MissingFieldException
499 */
500 MonoException *
mono_get_exception_missing_field(const char * class_name,const char * member_name)501 mono_get_exception_missing_field (const char *class_name, const char *member_name)
502 {
503 MonoError error;
504 MonoString *s1 = mono_string_new_checked (mono_domain_get (), class_name, &error);
505 mono_error_assert_ok (&error);
506 MonoString *s2 = mono_string_new_checked (mono_domain_get (), member_name, &error);
507 mono_error_assert_ok (&error);
508
509 MonoException *ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
510 "MissingFieldException", s1, s2, &error);
511 mono_error_assert_ok (&error);
512 return ret;
513 }
514
515 /**
516 * mono_get_exception_argument_null:
517 * \param arg the name of the argument that is null
518 * \returns a new instance of the \c System.ArgumentNullException
519 */
520 MonoException*
mono_get_exception_argument_null(const char * arg)521 mono_get_exception_argument_null (const char *arg)
522 {
523 MonoException *ex;
524
525 ex = mono_exception_from_name (
526 mono_get_corlib (), "System", "ArgumentNullException");
527
528 if (arg) {
529 MonoError error;
530 MonoArgumentException *argex = (MonoArgumentException *)ex;
531 MonoString *arg_str = mono_string_new_checked (mono_object_get_domain ((MonoObject*)ex), arg, &error);
532 mono_error_assert_ok (&error);
533 MONO_OBJECT_SETREF (argex, param_name, arg_str);
534 }
535
536 return ex;
537 }
538
539 /**
540 * mono_get_exception_argument:
541 * \param arg the name of the invalid argument.
542 * \returns a new instance of the \c System.ArgumentException
543 */
544 MonoException *
mono_get_exception_argument(const char * arg,const char * msg)545 mono_get_exception_argument (const char *arg, const char *msg)
546 {
547 MonoException *ex;
548
549 ex = mono_exception_from_name_msg (
550 mono_get_corlib (), "System", "ArgumentException", msg);
551
552 if (arg) {
553 MonoError error;
554 MonoArgumentException *argex = (MonoArgumentException *)ex;
555 MonoString *arg_str = mono_string_new_checked (mono_object_get_domain ((MonoObject*)ex), arg, &error);
556 mono_error_assert_ok (&error);
557 MONO_OBJECT_SETREF (argex, param_name, arg_str);
558 }
559
560 return ex;
561 }
562
563 /**
564 * mono_get_exception_argument_out_of_range:
565 * \param arg the name of the out of range argument.
566 * \returns a new instance of the \c System.ArgumentOutOfRangeException
567 */
568 MonoException *
mono_get_exception_argument_out_of_range(const char * arg)569 mono_get_exception_argument_out_of_range (const char *arg)
570 {
571 MonoException *ex;
572
573 ex = mono_exception_from_name (
574 mono_get_corlib (), "System", "ArgumentOutOfRangeException");
575
576 if (arg) {
577 MonoError error;
578 MonoArgumentException *argex = (MonoArgumentException *)ex;
579 MonoString *arg_str = mono_string_new_checked (mono_object_get_domain ((MonoObject*)ex), arg, &error);
580 mono_error_assert_ok (&error);
581 MONO_OBJECT_SETREF (argex, param_name, arg_str);
582 }
583
584 return ex;
585 }
586
587 /**
588 * mono_get_exception_thread_state:
589 * \param msg the message to present to the user
590 * \returns a new instance of the \c System.Threading.ThreadStateException
591 */
592 MonoException *
mono_get_exception_thread_state(const char * msg)593 mono_get_exception_thread_state (const char *msg)
594 {
595 return mono_exception_from_name_msg (
596 mono_get_corlib (), "System.Threading", "ThreadStateException", msg);
597 }
598
599 /**
600 * mono_get_exception_io:
601 * \param msg the message to present to the user
602 * \returns a new instance of the \c System.IO.IOException
603 */
604 MonoException *
mono_get_exception_io(const char * msg)605 mono_get_exception_io (const char *msg)
606 {
607 return mono_exception_from_name_msg (
608 mono_get_corlib (), "System.IO", "IOException", msg);
609 }
610
611 /**
612 * mono_get_exception_file_not_found:
613 * \param fname the name of the file not found.
614 * \returns a new instance of the \c System.IO.FileNotFoundException
615 */
616 MonoException *
mono_get_exception_file_not_found(MonoString * fname)617 mono_get_exception_file_not_found (MonoString *fname)
618 {
619 MonoError error;
620 MonoException *ret = mono_exception_from_name_two_strings_checked (
621 mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname, &error);
622 mono_error_assert_ok (&error);
623 return ret;
624 }
625
626 /**
627 * mono_get_exception_file_not_found2:
628 * \param msg an informative message for the user.
629 * \param fname the name of the file not found.
630 * \returns a new instance of the \c System.IO.FileNotFoundException
631 */
632 MonoException *
mono_get_exception_file_not_found2(const char * msg,MonoString * fname)633 mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
634 {
635 MonoError error;
636 MonoString *s = NULL;
637 if (msg) {
638 s = mono_string_new_checked (mono_domain_get (), msg, &error);
639 mono_error_assert_ok (&error);
640 }
641
642 MonoException *ret = mono_exception_from_name_two_strings_checked (
643 mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname, &error);
644 mono_error_assert_ok (&error);
645 return ret;
646 }
647
648 /**
649 * mono_get_exception_type_initialization:
650 * \param type_name the name of the type that failed to initialize.
651 * \param inner the inner exception.
652 * \returns a new instance of the \c System.TypeInitializationException
653 */
654 MonoException *
mono_get_exception_type_initialization(const gchar * type_name,MonoException * inner)655 mono_get_exception_type_initialization (const gchar *type_name, MonoException *inner)
656 {
657 MonoError error;
658 MonoException *ret = mono_get_exception_type_initialization_checked (type_name, inner, &error);
659 if (!is_ok (&error)) {
660 mono_error_cleanup (&error);
661 return NULL;
662 }
663
664 return ret;
665 }
666
667 MonoException *
mono_get_exception_type_initialization_checked(const gchar * type_name,MonoException * inner,MonoError * error)668 mono_get_exception_type_initialization_checked (const gchar *type_name, MonoException *inner, MonoError *error)
669 {
670 MonoClass *klass;
671 gpointer args [2];
672 MonoObject *exc;
673 MonoMethod *method;
674 gpointer iter;
675
676 error_init (error);
677
678 klass = mono_class_load_from_name (mono_get_corlib (), "System", "TypeInitializationException");
679
680 mono_class_init (klass);
681
682 iter = NULL;
683 while ((method = mono_class_get_methods (klass, &iter))) {
684 if (!strcmp (".ctor", mono_method_get_name (method))) {
685 MonoMethodSignature *sig = mono_method_signature (method);
686
687 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_STRING && mono_class_from_mono_type (sig->params [1]) == mono_defaults.exception_class)
688 break;
689 }
690 method = NULL;
691 }
692 g_assert (method);
693
694 MonoString *type_name_str = mono_string_new_checked (mono_domain_get (), type_name, error);
695 mono_error_assert_ok (error);
696 args [0] = type_name_str;
697 args [1] = inner;
698
699 exc = mono_object_new_checked (mono_domain_get (), klass, error);
700 mono_error_assert_ok (error);
701
702 mono_runtime_invoke_checked (method, exc, args, error);
703 return_val_if_nok (error, NULL);
704
705 return (MonoException *) exc;
706 }
707
708 /**
709 * mono_get_exception_synchronization_lock:
710 * \param inner the inner exception.
711 * \returns a new instance of the \c System.SynchronizationLockException
712 */
713 MonoException *
mono_get_exception_synchronization_lock(const char * msg)714 mono_get_exception_synchronization_lock (const char *msg)
715 {
716 return mono_exception_from_name_msg (mono_get_corlib (), "System.Threading", "SynchronizationLockException", msg);
717 }
718
719 /**
720 * mono_get_exception_cannot_unload_appdomain:
721 * \param inner the inner exception.
722 * \returns a new instance of the \c System.CannotUnloadAppDomainException
723 */
724 MonoException *
mono_get_exception_cannot_unload_appdomain(const char * msg)725 mono_get_exception_cannot_unload_appdomain (const char *msg)
726 {
727 return mono_exception_from_name_msg (mono_get_corlib (), "System", "CannotUnloadAppDomainException", msg);
728 }
729
730 /**
731 * mono_get_exception_appdomain_unloaded
732 * \returns a new instance of the \c System.AppDomainUnloadedException
733 */
734 MonoException *
mono_get_exception_appdomain_unloaded(void)735 mono_get_exception_appdomain_unloaded (void)
736 {
737 return mono_exception_from_name (mono_get_corlib (), "System", "AppDomainUnloadedException");
738 }
739
740 /**
741 * mono_get_exception_bad_image_format:
742 * \param msg an informative message for the user.
743 * \returns a new instance of the \c System.BadImageFormatException
744 */
745 MonoException *
mono_get_exception_bad_image_format(const char * msg)746 mono_get_exception_bad_image_format (const char *msg)
747 {
748 return mono_exception_from_name_msg (mono_get_corlib (), "System", "BadImageFormatException", msg);
749 }
750
751 /**
752 * mono_get_exception_bad_image_format2:
753 * \param msg an informative message for the user.
754 * \param fname The full name of the file with the invalid image.
755 * \returns a new instance of the \c System.BadImageFormatException
756 */
757 MonoException *
mono_get_exception_bad_image_format2(const char * msg,MonoString * fname)758 mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
759 {
760 MonoError error;
761 MonoString *s = NULL;
762
763 if (msg) {
764 s = mono_string_new_checked (mono_domain_get (), msg, &error);
765 mono_error_assert_ok (&error);
766 }
767
768 MonoException *ret = mono_exception_from_name_two_strings_checked (
769 mono_get_corlib (), "System", "BadImageFormatException", s, fname, &error);
770 mono_error_assert_ok (&error);
771 return ret;
772 }
773
774 /**
775 * mono_get_exception_stack_overflow:
776 * \returns a new instance of the \c System.StackOverflowException
777 */
778 MonoException *
mono_get_exception_stack_overflow(void)779 mono_get_exception_stack_overflow (void)
780 {
781 return mono_exception_from_name (mono_get_corlib (), "System", "StackOverflowException");
782 }
783
784 /**
785 * mono_get_exception_out_of_memory:
786 * \returns a new instance of the \c System.OutOfMemoryException
787 */
788 MonoException *
mono_get_exception_out_of_memory(void)789 mono_get_exception_out_of_memory (void)
790 {
791 return mono_exception_from_name (mono_get_corlib (), "System", "OutOfMemoryException");
792 }
793
794 /**
795 * mono_get_exception_field_access:
796 * \returns a new instance of the \c System.FieldAccessException
797 */
798 MonoException *
mono_get_exception_field_access(void)799 mono_get_exception_field_access (void)
800 {
801 return mono_exception_from_name (mono_get_corlib (), "System", "FieldAccessException");
802 }
803
804 /**
805 * mono_get_exception_field_access2:
806 * \param msg an informative message for the user.
807 * \returns a new instance of the \c System.FieldAccessException
808 */
809 MonoException *
mono_get_exception_field_access_msg(const char * msg)810 mono_get_exception_field_access_msg (const char *msg)
811 {
812 return mono_exception_from_name_msg (mono_get_corlib (), "System", "FieldAccessException", msg);
813 }
814
815 /**
816 * mono_get_exception_method_access:
817 * \returns a new instance of the \c System.MethodAccessException
818 */
819 MonoException *
mono_get_exception_method_access(void)820 mono_get_exception_method_access (void)
821 {
822 return mono_exception_from_name (mono_get_corlib (), "System", "MethodAccessException");
823 }
824
825 /**
826 * mono_get_exception_method_access2:
827 * \param msg an informative message for the user.
828 * \returns a new instance of the \c System.MethodAccessException
829 */
830 MonoException *
mono_get_exception_method_access_msg(const char * msg)831 mono_get_exception_method_access_msg (const char *msg)
832 {
833 return mono_exception_from_name_msg (mono_get_corlib (), "System", "MethodAccessException", msg);
834 }
835
836 /**
837 * mono_get_exception_reflection_type_load:
838 * \param types an array of types that were defined in the moduled loaded.
839 * \param exceptions an array of exceptions that were thrown during the type loading.
840 * \returns a new instance of the \c System.Reflection.ReflectionTypeLoadException
841 */
842 MonoException *
mono_get_exception_reflection_type_load(MonoArray * types_raw,MonoArray * exceptions_raw)843 mono_get_exception_reflection_type_load (MonoArray *types_raw, MonoArray *exceptions_raw)
844 {
845 HANDLE_FUNCTION_ENTER ();
846 MonoError error;
847 MONO_HANDLE_DCL (MonoArray, types);
848 MONO_HANDLE_DCL (MonoArray, exceptions);
849 MonoExceptionHandle ret = mono_get_exception_reflection_type_load_checked (types, exceptions, &error);
850 if (is_ok (&error)) {
851 mono_error_cleanup (&error);
852 ret = MONO_HANDLE_CAST (MonoException, NULL_HANDLE);
853 goto leave;
854 }
855
856 leave:
857 HANDLE_FUNCTION_RETURN_OBJ (ret);
858
859 }
860
861 MonoExceptionHandle
mono_get_exception_reflection_type_load_checked(MonoArrayHandle types,MonoArrayHandle exceptions,MonoError * error)862 mono_get_exception_reflection_type_load_checked (MonoArrayHandle types, MonoArrayHandle exceptions, MonoError *error)
863 {
864 MonoClass *klass;
865 MonoMethod *method;
866 gpointer iter;
867
868 error_init (error);
869
870 klass = mono_class_load_from_name (mono_get_corlib (), "System.Reflection", "ReflectionTypeLoadException");
871
872 mono_class_init (klass);
873
874 /* Find the Type[], Exception[] ctor */
875 iter = NULL;
876 while ((method = mono_class_get_methods (klass, &iter))) {
877 if (!strcmp (".ctor", mono_method_get_name (method))) {
878 MonoMethodSignature *sig = mono_method_signature (method);
879
880 if (sig->param_count == 2 && sig->params [0]->type == MONO_TYPE_SZARRAY && sig->params [1]->type == MONO_TYPE_SZARRAY)
881 break;
882 }
883 method = NULL;
884 }
885 g_assert (method);
886
887 MonoExceptionHandle exc = MONO_HANDLE_NEW (MonoException, mono_object_new_checked (mono_domain_get (), klass, error));
888 mono_error_assert_ok (error);
889
890 gpointer args [2];
891 args [0] = MONO_HANDLE_RAW (types);
892 args [1] = MONO_HANDLE_RAW (exceptions);
893
894 mono_runtime_invoke_checked (method, MONO_HANDLE_RAW (exc), args, error);
895 return_val_if_nok (error, MONO_HANDLE_CAST (MonoException, NULL_HANDLE));
896
897 return exc;
898 }
899
900 /**
901 * mono_get_exception_runtime_wrapped:
902 */
903 MonoException *
mono_get_exception_runtime_wrapped(MonoObject * wrapped_exception)904 mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception)
905 {
906 MonoError error;
907 MonoException *ret = mono_get_exception_runtime_wrapped_checked (wrapped_exception, &error);
908 if (!is_ok (&error)) {
909 mono_error_cleanup (&error);
910 return NULL;
911 }
912
913 return ret;
914 }
915
916 MonoException *
mono_get_exception_runtime_wrapped_checked(MonoObject * wrapped_exception,MonoError * error)917 mono_get_exception_runtime_wrapped_checked (MonoObject *wrapped_exception, MonoError *error)
918 {
919 MonoClass *klass;
920 MonoObject *o;
921 MonoMethod *method;
922 MonoDomain *domain = mono_domain_get ();
923 gpointer params [16];
924
925 klass = mono_class_load_from_name (mono_get_corlib (), "System.Runtime.CompilerServices", "RuntimeWrappedException");
926
927 o = mono_object_new_checked (domain, klass, error);
928 mono_error_assert_ok (error);
929 g_assert (o != NULL);
930
931 method = mono_class_get_method_from_name (klass, ".ctor", 1);
932 g_assert (method);
933
934 params [0] = wrapped_exception;
935
936 mono_runtime_invoke_checked (method, o, params, error);
937 return_val_if_nok (error, NULL);
938
939 return (MonoException *)o;
940 }
941
942 static gboolean
append_frame_and_continue(MonoMethod * method,gpointer ip,size_t native_offset,gboolean managed,gpointer user_data)943 append_frame_and_continue (MonoMethod *method, gpointer ip, size_t native_offset, gboolean managed, gpointer user_data)
944 {
945 MonoDomain *domain = mono_domain_get ();
946 GString *text = (GString*)user_data;
947
948 if (method) {
949 char *msg = mono_debug_print_stack_frame (method, native_offset, domain);
950 g_string_append_printf (text, "%s\n", msg);
951 g_free (msg);
952 } else {
953 g_string_append_printf (text, "<unknown native frame 0x%x>\n", ip);
954 }
955
956 return FALSE;
957 }
958
959 char *
mono_exception_get_managed_backtrace(MonoException * exc)960 mono_exception_get_managed_backtrace (MonoException *exc)
961 {
962 GString *text;
963
964 text = g_string_new_len (NULL, 20);
965
966 if (!mono_get_eh_callbacks ()->mono_exception_walk_trace (exc, append_frame_and_continue, text))
967 g_string_append (text, "managed backtrace not available\n");
968
969 return g_string_free (text, FALSE);
970 }
971
972 char *
mono_exception_handle_get_native_backtrace(MonoExceptionHandle exc)973 mono_exception_handle_get_native_backtrace (MonoExceptionHandle exc)
974 {
975 #ifdef HAVE_BACKTRACE_SYMBOLS
976 MonoDomain *domain;
977 MonoArrayHandle arr = MONO_HANDLE_NEW(MonoArray, NULL);
978 int i, len;
979 GString *text;
980 char **messages;
981
982 MONO_HANDLE_GET (arr, exc, native_trace_ips);
983
984 if (MONO_HANDLE_IS_NULL(arr))
985 return g_strdup ("");
986 domain = mono_domain_get ();
987 len = mono_array_handle_length (arr);
988 text = g_string_new_len (NULL, len * 20);
989 uint32_t gchandle;
990 void *addr = MONO_ARRAY_HANDLE_PIN (arr, gpointer, 0, &gchandle);
991 MONO_ENTER_GC_SAFE;
992 messages = backtrace_symbols (addr, len);
993 MONO_EXIT_GC_SAFE;
994 mono_gchandle_free (gchandle);
995
996 for (i = 0; i < len; ++i) {
997 gpointer ip;
998 MONO_HANDLE_ARRAY_GETVAL (ip, arr, gpointer, i);
999 MonoJitInfo *ji = mono_jit_info_table_find (mono_domain_get (), (char *)ip);
1000 if (ji) {
1001 char *msg = mono_debug_print_stack_frame (mono_jit_info_get_method (ji), (char*)ip - (char*)ji->code_start, domain);
1002 g_string_append_printf (text, "%s\n", msg);
1003 g_free (msg);
1004 } else {
1005 g_string_append_printf (text, "%s\n", messages [i]);
1006 }
1007 }
1008
1009 g_free (messages);
1010 return g_string_free (text, FALSE);
1011 #else
1012 return g_strdup ("");
1013 #endif
1014 }
1015
1016 MonoStringHandle
ves_icall_Mono_Runtime_GetNativeStackTrace(MonoExceptionHandle exc,MonoError * error)1017 ves_icall_Mono_Runtime_GetNativeStackTrace (MonoExceptionHandle exc, MonoError *error)
1018 {
1019 char *trace;
1020 MonoStringHandle res;
1021 error_init (error);
1022
1023 if (!exc) {
1024 mono_error_set_argument_null (error, "exception", "");
1025 return NULL_HANDLE_STRING;
1026 }
1027
1028 trace = mono_exception_handle_get_native_backtrace (exc);
1029 res = mono_string_new_handle (mono_domain_get (), trace, error);
1030 g_free (trace);
1031 return res;
1032 }
1033
1034 /**
1035 * mono_error_raise_exception_deprecated:
1036 * \param target_error the exception to raise
1037 *
1038 * Raises the exception of \p target_error.
1039 * Does nothing if \p target_error has a success error code.
1040 * Aborts in case of a double fault. This happens when it can't recover from an error caused by trying
1041 * to construct the first exception object.
1042 * The error object \p target_error is cleaned up.
1043 */
1044 void
mono_error_raise_exception_deprecated(MonoError * target_error)1045 mono_error_raise_exception_deprecated (MonoError *target_error)
1046 {
1047 MonoException *ex = mono_error_convert_to_exception (target_error);
1048 if (ex)
1049 mono_raise_exception_deprecated (ex);
1050 }
1051
1052 /**
1053 * mono_error_set_pending_exception:
1054 * \param error The error
1055 * If \p error is set, convert it to an exception and set the pending exception for the current icall.
1056 * \returns TRUE if \p error was set, or FALSE otherwise, so that you can write:
1057 * if (mono_error_set_pending_exception (error)) {
1058 * { ... cleanup code ... }
1059 * return;
1060 * }
1061 */
1062 gboolean
mono_error_set_pending_exception(MonoError * error)1063 mono_error_set_pending_exception (MonoError *error)
1064 {
1065 MonoException *ex = mono_error_convert_to_exception (error);
1066 if (ex) {
1067 mono_set_pending_exception (ex);
1068 return TRUE;
1069 } else {
1070 return FALSE;
1071 }
1072 }
1073
1074 void
mono_install_unhandled_exception_hook(MonoUnhandledExceptionFunc func,void * user_data)1075 mono_install_unhandled_exception_hook (MonoUnhandledExceptionFunc func, void *user_data)
1076 {
1077 unhandled_exception_hook = func;
1078 unhandled_exception_hook_data = user_data;
1079 }
1080
1081 void
mono_invoke_unhandled_exception_hook(MonoObject * exc)1082 mono_invoke_unhandled_exception_hook (MonoObject *exc)
1083 {
1084 if (unhandled_exception_hook) {
1085 unhandled_exception_hook (exc, unhandled_exception_hook_data);
1086 } else {
1087 MonoError inner_error;
1088 MonoObject *other = NULL;
1089 MonoString *str = mono_object_try_to_string (exc, &other, &inner_error);
1090 char *msg = NULL;
1091
1092 if (str && is_ok (&inner_error)) {
1093 msg = mono_string_to_utf8_checked (str, &inner_error);
1094 if (!is_ok (&inner_error)) {
1095 msg = g_strdup_printf ("Nested exception while formatting original exception");
1096 mono_error_cleanup (&inner_error);
1097 }
1098 } else if (other) {
1099 char *original_backtrace = mono_exception_get_managed_backtrace ((MonoException*)exc);
1100 char *nested_backtrace = mono_exception_get_managed_backtrace ((MonoException*)other);
1101
1102 msg = g_strdup_printf ("Nested exception detected.\nOriginal Exception: %s\nNested exception:%s\n",
1103 original_backtrace, nested_backtrace);
1104
1105 g_free (original_backtrace);
1106 g_free (nested_backtrace);
1107 } else {
1108 msg = g_strdup ("Nested exception trying to figure out what went wrong");
1109 }
1110 mono_runtime_printf_err ("[ERROR] FATAL UNHANDLED EXCEPTION: %s", msg);
1111 g_free (msg);
1112 #if defined(HOST_IOS)
1113 g_assertion_message ("Terminating runtime due to unhandled exception");
1114 #else
1115 exit (mono_environment_exitcode_get ());
1116 #endif
1117 }
1118
1119 g_assert_not_reached ();
1120 }
1121
1122
1123 static MonoException *
create_exception_four_strings(MonoClass * klass,MonoString * a1,MonoString * a2,MonoString * a3,MonoString * a4,MonoError * error)1124 create_exception_four_strings (MonoClass *klass, MonoString *a1, MonoString *a2, MonoString *a3, MonoString *a4, MonoError *error)
1125 {
1126 MonoDomain *domain = mono_domain_get ();
1127 MonoMethod *method = NULL;
1128 MonoObject *o;
1129 int count = 4;
1130 gpointer args [4];
1131 gpointer iter;
1132 MonoMethod *m;
1133
1134 o = mono_object_new_checked (domain, klass, error);
1135 mono_error_assert_ok (error);
1136
1137 iter = NULL;
1138 while ((m = mono_class_get_methods (klass, &iter))) {
1139 MonoMethodSignature *sig;
1140
1141 if (strcmp (".ctor", mono_method_get_name (m)))
1142 continue;
1143 sig = mono_method_signature (m);
1144 if (sig->param_count != count)
1145 continue;
1146
1147 int i;
1148 gboolean good = TRUE;
1149 for (i = 0; i < count; ++i) {
1150 if (sig->params [i]->type != MONO_TYPE_STRING) {
1151 good = FALSE;
1152 break;
1153 }
1154 }
1155 if (good) {
1156 method = m;
1157 break;
1158 }
1159 }
1160
1161 g_assert (method);
1162
1163 args [0] = a1;
1164 args [1] = a2;
1165 args [2] = a3;
1166 args [3] = a4;
1167
1168 mono_runtime_invoke_checked (method, o, args, error);
1169 return_val_if_nok (error, NULL);
1170
1171 return (MonoException *) o;
1172 }
1173
1174 MonoException *
mono_exception_from_name_four_strings_checked(MonoImage * image,const char * name_space,const char * name,MonoString * a1,MonoString * a2,MonoString * a3,MonoString * a4,MonoError * error)1175 mono_exception_from_name_four_strings_checked (MonoImage *image, const char *name_space,
1176 const char *name, MonoString *a1, MonoString *a2, MonoString *a3, MonoString *a4,
1177 MonoError *error)
1178 {
1179 MonoClass *klass;
1180
1181 error_init (error);
1182 klass = mono_class_load_from_name (image, name_space, name);
1183
1184 return create_exception_four_strings (klass, a1, a2, a3, a4, error);
1185 }
1186