From 680fe21e6952ba6dc73ad89095b34ebb3a9bfc86 Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Wed, 1 Jun 2016 18:01:43 -0400 Subject: [PATCH] [runtime] Mark mono_runtime_run_main external only. Runtime should use mono_runtime_run_main_checked or mono_runtime_try_run_main --- mono/metadata/coree.c | 3 +- mono/metadata/object-internals.h | 8 +++ mono/metadata/object.c | 103 ++++++++++++++++++++++++++----- mono/metadata/object.h | 1 + mono/mini/driver.c | 12 +++- 5 files changed, 106 insertions(+), 21 deletions(-) diff --git a/mono/metadata/coree.c b/mono/metadata/coree.c index 326251cc140..f0e2f0d58c8 100644 --- a/mono/metadata/coree.c +++ b/mono/metadata/coree.c @@ -197,7 +197,8 @@ __int32 STDMETHODCALLTYPE _CorExeMain(void) argv [i] = g_utf16_to_utf8 (argvw [i], -1, NULL, NULL, NULL); LocalFree (argvw); - mono_runtime_run_main (method, argc, argv, NULL); + mono_runtime_run_main_checked (method, argc, argv, &error); + mono_error_raise_exception (&error); /* FIXME don't raise here */ mono_thread_manage (); mono_runtime_quit (); diff --git a/mono/metadata/object-internals.h b/mono/metadata/object-internals.h index 9accbe3a410..fc3cb822d16 100644 --- a/mono/metadata/object-internals.h +++ b/mono/metadata/object-internals.h @@ -1738,6 +1738,14 @@ mono_runtime_delegate_invoke_checked (MonoObject *delegate, void **params, MonoArray* mono_runtime_get_main_args_checked (MonoError *error); +int +mono_runtime_run_main_checked (MonoMethod *method, int argc, char* argv[], + MonoError *error); + +int +mono_runtime_try_run_main (MonoMethod *method, int argc, char* argv[], + MonoObject **exc, MonoError *error); + int mono_runtime_exec_main_checked (MonoMethod *method, MonoArray *args, MonoError *error); diff --git a/mono/metadata/object.c b/mono/metadata/object.c index 6cd27da3666..98172647db7 100644 --- a/mono/metadata/object.c +++ b/mono/metadata/object.c @@ -4168,22 +4168,14 @@ mono_runtime_set_main_args (int argc, char* argv[]) return 0; } -/** - * mono_runtime_run_main: - * @method: the method to start the application with (usually Main) - * @argc: number of arguments from the command line - * @argv: array of strings from the command line - * @exc: excetption results - * - * Execute a standard Main() method (argc/argv contains the - * executable name). This method also sets the command line argument value - * needed by System.Environment. - * +/* + * Prepare an array of arguments in order to execute a standard Main() + * method (argc/argv contains the executable name). This method also + * sets the command line argument value needed by System.Environment. * */ -int -mono_runtime_run_main (MonoMethod *method, int argc, char* argv[], - MonoObject **exc) +static MonoArray* +prepare_run_main (MonoMethod *method, int argc, char *argv[]) { MONO_REQ_GC_UNSAFE_MODE; @@ -4274,15 +4266,92 @@ mono_runtime_run_main (MonoMethod *method, int argc, char* argv[], mono_assembly_set_main (method->klass->image->assembly); + return args; +} + +/** + * mono_runtime_run_main: + * @method: the method to start the application with (usually Main) + * @argc: number of arguments from the command line + * @argv: array of strings from the command line + * @exc: excetption results + * + * Execute a standard Main() method (argc/argv contains the + * executable name). This method also sets the command line argument value + * needed by System.Environment. + * + * + */ +int +mono_runtime_run_main (MonoMethod *method, int argc, char* argv[], + MonoObject **exc) +{ + MONO_REQ_GC_UNSAFE_MODE; + + MonoError error; + MonoArray *args = prepare_run_main (method, argc, argv); int res; - if (exc) + if (exc) { res = mono_runtime_try_exec_main (method, args, exc, &error); - else + if (*exc == NULL && !is_ok (&error)) + *exc = (MonoObject*) mono_error_convert_to_exception (&error); + else + mono_error_cleanup (&error); + } else { res = mono_runtime_exec_main_checked (method, args, &error); - mono_error_raise_exception (&error); /* FIXME don't raise here */ + mono_error_raise_exception (&error); /* OK to throw, external only without a better alternative */ + } return res; } +/** + * mono_runtime_run_main_checked: + * @method: the method to start the application with (usually Main) + * @argc: number of arguments from the command line + * @argv: array of strings from the command line + * @error: set on error + * + * Execute a standard Main() method (argc/argv contains the + * executable name). This method also sets the command line argument value + * needed by System.Environment. On failure sets @error. + * + * + */ +int +mono_runtime_run_main_checked (MonoMethod *method, int argc, char* argv[], + MonoError *error) +{ + mono_error_init (error); + MonoArray *args = prepare_run_main (method, argc, argv); + return mono_runtime_exec_main_checked (method, args, error); +} + +/** + * mono_runtime_try_run_main: + * @method: the method to start the application with (usually Main) + * @argc: number of arguments from the command line + * @argv: array of strings from the command line + * @exc: set if Main throws an exception + * @error: set if Main can't be executed + * + * Execute a standard Main() method (argc/argv contains the executable + * name). This method also sets the command line argument value needed + * by System.Environment. On failure sets @error if Main can't be + * executed or @exc if it threw and exception. + * + * + */ +int +mono_runtime_try_run_main (MonoMethod *method, int argc, char* argv[], + MonoObject **exc, MonoError *error) +{ + g_assert (exc); + mono_error_init (error); + MonoArray *args = prepare_run_main (method, argc, argv); + return mono_runtime_try_exec_main (method, args, exc, error); +} + + static MonoObject* serialize_object (MonoObject *obj, gboolean *failure, MonoObject **exc) { diff --git a/mono/metadata/object.h b/mono/metadata/object.h index a1551e4eddf..24f7284e8e2 100644 --- a/mono/metadata/object.h +++ b/mono/metadata/object.h @@ -279,6 +279,7 @@ mono_runtime_exec_managed_code (MonoDomain *domain, MonoMainThreadFunc main_func, void* main_args); +MONO_RT_EXTERNAL_ONLY MONO_API int mono_runtime_run_main (MonoMethod *method, int argc, char* argv[], MonoObject **exc); diff --git a/mono/mini/driver.c b/mono/mini/driver.c index 1a7b15b6f55..9835ccc6f08 100644 --- a/mono/mini/driver.c +++ b/mono/mini/driver.c @@ -1026,10 +1026,14 @@ mono_jit_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[ } if (mono_llvm_only) { - MonoObject *exc; + MonoObject *exc = NULL; int res; - res = mono_runtime_run_main (method, argc, argv, &exc); + res = mono_runtime_try_run_main (method, argc, argv, &exc, &error); + if (exc == NULL && !is_ok (&error)) + exc = (MonoObject*) mono_error_convert_to_exception (&error); + else + mono_error_cleanup (&error); if (exc) { mono_unhandled_exception (exc); mono_invoke_unhandled_exception_hook (exc); @@ -1037,7 +1041,9 @@ mono_jit_exec (MonoDomain *domain, MonoAssembly *assembly, int argc, char *argv[ } return res; } else { - return mono_runtime_run_main (method, argc, argv, NULL); + int res = mono_runtime_run_main_checked (method, argc, argv, &error); + mono_error_raise_exception (&error); /* FIXME don't raise here */ + return res; } } -- 2.25.1