Skip to content

[Mono] GetFunctionPointer throws for an abstract method on the JIT; CoreCLR and the interpreter return a pointer #136484

Open

Description

@caraioniurie47

Description

On the Mono JIT, RuntimeMethodHandle.GetFunctionPointer() throws BadImageFormatException: Method has no body for an abstract method: an abstract method of a class, an interface method without a default implementation, or a static abstract interface method.
CoreCLR and the Mono interpreter return a pointer for these methods; a call through the static abstract method's pointer throws BadImageFormatException.
So code that takes such a pointer without calling it fails on the Mono JIT but not on CoreCLR or the Mono interpreter.
Other methods without IL, such as a P/Invoke or a delegate's Invoke, get a pointer on the JIT too.

Reproduction Steps

using System;
using System.Reflection;

abstract class A
{
    public abstract void M();
}

interface I
{
    void N();
    static abstract void S();
}

static unsafe class Program
{
    static void Main()
    {
        Pointer(typeof(A).GetMethod(nameof(A.M))!);
        Pointer(typeof(I).GetMethod(nameof(I.N))!);
        IntPtr s = Pointer(typeof(I).GetMethod(nameof(I.S))!);
        if (s != IntPtr.Zero)
        {
            try
            {
                ((delegate*<void>)s)();
                Console.WriteLine("Call I.S: returned");
            }
            catch (Exception e)
            {
                Console.WriteLine($"Call I.S: {e.GetType().Name}");
            }
        }
    }

    static IntPtr Pointer(MethodInfo method)
    {
        try
        {
            IntPtr pointer = method.MethodHandle.GetFunctionPointer();
            Console.WriteLine($"{method.DeclaringType!.Name}.{method.Name}: {(pointer == IntPtr.Zero ? "zero" : "non-zero")}");
            return pointer;
        }
        catch (Exception e)
        {
            Console.WriteLine($"{method.DeclaringType!.Name}.{method.Name}: {e.GetType().Name}: {e.Message}");
            return IntPtr.Zero;
        }
    }
}

Build with <AllowUnsafeBlocks>true</AllowUnsafeBlocks> and run it on the Mono runtime (JIT).

Expected behavior

A pointer for each method, as on CoreCLR and the Mono interpreter, where the call through I.S's pointer then throws:

A.M: non-zero
I.N: non-zero
I.S: non-zero
Call I.S: BadImageFormatException

Actual behavior

A.M: BadImageFormatException: Method has no body
I.N: BadImageFormatException: Method has no body
I.S: BadImageFormatException: Method has no body

Regression?

No. The released Mono runtime 8.0.28 (win-x64) prints the same on the JIT, and the expected output on the interpreter.

Known Workarounds

Run on the Mono interpreter (MONO_ENV_OPTIONS=--interpreter).

Configuration

dotnet/runtime main at ec0059c with #135566 applied, Mono built with build.cmd mono+libs -rc checked; Windows 11, x64. Also the released Mono 8.0.28 win-x64 runtime and CoreCLR 11.0.0-rc.1.

Other information

On the JIT, GetFunctionPointer reaches the get_ftnptr callback, which compiles the method before returning a pointer:

mini-runtime.c:2946-2949

	if (!mono_llvm_only) {
		gpointer res = mono_jit_compile_method (method, error);
		res = mini_add_method_trampoline (method, res, mono_method_needs_static_rgctx_invoke (method, TRUE), need_unbox);
		return res;

Loading the header of a method without a body sets the error:

loader.c:2021-2027

	if (mono_method_has_no_body (method)) {
		if (mono_method_get_is_reabstracted (method))
			mono_error_set_generic_error (error, "System", "EntryPointNotFoundException", "%s", method->name);
		else
			mono_error_set_bad_image (error, img, "Method has no body");
		return NULL;
	}

NativeAOT throws PlatformNotSupportedException from GetFunctionPointer for an abstract class method and a static abstract method.
MethodInfoTests.GetFunctionPointer_StaticAbstract_MatchesAbstractMethod in #135566 checks that static abstract methods behave as an abstract instance method; it skips Mono and will reference this issue for the JIT.

I can implement a fix, with a test, if it is wanted. Could this be assigned to me?

Note

AI-generated, written at my direction and reviewed by me before posting. Verified on dotnet/runtime ec0059c with #135566 (Mono Debug, JIT and interpreter), on CoreCLR 11.0.0-rc.1 and on the released Mono 8.0.28 win-x64 runtime (JIT and interpreter), Windows 11 x64; the repro was run as written on each.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Interop-monountriaged

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions