Example #1
0
/**
 * mono_get_exception_file_not_found:
 * @fname: the name of the file not found.
 *
 * Returns: a new instance of the `System.IO.FileNotFoundException`
 */
MonoException *
mono_get_exception_file_not_found (MonoString *fname)
{
	MonoError error;
	MonoException *ret = mono_exception_from_name_two_strings_checked (
		mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname, &error);
	mono_error_assert_ok (&error);
	return ret;
}
Example #2
0
/**
 * mono_get_exception_file_not_found:
 * \param fname the name of the file not found.
 * \returns a new instance of the \c System.IO.FileNotFoundException
 */
MonoException *
mono_get_exception_file_not_found (MonoString *fname_raw)
{
	HANDLE_FUNCTION_ENTER ();
	ERROR_DECL (error);
	MONO_HANDLE_DCL (MonoString, fname);
	MonoExceptionHandle ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System.IO", "FileNotFoundException", fname, fname, error);
	mono_error_assert_ok (error);
	HANDLE_FUNCTION_RETURN_OBJ (ret);
}
Example #3
0
/**
 * mono_get_exception_bad_image_format2:
 * @msg: an informative message for the user.
 * @fname: The full name of the file with the invalid image.
 *
 * Returns: a new instance of the `System.BadImageFormatException`
 */
MonoException *
mono_get_exception_bad_image_format2 (const char *msg, MonoString *fname)
{
	MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;

	MonoError error;
	MonoException *ret = mono_exception_from_name_two_strings_checked (
		mono_get_corlib (), "System", "BadImageFormatException", s, fname, &error);
	mono_error_assert_ok (&error);
	return ret;
}
Example #4
0
/**
 * mono_get_exception_file_not_found2:
 * @msg: an informative message for the user.
 * @fname: the name of the file not found.
 *
 * Returns: a new instance of the `System.IO.FileNotFoundException`
 */
MonoException *
mono_get_exception_file_not_found2 (const char *msg, MonoString *fname)
{
	MonoString *s = msg ? mono_string_new (mono_domain_get (), msg) : NULL;

	MonoError error;
	MonoException *ret = mono_exception_from_name_two_strings_checked (
		mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname, &error);
	mono_error_assert_ok (&error);
	return ret;
}
Example #5
0
/**
 * mono_get_exception_type_load:
 * @class_name: the name of the class that could not be loaded
 * @assembly_name: the assembly where the class was looked up.
 *
 * Returns: a new instance of the `System.TypeLoadException`
 */
MonoException *
mono_get_exception_type_load (MonoString *class_name, char *assembly_name)
{
	MonoString *s = assembly_name ? mono_string_new (mono_domain_get (), assembly_name) : mono_string_new (mono_domain_get (), "");

	MonoError error;
	MonoException *ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
								   "TypeLoadException", class_name, s, &error);
	mono_error_assert_ok (&error);
	return ret;
}
Example #6
0
/**
 * mono_exception_from_name_two_strings:
 * @image: the Mono image where to look for the class
 * @name_space: the namespace for the class
 * @name: class name
 * @a1: first string argument to pass
 * @a2: second string argument to pass
 *
 * Creates an exception from a constructor that takes two string
 * arguments.
 *
 * Returns: the initialized exception instance.
 */
MonoException *
mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
				      const char *name, MonoString *a1, MonoString *a2)
{
	MonoError error;
	MonoException *ret;

	ret = mono_exception_from_name_two_strings_checked (image, name_space, name, a1, a2, &error);
	mono_error_cleanup (&error);
	return ret;
}
Example #7
0
/**
 * mono_get_exception_missing_field:
 * @class_name: the class where the lookup was performed
 * @member_name: the name of the missing method.
 *
 * Returns: a new instance of the `System.MissingFieldException`
 */
MonoException *
mono_get_exception_missing_field (const char *class_name, const char *member_name)
{
	MonoString *s1 = mono_string_new (mono_domain_get (), class_name);
	MonoString *s2 = mono_string_new (mono_domain_get (), member_name);

	MonoError error;
	MonoException *ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
								   "MissingFieldException", s1, s2, &error);
	mono_error_assert_ok (&error);
	return ret;
}
Example #8
0
/**
 * mono_exception_from_name_two_strings:
 * \param image the Mono image where to look for the class
 * \param name_space the namespace for the class
 * \param name class name
 * \param a1 first string argument to pass
 * \param a2 second string argument to pass
 *
 * Creates an exception from a constructor that takes two string
 * arguments.
 *
 * \returns the initialized exception instance.
 */
