Skip to content

Add analyzer+codefix for unsafe methods->RequiresUnsafe#125195

Closed
agocke wants to merge 3 commits intodotnet:mainfrom
agocke:add-unsafe-missing-requires-unsafe-analyzer
Closed

Add analyzer+codefix for unsafe methods->RequiresUnsafe#125195
agocke wants to merge 3 commits intodotnet:mainfrom
agocke:add-unsafe-missing-requires-unsafe-analyzer

Conversation

@agocke
Copy link
Member

@agocke agocke commented Mar 4, 2026

Adds an analyzer+codefix that warns on methods with an unsafe modifier, but are not marked [RequiresUnsafe].

Also adds [RequiresUnsafe] to all unsafe methods in System.Private.Corelib.

agocke and others added 2 commits March 3, 2026 17:04
Add a new DiagnosticAnalyzer (UnsafeMethodMissingRequiresUnsafeAnalyzer)
that warns when a method, constructor, or local function has the 'unsafe'
modifier but is not annotated with [RequiresUnsafe].

Add a matching CodeFixProvider that adds the [RequiresUnsafe] attribute
to the flagged declaration.

Both are #if DEBUG guarded and enabled via the existing
EnableUnsafeAnalyzer MSBuild property.

New diagnostic: IL5004 (UnsafeMethodMissingRequiresUnsafe)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 4, 2026 23:22
@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Mar 4, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a DEBUG-only Roslyn analyzer + code fix in the ILLink tooling to enforce that unsafe-modified methods are annotated with [RequiresUnsafe], and then applies [RequiresUnsafe] broadly across existing unsafe APIs (primarily in System.Private.CoreLib and related shared sources).

Changes:

  • Introduces a new diagnostic ID + resources and a new analyzer for unsafe methods missing [RequiresUnsafe].
  • Updates RequiresUnsafeAnalyzer to use a shared attribute-name constant.
  • Adds [RequiresUnsafe] annotations across many unsafe members throughout CoreLib and shared interop / common sources.

Reviewed changes

