/* Convert this MonoError to an exception if it's faulty or return NULL. The error object is cleant after. */ MonoException* mono_error_convert_to_exception (MonoError *target_error) { ERROR_DECL (error); MonoException *ex; /* Mempool stored error shouldn't be cleaned up */ g_assert (!is_boxed ((MonoErrorInternal*)target_error)); if (mono_error_ok (target_error)) return NULL; ex = mono_error_prepare_exception (target_error, error); if (!mono_error_ok (error)) { ERROR_DECL (second_chance); /*Try to produce the exception for the second error. FIXME maybe we should log about the original one*/ ex = mono_error_prepare_exception (error, second_chance); // We cannot reasonably handle double faults, maybe later. g_assert (mono_error_ok (second_chance)); mono_error_cleanup (error); } mono_error_cleanup (target_error); return ex; }
MonoException * mono_get_exception_runtime_wrapped (MonoObject *wrapped_exception) { MonoError error; MonoClass *klass; MonoObject *o; MonoMethod *method; MonoDomain *domain = mono_domain_get (); gpointer params [16]; klass = mono_class_load_from_name (mono_get_corlib (), "System.Runtime.CompilerServices", "RuntimeWrappedException"); o = mono_object_new_checked (domain, klass, &error); g_assert (o != NULL && mono_error_ok (&error)); /* FIXME don't swallow the error */ method = mono_class_get_method_from_name (klass, ".ctor", 1); g_assert (method); params [0] = wrapped_exception; mono_runtime_invoke_checked (method, o, params, &error); mono_error_raise_exception (&error); /* FIXME don't raise here */ return (MonoException *)o; }
/** * mono_exception_from_token: * @image: the Mono image where to look for the class * @token: The type token of the class * * Creates an exception of the type given by @token. * * Returns: the initialized exception instance. */ MonoException * mono_exception_from_token (MonoImage *image, guint32 token) { MonoError error; MonoClass *klass; MonoObject *o; klass = mono_class_get_checked (image, token, &error); g_assert (mono_error_ok (&error)); /* FIXME handle the error. */ o = mono_object_new_checked (mono_domain_get (), klass, &error); g_assert (o != NULL && mono_error_ok (&error)); /* FIXME don't swallow the error */ mono_runtime_object_init (o); return (MonoException *)o; }
void mono_error_assert_ok_pos (MonoError *error, const char* filename, int lineno) { if (mono_error_ok (error)) return; g_error ("%s:%d: %s\n", filename, lineno, mono_error_get_message (error)); }
MonoExceptionHandle mono_error_convert_to_exception_handle (MonoError *error) { //FIXMEcoop mono_error_convert_to_exception is raw pointer // The "optimization" here is important to significantly reduce handle usage. return mono_error_ok (error) ? MONO_HANDLE_CAST (MonoException, NULL_HANDLE) : MONO_HANDLE_NEW (MonoException, mono_error_convert_to_exception (error)); }
/* * mono_basic_block_split: * * Return the list of basic blocks of method. Return NULL on failure and set @error. */ MonoSimpleBasicBlock* mono_basic_block_split (MonoMethod *method, MonoError *error) { MonoSimpleBasicBlock *bb, *root; const unsigned char *start, *end; MonoMethodHeader *header = mono_method_get_header (method); mono_error_init (error); if (!header) { mono_error_set_not_verifiable (error, method, "Could not decode header"); return NULL; } start = header->code; end = start + header->code_size; bb = g_new0 (MonoSimpleBasicBlock, 1); bb->start = 0; bb->end = end - start; bb->colour = BLACK; bb->dead = FALSE; root = bb; bb_formation_il_pass (start, end, bb, &root, method, error); if (!mono_error_ok (error)) goto fail; bb_formation_eh_pass (header, bb, &root, method, error); if (!mono_error_ok (error)) goto fail; bb_liveness (bb); #if DEBUG_BB dump_bb_list (bb, &root, g_strdup_printf("AFTER LIVENESS %s", mono_method_full_name (method, TRUE))); #endif mono_metadata_free_mh (header); return bb; fail: mono_metadata_free_mh (header); mono_basic_block_free (bb); return NULL; }
/** * mono_exception_from_token_two_strings: * * Same as mono_exception_from_name_two_strings, but lookup the exception class using * IMAGE and TOKEN. */ MonoException * mono_exception_from_token_two_strings (MonoImage *image, guint32 token, MonoString *a1, MonoString *a2) { MonoError error; MonoClass *klass = mono_class_get_checked (image, token, &error); g_assert (mono_error_ok (&error)); /* FIXME handle the error. */ return create_exception_two_strings (klass, a1, a2); }
MonoException* mono_error_convert_to_exception (MonoError *target_error) { MonoError error; MonoException *ex; if (mono_error_ok (target_error)) return NULL; ex = mono_error_prepare_exception (target_error, &error); if (!mono_error_ok (&error)) { MonoError second_chance; /*Try to produce the exception for the second error. FIXME maybe we should log about the original one*/ ex = mono_error_prepare_exception (&error, &second_chance); g_assert (mono_error_ok (&second_chance)); /*We can't reasonable handle double faults, maybe later.*/ mono_error_cleanup (&error); } mono_error_cleanup (target_error); return ex; }
/** * mono_exception_from_name_domain: * @domain: Domain where the return object will be created. * @image: the Mono image where to look for the class * @name_space: the namespace for the class * @name: class name * * Creates an exception object of the given namespace/name class on * the given domain. * * Returns: the initialized exception instance. */ MonoException * mono_exception_from_name_domain (MonoDomain *domain, MonoImage *image, const char* name_space, const char *name) { MonoError error; MonoClass *klass; MonoObject *o; MonoDomain *caller_domain = mono_domain_get (); klass = mono_class_load_from_name (image, name_space, name); o = mono_object_new_checked (domain, klass, &error); g_assert (o != NULL && mono_error_ok (&error)); /* FIXME don't swallow the error */ if (domain != caller_domain) mono_domain_set_internal (domain); mono_runtime_object_init (o); if (domain != caller_domain) mono_domain_set_internal (caller_domain); return (MonoException *)o; }
/*Can fail with out-of-memory*/ MonoException* mono_error_prepare_exception (MonoError *oerror, MonoError *error_out) { MonoErrorInternal *error = (MonoErrorInternal*)oerror; MonoException* exception = NULL; MonoString *assembly_name = NULL, *type_name = NULL, *method_name = NULL, *field_name = NULL, *msg = NULL; MonoDomain *domain = mono_domain_get (); mono_error_init (error_out); switch (error->error_code) { case MONO_ERROR_NONE: return NULL; case MONO_ERROR_MISSING_METHOD: if ((error->type_name || error->exn.klass) && error->member_name) { type_name = get_type_name_as_mono_string (error, domain, error_out); if (!mono_error_ok (error_out)) break; method_name = mono_string_new (domain, error->member_name); if (!method_name) { mono_error_set_out_of_memory (error_out, "Could not allocate method name"); break; } exception = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "MissingMethodException", type_name, method_name, error_out); if (exception) set_message_on_exception (exception, error, error_out); } else { exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingMethodException", error->full_message); } break; case MONO_ERROR_MISSING_FIELD: if ((error->type_name || error->exn.klass) && error->member_name) { type_name = get_type_name_as_mono_string (error, domain, error_out); if (!mono_error_ok (error_out)) break; field_name = mono_string_new (domain, error->member_name); if (!field_name) { mono_error_set_out_of_memory (error_out, "Could not allocate field name"); break; } exception = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "MissingFieldException", type_name, field_name, error_out); if (exception) set_message_on_exception (exception, error, error_out); } else { exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingFieldException", error->full_message); } break; case MONO_ERROR_TYPE_LOAD: if ((error->type_name && error->assembly_name) || error->exn.klass) { type_name = get_type_name_as_mono_string (error, domain, error_out); if (!mono_error_ok (error_out)) break; if (error->assembly_name) { assembly_name = mono_string_new (domain, error->assembly_name); if (!assembly_name) { mono_error_set_out_of_memory (error_out, "Could not allocate assembly name"); break; } } exception = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System", "TypeLoadException", type_name, assembly_name, error_out); if (exception) set_message_on_exception (exception, error, error_out); } else { exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "TypeLoadException", error->full_message); } break; case MONO_ERROR_FILE_NOT_FOUND: case MONO_ERROR_BAD_IMAGE: if (error->assembly_name) { msg = mono_string_new (domain, error->full_message); if (!msg) { mono_error_set_out_of_memory (error_out, "Could not allocate message"); break; } if (error->assembly_name) { assembly_name = mono_string_new (domain, error->assembly_name); if (!assembly_name) { mono_error_set_out_of_memory (error_out, "Could not allocate assembly name"); break; } } if (error->error_code == MONO_ERROR_FILE_NOT_FOUND) exception = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System.IO", "FileNotFoundException", msg, assembly_name, error_out); else exception = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, "System", "BadImageFormatException", msg, assembly_name, error_out); } else { if (error->error_code == MONO_ERROR_FILE_NOT_FOUND) exception = mono_exception_from_name_msg (mono_get_corlib (), "System.IO", "FileNotFoundException", error->full_message); else exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "BadImageFormatException", error->full_message); } break; case MONO_ERROR_OUT_OF_MEMORY: exception = mono_get_exception_out_of_memory (); break; case MONO_ERROR_ARGUMENT: exception = mono_get_exception_argument (error->first_argument, error->full_message); break; case MONO_ERROR_ARGUMENT_NULL: exception = mono_get_exception_argument_null (error->first_argument); break; case MONO_ERROR_NOT_VERIFIABLE: { char *type_name = NULL, *message; if (error->exn.klass) { type_name = mono_type_get_full_name (error->exn.klass); if (!type_name) { mono_error_set_out_of_memory (error_out, "Could not allocate message"); break; } } message = g_strdup_printf ("Error in %s:%s %s", type_name, error->member_name, error->full_message); if (!message) { g_free (type_name); mono_error_set_out_of_memory (error_out, "Could not allocate message"); break; } exception = mono_exception_from_name_msg (mono_defaults.corlib, "System.Security", "VerificationException", message); g_free (message); g_free (type_name); break; } case MONO_ERROR_GENERIC: if (!error->exception_name_space || !error->exception_name) mono_error_set_execution_engine (error_out, "MonoError with generic error but no exception name was supplied"); else exception = mono_exception_from_name_msg (mono_defaults.corlib, error->exception_name_space, error->exception_name, error->full_message); break; case MONO_ERROR_EXCEPTION_INSTANCE: exception = (MonoException*) mono_gchandle_get_target (error->exn.instance_handle); break; case MONO_ERROR_CLEANUP_CALLED_SENTINEL: mono_error_set_execution_engine (error_out, "MonoError reused after mono_error_cleanup"); break; case MONO_ERROR_INVALID_PROGRAM: { gboolean lacks_message = error->flags & MONO_ERROR_INCOMPLETE; if (lacks_message) return mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", ""); else return mono_exception_from_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", error->full_message); } default: mono_error_set_execution_engine (error_out, "Invalid error-code %d", error->error_code); } if (!mono_error_ok (error_out)) return NULL; if (!exception) mono_error_set_out_of_memory (error_out, "Could not allocate exception object"); return exception; }
/*Can fail with out-of-memory*/ MonoException* mono_error_prepare_exception (MonoError *oerror, MonoError *error_out) { HANDLE_FUNCTION_ENTER (); MonoErrorInternal *error = (MonoErrorInternal*)oerror; MonoExceptionHandle exception = MONO_HANDLE_CAST (MonoException, mono_new_null ()); MonoDomain *domain = mono_domain_get (); char *type_name = NULL; char *message = NULL; error_init (error_out); const guint16 error_code = error->error_code; g_assert (error_code != MONO_ERROR_CLEANUP_CALLED_SENTINEL); switch (error_code) { case MONO_ERROR_NONE: goto exit; case MONO_ERROR_MISSING_METHOD: exception = mono_corlib_exception_new_with_args ("System", "MissingMethodException", error->full_message, error->first_argument, error_out); break; case MONO_ERROR_BAD_IMAGE: exception = mono_corlib_exception_new_with_args ("System", "BadImageFormatException", error->full_message, error->first_argument, error_out); break; case MONO_ERROR_FILE_NOT_FOUND: exception = mono_corlib_exception_new_with_args ("System.IO", "FileNotFoundException", error->full_message, error->first_argument, error_out); break; case MONO_ERROR_MISSING_FIELD: exception = mono_corlib_exception_new_with_args ("System", "MissingFieldException", error->full_message, error->first_argument, error_out); break; case MONO_ERROR_MEMBER_ACCESS: exception = mono_exception_new_by_name_msg (mono_defaults.corlib, "System", "MemberAccessException", error->full_message, error_out); break; case MONO_ERROR_TYPE_LOAD: { MonoStringHandle assembly_name; MonoStringHandle type_name; if ((error->type_name && error->assembly_name) || error->exn.klass) { type_name = get_type_name_as_mono_string (error, domain, error_out); if (!mono_error_ok (error_out)) break; if (error->assembly_name) { assembly_name = string_new_cleanup (domain, error->assembly_name); if (MONO_HANDLE_IS_NULL (assembly_name)) { mono_error_set_out_of_memory (error_out, "Could not allocate assembly name"); break; } } else { assembly_name = mono_string_empty_handle (domain); } exception = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System", "TypeLoadException", type_name, assembly_name, error_out); if (!MONO_HANDLE_IS_NULL (exception)) { const char *full_message = error->full_message; if (full_message && full_message [0]) { MonoStringHandle msg = string_new_cleanup (mono_domain_get (), full_message); if (!MONO_HANDLE_IS_NULL (msg)) MONO_HANDLE_SET (exception, message, msg); else mono_error_set_out_of_memory (error_out, "Could not allocate exception object"); } } } else { exception = mono_exception_new_by_name_msg (mono_defaults.corlib, "System", "TypeLoadException", error->full_message, error_out); } } break; case MONO_ERROR_OUT_OF_MEMORY: if (domain) exception = MONO_HANDLE_NEW (MonoException, domain->out_of_memory_ex); if (MONO_HANDLE_IS_NULL (exception)) exception = mono_get_exception_out_of_memory_handle (); break; case MONO_ERROR_ARGUMENT: exception = mono_exception_new_argument (error->first_argument, error->full_message, error_out); break; case MONO_ERROR_ARGUMENT_NULL: exception = mono_exception_new_argument_null (error->first_argument, error_out); break; case MONO_ERROR_ARGUMENT_OUT_OF_RANGE: exception = mono_exception_new_argument_out_of_range(error->first_argument, error->full_message, error_out); break; case MONO_ERROR_NOT_VERIFIABLE: if (error->exn.klass) { type_name = mono_type_get_full_name (error->exn.klass); if (!type_name) goto out_of_memory; } message = g_strdup_printf ("Error in %s:%s %s", type_name, error->member_name, error->full_message); if (!message) goto out_of_memory; exception = mono_exception_new_by_name_msg (mono_defaults.corlib, "System.Security", "VerificationException", message, error_out); break; case MONO_ERROR_GENERIC: if (!error->exception_name_space || !error->exception_name) mono_error_set_execution_engine (error_out, "MonoError with generic error but no exception name was supplied"); else exception = mono_exception_new_by_name_msg (mono_defaults.corlib, error->exception_name_space, error->exception_name, error->full_message, error_out); break; case MONO_ERROR_EXCEPTION_INSTANCE: exception = MONO_HANDLE_CAST (MonoException, mono_gchandle_get_target_handle (error->exn.instance_handle)); break; case MONO_ERROR_CLEANUP_CALLED_SENTINEL: mono_error_set_execution_engine (error_out, "MonoError reused after mono_error_cleanup"); break; case MONO_ERROR_INVALID_PROGRAM: exception = mono_exception_new_by_name_msg (mono_defaults.corlib, "System", "InvalidProgramException", (error->flags & MONO_ERROR_INCOMPLETE) ? "" : error->full_message, error_out); break; default: mono_error_set_execution_engine (error_out, "Invalid error-code %d", error->error_code); } if (!mono_error_ok (error_out)) goto return_null; if (MONO_HANDLE_IS_NULL (exception)) mono_error_set_out_of_memory (error_out, "Could not allocate exception object"); goto exit; out_of_memory: mono_error_set_out_of_memory (error_out, "Could not allocate message"); goto exit; return_null: exception = MONO_HANDLE_CAST (MonoException, mono_new_null ()); exit: g_free (message); g_free (type_name); HANDLE_FUNCTION_RETURN_OBJ (exception); }
/*Can fail with out-of-memory*/ MonoException* mono_error_prepare_exception (MonoError *oerror, MonoError *error_out) { MonoErrorInternal *error = (MonoErrorInternal*)oerror; MonoException* exception = NULL; MonoString *assembly_name = NULL, *type_name = NULL, *method_name = NULL, *field_name = NULL, *msg = NULL; MonoDomain *domain = mono_domain_get (); mono_error_init (error_out); switch (error->error_code) { case MONO_ERROR_NONE: return NULL; case MONO_ERROR_MISSING_METHOD: if ((error->type_name || error->klass) && error->member_name) { type_name = get_type_name_as_mono_string (error, domain, error_out); if (!mono_error_ok (error_out)) break; method_name = mono_string_new (domain, error->member_name); if (!method_name) { mono_error_set_out_of_memory (error_out, "Could not allocate method name"); break; } exception = mono_exception_from_name_two_strings (mono_defaults.corlib, "System", "MissingMethodException", type_name, method_name); if (exception) set_message_on_exception (exception, error, error_out); } else { exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingMethodException", mono_internal_error_get_message (error)); } break; case MONO_ERROR_MISSING_FIELD: if ((error->type_name || error->klass) && error->member_name) { type_name = get_type_name_as_mono_string (error, domain, error_out); if (!mono_error_ok (error_out)) break; field_name = mono_string_new (domain, error->member_name); if (!field_name) { mono_error_set_out_of_memory (error_out, "Could not allocate field name"); break; } exception = mono_exception_from_name_two_strings (mono_defaults.corlib, "System", "MissingFieldException", type_name, field_name); if (exception) set_message_on_exception (exception, error, error_out); } else { exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "MissingFieldException", mono_internal_error_get_message (error)); } break; case MONO_ERROR_TYPE_LOAD: if (error->type_name || error->assembly_name) { type_name = get_type_name_as_mono_string (error, domain, error_out); if (!mono_error_ok (error_out)) break; if (error->assembly_name) { assembly_name = mono_string_new (domain, error->assembly_name); if (!assembly_name) { mono_error_set_out_of_memory (error_out, "Could not allocate assembly name"); break; } } exception = mono_exception_from_name_two_strings (mono_get_corlib (), "System", "TypeLoadException", type_name, assembly_name); if (exception) set_message_on_exception (exception, error, error_out); } else { exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "TypeLoadException", mono_internal_error_get_message (error)); } break; case MONO_ERROR_FILE_NOT_FOUND: case MONO_ERROR_BAD_IMAGE: if (error->assembly_name) { msg = mono_string_new (domain, mono_internal_error_get_message (error)); if (!msg) { mono_error_set_out_of_memory (error_out, "Could not allocate message"); break; } if (error->assembly_name) { assembly_name = mono_string_new (domain, error->assembly_name); if (!assembly_name) { mono_error_set_out_of_memory (error_out, "Could not allocate assembly name"); break; } } if (error->error_code == MONO_ERROR_FILE_NOT_FOUND) exception = mono_exception_from_name_two_strings (mono_get_corlib (), "System.IO", "FileNotFoundException", msg, assembly_name); else exception = mono_exception_from_name_two_strings (mono_defaults.corlib, "System", "BadImageFormatException", msg, assembly_name); } else { if (error->error_code == MONO_ERROR_FILE_NOT_FOUND) exception = mono_exception_from_name_msg (mono_get_corlib (), "System.IO", "FileNotFoundException", mono_internal_error_get_message (error)); else exception = mono_exception_from_name_msg (mono_defaults.corlib, "System", "BadImageFormatException", mono_internal_error_get_message (error)); } break; case MONO_ERROR_OUT_OF_MEMORY: exception = mono_get_exception_out_of_memory (); break; case MONO_ERROR_ARGUMENT: exception = mono_get_exception_argument (error->type_name, mono_internal_error_get_message (error)); break; case MONO_ERROR_NOT_VERIFIABLE: { char *type_name = NULL, *message; if (error->klass) { type_name = mono_type_get_full_name (error->klass); if (!type_name) { mono_error_set_out_of_memory (error_out, "Could not allocate message"); break; } } message = g_strdup_printf ("Error in %s:%s %s", type_name, error->member_name, mono_internal_error_get_message (error)); if (!message) { g_free (type_name); mono_error_set_out_of_memory (error_out, "Could not allocate message"); break; } exception = mono_exception_from_name_msg (mono_defaults.corlib, "System.Security", "VerificationException", message); g_free (message); g_free (type_name); break; } case MONO_ERROR_GENERIC: if (!error->exception_name_space || !error->exception_name) mono_error_set_generic_error (error_out, "System", "ExecutionEngineException", "MonoError with generic error but no exception name was supplied"); else exception = mono_exception_from_name_msg (mono_defaults.corlib, error->exception_name_space, error->exception_name, mono_internal_error_get_message (error)); break; default: mono_error_set_generic_error (error_out, "System", "ExecutionEngineException", "Invalid error-code %d", error->error_code); } if (!mono_error_ok (error_out)) return NULL; if (!exception) mono_error_set_out_of_memory (error_out, "Could not allocate exception object"); return exception; }