diff --git a/eng/Analyzers.props b/eng/Analyzers.props index ff442658c33187..8babf8c41f5f9b 100644 --- a/eng/Analyzers.props +++ b/eng/Analyzers.props @@ -6,7 +6,7 @@ - + diff --git a/eng/CodeAnalysis.ruleset b/eng/CodeAnalysis.ruleset index 3560577d8f3c2b..bf63f11bb05a17 100644 --- a/eng/CodeAnalysis.ruleset +++ b/eng/CodeAnalysis.ruleset @@ -64,7 +64,7 @@ - + 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..64acf11a952f68 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,10 @@ internal static ErrorCode NCryptGetIntProperty(SafeNCryptHandle hObject, string { fixed (int* pResult = &result) { +#if NETSTANDARD || NETCOREAPP + Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); +#endif + 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..cd83dff8aae91d 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 @@ -166,6 +167,10 @@ private void WriteMessage(string message, EventLogEntryType eventLogEntryType, i private EventLogEntryType GetEventLogEntryType(LogLevel level) { +#if NETSTANDARD + Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); +#endif + switch (level) { case LogLevel.Information: diff --git a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLoggerProvider.cs b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/EventLogLoggerProvider.cs index 4bc27bd6a15b83..6063eb177a7382 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,9 @@ public void Dispose() { if (_settings.EventLog is WindowsEventLog windowsEventLog) { +#if NETSTANDARD + Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); +#endif 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..93bf512625ee38 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,9 @@ private IEventLog CreateDefaultEventLog() defaultEventId = 1000; } +#if NETSTANDARD + Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); +#endif 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 0c72558ec88db1..b148714f55289e 100644 --- a/src/libraries/Microsoft.Extensions.Logging.EventLog/src/WindowsEventLog.cs +++ b/src/libraries/Microsoft.Extensions.Logging.EventLog/src/WindowsEventLog.cs @@ -4,9 +4,11 @@ using System; using System.Diagnostics; using System.Security; +using System.Runtime.Versioning; namespace Microsoft.Extensions.Logging.EventLog { + [SupportedOSPlatform("windows")] internal class WindowsEventLog : IEventLog { // https://msdn.microsoft.com/EN-US/library/windows/desktop/aa363679.aspx 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..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,6 +657,7 @@ public void Save(Stream outputStream) } finally { + 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 2f53b9f4095d45..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,6 +39,7 @@ internal static void Add(ISystemColorTracker obj) if (!addedTracker) { + Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); 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(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 aa799d677a211e..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 @@ -3,6 +3,8 @@ using Microsoft.Win32; using System.ComponentModel; +using System.Diagnostics; +using System.Runtime.InteropServices; namespace System.IO.Ports { @@ -17,6 +19,10 @@ 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")) { if (serialKey != null) 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..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 @@ -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,9 @@ private void FinishOperationSendPackets() private static readonly unsafe IOCompletionCallback s_completionPortCallback = delegate (uint errorCode, uint numBytes, NativeOverlapped* nativeOverlapped) { - var saeaBox = (StrongBox)ThreadPoolBoundHandle.GetNativeOverlappedState(nativeOverlapped)!; + Debug.Assert(OperatingSystem.IsWindows()); + var saeaBox = (StrongBox)(ThreadPoolBoundHandle.GetNativeOverlappedState(nativeOverlapped)!); + 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..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,6 +129,10 @@ public sealed override byte[] GetSubjectKeyIdentifier(X509Certificate2 certifica if (keySpec == CryptKeySpec.CERT_NCRYPT_KEY_SPEC) { +#if NETSTANDARD || NETCOREAPP + Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); +#endif + using (SafeNCryptKeyHandle keyHandle = new SafeNCryptKeyHandle(handle.DangerousGetHandle(), handle)) { CngKeyHandleOpenOptions options = CngKeyHandleOpenOptions.None;