Copilot reviewed 281 out of 283 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/tools/illink/src/ILLink.Shared/SharedStrings.resx Adds title/message strings for the new diagnostic.
src/tools/illink/src/ILLink.Shared/DiagnosticId.cs Adds UnsafeMethodMissingRequiresUnsafe = 5004.
src/tools/illink/src/ILLink.RoslynAnalyzer/UnsafeMethodMissingRequiresUnsafeAnalyzer.cs New analyzer that reports when unsafe methods/ctors/local functions lack [RequiresUnsafe] (DEBUG-only).
src/tools/illink/src/ILLink.RoslynAnalyzer/RequiresUnsafeAnalyzer.cs Refactors attribute-name constant used by unsafe analyzers.
src/tools/illink/src/ILLink.CodeFix/Resources.resx Adds code fix title for adding [RequiresUnsafe] to unsafe methods.
src/libraries/System.Private.CoreLib/src/System/UIntPtr.cs Adds [RequiresUnsafe] to unsafe ctor and pointer conversion.
src/libraries/System.Private.CoreLib/src/System/UInt128.cs Adds [RequiresUnsafe] on an unsafe local helper.
src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.FullGlobalizationData.cs Adds using System.Diagnostics.CodeAnalysis; and [RequiresUnsafe] to unsafe helpers.
src/libraries/System.Private.CoreLib/src/System/TimeZoneInfo.FullGlobalizationData.Unix.cs Adds using + [RequiresUnsafe] to unsafe ICU helpers.
src/libraries/System.Private.CoreLib/src/System/Threading/Win32ThreadPoolNativeOverlapped.cs Adds using + [RequiresUnsafe] to unsafe allocation paths.
src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolBoundHandle.Unix.cs Adds using + [RequiresUnsafe] to unsafe APIs.
src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolBoundHandle.Portable.cs Adds using + [RequiresUnsafe] to unsafe portable core implementations.
src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPool.Unix.cs Adds using + [RequiresUnsafe] to unsafe thread pool APIs.
src/libraries/System.Private.CoreLib/src/System/Threading/Thread.cs Adds [RequiresUnsafe] to an unsafe stub method.
src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs Adds [RequiresUnsafe] to an unsafe generic API.
src/libraries/System.Private.CoreLib/src/System/Threading/PreAllocatedOverlapped.Unix.cs Adds using + [RequiresUnsafe] to unsafe ctor and final release path.
src/libraries/System.Private.CoreLib/src/System/Threading/PreAllocatedOverlapped.Portable.cs Adds using + [RequiresUnsafe] to unsafe helper.
src/libraries/System.Private.CoreLib/src/System/Threading/NamedMutex.Unix.cs Adds using + [RequiresUnsafe] to unsafe shared-memory helpers.
src/libraries/System.Private.CoreLib/src/System/Threading/Interlocked.cs Adds [RequiresUnsafe] to several unsafe generic overloads.
src/libraries/System.Private.CoreLib/src/System/Threading/Condition.cs Adds using + [RequiresUnsafe] to unsafe debug assertions.
src/libraries/System.Private.CoreLib/src/System/Threading/Backoff.cs Adds [RequiresUnsafe] to an unsafe helper.
src/libraries/System.Private.CoreLib/src/System/Text/Unicode/Utf8Utility.cs Adds using + [RequiresUnsafe] to an unsafe UTF-8 helper.
src/libraries/System.Private.CoreLib/src/System/Text/Unicode/Utf8.cs Adds using + [RequiresUnsafe] to unsafe transcode methods.
src/libraries/System.Private.CoreLib/src/System/Text/UTF8Encoding.Sealed.cs Adds using + [RequiresUnsafe] to unsafe small-input fast paths.
src/libraries/System.Private.CoreLib/src/System/Text/StringBuilder.cs Adds [RequiresUnsafe] to unsafe pointer overload.
src/libraries/System.Private.CoreLib/src/System/Text/Latin1Utility.cs Adds using + [RequiresUnsafe] to unsafe Latin-1 scanners.
src/libraries/System.Private.CoreLib/src/System/Text/Latin1Encoding.cs Adds using + [RequiresUnsafe] to unsafe overrides.
src/libraries/System.Private.CoreLib/src/System/Text/Encoding.Internal.cs Adds using + [RequiresUnsafe] to unsafe encoding internals.
src/libraries/System.Private.CoreLib/src/System/Text/EncoderReplacementFallback.cs Adds [RequiresUnsafe] to unsafe reset.
src/libraries/System.Private.CoreLib/src/System/Text/EncoderNLS.cs Adds using + [RequiresUnsafe] to unsafe overloads.
src/libraries/System.Private.CoreLib/src/System/Text/EncoderLatin1BestFitFallback.cs Adds using + [RequiresUnsafe] to unsafe reset.
src/libraries/System.Private.CoreLib/src/System/Text/EncoderFallback.cs Adds [RequiresUnsafe] to unsafe internal fallback helpers.
src/libraries/System.Private.CoreLib/src/System/Text/Encoder.cs Adds using + [RequiresUnsafe] to unsafe entry points.
src/libraries/System.Private.CoreLib/src/System/Text/DecoderReplacementFallback.cs Adds [RequiresUnsafe] to unsafe reset/fallback.
src/libraries/System.Private.CoreLib/src/System/Text/DecoderNLS.cs Adds using + [RequiresUnsafe] to unsafe overloads.
src/libraries/System.Private.CoreLib/src/System/Text/DecoderFallback.cs Adds [RequiresUnsafe] to unsafe internal fallback helpers.
src/libraries/System.Private.CoreLib/src/System/Text/Decoder.cs Adds using + [RequiresUnsafe] to unsafe entry points.
src/libraries/System.Private.CoreLib/src/System/Text/Ascii.cs Adds using + [RequiresUnsafe] to unsafe core helper.
src/libraries/System.Private.CoreLib/src/System/Text/Ascii.Utility.cs Adds using + [RequiresUnsafe] to unsafe ASCII scanning helpers.
src/libraries/System.Private.CoreLib/src/System/Text/Ascii.Transcoding.cs Adds using + [RequiresUnsafe] to unsafe transcode APIs.
src/libraries/System.Private.CoreLib/src/System/Text/Ascii.CaseConversion.cs Adds using + [RequiresUnsafe] to unsafe casing helpers.
src/libraries/System.Private.CoreLib/src/System/Text/ASCIIEncoding.cs Adds using + [RequiresUnsafe] to unsafe overrides.
src/libraries/System.Private.CoreLib/src/System/String.Comparison.cs Adds [RequiresUnsafe] to several unsafe hashing helpers.
src/libraries/System.Private.CoreLib/src/System/StartupHookProvider.cs Adds [RequiresUnsafe] to unsafe startup hook call paths.
src/libraries/System.Private.CoreLib/src/System/SpanHelpers.cs Adds using + [RequiresUnsafe] to unsafe helpers.
src/libraries/System.Private.CoreLib/src/System/SpanHelpers.T.cs Adds using + [RequiresUnsafe] to unsafe generic helpers.
src/libraries/System.Private.CoreLib/src/System/SpanHelpers.Packed.cs Adds using + [RequiresUnsafe] to unsafe packed helpers.
src/libraries/System.Private.CoreLib/src/System/SpanHelpers.Char.cs Adds using + [RequiresUnsafe] to unsafe char helpers.
src/libraries/System.Private.CoreLib/src/System/SpanHelpers.ByteMemOps.cs Adds using + [RequiresUnsafe] to unsafe memmove paths.
src/libraries/System.Private.CoreLib/src/System/SpanHelpers.Byte.cs Adds [RequiresUnsafe] to multiple unsafe byte helpers.
src/libraries/System.Private.CoreLib/src/System/Span.cs Adds using + [RequiresUnsafe] to pointer ctor and unsafe Clear().
src/libraries/System.Private.CoreLib/src/System/Security/SecureString.cs Adds using + [RequiresUnsafe] to unsafe ctor and span acquisition.
src/libraries/System.Private.CoreLib/src/System/SearchValues/ProbabilisticMap.cs Adds using + [RequiresUnsafe] to unsafe probabilistic helpers.
src/libraries/System.Private.CoreLib/src/System/SearchValues/IndexOfAnyAsciiSearcher.cs Adds using + [RequiresUnsafe] to unsafe bitmap helpers.
src/libraries/System.Private.CoreLib/src/System/SearchValues/Any5SearchValues.cs Adds using + [RequiresUnsafe] to unsafe ctor and method.
src/libraries/System.Private.CoreLib/src/System/SearchValues/Any4SearchValues.cs Adds using + [RequiresUnsafe] to unsafe ctor and method.
src/libraries/System.Private.CoreLib/src/System/SearchValues/Any3SearchValues.cs Adds using + [RequiresUnsafe] to unsafe ctor and method.
src/libraries/System.Private.CoreLib/src/System/SearchValues/Any2SearchValues.cs Adds using + [RequiresUnsafe] to unsafe ctor and method.
src/libraries/System.Private.CoreLib/src/System/SearchValues/Any1SearchValues.cs Adds using + [RequiresUnsafe] to unsafe ctor and method.
src/libraries/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.cs Adds [RequiresUnsafe] to unsafe unmanaged entry points.
src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/X86/X86Base.cs Adds [RequiresUnsafe] to unsafe CPUID helpers.
src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/X86/Sse3.cs Adds using + [RequiresUnsafe] to unsafe load helpers.
src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/X86/Bmi2.cs Adds using + [RequiresUnsafe] to unsafe MULX helpers.
src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/X86/Avx512DQ.cs Adds [RequiresUnsafe] to several unsafe broadcast helpers.
src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/X86/Avx10v2.cs Adds [RequiresUnsafe] to unsafe store helpers.
src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector64.cs Adds using + [RequiresUnsafe] to pointer load APIs.
src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector512.cs Adds using + [RequiresUnsafe] to pointer load APIs.
src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector256.cs Adds using + [RequiresUnsafe] to pointer load APIs.
src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector128.cs Adds using + [RequiresUnsafe] to pointer load APIs.
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/TypeMapLazyDictionary.cs Adds [RequiresUnsafe] to unsafe QCall/callback paths.
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ReferenceTrackerHost.cs Adds using + [RequiresUnsafe] to unsafe host APIs.
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/PosixSignalRegistration.Unix.cs Adds using + [RequiresUnsafe] to unsafe initialization.
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/PinnedGCHandle.T.cs Adds [RequiresUnsafe] to unsafe address accessor.
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ObjectiveC/ObjectiveCMarshal.PlatformNotSupported.cs Adds using + [RequiresUnsafe] to unsafe API signature.
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeLibrary.cs Adds using + [RequiresUnsafe] to unsafe symbol lookup.
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/NativeLibrary.Unix.cs Adds using + [RequiresUnsafe] to unsafe symbol lookup.
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshalling/ComVariant.cs Adds [RequiresUnsafe] to several unsafe APIs.
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.Unix.cs Adds using + [RequiresUnsafe] to unsafe ANSI conversion helpers.
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/GCHandleExtensions.cs Adds using + [RequiresUnsafe] to unsafe address accessors.
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/GCHandle.cs Adds [RequiresUnsafe] to unsafe handle validation helper.
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/ComAwareWeakReference.ComWrappers.cs Adds using + [RequiresUnsafe] to unsafe callback init.
src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.cs Adds using + [RequiresUnsafe] to an unsafe intrinsic.
src/libraries/System.Private.CoreLib/src/System/Resources/ResourceReader.cs Adds [RequiresUnsafe] to multiple unsafe internal helpers.
src/libraries/System.Private.CoreLib/src/System/Resources/FastResourceComparer.cs Adds [RequiresUnsafe] to unsafe compare helpers.
src/libraries/System.Private.CoreLib/src/System/Reflection/MethodInvoker.cs Adds using + [RequiresUnsafe] to unsafe invoke paths.
src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.cs Adds [RequiresUnsafe] to multiple unsafe invoke paths.
src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.Constructor.cs Adds using + [RequiresUnsafe] to unsafe ctor-invoke paths.
src/libraries/System.Private.CoreLib/src/System/Reflection/ConstructorInvoker.cs Adds using + [RequiresUnsafe] to unsafe invoke paths.
src/libraries/System.Private.CoreLib/src/System/ReadOnlySpan.cs Adds using + [RequiresUnsafe] to pointer ctor.
src/libraries/System.Private.CoreLib/src/System/ReadOnlyMemory.cs Adds [RequiresUnsafe] to unsafe pinning.
src/libraries/System.Private.CoreLib/src/System/Random.Xoshiro256StarStarImpl.cs Adds using + [RequiresUnsafe] to unsafe RNG impl.
src/libraries/System.Private.CoreLib/src/System/Numerics/Vector4.cs Adds [RequiresUnsafe] to pointer load APIs.
src/libraries/System.Private.CoreLib/src/System/Numerics/Vector3.cs Adds [RequiresUnsafe] to pointer load APIs.
src/libraries/System.Private.CoreLib/src/System/Numerics/Vector2.cs Adds [RequiresUnsafe] to pointer load APIs.
src/libraries/System.Private.CoreLib/src/System/Numerics/Vector.cs Adds using + [RequiresUnsafe] to pointer load APIs.
src/libraries/System.Private.CoreLib/src/System/Numerics/Matrix4x4.Impl.cs Adds [RequiresUnsafe] to an unsafe helper.
src/libraries/System.Private.CoreLib/src/System/Numerics/BFloat16.cs Adds [RequiresUnsafe] to unsafe rounding helpers.
src/libraries/System.Private.CoreLib/src/System/Number.Parsing.cs Adds [RequiresUnsafe] to unsafe number parsing helpers.
src/libraries/System.Private.CoreLib/src/System/Number.Formatting.cs Adds using + [RequiresUnsafe] to unsafe format helpers.
src/libraries/System.Private.CoreLib/src/System/Memory.cs Adds [RequiresUnsafe] to unsafe pinning.
src/libraries/System.Private.CoreLib/src/System/MathF.cs Adds using + [RequiresUnsafe] to unsafe truncate helper.
src/libraries/System.Private.CoreLib/src/System/Math.cs Adds [RequiresUnsafe] to unsafe BigMul/Truncate helpers.
src/libraries/System.Private.CoreLib/src/System/Marvin.cs Adds using + [RequiresUnsafe] to unsafe seed generation.
src/libraries/System.Private.CoreLib/src/System/IntPtr.cs Adds [RequiresUnsafe] to pointer ctor and conversion.
src/libraries/System.Private.CoreLib/src/System/IO/UnmanagedMemoryStream.cs Adds using + [RequiresUnsafe] to pointer-based ctors.
src/libraries/System.Private.CoreLib/src/System/IO/StreamWriter.cs Adds [RequiresUnsafe] to an unsafe span writer.
src/libraries/System.Private.CoreLib/src/System/IO/RandomAccess.Unix.cs Adds using + [RequiresUnsafe] to unsafe file ops.
src/libraries/System.Private.CoreLib/src/System/IO/PersistedFiles.Unix.cs Adds [RequiresUnsafe] to unsafe passwd helper.
src/libraries/System.Private.CoreLib/src/System/IO/Path.cs Adds [RequiresUnsafe] to unsafe random filename helpers.
src/libraries/System.Private.CoreLib/src/System/IO/Path.Unix.cs Adds [RequiresUnsafe] to unsafe temp filename helper.
src/libraries/System.Private.CoreLib/src/System/IO/FileSystemInfo.Unix.cs Adds using + [RequiresUnsafe] to unsafe init helper.
src/libraries/System.Private.CoreLib/src/System/IO/FileSystem.Unix.cs Adds using + [RequiresUnsafe] to unsafe timestamp setters.
src/libraries/System.Private.CoreLib/src/System/IO/FileStatus.Unix.cs Adds using + [RequiresUnsafe] to unsafe timestamp core helper.
src/libraries/System.Private.CoreLib/src/System/IO/Enumeration/FileSystemEnumerator.Unix.cs Adds using + [RequiresUnsafe] to unsafe enumeration core.
src/libraries/System.Private.CoreLib/src/System/IO/Directory.Unix.cs Adds using + [RequiresUnsafe] to unsafe temp dir helper.
src/libraries/System.Private.CoreLib/src/System/IO/BinaryReader.cs Adds using + [RequiresUnsafe] to unsafe reading overloads.
src/libraries/System.Private.CoreLib/src/System/HashCode.cs Adds using + [RequiresUnsafe] to unsafe seed generation.
src/libraries/System.Private.CoreLib/src/System/Guid.cs Adds [RequiresUnsafe] to unsafe formatting helpers.
src/libraries/System.Private.CoreLib/src/System/Guid.Unix.cs Adds using + [RequiresUnsafe] to unsafe guid generation.
src/libraries/System.Private.CoreLib/src/System/Globalization/TimeSpanFormat.cs Adds using + [RequiresUnsafe] to unsafe formatter.
src/libraries/System.Private.CoreLib/src/System/Globalization/TextInfo.cs Adds using + [RequiresUnsafe] to multiple unsafe casing helpers.
src/libraries/System.Private.CoreLib/src/System/Globalization/TextInfo.Nls.cs Adds using + [RequiresUnsafe] to unsafe NLS casing helper.
src/libraries/System.Private.CoreLib/src/System/Globalization/TextInfo.Icu.cs Adds using + [RequiresUnsafe] to unsafe ICU casing helper.
src/libraries/System.Private.CoreLib/src/System/Globalization/OrdinalCasing.Icu.cs Adds using + [RequiresUnsafe] to unsafe search helpers.
src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.Nls.cs Adds using + [RequiresUnsafe] to unsafe NLS normalization helpers.
src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.Icu.cs Adds using + [RequiresUnsafe] to unsafe ICU normalization helpers.
src/libraries/System.Private.CoreLib/src/System/Globalization/InvariantModeCasing.cs Adds using + [RequiresUnsafe] to unsafe casing helpers.
src/libraries/System.Private.CoreLib/src/System/Globalization/DateTimeFormat.cs Adds using + [RequiresUnsafe] to unsafe formatting helpers.
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.Unix.cs Adds using + [RequiresUnsafe] to unsafe region helper.
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.Nls.cs Adds [RequiresUnsafe] to multiple unsafe NLS helpers/callbacks.
src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.Icu.cs Adds [RequiresUnsafe] to multiple unsafe ICU helpers.
src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Utf8.cs Adds using + [RequiresUnsafe] to unsafe UTF-8 compare helper.
src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Nls.cs Adds using + [RequiresUnsafe] to unsafe NLS compare helpers.
src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.Icu.cs Adds using + [RequiresUnsafe] to unsafe ICU compare helpers.
src/libraries/System.Private.CoreLib/src/System/Globalization/CalendarData.Nls.cs Adds using + [RequiresUnsafe] to unsafe NLS helpers/callback.
src/libraries/System.Private.CoreLib/src/System/Globalization/CalendarData.Icu.cs Adds using + [RequiresUnsafe] to unsafe ICU helpers.
src/libraries/System.Private.CoreLib/src/System/Environment.Variables.Windows.cs Adds using + [RequiresUnsafe] to unsafe env var enumeration.
src/libraries/System.Private.CoreLib/src/System/Enum.EnumInfo.cs Adds using + [RequiresUnsafe] to unsafe cloning helper.
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/XplatEventLogger.cs Adds using + [RequiresUnsafe] to unsafe QCall helpers.
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/TraceLogging/SimpleTypeInfos.cs Adds [RequiresUnsafe] to an unsafe helper.
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/NativeRuntimeEventSource.cs Adds using + [RequiresUnsafe] to unsafe event processing.
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/NativeRuntimeEventSource.Threading.NativeSinks.cs Adds [RequiresUnsafe] to unsafe IO event helpers.
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/FrameworkEventSource.cs Adds using + [RequiresUnsafe] to unsafe WriteEvent helpers.
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventProvider.cs Adds using + [RequiresUnsafe] to unsafe ETW encoding/write paths.
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventPipeMetadataGenerator.cs Adds using + [RequiresUnsafe] to unsafe metadata generation/write helpers.
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventPipeEventProvider.cs Adds using + [RequiresUnsafe] to unsafe callback/enable paths.
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventPipeEventDispatcher.cs Adds using + [RequiresUnsafe] to unsafe dispatch loop.
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventPipe.cs Adds using + [RequiresUnsafe] to unsafe enable path.
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventPipe.Internal.cs Adds using + [RequiresUnsafe] to unsafe QCall P/Invokes.
src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/ActivityTracker.cs Adds using + [RequiresUnsafe] to unsafe GUID path helpers.
src/libraries/System.Private.CoreLib/src/System/Decimal.DecCalc.cs Adds using + [RequiresUnsafe] to unsafe arithmetic helpers.
src/libraries/System.Private.CoreLib/src/System/Collections/Generic/RandomizedStringEqualityComparer.cs Adds using + [RequiresUnsafe] to unsafe ctor.
src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs Adds using + [RequiresUnsafe] to unsafe sort helper.
src/libraries/System.Private.CoreLib/src/System/Collections/BitArray.cs Adds using + [RequiresUnsafe] to an unsafe override.
src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Parser/Utf8Parser.Float.cs Adds using + [RequiresUnsafe] to unsafe parse entry points.
src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Utf8Parser/Utf8Parser.Decimal.cs Adds using + [RequiresUnsafe] to unsafe parse entry point.
src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Url/Base64UrlEncoder.cs Adds using + [RequiresUnsafe] to unsafe encoder internals.
src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Url/Base64UrlDecoder.cs Adds using + [RequiresUnsafe] to unsafe vector-load helpers.
src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64EncoderHelper.cs Adds using + [RequiresUnsafe] to unsafe encode helpers.
src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64DecoderHelper.cs Adds using + [RequiresUnsafe] to unsafe decode helpers.
src/libraries/System.Private.CoreLib/src/System/Buffers/ArrayPoolEventSource.cs Adds [RequiresUnsafe] to unsafe ETW event writers.
src/libraries/System.Private.CoreLib/src/System/Buffer.cs Adds using + [RequiresUnsafe] to unsafe MemoryCopy APIs.
src/libraries/System.Private.CoreLib/src/System/BitConverter.cs Adds using + [RequiresUnsafe] to unsafe BFloat16 helpers.
src/libraries/System.Private.CoreLib/src/System/ArgumentNullException.cs Adds [RequiresUnsafe] to pointer overload.
src/libraries/System.Private.CoreLib/src/System/AppContext.cs Adds [RequiresUnsafe] to unsafe unmanaged entry point.
src/libraries/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComponentActivator.cs Adds [RequiresUnsafe] to unsafe unmanaged entry points.
src/libraries/System.Private.CoreLib/src/Internal/Console.Unix.cs Adds using + [RequiresUnsafe] to unsafe console writers.
src/libraries/Common/src/System/Number.Parsing.Common.cs Adds using + [RequiresUnsafe] to shared unsafe parsing helpers.
src/libraries/Common/src/System/Number.Formatting.Common.cs Adds using + [RequiresUnsafe] to shared unsafe formatting helpers.
src/libraries/Common/src/System/IO/StringParser.cs Adds using + [RequiresUnsafe] to unsafe parsing methods.
src/libraries/Common/src/Interop/Windows/Normaliz/Interop.Normalization.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signatures.
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.Globalization.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signatures.
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetEnvironmentStrings.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signature.
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FreeEnvironmentStrings.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signature.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.Write.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signatures.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.UTimensat.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signatures.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.Threading.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signatures.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.ReadDir.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signature.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.Read.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signature.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.PosixSignal.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signature.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.PWriteV.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signature.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.PWrite.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signature.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.PReadV.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signature.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.PRead.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signature.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.cs Adds using + [RequiresUnsafe] to unsafe callback interop.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.MksTemps.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signature.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.MkdTemp.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signature.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.Log.cs Adds using + [RequiresUnsafe] to unsafe P/Invoke signatures.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.IsMemberOfGroup.cs Adds using + [RequiresUnsafe] to unsafe helpers/PInvoke.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetRandomBytes.cs Adds using + [RequiresUnsafe] to unsafe random APIs.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetPwUid.cs Adds using + [RequiresUnsafe] to unsafe passwd helpers.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetHostName.cs Adds using + [RequiresUnsafe] to unsafe hostname helpers.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetEnviron.cs Adds using + [RequiresUnsafe] to unsafe env interop.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetEnv.cs Adds using + [RequiresUnsafe] to unsafe env interop.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.GetCwd.cs Adds using + [RequiresUnsafe] to unsafe cwd helper.
src/libraries/Common/src/Interop/Unix/System.Native/Interop.Futex.cs Adds using + [RequiresUnsafe] to unsafe futex P/Invokes.
src/libraries/Common/src/Interop/Unix/Interop.Errors.cs Adds using + [RequiresUnsafe] to unsafe strerror helper/PInvoke.
src/libraries/Common/src/Interop/Interop.TimeZoneInfo.cs Adds using + [RequiresUnsafe] to unsafe globalization P/Invokes.
src/libraries/Common/src/Interop/Interop.Normalization.cs Adds using + [RequiresUnsafe] to unsafe globalization P/Invokes.
src/libraries/Common/src/Interop/Interop.Locale.cs Adds using + [RequiresUnsafe] to unsafe globalization P/Invokes.
src/libraries/Common/src/Interop/Interop.Collation.cs Adds using + [RequiresUnsafe] to unsafe globalization P/Invokes.
src/libraries/Common/src/Interop/Interop.Casing.cs Adds using + [RequiresUnsafe] to unsafe globalization P/Invokes.
src/libraries/Common/src/Interop/Interop.Calendar.cs Adds using + [RequiresUnsafe] to unsafe globalization P/Invokes/helpers.
src/coreclr/System.Private.CoreLib/src/System/ValueType.cs Adds using + [RequiresUnsafe] to unsafe overrides and QCalls.
src/coreclr/System.Private.CoreLib/src/System/TypedReference.CoreCLR.cs Adds [RequiresUnsafe] to unsafe helper.
src/coreclr/System.Private.CoreLib/src/System/Threading/Thread.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe start/QCall.
src/coreclr/System.Private.CoreLib/src/System/Threading/ObjectHeader.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe header helpers.
src/coreclr/System.Private.CoreLib/src/System/Threading/Interlocked.CoreCLR.cs Adds [RequiresUnsafe] to unsafe intrinsics/internalcalls.
src/coreclr/System.Private.CoreLib/src/System/Text/StringBuilder.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe buffer replacement APIs.
src/coreclr/System.Private.CoreLib/src/System/String.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe fast allocation.
src/coreclr/System.Private.CoreLib/src/System/StartupHookProvider.CoreCLR.cs Adds [RequiresUnsafe] to unsafe startup entry.
src/coreclr/System.Private.CoreLib/src/System/Runtime/Loader/AssemblyLoadContext.CoreCLR.cs Adds [RequiresUnsafe] to unsafe internal load.
src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/Java/JavaMarshal.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe init/handles.
src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/ComWrappers.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe impl helpers/QCall.
src/coreclr/System.Private.CoreLib/src/System/Runtime/ExceptionServices/InternalCalls.cs Adds using + [RequiresUnsafe] to unsafe QCall declarations.
src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/VirtualDispatchHelpers.cs Adds using + [RequiresUnsafe] to unsafe QCall.
src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/StaticsHelpers.cs Adds using + [RequiresUnsafe] to unsafe dynamic address helpers.
src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/AsyncHelpers.CoreCLR.cs Adds [RequiresUnsafe] to multiple unsafe helpers/QCalls.
src/coreclr/System.Private.CoreLib/src/System/Reflection/TypeNameResolver.CoreCLR.cs Adds [RequiresUnsafe] to unsafe VM helper.
src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeAssembly.cs Adds [RequiresUnsafe] to multiple unsafe resource/load paths.
src/coreclr/System.Private.CoreLib/src/System/Reflection/MethodInvoker.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe ctors.
src/coreclr/System.Private.CoreLib/src/System/Reflection/MethodBaseInvoker.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe ctors.
src/coreclr/System.Private.CoreLib/src/System/Reflection/Metadata/MetadataUpdater.cs Adds [RequiresUnsafe] to unsafe QCalls.
src/coreclr/System.Private.CoreLib/src/System/Reflection/Metadata/AssemblyExtensions.cs Adds using + [RequiresUnsafe] to unsafe raw metadata access.
src/coreclr/System.Private.CoreLib/src/System/Reflection/MdImport.cs Adds using + [RequiresUnsafe] to unsafe marshal helpers/QCall.
src/coreclr/System.Private.CoreLib/src/System/Reflection/MdConstant.cs Adds using + [RequiresUnsafe] to unsafe constant reader.
src/coreclr/System.Private.CoreLib/src/System/Reflection/LoaderAllocator.cs Adds using + [RequiresUnsafe] to unsafe unmanaged entry point.
src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SignatureHelper.cs Adds [RequiresUnsafe] to unsafe signature helper.
src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeTypeBuilder.cs Adds [RequiresUnsafe] to unsafe constant setting paths.
src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/RuntimeAssemblyBuilder.cs Adds [RequiresUnsafe] to unsafe QCall + helper.
src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/DynamicILGenerator.cs Adds using + [RequiresUnsafe] to unsafe IL emit helpers.
src/coreclr/System.Private.CoreLib/src/System/Reflection/ConstructorInvoker.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe invoke path.
src/coreclr/System.Private.CoreLib/src/System/Reflection/AssemblyName.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe parsing helpers.
src/coreclr/System.Private.CoreLib/src/System/Object.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe intrinsics.
src/coreclr/System.Private.CoreLib/src/System/MulticastDelegate.CoreCLR.cs Adds [RequiresUnsafe] to unsafe allocation helper.
src/coreclr/System.Private.CoreLib/src/System/MathF.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe helpers/internalcalls.
src/coreclr/System.Private.CoreLib/src/System/Math.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe helpers/internalcalls.
src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe finalizer helpers/QCall.
src/coreclr/System.Private.CoreLib/src/System/Environment.CoreCLR.cs Adds [RequiresUnsafe] to unsafe argv processing.
src/coreclr/System.Private.CoreLib/src/System/Enum.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe helpers.
src/coreclr/System.Private.CoreLib/src/System/Delegate.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe internalcalls/QCalls.
src/coreclr/System.Private.CoreLib/src/System/Buffer.CoreCLR.cs Adds using + [RequiresUnsafe] to unsafe memcpy helpers.
src/coreclr/System.Private.CoreLib/src/System/ArgIterator.cs Adds using + [RequiresUnsafe] to unsafe APIs (PNSE).
src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/InMemoryAssemblyLoader.PlatformNotSupported.cs Adds using + [RequiresUnsafe] to unsafe PNSE stub.
src/coreclr/System.Private.CoreLib/src/Internal/Runtime/InteropServices/ComActivator.PlatformNotSupported.cs Adds using + [RequiresUnsafe] to unsafe unmanaged entry points.

