e193291a569cbb39a78448fd5a90ceda29dcffbd
[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 /*Return a pointer to the internal error message, might be NULL.
180 Caller should not release it.*/
181 const char*
182 mono_error_get_message (MonoError *oerror)
183 {
184         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
185         if (error->error_code == MONO_ERROR_NONE)
186                 return NULL;
187         if (error->full_message_with_fields)
188                 return error->full_message_with_fields;
189
190         error->full_message_with_fields = g_strdup_printf ("%s assembly:%s type:%s member:%s",
191                 error->full_message,
192                 get_assembly_name (error),
193                 get_type_name (error),
194                 error->member_name ? error->member_name : "<none>");
195
196         return error->full_message_with_fields ? error->full_message_with_fields : error->full_message;
197 }
198
199 /*
200  * Inform that this error has heap allocated strings.
201  * The strings will be duplicated if @dup_strings is TRUE
202  * otherwise they will just be free'd in mono_error_cleanup.
203  */
204 void
205 mono_error_dup_strings (MonoError *oerror, gboolean dup_strings)
206 {
207 #define DUP_STR(field) do { if (error->field) {\
208         if (!(error->field = g_strdup (error->field))) \
209                 error->flags |= MONO_ERROR_INCOMPLETE; \
210         }} while (0);
211
212         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
213         error->flags |= MONO_ERROR_FREE_STRINGS;
214
215         if (dup_strings) {
216                 DUP_STR (type_name);
217                 DUP_STR (assembly_name);
218                 DUP_STR (member_name);
219                 DUP_STR (exception_name_space);
220                 DUP_STR (exception_name);
221                 DUP_STR (first_argument);
222         }
223 #undef DUP_STR
224 }
225
226 void
227 mono_error_set_error (MonoError *oerror, int error_code, const char *msg_format, ...)
228 {
229         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
230         mono_error_prepare (error);
231
232         error->error_code = error_code;
233         set_error_message ();
234 }
235
236 static void
237 mono_error_set_assembly_name (MonoError *oerror, const char *assembly_name)
238 {
239         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
240         g_assert (error->error_code != MONO_ERROR_NONE);
241
242         error->assembly_name = assembly_name;
243 }
244
245 static void
246 mono_error_set_member_name (MonoError *oerror, const char *member_name)
247 {
248         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
249
250         error->member_name = member_name;
251 }
252
253 static void
254 mono_error_set_type_name (MonoError *oerror, const char *type_name)
255 {
256         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
257
258         error->type_name = type_name;
259 }
260
261 static void
262 mono_error_set_class (MonoError *oerror, MonoClass *klass)
263 {
264         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
265
266         if (is_managed_exception (error))
267                 return;
268         error->exn.klass = klass;       
269 }
270
271 static void
272 mono_error_set_corlib_exception (MonoError *oerror, const char *name_space, const char *name)
273 {
274         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
275
276         error->exception_name_space = name_space;
277         error->exception_name = name;
278 }
279
280
281 void
282 mono_error_set_assembly_load (MonoError *oerror, const char *assembly_name, const char *msg_format, ...)
283 {
284         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
285         mono_error_prepare (error);
286
287         error->error_code = MONO_ERROR_FILE_NOT_FOUND;
288         mono_error_set_assembly_name (oerror, assembly_name);
289
290         set_error_message ();
291 }
292
293
294 void
295 mono_error_set_assembly_load_simple (MonoError *oerror, const char *assembly_name, gboolean refection_only)
296 {
297         if (refection_only)
298                 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.");
299         else
300                 mono_error_set_assembly_load (oerror, assembly_name, "Could not load file or assembly '%s' or one of its dependencies.", assembly_name);
301 }
302
303 void
304 mono_error_set_type_load_class (MonoError *oerror, MonoClass *klass, const char *msg_format, ...)
305 {
306         va_list args;
307         va_start (args, msg_format);
308         mono_error_vset_type_load_class (oerror, klass, msg_format, args);
309         va_end (args);
310 }
311
312 void
313 mono_error_vset_type_load_class (MonoError *oerror, MonoClass *klass, const char *msg_format, va_list args)
314 {
315         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
316         mono_error_prepare (error);
317
318         error->error_code = MONO_ERROR_TYPE_LOAD;
319         mono_error_set_class (oerror, klass);
320         set_error_messagev ();
321 }
322
323 /*
324  * Different than other functions, this one here assumes that type_name and assembly_name to have been allocated just for us.
325  * Which means mono_error_cleanup will free them.
326  */
327 void
328 mono_error_set_type_load_name (MonoError *oerror, const char *type_name, const char *assembly_name, const char *msg_format, ...)
329 {
330         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
331         mono_error_prepare (error);
332
333         error->error_code = MONO_ERROR_TYPE_LOAD;
334         mono_error_set_type_name (oerror, type_name);
335         mono_error_set_assembly_name (oerror, assembly_name);
336         mono_error_dup_strings (oerror, FALSE);
337         set_error_message ();
338 }
339
340 void
341 mono_error_set_method_load (MonoError *oerror, MonoClass *klass, const char *method_name, const char *msg_format, ...)
342 {
343         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
344         mono_error_prepare (error);
345
346         error->error_code = MONO_ERROR_MISSING_METHOD;
347         mono_error_set_class (oerror, klass);
348         mono_error_set_member_name (oerror, method_name);
349         set_error_message ();
350 }
351
352 void
353 mono_error_set_field_load (MonoError *oerror, MonoClass *klass, const char *field_name, const char *msg_format, ...)
354 {
355         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
356         mono_error_prepare (error);
357
358         error->error_code = MONO_ERROR_MISSING_FIELD;
359         mono_error_set_class (oerror, klass);
360         mono_error_set_member_name (oerror, field_name);
361         set_error_message ();   
362 }
363
364 void
365 mono_error_set_bad_image_name (MonoError *oerror, const char *assembly_name, const char *msg_format, ...)
366 {
367         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
368         mono_error_prepare (error);
369
370         error->error_code = MONO_ERROR_BAD_IMAGE;
371         mono_error_set_assembly_name (oerror, assembly_name);
372         set_error_message ();
373 }
374
375 void
376 mono_error_set_bad_image (MonoError *oerror, MonoImage *image, 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         error->assembly_name = image ? mono_image_get_name (image) : "<no_image>";
383         set_error_message ();
384 }
385
386 void
387 mono_error_set_generic_errorv (MonoError *oerror, const char *name_space, const char *name, const char *msg_format, va_list args)
388 {
389         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
390         mono_error_prepare (error);
391
392         error->error_code = MONO_ERROR_GENERIC;
393         mono_error_set_corlib_exception (oerror, name_space, name);
394         set_error_messagev ();
395 }
396
397 void
398 mono_error_set_generic_error (MonoError *oerror, const char * name_space, const char *name, const char *msg_format, ...)
399 {
400         va_list args;
401         va_start (args, msg_format);
402         mono_error_set_generic_errorv (oerror, name_space, name, msg_format, args);
403         va_end (args);
404 }
405
406 /**
407  * mono_error_set_not_implemented:
408  *
409  * System.NotImplementedException
410  */
411 void
412 mono_error_set_not_implemented (MonoError *oerror, const char *msg_format, ...)
413 {
414         va_list args;
415         va_start (args, msg_format);
416         mono_error_set_generic_errorv (oerror, "System", "NotImplementedException", msg_format, args);
417         va_end (args);
418 }
419
420 /**
421  * mono_error_set_execution_engine:
422  *
423  * System.ExecutionEngineException
424  */
425 void
426 mono_error_set_execution_engine (MonoError *oerror, const char *msg_format, ...)
427 {
428         va_list args;
429         va_start (args, msg_format);
430         mono_error_set_generic_errorv (oerror, "System", "ExecutionEngineException", msg_format, args);
431         va_end (args);
432 }
433
434 /**
435  * mono_error_set_not_supported:
436  *
437  * System.NotSupportedException
438  */
439 void
440 mono_error_set_not_supported (MonoError *oerror, const char *msg_format, ...)
441 {
442         va_list args;
443         va_start (args, msg_format);
444         mono_error_set_generic_errorv (oerror, "System", "NotSupportedException", msg_format, args);
445         va_end (args);
446 }
447
448 /**
449  * mono_error_set_invalid_operation:
450  *
451  * System.InvalidOperationException
452  */
453 void
454 mono_error_set_invalid_operation (MonoError *oerror, const char *msg_format, ...)
455 {
456         va_list args;
457         va_start (args, msg_format);
458         mono_error_set_generic_errorv (oerror, "System", "InvalidOperationException", msg_format, args);
459         va_end (args);
460 }
461
462 /**
463  * mono_error_set_file_not_found:
464  *
465  * System.IO.FileNotFoundException
466  */
467 void
468 mono_error_set_file_not_found (MonoError *oerror, const char *msg_format, ...)
469 {
470         va_list args;
471         va_start (args, msg_format);
472         mono_error_set_generic_errorv (oerror, "System.IO", "FileNotFoundException", msg_format, args);
473         va_end (args);
474 }
475
476 void
477 mono_error_set_invalid_program (MonoError *oerror, const char *msg_format, ...)
478 {
479         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
480
481         mono_error_prepare (error);
482         error->error_code = MONO_ERROR_INVALID_PROGRAM;
483
484         set_error_message ();
485 }
486
487 /**
488  * mono_error_set_invalid_cast:
489  *
490  * System.InvalidCastException
491  */
492 void
493 mono_error_set_invalid_cast (MonoError *oerror)
494 {
495         mono_error_set_generic_error (oerror, "System", "InvalidCastException", "");
496 }
497
498 void
499 mono_error_set_exception_instance (MonoError *oerror, MonoException *exc)
500 {
501         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
502
503         mono_error_prepare (error);
504         error->error_code = MONO_ERROR_EXCEPTION_INSTANCE;
505         error->exn.instance_handle = mono_gchandle_new (exc ? &exc->object : NULL, FALSE);
506 }
507
508 void
509 mono_error_set_exception_handle (MonoError *oerror, MonoExceptionHandle exc)
510 {
511         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
512
513         mono_error_prepare (error);
514         error->error_code = MONO_ERROR_EXCEPTION_INSTANCE;
515         error->exn.instance_handle = mono_gchandle_from_handle (MONO_HANDLE_CAST(MonoObject, exc), FALSE);
516 }
517
518 void
519 mono_error_set_out_of_memory (MonoError *oerror, const char *msg_format, ...)
520 {
521         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
522         mono_error_prepare (error);
523
524         error->error_code = MONO_ERROR_OUT_OF_MEMORY;
525
526         set_error_message ();
527 }
528
529 void
530 mono_error_set_argument (MonoError *oerror, const char *argument, const char *msg_format, ...)
531 {
532         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
533         mono_error_prepare (error);
534
535         error->error_code = MONO_ERROR_ARGUMENT;
536         error->first_argument = argument;
537
538         set_error_message ();
539 }
540
541 void
542 mono_error_set_argument_null (MonoError *oerror, const char *argument, const char *msg_format, ...)
543 {
544         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
545         mono_error_prepare (error);
546
547         error->error_code = MONO_ERROR_ARGUMENT_NULL;
548         error->first_argument = argument;
549
550         set_error_message ();
551 }
552
553 void
554 mono_error_set_not_verifiable (MonoError *oerror, MonoMethod *method, const char *msg_format, ...)
555 {
556         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
557         mono_error_prepare (error);
558
559         error->error_code = MONO_ERROR_NOT_VERIFIABLE;
560         if (method) {
561                 mono_error_set_class (oerror, method->klass);
562                 mono_error_set_member_name (oerror, mono_method_full_name (method, 1));
563         }
564
565         set_error_message ();
566 }
567
568
569 /* Used by mono_error_prepare_exception - it sets its own error on mono_string_new_checked failure. */
570 static MonoString*
571 string_new_cleanup (MonoDomain *domain, const char *text)
572 {
573         MonoError ignored_err;
574         MonoString *result = mono_string_new_checked (domain, text, &ignored_err);
575         mono_error_cleanup (&ignored_err);
576         return result;
577 }
578
579 static MonoString*
580 get_type_name_as_mono_string (MonoErrorInternal *error, MonoDomain *domain, MonoError *error_out)
581 {
582         MonoString* res = NULL;
583
584         if (error->type_name) {
585                 res = string_new_cleanup (domain, error->type_name);
586                 
587         } else {
588                 MonoClass *klass = get_class (error);
589                 if (klass) {
590                         char *name = mono_type_full_name (&klass->byval_arg);
591                         if (name) {
592                                 res = string_new_cleanup (domain, name);
593                                 g_free (name);
594                         }
595                 }
596         }
597         if (!res)
598                 mono_error_set_out_of_memory (error_out, "Could not allocate type name");
599         return res;
600 }
601
602 static void
603 set_message_on_exception (MonoException *exception, MonoErrorInternal *error, MonoError *error_out)
604 {
605         MonoString *msg = string_new_cleanup (mono_domain_get (), error->full_message);
606         if (msg)
607                 MONO_OBJECT_SETREF (exception, message, msg);
608         else
609                 mono_error_set_out_of_memory (error_out, "Could not allocate exception object");
610 }
611
612 /*Can fail with out-of-memory*/
613 MonoException*
614 mono_error_prepare_exception (MonoError *oerror, MonoError *error_out)
615 {
616         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
617
618         MonoException* exception = NULL;
619         MonoString *assembly_name = NULL, *type_name = NULL, *method_name = NULL, *field_name = NULL, *msg = NULL;
620         MonoDomain *domain = mono_domain_get ();
621
622         error_init (error_out);
623
624         switch (error->error_code) {
625         case MONO_ERROR_NONE:
626                 return NULL;
627
628         case MONO_ERROR_MISSING_METHOD:
629                 if ((error->type_name || error->exn.klass) && error->member_name) {
630                         type_name = get_type_name_as_mono_string (error, domain, error_out);
631                         if (!mono_error_ok (error_out))
632                                 break;
633
634                         method_name = string_new_cleanup (domain, error->member_name);
635                         if (!method_name) {
636                                 mono_error_set_out_of_memory (error_out, "Could not allocate method name");
637                                 break;
638                         }
639
640                         exception = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "MissingMethodException", type_name, method_name, error_out);
641                         if (exception)
642                                 set_message_on_exception (exception, error, error_out);
643                 } else {
644                         exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingMethodException", error->full_message);
645                 }
646                 break;
647
648         case MONO_ERROR_MISSING_FIELD:
649                 if ((error->type_name || error->exn.klass) && error->member_name) {
650                         type_name = get_type_name_as_mono_string (error, domain, error_out);
651                         if (!mono_error_ok (error_out))
652                                 break;
653                         
654                         field_name = string_new_cleanup (domain, error->member_name);
655                         if (!field_name) {
656                                 mono_error_set_out_of_memory (error_out, "Could not allocate field name");
657                                 break;
658                         }
659                         
660                         exception = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "MissingFieldException", type_name, field_name, error_out);
661                         if (exception)
662                                 set_message_on_exception (exception, error, error_out);
663                 } else {
664                         exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingFieldException", error->full_message);
665                 }
666                 break;
667
668         case MONO_ERROR_TYPE_LOAD:
669                 if ((error->type_name && error->assembly_name) || error->exn.klass) {
670                         type_name = get_type_name_as_mono_string (error, domain, error_out);
671                         if (!mono_error_ok (error_out))
672                                 break;
673
674                         if (error->assembly_name) {
675                                 assembly_name = string_new_cleanup (domain, error->assembly_name);
676                                 if (!assembly_name) {
677                                         mono_error_set_out_of_memory (error_out, "Could not allocate assembly name");
678                                         break;
679                                 }
680                         } else {
681                                 assembly_name = mono_string_empty (domain);
682                         }
683
684                         exception = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System", "TypeLoadException", type_name, assembly_name, error_out);
685                         if (exception && error->full_message != NULL && strcmp (error->full_message, ""))
686                                 set_message_on_exception (exception, error, error_out);
687                 } else {
688                         exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "TypeLoadException", error->full_message);
689                 }
690                 break;
691
692         case MONO_ERROR_FILE_NOT_FOUND:
693         case MONO_ERROR_BAD_IMAGE:
694                 if (error->assembly_name) {
695                         msg = string_new_cleanup (domain, error->full_message);
696                         if (!msg) {
697                                 mono_error_set_out_of_memory (error_out, "Could not allocate message");
698                                 break;
699                         }
700
701                         if (error->assembly_name) {
702                                 assembly_name = string_new_cleanup (domain, error->assembly_name);
703                                 if (!assembly_name) {
704                                         mono_error_set_out_of_memory (error_out, "Could not allocate assembly name");
705                                         break;
706                                 }
707                         }
708
709                         if (error->error_code == MONO_ERROR_FILE_NOT_FOUND)
710                                 exception = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System.IO", "FileNotFoundException", msg, assembly_name, error_out);
711                         else
712                                 exception = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "BadImageFormatException", msg, assembly_name, error_out);
713                 } else {
714                         if (error->error_code == MONO_ERROR_FILE_NOT_FOUND)
715                                 exception = mono_exception_from_name_msg (mono_get_corlib (), "System.IO", "FileNotFoundException", error->full_message);
716                         else
717                                 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "BadImageFormatException", error->full_message);
718                 }
719                 break;
720
721         case MONO_ERROR_OUT_OF_MEMORY:
722                 exception = mono_get_exception_out_of_memory ();
723                 break;
724
725         case MONO_ERROR_ARGUMENT:
726                 exception = mono_get_exception_argument (error->first_argument, error->full_message);
727                 break;
728
729         case MONO_ERROR_ARGUMENT_NULL:
730                 exception = mono_get_exception_argument_null (error->first_argument);
731                 break;
732
733         case MONO_ERROR_NOT_VERIFIABLE: {
734                 char *type_name = NULL, *message;
735                 if (error->exn.klass) {
736                         type_name = mono_type_get_full_name (error->exn.klass);
737                         if (!type_name) {
738                                 mono_error_set_out_of_memory (error_out, "Could not allocate message");
739                                 break;
740                         }
741                 }
742                 message = g_strdup_printf ("Error in %s:%s %s", type_name, error->member_name, error->full_message);
743                 if (!message) {
744                         g_free (type_name);
745                         mono_error_set_out_of_memory (error_out, "Could not allocate message");
746                         break;  
747                 }
748                 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System.Security", "VerificationException", message);
749                 g_free (message);
750                 g_free (type_name);
751                 break;
752         }
753         case MONO_ERROR_GENERIC:
754                 if (!error->exception_name_space || !error->exception_name)
755                         mono_error_set_execution_engine (error_out, "MonoError with generic error but no exception name was supplied");
756                 else
757                         exception = mono_exception_from_name_msg (mono_defaults.corlib, error->exception_name_space, error->exception_name, error->full_message);
758                 break;
759
760         case MONO_ERROR_EXCEPTION_INSTANCE:
761                 exception = (MonoException*) mono_gchandle_get_target (error->exn.instance_handle);
762                 break;
763
764         case MONO_ERROR_CLEANUP_CALLED_SENTINEL:
765                 mono_error_set_execution_engine (error_out, "MonoError reused after mono_error_cleanup");
766                 break;
767
768         case MONO_ERROR_INVALID_PROGRAM: {
769                 gboolean lacks_message = error->flags & MONO_ERROR_INCOMPLETE;
770                 if (lacks_message)
771                         return mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", "");
772                 else
773                         return mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", error->full_message);
774         }
775         default:
776                 mono_error_set_execution_engine (error_out, "Invalid error-code %d", error->error_code);
777         }
778
779         if (!mono_error_ok (error_out))
780                 return NULL;
781         if (!exception)
782                 mono_error_set_out_of_memory (error_out, "Could not allocate exception object");
783         return exception;
784 }
785
786 /*
787 Convert this MonoError to an exception if it's faulty or return NULL.
788 The error object is cleant after.
789 */
790
791 MonoException*
792 mono_error_convert_to_exception (MonoError *target_error)
793 {
794         MonoError error;
795         MonoException *ex;
796
797         /* Mempool stored error shouldn't be cleaned up */
798         g_assert (!is_boxed ((MonoErrorInternal*)target_error));
799
800         if (mono_error_ok (target_error))
801                 return NULL;
802
803         ex = mono_error_prepare_exception (target_error, &error);
804         if (!mono_error_ok (&error)) {
805                 MonoError second_chance;
806                 /*Try to produce the exception for the second error. FIXME maybe we should log about the original one*/
807                 ex = mono_error_prepare_exception (&error, &second_chance);
808
809                 g_assert (mono_error_ok (&second_chance)); /*We can't reasonable handle double faults, maybe later.*/
810                 mono_error_cleanup (&error);
811         }
812         mono_error_cleanup (target_error);
813         return ex;
814 }
815
816 void
817 mono_error_move (MonoError *dest, MonoError *src)
818 {
819         memcpy (dest, src, sizeof (MonoErrorInternal));
820         error_init (src);
821 }
822
823 /**
824  * mono_error_box:
825  * \param ierror The input error that will be boxed.
826  * \param image The mempool of this image will hold the boxed error.
827  * Creates a new boxed error in the given mempool from \c MonoError.
828  * It does not alter \p ierror, so you still have to clean it up with
829  * \c mono_error_cleanup or \c mono_error_convert_to_exception or another such function.
830  * \returns the boxed error, or NULL if the mempool could not allocate.
831  */
832 MonoErrorBoxed*
833 mono_error_box (const MonoError *ierror, MonoImage *image)
834 {
835         MonoErrorInternal *from = (MonoErrorInternal*)ierror;
836         /* Don't know how to box a gchandle */
837         g_assert (!is_managed_exception (from));
838         MonoErrorBoxed* box = mono_image_alloc (image, sizeof (MonoErrorBoxed));
839         box->image = image;
840         mono_error_init_flags (&box->error, MONO_ERROR_MEMPOOL_BOXED);
841         MonoErrorInternal *to = (MonoErrorInternal*)&box->error;
842
843 #define DUP_STR(field) do {                                             \
844                 if (from->field) {                                      \
845                         if (!(to->field = mono_image_strdup (image, from->field))) \
846                                 to->flags |= MONO_ERROR_INCOMPLETE;     \
847                 } else {                                                \
848                         to->field = NULL;                               \
849                 }                                                       \
850         } while (0)
851
852         to->error_code = from->error_code;
853         DUP_STR (type_name);
854         DUP_STR (assembly_name);
855         DUP_STR (member_name);
856         DUP_STR (exception_name_space);
857         DUP_STR (exception_name);
858         DUP_STR (full_message);
859         DUP_STR (full_message_with_fields);
860         DUP_STR (first_argument);
861         to->exn.klass = from->exn.klass;
862
863 #undef DUP_STR
864         
865         return box;
866 }
867
868
869 /**
870  * mono_error_set_from_boxed:
871  * \param oerror The error that will be set to the contents of the box.
872  * \param box A mempool-allocated error.
873  * Sets the error condition in the oerror from the contents of the
874  * given boxed error.  Does not alter the boxed error, so it can be
875  * used in a future call to \c mono_error_set_from_boxed as needed.  The
876  * \p oerror should've been previously initialized with \c mono_error_init,
877  * as usual.
878  * \returns TRUE on success or FALSE on failure.
879  */
880 gboolean
881 mono_error_set_from_boxed (MonoError *oerror, const MonoErrorBoxed *box)
882 {
883         MonoErrorInternal* to = (MonoErrorInternal*)oerror;
884         MonoErrorInternal* from = (MonoErrorInternal*)&box->error;
885         g_assert (!is_managed_exception (from));
886
887         mono_error_prepare (to);
888         to->flags |= MONO_ERROR_FREE_STRINGS;
889 #define DUP_STR(field)  do {                                            \
890                 if (from->field) {                                      \
891                         if (!(to->field = g_strdup (from->field)))      \
892                                 to->flags |= MONO_ERROR_INCOMPLETE;     \
893                 } else {                                                \
894                         to->field = NULL;                               \
895                 }                                                       \
896         } while (0)
897
898         to->error_code = from->error_code;
899         DUP_STR (type_name);
900         DUP_STR (assembly_name);
901         DUP_STR (member_name);
902         DUP_STR (exception_name_space);
903         DUP_STR (exception_name);
904         DUP_STR (full_message);
905         DUP_STR (full_message_with_fields);
906         DUP_STR (first_argument);
907         to->exn.klass = from->exn.klass;
908                   
909 #undef DUP_STR
910         return (to->flags & MONO_ERROR_INCOMPLETE) == 0 ;
911 }