corert icon indicating copy to clipboard operation
corert copied to clipboard

Add marshalling support for CriticalHandle

Open shrah opened this issue 9 years ago • 11 comments

We need to port CriticalHandle and HandleRef to CoreLib in order to have P/Invoke support for them.

shrah avatar Jan 24 '17 20:01 shrah

Do you know if any progress is made or planned in this issue?

farkasmark avatar Jun 28 '17 10:06 farkasmark

Both classes are already ported to CoreRT, but the marshalling code generator in the CoreRT compiler doesn't handle them.

I think we can repurpose this issue to track implementing this in the CoreRT compiler. (Cc @shrah)

@farkasmark Are you asking about support for this in the CoreRT compiler?

MichalStrehovsky avatar Jun 28 '17 15:06 MichalStrehovsky

@MichalStrehovsky Yes, I am asking about the CoreRT compiler support. I can build the solution successfully but I get a runtime exception from the natively compiled application.

farkasmark avatar Jun 28 '17 15:06 farkasmark

@farkasmark I should be able to work on the marshalling support for CriticalHandle and HandleRef around mid July if that works for you.

shrah avatar Jun 30 '17 00:06 shrah

@shrah That would be perfect! Let me know if I can assist you in that! (in testing for example)

farkasmark avatar Jul 03 '17 07:07 farkasmark

@shrah May I ask if you could have made any progress on this matter? Thanks a lot!

farkasmark avatar Jul 22 '17 10:07 farkasmark

Does anyone have any idea how to get started with this? Thanks!

farkasmark avatar Sep 22 '17 12:09 farkasmark

@farkasmark I apologize for not getting to you sooner. With shipping .NET Native and parental leave I couldn't get to it in time.

If you want to work on it, you can start by looking into the corresponding CoreCLR implementation here https://github.com/dotnet/coreclr/blob/52a816d3011f4d03b80b5940dc036b40d701f52d/src/vm/ilmarshalers.cpp#L3002

shrah avatar Sep 23 '17 00:09 shrah

@shrah Congratulations! :) No problem, I try to make it work based on the corresponding CoreCLR implementation you have given! Thanks for the help and I get back to you if I stuck.

farkasmark avatar Sep 26 '17 18:09 farkasmark

This should not be hard - marking up-for-grabs.

  • For CriticalHandle:

    • Start by creating clone of SafeHandleMarshaller as CriticalHandleMarshaller in https://github.com/dotnet/corert/blob/master/src/Common/src/TypeSystem/Interop/IL/Marshaller.cs. Alternatively, add a bool argument to SafeHandleMarshaller that says whether it is marshalling safehandle or critical handle.
    • Delete code that deals with DangerousAddRef/DangerousRelease in CriticalHandleMarshaller
    • Add a test to https://github.com/dotnet/corert/tree/master/tests/src/Simple/PInvoke to verify that it works
  • For HandleRef:

    • Start by creating clone of DelegateMarshaller as HandleRefMarshaller in https://github.com/dotnet/corert/blob/master/src/Common/src/TypeSystem/Interop/IL/Marshaller.cs.
    • Change AllocAndTransformManagedToNative to fetch _handle field from HandleRef, change EmitCleanupManaged to call KeepAlive on the _object field, and change TransformNativeToManaged to throw since native->managed direction is not supported for HandleRefs.
    • Add a test to - https://github.com/dotnet/corert/tree/master/tests/src/Simple/PInvoke to verify that it works

jkotas avatar Dec 24 '17 17:12 jkotas

HandleRef is done in #6395. What's left is CriticalHandle. It shouldn't be hard to implement based on the information in this issue, so that's still up for grabs.

MichalStrehovsky avatar Oct 11 '18 15:10 MichalStrehovsky