From 3bd3361e7ef2e91d05c30a333175b4e49d013537 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Wed, 2 Sep 2020 14:24:05 -0700 Subject: [PATCH 1/6] Enabling CA1416 --- eng/CodeAnalysis.ruleset | 1 - .../src/Interop/Windows/NCrypt/Interop.Properties.cs | 7 ++++++- .../Interop/Windows/WinSock/SafeNativeOverlapped.cs | 1 + .../src/System/Net/ContextAwareResult.Windows.cs | 3 +++ .../src/EventLogLogger.cs | 4 ++++ .../src/WindowsEventLog.cs | 5 +++++ .../src/System/Drawing/Icon.Windows.cs | 1 + .../src/System/Drawing/Internal/SystemColorTracker.cs | 2 ++ .../src/System/IO/Ports/SerialPort.Win32.cs | 5 +++++ .../Net/Sockets/BaseOverlappedAsyncResult.Windows.cs | 2 ++ .../src/System/Net/Sockets/SafeSocketHandle.Windows.cs | 2 ++ .../System/Net/Sockets/SocketAsyncEventArgs.Windows.cs | 10 +++++++++- .../Cryptography/Pal/Windows/PkcsPalWindows.cs | 4 ++++ 13 files changed, 44 insertions(+), 3 deletions(-) diff --git a/eng/CodeAnalysis.ruleset b/eng/CodeAnalysis.ruleset index 3560577d8f3c2b..293d25a2161602 100644 --- a/eng/CodeAnalysis.ruleset +++ b/eng/CodeAnalysis.ruleset @@ -64,7 +64,6 @@ - diff --git a/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Properties.cs b/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Properties.cs index 79ac4f59950d91..eb81adcda802d1 100644 --- a/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Properties.cs +++ b/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Properties.cs @@ -1,9 +1,11 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using System; +using System.Diagnostics; using System.Runtime.InteropServices; +using System.Runtime.Versioning; using System.Security.Cryptography; -using System.Diagnostics; using Microsoft.Win32.SafeHandles; @@ -17,6 +19,7 @@ internal static partial class NCrypt [DllImport(Interop.Libraries.NCrypt, CharSet = CharSet.Unicode)] internal static extern unsafe ErrorCode NCryptSetProperty(SafeNCryptHandle hObject, string pszProperty, [In] void* pbInput, int cbInput, CngPropertyOptions dwFlags); + [SupportedOSPlatform("windows")] internal static ErrorCode NCryptGetByteProperty(SafeNCryptHandle hObject, string pszProperty, ref byte result, CngPropertyOptions options = CngPropertyOptions.None) { int cbResult; @@ -53,6 +56,8 @@ internal static ErrorCode NCryptGetIntProperty(SafeNCryptHandle hObject, string { fixed (int* pResult = &result) { + Debug.Assert(OperatingSystem.IsWindows()); + errorCode = Interop.NCrypt.NCryptGetProperty( hObject, pszProperty, diff --git a/src/libraries/Common/src/Interop/Windows/WinSock/SafeNativeOverlapped.cs b/src/libraries/Common/src/Interop/Windows/WinSock/SafeNativeOverlapped.cs index ef51b07b44d69b..bf684547ac8ee2 100644 --- a/src/libraries/Common/src/Interop/Windows/WinSock/SafeNativeOverlapped.cs +++ b/src/libraries/Common/src/Interop/Windows/WinSock/SafeNativeOverlapped.cs @@ -57,6 +57,7 @@ private void FreeNativeOverlapped() { unsafe { + Debug.Assert(OperatingSystem.IsWindows()); Debug.Assert(_socketHandle != null, "_socketHandle is null."); ThreadPoolBoundHandle? boundHandle = _socketHandle.IOCPBoundHandle; diff --git a/src/libraries/Common/src/System/Net/ContextAwareResult.Windows.cs b/src/libraries/Common/src/System/Net/ContextAwareResult.Windows.cs index e7c57f9df35cc4..3ff41453851d4a 100644 --- a/src/libraries/Common/src/System/Net/ContextAwareResult.Windows.cs +++ b/src/libraries/Common/src/System/Net/ContextAwareResult.Windows.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. #nullable enable +using System.Diagnostics; using System.Security.Principal; namespace System.Net @@ -13,6 +14,7 @@ internal partial class ContextAwareResult // Security: We need an assert for a call into WindowsIdentity.GetCurrent. private void SafeCaptureIdentity() { + Debug.Assert(OperatingSystem.IsWindows()); _windowsIdentity = WindowsIdentity.GetCurrent(); } @@ -20,6 +22,7 @@ private void CleanupInternal() { if (_windowsIdentity != null) { + Debug.Assert(OperatingSystem.IsWindows()); _windowsIdentity.Dispose(); _windowsIdentity = null; } diff --git a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLogger.cs b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLogger.cs index a8ee86b54282ca..7e3998c6f5f9cc 100644 --- a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLogger.cs +++ b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLogger.cs @@ -164,6 +164,8 @@ private void WriteMessage(string message, EventLogEntryType eventLogEntryType, i } } +#pragma warning disable CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available + private EventLogEntryType GetEventLogEntryType(LogLevel level) { switch (level) @@ -181,5 +183,7 @@ private EventLogEntryType GetEventLogEntryType(LogLevel level) return EventLogEntryType.Information; } } + +#pragma warning restore CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available } } diff --git a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/WindowsEventLog.cs b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/WindowsEventLog.cs index 0c72558ec88db1..70b2bc7972e6ac 100644 --- a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/WindowsEventLog.cs +++ b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/WindowsEventLog.cs @@ -4,9 +4,12 @@ using System; using System.Diagnostics; using System.Security; +using System.Runtime.Versioning; namespace Microsoft.Extensions.Logging.EventLog { +#pragma warning disable CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available + internal class WindowsEventLog : IEventLog { // https://msdn.microsoft.com/EN-US/library/windows/desktop/aa363679.aspx @@ -52,4 +55,6 @@ public void WriteEntry(string message, EventLogEntryType type, int eventID, shor } } } + +#pragma warning restore CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs index 7e1834e3708f86..fc9e32b06975df 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs @@ -657,6 +657,7 @@ public void Save(Stream outputStream) } finally { + Debug.Assert(OperatingSystem.IsWindows()); Marshal.ReleaseComObject(picture); } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Internal/SystemColorTracker.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Internal/SystemColorTracker.cs index 2f53b9f4095d45..609a547be45e6e 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Internal/SystemColorTracker.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Internal/SystemColorTracker.cs @@ -39,6 +39,7 @@ internal static void Add(ISystemColorTracker obj) if (!addedTracker) { + Debug.Assert(OperatingSystem.IsWindows()); addedTracker = true; SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(OnUserPreferenceChanged); } @@ -131,6 +132,7 @@ private static void GarbageCollectList() private static void OnUserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e) { + Debug.Assert(OperatingSystem.IsWindows()); // Update pens and brushes if (e.Category == UserPreferenceCategory.Color) diff --git a/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.Win32.cs b/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.Win32.cs index aa799d677a211e..e355ba53fd1369 100644 --- a/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.Win32.cs +++ b/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.Win32.cs @@ -3,6 +3,7 @@ using Microsoft.Win32; using System.ComponentModel; +using System.Diagnostics; namespace System.IO.Ports { @@ -17,6 +18,8 @@ public static string[] GetPortNames() // // QueryDosDevice involves finding any ports that map to \Device\Serialx (call with null to get all, then iterate to get the actual device name) +#pragma warning disable CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available + using (RegistryKey serialKey = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM")) { if (serialKey != null) @@ -31,6 +34,8 @@ public static string[] GetPortNames() } } +#pragma warning restore CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available + return Array.Empty(); } } diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.Windows.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.Windows.cs index 3b305d33d56095..d431c2447514d6 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.Windows.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/BaseOverlappedAsyncResult.Windows.cs @@ -50,6 +50,7 @@ internal void SetUnmanagedStructures(object? objectsToPin) unsafe { + Debug.Assert(OperatingSystem.IsWindows()); NativeOverlapped* overlapped = boundHandle.AllocateNativeOverlapped(s_ioCallback, this, objectsToPin); _nativeOverlapped = new SafeNativeOverlapped(s.SafeHandle, overlapped); } @@ -59,6 +60,7 @@ internal void SetUnmanagedStructures(object? objectsToPin) private static unsafe void CompletionPortCallback(uint errorCode, uint numBytes, NativeOverlapped* nativeOverlapped) { + Debug.Assert(OperatingSystem.IsWindows()); BaseOverlappedAsyncResult asyncResult = (BaseOverlappedAsyncResult)ThreadPoolBoundHandle.GetNativeOverlappedState(nativeOverlapped)!; if (asyncResult.InternalPeekCompleted) diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SafeSocketHandle.Windows.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SafeSocketHandle.Windows.cs index 29b3f7da3ea4e5..ad59c2e65dbc27 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SafeSocketHandle.Windows.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SafeSocketHandle.Windows.cs @@ -48,6 +48,7 @@ internal ThreadPoolBoundHandle GetOrAllocateThreadPoolBoundHandle(bool trySkipCo try { + Debug.Assert(OperatingSystem.IsWindows()); // The handle (this) may have been already released: // E.g.: The socket has been disposed in the main thread. A completion callback may // attempt starting another operation. @@ -105,6 +106,7 @@ private bool OnHandleClose() // ThreadPoolBoundHandle allows FreeNativeOverlapped even after it has been disposed. if (_iocpBoundHandle != null) { + Debug.Assert(OperatingSystem.IsWindows()); _iocpBoundHandle.Dispose(); } diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs index 6368666ec6bf43..0c4e2b1487a096 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs @@ -56,6 +56,7 @@ private void InitializeInternals() bool suppressFlow = !ExecutionContext.IsFlowSuppressed(); try { + Debug.Assert(OperatingSystem.IsWindows()); if (suppressFlow) ExecutionContext.SuppressFlow(); _preAllocatedOverlapped = new PreAllocatedOverlapped(s_completionPortCallback, _strongThisRef, null); } @@ -75,6 +76,7 @@ private void FreeInternals() private unsafe NativeOverlapped* AllocateNativeOverlapped() { + Debug.Assert(OperatingSystem.IsWindows()); Debug.Assert(_operating == InProgress, $"Expected {nameof(_operating)} == {nameof(InProgress)}, got {_operating}"); Debug.Assert(_currentSocket != null, "_currentSocket is null"); Debug.Assert(_currentSocket.SafeHandle != null, "_currentSocket.SafeHandle is null"); @@ -86,6 +88,7 @@ private void FreeInternals() private unsafe void FreeNativeOverlapped(NativeOverlapped* overlapped) { + Debug.Assert(OperatingSystem.IsWindows()); Debug.Assert(overlapped != null, "overlapped is null"); Debug.Assert(_operating == InProgress, $"Expected _operating == InProgress, got {_operating}"); Debug.Assert(_currentSocket != null, "_currentSocket is null"); @@ -904,6 +907,7 @@ private void FreeOverlapped() // any pinned buffers. if (_preAllocatedOverlapped != null) { + Debug.Assert(OperatingSystem.IsWindows()); _preAllocatedOverlapped.Dispose(); _preAllocatedOverlapped = null!; } @@ -1219,7 +1223,11 @@ private void FinishOperationSendPackets() private static readonly unsafe IOCompletionCallback s_completionPortCallback = delegate (uint errorCode, uint numBytes, NativeOverlapped* nativeOverlapped) { - var saeaBox = (StrongBox)ThreadPoolBoundHandle.GetNativeOverlappedState(nativeOverlapped)!; +#pragma warning disable CA1416 // https://github.com/dotnet/roslyn-analyzers/issues/4090 + Debug.Assert(OperatingSystem.IsWindows()); + var saeaBox = (StrongBox)(ThreadPoolBoundHandle.GetNativeOverlappedState(nativeOverlapped)!); +#pragma warning restore CA1416 // https://github.com/dotnet/roslyn-analyzers/issues/4090 + Debug.Assert(saeaBox.Value != null); SocketAsyncEventArgs saea = saeaBox.Value; diff --git a/src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/PkcsPalWindows.cs b/src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/PkcsPalWindows.cs index a720ce02fd72d8..31847e718e85a0 100644 --- a/src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/PkcsPalWindows.cs +++ b/src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/PkcsPalWindows.cs @@ -129,6 +129,10 @@ public sealed override byte[] GetSubjectKeyIdentifier(X509Certificate2 certifica if (keySpec == CryptKeySpec.CERT_NCRYPT_KEY_SPEC) { +#if NET5_0 + Debug.Assert(OperatingSystem.IsWindows()); +#endif + using (SafeNCryptKeyHandle keyHandle = new SafeNCryptKeyHandle(handle.DangerousGetHandle(), handle)) { CngKeyHandleOpenOptions options = CngKeyHandleOpenOptions.None; From 3c0cbc52eb399f9210c11036a15007b96a8b94c3 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Fri, 4 Sep 2020 12:45:15 -0700 Subject: [PATCH 2/6] Resolving PR feedback --- eng/Analyzers.props | 2 +- eng/CodeAnalysis.ruleset | 1 + .../src/EventLogLogger.cs | 7 +++---- .../src/EventLogLoggerProvider.cs | 3 +++ .../src/EventLogSettings.cs | 4 ++++ .../src/Microsoft.Extensions.Logging.EventLog.csproj | 3 ++- .../src/WindowsEventLog.cs | 5 +---- .../src/System/IO/Ports/SerialPort.Win32.cs | 5 ++--- 8 files changed, 17 insertions(+), 13 deletions(-) diff --git a/eng/Analyzers.props b/eng/Analyzers.props index ff442658c33187..21c6db5529efa1 100644 --- a/eng/Analyzers.props +++ b/eng/Analyzers.props @@ -6,7 +6,7 @@ - + diff --git a/eng/CodeAnalysis.ruleset b/eng/CodeAnalysis.ruleset index 293d25a2161602..bf63f11bb05a17 100644 --- a/eng/CodeAnalysis.ruleset +++ b/eng/CodeAnalysis.ruleset @@ -64,6 +64,7 @@ + diff --git a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLogger.cs b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLogger.cs index 7e3998c6f5f9cc..6f760ac45f9516 100644 --- a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLogger.cs +++ b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLogger.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Diagnostics; +using System.Runtime.InteropServices; using System.Text; namespace Microsoft.Extensions.Logging.EventLog @@ -164,10 +165,10 @@ private void WriteMessage(string message, EventLogEntryType eventLogEntryType, i } } -#pragma warning disable CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available - private EventLogEntryType GetEventLogEntryType(LogLevel level) { + Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); + switch (level) { case LogLevel.Information: @@ -183,7 +184,5 @@ private EventLogEntryType GetEventLogEntryType(LogLevel level) return EventLogEntryType.Information; } } - -#pragma warning restore CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available } } diff --git a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLoggerProvider.cs b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLoggerProvider.cs index 4bc27bd6a15b83..d5470a8fe03f0b 100644 --- a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLoggerProvider.cs +++ b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLoggerProvider.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Diagnostics; +using System.Runtime.InteropServices; using Microsoft.Extensions.Options; namespace Microsoft.Extensions.Logging.EventLog @@ -53,6 +55,7 @@ public void Dispose() { if (_settings.EventLog is WindowsEventLog windowsEventLog) { + Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); windowsEventLog.DiagnosticsEventLog.Dispose(); } } diff --git a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogSettings.cs b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogSettings.cs index 029a35dc318724..ff82d41fd7dc67 100644 --- a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogSettings.cs +++ b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogSettings.cs @@ -2,6 +2,8 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Diagnostics; +using System.Runtime.InteropServices; namespace Microsoft.Extensions.Logging.EventLog { @@ -53,6 +55,8 @@ private IEventLog CreateDefaultEventLog() defaultEventId = 1000; } + + Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); return new WindowsEventLog(logName, machineName, sourceName) { DefaultEventId = defaultEventId }; } } diff --git a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/Microsoft.Extensions.Logging.EventLog.csproj b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/Microsoft.Extensions.Logging.EventLog.csproj index 9ae5802d972d1a..5e290a023ea830 100644 --- a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/Microsoft.Extensions.Logging.EventLog.csproj +++ b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/Microsoft.Extensions.Logging.EventLog.csproj @@ -3,6 +3,7 @@ netstandard2.0;net461 true + true @@ -18,7 +19,7 @@ - + diff --git a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/WindowsEventLog.cs b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/WindowsEventLog.cs index 70b2bc7972e6ac..b148714f55289e 100644 --- a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/WindowsEventLog.cs +++ b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/WindowsEventLog.cs @@ -8,8 +8,7 @@ namespace Microsoft.Extensions.Logging.EventLog { -#pragma warning disable CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available - + [SupportedOSPlatform("windows")] internal class WindowsEventLog : IEventLog { // https://msdn.microsoft.com/EN-US/library/windows/desktop/aa363679.aspx @@ -55,6 +54,4 @@ public void WriteEntry(string message, EventLogEntryType type, int eventID, shor } } } - -#pragma warning restore CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available } diff --git a/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.Win32.cs b/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.Win32.cs index e355ba53fd1369..fbc38aa3cb0cd4 100644 --- a/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.Win32.cs +++ b/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.Win32.cs @@ -4,6 +4,7 @@ using Microsoft.Win32; using System.ComponentModel; using System.Diagnostics; +using System.Runtime.InteropServices; namespace System.IO.Ports { @@ -18,7 +19,7 @@ public static string[] GetPortNames() // // QueryDosDevice involves finding any ports that map to \Device\Serialx (call with null to get all, then iterate to get the actual device name) -#pragma warning disable CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available + Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); using (RegistryKey serialKey = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM")) { @@ -34,8 +35,6 @@ public static string[] GetPortNames() } } -#pragma warning restore CA1416 // Debug.Assert(OperatingSystem.IsWindows()) is not available - return Array.Empty(); } } From 91777a42630c8e6ca194109bb805ccb46071a6fb Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Tue, 8 Sep 2020 14:20:00 -0700 Subject: [PATCH 3/6] Updating the .NET analyzers to 5.0.0-rc2.20458.2 --- eng/Analyzers.props | 2 +- .../src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/eng/Analyzers.props b/eng/Analyzers.props index 21c6db5529efa1..8babf8c41f5f9b 100644 --- a/eng/Analyzers.props +++ b/eng/Analyzers.props @@ -6,7 +6,7 @@ - + diff --git a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs index 0c4e2b1487a096..6b304a2b8b34ec 100644 --- a/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs +++ b/src/libraries/System.Net.Sockets/src/System/Net/Sockets/SocketAsyncEventArgs.Windows.cs @@ -1223,10 +1223,8 @@ private void FinishOperationSendPackets() private static readonly unsafe IOCompletionCallback s_completionPortCallback = delegate (uint errorCode, uint numBytes, NativeOverlapped* nativeOverlapped) { -#pragma warning disable CA1416 // https://github.com/dotnet/roslyn-analyzers/issues/4090 Debug.Assert(OperatingSystem.IsWindows()); var saeaBox = (StrongBox)(ThreadPoolBoundHandle.GetNativeOverlappedState(nativeOverlapped)!); -#pragma warning restore CA1416 // https://github.com/dotnet/roslyn-analyzers/issues/4090 Debug.Assert(saeaBox.Value != null); SocketAsyncEventArgs saea = saeaBox.Value; From 55d3daf0abd5e4cfe7d5196b087e40d930958a1d Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Wed, 9 Sep 2020 14:38:57 -0700 Subject: [PATCH 4/6] Resolving compilation errors on full framework --- .../Common/src/Interop/Windows/NCrypt/Interop.Properties.cs | 2 ++ .../src/EventLogSettings.cs | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Properties.cs b/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Properties.cs index eb81adcda802d1..edf53cd6d55dd0 100644 --- a/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Properties.cs +++ b/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Properties.cs @@ -56,7 +56,9 @@ internal static ErrorCode NCryptGetIntProperty(SafeNCryptHandle hObject, string { fixed (int* pResult = &result) { +#if NETSTANDARD || NETCOREAPP Debug.Assert(OperatingSystem.IsWindows()); +#endif errorCode = Interop.NCrypt.NCryptGetProperty( hObject, diff --git a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogSettings.cs b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogSettings.cs index ff82d41fd7dc67..93bf512625ee38 100644 --- a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogSettings.cs +++ b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogSettings.cs @@ -55,8 +55,9 @@ private IEventLog CreateDefaultEventLog() defaultEventId = 1000; } - +#if NETSTANDARD Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); +#endif return new WindowsEventLog(logName, machineName, sourceName) { DefaultEventId = defaultEventId }; } } From 86d1c74ce719678220e6b72e245f589e6bbb9b19 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Wed, 9 Sep 2020 18:36:24 -0700 Subject: [PATCH 5/6] Fixing two more tfm issues with the platform compat checks --- .../Common/src/Interop/Windows/NCrypt/Interop.Properties.cs | 2 +- .../src/EventLogLoggerProvider.cs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Properties.cs b/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Properties.cs index edf53cd6d55dd0..64acf11a952f68 100644 --- a/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Properties.cs +++ b/src/libraries/Common/src/Interop/Windows/NCrypt/Interop.Properties.cs @@ -57,7 +57,7 @@ internal static ErrorCode NCryptGetIntProperty(SafeNCryptHandle hObject, string fixed (int* pResult = &result) { #if NETSTANDARD || NETCOREAPP - Debug.Assert(OperatingSystem.IsWindows()); + Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); #endif errorCode = Interop.NCrypt.NCryptGetProperty( diff --git a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLoggerProvider.cs b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLoggerProvider.cs index d5470a8fe03f0b..6063eb177a7382 100644 --- a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLoggerProvider.cs +++ b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLoggerProvider.cs @@ -55,7 +55,9 @@ public void Dispose() { if (_settings.EventLog is WindowsEventLog windowsEventLog) { +#if NETSTANDARD Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); +#endif windowsEventLog.DiagnosticsEventLog.Dispose(); } } From 5760c4306a2ef0e434e2e66ce1ed6364b3d0de60 Mon Sep 17 00:00:00 2001 From: Tanner Gooding Date: Wed, 9 Sep 2020 21:22:48 -0700 Subject: [PATCH 6/6] Adjusting various platform compat checks to work with netstandard and netfx --- .../src/EventLogLogger.cs | 2 ++ .../System.Drawing.Common/src/System/Drawing/Icon.Windows.cs | 2 +- .../src/System/Drawing/Internal/SystemColorTracker.cs | 4 ++-- .../System.IO.Ports/src/System/IO/Ports/SerialPort.Win32.cs | 2 ++ .../src/Internal/Cryptography/Pal/Windows/PkcsPalWindows.cs | 4 ++-- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLogger.cs b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLogger.cs index 6f760ac45f9516..cd83dff8aae91d 100644 --- a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLogger.cs +++ b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLogger.cs @@ -167,7 +167,9 @@ private void WriteMessage(string message, EventLogEntryType eventLogEntryType, i private EventLogEntryType GetEventLogEntryType(LogLevel level) { +#if NETSTANDARD Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); +#endif switch (level) { diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs index fc9e32b06975df..dc25bdf08d11c6 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs @@ -657,7 +657,7 @@ public void Save(Stream outputStream) } finally { - Debug.Assert(OperatingSystem.IsWindows()); + Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); Marshal.ReleaseComObject(picture); } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Internal/SystemColorTracker.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Internal/SystemColorTracker.cs index 609a547be45e6e..9d2343d83b598a 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Internal/SystemColorTracker.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Internal/SystemColorTracker.cs @@ -39,7 +39,7 @@ internal static void Add(ISystemColorTracker obj) if (!addedTracker) { - Debug.Assert(OperatingSystem.IsWindows()); + Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); addedTracker = true; SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(OnUserPreferenceChanged); } @@ -132,7 +132,7 @@ private static void GarbageCollectList() private static void OnUserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e) { - Debug.Assert(OperatingSystem.IsWindows()); + Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); // Update pens and brushes if (e.Category == UserPreferenceCategory.Color) diff --git a/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.Win32.cs b/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.Win32.cs index fbc38aa3cb0cd4..ac930c78a1a1d5 100644 --- a/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.Win32.cs +++ b/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialPort.Win32.cs @@ -19,7 +19,9 @@ public static string[] GetPortNames() // // QueryDosDevice involves finding any ports that map to \Device\Serialx (call with null to get all, then iterate to get the actual device name) +#if NETSTANDARD Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); +#endif using (RegistryKey serialKey = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM")) { diff --git a/src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/PkcsPalWindows.cs b/src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/PkcsPalWindows.cs index 31847e718e85a0..19a47eec365000 100644 --- a/src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/PkcsPalWindows.cs +++ b/src/libraries/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/PkcsPalWindows.cs @@ -129,8 +129,8 @@ public sealed override byte[] GetSubjectKeyIdentifier(X509Certificate2 certifica if (keySpec == CryptKeySpec.CERT_NCRYPT_KEY_SPEC) { -#if NET5_0 - Debug.Assert(OperatingSystem.IsWindows()); +#if NETSTANDARD || NETCOREAPP + Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); #endif using (SafeNCryptKeyHandle keyHandle = new SafeNCryptKeyHandle(handle.DangerousGetHandle(), handle))