Comment on lines +14 to 16
[RequiresUnsafe]
private static unsafe bool TryParseNumber<TChar>(scoped ref TChar* str, TChar* strEnd, NumberStyles styles, ref NumberBuffer number, NumberFormatInfo info)
where TChar : unmanaged, IUtfChar<TChar>
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[RequiresUnsafe] is applied in a src/libraries/Common/src/... shared source file. RequiresUnsafeAttribute is currently defined as an internal type in System.Private.CoreLib, so any other assembly that compiles this Common file (e.g., System.Runtime.Numerics links it) will fail to compile because it can't reference that internal attribute. Consider moving RequiresUnsafeAttribute into src/libraries/Common/src/System/Diagnostics/CodeAnalysis/ and linking it into all consuming projects (or otherwise conditionally compiling these annotations only for System.Private.CoreLib).

Copilot uses AI. Check for mistakes.
Comment on lines 21 to 24
[LibraryImport(Libraries.SystemNative, EntryPoint = "SystemNative_Read", SetLastError = true)]
[RequiresUnsafe]
internal static unsafe partial int Read(SafeHandle fd, byte* buffer, int count);
}
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[RequiresUnsafe] is added to a Common Interop source file which is typically compiled into multiple assemblies. Since RequiresUnsafeAttribute is internal to System.Private.CoreLib today, this will break compilation for other assemblies that include this file. Either make the attribute available to all consuming projects (e.g., move it to Common and link it) or guard these annotations so they only compile where the attribute is defined.

