Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/7.0] [Mono] Fix function pointer check #80927

Merged
merged 5 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/coreclr/tools/Common/Compiler/NativeAotNameMangler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 5 additions & 5 deletions src/mono/mono/metadata/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions src/mono/mono/metadata/marshal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
46 changes: 46 additions & 0 deletions src/tests/Loader/classloader/Casting/Functionpointer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Runtime.InteropServices;

namespace TestFunctionPointer
{
unsafe class TestThings
{
public static delegate* managed<int>[][] Functions = {
new delegate* managed<int>[]
{
&Function,
},
};

public static int Function() => 100;

public static delegate* unmanaged<int>[][] Functions1 = {
new delegate* unmanaged<int>[]
{
&Function1,
},
};

[UnmanagedCallersOnly]
public static int Function1() => 100;

public static delegate* managed<int, int>[][] Functions2 = {
new delegate* managed<int, int>[]
{
&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]());
}
}
}
9 changes: 9 additions & 0 deletions src/tests/Loader/classloader/Casting/Functionpointer.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Include="Functionpointer.cs" />
</ItemGroup>
</Project>