Skip to content

Calli with thiscall convention fails at runtime #33129

Closed

Description

@333fred

This program (IL at the bottom of the issue) causes .net core 3.1 to fail with:

Unhandled exception. System.InvalidProgramException: Common Language Runtime detected an invalid program.
   at UnmanagedFunctionPointer.Main()

The program defines a delegate type that takes a pointer to a structure and a float, and returns a structure that contains a field from that first pointer and the float. It then creates a thiscall function pointer stub around the delegate by using Marshal.CreateFunctionPointerForDelegate, and calli's that pointer. On .net framework, this correctly prints 12.3. On .net core, this fails with the above exception. /cc @jkoritzinsky

.assembly UnmanagedFunctionPointer
{
    .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = (
        01 00 08 00 00 00 00 00
    )
    .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = (
        01 00 01 00 54 02 16 57 72 61 70 4e 6f 6e 45 78
        63 65 70 74 69 6f 6e 54 68 72 6f 77 73 01
    )
    .custom instance void [mscorlib]System.Diagnostics.DebuggableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggableAttribute/DebuggingModes) = (
        01 00 07 01 00 00 00 00
    )
    .permissionset reqmin = {
        [mscorlib]System.Security.Permissions.SecurityPermissionAttribute = {
            property bool SkipVerification = bool(true)
        }
    }
    .hash algorithm 0x00008004 // SHA1
    .ver 0:0:0:0
}

.module UnmanagedFunctionPointer.dll

.class public sequential ansi sealed beforefieldinit ReturnWrapper
    extends [mscorlib]System.ValueType
{
    // Fields
    .field public int32 i1
    .field public float32 f2

    // Methods
    .method public hidebysig specialname rtspecialname 
        instance void .ctor (
            int32 i1,
            float32 f2
        ) cil managed 
    {
        // Method begins at RVA 0x205a
        // Code size 16 (0x10)
        .maxstack 8

        nop
        ldarg.0
        ldarg.1
        stfld int32 ReturnWrapper::i1
        ldarg.0
        ldarg.2
        stfld float32 ReturnWrapper::f2
        ret
    } // end of method ReturnWrapper::.ctor

} // end of class ReturnWrapper

.class public sequential ansi sealed beforefieldinit S
    extends [mscorlib]System.ValueType
{
    // Fields
    .field public int32 i

    // Methods
    .method public hidebysig static 
        valuetype ReturnWrapper GetReturn (
            valuetype S* s,
            float32 f
        ) cil managed 
    {
        // Method begins at RVA 0x208c
        // Code size 18 (0x12)
        .maxstack 2
        .locals init (
            [0] valuetype ReturnWrapper
        )

        nop
        ldarg.0
        ldfld int32 S::i
        ldarg.1
        newobj instance void ReturnWrapper::.ctor(int32, float32)
        ret
    } // end of method S::GetReturn

} // end of class S

.class public auto ansi beforefieldinit UnmanagedFunctionPointer
    extends [mscorlib]System.Object
{
    // Nested Types
    .class nested private auto ansi sealed MultipleParams
        extends [mscorlib]System.MulticastDelegate
    {
        .custom instance void [mscorlib]System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute::.ctor(valuetype [mscorlib]System.Runtime.InteropServices.CallingConvention) = (
            01 00 04 00 00 00 00 00
        )
        // Methods
        .method public hidebysig specialname rtspecialname 
            instance void .ctor (
                object 'object',
                native int 'method'
            ) runtime managed 
        {
        } // end of method MultipleParams::.ctor

        .method public hidebysig newslot virtual 
            instance valuetype ReturnWrapper Invoke (
                valuetype S* s,
                float32 f
            ) runtime managed 
        {
        } // end of method MultipleParams::Invoke

        .method public hidebysig newslot virtual 
            instance class [mscorlib]System.IAsyncResult BeginInvoke (
                valuetype S* s,
                float32 f,
                class [mscorlib]System.AsyncCallback callback,
                object 'object'
            ) runtime managed 
        {
        } // end of method MultipleParams::BeginInvoke

        .method public hidebysig newslot virtual 
            instance valuetype ReturnWrapper EndInvoke (
                class [mscorlib]System.IAsyncResult result
            ) runtime managed 
        {
        } // end of method MultipleParams::EndInvoke

    } // end of class MultipleParams

    // Methods
    .method public hidebysig static 
        void Main () cil managed 
    {
        // Method begins at RVA 0x20e0
        // Code size 37 (0x25)
        .maxstack 4
        .entrypoint
        .locals init (
            [0] native int,
            [1] valuetype S,
            [2] valuetype ReturnWrapper
        )

        nop
        ldnull
        ldftn valuetype ReturnWrapper S::GetReturn(valuetype S*, float32)
        newobj instance void UnmanagedFunctionPointer/MultipleParams::.ctor(object, native int)
        call native int [mscorlib]System.Runtime.InteropServices.Marshal::GetFunctionPointerForDelegate<class UnmanagedFunctionPointer/MultipleParams>(!!0)
        stloc.0
        ldloc.0
        box [mscorlib]System.IntPtr
        call void [mscorlib]System.GC::KeepAlive(object)
        ldloca.s 1
        initobj [UnmanagedFunctionPointer]S
        ldloca.s 1
        ldc.i4.1
        stfld int32 [UnmanagedFunctionPointer]S::i
        ldloca.s 1
        conv.u
        ldc.r4 2.3
        ldloc.0
        calli unmanaged thiscall valuetype ReturnWrapper(valuetype S*, float32)
        stloc.2
        ldloc.2
        ldfld int32 [UnmanagedFunctionPointer]ReturnWrapper::i1
        call void [mscorlib]System.Console::Write(int32)
        ldloc.2
        ldfld float32 [UnmanagedFunctionPointer]ReturnWrapper::f2
        call void [mscorlib]System.Console::Write(float32)
        ret
    } // end of method UnmanagedFunctionPointer::GetFuncPtrMultipleParams
} // end of class UnmanagedFunctionPointer

