Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,13 @@ namespace System.Net
{
internal sealed class SafeSslHandle : SafeHandle
{
// Backreference used by AppleCryptoNative_SslSetConnection so native
// Read/Write callbacks can resolve the owning SafeDeleteSslContext.
// Owned here so the lifetime is tied to ReleaseHandle, which only
// runs once all outstanding P/Invokes (and therefore any in-flight
// callbacks) have completed.
private GCHandle<SafeDeleteSslContext> _connectionGCHandle;

public SafeSslHandle()
: base(IntPtr.Zero, ownsHandle: true)
{
Expand All @@ -470,10 +477,18 @@ internal SafeSslHandle(IntPtr invalidHandleValue, bool ownsHandle)
{
}

internal void SetConnectionGCHandle(GCHandle<SafeDeleteSslContext> handle)
{
Debug.Assert(!_connectionGCHandle.IsAllocated, "Connection GCHandle already set");
_connectionGCHandle = handle;
}

protected override bool ReleaseHandle()
{
Interop.CoreFoundation.CFRelease(handle);
SetHandle(IntPtr.Zero);
_connectionGCHandle.Dispose();
Comment thread
liveans marked this conversation as resolved.

return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,10 @@ private static SafeSslHandle CreateSslContext(SslAuthenticationOptions sslAuthen

private void SslSetConnection(SafeSslHandle sslContext)
{
GCHandle handle = GCHandle.Alloc(this, GCHandleType.Weak);
var handle = new GCHandle<SafeDeleteSslContext>(this);
sslContext.SetConnectionGCHandle(handle);

Interop.AppleCrypto.SslSetConnection(sslContext, GCHandle.ToIntPtr(handle));
Interop.AppleCrypto.SslSetConnection(sslContext, GCHandle<SafeDeleteSslContext>.ToIntPtr(handle));
Comment thread
liveans marked this conversation as resolved.
}

public override bool IsInvalid => _sslContext?.IsInvalid ?? true;
Expand All @@ -220,8 +221,7 @@ protected override void Dispose(bool disposing)
[UnmanagedCallersOnly]
private static unsafe int WriteToConnection(IntPtr connection, byte* data, void** dataLength)
{
SafeDeleteSslContext? context = (SafeDeleteSslContext?)GCHandle.FromIntPtr(connection).Target;
Debug.Assert(context != null);
SafeDeleteSslContext context = GCHandle<SafeDeleteSslContext>.FromIntPtr(connection).Target;

Comment thread
liveans marked this conversation as resolved.
// We don't pool these buffers and we can't because there's a race between their us in the native
// read/write callbacks and being disposed when the SafeHandle is disposed. This race is benign currently,
Expand Down Expand Up @@ -255,8 +255,7 @@ private static unsafe int WriteToConnection(IntPtr connection, byte* data, void*
[UnmanagedCallersOnly]
private static unsafe int ReadFromConnection(IntPtr connection, byte* data, void** dataLength)
{
SafeDeleteSslContext? context = (SafeDeleteSslContext?)GCHandle.FromIntPtr(connection).Target;
Debug.Assert(context != null);
SafeDeleteSslContext context = GCHandle<SafeDeleteSslContext>.FromIntPtr(connection).Target;

Comment thread
liveans marked this conversation as resolved.
try
{
Expand Down
Loading