From 91c55ee8f8e34ca77cf89cbaecc3d56f83bd61aa Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Wed, 6 Apr 2022 11:44:06 -0400 Subject: [PATCH 1/2] [runtime] copy updated byref nullables after invoke Bring back some code lost in https://github.com/dotnet/runtime/pull/60270 When a `null` is passed for a `ref Nullable` argument to a runtime invoke, the invoke wrapper creates a default `Nullable` that should be copied back into the original arguments array of the invoke. There is code in the managed `Invoke` method to do some of the copying from the temporary array that is created for the Invoke (we pass a `Span` over that array from managed to native), but in `mono_runtime_try_invoke_span` we create a C array `pa` that has the arguments (this takes care of by-value semantics for value types, for example). We need to copy the byref nullables back from `pa` to the span, so that managed code can then copy it back from the span to the original input array of the Invoke. Fixes https://github.com/dotnet/runtime/issues/67269 --- src/mono/mono/metadata/object.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/mono/mono/metadata/object.c b/src/mono/mono/metadata/object.c index 8f77ce920e65b0..ac36e93c7c54aa 100644 --- a/src/mono/mono/metadata/object.c +++ b/src/mono/mono/metadata/object.c @@ -4841,6 +4841,20 @@ mono_runtime_try_invoke_span (MonoMethod *method, void *obj, MonoSpanOfObjects * g_assert (box_exc == NULL); mono_error_assert_ok (error); } + + if (has_byref_nullables) { + /* + * The runtime invoke wrapper already converted byref nullables back, + * and stored them in pa, we just need to copy them back to the + * managed array. + */ + for (i = 0; i < params_length; i++) { + MonoType *t = sig->params [i]; + + if (m_type_is_byref (t) && t->type == MONO_TYPE_GENERICINST && mono_class_is_nullable (mono_class_from_mono_type_internal (t))) + mono_span_setref (params_span, i, pa [i]); + } + } } goto exit; exit_null: From 82e8f9ba07252fb0abeb7b546520f1ca3fc57daa Mon Sep 17 00:00:00 2001 From: Aleksey Kliger Date: Fri, 8 Apr 2022 19:50:26 -0400 Subject: [PATCH 2/2] remove ActiveIssue --- src/libraries/System.Reflection/tests/MethodInfoTests.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/libraries/System.Reflection/tests/MethodInfoTests.cs b/src/libraries/System.Reflection/tests/MethodInfoTests.cs index 890befefd88e48..bdc8cfd07a0e86 100644 --- a/src/libraries/System.Reflection/tests/MethodInfoTests.cs +++ b/src/libraries/System.Reflection/tests/MethodInfoTests.cs @@ -620,7 +620,6 @@ public void ToStringTest_ByMethodInfo(MethodInfo methodInfo, string expected) Assert.Equal(expected, methodInfo.ToString()); } - [ActiveIssue("https://github.com/dotnet/runtime/issues/67269", TestRuntimes.Mono)] [Fact] public void InvokeNullableRefs() { @@ -656,7 +655,6 @@ static MethodInfo GetMethod(string name) => typeof(NullableRefMethods).GetMethod name, BindingFlags.Public | BindingFlags.Static)!; } - [ActiveIssue("https://github.com/dotnet/runtime/issues/67269", TestRuntimes.Mono)] [Fact] public void InvokeBoxedNullableRefs() {