Activity

  1. added
    untriaged
    on Mar 4, 2020
  2. jkoritzinsky commented on Mar 4, 2020

    @jkoritzinsky
    Member

    This is likely related to #12375

  3. jkoritzinsky commented on Mar 4, 2020

    @jkoritzinsky
    Member

    After more investigating, this is actually an interop bug with the IL stubs. I'll fix it.

  4. self-assigned this
    on Mar 4, 2020
  5. removed
    untriaged
    on Mar 4, 2020
  6. jkoritzinsky commented on Mar 5, 2020

    @jkoritzinsky
    Member

    After I've fixed the interop IL stub bug (PR incoming), there's an additional bug here.

    On .NET Core, the program outputs 00 instead of 12.3.

  7. jkoritzinsky commented on Mar 5, 2020

    @jkoritzinsky
    Member

    The second bug would fall on the JIT's support of thiscall. cc: @davidwrighton

  8. davidwrighton commented on Mar 9, 2020

    @davidwrighton
    Member

    @dotnet/jit-contrib Could someone look at the incorrect behavior that @jkoritzinsky is talking about here?

  9. added this to the 5.0 milestone on Mar 27, 2020
  10. added
    area-CodeGen-coreclr
    and removed on May 6, 2020
  11. AaronRobinsonMSFT commented on May 6, 2020

    @AaronRobinsonMSFT
    Member

    The second bug would fall on the JIT's support of thiscall.

    Updating tag based on this statement.

    /cc @dotnet/jit-contrib

  12. AndyAyersMS commented on Jun 5, 2020

    @AndyAyersMS
    Member

    I'll take a look.

  13. 8 remaining items

  14. AaronRobinsonMSFT commented on Jul 6, 2020

    @AaronRobinsonMSFT
    Member

    @jkoritzinsky I think fixing #12375 would be a good thing. We would need to get that going soon though. Do you know what the JIT would need to do? Is there prior art on what needs to happen?

  15. jkoritzinsky commented on Jul 6, 2020

    @jkoritzinsky
    Member

    I was just chatting with @CarolEidt about #12375 and apparently the code to implement it would be spread across the JIT in various locations.

  16. AndyAyersMS commented on Jul 7, 2020

    @AndyAyersMS
    Member

    @jkoritzinsky so your fix in #38829 will fix this particular test case? If so, I'll assign this back to you and re-label.

    For broader thiscall support, it would be good to clarify what exactly the convention is for non-x86, non-windows, since technically those ABIs don't have thiscall.

  17. jkoritzinsky commented on Jul 7, 2020

    @jkoritzinsky
    Member

    Yes, #38829 will fix this specific case.

    The broader support is being able to call function pointers pointing to native instance methods that return structs on Windows x86, x64, and ARM64 and being able to define reverse-P/Invoke delegates marked with [UnmanagedFunctionPointer(CallingConvention.ThisCall)] or [UnmanagedCallersOnly(CallConvs = typeof(CallConvThiscall))] that can be passed to native and called as though they are native instance methods even when they return structs.

  18. added and removed
    area-CodeGen-coreclr
    on Jul 7, 2020
  19. removed their assignment
    on Jul 7, 2020
  20. jkoritzinsky commented on Jul 7, 2020

    @jkoritzinsky
    Member

    We'd like to be able to define as part of the "extensible calling convention" support in .NET 6 an "instance stdcall" (stdcall but struct return values are always returned byref) calling convention for Windows since that's technically what COM uses (the differences are visible in DirectX) to enable people like @tannergooding to provide DirectX bindings without having to manually account for the calling convention issues. In cases like UnmanagedCallersOnly where there is no IL stub, this needs to be done by the JIT to work correctly.

  21. tannergooding commented on Jul 7, 2020

    @tannergooding
    Member

    @jkoritzinsky, should that comment be a new issue tracking support for delegate* unmanaged[Thiscall, *] where * is Stdcall, Cdecl, or Fastcall?

    I think Stdcall is the primary, but IIRC Cdecl and Fastcalll were slightly tweaked as well (although I've never seen them in practice).

  22. jkoritzinsky commented on Jul 7, 2020

    @jkoritzinsky
    Member

    I think we have an issue tracking it but we haven’t decided on the final shape (if we’re going to do Thiscall + Stdcall or make a new type CallConvInstanceMethod to avoid confusion)

  23. PathogenDavid commented on Jul 8, 2020

    @PathogenDavid
    Contributor

    I think we have an issue tracking it

    Is this an internal issue? If not, can you link it? I couldn't find it anywhere on the runtime repo.

  24. AaronRobinsonMSFT commented on Jul 8, 2020

    @AaronRobinsonMSFT
    Member

    @jkoritzinsky Doesn't look like we have had an official request at the moment. We have one for CallConvSuppressGCTransition, but not for a value indicating the combination of stdcall and thiscall.

    In fact the semantics here are still up for debate since we haven't implemented the supported callconv matrix for function pointers - which will be in the runtime. See #34805.

  25. ghost locked as resolved and limited conversation to collaborators on Dec 10, 2020
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