Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
003645d
Initial plan
Copilot Mar 4, 2026
baba7fd
Add analyzer and code fixer for unsafe methods missing [RequiresUnsafe]
agocke Mar 4, 2026
315b2b0
Change analyzer/codefix implementation
agocke Mar 5, 2026
ce61325
Mark methods with pointer types as
agocke Mar 5, 2026
a5aa1e8
Merge remote-tracking branch 'upstream/main' into copilot/duplicate-p…
agocke Mar 6, 2026
851d167
Fix lambda scoping, field declarations, and shared file issues
agocke Mar 6, 2026
c4b5726
Set IL5004 diagnostic severity to Info
agocke Mar 6, 2026
a6490f4
Remove [RequiresUnsafe] from AsPointer
agocke Mar 6, 2026
77fc1e7
Merge remote-tracking branch 'upstream/main' into copilot/duplicate-p…
agocke Mar 6, 2026
c36349c
Annotate EventProvider
agocke Mar 6, 2026
eeb56b0
Include in Microsoft.Bcl.Memory
agocke Mar 6, 2026
c3d93e4
Revert [RequiresUnsafe] from IntPtr.cs and UIntPtr.cs
Copilot Mar 6, 2026
66211d8
Remove [RequiresUnsafe] from Add and Subtract methods in Unsafe.cs
Copilot Mar 6, 2026
4a5abce
Add [RequiresUnsafe] to Add and Subtract methods returning ref T in U…
Copilot Mar 6, 2026
f06fd45
Remove [RequiresUnsafe] from Alloc and AllocZeroed in NativeMemory
Copilot Mar 6, 2026
ba966e8
Add [RequiresUnsafe] to AddByteOffset and SubtractByteOffset methods …
Copilot Mar 6, 2026
051551e
Remove more incorrect [RequiresUnsafe] attributes
agocke Mar 9, 2026
64f7d7c
Unmark Array/String marshalling helpers
agocke Mar 9, 2026
2d7a196
Revert changes to Unsafe.Add/Subtract and remove some conservative Re…
agocke Mar 10, 2026
9a002a8
Remove more conservative RequiresUnsafe annotations
agocke Mar 10, 2026
7c9bf4b
Merge branch 'main' of https://github.com/dotnet/runtime into copilot…
Copilot Mar 11, 2026
06eb913
Revert whitespace-only changes and restore BOMs in 63 files
Copilot Mar 14, 2026
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ internal static unsafe partial class ThrowHelpers
{
[DoesNotReturn]
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ExceptionNative_ThrowAmbiguousResolutionException")]
[RequiresUnsafe]
private static partial void ThrowAmbiguousResolutionException(MethodTable* targetType, MethodTable* interfaceType, void* methodDesc);

[DoesNotReturn]
[DebuggerHidden]
[RequiresUnsafe]
internal static void ThrowAmbiguousResolutionException(
void* method, // MethodDesc*
void* interfaceType, // MethodTable*
Expand All @@ -26,10 +28,12 @@ internal static void ThrowAmbiguousResolutionException(

[DoesNotReturn]
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ExceptionNative_ThrowEntryPointNotFoundException")]
[RequiresUnsafe]
private static partial void ThrowEntryPointNotFoundException(MethodTable* targetType, MethodTable* interfaceType, void* methodDesc);

[DoesNotReturn]
[DebuggerHidden]
[RequiresUnsafe]
internal static void ThrowEntryPointNotFoundException(
void* method, // MethodDesc*
void* interfaceType, // MethodTable*
Expand All @@ -40,11 +44,13 @@ internal static void ThrowEntryPointNotFoundException(

[DoesNotReturn]
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ExceptionNative_ThrowMethodAccessException")]
[RequiresUnsafe]
private static partial void ThrowMethodAccessExceptionInternal(void* caller, void* callee);

// implementation of CORINFO_HELP_METHOD_ACCESS_EXCEPTION
[DoesNotReturn]
[DebuggerHidden]
[RequiresUnsafe]
internal static void ThrowMethodAccessException(
void* caller, // MethodDesc*
void* callee) // MethodDesc*
Expand All @@ -54,11 +60,13 @@ internal static void ThrowMethodAccessException(

[DoesNotReturn]
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ExceptionNative_ThrowFieldAccessException")]
[RequiresUnsafe]
private static partial void ThrowFieldAccessExceptionInternal(void* caller, void* callee);

// implementation of CORINFO_HELP_FIELD_ACCESS_EXCEPTION
[DoesNotReturn]
[DebuggerHidden]
[RequiresUnsafe]
internal static void ThrowFieldAccessException(
void* caller, // MethodDesc*
void* callee) // FieldDesc*
Expand All @@ -68,11 +76,13 @@ internal static void ThrowFieldAccessException(

[DoesNotReturn]
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ExceptionNative_ThrowClassAccessException")]
[RequiresUnsafe]
private static partial void ThrowClassAccessExceptionInternal(void* caller, void* callee);

// implementation of CORINFO_HELP_CLASS_ACCESS_EXCEPTION
[DoesNotReturn]
[DebuggerHidden]
[RequiresUnsafe]
internal static void ThrowClassAccessException(
void* caller, // MethodDesc*
void* callee) // Type handle
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.

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;

namespace Internal.Runtime.InteropServices
Expand All @@ -13,6 +14,7 @@ internal static class ComActivator
/// </summary>
/// <param name="pCxtInt">Pointer to a <see cref="ComActivationContextInternal"/> instance</param>
[UnmanagedCallersOnly]
[RequiresUnsafe]
private static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInternal* pCxtInt)
=> throw new PlatformNotSupportedException();

Expand All @@ -21,6 +23,7 @@ private static unsafe int GetClassFactoryForTypeInternal(ComActivationContextInt
/// </summary>
/// <param name="pCxtInt">Pointer to a <see cref="ComActivationContextInternal"/> instance</param>
[UnmanagedCallersOnly]
[RequiresUnsafe]
private static unsafe int RegisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
=> throw new PlatformNotSupportedException();

Expand All @@ -29,6 +32,7 @@ private static unsafe int RegisterClassForTypeInternal(ComActivationContextInter
/// </summary>
/// <param name="pCxtInt">Pointer to a <see cref="ComActivationContextInternal"/> instance</param>
[UnmanagedCallersOnly]
[RequiresUnsafe]
private static unsafe int UnregisterClassForTypeInternal(ComActivationContextInternal* pCxtInt)
=> throw new PlatformNotSupportedException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Expand All @@ -9,6 +10,7 @@ namespace System
public static partial class AppContext
{
[UnmanagedCallersOnly]
[RequiresUnsafe]
private static unsafe void OnProcessExit(Exception* pException)
{
try
Expand All @@ -22,6 +24,7 @@ private static unsafe void OnProcessExit(Exception* pException)
}

[UnmanagedCallersOnly]
[RequiresUnsafe]
private static unsafe void OnUnhandledException(object* pException, Exception* pOutException)
{
try
Expand All @@ -35,6 +38,7 @@ private static unsafe void OnUnhandledException(object* pException, Exception* p
}

[UnmanagedCallersOnly]
[RequiresUnsafe]
internal static unsafe void OnFirstChanceException(Exception* pException, Exception* pOutException)
{
try
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/System.Private.CoreLib/src/System/ArgIterator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Expand Down Expand Up @@ -161,6 +162,7 @@ public ArgIterator(RuntimeArgumentHandle arglist)
}

[CLSCompliant(false)]
[RequiresUnsafe]
public unsafe ArgIterator(RuntimeArgumentHandle arglist, void* ptr)
{
throw new PlatformNotSupportedException(SR.PlatformNotSupported_ArgIterator); // https://github.com/dotnet/runtime/issues/7317
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ namespace System
public abstract partial class Array : ICloneable, IList, IStructuralComparable, IStructuralEquatable
{
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "Array_CreateInstance")]
[RequiresUnsafe]
private static unsafe partial void InternalCreate(QCallTypeHandle type, int rank, int* pLengths, int* pLowerBounds,
[MarshalAs(UnmanagedType.Bool)] bool fromArrayType, ObjectHandleOnStack retArray);

[RequiresUnsafe]
private static unsafe Array InternalCreate(RuntimeType elementType, int rank, int* pLengths, int* pLowerBounds)
{
Array? retArray = null;
Expand All @@ -27,6 +29,7 @@ private static unsafe Array InternalCreate(RuntimeType elementType, int rank, in
return retArray!;
}

[RequiresUnsafe]
private static unsafe Array InternalCreateFromArrayType(RuntimeType arrayType, int rank, int* pLengths, int* pLowerBounds)
{
Array? retArray = null;
Expand All @@ -36,12 +39,14 @@ private static unsafe Array InternalCreateFromArrayType(RuntimeType arrayType, i
}

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "Array_Ctor")]
[RequiresUnsafe]
private static unsafe partial void Ctor(MethodTable* pArrayMT, uint dwNumArgs, int* pArgList, ObjectHandleOnStack retArray);

// implementation of CORINFO_HELP_NEW_MDARR and CORINFO_HELP_NEW_MDARR_RARE.
[StackTraceHidden]
[DebuggerStepThrough]
[DebuggerHidden]
[RequiresUnsafe]
internal static unsafe Array Ctor(MethodTable* pArrayMT, uint dwNumArgs, int* pArgList)
{
Array? arr = null;
Expand Down Expand Up @@ -304,6 +309,7 @@ public int Rank
[MethodImpl(MethodImplOptions.InternalCall)]
internal extern CorElementType GetCorElementTypeOfElementType();

[RequiresUnsafe]
private unsafe MethodTable* ElementMethodTable => RuntimeHelpers.GetMethodTable(this)->GetArrayElementTypeHandle().AsMethodTable();

private unsafe bool IsValueOfElementType(object value)
Expand Down Expand Up @@ -350,8 +356,10 @@ internal sealed unsafe partial class ArrayInitializeCache : RuntimeType.IGeneric
internal readonly delegate*<ref byte, void> ConstructorEntrypoint;

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "Array_GetElementConstructorEntrypoint")]
[RequiresUnsafe]
private static partial delegate*<ref byte, void> GetElementConstructorEntrypoint(QCallTypeHandle arrayType);

[RequiresUnsafe]
private ArrayInitializeCache(delegate*<ref byte, void> constructorEntrypoint)
{
ConstructorEntrypoint = constructorEntrypoint;
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.

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Expand All @@ -13,13 +14,15 @@ public partial class Buffer
private static extern void BulkMoveWithWriteBarrierInternal(ref byte destination, ref byte source, nuint byteCount);

// Used by ilmarshalers.cpp
[RequiresUnsafe]
internal static unsafe void Memcpy(byte* dest, byte* src, int len)
{
Debug.Assert(len >= 0, "Negative length in memcpy!");
Memmove(ref *dest, ref *src, (nuint)(uint)len /* force zero-extension */);
}

// Used by ilmarshalers.cpp
[RequiresUnsafe]
internal static unsafe void Memcpy(byte* pDest, int destIndex, byte[] src, int srcIndex, int len)
{
Debug.Assert((srcIndex >= 0) && (destIndex >= 0) && (len >= 0), "Index and length must be non-negative!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,11 @@ private void DelegateConstruct(object target, IntPtr method)
private static partial void Construct(ObjectHandleOnStack _this, ObjectHandleOnStack target, IntPtr method);

[MethodImpl(MethodImplOptions.InternalCall)]
[RequiresUnsafe]
private static extern unsafe void* GetMulticastInvoke(MethodTable* pMT);

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "Delegate_GetMulticastInvokeSlow")]
[RequiresUnsafe]
private static unsafe partial void* GetMulticastInvokeSlow(MethodTable* pMT);

internal unsafe IntPtr GetMulticastInvoke()
Expand All @@ -503,6 +505,7 @@ internal unsafe IntPtr GetMulticastInvoke()
}

[MethodImpl(MethodImplOptions.InternalCall)]
[RequiresUnsafe]
private static extern unsafe void* GetInvokeMethod(MethodTable* pMT);

internal unsafe IntPtr GetInvokeMethod()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private static void FailFast(ref StackCrawlMark mark, string? message, Exception
[DoesNotReturn]
private static partial void FailFast(StackCrawlMarkHandle mark, string? message, ObjectHandleOnStack exception, string? errorMessage);

[RequiresUnsafe]
private static unsafe string[] InitializeCommandLineArgs(char* exePath, int argc, char** argv) // invoked from VM
{
string[] commandLineArgs = new string[argc + 1];
Expand All @@ -107,6 +108,7 @@ private static unsafe string[] InitializeCommandLineArgs(char* exePath, int argc
internal static partial int GetProcessorCount();

[UnmanagedCallersOnly]
[RequiresUnsafe]
private static unsafe void GetResourceString(char* pKey, string* pResult, Exception* pException)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ internal void InternalPreserveStackTrace()
}

[UnmanagedCallersOnly]
[RequiresUnsafe]
private static unsafe void InternalPreserveStackTrace(Exception* pException, Exception* pOutException)
{
try
Expand Down Expand Up @@ -271,6 +272,7 @@ private bool CanSetRemoteStackTrace()
}

[UnmanagedCallersOnly]
[RequiresUnsafe]
internal static unsafe void CreateRuntimeWrappedException(object* pThrownObject, object* pResult, Exception* pException)
{
try
Expand All @@ -284,6 +286,7 @@ internal static unsafe void CreateRuntimeWrappedException(object* pThrownObject,
}

[UnmanagedCallersOnly]
[RequiresUnsafe]
internal static unsafe void CreateTypeInitializationException(char* pTypeName, Exception* pInnerException, object* pResult, Exception* pException)
{
try
Expand Down
5 changes: 5 additions & 0 deletions src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -308,6 +309,7 @@ public static int GetGeneration(WeakReference wo)
public static int MaxGeneration => GetMaxGeneration();

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "GCInterface_GetNextFinalizableObject")]
[RequiresUnsafe]
private static unsafe partial void* GetNextFinalizeableObject(ObjectHandleOnStack target);

private static unsafe uint RunFinalizers()
Expand Down Expand Up @@ -759,6 +761,7 @@ static void Free(NoGCRegionCallbackFinalizerWorkItem* pWorkItem)
}

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "GCInterface_EnableNoGCRegionCallback")]
[RequiresUnsafe]
private static unsafe partial EnableNoGCRegionCallbackStatus _EnableNoGCRegionCallback(NoGCRegionCallbackFinalizerWorkItem* callback, long totalSize);

internal static long GetGenerationBudget(int generation)
Expand Down Expand Up @@ -871,6 +874,7 @@ internal struct GCConfigurationContext
}

[UnmanagedCallersOnly]
[RequiresUnsafe]
private static unsafe void ConfigCallback(void* configurationContext, byte* name, byte* publicKey, GCConfigurationType type, long data)
{
// If the public key is null, it means that the corresponding configuration isn't publicly available
Expand Down Expand Up @@ -932,6 +936,7 @@ internal enum GCConfigurationType
}

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "GCInterface_EnumerateConfigurationValues")]
[RequiresUnsafe]
internal static unsafe partial void _EnumerateConfigurationValues(void* configurationDictionary, delegate* unmanaged<void*, byte*, byte*, GCConfigurationType, long, void> callback);

internal enum RefreshMemoryStatus
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

Expand All @@ -9,6 +10,7 @@ namespace System.IO
public abstract unsafe partial class Stream
{
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "Stream_HasOverriddenSlow")]
[RequiresUnsafe]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool HasOverriddenSlow(MethodTable* pMT, [MarshalAs(UnmanagedType.Bool)] bool isRead);

Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/System.Private.CoreLib/src/System/Math.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
**
===========================================================*/

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace System
Expand Down Expand Up @@ -122,9 +123,11 @@ public static unsafe (double Sin, double Cos) SinCos(double x)
public static extern double Tanh(double value);

[MethodImpl(MethodImplOptions.InternalCall)]
[RequiresUnsafe]
private static extern unsafe double ModF(double x, double* intptr);

[MethodImpl(MethodImplOptions.InternalCall)]
[RequiresUnsafe]
private static extern unsafe void SinCos(double x, double* sin, double* cos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
**
===========================================================*/

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace System
Expand Down Expand Up @@ -119,9 +120,11 @@ public static unsafe (float Sin, float Cos) SinCos(float x)
public static extern float Tanh(float x);

[MethodImpl(MethodImplOptions.InternalCall)]
[RequiresUnsafe]
private static extern unsafe float ModF(float x, float* intptr);

[MethodImpl(MethodImplOptions.InternalCall)]
[RequiresUnsafe]
private static extern unsafe void SinCos(float x, float* sin, float* cos);
}
}
Loading
Loading