[runtime] Don't insta-fail when a faulty COM type is encountered. (#5616)
[mono.git] / mono / utils / mono-error.c
1 /**
2  * \file
3  * Error handling code
4  *
5  * Authors:
6  *      Rodrigo Kumpera    (rkumpera@novell.com)
7  * Copyright 2009 Novell, Inc (http://www.novell.com)
8  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
9  */
10 #include <glib.h>
11
12 #include <config.h>
13 #include "mono-error.h"
14 #include "mono-error-internals.h"
15
16 #include <mono/metadata/exception.h>
17 #include <mono/metadata/exception-internals.h>
18 #include <mono/metadata/debug-helpers.h>
19 #include <mono/metadata/object-internals.h>
20
21 #define set_error_messagev() do { \
22         if (!(error->full_message = g_strdup_vprintf (msg_format, args))) \
23                         error->flags |= MONO_ERROR_INCOMPLETE; \
24 } while (0)
25
26 #define set_error_message() do { \
27         va_list args; \
28         va_start (args, msg_format); \
29         set_error_messagev();        \
30         va_end (args); \
31 } while (0)
32
33 static void
34 mono_error_set_generic_errorv (MonoError *oerror, const char *name_space, const char *name, const char *msg_format, va_list args);
35
36 static gboolean
37 is_managed_exception (MonoErrorInternal *error)
38 {
39         return (error->error_code == MONO_ERROR_EXCEPTION_INSTANCE);
40 }
41
42 static gboolean
43 is_boxed (MonoErrorInternal *error)
44 {
45         return ((error->flags & MONO_ERROR_MEMPOOL_BOXED) != 0);
46 }
47
48 static void
49 mono_error_prepare (MonoErrorInternal *error)
50 {
51         /* mono_error_set_* after a mono_error_cleanup without an intervening init */
52         g_assert (error->error_code != MONO_ERROR_CLEANUP_CALLED_SENTINEL);
53         if (error->error_code != MONO_ERROR_NONE)
54                 return;
55
56         error->type_name = error->assembly_name = error->member_name = error->full_message = error->exception_name_space = error->exception_name = error->full_message_with_fields = error->first_argument = NULL;
57         error->exn.klass = NULL;
58 }
59
60 static MonoClass*
61 get_class (MonoErrorInternal *error)
62 {
63         MonoClass *klass = NULL;
64         if (is_managed_exception (error))
65                 klass = mono_object_class (mono_gchandle_get_target (error->exn.instance_handle));
66         else
67                 klass = error->exn.klass;
68         return klass;
69 }
70
71 static const char*
72 get_type_name (MonoErrorInternal *error)
73 {
74         if (error->type_name)
75                 return error->type_name;
76         MonoClass *klass = get_class (error);
77         if (klass)
78                 return klass->name;
79         return "<unknown type>";
80 }
81
82 static const char*
83 get_assembly_name (MonoErrorInternal *error)
84 {
85         if (error->assembly_name)
86                 return error->assembly_name;
87         MonoClass *klass = get_class (error);
88         if (klass && klass->image)
89                 return klass->image->name;
90         return "<unknown assembly>";
91 }
92
93 void
94 mono_error_init_flags (MonoError *oerror, unsigned short flags)
95 {
96         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
97         g_assert (sizeof (MonoError) == sizeof (MonoErrorInternal));
98
99         error->error_code = MONO_ERROR_NONE;
100         error->flags = flags;
101 }
102
103 /**
104  * mono_error_init:
105  * \param error Pointer to \c MonoError struct to initialize
106  * Any function which takes a \c MonoError for purposes of reporting an error
107  * is required to call either this or \c mono_error_init_flags on entry.
108  */
109 void
110 mono_error_init (MonoError *error)
111 {
112         mono_error_init_flags (error, 0);
113 }
114
115 void
116 mono_error_cleanup (MonoError *oerror)
117 {
118         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
119         short int orig_error_code = error->error_code;
120         gboolean free_strings = error->flags & MONO_ERROR_FREE_STRINGS;
121         gboolean has_instance_handle = is_managed_exception (error);
122
123         /* Two cleanups in a row without an intervening init. */
124         g_assert (orig_error_code != MONO_ERROR_CLEANUP_CALLED_SENTINEL);
125         /* Mempool stored error shouldn't be cleaned up */
126         g_assert (!is_boxed (error));
127
128         /* Mark it as cleaned up. */
129         error->error_code = MONO_ERROR_CLEANUP_CALLED_SENTINEL;
130         error->flags = 0;
131
132         if (orig_error_code == MONO_ERROR_NONE)
133                 return;
134
135
136         if (has_instance_handle)
137                 mono_gchandle_free (error->exn.instance_handle);
138
139
140         g_free ((char*)error->full_message);
141         g_free ((char*)error->full_message_with_fields);
142         error->full_message = NULL;
143         error->full_message_with_fields = NULL;
144         if (!free_strings) //no memory was allocated
145                 return;
146
147         g_free ((char*)error->type_name);
148         g_free ((char*)error->assembly_name);
149         g_free ((char*)error->member_name);
150         g_free ((char*)error->exception_name_space);
151         g_free ((char*)error->exception_name);
152         g_free ((char*)error->first_argument);
153         error->type_name = error->assembly_name = error->member_name = error->exception_name_space = error->exception_name = error->first_argument = NULL;
154         error->exn.klass = NULL;
155
156 }
157
158 gboolean
159 mono_error_ok (MonoError *error)
160 {
161         return error->error_code == MONO_ERROR_NONE;
162 }
163
164 void
165 mono_error_assert_ok_pos (MonoError *error, const char* filename, int lineno)
166 {
167         if (mono_error_ok (error))
168                 return;
169
170         g_error ("%s:%d: %s\n", filename, lineno, mono_error_get_message (error));
171 }
172
173 unsigned short
174 mono_error_get_error_code (MonoError *error)
175 {
176         return error->error_code;
177 }
178
179 const char*
180 mono_error_get_exception_name (MonoError *oerror)
181 {
182         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
183
184         if (error->error_code == MONO_ERROR_NONE)
185                 return NULL;
186
187         return error->exception_name;
188 }
189
190 /*Return a pointer to the internal error message, might be NULL.
191 Caller should not release it.*/
192 const char*
193 mono_error_get_message (MonoError *oerror)
194 {
195         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
196         if (error->error_code == MONO_ERROR_NONE)
197                 return NULL;
198         if (error->full_message_with_fields)
199                 return error->full_message_with_fields;
200
201         error->full_message_with_fields = g_strdup_printf ("%s assembly:%s type:%s member:%s",
202                 error->full_message,
203                 get_assembly_name (error),
204                 get_type_name (error),
205                 error->member_name ? error->member_name : "<none>");
206
207         return error->full_message_with_fields ? error->full_message_with_fields : error->full_message;
208 }
209
210 /*
211  * Inform that this error has heap allocated strings.
212  * The strings will be duplicated if @dup_strings is TRUE
213  * otherwise they will just be free'd in mono_error_cleanup.
214  */
215 void
216 mono_error_dup_strings (MonoError *oerror, gboolean dup_strings)
217 {
218 #define DUP_STR(field) do { if (error->field) {\
219         if (!(error->field = g_strdup (error->field))) \
220                 error->flags |= MONO_ERROR_INCOMPLETE; \
221         }} while (0);
222
223         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
224         error->flags |= MONO_ERROR_FREE_STRINGS;
225
226         if (dup_strings) {
227                 DUP_STR (type_name);
228                 DUP_STR (assembly_name);
229                 DUP_STR (member_name);
230                 DUP_STR (exception_name_space);
231                 DUP_STR (exception_name);
232                 DUP_STR (first_argument);
233         }
234 #undef DUP_STR
235 }
236
237 void
238 mono_error_set_error (MonoError *oerror, int error_code, const char *msg_format, ...)
239 {
240         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
241         mono_error_prepare (error);
242
243         error->error_code = error_code;
244         set_error_message ();
245 }
246
247 static void
248 mono_error_set_assembly_name (MonoError *oerror, const char *assembly_name)
249 {
250         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
251         g_assert (error->error_code != MONO_ERROR_NONE);
252
253         error->assembly_name = assembly_name;
254 }
255
256 static void
257 mono_error_set_member_name (MonoError *oerror, const char *member_name)
258 {
259         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
260
261         error->member_name = member_name;
262 }
263
264 static void
265 mono_error_set_type_name (MonoError *oerror, const char *type_name)
266 {
267         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
268
269         error->type_name = type_name;
270 }
271
272 static void
273 mono_error_set_class (MonoError *oerror, MonoClass *klass)
274 {
275         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
276
277         if (is_managed_exception (error))
278                 return;
279         error->exn.klass = klass;       
280 }
281
282 static void
283 mono_error_set_corlib_exception (MonoError *oerror, const char *name_space, const char *name)
284 {
285         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
286
287         error->exception_name_space = name_space;
288         error->exception_name = name;
289 }
290
291
292 void
293 mono_error_set_assembly_load (MonoError *oerror, const char *assembly_name, const char *msg_format, ...)
294 {
295         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
296         mono_error_prepare (error);
297
298         error->error_code = MONO_ERROR_FILE_NOT_FOUND;
299         mono_error_set_assembly_name (oerror, assembly_name);
300
301         set_error_message ();
302 }
303
304
305 void
306 mono_error_set_assembly_load_simple (MonoError *oerror, const char *assembly_name, gboolean refection_only)
307 {
308         if (refection_only)
309                 mono_error_set_assembly_load (oerror, assembly_name, "Cannot resolve dependency to assembly because it has not been preloaded. When using the ReflectionOnly APIs, dependent assemblies must be pre-loaded or loaded on demand through the ReflectionOnlyAssemblyResolve event.");
310         else
311                 mono_error_set_assembly_load (oerror, assembly_name, "Could not load file or assembly '%s' or one of its dependencies.", assembly_name);
312 }
313
314 void
315 mono_error_set_type_load_class (MonoError *oerror, MonoClass *klass, const char *msg_format, ...)
316 {
317         va_list args;
318         va_start (args, msg_format);
319         mono_error_vset_type_load_class (oerror, klass, msg_format, args);
320         va_end (args);
321 }
322
323 void
324 mono_error_vset_type_load_class (MonoError *oerror, MonoClass *klass, const char *msg_format, va_list args)
325 {
326         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
327         mono_error_prepare (error);
328
329         error->error_code = MONO_ERROR_TYPE_LOAD;
330         mono_error_set_class (oerror, klass);
331         set_error_messagev ();
332 }
333
334 /*
335  * Different than other functions, this one here assumes that type_name and assembly_name to have been allocated just for us.
336  * Which means mono_error_cleanup will free them.
337  */
338 void
339 mono_error_set_type_load_name (MonoError *oerror, const char *type_name, const char *assembly_name, const char *msg_format, ...)
340 {
341         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
342         mono_error_prepare (error);
343
344         error->error_code = MONO_ERROR_TYPE_LOAD;
345         mono_error_set_type_name (oerror, type_name);
346         mono_error_set_assembly_name (oerror, assembly_name);
347         mono_error_dup_strings (oerror, FALSE);
348         set_error_message ();
349 }
350
351 void
352 mono_error_set_method_load (MonoError *oerror, MonoClass *klass, const char *method_name, const char *msg_format, ...)
353 {
354         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
355         mono_error_prepare (error);
356
357         error->error_code = MONO_ERROR_MISSING_METHOD;
358         mono_error_set_class (oerror, klass);
359         mono_error_set_member_name (oerror, method_name);
360         set_error_message ();
361 }
362
363 void
364 mono_error_set_field_load (MonoError *oerror, MonoClass *klass, const char *field_name, const char *msg_format, ...)
365 {
366         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
367         mono_error_prepare (error);
368
369         error->error_code = MONO_ERROR_MISSING_FIELD;
370         mono_error_set_class (oerror, klass);
371         mono_error_set_member_name (oerror, field_name);
372         set_error_message ();   
373 }
374
375 void
376 mono_error_set_bad_image_name (MonoError *oerror, const char *assembly_name, const char *msg_format, ...)
377 {
378         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
379         mono_error_prepare (error);
380
381         error->error_code = MONO_ERROR_BAD_IMAGE;
382         mono_error_set_assembly_name (oerror, assembly_name);
383         set_error_message ();
384 }
385
386 void
387 mono_error_set_bad_image (MonoError *oerror, MonoImage *image, const char *msg_format, ...)
388 {
389         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
390         mono_error_prepare (error);
391
392         error->error_code = MONO_ERROR_BAD_IMAGE;
393         error->assembly_name = image ? mono_image_get_name (image) : "<no_image>";
394         set_error_message ();
395 }
396
397 void
398 mono_error_set_generic_errorv (MonoError *oerror, const char *name_space, const char *name, const char *msg_format, va_list args)
399 {
400         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
401         mono_error_prepare (error);
402
403         error->error_code = MONO_ERROR_GENERIC;
404         mono_error_set_corlib_exception (oerror, name_space, name);
405         set_error_messagev ();
406 }
407
408 void
409 mono_error_set_generic_error (MonoError *oerror, const char * name_space, const char *name, const char *msg_format, ...)
410 {
411         va_list args;
412         va_start (args, msg_format);
413         mono_error_set_generic_errorv (oerror, name_space, name, msg_format, args);
414         va_end (args);
415 }
416
417 /**
418  * mono_error_set_not_implemented:
419  *
420  * System.NotImplementedException
421  */
422 void
423 mono_error_set_not_implemented (MonoError *oerror, const char *msg_format, ...)
424 {
425         va_list args;
426         va_start (args, msg_format);
427         mono_error_set_generic_errorv (oerror, "System", "NotImplementedException", msg_format, args);
428         va_end (args);
429 }
430
431 /**
432  * mono_error_set_execution_engine:
433  *
434  * System.ExecutionEngineException
435  */
436 void
437 mono_error_set_execution_engine (MonoError *oerror, const char *msg_format, ...)
438 {
439         va_list args;
440         va_start (args, msg_format);
441         mono_error_set_generic_errorv (oerror, "System", "ExecutionEngineException", msg_format, args);
442         va_end (args);
443 }
444
445 /**
446  * mono_error_set_not_supported:
447  *
448  * System.NotSupportedException
449  */
450 void
451 mono_error_set_not_supported (MonoError *oerror, const char *msg_format, ...)
452 {
453         va_list args;
454         va_start (args, msg_format);
455         mono_error_set_generic_errorv (oerror, "System", "NotSupportedException", msg_format, args);
456         va_end (args);
457 }
458
459 /**
460  * mono_error_set_invalid_operation:
461  *
462  * System.InvalidOperationException
463  */
464 void
465 mono_error_set_invalid_operation (MonoError *oerror, const char *msg_format, ...)
466 {
467         va_list args;
468         va_start (args, msg_format);
469         mono_error_set_generic_errorv (oerror, "System", "InvalidOperationException", msg_format, args);
470         va_end (args);
471 }
472
473 /**
474  * mono_error_set_file_not_found:
475  *
476  * System.IO.FileNotFoundException
477  */
478 void
479 mono_error_set_file_not_found (MonoError *oerror, const char *msg_format, ...)
480 {
481         va_list args;
482         va_start (args, msg_format);
483         mono_error_set_generic_errorv (oerror, "System.IO", "FileNotFoundException", msg_format, args);
484         va_end (args);
485 }
486
487 void
488 mono_error_set_invalid_program (MonoError *oerror, const char *msg_format, ...)
489 {
490         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
491
492         mono_error_prepare (error);
493         error->error_code = MONO_ERROR_INVALID_PROGRAM;
494
495         set_error_message ();
496 }
497
498 /**
499  * mono_error_set_invalid_cast:
500  *
501  * System.InvalidCastException
502  */
503 void
504 mono_error_set_invalid_cast (MonoError *oerror)
505 {
506         mono_error_set_generic_error (oerror, "System", "InvalidCastException", "");
507 }
508
509 void
510 mono_error_set_exception_instance (MonoError *oerror, MonoException *exc)
511 {
512         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
513
514         mono_error_prepare (error);
515         error->error_code = MONO_ERROR_EXCEPTION_INSTANCE;
516         error->exn.instance_handle = mono_gchandle_new (exc ? &exc->object : NULL, FALSE);
517 }
518
519 void
520 mono_error_set_exception_handle (MonoError *oerror, MonoExceptionHandle exc)
521 {
522         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
523
524         mono_error_prepare (error);
525         error->error_code = MONO_ERROR_EXCEPTION_INSTANCE;
526         error->exn.instance_handle = mono_gchandle_from_handle (MONO_HANDLE_CAST(MonoObject, exc), FALSE);
527 }
528
529 void
530 mono_error_set_out_of_memory (MonoError *oerror, const char *msg_format, ...)
531 {
532         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
533         mono_error_prepare (error);
534
535         error->error_code = MONO_ERROR_OUT_OF_MEMORY;
536
537         set_error_message ();
538 }
539
540 void
541 mono_error_set_argument (MonoError *oerror, const char *argument, const char *msg_format, ...)
542 {
543         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
544         mono_error_prepare (error);
545
546         error->error_code = MONO_ERROR_ARGUMENT;
547         error->first_argument = argument;
548
549         set_error_message ();
550 }
551
552 void
553 mono_error_set_argument_null (MonoError *oerror, const char *argument, const char *msg_format, ...)
554 {
555         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
556         mono_error_prepare (error);
557
558         error->error_code = MONO_ERROR_ARGUMENT_NULL;
559         error->first_argument = argument;
560
561         set_error_message ();
562 }
563
564 void
565 mono_error_set_not_verifiable (MonoError *oerror, MonoMethod *method, const char *msg_format, ...)
566 {
567         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
568         mono_error_prepare (error);
569
570         error->error_code = MONO_ERROR_NOT_VERIFIABLE;
571         if (method) {
572                 mono_error_set_class (oerror, method->klass);
573                 mono_error_set_member_name (oerror, mono_method_full_name (method, 1));
574         }
575
576         set_error_message ();
577 }
578
579
580 /* Used by mono_error_prepare_exception - it sets its own error on mono_string_new_checked failure. */
581 static MonoString*
582 string_new_cleanup (MonoDomain *domain, const char *text)
583 {
584         MonoError ignored_err;
585         MonoString *result = mono_string_new_checked (domain, text, &ignored_err);
586         mono_error_cleanup (&ignored_err);
587         return result;
588 }
589
590 static MonoString*
591 get_type_name_as_mono_string (MonoErrorInternal *error, MonoDomain *domain, MonoError *error_out)
592 {
593         MonoString* res = NULL;
594
595         if (error->type_name) {
596                 res = string_new_cleanup (domain, error->type_name);
597                 
598         } else {
599                 MonoClass *klass = get_class (error);
600                 if (klass) {
601                         char *name = mono_type_full_name (&klass->byval_arg);
602                         if (name) {
603                                 res = string_new_cleanup (domain, name);
604                                 g_free (name);
605                         }
606                 }
607         }
608         if (!res)
609                 mono_error_set_out_of_memory (error_out, "Could not allocate type name");
610         return res;
611 }
612
613 static void
614 set_message_on_exception (MonoException *exception, MonoErrorInternal *error, MonoError *error_out)
615 {
616         MonoString *msg = string_new_cleanup (mono_domain_get (), error->full_message);
617         if (msg)
618                 MONO_OBJECT_SETREF (exception, message, msg);
619         else
620                 mono_error_set_out_of_memory (error_out, "Could not allocate exception object");
621 }
622
623 /*Can fail with out-of-memory*/
624 MonoException*
625 mono_error_prepare_exception (MonoError *oerror, MonoError *error_out)
626 {
627         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
628
629         MonoException* exception = NULL;
630         MonoString *assembly_name = NULL, *type_name = NULL, *method_name = NULL, *field_name = NULL, *msg = NULL;
631         MonoDomain *domain = mono_domain_get ();
632
633         error_init (error_out);
634
635         switch (error->error_code) {
636         case MONO_ERROR_NONE:
637                 return NULL;
638
639         case MONO_ERROR_MISSING_METHOD:
640                 if ((error->type_name || error->exn.klass) && error->member_name) {
641                         type_name = get_type_name_as_mono_string (error, domain, error_out);
642                         if (!mono_error_ok (error_out))
643                                 break;
644
645                         method_name = string_new_cleanup (domain, error->member_name);
646                         if (!method_name) {
647                                 mono_error_set_out_of_memory (error_out, "Could not allocate method name");
648                                 break;
649                         }
650
651                         exception = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "MissingMethodException", type_name, method_name, error_out);
652                         if (exception)
653                                 set_message_on_exception (exception, error, error_out);
654                 } else {
655                         exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingMethodException", error->full_message);
656                 }
657                 break;
658
659         case MONO_ERROR_MISSING_FIELD:
660                 if ((error->type_name || error->exn.klass) && error->member_name) {
661                         type_name = get_type_name_as_mono_string (error, domain, error_out);
662                         if (!mono_error_ok (error_out))
663                                 break;
664                         
665                         field_name = string_new_cleanup (domain, error->member_name);
666                         if (!field_name) {
667                                 mono_error_set_out_of_memory (error_out, "Could not allocate field name");
668                                 break;
669                         }
670                         
671                         exception = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "MissingFieldException", type_name, field_name, error_out);
672                         if (exception)
673                                 set_message_on_exception (exception, error, error_out);
674                 } else {
675                         exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingFieldException", error->full_message);
676                 }
677                 break;
678
679         case MONO_ERROR_TYPE_LOAD:
680                 if ((error->type_name && error->assembly_name) || error->exn.klass) {
681                         type_name = get_type_name_as_mono_string (error, domain, error_out);
682                         if (!mono_error_ok (error_out))
683                                 break;
684
685                         if (error->assembly_name) {
686                                 assembly_name = string_new_cleanup (domain, error->assembly_name);
687                                 if (!assembly_name) {
688                                         mono_error_set_out_of_memory (error_out, "Could not allocate assembly name");
689                                         break;
690                                 }
691                         } else {
692                                 assembly_name = mono_string_empty (domain);
693                         }
694
695                         exception = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System", "TypeLoadException", type_name, assembly_name, error_out);
696                         if (exception && error->full_message != NULL && strcmp (error->full_message, ""))
697                                 set_message_on_exception (exception, error, error_out);
698                 } else {
699                         exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "TypeLoadException", error->full_message);
700                 }
701                 break;
702
703         case MONO_ERROR_FILE_NOT_FOUND:
704         case MONO_ERROR_BAD_IMAGE:
705                 if (error->assembly_name) {
706                         msg = string_new_cleanup (domain, error->full_message);
707                         if (!msg) {
708                                 mono_error_set_out_of_memory (error_out, "Could not allocate message");
709                                 break;
710                         }
711
712                         if (error->assembly_name) {
713                                 assembly_name = string_new_cleanup (domain, error->assembly_name);
714                                 if (!assembly_name) {
715                                         mono_error_set_out_of_memory (error_out, "Could not allocate assembly name");
716                                         break;
717                                 }
718                         }
719
720                         if (error->error_code == MONO_ERROR_FILE_NOT_FOUND)
721                                 exception = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System.IO", "FileNotFoundException", msg, assembly_name, error_out);
722                         else
723                                 exception = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "BadImageFormatException", msg, assembly_name, error_out);
724                 } else {
725                         if (error->error_code == MONO_ERROR_FILE_NOT_FOUND)
726                                 exception = mono_exception_from_name_msg (mono_get_corlib (), "System.IO", "FileNotFoundException", error->full_message);
727                         else
728                                 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "BadImageFormatException", error->full_message);
729                 }
730                 break;
731
732         case MONO_ERROR_OUT_OF_MEMORY:
733                 exception = mono_get_exception_out_of_memory ();
734                 break;
735
736         case MONO_ERROR_ARGUMENT:
737                 exception = mono_get_exception_argument (error->first_argument, error->full_message);
738                 break;
739
740         case MONO_ERROR_ARGUMENT_NULL:
741                 exception = mono_get_exception_argument_null (error->first_argument);
742                 break;
743
744         case MONO_ERROR_NOT_VERIFIABLE: {
745                 char *type_name = NULL, *message;
746                 if (error->exn.klass) {
747                         type_name = mono_type_get_full_name (error->exn.klass);
748                         if (!type_name) {
749                                 mono_error_set_out_of_memory (error_out, "Could not allocate message");
750                                 break;
751                         }
752                 }
753                 message = g_strdup_printf ("Error in %s:%s %s", type_name, error->member_name, error->full_message);
754                 if (!message) {
755                         g_free (type_name);
756                         mono_error_set_out_of_memory (error_out, "Could not allocate message");
757                         break;  
758                 }
759                 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System.Security", "VerificationException", message);
760                 g_free (message);
761                 g_free (type_name);
762                 break;
763         }
764         case MONO_ERROR_GENERIC:
765                 if (!error->exception_name_space || !error->exception_name)
766                         mono_error_set_execution_engine (error_out, "MonoError with generic error but no exception name was supplied");
767                 else
768                         exception = mono_exception_from_name_msg (mono_defaults.corlib, error->exception_name_space, error->exception_name, error->full_message);
769                 break;
770
771         case MONO_ERROR_EXCEPTION_INSTANCE:
772                 exception = (MonoException*) mono_gchandle_get_target (error->exn.instance_handle);
773                 break;
774
775         case MONO_ERROR_CLEANUP_CALLED_SENTINEL:
776                 mono_error_set_execution_engine (error_out, "MonoError reused after mono_error_cleanup");
777                 break;
778
779         case MONO_ERROR_INVALID_PROGRAM: {
780                 gboolean lacks_message = error->flags & MONO_ERROR_INCOMPLETE;
781                 if (lacks_message)
782                         return mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", "");
783                 else
784                         return mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", error->full_message);
785         }
786         default:
787                 mono_error_set_execution_engine (error_out, "Invalid error-code %d", error->error_code);
788         }
789
790         if (!mono_error_ok (error_out))
791                 return NULL;
792         if (!exception)
793                 mono_error_set_out_of_memory (error_out, "Could not allocate exception object");
794         return exception;
795 }
796
797 /*
798 Convert this MonoError to an exception if it's faulty or return NULL.
799 The error object is cleant after.
800 */
801
802 MonoException*
803 mono_error_convert_to_exception (MonoError *target_error)
804 {
805         MonoError error;
806         MonoException *ex;
807
808         /* Mempool stored error shouldn't be cleaned up */
809         g_assert (!is_boxed ((MonoErrorInternal*)target_error));
810
811         if (mono_error_ok (target_error))
812                 return NULL;
813
814         ex = mono_error_prepare_exception (target_error, &error);
815         if (!mono_error_ok (&error)) {
816                 MonoError second_chance;
817                 /*Try to produce the exception for the second error. FIXME maybe we should log about the original one*/
818                 ex = mono_error_prepare_exception (&error, &second_chance);
819
820                 g_assert (mono_error_ok (&second_chance)); /*We can't reasonable handle double faults, maybe later.*/
821                 mono_error_cleanup (&error);
822         }
823         mono_error_cleanup (target_error);
824         return ex;
825 }
826
827 void
828 mono_error_move (MonoError *dest, MonoError *src)
829 {
830         memcpy (dest, src, sizeof (MonoErrorInternal));
831         error_init (src);
832 }
833
834 /**
835  * mono_error_box:
836  * \param ierror The input error that will be boxed.
837  * \param image The mempool of this image will hold the boxed error.
838  * Creates a new boxed error in the given mempool from \c MonoError.
839  * It does not alter \p ierror, so you still have to clean it up with
840  * \c mono_error_cleanup or \c mono_error_convert_to_exception or another such function.
841  * \returns the boxed error, or NULL if the mempool could not allocate.
842  */
843 MonoErrorBoxed*
844 mono_error_box (const MonoError *ierror, MonoImage *image)
845 {
846         MonoErrorInternal *from = (MonoErrorInternal*)ierror;
847         /* Don't know how to box a gchandle */
848         g_assert (!is_managed_exception (from));
849         MonoErrorBoxed* box = mono_image_alloc (image, sizeof (MonoErrorBoxed));
850         box->image = image;
851         mono_error_init_flags (&box->error, MONO_ERROR_MEMPOOL_BOXED);
852         MonoErrorInternal *to = (MonoErrorInternal*)&box->error;
853
854 #define DUP_STR(field) do {                                             \
855                 if (from->field) {                                      \
856                         if (!(to->field = mono_image_strdup (image, from->field))) \
857                                 to->flags |= MONO_ERROR_INCOMPLETE;     \
858                 } else {                                                \
859                         to->field = NULL;                               \
860                 }                                                       \
861         } while (0)
862
863         to->error_code = from->error_code;
864         DUP_STR (type_name);
865         DUP_STR (assembly_name);
866         DUP_STR (member_name);
867         DUP_STR (exception_name_space);
868         DUP_STR (exception_name);
869         DUP_STR (full_message);
870         DUP_STR (full_message_with_fields);
871         DUP_STR (first_argument);
872         to->exn.klass = from->exn.klass;
873
874 #undef DUP_STR
875         
876         return box;
877 }
878
879
880 /**
881  * mono_error_set_from_boxed:
882  * \param oerror The error that will be set to the contents of the box.
883  * \param box A mempool-allocated error.
884  * Sets the error condition in the oerror from the contents of the
885  * given boxed error.  Does not alter the boxed error, so it can be
886  * used in a future call to \c mono_error_set_from_boxed as needed.  The
887  * \p oerror should've been previously initialized with \c mono_error_init,
888  * as usual.
889  * \returns TRUE on success or FALSE on failure.
890  */
891 gboolean
892 mono_error_set_from_boxed (MonoError *oerror, const MonoErrorBoxed *box)
893 {
894         MonoErrorInternal* to = (MonoErrorInternal*)oerror;
895         MonoErrorInternal* from = (MonoErrorInternal*)&box->error;
896         g_assert (!is_managed_exception (from));
897
898         mono_error_prepare (to);
899         to->flags |= MONO_ERROR_FREE_STRINGS;
900 #define DUP_STR(field)  do {                                            \
901                 if (from->field) {                                      \
902                         if (!(to->field = g_strdup (from->field)))      \
903                                 to->flags |= MONO_ERROR_INCOMPLETE;     \
904                 } else {                                                \
905                         to->field = NULL;                               \
906                 }                                                       \
907         } while (0)
908
909         to->error_code = from->error_code;
910         DUP_STR (type_name);
911         DUP_STR (assembly_name);
912         DUP_STR (member_name);
913         DUP_STR (exception_name_space);
914         DUP_STR (exception_name);
915         DUP_STR (full_message);
916         DUP_STR (full_message_with_fields);
917         DUP_STR (first_argument);
918         to->exn.klass = from->exn.klass;
919                   
920 #undef DUP_STR
921         return (to->flags & MONO_ERROR_INCOMPLETE) == 0 ;
922 }