diff --git a/eng/CodeAnalysis.ruleset b/eng/CodeAnalysis.ruleset
index 3560577d8f3c2..293d25a216160 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 79ac4f59950d9..eb81adcda802d 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 ef51b07b44d69..bf684547ac8ee 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 e7c57f9df35cc..3ff41453851d4 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 a8ee86b54282c..7e3998c6f5f9c 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 0c72558ec88db..70b2bc7972e6a 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 7e1834e3708f8..fc9e32b06975d 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 2f53b9f4095d4..609a547be45e6 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 aa799d677a211..e355ba53fd136 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 3b305d33d5609..d431c2447514d 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 29b3f7da3ea4e..ad59c2e65dbc2 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 6368666ec6bf4..0c4e2b1487a09 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 a720ce02fd72d..31847e718e85a 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;