MonoException *
mono_exception_from_name_two_strings (MonoImage *image, const char *name_space,
				      const char *name, MonoString *a1_raw, MonoString *a2_raw)
{
	HANDLE_FUNCTION_ENTER ();
	ERROR_DECL (error);
	MONO_HANDLE_DCL (MonoString, a1);
	MONO_HANDLE_DCL (MonoString, a2);
	MonoExceptionHandle ret = mono_exception_from_name_two_strings_checked (image, name_space, name, a1, a2, error);
	mono_error_cleanup (error);
	HANDLE_FUNCTION_RETURN_OBJ (ret);
}
Example #9
0
static MonoExceptionHandle
mono_exception_new_argument_internal (const char *type, const char *arg, const char *msg, MonoError *error)
{
#ifdef ENABLE_NETCORE
	MonoStringHandle arg_str = arg ? mono_string_new_handle (mono_domain_get (), arg, error) : NULL_HANDLE_STRING;
	MonoStringHandle msg_str = msg ? mono_string_new_handle (mono_domain_get (), msg, error) : NULL_HANDLE_STRING;

	if (!strcmp (type, "ArgumentException"))
		return mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System", type, msg_str, arg_str, error);
	else
		return mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System", type, arg_str, msg_str, error);
#else
	MonoExceptionHandle ex = mono_exception_new_by_name_msg (mono_get_corlib (), "System", type, msg, error);

	if (arg && !MONO_HANDLE_IS_NULL (ex)) {
		MonoArgumentExceptionHandle argex = MONO_HANDLE_CAST (MonoArgumentException, ex);
		MonoStringHandle arg_str = mono_string_new_handle (MONO_HANDLE_DOMAIN (ex), arg, error);
		MONO_HANDLE_SET (argex, param_name, arg_str);
	}
	return ex;
#endif
}
Example #10
0
/**
 * mono_get_exception_file_not_found2:
 * \param msg an informative message for the user.
 * \param fname the name of the file not found.
 * \returns a new instance of the \c System.IO.FileNotFoundException
 */
MonoException *
mono_get_exception_file_not_found2 (const char *msg, MonoString *fname_raw)
{
	HANDLE_FUNCTION_ENTER ();
	ERROR_DECL (error);
	MONO_HANDLE_DCL (MonoString, fname);
	MonoStringHandle s = NULL_HANDLE_STRING;
	if (msg) {
		s = mono_string_new_handle (mono_domain_get (), msg, error);
		mono_error_assert_ok (error);
	}
	MonoExceptionHandle ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System.IO", "FileNotFoundException", s, fname, error);
	mono_error_assert_ok (error);
	HANDLE_FUNCTION_RETURN_OBJ (ret);
}
Example #11
0
/**
 * mono_get_exception_missing_member:
 * \param exception_type the specific exception type for the specific member type, i.e. field or method
 * \param class_name the class where the lookup was performed.
 * \param member_name the name of the missing method.
 * \returns a new instance of the \c exception_type (MissingFieldException or MissingMethodException)
 */
static MonoException*
mono_get_exception_missing_member (const char *exception_type, const char *class_name, const char *member_name)
{
	HANDLE_FUNCTION_ENTER ();
	ERROR_DECL (error);
	MonoDomain * const domain = mono_domain_get ();
	MonoStringHandle s1 = mono_string_new_handle (domain, class_name, error);
	mono_error_assert_ok (error);
	MonoStringHandle s2 = mono_string_new_handle (domain, member_name, error);
	mono_error_assert_ok (error);

	MonoExceptionHandle ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
									   exception_type, s1, s2, error);
	mono_error_assert_ok (error);
	HANDLE_FUNCTION_RETURN_OBJ (ret);
}
Example #12
0
/**
 * mono_get_exception_type_load:
 * \param class_name the name of the class that could not be loaded
 * \param assembly_name the assembly where the class was looked up.
 * \returns a new instance of the \c System.TypeLoadException
 */
MonoException *
mono_get_exception_type_load (MonoString *class_name_raw, char *assembly_name)
{
	ERROR_DECL (error);
	HANDLE_FUNCTION_ENTER ();
	MONO_HANDLE_DCL (MonoString, class_name);
	MonoStringHandle s = NULL_HANDLE_STRING;
	MonoDomain * const domain = mono_domain_get ();
	if (assembly_name) {
		s = mono_string_new_handle (domain, assembly_name, error);
		mono_error_assert_ok (error);
	} else
		s = mono_string_empty_handle (domain);

	MonoExceptionHandle ret = mono_exception_from_name_two_strings_checked (mono_get_corlib (), "System",
								   "TypeLoadException", class_name, s, error);
	mono_error_assert_ok (error);
	HANDLE_FUNCTION_RETURN_OBJ (ret);
}
Example #13
0
MonoExceptionHandle
mono_corlib_exception_new_with_args (const char *name_space, const char *name, const char *arg_0, const char *arg_1, MonoError *error)
{
	HANDLE_FUNCTION_ENTER ();

	MonoStringHandle str_0 = NULL_HANDLE_STRING;
	MonoStringHandle str_1 = NULL_HANDLE_STRING;
	MonoExceptionHandle ex = MONO_HANDLE_CAST (MonoException, NULL_HANDLE);
	MonoDomain * const domain = mono_domain_get ();

	str_0 = arg_0 ? mono_string_new_handle (domain, arg_0, error) : NULL_HANDLE_STRING;
	goto_if_nok (error, return_null);

	str_1 = arg_1 ? mono_string_new_handle (domain, arg_1, error) : NULL_HANDLE_STRING;
	goto_if_nok (error, return_null);

	ex = mono_exception_from_name_two_strings_checked (mono_defaults.corlib, name_space, name, str_0, str_1, error);
	goto exit;
return_null:
	ex = MONO_HANDLE_CAST (MonoException, mono_new_null ());
exit:
	HANDLE_FUNCTION_RETURN_REF (MonoException, ex);
}
Example #14
0
/*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;
}
Example #15
0
/*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);
}