diff --git a/src/coreclr/vm/corelib.h b/src/coreclr/vm/corelib.h index b5ff0a1c795939..a2158726dd8303 100644 --- a/src/coreclr/vm/corelib.h +++ b/src/coreclr/vm/corelib.h @@ -693,7 +693,6 @@ DEFINE_METHOD(UNSAFE, AS_POINTER, AsPointer, NoSig) DEFINE_METHOD(UNSAFE, BYREF_IS_NULL, IsNullRef, NoSig) DEFINE_METHOD(UNSAFE, BYREF_NULLREF, NullRef, NoSig) DEFINE_METHOD(UNSAFE, AS_REF_IN, AsRef, GM_RefT_RetRefT) -DEFINE_METHOD(UNSAFE, AS_REF_POINTER, AsRef, GM_VoidPtr_RetRefT) DEFINE_METHOD(UNSAFE, BYREF_AS, As, GM_RefTFrom_RetRefTTo) DEFINE_METHOD(UNSAFE, OBJECT_AS, As, GM_Obj_RetT) DEFINE_METHOD(UNSAFE, BYREF_ADD, Add, GM_RefT_Int_RetRefT) diff --git a/src/coreclr/vm/jitinterface.cpp b/src/coreclr/vm/jitinterface.cpp index a1dd582f554203..5bc558cd3db1f8 100644 --- a/src/coreclr/vm/jitinterface.cpp +++ b/src/coreclr/vm/jitinterface.cpp @@ -6791,7 +6791,6 @@ bool getILIntrinsicImplementationForUnsafe(MethodDesc * ftn, } else if (tk == CoreLibBinder::GetMethod(METHOD__UNSAFE__BYREF_AS)->GetMemberDef() || tk == CoreLibBinder::GetMethod(METHOD__UNSAFE__OBJECT_AS)->GetMemberDef() || - tk == CoreLibBinder::GetMethod(METHOD__UNSAFE__AS_REF_POINTER)->GetMemberDef() || tk == CoreLibBinder::GetMethod(METHOD__UNSAFE__AS_REF_IN)->GetMemberDef()) { // Return the argument that was passed in. diff --git a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.cs b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.cs index 4f5f655640874f..7a7a537a71ca3b 100644 --- a/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.cs +++ b/src/libraries/Common/src/Interop/Unix/System.Native/Interop.MountPoints.cs @@ -7,6 +7,8 @@ using System.Runtime.ExceptionServices; using System.Runtime.InteropServices; +#pragma warning disable 8500 // takes address of managed type + internal static partial class Interop { internal static partial class Sys @@ -23,15 +25,14 @@ private struct AllMountPointsContext [UnmanagedCallersOnly] private static unsafe void AddMountPoint(void* context, byte* name) { - ref AllMountPointsContext callbackContext = ref Unsafe.As(ref *(byte*)context); - + AllMountPointsContext* callbackContext = (AllMountPointsContext*)context; try { - callbackContext._results.Add(Marshal.PtrToStringUTF8((IntPtr)name)!); + callbackContext->_results.Add(Marshal.PtrToStringUTF8((IntPtr)name)!); } catch (Exception e) { - callbackContext._exception = ExceptionDispatchInfo.Capture(e); + callbackContext->_exception = ExceptionDispatchInfo.Capture(e); } } @@ -42,7 +43,7 @@ internal static string[] GetAllMountPoints() unsafe { - GetAllMountPoints(&AddMountPoint, Unsafe.AsPointer(ref context)); + GetAllMountPoints(&AddMountPoint, &context); } context._exception?.Throw(); diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPv4GlobalStatistics.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPv4GlobalStatistics.cs index a2333229a7a2e5..fc77f10c6cc487 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPv4GlobalStatistics.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIPv4GlobalStatistics.cs @@ -6,6 +6,8 @@ using System.Runtime.InteropServices; using System.Runtime.Versioning; +#pragma warning disable 8500 // taking address of managed types + namespace System.Net.NetworkInformation { internal sealed class BsdIPv4GlobalStatistics : IPGlobalStatistics @@ -37,10 +39,10 @@ private struct Context [UnmanagedCallersOnly] private static unsafe void ProcessIpv4Address(void* pContext, byte* ifaceName, Interop.Sys.IpAddressInfo* ipAddr) { - ref Context context = ref Unsafe.As(ref *(byte*)pContext); + Context* context = (Context*)pContext; - context._interfaceSet.Add(new string((sbyte*)ifaceName)); - context._numIPAddresses++; + context->_interfaceSet.Add(new string((sbyte*)ifaceName)); + context->_numIPAddresses++; } public unsafe BsdIPv4GlobalStatistics() @@ -71,7 +73,7 @@ public unsafe BsdIPv4GlobalStatistics() context._interfaceSet = new HashSet(); Interop.Sys.EnumerateInterfaceAddresses( - Unsafe.AsPointer(ref context), + &context, &ProcessIpv4Address, null, null); diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIpInterfaceProperties.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIpInterfaceProperties.cs index 2bf1226c0f47b0..6f0ef7f4a26907 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIpInterfaceProperties.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdIpInterfaceProperties.cs @@ -6,6 +6,8 @@ using System.Runtime.InteropServices; using System.Runtime.Versioning; +#pragma warning disable 8500 // taking address of managed types + namespace System.Net.NetworkInformation { internal sealed class BsdIpInterfaceProperties : UnixIPInterfaceProperties @@ -74,7 +76,7 @@ private static unsafe GatewayIPAddressInformationCollection GetGatewayAddresses( Context context; context._interfaceIndex = interfaceIndex; context._addressSet = new HashSet(); - if (Interop.Sys.EnumerateGatewayAddressesForInterface(Unsafe.AsPointer(ref context), (uint)interfaceIndex, &OnGatewayFound) == -1) + if (Interop.Sys.EnumerateGatewayAddressesForInterface(&context, (uint)interfaceIndex, &OnGatewayFound) == -1) { throw new NetworkInformationException(SR.net_PInvokeError); } @@ -91,15 +93,15 @@ private static unsafe GatewayIPAddressInformationCollection GetGatewayAddresses( [UnmanagedCallersOnly] private static unsafe void OnGatewayFound(void* pContext, Interop.Sys.IpAddressInfo* gatewayAddressInfo) { - ref Context context = ref Unsafe.As(ref *(byte*)pContext); + Context* context = (Context*)pContext; IPAddress ipAddress = new IPAddress(new ReadOnlySpan(gatewayAddressInfo->AddressBytes, gatewayAddressInfo->NumAddressBytes)); if (ipAddress.IsIPv6LinkLocal) { // For Link-Local addresses add ScopeId as that is not part of the route entry. - ipAddress.ScopeId = context._interfaceIndex; + ipAddress.ScopeId = context->_interfaceIndex; } - context._addressSet.Add(ipAddress); + context->_addressSet.Add(ipAddress); } } } diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdNetworkInterface.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdNetworkInterface.cs index bbce06de8f3663..b340e7d2032776 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdNetworkInterface.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/BsdNetworkInterface.cs @@ -5,6 +5,8 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +#pragma warning disable 8500 // taking address of managed types + namespace System.Net.NetworkInformation { internal sealed class BsdNetworkInterface : UnixNetworkInterface @@ -72,42 +74,42 @@ internal void AddException(Exception e) [UnmanagedCallersOnly] private static unsafe void ProcessIpv4Address(void* pContext, byte* ifaceName, Interop.Sys.IpAddressInfo* ipAddr) { - ref Context context = ref Unsafe.As(ref *(byte*)pContext); + Context* context = (Context*)pContext; try { - context.GetOrCreate(ifaceName, ipAddr->InterfaceIndex).ProcessIpv4Address(ipAddr); + context->GetOrCreate(ifaceName, ipAddr->InterfaceIndex).ProcessIpv4Address(ipAddr); } catch (Exception e) { - context.AddException(e); + context->AddException(e); } } [UnmanagedCallersOnly] private static unsafe void ProcessIpv6Address(void* pContext, byte* ifaceName, Interop.Sys.IpAddressInfo* ipAddr, uint* scopeId) { - ref Context context = ref Unsafe.As(ref *(byte*)pContext); + Context* context = (Context*)pContext; try { - context.GetOrCreate(ifaceName, ipAddr->InterfaceIndex).ProcessIpv6Address(ipAddr, *scopeId); + context->GetOrCreate(ifaceName, ipAddr->InterfaceIndex).ProcessIpv6Address(ipAddr, *scopeId); } catch (Exception e) { - context.AddException(e); + context->AddException(e); } } [UnmanagedCallersOnly] private static unsafe void ProcessLinkLayerAddress(void* pContext, byte* ifaceName, Interop.Sys.LinkLayerAddressInfo* llAddr) { - ref Context context = ref Unsafe.As(ref *(byte*)pContext); + Context* context = (Context*)pContext; try { - context.GetOrCreate(ifaceName, llAddr->InterfaceIndex).ProcessLinkLayerAddress(llAddr); + context->GetOrCreate(ifaceName, llAddr->InterfaceIndex).ProcessLinkLayerAddress(llAddr); } catch (Exception e) { - context.AddException(e); + context->AddException(e); } } @@ -123,7 +125,7 @@ public static unsafe NetworkInterface[] GetBsdNetworkInterfaces() // Because these callbacks are executed in a reverse-PInvoke, we do not want any exceptions // to propagate out, because they will not be catchable. Instead, we track all the exceptions // that are thrown in these callbacks, and aggregate them at the end. - int result = Interop.Sys.EnumerateInterfaceAddresses(Unsafe.AsPointer(ref context), &ProcessIpv4Address, &ProcessIpv6Address, &ProcessLinkLayerAddress); + int result = Interop.Sys.EnumerateInterfaceAddresses(&context, &ProcessIpv4Address, &ProcessIpv6Address, &ProcessLinkLayerAddress); if (context._exceptions != null) { throw new NetworkInformationException(SR.net_PInvokeError, new AggregateException(context._exceptions)); diff --git a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixIPGlobalProperties.cs b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixIPGlobalProperties.cs index 9884d093e58dac..9de6b8b88f8a25 100644 --- a/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixIPGlobalProperties.cs +++ b/src/libraries/System.Net.NetworkInformation/src/System/Net/NetworkInformation/UnixIPGlobalProperties.cs @@ -8,6 +8,8 @@ using System.Threading; using System.Threading.Tasks; +#pragma warning disable 8500 // taking address of managed types + namespace System.Net.NetworkInformation { internal abstract class UnixIPGlobalProperties : IPGlobalProperties @@ -70,36 +72,36 @@ internal void AddException(Exception e) [UnmanagedCallersOnly] private static unsafe void ProcessIpv4Address(void* pContext, byte* ifaceName, Interop.Sys.IpAddressInfo* ipAddr) { - ref Context context = ref Unsafe.As(ref *(byte*)pContext); + Context* context = (Context*)pContext; try { IPAddress ipAddress = IPAddressUtil.GetIPAddressFromNativeInfo(ipAddr); if (!IPAddressUtil.IsMulticast(ipAddress)) { - context._collection.InternalAdd(new UnixUnicastIPAddressInformation(ipAddress, ipAddr->PrefixLength)); + context->_collection.InternalAdd(new UnixUnicastIPAddressInformation(ipAddress, ipAddr->PrefixLength)); } } catch (Exception e) { - context.AddException(e); + context->AddException(e); } } [UnmanagedCallersOnly] private static unsafe void ProcessIpv6Address(void* pContext, byte* ifaceName, Interop.Sys.IpAddressInfo* ipAddr, uint* scopeId) { - ref Context context = ref Unsafe.As(ref *(byte*)pContext); + Context* context = (Context*)pContext; try { IPAddress ipAddress = IPAddressUtil.GetIPAddressFromNativeInfo(ipAddr); if (!IPAddressUtil.IsMulticast(ipAddress)) { - context._collection.InternalAdd(new UnixUnicastIPAddressInformation(ipAddress, ipAddr->PrefixLength)); + context->_collection.InternalAdd(new UnixUnicastIPAddressInformation(ipAddress, ipAddr->PrefixLength)); } } catch (Exception e) { - context.AddException(e); + context->AddException(e); } } @@ -110,7 +112,7 @@ public override unsafe UnicastIPAddressInformationCollection GetUnicastAddresses context._exceptions = null; // Ignore link-layer addresses that are discovered; don't create a callback. - Interop.Sys.EnumerateInterfaceAddresses(Unsafe.AsPointer(ref context), &ProcessIpv4Address, &ProcessIpv6Address, null); + Interop.Sys.EnumerateInterfaceAddresses(&context, &ProcessIpv4Address, &ProcessIpv6Address, null); if (context._exceptions != null) throw new NetworkInformationException(SR.net_PInvokeError, new AggregateException(context._exceptions)); 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 51edda47b5023e..1ac42e04d237ff 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 @@ -237,7 +237,7 @@ private unsafe SocketError ProcessIOCPResult(bool success, int bytesTransferred, socketError = (SocketError)(packedResult & 0xFFFFFFFF); if (socketError != SocketError.Success) { - GetOverlappedResultOnError(ref socketError, ref Unsafe.As(ref bytesTransferred), ref socketFlags, overlapped); + GetOverlappedResultOnError(ref socketError, ref *(uint*)&bytesTransferred, ref socketFlags, overlapped); } FreeNativeOverlapped(ref overlapped); } diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CalendarData.Icu.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CalendarData.Icu.cs index 7be936a58c5059..7b57d6f9b48d5e 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CalendarData.Icu.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CalendarData.Icu.cs @@ -427,11 +427,13 @@ private static unsafe void EnumCalendarInfoCallback(char* calendarStringPtr, Int try { ReadOnlySpan calendarStringSpan = MemoryMarshal.CreateReadOnlySpanFromNullTerminated(calendarStringPtr); - ref IcuEnumCalendarsData callbackContext = ref Unsafe.As(ref *(byte*)context); +#pragma warning disable 8500 + IcuEnumCalendarsData* callbackContext = (IcuEnumCalendarsData*)context; +#pragma warning restore 8500 - if (callbackContext.DisallowDuplicates) + if (callbackContext->DisallowDuplicates) { - foreach (string existingResult in callbackContext.Results) + foreach (string existingResult in callbackContext->Results) { if (string.CompareOrdinal(calendarStringSpan, existingResult) == 0) { @@ -441,7 +443,7 @@ private static unsafe void EnumCalendarInfoCallback(char* calendarStringPtr, Int } } - callbackContext.Results.Add(calendarStringSpan.ToString()); + callbackContext->Results.Add(calendarStringSpan.ToString()); } catch (Exception e) { diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CalendarData.Nls.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CalendarData.Nls.cs index 5753747a80bc78..2e04ff6c688b2a 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CalendarData.Nls.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CalendarData.Nls.cs @@ -6,6 +6,8 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +#pragma warning disable 8500 // taking address of managed type + namespace System.Globalization { internal sealed partial class CalendarData @@ -61,16 +63,16 @@ private struct EnumData [UnmanagedCallersOnly] private static unsafe Interop.BOOL EnumCalendarInfoCallback(char* lpCalendarInfoString, uint calendar, IntPtr pReserved, void* lParam) { - ref EnumData context = ref Unsafe.As(ref *(byte*)lParam); + EnumData* context = (EnumData*)lParam; try { string calendarInfo = new string(lpCalendarInfoString); // If we had a user override, check to make sure this differs - if (context.userOverride != calendarInfo) + if (context->userOverride != calendarInfo) { - Debug.Assert(context.strings != null); - context.strings.Add(calendarInfo); + Debug.Assert(context->strings != null); + context->strings!.Add(calendarInfo); // TODO https://github.com/dotnet/roslyn/issues/65634: Remove ! when no longer needed } return Interop.BOOL.TRUE; @@ -93,12 +95,12 @@ public struct NlsEnumCalendarsData [UnmanagedCallersOnly] private static unsafe Interop.BOOL EnumCalendarsCallback(char* lpCalendarInfoString, uint calendar, IntPtr reserved, void* lParam) { - ref NlsEnumCalendarsData context = ref Unsafe.As(ref *(byte*)lParam); + NlsEnumCalendarsData* context = (NlsEnumCalendarsData*)lParam; try { // If we had a user override, check to make sure this differs - if (context.userOverride != calendar) - context.calendars.Add((int)calendar); + if (context->userOverride != calendar) + context->calendars.Add((int)calendar); return Interop.BOOL.TRUE; } diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CalendarData.Windows.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CalendarData.Windows.cs index 4602b06da94e2f..b1e8cefd6f41a2 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CalendarData.Windows.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CalendarData.Windows.cs @@ -5,6 +5,8 @@ using System.Diagnostics; using System.Runtime.CompilerServices; +#pragma warning disable 8500 // taking address of managed type + namespace System.Globalization { internal sealed partial class CalendarData @@ -265,7 +267,7 @@ private static unsafe bool CallEnumCalendarInfo(string localeName, CalendarId ca } // Now call the enumeration API. Work is done by our callback function - Interop.Kernel32.EnumCalendarInfoExEx(&EnumCalendarInfoCallback, localeName, (uint)calendar, null, calType, Unsafe.AsPointer(ref context)); + Interop.Kernel32.EnumCalendarInfoExEx(&EnumCalendarInfoCallback, localeName, (uint)calendar, null, calType, &context); // Now we have a list of data, fail if we didn't find anything. Debug.Assert(context.strings != null); @@ -417,7 +419,7 @@ private static int NlsGetCalendars(string localeName, bool useUserOverride, Cale unsafe { - Interop.Kernel32.EnumCalendarInfoExEx(&EnumCalendarsCallback, localeName, ENUM_ALL_CALENDARS, null, CAL_ICALINTVALUE, Unsafe.AsPointer(ref data)); + Interop.Kernel32.EnumCalendarInfoExEx(&EnumCalendarsCallback, localeName, ENUM_ALL_CALENDARS, null, CAL_ICALINTVALUE, &data); } // Copy to the output array diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CharUnicodeInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CharUnicodeInfo.cs index 2ea405507f801a..0e391c2ad7daca 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CharUnicodeInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CharUnicodeInfo.cs @@ -245,7 +245,7 @@ private static double GetNumericValueNoBoundsCheck(uint codePoint) { ulong temp = Unsafe.ReadUnaligned(ref refToValue); temp = BinaryPrimitives.ReverseEndianness(temp); - return Unsafe.As(ref temp); + return BitConverter.UInt64BitsToDouble(temp); } } diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.Nls.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.Nls.cs index 0f6d728c5e96ca..a32baa99ecbe14 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.Nls.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.Nls.cs @@ -8,6 +8,8 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +#pragma warning disable 8500 // taking address of managed type + namespace System.Globalization { internal sealed partial class CultureData @@ -123,7 +125,7 @@ private int NlsGetFirstDayOfWeek() unsafe { - Interop.Kernel32.EnumSystemLocalesEx(&EnumSystemLocalesProc, Interop.Kernel32.LOCALE_SPECIFICDATA | Interop.Kernel32.LOCALE_SUPPLEMENTAL, Unsafe.AsPointer(ref context), IntPtr.Zero); + Interop.Kernel32.EnumSystemLocalesEx(&EnumSystemLocalesProc, Interop.Kernel32.LOCALE_SPECIFICDATA | Interop.Kernel32.LOCALE_SUPPLEMENTAL, &context, IntPtr.Zero); } if (context.cultureName != null) @@ -347,14 +349,14 @@ private struct EnumLocaleData [UnmanagedCallersOnly] private static unsafe Interop.BOOL EnumSystemLocalesProc(char* lpLocaleString, uint flags, void* contextHandle) { - ref EnumLocaleData context = ref Unsafe.As(ref *(byte*)contextHandle); + EnumLocaleData* context = (EnumLocaleData*)contextHandle; try { string cultureName = new string(lpLocaleString); string? regionName = GetLocaleInfoEx(cultureName, Interop.Kernel32.LOCALE_SISO3166CTRYNAME); - if (regionName != null && regionName.Equals(context.regionName, StringComparison.OrdinalIgnoreCase)) + if (regionName != null && regionName.Equals(context->regionName, StringComparison.OrdinalIgnoreCase)) { - context.cultureName = cultureName; + context->cultureName = cultureName; return Interop.BOOL.FALSE; // we found a match, then stop the enumeration } @@ -370,10 +372,9 @@ private static unsafe Interop.BOOL EnumSystemLocalesProc(char* lpLocaleString, u [UnmanagedCallersOnly] private static unsafe Interop.BOOL EnumAllSystemLocalesProc(char* lpLocaleString, uint flags, void* contextHandle) { - ref EnumData context = ref Unsafe.As(ref *(byte*)contextHandle); try { - context.strings.Add(new string(lpLocaleString)); + ((EnumData*)contextHandle)->strings.Add(new string(lpLocaleString)); return Interop.BOOL.TRUE; } catch (Exception) @@ -392,10 +393,9 @@ private struct EnumData [UnmanagedCallersOnly] private static unsafe Interop.BOOL EnumTimeCallback(char* lpTimeFormatString, void* lParam) { - ref EnumData context = ref Unsafe.As(ref *(byte*)lParam); try { - context.strings.Add(new string(lpTimeFormatString)); + ((EnumData*)lParam)->strings.Add(new string(lpTimeFormatString)); return Interop.BOOL.TRUE; } catch (Exception) @@ -410,7 +410,7 @@ private static unsafe Interop.BOOL EnumTimeCallback(char* lpTimeFormatString, vo data.strings = new List(); // Now call the enumeration API. Work is done by our callback function - Interop.Kernel32.EnumTimeFormatsEx(&EnumTimeCallback, localeName, dwFlags, Unsafe.AsPointer(ref data)); + Interop.Kernel32.EnumTimeFormatsEx(&EnumTimeCallback, localeName, dwFlags, &data); if (data.strings.Count > 0) { @@ -496,7 +496,7 @@ private static CultureInfo[] NlsEnumCultures(CultureTypes types) unsafe { - Interop.Kernel32.EnumSystemLocalesEx(&EnumAllSystemLocalesProc, flags, Unsafe.AsPointer(ref context), IntPtr.Zero); + Interop.Kernel32.EnumSystemLocalesEx(&EnumAllSystemLocalesProc, flags, &context, IntPtr.Zero); } CultureInfo[] cultures = new CultureInfo[context.strings.Count]; @@ -524,7 +524,7 @@ internal bool NlsIsReplacementCulture unsafe { - Interop.Kernel32.EnumSystemLocalesEx(&EnumAllSystemLocalesProc, Interop.Kernel32.LOCALE_REPLACEMENT, Unsafe.AsPointer(ref context), IntPtr.Zero); + Interop.Kernel32.EnumSystemLocalesEx(&EnumAllSystemLocalesProc, Interop.Kernel32.LOCALE_REPLACEMENT, &context, IntPtr.Zero); } for (int i = 0; i < context.strings.Count; i++) diff --git a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/Unsafe.cs b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/Unsafe.cs index 9b2891bc2ebe7b..c4c3f669819544 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/Unsafe.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/Unsafe.cs @@ -12,7 +12,7 @@ // In AOT compilers, see Internal.IL.Stubs.UnsafeIntrinsics for details. // -#pragma warning disable 8500 // sizeof of managed types +#pragma warning disable 8500 // address / sizeof of managed types namespace System.Runtime.CompilerServices { @@ -501,7 +501,7 @@ public static T ReadUnaligned(void* source) typeof(T).ToString(); // Type token used by the actual method body throw new PlatformNotSupportedException(); #else - return Unsafe.As(ref *(byte*)source); + return *(T*)source; #endif // ldarg.0 @@ -525,7 +525,7 @@ public static T ReadUnaligned(ref byte source) typeof(T).ToString(); // Type token used by the actual method body throw new PlatformNotSupportedException(); #else - return Unsafe.As(ref source); + return As(ref source); #endif // ldarg.0 @@ -550,7 +550,7 @@ public static void WriteUnaligned(void* destination, T value) typeof(T).ToString(); // Type token used by the actual method body throw new PlatformNotSupportedException(); #else - Unsafe.As(ref *(byte*)destination) = value; + *(T*)destination = value; #endif // ldarg .0 @@ -575,7 +575,7 @@ public static void WriteUnaligned(ref byte destination, T value) typeof(T).ToString(); // Type token used by the actual method body throw new PlatformNotSupportedException(); #else - Unsafe.As(ref destination) = value; + As(ref destination) = value; #endif // ldarg .0 @@ -608,54 +608,35 @@ public static ref T AddByteOffset(ref T source, IntPtr byteOffset) /// /// Reads a value of type from the given location. /// - //[Intrinsic] - // AOT:Read [NonVersionable] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static T Read(void* source) { - return Unsafe.As(ref *(byte*)source); - - // ldarg.0 - // ldobj !!T - // ret + return *(T*)source; } /// /// Writes a value of type to the given location. /// - //[Intrinsic] - // AOT:Write [NonVersionable] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Write(void* destination, T value) { - Unsafe.As(ref *(byte*)destination) = value; - - // ldarg .0 - // ldarg .1 - // stobj !!T - // ret + *(T*)destination = value; } /// /// Reinterprets the given location as a reference to a value of type . /// [Intrinsic] - // CoreCLR:METHOD__UNSAFE__AS_REF_POINTER - // AOT:AsRef - // Mono:AsRef [NonVersionable] [CLSCompliant(false)] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ref T AsRef(void* source) { - return ref Unsafe.As(ref *(byte*)source); - - // ldarg .0 - // ret + return ref *(T*)source; } /// @@ -705,7 +686,7 @@ public static IntPtr ByteOffset([AllowNull] ref T origin, [AllowNull] ref T t [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ref T NullRef() { - return ref Unsafe.AsRef(null); + return ref AsRef(null); // ldc.i4.0 // conv.u @@ -725,7 +706,7 @@ public static ref T NullRef() [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsNullRef(ref T source) { - return Unsafe.AsPointer(ref source) == null; + return AsPointer(ref source) == null; // ldarg.0 // ldc.i4.0 diff --git a/src/libraries/System.Private.CoreLib/src/System/Span.cs b/src/libraries/System.Private.CoreLib/src/System/Span.cs index ce0f2a28012915..8fd00d74c7c837 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Span.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Span.cs @@ -10,8 +10,7 @@ using EditorBrowsableState = System.ComponentModel.EditorBrowsableState; #pragma warning disable 0809 //warning CS0809: Obsolete member 'Span.Equals(object)' overrides non-obsolete member 'object.Equals(object)' - -#pragma warning disable 8500 // sizeof of managed types +#pragma warning disable 8500 // address / sizeof of managed types namespace System { @@ -113,7 +112,7 @@ public unsafe Span(void* pointer, int length) if (length < 0) ThrowHelper.ThrowArgumentOutOfRangeException(); - _reference = ref Unsafe.As(ref *(byte*)pointer); + _reference = ref *(T*)pointer; _length = length; } @@ -305,7 +304,7 @@ public unsafe void Fill(T value) // The runtime eventually calls memset, which can efficiently support large buffers. // We don't need to check IsReferenceOrContainsReferences because no references // can ever be stored in types this small. - Unsafe.InitBlockUnaligned(ref Unsafe.As(ref _reference), Unsafe.As(ref value), (uint)_length); + Unsafe.InitBlockUnaligned(ref Unsafe.As(ref _reference), *(byte*)&value, (uint)_length); } else { diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/OpenSslX509ChainProcessor.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/OpenSslX509ChainProcessor.cs index f67edc910c290e..07eb0037118078 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/OpenSslX509ChainProcessor.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/OpenSslX509ChainProcessor.cs @@ -16,6 +16,8 @@ using X509VerifyStatusCodeUniversal = Interop.Crypto.X509VerifyStatusCodeUniversal; +#pragma warning disable 8500 // taking address of managed type + namespace System.Security.Cryptography.X509Certificates { internal sealed class OpenSslX509ChainProcessor : IChainPal @@ -647,11 +649,7 @@ private static unsafe int VerifyCallback(int ok, IntPtr ctx) { using (var storeCtx = new SafeX509StoreCtxHandle(ctx, ownsHandle: false)) { - void* appData = Interop.Crypto.X509StoreCtxGetAppData(storeCtx); - - ref WorkingChain workingChain = ref Unsafe.As(ref *(byte*)appData); - - return workingChain.VerifyCallback(storeCtx); + return ((WorkingChain*)Interop.Crypto.X509StoreCtxGetAppData(storeCtx))->VerifyCallback(storeCtx); } } catch @@ -672,7 +670,7 @@ private unsafe WorkingChain BuildWorkingChain() Interop.Crypto.X509StoreCtxReset(_storeCtx); - Interop.Crypto.X509StoreCtxSetVerifyCallback(_storeCtx, &VerifyCallback, Unsafe.AsPointer(ref workingChain)); + Interop.Crypto.X509StoreCtxSetVerifyCallback(_storeCtx, &VerifyCallback, &workingChain); bool verify = Interop.Crypto.X509VerifyCert(_storeCtx); @@ -685,7 +683,7 @@ private unsafe WorkingChain BuildWorkingChain() extraDispose = workingChain; workingChain = new WorkingChain(abortOnSignatureError: false); - Interop.Crypto.X509StoreCtxSetVerifyCallback(_storeCtx, &VerifyCallback, Unsafe.AsPointer(ref workingChain)); + Interop.Crypto.X509StoreCtxSetVerifyCallback(_storeCtx, &VerifyCallback, &workingChain); verify = Interop.Crypto.X509VerifyCert(_storeCtx); } diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/StorePal.Android.AndroidKeyStore.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/StorePal.Android.AndroidKeyStore.cs index 6952881cd4dfc2..59c70f4ae78c46 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/StorePal.Android.AndroidKeyStore.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/StorePal.Android.AndroidKeyStore.cs @@ -6,6 +6,8 @@ using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; +#pragma warning disable 8500 // taking address of managed type + namespace System.Security.Cryptography.X509Certificates { internal sealed partial class StorePal @@ -101,7 +103,7 @@ public void CloneTo(X509Certificate2Collection collection) bool success = Interop.AndroidCrypto.X509StoreEnumerateCertificates( _keyStoreHandle, &EnumCertificatesCallback, - Unsafe.AsPointer(ref context)); + &context); if (!success) { throw new CryptographicException(SR.Cryptography_X509_StoreEnumerateFailure); @@ -127,7 +129,9 @@ private struct EnumCertificatesContext [UnmanagedCallersOnly] private static unsafe void EnumCertificatesCallback(void* certPtr, void* privateKeyPtr, Interop.AndroidCrypto.PAL_KeyAlgorithm privateKeyAlgorithm, void* context) { - ref EnumCertificatesContext callbackContext = ref Unsafe.As(ref *(byte*)context); +#pragma warning disable 8500 // taking address of managed type + EnumCertificatesContext* callbackContext = (EnumCertificatesContext*)context; +#pragma warning restore 8500 AndroidCertificatePal certPal; var handle = new SafeX509Handle((IntPtr)certPtr); @@ -148,7 +152,7 @@ private static unsafe void EnumCertificatesCallback(void* certPtr, void* private } var cert = new X509Certificate2(certPal); - if (!callbackContext.Results.Add(cert)) + if (!callbackContext->Results.Add(cert)) cert.Dispose(); } } diff --git a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/StorePal.Android.TrustedStore.cs b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/StorePal.Android.TrustedStore.cs index d6089421860a9f..dc86f467b9e6bc 100644 --- a/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/StorePal.Android.TrustedStore.cs +++ b/src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/X509Certificates/StorePal.Android.TrustedStore.cs @@ -6,6 +6,8 @@ using System.Runtime.InteropServices; using Microsoft.Win32.SafeHandles; +#pragma warning disable 8500 // taking address of managed type + namespace System.Security.Cryptography.X509Certificates { internal sealed partial class StorePal @@ -35,7 +37,7 @@ public void Remove(ICertificatePal cert) throw new CryptographicException(SR.Cryptography_X509_StoreReadOnly); } - public void CloneTo(X509Certificate2Collection collection) + public unsafe void CloneTo(X509Certificate2Collection collection) { EnumCertificatesContext context = default; context.Results = new HashSet(); @@ -46,7 +48,7 @@ public void CloneTo(X509Certificate2Collection collection) bool success = Interop.AndroidCrypto.X509StoreEnumerateTrustedCertificates( (byte)(systemOnly ? 1 : 0), &EnumCertificatesCallback, - Unsafe.AsPointer(ref context)); + &context); if (!success) { throw new CryptographicException(SR.Cryptography_X509_StoreEnumerateFailure); @@ -67,10 +69,10 @@ private struct EnumCertificatesContext [UnmanagedCallersOnly] private static unsafe void EnumCertificatesCallback(void* certPtr, void* context) { - ref EnumCertificatesContext callbackContext = ref Unsafe.As(ref *(byte*)context); + EnumCertificatesContext* callbackContext = (EnumCertificatesContext*)context; var handle = new SafeX509Handle((IntPtr)certPtr); var cert = new X509Certificate2(new AndroidCertificatePal(handle)); - if (!callbackContext.Results.Add(cert)) + if (!callbackContext->Results.Add(cert)) cert.Dispose(); } } diff --git a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs index 03f6f1711dd76b..9f571702c6d8ae 100644 --- a/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs +++ b/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverter.cs @@ -283,7 +283,7 @@ internal override T ReadAsPropertyNameCore(ref Utf8JsonReader reader, Type typeT return value; } - internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, T value, JsonSerializerOptions options, bool isWritingExtensionDataProperty) + internal override unsafe void WriteAsPropertyNameCore(Utf8JsonWriter writer, T value, JsonSerializerOptions options, bool isWritingExtensionDataProperty) { ulong key = ConvertToUInt64(value); @@ -322,36 +322,38 @@ internal override void WriteAsPropertyNameCore(Utf8JsonWriter writer, T value, J return; } +#pragma warning disable 8500 // address of managed type switch (s_enumTypeCode) { case TypeCode.Int32: - writer.WritePropertyName(Unsafe.As(ref value)); + writer.WritePropertyName(*(int*)&value); break; case TypeCode.UInt32: - writer.WritePropertyName(Unsafe.As(ref value)); + writer.WritePropertyName(*(uint*)&value); break; case TypeCode.UInt64: - writer.WritePropertyName(Unsafe.As(ref value)); + writer.WritePropertyName(*(ulong*)&value); break; case TypeCode.Int64: - writer.WritePropertyName(Unsafe.As(ref value)); + writer.WritePropertyName(*(long*)&value); break; case TypeCode.Int16: - writer.WritePropertyName(Unsafe.As(ref value)); + writer.WritePropertyName(*(short*)&value); break; case TypeCode.UInt16: - writer.WritePropertyName(Unsafe.As(ref value)); + writer.WritePropertyName(*(ushort*)&value); break; case TypeCode.Byte: - writer.WritePropertyName(Unsafe.As(ref value)); + writer.WritePropertyName(*(byte*)&value); break; case TypeCode.SByte: - writer.WritePropertyName(Unsafe.As(ref value)); + writer.WritePropertyName(*(sbyte*)&value); break; default: ThrowHelper.ThrowJsonException(); break; } +#pragma warning restore 8500 } #if NETCOREAPP