Skip to content

Commit 8502110

Browse files
github-actions[bot]fanyang-monotrylek
authored
[release/7.0] [Mono] Fix function pointer check (#80927)
* Fix function pointer check * Make is_monomorphic_array return false when the element is function pointer * Move test to a better location * Disable function pointer test for crossgen due to an existing issue * 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 --------- Co-authored-by: Fan Yang <[email protected]> Co-authored-by: Fan Yang <[email protected]> Co-authored-by: Tomas <[email protected]>
1 parent d31ff20 commit 8502110

File tree

5 files changed

+63
-7
lines changed

5 files changed

+63
-7
lines changed

src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,8 @@ private string ComputeMangledTypeName(TypeDesc type)
303303
mangledName = GetMangledTypeName(((PointerType)type).ParameterType) + NestMangledName("Pointer");
304304
break;
305305
case TypeFlags.FunctionPointer:
306-
// TODO: need to also encode calling convention (or all modopts?)
307306
var fnPtrType = (FunctionPointerType)type;
308-
mangledName = "__FnPtr" + EnterNameScopeSequence;
307+
mangledName = "__FnPtr_" + ((int)fnPtrType.Signature.Flags).ToString("X2") + EnterNameScopeSequence;
309308
mangledName += GetMangledTypeName(fnPtrType.Signature.ReturnType);
310309

311310
mangledName += EnterNameScopeSequence;

src/mono/mono/metadata/class.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4218,11 +4218,11 @@ mono_class_is_assignable_from_general (MonoClass *klass, MonoClass *oklass, gboo
42184218
}
42194219

42204220
if (m_class_get_byval_arg (klass)->type == MONO_TYPE_FNPTR) {
4221-
/*
4222-
* if both klass and oklass are fnptr, and they're equal, we would have returned at the
4223-
* beginning.
4224-
*/
4225-
/* Is this right? or do we need to look at signature compatibility? */
4221+
if (mono_metadata_signature_equal (klass_byval_arg->data.method, oklass_byval_arg->data.method)) {
4222+
*result = TRUE;
4223+
return;
4224+
}
4225+
42264226
*result = FALSE;
42274227
return;
42284228
}

src/mono/mono/metadata/marshal.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4618,6 +4618,8 @@ is_monomorphic_array (MonoClass *klass)
46184618
return FALSE;
46194619

46204620
element_class = m_class_get_element_class (klass);
4621+
if (m_class_get_byval_arg (element_class)->type == MONO_TYPE_FNPTR)
4622+
return FALSE;
46214623
return mono_class_is_sealed (element_class) || m_class_is_valuetype (element_class);
46224624
}
46234625

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Runtime.InteropServices;
3+
4+
namespace TestFunctionPointer
5+
{
6+
unsafe class TestThings
7+
{
8+
public static delegate* managed<int>[][] Functions = {
9+
new delegate* managed<int>[]
10+
{
11+
&Function,
12+
},
13+
};
14+
15+
public static int Function() => 100;
16+
17+
public static delegate* unmanaged<int>[][] Functions1 = {
18+
new delegate* unmanaged<int>[]
19+
{
20+
&Function1,
21+
},
22+
};
23+
24+
[UnmanagedCallersOnly]
25+
public static int Function1() => 100;
26+
27+
public static delegate* managed<int, int>[][] Functions2 = {
28+
new delegate* managed<int, int>[]
29+
{
30+
&Function2,
31+
},
32+
};
33+
34+
public static int Function2(int a) {
35+
return a;
36+
}
37+
}
38+
39+
unsafe class Program
40+
{
41+
public static int Main()
42+
{
43+
return TestThings.Functions2[0][0](TestThings.Functions[0][0]());
44+
}
45+
}
46+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
5+
</PropertyGroup>
6+
<ItemGroup>
7+
<Compile Include="Functionpointer.cs" />
8+
</ItemGroup>
9+
</Project>

0 commit comments

Comments
 (0)