Repository navigation
Closed
Description
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
jkoritzinsky commentedon Mar 4, 2020
This is likely related to #12375
jkoritzinsky commentedon Mar 4, 2020
After more investigating, this is actually an interop bug with the IL stubs. I'll fix it.
jkoritzinsky commentedon Mar 5, 2020
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.
jkoritzinsky commentedon Mar 5, 2020
The second bug would fall on the JIT's support of thiscall. cc: @davidwrighton
davidwrighton commentedon Mar 9, 2020
@dotnet/jit-contrib Could someone look at the incorrect behavior that @jkoritzinsky is talking about here?
AaronRobinsonMSFT commentedon May 6, 2020
Updating tag based on this statement.
/cc @dotnet/jit-contrib
AndyAyersMS commentedon Jun 5, 2020
I'll take a look.
8 remaining items
AaronRobinsonMSFT commentedon Jul 6, 2020
@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?
jkoritzinsky commentedon Jul 6, 2020
I was just chatting with @CarolEidt about #12375 and apparently the code to implement it would be spread across the JIT in various locations.
AndyAyersMS commentedon Jul 7, 2020
@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.
jkoritzinsky commentedon Jul 7, 2020
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.jkoritzinsky commentedon Jul 7, 2020
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
UnmanagedCallersOnlywhere there is no IL stub, this needs to be done by the JIT to work correctly.tannergooding commentedon Jul 7, 2020
@jkoritzinsky, should that comment be a new issue tracking support for
delegate* unmanaged[Thiscall, *]where*isStdcall,Cdecl, orFastcall?I think Stdcall is the primary, but IIRC Cdecl and Fastcalll were slightly tweaked as well (although I've never seen them in practice).
jkoritzinsky commentedon Jul 7, 2020
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)
PathogenDavid commentedon Jul 8, 2020
Is this an internal issue? If not, can you link it? I couldn't find it anywhere on the runtime repo.
AaronRobinsonMSFT commentedon Jul 8, 2020
@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 ofstdcallandthiscall.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.