Copilot uses AI. Check for mistakes.
Comment on lines +14 to +26
[DiagnosticAnalyzer (LanguageNames.CSharp)]
public sealed class UnsafeMethodMissingRequiresUnsafeAnalyzer : DiagnosticAnalyzer
{
private static readonly DiagnosticDescriptor s_rule = DiagnosticDescriptors.GetDiagnosticDescriptor (DiagnosticId.UnsafeMethodMissingRequiresUnsafe);

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create (s_rule);

public override void Initialize (AnalysisContext context)
{
context.EnableConcurrentExecution ();
context.ConfigureGeneratedCodeAnalysis (GeneratedCodeAnalysisFlags.None);
context.RegisterCompilationStartAction (context => {
if (!context.Options.IsMSBuildPropertyValueTrue (MSBuildPropertyOptionNames.EnableUnsafeAnalyzer))
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new analyzer file uses inconsistent spacing in attributes and method invocations (e.g., [DiagnosticAnalyzer (LanguageNames.CSharp)], EnableConcurrentExecution ()) compared to the existing analyzers in this project. This makes the file stand out and may conflict with repo formatting conventions; please align formatting with the surrounding ILLink analyzers.

Copilot uses AI. Check for mistakes.
@agocke
Copy link
Member Author

agocke commented Mar 5, 2026

Closing in favor of #125196

@agocke agocke closed this Mar 5, 2026
@agocke agocke deleted the add-unsafe-missing-requires-unsafe-analyzer branch March 5, 2026 22:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Runtime.InteropServices linkable-framework Issues associated with delivering a linker friendly framework

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

2 participants