From 5702017a18ca93cdd37d99e09cea274f87e04ea4 Mon Sep 17 00:00:00 2001 From: Gregg Miskelly Date: Fri, 29 Jul 2022 11:49:12 -0700 Subject: [PATCH] Move System.ValueType.s_seed into System.Runtime.CompilerServices.RuntimeHelpers PR https://github.com/dotnet/runtime/pull/69723 added a 's_seed' field to System.ValueType. This causes the debugger to show all structs as having static members. This PR moves the field and the function that used it into RuntimeHelpers. --- .../src/System/ArgIterator.cs | 2 +- .../src/System/MulticastDelegate.cs | 2 +- .../src/System/Reflection/MdImport.cs | 2 +- .../Reflection/RuntimeMethodInfo.CoreCLR.cs | 2 +- .../RuntimeHelpers.CoreCLR.cs | 28 ++++++++++++++++++ .../src/System/RuntimeHandles.cs | 4 +-- .../src/System/ValueType.cs | 29 ------------------- 7 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/ArgIterator.cs b/src/coreclr/System.Private.CoreLib/src/System/ArgIterator.cs index 10ede24a6573c5..37a75996c0322d 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/ArgIterator.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/ArgIterator.cs @@ -123,7 +123,7 @@ public unsafe RuntimeTypeHandle GetNextArgType() public override int GetHashCode() { - return ValueType.GetHashCodeOfPtr(ArgCookie); + return RuntimeHelpers.GetHashCodeOfPtr(ArgCookie); } // Inherited from object diff --git a/src/coreclr/System.Private.CoreLib/src/System/MulticastDelegate.cs b/src/coreclr/System.Private.CoreLib/src/System/MulticastDelegate.cs index 259bfc90070cf2..4746430b5139fb 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/MulticastDelegate.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/MulticastDelegate.cs @@ -460,7 +460,7 @@ public sealed override Delegate[] GetInvocationList() public sealed override int GetHashCode() { if (IsUnmanagedFunctionPtr()) - return ValueType.GetHashCodeOfPtr(_methodPtr) ^ ValueType.GetHashCodeOfPtr(_methodPtrAux); + return RuntimeHelpers.GetHashCodeOfPtr(_methodPtr) ^ RuntimeHelpers.GetHashCodeOfPtr(_methodPtrAux); if (_invocationCount != (IntPtr)0) { diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/MdImport.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/MdImport.cs index cdc15dc8fb6e08..f945f3c7352e94 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/MdImport.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/MdImport.cs @@ -206,7 +206,7 @@ internal readonly struct MetadataImport public override int GetHashCode() { - return ValueType.GetHashCodeOfPtr(m_metadataImport2); + return RuntimeHelpers.GetHashCodeOfPtr(m_metadataImport2); } public override bool Equals(object? obj) diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.CoreCLR.cs index 229e2981bb6f25..8878b27db60dde 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.CoreCLR.cs @@ -161,7 +161,7 @@ public override int GetHashCode() { // See RuntimeMethodInfo.Equals() below. if (IsGenericMethod) - return ValueType.GetHashCodeOfPtr(m_handle); + return RuntimeHelpers.GetHashCodeOfPtr(m_handle); else return base.GetHashCode(); } diff --git a/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs index 196f43f4f63f8a..9465e62cba063b 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs @@ -8,11 +8,14 @@ using System.Runtime.InteropServices; using System.Runtime.Serialization; using System.Runtime.Versioning; +using System.Threading; namespace System.Runtime.CompilerServices { public static partial class RuntimeHelpers { + private static int s_pointerHashSeed; + [Intrinsic] [MethodImpl(MethodImplOptions.InternalCall)] public static extern void InitializeArray(Array array, RuntimeFieldHandle fldHandle); @@ -397,6 +400,31 @@ public GCFrameRegistration(void* allocation, uint elemCount, bool areByRefs = tr [MethodImpl(MethodImplOptions.InternalCall)] internal static extern unsafe void UnregisterForGCReporting(GCFrameRegistration* pRegistration); + + internal static int GetHashCodeOfPtr(IntPtr ptr) + { + int hashCode = (int)ptr; + + if (hashCode == 0) + { + return 0; + } + + int seed = s_pointerHashSeed; + + // Initialize s_pointerHashSeed lazily + if (seed == 0) + { + // We use the first non-0 pointer as the seed, all hashcodes will be based off that. + // This is to make sure that we only reveal relative memory addresses and never absolute ones. + seed = hashCode; + Interlocked.CompareExchange(ref s_pointerHashSeed, seed, 0); + seed = s_pointerHashSeed; + } + + Debug.Assert(s_pointerHashSeed != 0); + return hashCode - seed; + } } // Helper class to assist with unsafe pinning of arbitrary objects. // It's used by VM code. diff --git a/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs b/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs index 49b1b5ac0f7f13..6b7d2425135cc0 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs @@ -873,7 +873,7 @@ public void GetObjectData(SerializationInfo info, StreamingContext context) public override int GetHashCode() { - return ValueType.GetHashCodeOfPtr(Value); + return RuntimeHelpers.GetHashCodeOfPtr(Value); } public override bool Equals(object? obj) @@ -1226,7 +1226,7 @@ internal bool IsNullHandle() public override int GetHashCode() { - return ValueType.GetHashCodeOfPtr(Value); + return RuntimeHelpers.GetHashCodeOfPtr(Value); } public override bool Equals(object? obj) diff --git a/src/coreclr/System.Private.CoreLib/src/System/ValueType.cs b/src/coreclr/System.Private.CoreLib/src/System/ValueType.cs index 64b547b699fb7f..880de9ac6af953 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/ValueType.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/ValueType.cs @@ -10,11 +10,9 @@ ** ===========================================================*/ -using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Runtime.CompilerServices; -using System.Threading; namespace System { @@ -84,33 +82,6 @@ ref RuntimeHelpers.GetRawData(obj), [MethodImpl(MethodImplOptions.InternalCall)] public extern override int GetHashCode(); - private static int s_seed; - - internal static int GetHashCodeOfPtr(IntPtr ptr) - { - int hashCode = (int)ptr; - - if (hashCode == 0) - { - return 0; - } - - int seed = s_seed; - - // Initialize s_seed lazily - if (seed == 0) - { - // We use the first non-0 pointer as the seed, all hashcodes will be based off that. - // This is to make sure that we only reveal relative memory addresses and never absolute ones. - seed = hashCode; - Interlocked.CompareExchange(ref s_seed, seed, 0); - seed = s_seed; - } - - Debug.Assert(s_seed != 0); - return hashCode - seed; - } - public override string? ToString() { return this.GetType().ToString();