From 969616ef6e9713bcd791fc4505c35f85cc3f73eb Mon Sep 17 00:00:00 2001 From: Lawrence LCI Date: Tue, 3 May 2022 15:38:17 -0700 Subject: [PATCH 1/4] Merge changes from netfx to netcore for TdsParserSafeHandles --- .../Data/SqlClient/TdsParserSafeHandles.cs | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs index 980e0d556c..27fbcd4bc4 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs @@ -7,6 +7,10 @@ using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; +#if NETFRAMEWORK +using System.Runtime.CompilerServices; +using Microsoft.Data.Common; +#endif namespace Microsoft.Data.SqlClient { @@ -23,7 +27,7 @@ internal sealed partial class SNILoadHandle : SafeHandle private SNILoadHandle() : base(IntPtr.Zero, true) { - // From security review - SafeHandle guarantees this is only called once. + // SQL BU DT 346588 - from security review - SafeHandle guarantees this is only called once. // The reason for the safehandle is guaranteed initialization and termination of SNI to // ensure SNI terminates and cleans up properly. try @@ -81,7 +85,12 @@ override protected bool ReleaseHandle() return true; } +#if NETFRAMEWORK +// TODO: update references + public uint SNIStatus => _sniStatus; +#else public uint Status => _sniStatus; +#endif public EncryptionOptions Options => _encryptionOption; @@ -101,7 +110,11 @@ private static void ReadDispatcher(IntPtr key, IntPtr packet, uint error) if (null != stateObj) { +#if NETFRAMEWORK + stateObj.ReadAsyncCallback(IntPtr.Zero, packet, error); +#else stateObj.ReadAsyncCallback(IntPtr.Zero, PacketHandle.FromNativePointer(packet), error); +#endif // NETFRAMEWORK } } } @@ -122,7 +135,11 @@ private static void WriteDispatcher(IntPtr key, IntPtr packet, uint error) if (null != stateObj) { +#if NETFRAMEWORK + stateObj.WriteAsyncCallback(IntPtr.Zero, packet, error); +#else stateObj.WriteAsyncCallback(IntPtr.Zero, PacketHandle.FromNativePointer(packet), error); +#endif // NETFRAMEWORK } } } @@ -144,10 +161,17 @@ internal SNIHandle( bool flushCache, bool fSync, bool fParallel, + #if NETFRAMEWORK + TransparentNetworkResolutionState transparentNetworkResolutionState, + int totalTimeout, + #endif SqlConnectionIPAddressPreference ipPreference, SQLDNSInfo cachedDNSInfo) : base(IntPtr.Zero, true) { +#if NETFRAMEWORK + RuntimeHelpers.PrepareConstrainedRegions(); +#endif try { } finally @@ -156,11 +180,21 @@ internal SNIHandle( instanceName = new byte[256]; // Size as specified by netlibs. if (ignoreSniOpenTimeout) { + // UNDONE: ITEM12001110 (DB Mirroring Reconnect) Old behavior of not truly honoring timeout presevered + // for non-failover scenarios to avoid breaking changes as part of a QFE. Consider fixing timeout + // handling in next full release and removing ignoreSniOpenTimeout parameter. timeout = Timeout.Infinite; // -1 == native SNIOPEN_TIMEOUT_VALUE / INFINITE } +#if NETFRAMEWORK + int transparentNetworkResolutionStateNo = (int)transparentNetworkResolutionState; + _status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref base.handle, + spnBuffer, instanceName, flushCache, fSync, timeout, fParallel, transparentNetworkResolutionStateNo, totalTimeout, + ADP.IsAzureSqlServerEndpoint(serverName), ipPreference, cachedDNSInfo); +#else _status = SNINativeMethodWrapper.SNIOpenSyncEx(myInfo, serverName, ref base.handle, spnBuffer, instanceName, flushCache, fSync, timeout, fParallel, ipPreference, cachedDNSInfo); +#endif // NETFRAMEWORK } } From e2cbf9e92dce02bf94a7889f90ebe0e4537adc25 Mon Sep 17 00:00:00 2001 From: Lawrence LCI Date: Tue, 3 May 2022 15:42:56 -0700 Subject: [PATCH 2/4] Move the netcore version of TdsParserSafeHandles to common src and delete the netfx one and update project references --- .../netcore/src/Microsoft.Data.SqlClient.csproj | 4 +++- .../netfx/src/Microsoft.Data.SqlClient.csproj | 4 +++- .../src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs | 0 3 files changed, 6 insertions(+), 2 deletions(-) rename src/Microsoft.Data.SqlClient/{netcore => }/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs (100%) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj index 70fe79041e..3ef7442f3f 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -475,6 +475,9 @@ Microsoft\Data\SqlClient\TdsParameterSetter.cs + + Microsoft\Data\SqlClient\TdsParserSafeHandles.cs + Microsoft\Data\SqlClient\TdsRecordBufferSetter.cs @@ -744,7 +747,6 @@ - diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj index b1e683badc..d7a7f9ccbc 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj @@ -539,6 +539,9 @@ Microsoft\Data\SqlClient\TdsParameterSetter.cs + + Microsoft\Data\SqlClient\TdsParserSafeHandles.cs + Microsoft\Data\SqlClient\TdsRecordBufferSetter.cs @@ -631,7 +634,6 @@ - diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs similarity index 100% rename from src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs rename to src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs From 4e235d3f3aae2d247c08936d72479438aa2724fc Mon Sep 17 00:00:00 2001 From: Lawrence LCI Date: Wed, 4 May 2022 17:04:48 -0700 Subject: [PATCH 3/4] Address PR comments and fix compiler error on unix since TdsParserSafeHandles only exist on windows --- .../netcore/src/Microsoft.Data.SqlClient.csproj | 6 +++--- .../src/Microsoft/Data/SqlClient/TdsParser.cs | 2 +- .../Data/SqlClient/TdsParserSafeHandles.cs | 15 ++++----------- 3 files changed, 8 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj index 3ef7442f3f..69c337e788 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -475,9 +475,6 @@ Microsoft\Data\SqlClient\TdsParameterSetter.cs - - Microsoft\Data\SqlClient\TdsParserSafeHandles.cs - Microsoft\Data\SqlClient\TdsRecordBufferSetter.cs @@ -665,6 +662,9 @@ + + Microsoft\Data\SqlClient\TdsParserSafeHandles.cs + diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs index 9301a3d958..5e98a649f2 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs @@ -517,7 +517,7 @@ internal void Connect(ServerInfo serverInfo, // Clean up IsSQLDNSCachingSupported flag from previous status _connHandler.IsSQLDNSCachingSupported = false; - UInt32 sniStatus = SNILoadHandle.SingletonInstance.SNIStatus; + UInt32 sniStatus = SNILoadHandle.SingletonInstance.Status; if (sniStatus != TdsEnums.SNI_SUCCESS) { _physicalStateObj.AddError(ProcessSNIError(_physicalStateObj)); diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs index 27fbcd4bc4..b3fb0a568e 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs @@ -5,10 +5,10 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Threading; #if NETFRAMEWORK -using System.Runtime.CompilerServices; using Microsoft.Data.Common; #endif @@ -53,7 +53,7 @@ public bool ClientOSEncryptionSupport { try { - UInt32 value = 0; + uint value = 0; // Query OS to find out whether encryption is supported. SNINativeMethodWrapper.SNIQueryInfo(SNINativeMethodWrapper.QTypes.SNI_QUERY_CLIENT_ENCRYPT_POSSIBLE, ref value); _clientOSEncryptionSupport = value != 0; @@ -85,12 +85,7 @@ override protected bool ReleaseHandle() return true; } -#if NETFRAMEWORK -// TODO: update references - public uint SNIStatus => _sniStatus; -#else public uint Status => _sniStatus; -#endif public EncryptionOptions Options => _encryptionOption; @@ -161,17 +156,15 @@ internal SNIHandle( bool flushCache, bool fSync, bool fParallel, - #if NETFRAMEWORK +#if NETFRAMEWORK TransparentNetworkResolutionState transparentNetworkResolutionState, int totalTimeout, - #endif +#endif SqlConnectionIPAddressPreference ipPreference, SQLDNSInfo cachedDNSInfo) : base(IntPtr.Zero, true) { -#if NETFRAMEWORK RuntimeHelpers.PrepareConstrainedRegions(); -#endif try { } finally From c5f0f10d7d28ea11b36ad26d477fe7dc735cf143 Mon Sep 17 00:00:00 2001 From: Lawrence LCI Date: Thu, 5 May 2022 08:37:04 -0700 Subject: [PATCH 4/4] Rename file with Windows suffix and update references --- .../netcore/src/Microsoft.Data.SqlClient.csproj | 4 ++-- .../netfx/src/Microsoft.Data.SqlClient.csproj | 4 ++-- ...dsParserSafeHandles.cs => TdsParserSafeHandles.Windows.cs} | 0 3 files changed, 4 insertions(+), 4 deletions(-) rename src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/{TdsParserSafeHandles.cs => TdsParserSafeHandles.Windows.cs} (100%) diff --git a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj index 69c337e788..83109c32cc 100644 --- a/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netcore/src/Microsoft.Data.SqlClient.csproj @@ -662,8 +662,8 @@ - - Microsoft\Data\SqlClient\TdsParserSafeHandles.cs + + Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj index d7a7f9ccbc..0404371a14 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft.Data.SqlClient.csproj @@ -539,8 +539,8 @@ Microsoft\Data\SqlClient\TdsParameterSetter.cs - - Microsoft\Data\SqlClient\TdsParserSafeHandles.cs + + Microsoft\Data\SqlClient\TdsParserSafeHandles.Windows.cs Microsoft\Data\SqlClient\TdsRecordBufferSetter.cs diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.Windows.cs similarity index 100% rename from src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.cs rename to src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserSafeHandles.Windows.cs