In an application that's using the Redis client library, a significant portion of the allocations are coming from calls to ReadByte on an SslStream. Since SslStream doesn't override ReadByte, the base Stream.ReadByte is used, and it ends up allocating a temporary array to use in a call to Read.
This commit adds an override of ReadByte on SslStream that returns data from the stream's internal buffer if any is available, otherwise falling back to the old behavior.
On a test that repeatedly writes 1000 bytes and then reads each of those with ReadByte, allocations are reduced by 10x and throughput is doubled. (Note that removing the Interlocked.Exchange in ReadByte yields another 20% improvement in throughput, but I've left it in so as not to impact guarantees of the type.)
Commit migrated from https://github.com/dotnet/corefx/commit/b999431c9c519a797aff3eb472b614e0f15c6650
Pass the handle:
The Windows SslStream flow is to start a TLS session as server auth, then
process a CredentialsRequired during the handshake, add credentials to
the existing session, and continue.
It's hard to maintain symmetry on that flow for the Unix pathway, due to
the API differences in OpenSSL and SChannel. So, the Unix session will
always pass down to native the provided client auth credentials, and
OpenSSL can use them when it sees appropriate.
Untrusted certificates should still complete handshake:
The Unix SslStream implementation had two different places that a client
authentication certificate was validated. The first one was from an
OpenSSL callback, and reporting an error will interrupt the TLS handshake.
The second was from after the handshake, and presents the results to the
user validation callback to determine if data should be sent after the handshake.
In the ideal case the first version is redundant, in a user-handled case it caused
a failure when success was expected; so it should just be removed.
Enable the client authentication cert test for Unix:
Also asserts that the IsMutuallyAuthenticated property gets set, the certificates
used were the correct ones, and that both forward and callback versions of
providing the certificate function.
Commit migrated from https://github.com/dotnet/corefx/commit/908a129d5f32a6b5d2737d651445fdd38e1937ea
The design of AuthenticateAsServerAsync has a clientCertificatesRequired flag. On Unix we were mapping that to "SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT", which causes OpenSSL to fail the handshake if no cert is provided. But the Windows code actually still allows this through, instead setting a policy error to indicate that no certificate was required. I'm simply removing the SSL_VERIFY_FAIL_IF_NO_PEER_CERT flag to match that design. I've also updated the test to assert that the error is actually being set.
Commit migrated from https://github.com/dotnet/corefx/commit/81f6c96f4f0562f1dfdf348f5ea9c126be28251e
The method sets a flag to indicate that an operation is in progress, then kicks off the operation, and then in a finally block potentially resets the flag. The intent of resetting it is to reset the flag iff the operation is no longer in progress, which would be the case if the operation was synchronous or if the initiation of the asynchronous operation resulted in an exception. As such, the finally block is checking whether the operation was synchronous or whether an exception has been captured.
However, for the exceptional case, this leads to race conditions. The initiation of the asynchronous operaiton can lead to the operation running concurrently with the remainder of the ProcessAuthentication method, namely concurrently with the finally block. If the operation starts quickly and fails quickly, it'll store the exception, which will cause the finally block to see that an exception has been captured and mistakenly believe that the operation failed synchronously, thus resetting the flag. But the operation was actually asynchronous, and when EndAuthentication is called, it'll see that the flag has been reset and think that the EndAuthentication call is erroneous, thus throwing an exception. While it should throw an exception (the one that caused the operation to fail), it ends up throwing an InvalidOperationException instead to signal that EndAuthentication was used incorrectly.
The fix is to move the check for the exceptional situation out of the finally and into a catch block. The catch block is then used to reset the flag only if an exception emerges synchronously, and the finally block is used to reset the flag only for synchronous operations. (It's possible that for synchronous operations that fail, the flag could be written as 0 twice... that's fine.)
This fixes the failure on both Windows and Unix. The failure wasn't reproing on Windows but only because of differences in timing... adding a brief pause at the end of the try block would trigger the failure on Windows as well.
Commit migrated from https://github.com/dotnet/corefx/commit/9e63a418f6ab0dc01ab9e51f2ad9d498a35899cd
It looks like a couple libraries were using
System.Security.Principal.Windows unintentionally in their
Unix implementations. @dagood discovered this when
trying to restore our tools-runtime project for unix RIDs.
This fix removes the dependencies from those libs
in the CoreFx repro.
/cc @CIPop @saurabh500
It looks like WCF also has this problem as well,
since it hasn't split System.Private.ServiceModel
into windows specific packages.
/cc @StephenBonikowsky
Commit migrated from https://github.com/dotnet/corefx/commit/6902316fed76f2a905ef626b7ef0a64b4749a237
Some more GlobalLog.IsEnabled guards. I noticed a few while doing some other profiling, and so did another set of searches across the whole codebase. Many of these came from the System.Net.Security PRs that were in-progress at the same time I was doing the previous logging changes.
Commit migrated from https://github.com/dotnet/corefx/commit/a04c9898b9bc6d06cd42e1dfd93a63633da4dd30
Substituting Logging calls to EventSource calls, or calls to
LoggingHash.
Formatting Changes
Fixing Aligment, and casing.
Changing Dump methods log the hash of the object calling it instead of the method
After some discussion, it was decided that it is more useful to log the
object where the dump happens, than the method. Doing the necessary
changes for it.
Changing usage of "this" to [CallerMemberName]
Instead of using this to know who called the logging method, using
[CallerMemberName] as an attribute to the parameter to fill that
information.
Adding a TODO item.
Making a note to not change NetEventSource or it can break.
Missing Underscore
Adding missing underscore in _wsaBuffer
Commit migrated from https://github.com/dotnet/corefx/commit/f39502ef910c90ba1e67d787df98b627d39ba6cc