From: Jan Kotas Date: Wed, 17 Feb 2021 07:38:54 +0000 (-0800) Subject: Update comment and add test for Marshal.GetDelegateForFunctionPointer (#48362) X-Git-Tag: submit/tizen/20210909.063632~3201 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=44d5fd11d470e22977e7bb17b21e3cda876cc9ce;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Update comment and add test for Marshal.GetDelegateForFunctionPointer (#48362) * Update comment and fix test for Marshal.GetDelegateForFunctionPointer argument validation * Disable test on Mono --- diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs index 9f8cc4c..3c247a1 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs @@ -867,11 +867,10 @@ namespace System.Runtime.InteropServices throw new ArgumentException(SR.Argument_NeedNonGenericType, nameof(t)); } - // COMPAT: This block of code isn't entirely correct. - // Users passing in typeof(MulticastDelegate) as 't' skip this check - // since Delegate is a base type of MulticastDelegate. - Type? c = t.BaseType; - if (c != typeof(Delegate) && c != typeof(MulticastDelegate)) + // For backward compatibility, we allow lookup up of existing delegate to + // function pointer mappings using abstract MulticastDelegate type. We will check + // for the non-abstract delegate type later if no existing mapping is found. + if (t.BaseType != typeof(MulticastDelegate) && t != typeof(MulticastDelegate)) { throw new ArgumentException(SR.Arg_MustBeDelegate, nameof(t)); } diff --git a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/GetDelegateForFunctionPointerTests.cs b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/GetDelegateForFunctionPointerTests.cs index 6f12389..c11c755 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/GetDelegateForFunctionPointerTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/System/Runtime/InteropServices/Marshal/GetDelegateForFunctionPointerTests.cs @@ -75,6 +75,16 @@ namespace System.Runtime.InteropServices.Tests VerifyDelegate(functionDelegate, targetMethod); } + [Fact] + [ActiveIssue("https://github.com/dotnet/runtime/issues/48379", TestRuntimes.Mono)] + public void GetDelegateForFunctionPointer_MulticastDelegate_ThrowsMustBeDelegate() + { + IntPtr ptr = Marshal.AllocHGlobal(16); + AssertExtensions.Throws("t", () => Marshal.GetDelegateForFunctionPointer(ptr, typeof(MulticastDelegate))); + AssertExtensions.Throws("t", () => Marshal.GetDelegateForFunctionPointer(ptr)); + Marshal.FreeHGlobal(ptr); + } + private static void VerifyDelegate(Delegate d, MethodInfo expectedMethod) { Assert.IsType(d);