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

Add UnmanagedCallersOnly attribute to SafeDeleteSslContext.ReadFromConnection/WriteToConnection methods #55947

Merged
merged 8 commits into from
Aug 17, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ private static extern int AppleCryptoNative_SslSetTargetName(
private static extern int AppleCryptoNative_SslSetAcceptClientCert(SafeSslHandle sslHandle);

[DllImport(Interop.Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_SslSetIoCallbacks")]
internal static extern int SslSetIoCallbacks(
internal static extern unsafe int SslSetIoCallbacks(
SafeSslHandle sslHandle,
SSLReadFunc readCallback,
SSLWriteFunc writeCallback);
delegate* unmanaged<void*, byte*, void**> readCallback,
delegate* unmanaged<void*, byte*, void**> writeCallback);
MaximLipnin marked this conversation as resolved.
Show resolved Hide resolved

[DllImport(Interop.Libraries.AppleCryptoNative, EntryPoint = "AppleCryptoNative_SslWrite")]
internal static extern unsafe PAL_TlsIo SslWrite(SafeSslHandle sslHandle, byte* writeFrom, int count, out int bytesWritten);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics;
using System.Net.Http;
using System.Net.Security;
using System.Runtime.InteropServices;
using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates;
using Microsoft.Win32.SafeHandles;
Expand All @@ -22,8 +23,6 @@ internal sealed class SafeDeleteSslContext : SafeDeleteContext
private const int OSStatus_errSSLWouldBlock = -9803;
private const int InitialBufferSize = 2048;
private SafeSslHandle _sslContext;
private Interop.AppleCrypto.SSLReadFunc _readCallback;
private Interop.AppleCrypto.SSLWriteFunc _writeCallback;
private ArrayBuffer _inputBuffer = new ArrayBuffer(InitialBufferSize);
private ArrayBuffer _outputBuffer = new ArrayBuffer(InitialBufferSize);

Expand All @@ -38,18 +37,12 @@ public SafeDeleteSslContext(SafeFreeSslCredentials credential, SslAuthentication
{
int osStatus;

unsafe
{
_readCallback = ReadFromConnection;
_writeCallback = WriteToConnection;
}

_sslContext = CreateSslContext(credential, sslAuthenticationOptions.IsServer);

osStatus = Interop.AppleCrypto.SslSetIoCallbacks(
_sslContext,
_readCallback,
_writeCallback);
&ReadFromConnection,
&WriteToConnection);

if (osStatus != 0)
{
Expand Down Expand Up @@ -160,7 +153,8 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

private unsafe int WriteToConnection(void* connection, byte* data, void** dataLength)
[UnmanagedCallersOnly]
private static unsafe int WriteToConnection(void* connection, byte* data, void** dataLength)
{
// 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 @@ -188,7 +182,8 @@ private unsafe int WriteToConnection(void* connection, byte* data, void** dataLe
}
}

private unsafe int ReadFromConnection(void* connection, byte* data, void** dataLength)
[UnmanagedCallersOnly]
private static unsafe int ReadFromConnection(void* connection, byte* data, void** dataLength)
{
try
{
Expand Down