[runtime] Call error_init instead of mono_error_init (#4425)
[mono.git] / mono / utils / mono-error.c
1 /*
2  * mono-error.c: Error handling code
3  *
4  * Authors:
5  *      Rodrigo Kumpera    (rkumpera@novell.com)
6  * Copyright 2009 Novell, Inc (http://www.novell.com)
7  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
8  */
9 #include <glib.h>
10
11 #include <config.h>
12 #include "mono-error.h"
13 #include "mono-error-internals.h"
14
15 #include <mono/metadata/exception.h>
16 #include <mono/metadata/exception-internals.h>
17 #include <mono/metadata/debug-helpers.h>
18 #include <mono/metadata/object-internals.h>
19
20 #define set_error_messagev() do { \
21         if (!(error->full_message = g_strdup_vprintf (msg_format, args))) \
22                         error->flags |= MONO_ERROR_INCOMPLETE; \
23 } while (0)
24
25 #define set_error_message() do { \
26         va_list args; \
27         va_start (args, msg_format); \
28         set_error_messagev();        \
29         va_end (args); \
30 } while (0)
31
32 static void
33 mono_error_set_generic_errorv (MonoError *oerror, const char *name_space, const char *name, const char *msg_format, va_list args);
34
35 static gboolean
36 is_managed_exception (MonoErrorInternal *error)
37 {
38         return (error->error_code == MONO_ERROR_EXCEPTION_INSTANCE);
39 }
40
41 static gboolean
42 is_boxed (MonoErrorInternal *error)
43 {
44         return ((error->flags & MONO_ERROR_MEMPOOL_BOXED) != 0);
45 }
46
47 static void
48 mono_error_prepare (MonoErrorInternal *error)
49 {
50         /* mono_error_set_* after a mono_error_cleanup without an intervening init */
51         g_assert (error->error_code != MONO_ERROR_CLEANUP_CALLED_SENTINEL);
52         if (error->error_code != MONO_ERROR_NONE)
53                 return;
54
55         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;
56         error->exn.klass = NULL;
57 }
58
59 static MonoClass*
60 get_class (MonoErrorInternal *error)
61 {
62         MonoClass *klass = NULL;
63         if (is_managed_exception (error))
64                 klass = mono_object_class (mono_gchandle_get_target (error->exn.instance_handle));
65         else
66                 klass = error->exn.klass;
67         return klass;
68 }
69
70 static const char*
71 get_type_name (MonoErrorInternal *error)
72 {
73         if (error->type_name)
74                 return error->type_name;
75         MonoClass *klass = get_class (error);
76         if (klass)
77                 return klass->name;
78         return "<unknown type>";
79 }
80
81 static const char*
82 get_assembly_name (MonoErrorInternal *error)
83 {
84         if (error->assembly_name)
85                 return error->assembly_name;
86         MonoClass *klass = get_class (error);
87         if (klass && klass->image)
88                 return klass->image->name;
89         return "<unknown assembly>";
90 }
91
92 void
93 mono_error_init_flags (MonoError *oerror, unsigned short flags)
94 {
95         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
96         g_assert (sizeof (MonoError) == sizeof (MonoErrorInternal));
97
98         error->error_code = MONO_ERROR_NONE;
99         error->flags = flags;
100 }
101
102 /**
103  * mono_error_init:
104  * @error: Pointer to MonoError struct to initialize
105  *
106  * Any function which takes a MonoError for purposes of reporting an error
107  * is required to call either this or 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 void
463 mono_error_set_invalid_program (MonoError *oerror, const char *msg_format, ...)
464 {
465         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
466
467         mono_error_prepare (error);
468         error->error_code = MONO_ERROR_INVALID_PROGRAM;
469
470         set_error_message ();
471 }
472
473 void
474 mono_error_set_exception_instance (MonoError *oerror, MonoException *exc)
475 {
476         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
477
478         mono_error_prepare (error);
479         error->error_code = MONO_ERROR_EXCEPTION_INSTANCE;
480         error->exn.instance_handle = mono_gchandle_new (exc ? &exc->object : NULL, FALSE);
481 }
482
483 void
484 mono_error_set_exception_handle (MonoError *oerror, MonoExceptionHandle exc)
485 {
486         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
487
488         mono_error_prepare (error);
489         error->error_code = MONO_ERROR_EXCEPTION_INSTANCE;
490         error->exn.instance_handle = mono_gchandle_from_handle (MONO_HANDLE_CAST(MonoObject, exc), FALSE);
491 }
492
493 void
494 mono_error_set_out_of_memory (MonoError *oerror, const char *msg_format, ...)
495 {
496         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
497         mono_error_prepare (error);
498
499         error->error_code = MONO_ERROR_OUT_OF_MEMORY;
500
501         set_error_message ();
502 }
503
504 void
505 mono_error_set_argument (MonoError *oerror, const char *argument, const char *msg_format, ...)
506 {
507         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
508         mono_error_prepare (error);
509
510         error->error_code = MONO_ERROR_ARGUMENT;
511         error->first_argument = argument;
512
513         set_error_message ();
514 }
515
516 void
517 mono_error_set_argument_null (MonoError *oerror, const char *argument, const char *msg_format, ...)
518 {
519         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
520         mono_error_prepare (error);
521
522         error->error_code = MONO_ERROR_ARGUMENT_NULL;
523         error->first_argument = argument;
524
525         set_error_message ();
526 }
527
528 void
529 mono_error_set_not_verifiable (MonoError *oerror, MonoMethod *method, const char *msg_format, ...)
530 {
531         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
532         mono_error_prepare (error);
533
534         error->error_code = MONO_ERROR_NOT_VERIFIABLE;
535         if (method) {
536                 mono_error_set_class (oerror, method->klass);
537                 mono_error_set_member_name (oerror, mono_method_full_name (method, 1));
538         }
539
540         set_error_message ();
541 }
542
543
544 static MonoString*
545 get_type_name_as_mono_string (MonoErrorInternal *error, MonoDomain *domain, MonoError *error_out)
546 {
547         MonoString* res = NULL;
548
549         if (error->type_name) {
550                 res = mono_string_new (domain, error->type_name);
551                 
552         } else {
553                 MonoClass *klass = get_class (error);
554                 if (klass) {
555                         char *name = mono_type_full_name (&klass->byval_arg);
556                         if (name) {
557                                 res = mono_string_new (domain, name);
558                                 g_free (name);
559                         }
560                 }
561         }
562         if (!res)
563                 mono_error_set_out_of_memory (error_out, "Could not allocate type name");
564         return res;
565 }
566
567 static void
568 set_message_on_exception (MonoException *exception, MonoErrorInternal *error, MonoError *error_out)
569 {
570         MonoString *msg = mono_string_new (mono_domain_get (), error->full_message);
571         if (msg)
572                 MONO_OBJECT_SETREF (exception, message, msg);
573         else
574                 mono_error_set_out_of_memory (error_out, "Could not allocate exception object");
575 }
576
577 /*Can fail with out-of-memory*/
578 MonoException*
579 mono_error_prepare_exception (MonoError *oerror, MonoError *error_out)
580 {
581         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
582
583         MonoException* exception = NULL;
584         MonoString *assembly_name = NULL, *type_name = NULL, *method_name = NULL, *field_name = NULL, *msg = NULL;
585         MonoDomain *domain = mono_domain_get ();
586
587         error_init (error_out);
588
589         switch (error->error_code) {
590         case MONO_ERROR_NONE:
591                 return NULL;
592
593         case MONO_ERROR_MISSING_METHOD:
594                 if ((error->type_name || error->exn.klass) && error->member_name) {
595                         type_name = get_type_name_as_mono_string (error, domain, error_out);
596                         if (!mono_error_ok (error_out))
597                                 break;
598
599                         method_name = mono_string_new (domain, error->member_name);
600                         if (!method_name) {
601                                 mono_error_set_out_of_memory (error_out, "Could not allocate method name");
602                                 break;
603                         }
604
605                         exception = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "MissingMethodException", type_name, method_name, error_out);
606                         if (exception)
607                                 set_message_on_exception (exception, error, error_out);
608                 } else {
609                         exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingMethodException", error->full_message);
610                 }
611                 break;
612
613         case MONO_ERROR_MISSING_FIELD:
614                 if ((error->type_name || error->exn.klass) && error->member_name) {
615                         type_name = get_type_name_as_mono_string (error, domain, error_out);
616                         if (!mono_error_ok (error_out))
617                                 break;
618                         
619                         field_name = mono_string_new (domain, error->member_name);
620                         if (!field_name) {
621                                 mono_error_set_out_of_memory (error_out, "Could not allocate field name");
622                                 break;
623                         }
624                         
625                         exception = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "MissingFieldException", type_name, field_name, error_out);
626                         if (exception)
627                                 set_message_on_exception (exception, error, error_out);
628                 } else {
629                         exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingFieldException", error->full_message);
630                 }
631                 break;
632
633         case MONO_ERROR_TYPE_LOAD:
634                 if ((error->type_name && error->assembly_name) || error->exn.klass) {
635                         type_name = get_type_name_as_mono_string (error, domain, error_out);
636                         if (!mono_error_ok (error_out))
637                                 break;
638
639                         if (error->assembly_name) {
640                                 assembly_name = mono_string_new (domain, error->assembly_name);
641                                 if (!assembly_name) {
642                                         mono_error_set_out_of_memory (error_out, "Could not allocate assembly name");
643                                         break;
644                                 }
645                         }
646
647                         exception = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System", "TypeLoadException", type_name, assembly_name, error_out);
648                         if (exception && error->full_message != NULL && strcmp (error->full_message, ""))
649                                 set_message_on_exception (exception, error, error_out);
650                 } else {
651                         exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "TypeLoadException", error->full_message);
652                 }
653                 break;
654
655         case MONO_ERROR_FILE_NOT_FOUND:
656         case MONO_ERROR_BAD_IMAGE:
657                 if (error->assembly_name) {
658                         msg = mono_string_new (domain, error->full_message);
659                         if (!msg) {
660                                 mono_error_set_out_of_memory (error_out, "Could not allocate message");
661                                 break;
662                         }
663
664                         if (error->assembly_name) {
665                                 assembly_name = mono_string_new (domain, error->assembly_name);
666                                 if (!assembly_name) {
667                                         mono_error_set_out_of_memory (error_out, "Could not allocate assembly name");
668                                         break;
669                                 }
670                         }
671
672                         if (error->error_code == MONO_ERROR_FILE_NOT_FOUND)
673                                 exception = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System.IO", "FileNotFoundException", msg, assembly_name, error_out);
674                         else
675                                 exception = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "BadImageFormatException", msg, assembly_name, error_out);
676                 } else {
677                         if (error->error_code == MONO_ERROR_FILE_NOT_FOUND)
678                                 exception = mono_exception_from_name_msg (mono_get_corlib (), "System.IO", "FileNotFoundException", error->full_message);
679                         else
680                                 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "BadImageFormatException", error->full_message);
681                 }
682                 break;
683
684         case MONO_ERROR_OUT_OF_MEMORY:
685                 exception = mono_get_exception_out_of_memory ();
686                 break;
687
688         case MONO_ERROR_ARGUMENT:
689                 exception = mono_get_exception_argument (error->first_argument, error->full_message);
690                 break;
691
692         case MONO_ERROR_ARGUMENT_NULL:
693                 exception = mono_get_exception_argument_null (error->first_argument);
694                 break;
695
696         case MONO_ERROR_NOT_VERIFIABLE: {
697                 char *type_name = NULL, *message;
698                 if (error->exn.klass) {
699                         type_name = mono_type_get_full_name (error->exn.klass);
700                         if (!type_name) {
701                                 mono_error_set_out_of_memory (error_out, "Could not allocate message");
702                                 break;
703                         }
704                 }
705                 message = g_strdup_printf ("Error in %s:%s %s", type_name, error->member_name, error->full_message);
706                 if (!message) {
707                         g_free (type_name);
708                         mono_error_set_out_of_memory (error_out, "Could not allocate message");
709                         break;  
710                 }
711                 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System.Security", "VerificationException", message);
712                 g_free (message);
713                 g_free (type_name);
714                 break;
715         }
716         case MONO_ERROR_GENERIC:
717                 if (!error->exception_name_space || !error->exception_name)
718                         mono_error_set_execution_engine (error_out, "MonoError with generic error but no exception name was supplied");
719                 else
720                         exception = mono_exception_from_name_msg (mono_defaults.corlib, error->exception_name_space, error->exception_name, error->full_message);
721                 break;
722
723         case MONO_ERROR_EXCEPTION_INSTANCE:
724                 exception = (MonoException*) mono_gchandle_get_target (error->exn.instance_handle);
725                 break;
726
727         case MONO_ERROR_CLEANUP_CALLED_SENTINEL:
728                 mono_error_set_execution_engine (error_out, "MonoError reused after mono_error_cleanup");
729                 break;
730
731         case MONO_ERROR_INVALID_PROGRAM: {
732                 gboolean lacks_message = error->flags & MONO_ERROR_INCOMPLETE;
733                 if (lacks_message)
734                         return mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", "");
735                 else
736                         return mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", error->full_message);
737         }
738         default:
739                 mono_error_set_execution_engine (error_out, "Invalid error-code %d", error->error_code);
740         }
741
742         if (!mono_error_ok (error_out))
743                 return NULL;
744         if (!exception)
745                 mono_error_set_out_of_memory (error_out, "Could not allocate exception object");
746         return exception;
747 }
748
749 /*
750 Convert this MonoError to an exception if it's faulty or return NULL.
751 The error object is cleant after.
752 */
753
754 MonoException*
755 mono_error_convert_to_exception (MonoError *target_error)
756 {
757         MonoError error;
758         MonoException *ex;
759
760         /* Mempool stored error shouldn't be cleaned up */
761         g_assert (!is_boxed ((MonoErrorInternal*)target_error));
762
763         if (mono_error_ok (target_error))
764                 return NULL;
765
766         ex = mono_error_prepare_exception (target_error, &error);
767         if (!mono_error_ok (&error)) {
768                 MonoError second_chance;
769                 /*Try to produce the exception for the second error. FIXME maybe we should log about the original one*/
770                 ex = mono_error_prepare_exception (&error, &second_chance);
771
772                 g_assert (mono_error_ok (&second_chance)); /*We can't reasonable handle double faults, maybe later.*/
773                 mono_error_cleanup (&error);
774         }
775         mono_error_cleanup (target_error);
776         return ex;
777 }
778
779 void
780 mono_error_move (MonoError *dest, MonoError *src)
781 {
782         memcpy (dest, src, sizeof (MonoErrorInternal));
783         error_init (src);
784 }
785
786 /**
787  * mono_error_box:
788  * @ierror: The input error that will be boxed.
789  * @image: The mempool of this image will hold the boxed error.
790  *
791  * Creates a new boxed error in the given mempool from MonoError.
792  * It does not alter ierror, so you still have to clean it up with
793  * mono_error_cleanup or mono_error_convert_to_exception or another such function.
794  *
795  * Returns the boxed error, or NULL if the mempool could not allocate.
796  */
797 MonoErrorBoxed*
798 mono_error_box (const MonoError *ierror, MonoImage *image)
799 {
800         MonoErrorInternal *from = (MonoErrorInternal*)ierror;
801         /* Don't know how to box a gchandle */
802         g_assert (!is_managed_exception (from));
803         MonoErrorBoxed* box = mono_image_alloc (image, sizeof (MonoErrorBoxed));
804         box->image = image;
805         mono_error_init_flags (&box->error, MONO_ERROR_MEMPOOL_BOXED);
806         MonoErrorInternal *to = (MonoErrorInternal*)&box->error;
807
808 #define DUP_STR(field) do {                                             \
809                 if (from->field) {                                      \
810                         if (!(to->field = mono_image_strdup (image, from->field))) \
811                                 to->flags |= MONO_ERROR_INCOMPLETE;     \
812                 } else {                                                \
813                         to->field = NULL;                               \
814                 }                                                       \
815         } while (0)
816
817         to->error_code = from->error_code;
818         DUP_STR (type_name);
819         DUP_STR (assembly_name);
820         DUP_STR (member_name);
821         DUP_STR (exception_name_space);
822         DUP_STR (exception_name);
823         DUP_STR (full_message);
824         DUP_STR (full_message_with_fields);
825         DUP_STR (first_argument);
826         to->exn.klass = from->exn.klass;
827
828 #undef DUP_STR
829         
830         return box;
831 }
832
833
834 /**
835  * mono_error_set_from_boxed:
836  * @oerror: The error that will be set to the contents of the box.
837  * @box: A mempool-allocated error.
838  *
839  * Sets the error condition in the oerror from the contents of the
840  * given boxed error.  Does not alter the boxed error, so it can be
841  * used in a future call to mono_error_set_from_boxed as needed.  The
842  * oerror should've been previously initialized with mono_error_init,
843  * as usual.
844  *
845  * Returns TRUE on success or FALSE on failure.
846  */
847 gboolean
848 mono_error_set_from_boxed (MonoError *oerror, const MonoErrorBoxed *box)
849 {
850         MonoErrorInternal* to = (MonoErrorInternal*)oerror;
851         MonoErrorInternal* from = (MonoErrorInternal*)&box->error;
852         g_assert (!is_managed_exception (from));
853
854         mono_error_prepare (to);
855         to->flags |= MONO_ERROR_FREE_STRINGS;
856 #define DUP_STR(field)  do {                                            \
857                 if (from->field) {                                      \
858                         if (!(to->field = g_strdup (from->field)))      \
859                                 to->flags |= MONO_ERROR_INCOMPLETE;     \
860                 } else {                                                \
861                         to->field = NULL;                               \
862                 }                                                       \
863         } while (0)
864
865         to->error_code = from->error_code;
866         DUP_STR (type_name);
867         DUP_STR (assembly_name);
868         DUP_STR (member_name);
869         DUP_STR (exception_name_space);
870         DUP_STR (exception_name);
871         DUP_STR (full_message);
872         DUP_STR (full_message_with_fields);
873         DUP_STR (first_argument);
874         to->exn.klass = from->exn.klass;
875                   
876 #undef DUP_STR
877         return (to->flags & MONO_ERROR_INCOMPLETE) == 0 ;
878 }