From cc61a87130c06bd4498802421d97a3ac55328b67 Mon Sep 17 00:00:00 2001 From: Fan Yang Date: Thu, 19 Jan 2023 07:52:40 -0800 Subject: [PATCH 1/5] Fix function pointer check --- src/mono/mono/metadata/class.c | 10 ++-- src/mono/mono/metadata/marshal.c | 2 +- .../functionpointer/Functionpointer.cs | 46 +++++++++++++++++++ .../functionpointer/Functionpointer.csproj | 9 ++++ 4 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 src/tests/baseservices/typeequivalence/functionpointer/Functionpointer.cs create mode 100644 src/tests/baseservices/typeequivalence/functionpointer/Functionpointer.csproj diff --git a/src/mono/mono/metadata/class.c b/src/mono/mono/metadata/class.c index 9ac6c35553bbd7..dc72646333f7ff 100644 --- a/src/mono/mono/metadata/class.c +++ b/src/mono/mono/metadata/class.c @@ -4218,11 +4218,11 @@ mono_class_is_assignable_from_general (MonoClass *klass, MonoClass *oklass, gboo } if (m_class_get_byval_arg (klass)->type == MONO_TYPE_FNPTR) { - /* - * if both klass and oklass are fnptr, and they're equal, we would have returned at the - * beginning. - */ - /* Is this right? or do we need to look at signature compatibility? */ + if (mono_metadata_signature_equal (klass_byval_arg->data.method, oklass_byval_arg->data.method)) { + *result = TRUE; + return; + } + *result = FALSE; return; } diff --git a/src/mono/mono/metadata/marshal.c b/src/mono/mono/metadata/marshal.c index 6caac8e640b333..1f54f0bfed97ee 100644 --- a/src/mono/mono/metadata/marshal.c +++ b/src/mono/mono/metadata/marshal.c @@ -4627,7 +4627,7 @@ get_virtual_stelemref_kind (MonoClass *element_class) if (element_class == mono_defaults.object_class) return STELEMREF_OBJECT; if (is_monomorphic_array (element_class)) - return STELEMREF_SEALED_CLASS; + return STELEMREF_COMPLEX; /* magic ifaces requires aditional checks for when the element type is an array */ if (MONO_CLASS_IS_INTERFACE_INTERNAL (element_class) && m_class_is_array_special_interface (element_class)) diff --git a/src/tests/baseservices/typeequivalence/functionpointer/Functionpointer.cs b/src/tests/baseservices/typeequivalence/functionpointer/Functionpointer.cs new file mode 100644 index 00000000000000..95d7bf33b1303c --- /dev/null +++ b/src/tests/baseservices/typeequivalence/functionpointer/Functionpointer.cs @@ -0,0 +1,46 @@ +using System; +using System.Runtime.InteropServices; + +namespace TestFunctionPointer +{ + unsafe class TestThings + { + public static delegate* managed[][] Functions = { + new delegate* managed[] + { + &Function, + }, + }; + + public static int Function() => 100; + + public static delegate* unmanaged[][] Functions1 = { + new delegate* unmanaged[] + { + &Function1, + }, + }; + + [UnmanagedCallersOnly] + public static int Function1() => 100; + + public static delegate* managed[][] Functions2 = { + new delegate* managed[] + { + &Function2, + }, + }; + + public static int Function2(int a) { + return a; + } + } + + unsafe class Program + { + public static int Main() + { + return TestThings.Functions2[0][0](TestThings.Functions[0][0]()); + } + } +} \ No newline at end of file diff --git a/src/tests/baseservices/typeequivalence/functionpointer/Functionpointer.csproj b/src/tests/baseservices/typeequivalence/functionpointer/Functionpointer.csproj new file mode 100644 index 00000000000000..51f7075c49c815 --- /dev/null +++ b/src/tests/baseservices/typeequivalence/functionpointer/Functionpointer.csproj @@ -0,0 +1,9 @@ + + + Exe + true + + + + + \ No newline at end of file From fb0c821c9c4ce855756596a2e8dfe5257c514a56 Mon Sep 17 00:00:00 2001 From: Fan Yang Date: Thu, 19 Jan 2023 10:58:53 -0800 Subject: [PATCH 2/5] Make is_monomorphic_array return false when the element is function pointer --- src/mono/mono/metadata/marshal.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/mono/mono/metadata/marshal.c b/src/mono/mono/metadata/marshal.c index 1f54f0bfed97ee..f7262ee0dff8d4 100644 --- a/src/mono/mono/metadata/marshal.c +++ b/src/mono/mono/metadata/marshal.c @@ -4618,6 +4618,8 @@ is_monomorphic_array (MonoClass *klass) return FALSE; element_class = m_class_get_element_class (klass); + if (m_class_get_byval_arg (element_class)->type == MONO_TYPE_FNPTR) + return FALSE; return mono_class_is_sealed (element_class) || m_class_is_valuetype (element_class); } @@ -4627,7 +4629,7 @@ get_virtual_stelemref_kind (MonoClass *element_class) if (element_class == mono_defaults.object_class) return STELEMREF_OBJECT; if (is_monomorphic_array (element_class)) - return STELEMREF_COMPLEX; + return STELEMREF_SEALED_CLASS; /* magic ifaces requires aditional checks for when the element type is an array */ if (MONO_CLASS_IS_INTERFACE_INTERNAL (element_class) && m_class_is_array_special_interface (element_class)) From 38ccdb725c9e66485d883f91c65a215b1392cbfe Mon Sep 17 00:00:00 2001 From: Fan Yang Date: Thu, 19 Jan 2023 11:36:09 -0800 Subject: [PATCH 3/5] Move test to a better location --- .../classloader/Casting}/Functionpointer.cs | 0 .../classloader/Casting}/Functionpointer.csproj | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/tests/{baseservices/typeequivalence/functionpointer => Loader/classloader/Casting}/Functionpointer.cs (100%) rename src/tests/{baseservices/typeequivalence/functionpointer => Loader/classloader/Casting}/Functionpointer.csproj (100%) diff --git a/src/tests/baseservices/typeequivalence/functionpointer/Functionpointer.cs b/src/tests/Loader/classloader/Casting/Functionpointer.cs similarity index 100% rename from src/tests/baseservices/typeequivalence/functionpointer/Functionpointer.cs rename to src/tests/Loader/classloader/Casting/Functionpointer.cs diff --git a/src/tests/baseservices/typeequivalence/functionpointer/Functionpointer.csproj b/src/tests/Loader/classloader/Casting/Functionpointer.csproj similarity index 100% rename from src/tests/baseservices/typeequivalence/functionpointer/Functionpointer.csproj rename to src/tests/Loader/classloader/Casting/Functionpointer.csproj From 3bd768563e8e7b86eaa7031917edcdc7381e47f0 Mon Sep 17 00:00:00 2001 From: Fan Yang <52458914+fanyang-mono@users.noreply.github.com> Date: Mon, 30 Jan 2023 11:11:38 -0500 Subject: [PATCH 4/5] Disable function pointer test for crossgen due to an existing issue --- src/tests/issues.targets | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/tests/issues.targets b/src/tests/issues.targets index cf842bd2967bd8..67b551fb8a4d76 100644 --- a/src/tests/issues.targets +++ b/src/tests/issues.targets @@ -919,6 +919,9 @@ https://github.com/dotnet/runtime/issues/62881 + + https://github.com/dotnet/runtime/issues/81106 + From 565cd48b7c9b90168d900f11cd7d6168cec0e7e9 Mon Sep 17 00:00:00 2001 From: Tomas Date: Thu, 2 Feb 2023 19:40:21 +0100 Subject: [PATCH 5/5] Backport of PR #81122 to .NET 7.0, revert change to issues.targets Based on tactical discussion with Fan and Sam I have backported the PR #81122 including the removal of the issues.targets entry for function pointer tests as an extra commit on top of the backport of Fan's PR #80855. Thanks Tomas --- src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs | 3 +-- src/tests/issues.targets | 3 --- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs b/src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs index 65e58642acaf45..dcd464d5753d5a 100644 --- a/src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs +++ b/src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs @@ -303,9 +303,8 @@ private string ComputeMangledTypeName(TypeDesc type) mangledName = GetMangledTypeName(((PointerType)type).ParameterType) + NestMangledName("Pointer"); break; case TypeFlags.FunctionPointer: - // TODO: need to also encode calling convention (or all modopts?) var fnPtrType = (FunctionPointerType)type; - mangledName = "__FnPtr" + EnterNameScopeSequence; + mangledName = "__FnPtr_" + ((int)fnPtrType.Signature.Flags).ToString("X2") + EnterNameScopeSequence; mangledName += GetMangledTypeName(fnPtrType.Signature.ReturnType); mangledName += EnterNameScopeSequence; diff --git a/src/tests/issues.targets b/src/tests/issues.targets index 67b551fb8a4d76..cf842bd2967bd8 100644 --- a/src/tests/issues.targets +++ b/src/tests/issues.targets @@ -919,9 +919,6 @@ https://github.com/dotnet/runtime/issues/62881 - - https://github.com/dotnet/runtime/issues/81106 -