From fd69dd9e550ba8cb357ecae86530e4a61d6d80af Mon Sep 17 00:00:00 2001 From: Zoltan Varga Date: Sat, 8 May 2021 08:21:59 -0400 Subject: [PATCH] [mono] Fix runtime invokes to methods returning byref values in llvmonly mode. Fixes some System.Runtime test failures on wasm. --- src/mono/mono/mini/mini-runtime.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/mono/mono/mini/mini-runtime.c b/src/mono/mono/mini/mini-runtime.c index 6c2e6c4e7e17b..9ab4ebb002810 100644 --- a/src/mono/mono/mini/mini-runtime.c +++ b/src/mono/mono/mini/mini-runtime.c @@ -3059,6 +3059,8 @@ create_runtime_invoke_info (MonoMethod *method, gpointer compiled_method, gboole return ret; } +static GENERATE_GET_CLASS_WITH_CACHE (nullbyrefreturn_ex, "Mono", "NullByRefReturnException"); + static MonoObject* mono_llvmonly_runtime_invoke (MonoMethod *method, RuntimeInvokeInfo *info, void *obj, void **params, MonoObject **exc, MonoError *error) { @@ -3127,11 +3129,29 @@ mono_llvmonly_runtime_invoke (MonoMethod *method, RuntimeInvokeInfo *info, void if (exc && *exc) return NULL; + if (sig->ret->byref) { + if (*(gpointer*)retval == NULL) { + MonoClass *klass = mono_class_get_nullbyrefreturn_ex_class (); + MonoObject *ex = mono_object_new_checked (klass, error); + mono_error_assert_ok (error); + mono_error_set_exception_instance (error, (MonoException*)ex); + return NULL; + } + } + if (sig->ret->type != MONO_TYPE_VOID) { - if (info->ret_box_class) - return mono_value_box_checked (info->ret_box_class, retval, error); - else - return *(MonoObject**)retval; + if (info->ret_box_class) { + if (sig->ret->byref) { + return mono_value_box_checked (info->ret_box_class, *(gpointer*)retval, error); + } else { + return mono_value_box_checked (info->ret_box_class, retval, error); + } + } else { + if (sig->ret->byref) + return **(MonoObject***)retval; + else + return *(MonoObject**)retval; + } } else { return NULL; }