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

SafeHandle marshalling #133

Merged
merged 8 commits into from
Oct 5, 2020

Conversation

jkoritzinsky
Copy link
Member

Implement default marshalling for SafeHandles using both CoreCLR and CoreRT's implementations as a baseline.

Introduce a new phase : "LeakSafeUnmarshal" to represent unmarshalling steps that cause leaks if skipped in exceptional cases.

Since we don't have access to a Compilation object in MarshallingGenerators.TryCreate and plumbing one down there requires passing it through 3 or 4 layers of the API, I've renamed the MarshallingAttributeInfo distriminated union to MarshallingInfo and extended it to also represent marshalling info that requires a Compilation object to calculate it.

I've added some xunit-like execution tests in the Demo project that the Main method just calls directly until we decide how we want to do our execution testing.

Extend the MarshallingInfo discriminated union to not only represent attribute info but to also represent any other marshalling info that requires a Compilation object to determine. This avoids having to plumb passing down a compilation object through 4 API layers and makes the marshaller selection switch statement a little bit simpler.
… using xunit asserts that currently we just call directly in the Demo.
…mentation to explain the overall logic of SafeHandle marshaller.

Signed-off-by: Jeremy Koritzinsky <jekoritz@microsoft.com>
@jkoritzinsky jkoritzinsky added the area-DllImportGenerator Source Generated stubs for P/Invokes in C# label Sep 30, 2020
/// where native values will leak if we
/// fail to unmarshal, but do run cleanup.
/// </summary>
LeakSafeUnmarshal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be useful to have a comment that describes the overall structure of the method to make it clear how the stages fit together. Something like:

... Setup ...
try
{
    ... Marshal ...
    fixed (... Pin ...)
    {
        ... Invoke ...
    }
    ... Unmarshal ...
}
finally
{
    ... Cleanup...
}

... KeepAlive ...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something we are attempting to push into the design documents: https://github.com/dotnet/runtimelab/blob/DllImportGenerator/DllImportGenerator/designs/Pipeline.md

Which also need to be updated with these new stages.

DllImportGenerator/DllImportGenerator/StubCodeContext.cs Outdated Show resolved Hide resolved
/// where native values will leak if we
/// fail to unmarshal, but do run cleanup.
/// </summary>
LeakSafeUnmarshal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something we are attempting to push into the design documents: https://github.com/dotnet/runtimelab/blob/DllImportGenerator/DllImportGenerator/designs/Pipeline.md

Which also need to be updated with these new stages.

…rshaller.cs

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
@jkotas
Copy link
Member

jkotas commented Sep 30, 2020

an exception can throw in a finally

I think exception thrown during cleanup should be a non-scenario.

@jkoritzinsky
Copy link
Member Author

Any more feedback?

InvocationExpression(
MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
ParseTypeName(TypeNames.System_Runtime_InteropServices_MarshalEx),
IdentifierName("SetHandle")),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being a guest here, but happened to see this is the only that doesn't currently use nameof?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MarshalEx type isn't a type in the .NET Standard 2.0 contract, so we can't use nameof for any of its methods. It's currently in Ancillary.Interop. In the future its methods will move into dotnet/runtime in some fashion.

@jkoritzinsky jkoritzinsky merged commit 2cde5aa into dotnet:DllImportGenerator Oct 5, 2020
@jkoritzinsky jkoritzinsky deleted the safehandle branch October 5, 2020 21:49
jkoritzinsky added a commit to jkoritzinsky/runtime that referenced this pull request Sep 20, 2021
Co-authored-by: Jan Kotas <jkotas@microsoft.com>

Commit migrated from dotnet/runtimelab@2cde5aa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-DllImportGenerator Source Generated stubs for P/Invokes in C#
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants