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

Calling instance-based methods with function pointers is not supported #110061

Closed
steveharter opened this issue Nov 21, 2024 · 5 comments
Closed
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Comments

@steveharter
Copy link
Member

steveharter commented Nov 21, 2024

Calling a managed function pointer on an instance that returns a non-primitive value type (perhaps based on length?) will return random values and causes stack\heap issues later.

UPDATED: I assume calling a function pointer on an instance method is supposed to work, provided the signature is correct for having the first parameter being the correct type (here "object" was used, but "TestClass" has the same result since it is a reference type). However, the function pointer spec says that instance methods are not supported (at least initially; not sure what transpired since then).

Note the IL looks correct, and the method is actually called.

This does not appear to be a regression.

Repro: the long property works but Guid doesn't, and a subsequent call to the long property returns a corrupted value.

public class TestClass
{
    public Guid _guid = new Guid("12300000-0000-0000-0000-000000000456");
    public int _long = 42;

    public Guid MyGuid => _guid;
    public long MyLong => _long;
}

internal unsafe class Program
{
    static void Main(string[] args)
    {
        MethodInfo method;
        IntPtr fp;
        
        TestClass testClass = new();
        
        fp = typeof(TestClass).GetMethod("get_MyLong").MethodHandle.GetFunctionPointer();
        delegate* managed<object, long> fpLong = (delegate* managed<object, long>)fp;
        long vLong = fpLong(testClass);
        Console.WriteLine(vLong); //42
        testClass._long++;
        vLong = fpLong(testClass);
        Console.WriteLine(vLong); //43
        
        fp = typeof(TestClass).GetMethod("get_MyGuid").MethodHandle.GetFunctionPointer();
        delegate* managed<object, Guid> fGuid = (delegate* managed<object, Guid>)fp;
        Guid vGuid = fGuid(testClass);
        Console.WriteLine(vGuid);           // 00000000-0000-0000-0000-000000000000 (wrong)
        Console.WriteLine(testClass._guid); // 12300000-0000-0000-0000-000000000456
        
        vLong = fpLong(testClass);
        Console.WriteLine(vLong); // random number (not 43)
    }
}
@steveharter steveharter added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 21, 2024
Copy link
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 21, 2024
@steveharter
Copy link
Member Author

Closing; wrong repro

@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Nov 21, 2024
@steveharter steveharter changed the title Calling a managed function pointer with non-primitive return value fails Calling a managed function pointer on instance with non-primitive return value fails Nov 21, 2024
@steveharter
Copy link
Member Author

Reopened; updated repro

@steveharter steveharter reopened this Nov 21, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 21, 2024
@steveharter steveharter changed the title Calling a managed function pointer on instance with non-primitive return value fails Calling instance-based methods with function pointers is not supported Nov 21, 2024
@steveharter
Copy link
Member Author

Closing for now; the spec proposes "instance" keyword:

unsafe class Instance {
    void Use() {
        delegate* instance<Instance, string> f = &ToString;
        f(this);
    }
}

@steveharter steveharter closed this as not planned Won't fix, can't repro, duplicate, stale Nov 21, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Nov 21, 2024
@MichalPetryka
Copy link
Contributor

says that instance methods are not supported (at least initially; not sure what transpired since then).

AFAIR LDM decided they're too nieche too expose in C#, but they should be fully functional in IL.

I assume if they were to be exposed in C#, it'd only be explicitthis ones since I don't see a way to reasonably handle ones with implicit this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

No branches or pull requests

2 participants