Skip to content

RuntimeMethodHandle.GetFunctionPointer() should check if generic method is constructed #101664

Closed

Description

@andriivitiv

Description

Calling GetFunctionPointer() on a non-constructed generic method throws InvalidProgramException: Common Language Runtime detected an invalid program. A more helpful error message could be added.

Reproduction Steps

unsafe
{
    var func = (delegate*<void>) typeof(Foo)
        .GetMethod(nameof(Foo.Func))!
        .MethodHandle
        .GetFunctionPointer();
    
    func();
}

static class Foo
{
    public static void Func<T>()
    {
    }
}

Expected behavior

User friendly exception is thrown.

Actual behavior

InvalidProgramException is thrown.

Unhandled exception. System.InvalidProgramException: Common Language Runtime detected an invalid program.
   at System.RuntimeMethodHandle.GetFunctionPointer() in /runtime/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs:line 829
   at Program.<Main>$(String[] args)

Regression?

No

Known Workarounds

Not applicable

Configuration

.NET 8 and 9
linux-x64

Other information

If the type itself is a generic type it throws System.InvalidOperationException: Could not execute the method because either the method itself or the containing type is not fully instantiated.. I think that's the correct exception to be thrown.

unsafe
{
    var func = (delegate*<void>) typeof(Foo<>)
        .GetMethod("Func")!
        .MethodHandle
        .GetFunctionPointer();
    
    func();
}

static class Foo<T>
{
    public static void Func()
    {
    }
}

Activity

  1. ghost added
    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
    on Apr 28, 2024
  2. added and removed
    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
    on Apr 28, 2024
  3. dotnet-policy-service commented on Apr 28, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-reflection
    See info in area-owners.md if you want to be subscribed.

  4. added and removed
    untriagedNew issue has not been triaged by the area owner
    on May 1, 2024
  5. self-assigned this
    on May 1, 2024
  6. added this to the 9.0.0 milestone on May 1, 2024
  7. locked and limited conversation to collaborators on Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions