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