From 95a18923fcb96924a6aca0e6b53e0efc551c913b Mon Sep 17 00:00:00 2001 From: Adeel Mujahid <3840695+am11@users.noreply.github.com> Date: Wed, 4 Dec 2024 17:04:48 +0200 Subject: [PATCH] Move GetRefAny (with FCThrow) to managed (#110344) * Move GetRefAny (with FCThrow) to managed * Move impl. to TypeReference --- .../src/System/TypedReference.CoreCLR.cs | 17 +++++++++++++++++ src/coreclr/inc/jithelpers.h | 2 +- src/coreclr/vm/corelib.h | 7 +++++-- src/coreclr/vm/jithelpers.cpp | 18 ------------------ 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/coreclr/System.Private.CoreLib/src/System/TypedReference.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/TypedReference.CoreCLR.cs index de5b8d36c5053..5ae74e571295e 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/TypedReference.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/TypedReference.CoreCLR.cs @@ -5,6 +5,7 @@ // These are blob that must be dealt with by the compiler. using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Runtime.Versioning; @@ -16,6 +17,22 @@ public ref partial struct TypedReference private readonly ref byte _value; private readonly IntPtr _type; + // implementation of CORINFO_HELP_GETREFANY + [StackTraceHidden] + internal static ref byte GetRefAny(IntPtr clsHnd, TypedReference typedByRef) + { + if (clsHnd != typedByRef._type) + { + ThrowInvalidCastException(); + } + + return ref typedByRef._value; + + [DoesNotReturn] + [StackTraceHidden] + static void ThrowInvalidCastException() => throw new InvalidCastException(); + } + private TypedReference(ref byte target, RuntimeType type) { _value = ref target; diff --git a/src/coreclr/inc/jithelpers.h b/src/coreclr/inc/jithelpers.h index b163f80ab4941..8b97677ea11f9 100644 --- a/src/coreclr/inc/jithelpers.h +++ b/src/coreclr/inc/jithelpers.h @@ -117,7 +117,7 @@ DYNAMICJITHELPER(CORINFO_HELP_UNBOX_TYPETEST,NULL, METHOD__CASTHELPERS__UNBOX_TYPETEST) DYNAMICJITHELPER(CORINFO_HELP_UNBOX_NULLABLE,NULL, METHOD__CASTHELPERS__UNBOX_NULLABLE) - JITHELPER(CORINFO_HELP_GETREFANY, JIT_GetRefAny, METHOD__NIL) + DYNAMICJITHELPER(CORINFO_HELP_GETREFANY, NULL, METHOD__TYPED_REFERENCE__GETREFANY) DYNAMICJITHELPER(CORINFO_HELP_ARRADDR_ST, NULL, METHOD__CASTHELPERS__STELEMREF) DYNAMICJITHELPER(CORINFO_HELP_LDELEMA_REF, NULL, METHOD__CASTHELPERS__LDELEMAREF) diff --git a/src/coreclr/vm/corelib.h b/src/coreclr/vm/corelib.h index 5f20ad2d3b54f..7a5fe8d99743b 100644 --- a/src/coreclr/vm/corelib.h +++ b/src/coreclr/vm/corelib.h @@ -344,6 +344,9 @@ DEFINE_METHOD(RT_TYPE_HANDLE, ALLOCATECOMOBJECT, AllocateComObject, #endif DEFINE_FIELD(RT_TYPE_HANDLE, M_TYPE, m_type) +// DEFINE_CLASS(TYPED_REFERENCE, System, TypedReference) +DEFINE_METHOD(TYPED_REFERENCE, GETREFANY, GetRefAny, NoSig) + DEFINE_CLASS(TYPE_NAME_RESOLVER, Reflection, TypeNameResolver) DEFINE_METHOD(TYPE_NAME_RESOLVER, GET_TYPE_HELPER, GetTypeHelper, SM_Type_CharPtr_RuntimeAssembly_Bool_Bool_RetRuntimeType) @@ -1178,8 +1181,8 @@ DEFINE_METHOD(CASTHELPERS, UNBOX, Unbox, NoSig) DEFINE_METHOD(CASTHELPERS, STELEMREF, StelemRef, SM_ArrObject_IntPtr_Obj_RetVoid) DEFINE_METHOD(CASTHELPERS, LDELEMAREF, LdelemaRef, SM_ArrObject_IntPtr_PtrVoid_RetRefObj) DEFINE_METHOD(CASTHELPERS, ARRAYTYPECHECK, ArrayTypeCheck, SM_Obj_Array_RetVoid) -DEFINE_METHOD(CASTHELPERS, UNBOX_NULLABLE, Unbox_Nullable, NoSig) -DEFINE_METHOD(CASTHELPERS, UNBOX_TYPETEST, Unbox_TypeTest, NoSig) +DEFINE_METHOD(CASTHELPERS, UNBOX_NULLABLE, Unbox_Nullable, NoSig) +DEFINE_METHOD(CASTHELPERS, UNBOX_TYPETEST, Unbox_TypeTest, NoSig) DEFINE_CLASS(VIRTUALDISPATCHHELPERS, CompilerServices, VirtualDispatchHelpers) DEFINE_METHOD(VIRTUALDISPATCHHELPERS, VIRTUALFUNCTIONPOINTER, VirtualFunctionPointer, NoSig) diff --git a/src/coreclr/vm/jithelpers.cpp b/src/coreclr/vm/jithelpers.cpp index 88bb4981d6572..faa1276e5e3e1 100644 --- a/src/coreclr/vm/jithelpers.cpp +++ b/src/coreclr/vm/jithelpers.cpp @@ -1382,24 +1382,6 @@ HCIMPL2(Object*, JIT_Box, CORINFO_CLASS_HANDLE type, void* unboxedData) } HCIMPLEND -/*************************************************************/ -HCIMPL2_IV(LPVOID, JIT_GetRefAny, CORINFO_CLASS_HANDLE type, TypedByRef typedByRef) -{ - FCALL_CONTRACT; - - TypeHandle clsHnd(type); - // @TODO right now we check for precisely the correct type. - // do we want to allow inheritance? (watch out since value - // classes inherit from object but do not normal object layout). - if (clsHnd != typedByRef.type) { - FCThrow(kInvalidCastException); - } - - return(typedByRef.data); -} -HCIMPLEND - - /*************************************************************/ HCIMPL2(BOOL, JIT_IsInstanceOfException, CORINFO_CLASS_HANDLE type, Object* obj) {