-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Expose interop manipulation of SafeHandle #46830
Expose interop manipulation of SafeHandle #46830
Comments
What is the purpose of the CreateSafeHandle method? Does it do reflection to access a private parameterless ctor? |
Yes, it uses reflection to access the private parameterless constructor on the passed in SafeHandle-derived type. |
Would it better to change the guidelines for safe handles instead? We can fix the ones that are part of the platform that should cover a lot. SetHandle should be an instance method on SafeHandle. |
That is an option covered in the Alternative Design. I prefer the new APIs since it avoids anyone having to change code in order to consume the new source generator. That is my only argument though so if that isn't a concern then exposing |
I think we should go with changing the guidelines as our first choice. It avoids all issues with private reflection and saves us from building creative solutions to avoid reflection performance issues. If the interop source generators end up needing a private reflection fallback, they can just emit |
Okay. I will flip the proposal then - update guidance and expose |
We think that a hybrid approach is best:
namespace System.Runtime.InteropServices
{
public static class Marshal
{
/// <summary>
/// Initializes the handle of a newly created blah blah blah.
/// </summary>
/// <param name="safeHandle"><see cref="SafeHandle"/> instance to update</param>
/// <param name="handle">Pre-existing handle</param>
public static void InitHandle(SafeHandle safeHandle, IntPtr handle);
}
} |
Could an analyzer be added to help with the guidance? |
@jkoritzinsky, this can be closed now, right? |
We still need to add the analyzer. If we want, we can open another issue to track just that |
Background and Motivation
The
SafeHandle
type was designed around an assumption that the runtime could manage all aspects of its lifetime. The source generation proposal pushed this responsible onto a third party tool (i.e. Roslyn source generator). In order to better support that scenario, the following APIs are proposed to help withSafeHandle
manipulation.Proposed API
Update guidance here to indicate derived types would need a public default constructor. This proposal would include ensuring all
SafeHandle
implementations provided by .NET follow this guidance.Microsoft.Win32.SafeHandles.SafeAccessTokenHandle
Microsoft.Win32.SafeHandles.SafeHandleMinusOneIsInvalid
System.Net.Sockets.SafeSocketHandle
Microsoft.Win32.SafeHandles.SafeHandleZeroOrMinusOneIsInvalid
Microsoft.Win32.SafeHandles.SafeFileHandle
Microsoft.Win32.SafeHandles.SafeMemoryMappedFileHandle
Microsoft.Win32.SafeHandles.SafeNCryptHandle
Microsoft.Win32.SafeHandles.SafePipeHandle
Microsoft.Win32.SafeHandles.SafeProcessHandle
Microsoft.Win32.SafeHandles.SafeRegistryHandle
Microsoft.Win32.SafeHandles.SafeWaitHandle
Microsoft.Win32.SafeHandles.SafeX509ChainHandle
System.Runtime.InteropServices.SafeBuffer
System.Security.Authentication.ExtendedProtection.ChannelBinding
System.Security.Cryptography.SafeEvpPKeyHandle
Expose
SetHandle
as apublic
API so source generators may call it.See #45633 (comment).
Usage Examples
Usage example would the ability to call
SetHandle
and instantiate any type derived fromSafeHandle
so they can be used from a source generator.Alternative Designs
Expose static functions for
SafeHandle
manipulation.Risks
Low. These APIs expose internal lifetime operations that users have often wanted to hide. This is however mitigated by the fact that using reflection these "hidden" APIs can be used regardless.
The text was updated successfully, but these errors were encountered: