From 798f9ef5eb592e3db8acf26673f3ace108960221 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Sat, 6 May 2023 06:50:43 -0700 Subject: [PATCH] Replace GetInternalValue methods on RuntimeHandles with public equivalents (#85861) --- .../System.Private.CoreLib/src/System/GC.CoreCLR.cs | 2 +- .../CompilerServices/RuntimeHelpers.CoreCLR.cs | 2 +- .../src/System/RuntimeHandles.cs | 11 +---------- src/coreclr/jit/importercalls.cpp | 8 ++++---- src/coreclr/jit/namedintrinsiclist.h | 2 +- .../src/Internal/Runtime/Augments/RuntimeAugments.cs | 5 ----- .../src/System/RuntimeTypeHandle.cs | 8 -------- .../Reflection/Execution/RuntimeHandlesExtensions.cs | 4 +--- .../Runtime/TypeLoader/TypeSystemExtensions.cs | 16 ++++------------ .../Test.CoreLib/src/System/RuntimeTypeHandle.cs | 2 +- .../nativeaot/Test.CoreLib/src/System/Type.cs | 2 +- src/coreclr/vm/corelib.h | 4 ++-- src/coreclr/vm/ilmarshalers.cpp | 20 ++++++++++---------- src/coreclr/vm/methodtable.cpp | 2 +- src/coreclr/vm/virtualcallstub.cpp | 2 +- 15 files changed, 29 insertions(+), 61 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs index 3bd17b2..c68bc6a 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs @@ -681,7 +681,7 @@ namespace System if (pinned) flags |= GC_ALLOC_FLAGS.GC_ALLOC_PINNED_OBJECT_HEAP; - return Unsafe.As(AllocateNewArray(typeof(T[]).TypeHandle.Value, length, flags)); + return Unsafe.As(AllocateNewArray(RuntimeTypeHandle.ToIntPtr(typeof(T[]).TypeHandle), length, flags)); } } 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 1b35082..7a44b24 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 @@ -664,7 +664,7 @@ namespace System.Runtime.CompilerServices [MethodImpl(MethodImplOptions.AggressiveInlining)] public static TypeHandle TypeHandleOf() { - return new TypeHandle((void*)RuntimeTypeHandle.GetValueInternal(typeof(T).TypeHandle)); + return new TypeHandle((void*)RuntimeTypeHandle.ToIntPtr(typeof(T).TypeHandle)); } } diff --git a/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs b/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs index bc3b9cb..38125e8 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs @@ -52,6 +52,7 @@ namespace System /// /// A object to retrieve an internal pointer representation from. /// An object that represents a object. + [Intrinsic] public static IntPtr ToIntPtr(RuntimeTypeHandle value) => value.Value; public static bool operator ==(RuntimeTypeHandle left, object? right) => left.Equals(right); @@ -76,10 +77,6 @@ namespace System public IntPtr Value => m_type?.m_handle ?? 0; - [Intrinsic] - internal static IntPtr GetValueInternal(RuntimeTypeHandle handle) - => handle.m_type?.GetUnderlyingNativeHandle() ?? 0; - internal RuntimeTypeHandle(RuntimeType type) { m_type = type; @@ -773,12 +770,6 @@ namespace System return m_value; } - // Used by EE - private static IntPtr GetValueInternal(RuntimeMethodHandle rmh) - { - return rmh.Value; - } - // ISerializable interface [Obsolete(Obsoletions.LegacyFormatterImplMessage, DiagnosticId = Obsoletions.LegacyFormatterImplDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] [EditorBrowsable(EditorBrowsableState.Never)] diff --git a/src/coreclr/jit/importercalls.cpp b/src/coreclr/jit/importercalls.cpp index e80f210..993b284 100644 --- a/src/coreclr/jit/importercalls.cpp +++ b/src/coreclr/jit/importercalls.cpp @@ -2589,7 +2589,7 @@ GenTree* Compiler::impIntrinsic(GenTree* newobjThis, case NI_System_Runtime_CompilerServices_RuntimeHelpers_IsKnownConstant: // We need these to be able to fold "typeof(...) == typeof(...)" - case NI_System_RuntimeTypeHandle_GetValueInternal: + case NI_System_RuntimeTypeHandle_ToIntPtr: case NI_System_Type_GetTypeFromHandle: case NI_System_Type_op_Equality: case NI_System_Type_op_Inequality: @@ -2967,7 +2967,7 @@ GenTree* Compiler::impIntrinsic(GenTree* newobjThis, return lengthField; } - case NI_System_RuntimeTypeHandle_GetValueInternal: + case NI_System_RuntimeTypeHandle_ToIntPtr: { GenTree* op1 = impStackTop(0).val; if (op1->gtOper == GT_CALL && (op1->AsCall()->gtCallType == CT_HELPER) && @@ -8112,9 +8112,9 @@ NamedIntrinsic Compiler::lookupNamedIntrinsic(CORINFO_METHOD_HANDLE method) } else if (strcmp(className, "RuntimeTypeHandle") == 0) { - if (strcmp(methodName, "GetValueInternal") == 0) + if (strcmp(methodName, "ToIntPtr") == 0) { - result = NI_System_RuntimeTypeHandle_GetValueInternal; + result = NI_System_RuntimeTypeHandle_ToIntPtr; } } break; diff --git a/src/coreclr/jit/namedintrinsiclist.h b/src/coreclr/jit/namedintrinsiclist.h index 2e3f1f0..1072528 100644 --- a/src/coreclr/jit/namedintrinsiclist.h +++ b/src/coreclr/jit/namedintrinsiclist.h @@ -77,7 +77,7 @@ enum NamedIntrinsic : unsigned short NI_System_Array_GetUpperBound, NI_System_Object_MemberwiseClone, NI_System_Object_GetType, - NI_System_RuntimeTypeHandle_GetValueInternal, + NI_System_RuntimeTypeHandle_ToIntPtr, NI_System_StubHelpers_GetStubContext, NI_System_StubHelpers_NextCallReturnAddress, diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs index 74c7570..b5af919 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/Augments/RuntimeAugments.cs @@ -778,11 +778,6 @@ namespace Internal.Runtime.Augments return modulePath; } - public static IntPtr GetRuntimeTypeHandleRawValue(RuntimeTypeHandle runtimeTypeHandle) - { - return runtimeTypeHandle.RawValue; - } - // if functionPointer points at an import or unboxing stub, find the target of the stub public static IntPtr GetCodeTarget(IntPtr functionPointer) { diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeTypeHandle.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeTypeHandle.cs index 526362d..f3f124a 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeTypeHandle.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/RuntimeTypeHandle.cs @@ -154,14 +154,6 @@ namespace System } } - internal IntPtr RawValue - { - get - { - return _value; - } - } - private IntPtr _value; } } diff --git a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/RuntimeHandlesExtensions.cs b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/RuntimeHandlesExtensions.cs index be21357..a1597d9 100644 --- a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/RuntimeHandlesExtensions.cs +++ b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/RuntimeHandlesExtensions.cs @@ -10,8 +10,6 @@ namespace Internal.Reflection.Execution internal static class RuntimeHandlesExtensions { public static bool IsNull(this RuntimeTypeHandle rtth) - { - return RuntimeAugments.GetRuntimeTypeHandleRawValue(rtth) == IntPtr.Zero; - } + => RuntimeTypeHandle.ToIntPtr(rtth) == 0; } } diff --git a/src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/TypeSystemExtensions.cs b/src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/TypeSystemExtensions.cs index 6ad05df..a50e1eb 100644 --- a/src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/TypeSystemExtensions.cs +++ b/src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Runtime/TypeLoader/TypeSystemExtensions.cs @@ -50,20 +50,12 @@ namespace Internal.TypeSystem internal static class RuntimeHandleExtensions { public static bool IsNull(this RuntimeTypeHandle rtth) - { - return RuntimeAugments.GetRuntimeTypeHandleRawValue(rtth) == IntPtr.Zero; - } + => RuntimeTypeHandle.ToIntPtr(rtth) == 0; public static unsafe bool IsDynamic(this RuntimeFieldHandle rtfh) - { - IntPtr rtfhValue = *(IntPtr*)&rtfh; - return (rtfhValue.ToInt64() & 0x1) == 0x1; - } + => (RuntimeFieldHandle.ToIntPtr(rtfh) & 1) != 0; - public static unsafe bool IsDynamic(this RuntimeMethodHandle rtfh) - { - IntPtr rtfhValue = *(IntPtr*)&rtfh; - return (rtfhValue.ToInt64() & 0x1) == 0x1; - } + public static unsafe bool IsDynamic(this RuntimeMethodHandle rtmh) + => (RuntimeMethodHandle.ToIntPtr(rtmh) & 1) != 0; } } diff --git a/src/coreclr/nativeaot/Test.CoreLib/src/System/RuntimeTypeHandle.cs b/src/coreclr/nativeaot/Test.CoreLib/src/System/RuntimeTypeHandle.cs index 4519321..d921e27 100644 --- a/src/coreclr/nativeaot/Test.CoreLib/src/System/RuntimeTypeHandle.cs +++ b/src/coreclr/nativeaot/Test.CoreLib/src/System/RuntimeTypeHandle.cs @@ -18,7 +18,7 @@ namespace System } [Intrinsic] - internal static unsafe IntPtr GetValueInternal(RuntimeTypeHandle handle) + internal static unsafe IntPtr ToIntPtr(RuntimeTypeHandle handle) { return (IntPtr)handle._pEEType.ToPointer(); } diff --git a/src/coreclr/nativeaot/Test.CoreLib/src/System/Type.cs b/src/coreclr/nativeaot/Test.CoreLib/src/System/Type.cs index f038c88..09c47b8 100644 --- a/src/coreclr/nativeaot/Test.CoreLib/src/System/Type.cs +++ b/src/coreclr/nativeaot/Test.CoreLib/src/System/Type.cs @@ -29,7 +29,7 @@ namespace System [Intrinsic] public static bool operator ==(Type left, Type right) { - return RuntimeTypeHandle.GetValueInternal(left._typeHandle) == RuntimeTypeHandle.GetValueInternal(right._typeHandle); + return RuntimeTypeHandle.ToIntPtr(left._typeHandle) == RuntimeTypeHandle.ToIntPtr(right._typeHandle); } [Intrinsic] diff --git a/src/coreclr/vm/corelib.h b/src/coreclr/vm/corelib.h index e325d8e..ed41cec 100644 --- a/src/coreclr/vm/corelib.h +++ b/src/coreclr/vm/corelib.h @@ -353,7 +353,7 @@ DEFINE_METHOD(THREAD_START_EXCEPTION,EX_CTOR, .ctor, DEFINE_CLASS(TYPE_HANDLE, System, RuntimeTypeHandle) DEFINE_CLASS(RT_TYPE_HANDLE, System, RuntimeTypeHandle) DEFINE_METHOD(RT_TYPE_HANDLE, PVOID_CTOR, .ctor, IM_RuntimeType_RetVoid) -DEFINE_METHOD(RT_TYPE_HANDLE, GETVALUEINTERNAL, GetValueInternal, SM_RuntimeTypeHandle_RetIntPtr) +DEFINE_METHOD(RT_TYPE_HANDLE, TO_INTPTR, ToIntPtr, SM_RuntimeTypeHandle_RetIntPtr) #ifdef FEATURE_COMINTEROP DEFINE_METHOD(RT_TYPE_HANDLE, ALLOCATECOMOBJECT, AllocateComObject, SM_VoidPtr_RetObj) #endif @@ -543,7 +543,7 @@ DEFINE_CLASS(METHOD_HANDLE_INTERNAL,System, RuntimeMethodHandleI DEFINE_CLASS(METHOD_HANDLE, System, RuntimeMethodHandle) DEFINE_FIELD(METHOD_HANDLE, METHOD, m_value) -DEFINE_METHOD(METHOD_HANDLE, GETVALUEINTERNAL, GetValueInternal, SM_RuntimeMethodHandle_RetIntPtr) +DEFINE_METHOD(METHOD_HANDLE, TO_INTPTR, ToIntPtr, SM_RuntimeMethodHandle_RetIntPtr) DEFINE_CLASS(MISSING, Reflection, Missing) DEFINE_FIELD(MISSING, VALUE, Value) diff --git a/src/coreclr/vm/ilmarshalers.cpp b/src/coreclr/vm/ilmarshalers.cpp index 3aff514..35af997 100644 --- a/src/coreclr/vm/ilmarshalers.cpp +++ b/src/coreclr/vm/ilmarshalers.cpp @@ -1343,7 +1343,7 @@ void ILInterfaceMarshaler::EmitConvertContentsCLRToNative(ILCodeStream* pslILEmi if (itfInfo.thNativeItf.GetMethodTable()) { pslILEmit->EmitLDTOKEN(pslILEmit->GetToken(itfInfo.thNativeItf.GetMethodTable())); - pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__GETVALUEINTERNAL, 1, 1); + pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__TO_INTPTR, 1, 1); } else { @@ -1353,7 +1353,7 @@ void ILInterfaceMarshaler::EmitConvertContentsCLRToNative(ILCodeStream* pslILEmi if (itfInfo.thClass.GetMethodTable()) { pslILEmit->EmitLDTOKEN(pslILEmit->GetToken(itfInfo.thClass.GetMethodTable())); - pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__GETVALUEINTERNAL, 1, 1); + pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__TO_INTPTR, 1, 1); } else { @@ -1380,7 +1380,7 @@ void ILInterfaceMarshaler::EmitConvertContentsNativeToCLR(ILCodeStream* pslILEmi if (itfInfo.thItf.GetMethodTable()) { pslILEmit->EmitLDTOKEN(pslILEmit->GetToken(itfInfo.thItf.GetMethodTable())); - pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__GETVALUEINTERNAL, 1, 1); + pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__TO_INTPTR, 1, 1); } else { @@ -1390,7 +1390,7 @@ void ILInterfaceMarshaler::EmitConvertContentsNativeToCLR(ILCodeStream* pslILEmi if (itfInfo.thClass.GetMethodTable()) { pslILEmit->EmitLDTOKEN(pslILEmit->GetToken(itfInfo.thClass.GetMethodTable())); - pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__GETVALUEINTERNAL, 1, 1); + pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__TO_INTPTR, 1, 1); } else { @@ -2233,7 +2233,7 @@ void ILLayoutClassPtrMarshalerBase::EmitConvertSpaceNativeToCLR(ILCodeStream* ps pslILEmit->EmitBRFALSE(pNullRefLabel); pslILEmit->EmitLDTOKEN(pslILEmit->GetToken(m_pargs->m_pMT)); - pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__GETVALUEINTERNAL, 1, 1); + pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__TO_INTPTR, 1, 1); // static object AllocateInternal(IntPtr typeHandle); pslILEmit->EmitCALL(METHOD__STUBHELPERS__ALLOCATE_INTERNAL, 1, 1); EmitStoreManagedValue(pslILEmit); @@ -3895,7 +3895,7 @@ void ILNativeArrayMarshaler::EmitCreateMngdMarshaler(ILCodeStream* pslILEmit) pslILEmit->EmitLDLOC(m_dwMngdMarshalerLocalNum); pslILEmit->EmitLDTOKEN(pslILEmit->GetToken(mops.methodTable)); - pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__GETVALUEINTERNAL, 1, 1); + pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__TO_INTPTR, 1, 1); DWORD dwFlags = mops.elementType; dwFlags |= (((DWORD)mops.bestfitmapping) << 16); @@ -4517,7 +4517,7 @@ void ILFixedArrayMarshaler::EmitCreateMngdMarshaler(ILCodeStream* pslILEmit) pslILEmit->EmitLDLOC(m_dwMngdMarshalerLocalNum); pslILEmit->EmitLDTOKEN(pslILEmit->GetToken(mops.methodTable)); - pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__GETVALUEINTERNAL, 1, 1); + pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__TO_INTPTR, 1, 1); DWORD dwFlags = mops.elementType; dwFlags |= (((DWORD)mops.bestfitmapping) << 16); @@ -4759,7 +4759,7 @@ void ILSafeArrayMarshaler::EmitCreateMngdMarshaler(ILCodeStream* pslILEmit) pslILEmit->EmitLDLOC(m_dwMngdMarshalerLocalNum); pslILEmit->EmitLDTOKEN(pslILEmit->GetToken(mops.methodTable)); - pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__GETVALUEINTERNAL, 1, 1); + pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__TO_INTPTR, 1, 1); pslILEmit->EmitLDC(m_pargs->m_pMarshalInfo->GetArrayRank()); pslILEmit->EmitLDC(dwFlags); @@ -5039,12 +5039,12 @@ void ILReferenceCustomMarshaler::EmitCreateMngdMarshaler(ILCodeStream* pslILEmit // pslILEmit->EmitLDTOKEN(pslILEmit->GetToken(m_pargs->rcm.m_pMD)); - pslILEmit->EmitCALL(METHOD__METHOD_HANDLE__GETVALUEINTERNAL, 1, 1); + pslILEmit->EmitCALL(METHOD__METHOD_HANDLE__TO_INTPTR, 1, 1); pslILEmit->EmitLDC(m_pargs->rcm.m_paramToken); pslILEmit->EmitLDTOKEN(pslILEmit->GetToken(TypeHandle::FromPtr(m_pargs->rcm.m_hndManagedType))); - pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__GETVALUEINTERNAL, 1, 1); + pslILEmit->EmitCALL(METHOD__RT_TYPE_HANDLE__TO_INTPTR, 1, 1); pslILEmit->EmitCALL(METHOD__STUBHELPERS__CREATE_CUSTOM_MARSHALER_HELPER, 3, 1); // arg to CreateMarshaler diff --git a/src/coreclr/vm/methodtable.cpp b/src/coreclr/vm/methodtable.cpp index d40eb08..a2d8f66 100644 --- a/src/coreclr/vm/methodtable.cpp +++ b/src/coreclr/vm/methodtable.cpp @@ -693,7 +693,7 @@ PTR_MethodTable InterfaceInfo_t::GetApproxMethodTable(Module * pContainingModule #ifdef FEATURE_ICASTABLE // In case of ICastable, instead of trying to find method implementation in the real object type - // we call pObj.GetValueInternal() and call GetMethodDescForInterfaceMethod() again with whatever type it returns. + // we call GetMethodDescForInterfaceMethod() again with whatever type it returns. // It allows objects that implement ICastable to mimic behavior of other types. if (pServerMT->IsICastable() && !pItfMD->HasMethodInstantiation() && diff --git a/src/coreclr/vm/virtualcallstub.cpp b/src/coreclr/vm/virtualcallstub.cpp index 32a9637..5bcdbc2 100644 --- a/src/coreclr/vm/virtualcallstub.cpp +++ b/src/coreclr/vm/virtualcallstub.cpp @@ -1999,7 +1999,7 @@ VirtualCallStubManager::Resolver( GCStress::MaybeTrigger(); // In case of ICastable, instead of trying to find method implementation in the real object type - // we call pObj.GetValueInternal() and call Resolver() again with whatever type it returns. + // we call Resolver() again with whatever type it returns. // It allows objects that implement ICastable to mimic behavior of other types. MethodTable * pTokenMT = GetTypeFromToken(token); -- 2.7.4