Merge pull request #3406 from alexanderkyte/mobile_static_default
[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_out_of_memory (MonoError *oerror, const char *msg_format, ...)
485 {
486         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
487         mono_error_prepare (error);
488
489         error->error_code = MONO_ERROR_OUT_OF_MEMORY;
490
491         set_error_message ();
492 }
493
494 void
495 mono_error_set_argument (MonoError *oerror, const char *argument, const char *msg_format, ...)
496 {
497         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
498         mono_error_prepare (error);
499
500         error->error_code = MONO_ERROR_ARGUMENT;
501         error->first_argument = argument;
502
503         set_error_message ();
504 }
505
506 void
507 mono_error_set_argument_null (MonoError *oerror, const char *argument, const char *msg_format, ...)
508 {
509         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
510         mono_error_prepare (error);
511
512         error->error_code = MONO_ERROR_ARGUMENT_NULL;
513         error->first_argument = argument;
514
515         set_error_message ();
516 }
517
518 void
519 mono_error_set_not_verifiable (MonoError *oerror, MonoMethod *method, const char *msg_format, ...)
520 {
521         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
522         mono_error_prepare (error);
523
524         error->error_code = MONO_ERROR_NOT_VERIFIABLE;
525         if (method) {
526                 mono_error_set_class (oerror, method->klass);
527                 mono_error_set_member_name (oerror, mono_method_full_name (method, 1));
528         }
529
530         set_error_message ();
531 }
532
533
534 static MonoString*
535 get_type_name_as_mono_string (MonoErrorInternal *error, MonoDomain *domain, MonoError *error_out)
536 {
537         MonoString* res = NULL;
538
539         if (error->type_name) {
540                 res = mono_string_new (domain, error->type_name);
541                 
542         } else {
543                 MonoClass *klass = get_class (error);
544                 if (klass) {
545                         char *name = mono_type_full_name (&klass->byval_arg);
546                         if (name) {
547                                 res = mono_string_new (domain, name);
548                                 g_free (name);
549                         }
550                 }
551         }
552         if (!res)
553                 mono_error_set_out_of_memory (error_out, "Could not allocate type name");
554         return res;
555 }
556
557 static void
558 set_message_on_exception (MonoException *exception, MonoErrorInternal *error, MonoError *error_out)
559 {
560         MonoString *msg = mono_string_new (mono_domain_get (), error->full_message);
561         if (msg)
562                 MONO_OBJECT_SETREF (exception, message, msg);
563         else
564                 mono_error_set_out_of_memory (error_out, "Could not allocate exception object");
565 }
566
567 /*Can fail with out-of-memory*/
568 MonoException*
569 mono_error_prepare_exception (MonoError *oerror, MonoError *error_out)
570 {
571         MonoErrorInternal *error = (MonoErrorInternal*)oerror;
572
573         MonoException* exception = NULL;
574         MonoString *assembly_name = NULL, *type_name = NULL, *method_name = NULL, *field_name = NULL, *msg = NULL;
575         MonoDomain *domain = mono_domain_get ();
576
577         mono_error_init (error_out);
578
579         switch (error->error_code) {
580         case MONO_ERROR_NONE:
581                 return NULL;
582
583         case MONO_ERROR_MISSING_METHOD:
584                 if ((error->type_name || error->exn.klass) && error->member_name) {
585                         type_name = get_type_name_as_mono_string (error, domain, error_out);
586                         if (!mono_error_ok (error_out))
587                                 break;
588
589                         method_name = mono_string_new (domain, error->member_name);
590                         if (!method_name) {
591                                 mono_error_set_out_of_memory (error_out, "Could not allocate method name");
592                                 break;
593                         }
594
595                         exception = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "MissingMethodException", type_name, method_name, error_out);
596                         if (exception)
597                                 set_message_on_exception (exception, error, error_out);
598                 } else {
599                         exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingMethodException", error->full_message);
600                 }
601                 break;
602
603         case MONO_ERROR_MISSING_FIELD:
604                 if ((error->type_name || error->exn.klass) && error->member_name) {
605                         type_name = get_type_name_as_mono_string (error, domain, error_out);
606                         if (!mono_error_ok (error_out))
607                                 break;
608                         
609                         field_name = mono_string_new (domain, error->member_name);
610                         if (!field_name) {
611                                 mono_error_set_out_of_memory (error_out, "Could not allocate field name");
612                                 break;
613                         }
614                         
615                         exception = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "MissingFieldException", type_name, field_name, error_out);
616                         if (exception)
617                                 set_message_on_exception (exception, error, error_out);
618                 } else {
619                         exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingFieldException", error->full_message);
620                 }
621                 break;
622
623         case MONO_ERROR_TYPE_LOAD:
624                 if ((error->type_name && error->assembly_name) || error->exn.klass) {
625                         type_name = get_type_name_as_mono_string (error, domain, error_out);
626                         if (!mono_error_ok (error_out))
627                                 break;
628
629                         if (error->assembly_name) {
630                                 assembly_name = mono_string_new (domain, error->assembly_name);
631                                 if (!assembly_name) {
632                                         mono_error_set_out_of_memory (error_out, "Could not allocate assembly name");
633                                         break;
634                                 }
635                         }
636
637                         exception = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System", "TypeLoadException", type_name, assembly_name, error_out);
638                         if (exception && error->full_message != NULL && strcmp (error->full_message, ""))
639                                 set_message_on_exception (exception, error, error_out);
640                 } else {
641                         exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "TypeLoadException", error->full_message);
642                 }
643                 break;
644
645         case MONO_ERROR_FILE_NOT_FOUND:
646         case MONO_ERROR_BAD_IMAGE:
647                 if (error->assembly_name) {
648                         msg = mono_string_new (domain, error->full_message);
649                         if (!msg) {
650                                 mono_error_set_out_of_memory (error_out, "Could not allocate message");
651                                 break;
652                         }
653
654                         if (error->assembly_name) {
655                                 assembly_name = mono_string_new (domain, error->assembly_name);
656                                 if (!assembly_name) {
657                                         mono_error_set_out_of_memory (error_out, "Could not allocate assembly name");
658                                         break;
659                                 }
660                         }
661
662                         if (error->error_code == MONO_ERROR_FILE_NOT_FOUND)
663                                 exception = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System.IO", "FileNotFoundException", msg, assembly_name, error_out);
664                         else
665                                 exception = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "BadImageFormatException", msg, assembly_name, error_out);
666                 } else {
667                         if (error->error_code == MONO_ERROR_FILE_NOT_FOUND)
668                                 exception = mono_exception_from_name_msg (mono_get_corlib (), "System.IO", "FileNotFoundException", error->full_message);
669                         else
670                                 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "BadImageFormatException", error->full_message);
671                 }
672                 break;
673
674         case MONO_ERROR_OUT_OF_MEMORY:
675                 exception = mono_get_exception_out_of_memory ();
676                 break;
677
678         case MONO_ERROR_ARGUMENT:
679                 exception = mono_get_exception_argument (error->first_argument, error->full_message);
680                 break;
681
682         case MONO_ERROR_ARGUMENT_NULL:
683                 exception = mono_get_exception_argument_null (error->first_argument);
684                 break;
685
686         case MONO_ERROR_NOT_VERIFIABLE: {
687                 char *type_name = NULL, *message;
688                 if (error->exn.klass) {
689                         type_name = mono_type_get_full_name (error->exn.klass);
690                         if (!type_name) {
691                                 mono_error_set_out_of_memory (error_out, "Could not allocate message");
692                                 break;
693                         }
694                 }
695                 message = g_strdup_printf ("Error in %s:%s %s", type_name, error->member_name, error->full_message);
696                 if (!message) {
697                         g_free (type_name);
698                         mono_error_set_out_of_memory (error_out, "Could not allocate message");
699                         break;  
700                 }
701                 exception = mono_exception_from_name_msg (mono_defaults.corlib, "System.Security", "VerificationException", message);
702                 g_free (message);
703                 g_free (type_name);
704                 break;
705         }
706         case MONO_ERROR_GENERIC:
707                 if (!error->exception_name_space || !error->exception_name)
708                         mono_error_set_execution_engine (error_out, "MonoError with generic error but no exception name was supplied");
709                 else
710                         exception = mono_exception_from_name_msg (mono_defaults.corlib, error->exception_name_space, error->exception_name, error->full_message);
711                 break;
712
713         case MONO_ERROR_EXCEPTION_INSTANCE:
714                 exception = (MonoException*) mono_gchandle_get_target (error->exn.instance_handle);
715                 break;
716
717         case MONO_ERROR_CLEANUP_CALLED_SENTINEL:
718                 mono_error_set_execution_engine (error_out, "MonoError reused after mono_error_cleanup");
719                 break;
720
721         case MONO_ERROR_INVALID_PROGRAM: {
722                 gboolean lacks_message = error->flags & MONO_ERROR_INCOMPLETE;
723                 if (lacks_message)
724                         return mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", "");
725                 else
726                         return mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", error->full_message);
727         }
728         default:
729                 mono_error_set_execution_engine (error_out, "Invalid error-code %d", error->error_code);
730         }
731
732         if (!mono_error_ok (error_out))
733                 return NULL;
734         if (!exception)
735                 mono_error_set_out_of_memory (error_out, "Could not allocate exception object");
736         return exception;
737 }
738
739 /*
740 Convert this MonoError to an exception if it's faulty or return NULL.
741 The error object is cleant after.
742 */
743
744 MonoException*
745 mono_error_convert_to_exception (MonoError *target_error)
746 {
747         MonoError error;
748         MonoException *ex;
749
750         /* Mempool stored error shouldn't be cleaned up */
751         g_assert (!is_boxed ((MonoErrorInternal*)target_error));
752
753         if (mono_error_ok (target_error))
754                 return NULL;
755
756         ex = mono_error_prepare_exception (target_error, &error);
757         if (!mono_error_ok (&error)) {
758                 MonoError second_chance;
759                 /*Try to produce the exception for the second error. FIXME maybe we should log about the original one*/
760                 ex = mono_error_prepare_exception (&error, &second_chance);
761
762                 g_assert (mono_error_ok (&second_chance)); /*We can't reasonable handle double faults, maybe later.*/
763                 mono_error_cleanup (&error);
764         }
765         mono_error_cleanup (target_error);
766         return ex;
767 }
768
769 void
770 mono_error_move (MonoError *dest, MonoError *src)
771 {
772         memcpy (dest, src, sizeof (MonoErrorInternal));
773         mono_error_init (src);
774 }
775
776 /**
777  * mono_error_box:
778  * @ierror: The input error that will be boxed.
779  * @image: The mempool of this image will hold the boxed error.
780  *
781  * Creates a new boxed error in the given mempool from MonoError.
782  * It does not alter ierror, so you still have to clean it up with
783  * mono_error_cleanup or mono_error_convert_to_exception or another such function.
784  *
785  * Returns the boxed error, or NULL if the mempool could not allocate.
786  */
787 MonoErrorBoxed*
788 mono_error_box (const MonoError *ierror, MonoImage *image)
789 {
790         MonoErrorInternal *from = (MonoErrorInternal*)ierror;
791         /* Don't know how to box a gchandle */
792         g_assert (!is_managed_exception (from));
793         MonoErrorBoxed* box = mono_image_alloc (image, sizeof (MonoErrorBoxed));
794         box->image = image;
795         mono_error_init_flags (&box->error, MONO_ERROR_MEMPOOL_BOXED);
796         MonoErrorInternal *to = (MonoErrorInternal*)&box->error;
797
798 #define DUP_STR(field) do {                                             \
799                 if (from->field) {                                      \
800                         if (!(to->field = mono_image_strdup (image, from->field))) \
801                                 to->flags |= MONO_ERROR_INCOMPLETE;     \
802                 } else {                                                \
803                         to->field = NULL;                               \
804                 }                                                       \
805         } while (0)
806
807         to->error_code = from->error_code;
808         DUP_STR (type_name);
809         DUP_STR (assembly_name);
810         DUP_STR (member_name);
811         DUP_STR (exception_name_space);
812         DUP_STR (exception_name);
813         DUP_STR (full_message);
814         DUP_STR (full_message_with_fields);
815         DUP_STR (first_argument);
816         to->exn.klass = from->exn.klass;
817
818 #undef DUP_STR
819         
820         return box;
821 }
822
823
824 /**
825  * mono_error_set_from_boxed:
826  * @oerror: The error that will be set to the contents of the box.
827  * @box: A mempool-allocated error.
828  *
829  * Sets the error condition in the oerror from the contents of the
830  * given boxed error.  Does not alter the boxed error, so it can be
831  * used in a future call to mono_error_set_from_boxed as needed.  The
832  * oerror should've been previously initialized with mono_error_init,
833  * as usual.
834  *
835  * Returns TRUE on success or FALSE on failure.
836  */
837 gboolean
838 mono_error_set_from_boxed (MonoError *oerror, const MonoErrorBoxed *box)
839 {
840         MonoErrorInternal* to = (MonoErrorInternal*)oerror;
841         MonoErrorInternal* from = (MonoErrorInternal*)&box->error;
842         g_assert (!is_managed_exception (from));
843
844         mono_error_prepare (to);
845         to->flags |= MONO_ERROR_FREE_STRINGS;
846 #define DUP_STR(field)  do {                                            \
847                 if (from->field) {                                      \
848                         if (!(to->field = g_strdup (from->field)))      \
849                                 to->flags |= MONO_ERROR_INCOMPLETE;     \
850                 } else {                                                \
851                         to->field = NULL;                               \
852                 }                                                       \
853         } while (0)
854
855         to->error_code = from->error_code;
856         DUP_STR (type_name);
857         DUP_STR (assembly_name);
858         DUP_STR (member_name);
859         DUP_STR (exception_name_space);
860         DUP_STR (exception_name);
861         DUP_STR (full_message);
862         DUP_STR (full_message_with_fields);
863         DUP_STR (first_argument);
864         to->exn.klass = from->exn.klass;
865                   
866 #undef DUP_STR
867         return (to->flags & MONO_ERROR_INCOMPLETE) == 0 ;
868 }