runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Marshal.PtrToStructure CLR Error on .NET 6.0

Open darius-iko opened this issue 4 years ago • 10 comments

Description

Here is my test code:

`
var vtablePtr = Native.DerivedClassPtrToTestClassPtr(derivedVTablePtr);

            {
                // IBase1 + IBase2
                var ibase2Ptr = Native.TestClassPtrToIBase2Ptr(vtablePtr);
                IntPtr vt = Marshal.ReadIntPtr(ibase2Ptr.Ptr);

                // This will error on .NET 6 !!!
                **var ibase2 = Marshal.PtrToStructure<Native.IBase2>(vt);**

                Console.WriteLine($"C#, Result: {ibase2.IBase1Method1(new Native.IBase1Ptr { Ptr = ibase2Ptr.Ptr }, 10000)}");
                Console.WriteLine($"C#, Result: {ibase2.IBase1Method2(new Native.IBase1Ptr { Ptr = ibase2Ptr.Ptr }, 10000)}");
                Console.WriteLine($"C#, Result: {ibase2.IBase1Method3(new Native.IBase1Ptr { Ptr = ibase2Ptr.Ptr }, 10000)}");
                Console.WriteLine($"C#, Result: {ibase2.IBase2Method1(ibase2Ptr, 10000)}");
                Console.WriteLine($"C#, Result: {ibase2.IBase2Method2(ibase2Ptr, 10000)}");
                Console.WriteLine();
            }

`

Marshal.PtrToStructure does not error when the project target framework is set to .NET 5.0, but that code will become CLR error when the target framework is set to .NET 6. image

Here is the project solution: ConsoleAppError.zip

Reproduction Steps

Marshal.PtrToStructure does not error when the project target framework is set to .NET 5.0, but that code will become CLR error when the target framework is set to .NET 6.

Expected behavior

Marshal.PtrToStructure does not error on .NET 5 and .NET 6.

Actual behavior

Marshal.PtrToStructure does not error when the project target framework is set to .NET 5.0, but that code will become CLR error when the target framework is set to .NET 6.

Regression?

yes, it works on .NET 5.

Known Workarounds

No response

Configuration

.NET 6 Windows 10 x64

Other information

No response

darius-iko avatar Oct 11 '22 05:10 darius-iko

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ghost avatar Oct 11 '22 05:10 ghost

Thank you for the repro project the actual error is from Marshal.GetDelegateForFunctionPointer

System.InvalidProgramException: Common Language Runtime detected an invalid program.
   at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointerInternal(IntPtr ptr, Type t)
   at System.Runtime.InteropServices.Marshal.PtrToStructureHelper(IntPtr ptr, Object structure, Boolean allowValueClasses)
   at System.Runtime.InteropServices.Marshal.PtrToStructure[T](IntPtr ptr)
   at ConsoleAppError.TestClass.TestAll() in C:\Users\elfung\Downloads\ConsoleAppError\ConsoleAppError\ConsoleAppError\TestClass.cs:line 27
   at ConsoleAppError.Program.Main(String[] args) in C:\Users\elfung\Downloads\ConsoleAppError\ConsoleAppError\ConsoleAppError\Program.cs:line 47

when trying to handle the IBase2MethodInt delegate field on IBase2.

We hit the below https://github.com/dotnet/runtime/blob/51e3afb2ba42609c3711cb50fb5eaa6800bc1fde/src/coreclr/jit/importercalls.cpp#L4152-L4155 because the first argument for a ThisCall function is expected to be a pointer, but it is defined as a struct.

[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
public delegate int IBase2MethodInt(IBase2Ptr ptr, int par);

@darius-iko could you try switching your thiscall delegates to take a pointer instead?

@AaronRobinsonMSFT @jkoritzinsky I think .NET 5 is allowing blittable structs as the first argument for a thiscall delegate and we ended up being more strict about the argument type when we pushed some of the handling from interop to the JIT. While it does manifest as a regression from .NET 5, I think the current behaviour is technically correct and would rather avoid adding back any handling for the previous behaviour. Thoughts?

elinor-fung avatar Oct 19 '22 01:10 elinor-fung

While it does manifest as a regression from .NET 5, I think the current behaviour is technically correct and would rather avoid adding back any handling for the previous behaviour. Thoughts?

Agree.

Open to changes if there is a large enough impact to the community though, however I'm doubtful this impacts many since it has been this way for a few releases.

AaronRobinsonMSFT avatar Oct 19 '22 14:10 AaronRobinsonMSFT

This issue has been marked needs-author-action and may be missing some important information.

ghost avatar Oct 19 '22 14:10 ghost

@elinor-fung could you fix the project solution I provided in the first post and repost the fixed solution? I got that solution from other place and I am new to CLR topics.

darius-iko avatar Oct 22 '22 10:10 darius-iko

@darius-iko The key part is changing the delegate to take an IntPtr as the first argument instead of the structs. This is what was causing the marshalling to fail.

[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
-public delegate int IBase1MethodInt(IBase1Ptr ptr, int par);
+public delegate int IBase1MethodInt(IntPtr ptr, int par);

[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
-public delegate int IBase2MethodInt(IBase2Ptr ptr, int par);
+public delegate int IBase2MethodInt(IntPtr ptr, int par);

And then when calling it, pass in the pointer directly rather than wrapping it in a struct. For example:

-Console.WriteLine($"C#, Result: {ibase2.IBase1Method1(new Native.IBase1Ptr { Ptr = ibase2Ptr.Ptr }, 10000)}");
+Console.WriteLine($"C#, Result: {ibase2.IBase1Method1(ibase2Ptr.Ptr, 10000)}");

This is an updated version of the TestClass.cs file from your project (renamed to .txt for upload). It is the only thing that needs to change. TestClass.txt

elinor-fung avatar Oct 25 '22 17:10 elinor-fung

@elinor-fung Thank you

darius-iko avatar Oct 26 '22 01:10 darius-iko

@darius-iko Does that work for you? Do you have any other concerns here?

elinor-fung avatar Oct 27 '22 00:10 elinor-fung

@elinor-fung yes, it works. Thank you.

darius-iko avatar Oct 27 '22 12:10 darius-iko

Glad to hear it. I'm going to close this then.

elinor-fung avatar Oct 27 '22 16:10 elinor-fung