Skip to content

Commit

Permalink
Remove redundant 'unsafe' context (#110953)
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorBo authored Dec 28, 2024
1 parent 2eb59bf commit 670db5c
Show file tree
Hide file tree
Showing 105 changed files with 209 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal static class InMemoryAssemblyLoader
/// </summary>
/// <param name="moduleHandle">The native module handle for the assembly.</param>
/// <param name="assemblyPath">The path to the assembly (as a pointer to a UTF-16 C string).</param>
public static unsafe void LoadInMemoryAssembly(IntPtr moduleHandle, IntPtr assemblyPath)
public static void LoadInMemoryAssembly(IntPtr moduleHandle, IntPtr assemblyPath)
{
if (!IsSupported)
throw new NotSupportedException(SR.NotSupported_CppCli);
Expand All @@ -37,7 +37,7 @@ public static unsafe void LoadInMemoryAssembly(IntPtr moduleHandle, IntPtr assem
// It is intentionally left in the product, so developers get a warning when trimming an app which enabled `Internal.Runtime.InteropServices.InMemoryAssemblyLoader.IsSupported`.
// For runtime build the warning is suppressed in the ILLink.Suppressions.LibraryBuild.xml, but we only want to suppress it if the feature is enabled (IsSupported is true).
// The call is extracted into a separate method which is the sole target of the suppression.
private static unsafe void LoadInMemoryAssemblyInContextWhenSupported(IntPtr moduleHandle, IntPtr assemblyPath)
private static void LoadInMemoryAssemblyInContextWhenSupported(IntPtr moduleHandle, IntPtr assemblyPath)
{
#pragma warning disable IL2026 // suppressed in ILLink.Suppressions.LibraryBuild.xml
LoadInMemoryAssemblyInContextImpl(moduleHandle, assemblyPath);
Expand All @@ -54,7 +54,7 @@ private static unsafe void LoadInMemoryAssemblyInContextWhenSupported(IntPtr mod
[UnmanagedCallersOnly]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
Justification = "The same C++/CLI feature switch applies to LoadInMemoryAssembly and this function. We rely on the warning from LoadInMemoryAssembly.")]
public static unsafe void LoadInMemoryAssemblyInContext(IntPtr moduleHandle, IntPtr assemblyPath, IntPtr loadContext)
public static void LoadInMemoryAssemblyInContext(IntPtr moduleHandle, IntPtr assemblyPath, IntPtr loadContext)
{
if (!IsSupported)
throw new NotSupportedException(SR.NotSupported_CppCli);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public int GetRemainingCount()
}

// Gets the type of the current arg, does NOT advance the iterator
public unsafe RuntimeTypeHandle GetNextArgType()
public RuntimeTypeHandle GetNextArgType()
{
return RuntimeTypeHandle.FromIntPtr(GetNextArgType(ThisPtr));
}
Expand Down
10 changes: 5 additions & 5 deletions src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private static CorElementType GetNormalizedIntegralArrayElementType(CorElementTy
// instance & might fail when called from within a CER, or if the
// reliable flag is true, it will either always succeed or always
// throw an exception with no side effects.
private static unsafe void CopySlow(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length, ArrayAssignType assignType)
private static void CopySlow(Array sourceArray, int sourceIndex, Array destinationArray, int destinationIndex, int length, ArrayAssignType assignType)
{
Debug.Assert(sourceArray.Rank == destinationArray.Rank);

Expand Down Expand Up @@ -605,7 +605,7 @@ private unsafe void InternalSetValue(object? value, nint flattenedIndex)

public long LongLength => (long)NativeLength;

public unsafe int Rank
public int Rank
{
get
{
Expand All @@ -615,7 +615,7 @@ public unsafe int Rank
}

[Intrinsic]
public unsafe int GetLength(int dimension)
public int GetLength(int dimension)
{
int rank = RuntimeHelpers.GetMultiDimensionalArrayRank(this);
if (rank == 0 && dimension == 0)
Expand All @@ -628,7 +628,7 @@ public unsafe int GetLength(int dimension)
}

[Intrinsic]
public unsafe int GetUpperBound(int dimension)
public int GetUpperBound(int dimension)
{
int rank = RuntimeHelpers.GetMultiDimensionalArrayRank(this);
if (rank == 0 && dimension == 0)
Expand All @@ -642,7 +642,7 @@ public unsafe int GetUpperBound(int dimension)
}

[Intrinsic]
public unsafe int GetLowerBound(int dimension)
public int GetLowerBound(int dimension)
{
int rank = RuntimeHelpers.GetMultiDimensionalArrayRank(this);
if (rank == 0 && dimension == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ internal static unsafe MarshalAsAttribute GetMarshalAs(ConstArray nativeType, Ru

#region Constructor
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern unsafe IntPtr GetMetadataImport(RuntimeModule module);
private static extern IntPtr GetMetadataImport(RuntimeModule module);

internal MetadataImport(RuntimeModule module)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace System.Reflection
{
internal sealed unsafe class RtFieldInfo : RuntimeFieldInfo, IRuntimeFieldInfo
internal sealed class RtFieldInfo : RuntimeFieldInfo, IRuntimeFieldInfo
{
#region Private Data Members
// aggressive caching
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace System.Reflection
{
internal sealed unsafe class RuntimeParameterInfo : ParameterInfo
internal sealed class RuntimeParameterInfo : ParameterInfo
{
#region Static Members
internal static ParameterInfo[] GetParameters(IRuntimeMethodInfo method, MemberInfo member, Signature sig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ private static void StelemRef_Helper_NoCacheLookup(ref object? element, void* el
}

[DebuggerHidden]
private static unsafe void ArrayTypeCheck(object obj, Array array)
private static void ArrayTypeCheck(object obj, Array array)
{
Debug.Assert(obj != null);

Expand All @@ -507,7 +507,7 @@ private static unsafe void ArrayTypeCheck(object obj, Array array)

[DebuggerHidden]
[MethodImpl(MethodImplOptions.NoInlining)]
private static unsafe void ArrayTypeCheck_Helper(object obj, void* elementType)
private static void ArrayTypeCheck_Helper(object obj, void* elementType)
{
Debug.Assert(obj != null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static IntPtr Method(IntPtr methodHnd, IntPtr signature)
}

[DebuggerHidden]
public static unsafe IntPtr MethodWithSlotAndModule(IntPtr methodHnd, GenericHandleArgs * pArgs)
public static IntPtr MethodWithSlotAndModule(IntPtr methodHnd, GenericHandleArgs * pArgs)
{
return GenericHandleWorker(methodHnd, IntPtr.Zero, pArgs->signature, pArgs->dictionaryIndexAndSlot, pArgs->module);
}
Expand All @@ -40,7 +40,7 @@ public static IntPtr Class(IntPtr classHnd, IntPtr signature)
}

[DebuggerHidden]
public static unsafe IntPtr ClassWithSlotAndModule(IntPtr classHnd, GenericHandleArgs * pArgs)
public static IntPtr ClassWithSlotAndModule(IntPtr classHnd, GenericHandleArgs * pArgs)
{
return GenericHandleWorker(IntPtr.Zero, classHnd, pArgs->signature, pArgs->dictionaryIndexAndSlot, pArgs->module);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ static int GetHashCodeWorker(object? o)
}

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern unsafe bool ContentEquals(object o1, object o2);
private static extern bool ContentEquals(object o1, object o2);

[Obsolete("OffsetToStringData has been deprecated. Use string.GetPinnableReference() instead.")]
public static int OffsetToStringData
Expand Down Expand Up @@ -414,7 +414,7 @@ internal static unsafe ushort GetElementSize(this Array array)

// Returns pointer to the multi-dimensional array bounds.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static unsafe ref int GetMultiDimensionalArrayBounds(Array array)
internal static ref int GetMultiDimensionalArrayBounds(Array array)
{
Debug.Assert(GetMultiDimensionalArrayRank(array) > 0);
// See comment on RawArrayData for details
Expand Down Expand Up @@ -553,7 +553,7 @@ private static unsafe void DispatchTailCalls(
/// <exception cref="ArgumentNullException">The specified type handle is <c>null</c>.</exception>
/// <exception cref="ArgumentException">The specified type cannot have a boxed instance of itself created.</exception>
/// <exception cref="NotSupportedException">The passed in type is a by-ref-like type.</exception>
public static unsafe object? Box(ref byte target, RuntimeTypeHandle type)
public static object? Box(ref byte target, RuntimeTypeHandle type)
{
if (type.IsNullHandle())
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.type);
Expand All @@ -574,7 +574,7 @@ private static unsafe void DispatchTailCalls(
/// <remarks>
/// This API returns the same value as <see cref="Unsafe.SizeOf{T}"/> for the type that <paramref name="type"/> represents.
/// </remarks>
public static unsafe int SizeOf(RuntimeTypeHandle type)
public static int SizeOf(RuntimeTypeHandle type)
{
if (type.IsNullHandle())
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.type);
Expand Down Expand Up @@ -951,19 +951,19 @@ internal static ref byte MaskStaticsPointer(ref byte staticsPtr)
}
}

internal unsafe MethodTable* _methodTable;
internal MethodTable* _methodTable;
}

[StructLayout(LayoutKind.Sequential)]
internal unsafe ref struct GenericsStaticsInfo
internal ref struct GenericsStaticsInfo
{
// Pointer to field descs for statics
internal IntPtr _pFieldDescs;
internal DynamicStaticsInfo _dynamicStatics;
}

[StructLayout(LayoutKind.Sequential)]
internal unsafe ref struct ThreadStaticsInfo
internal ref struct ThreadStaticsInfo
{
internal int _nonGCTlsIndex;
internal int _gcTlsIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private struct VirtualFunctionPointerArgs

[MethodImpl(MethodImplOptions.NoInlining)]
[DebuggerHidden]
private static unsafe IntPtr VirtualFunctionPointerSlow(object obj, IntPtr classHandle, IntPtr methodHandle)
private static IntPtr VirtualFunctionPointerSlow(object obj, IntPtr classHandle, IntPtr methodHandle)
{
IntPtr result = ResolveVirtualFunctionPointer(ObjectHandleOnStack.Create(ref obj), classHandle, methodHandle);
s_virtualFunctionPointerCache.TrySet(new VirtualResolutionData(RuntimeHelpers.GetMethodTable(obj), classHandle, methodHandle), result);
Expand All @@ -72,7 +72,7 @@ private static unsafe IntPtr VirtualFunctionPointerSlow(object obj, IntPtr class
}

[DebuggerHidden]
private static unsafe IntPtr VirtualFunctionPointer(object obj, IntPtr classHandle, IntPtr methodHandle)
private static IntPtr VirtualFunctionPointer(object obj, IntPtr classHandle, IntPtr methodHandle)
{
if (s_virtualFunctionPointerCache.TryGet(new VirtualResolutionData(RuntimeHelpers.GetMethodTable(obj), classHandle, methodHandle), out IntPtr result))
{
Expand All @@ -82,7 +82,7 @@ private static unsafe IntPtr VirtualFunctionPointer(object obj, IntPtr classHand
}

[DebuggerHidden]
private static unsafe IntPtr VirtualFunctionPointer_Dynamic(object obj, ref VirtualFunctionPointerArgs virtualFunctionPointerArgs)
private static IntPtr VirtualFunctionPointer_Dynamic(object obj, ref VirtualFunctionPointerArgs virtualFunctionPointerArgs)
{
IntPtr classHandle = virtualFunctionPointerArgs.classHnd;
IntPtr methodHandle = virtualFunctionPointerArgs.methodHnd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public abstract partial class ComWrappers
/// it is important for the caller to understand the COM object may have apartment affinity and therefore
/// if the current thread is not in the correct apartment or the COM object is not a proxy this call may fail.
/// </remarks>
public static unsafe bool TryGetComInstance(object obj, out IntPtr unknown)
public static bool TryGetComInstance(object obj, out IntPtr unknown)
{
if (obj == null)
{
Expand All @@ -61,7 +61,7 @@ public static unsafe bool TryGetComInstance(object obj, out IntPtr unknown)
/// <param name="unknown">An unmanaged wrapper</param>
/// <param name="obj">A managed object</param>
/// <returns>True if the wrapper was resolved to a managed object, otherwise false.</returns>
public static unsafe bool TryGetObject(IntPtr unknown, [NotNullWhen(true)] out object? obj)
public static bool TryGetObject(IntPtr unknown, [NotNullWhen(true)] out object? obj)
{
obj = null;
if (unknown == IntPtr.Zero)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ internal string ConstructName(TypeNameFormatFlags formatFlags)
// Since the returned string is a pointer into metadata, the caller should
// ensure the passed in type is alive for at least as long as returned result is
// needed.
internal static unsafe MdUtf8String GetUtf8Name(RuntimeType type)
internal static MdUtf8String GetUtf8Name(RuntimeType type)
{
TypeHandle th = type.GetNativeTypeHandle();
if (th.IsTypeDesc || th.AsMethodTable()->IsArray)
Expand Down Expand Up @@ -673,7 +673,7 @@ internal static bool CanCastTo(RuntimeType type, RuntimeType target)
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "RuntimeTypeHandle_GetDeclaringTypeHandle")]
private static partial IntPtr GetDeclaringTypeHandle(IntPtr typeHandle);

internal static unsafe RuntimeType? GetDeclaringType(RuntimeType type)
internal static RuntimeType? GetDeclaringType(RuntimeType type)
{
IntPtr retTypeHandle = IntPtr.Zero;
TypeHandle typeHandle = type.GetNativeTypeHandle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ private unsafe void PopulateRtFields(Filter filter, RuntimeType declaringType, r
}
}

private unsafe void PopulateRtFields(Filter filter,
private void PopulateRtFields(Filter filter,
ReadOnlySpan<IntPtr> fieldHandles, RuntimeType declaringType, ref ListBuilder<RuntimeFieldInfo> list)
{
Debug.Assert(declaringType != null);
Expand Down
8 changes: 4 additions & 4 deletions src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ internal static unsafe void ClearNative(IntPtr pVariant)

internal sealed class HandleMarshaler
{
internal static unsafe IntPtr ConvertSafeHandleToNative(SafeHandle? handle, ref CleanupWorkListElement? cleanupWorkList)
internal static IntPtr ConvertSafeHandleToNative(SafeHandle? handle, ref CleanupWorkListElement? cleanupWorkList)
{
if (Unsafe.IsNullRef(ref cleanupWorkList))
{
Expand All @@ -516,12 +516,12 @@ internal static unsafe IntPtr ConvertSafeHandleToNative(SafeHandle? handle, ref
return StubHelpers.AddToCleanupList(ref cleanupWorkList, handle);
}

internal static unsafe void ThrowSafeHandleFieldChanged()
internal static void ThrowSafeHandleFieldChanged()
{
throw new NotSupportedException(SR.Interop_Marshal_CannotCreateSafeHandleField);
}

internal static unsafe void ThrowCriticalHandleFieldChanged()
internal static void ThrowCriticalHandleFieldChanged()
{
throw new NotSupportedException(SR.Interop_Marshal_CannotCreateCriticalHandleField);
}
Expand Down Expand Up @@ -797,7 +797,7 @@ internal static void ClearNative(IntPtr pMarshalState, in object pManagedHome, I

internal static unsafe partial class MngdRefCustomMarshaler
{
internal static unsafe void ConvertContentsToNative(ICustomMarshaler marshaler, in object pManagedHome, IntPtr* pNativeHome)
internal static void ConvertContentsToNative(ICustomMarshaler marshaler, in object pManagedHome, IntPtr* pNativeHome)
{
// COMPAT: We never pass null to MarshalManagedToNative.
if (pManagedHome is null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract partial class WaitHandle
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "WaitHandle_WaitOneCore")]
private static partial int WaitOneCore(IntPtr waitHandle, int millisecondsTimeout, [MarshalAs(UnmanagedType.Bool)] bool useTrivialWaits);

private static unsafe int WaitMultipleIgnoringSyncContextCore(ReadOnlySpan<IntPtr> waitHandles, bool waitAll, int millisecondsTimeout)
private static int WaitMultipleIgnoringSyncContextCore(ReadOnlySpan<IntPtr> waitHandles, bool waitAll, int millisecondsTimeout)
=> WaitMultipleIgnoringSyncContext(waitHandles, waitHandles.Length, waitAll, millisecondsTimeout);

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "WaitHandle_WaitMultipleIgnoringSyncContext")]
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/System.Private.CoreLib/src/System/Variant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal static ComVariant GetIUnknownOrIDispatchFromObject(object? obj)
return pUnk == IntPtr.Zero ? null : Marshal.GetObjectForIUnknown(pUnk);
}

private static unsafe object? ConvertWrappedObject(object? wrapped)
private static object? ConvertWrappedObject(object? wrapped)
{
// Historically, for UnknownWrapper and DispatchWrapper, the wrapped object is passed
// into Variant.SetFieldsObject, and the result set in objRef field is used for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ private struct OSCONTEXT
{
}

internal static unsafe void* PointerAlign(void* ptr, int alignmentInBytes)
internal static void* PointerAlign(void* ptr, int alignmentInBytes)
{
int alignMask = alignmentInBytes - 1;
#if TARGET_64BIT
Expand Down Expand Up @@ -205,7 +205,7 @@ private static void OnUnhandledExceptionViaClassLib(object exception)
}

[MethodImpl(MethodImplOptions.NoInlining)]
internal static unsafe void UnhandledExceptionFailFastViaClasslib(
internal static void UnhandledExceptionFailFastViaClasslib(
RhFailFastReason reason, object unhandledException, IntPtr classlibAddress, ref ExInfo exInfo)
{
#if NATIVEAOT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal unsafe struct CMSG_CMS_RECIPIENT_INFO
//
private void* pRecipientInfo; // Do NOT add an underscore - this name still maps to a C++ Win32 header definition.

internal unsafe CMSG_KEY_TRANS_RECIPIENT_INFO* KeyTrans
internal CMSG_KEY_TRANS_RECIPIENT_INFO* KeyTrans
{
get
{
Expand All @@ -29,7 +29,7 @@ internal unsafe CMSG_KEY_TRANS_RECIPIENT_INFO* KeyTrans
}
}

internal unsafe CMSG_KEY_AGREE_RECIPIENT_INFO* KeyAgree
internal CMSG_KEY_AGREE_RECIPIENT_INFO* KeyAgree
{
get
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ internal unsafe struct OBJECT_ATTRIBUTES
/// <summary>
/// Equivalent of InitializeObjectAttributes macro with the exception that you can directly set SQOS.
/// </summary>
public unsafe OBJECT_ATTRIBUTES(UNICODE_STRING* objectName, ObjectAttributes attributes, IntPtr rootDirectory, SECURITY_QUALITY_OF_SERVICE* securityQualityOfService = null)
public OBJECT_ATTRIBUTES(UNICODE_STRING* objectName, ObjectAttributes attributes, IntPtr rootDirectory, SECURITY_QUALITY_OF_SERVICE* securityQualityOfService = null)
{
Length = (uint)sizeof(OBJECT_ATTRIBUTES);
RootDirectory = rootDirectory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal unsafe struct SECURITY_QUALITY_OF_SERVICE
public ContextTrackingMode ContextTrackingMode;
public BOOLEAN EffectiveOnly;

public unsafe SECURITY_QUALITY_OF_SERVICE(ImpersonationLevel impersonationLevel, ContextTrackingMode contextTrackingMode, bool effectiveOnly)
public SECURITY_QUALITY_OF_SERVICE(ImpersonationLevel impersonationLevel, ContextTrackingMode contextTrackingMode, bool effectiveOnly)
{
Length = (uint)sizeof(SECURITY_QUALITY_OF_SERVICE);
ImpersonationLevel = impersonationLevel;
Expand Down
Loading

0 comments on commit 670db5c

Please sign in to comment.