Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion eng/Analyzers.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>
<ItemGroup Condition="'$(EnableAnalyzers)' == 'true'">
<PackageReference Include="Microsoft.DotNet.CodeAnalysis" Version="$(MicrosoftDotNetCodeAnalysisVersion)" PrivateAssets="all" IsImplicitlyDefined="true" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.0-rc1.20413.9" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="5.0.0-rc2.20458.2" PrivateAssets="all" />
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.164" PrivateAssets="all" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion eng/CodeAnalysis.ruleset
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
<Rule Id="CA1308" Action="None" /> <!-- Normalize strings to uppercase -->
<Rule Id="CA1309" Action="None" /> <!-- Use ordinal stringcomparison -->
<Rule Id="CA1401" Action="Warning" /> <!-- P/Invokes should not be visible -->
<Rule Id="CA1416" Action="None" /> <!-- Validate platform compatibility; https://github.com/dotnet/runtime/issues/41354 -->
<Rule Id="CA1416" Action="Warning" /> <!-- Validate platform compatibility -->
<Rule Id="CA1417" Action="Warning" /> <!-- Do not use 'OutAttribute' on string parameters for P/Invokes -->
<Rule Id="CA1501" Action="None" /> <!-- Avoid excessive inheritance -->
<Rule Id="CA1502" Action="None" /> <!-- Avoid excessive complexity -->
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -53,6 +56,10 @@ internal static ErrorCode NCryptGetIntProperty(SafeNCryptHandle hObject, string
{
fixed (int* pResult = &result)
{
#if NETSTANDARD || NETCOREAPP
Debug.Assert(OperatingSystem.IsWindows());
#endif

errorCode = Interop.NCrypt.NCryptGetProperty(
hObject,
pszProperty,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ private void FreeNativeOverlapped()
{
unsafe
{
Debug.Assert(OperatingSystem.IsWindows());
Debug.Assert(_socketHandle != null, "_socketHandle is null.");

ThreadPoolBoundHandle? boundHandle = _socketHandle.IOCPBoundHandle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,13 +14,15 @@ 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();
}

private void CleanupInternal()
{
if (_windowsIdentity != null)
{
Debug.Assert(OperatingSystem.IsWindows());
_windowsIdentity.Dispose();
_windowsIdentity = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -166,6 +167,8 @@ private void WriteMessage(string message, EventLogEntryType eventLogEntryType, i

private EventLogEntryType GetEventLogEntryType(LogLevel level)
{
Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));

switch (level)
{
case LogLevel.Information:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,6 +55,7 @@ public void Dispose()
{
if (_settings.EventLog is WindowsEventLog windowsEventLog)
{
Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
windowsEventLog.DiagnosticsEventLog.Dispose();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 };
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461</TargetFrameworks>
<EnableDefaultItems>true</EnableDefaultItems>
<IncludePlatformAttributes>true</IncludePlatformAttributes>
</PropertyGroup>

<ItemGroup>
Expand All @@ -18,7 +19,7 @@
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Logging.Abstractions\src\Microsoft.Extensions.Logging.Abstractions.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Options\src\Microsoft.Extensions.Options.csproj" />
</ItemGroup>

<ItemGroup Condition="!$(TargetFramework.StartsWith('net4'))">
<ProjectReference Include="$(LibrariesProjectRoot)System.Diagnostics.EventLog\src\System.Diagnostics.EventLog.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@ public void Save(Stream outputStream)
}
finally
{
Debug.Assert(OperatingSystem.IsWindows());
Marshal.ReleaseComObject(picture);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ internal static void Add(ISystemColorTracker obj)

if (!addedTracker)
{
Debug.Assert(OperatingSystem.IsWindows());
addedTracker = true;
SystemEvents.UserPreferenceChanged += new UserPreferenceChangedEventHandler(OnUserPreferenceChanged);
}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using Microsoft.Win32;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace System.IO.Ports
{
Expand All @@ -17,6 +19,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)

Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));

using (RegistryKey serialKey = Registry.LocalMachine.OpenSubKey(@"HARDWARE\DEVICEMAP\SERIALCOMM"))
{
if (serialKey != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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");
Expand All @@ -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");
Expand Down Expand Up @@ -904,6 +907,7 @@ private void FreeOverlapped()
// any pinned buffers.
if (_preAllocatedOverlapped != null)
{
Debug.Assert(OperatingSystem.IsWindows());
_preAllocatedOverlapped.Dispose();
_preAllocatedOverlapped = null!;
}
Expand Down Expand Up @@ -1219,7 +1223,9 @@ private void FinishOperationSendPackets()

private static readonly unsafe IOCompletionCallback s_completionPortCallback = delegate (uint errorCode, uint numBytes, NativeOverlapped* nativeOverlapped)
{
var saeaBox = (StrongBox<SocketAsyncEventArgs>)ThreadPoolBoundHandle.GetNativeOverlappedState(nativeOverlapped)!;
Debug.Assert(OperatingSystem.IsWindows());
var saeaBox = (StrongBox<SocketAsyncEventArgs>)(ThreadPoolBoundHandle.GetNativeOverlappedState(nativeOverlapped)!);

Debug.Assert(saeaBox.Value != null);
SocketAsyncEventArgs saea = saeaBox.Value;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down