Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move GetRefAny (with FCThrow) to managed #110344

Merged
merged 7 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,6 +17,21 @@ 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)
am11 marked this conversation as resolved.
Show resolved Hide resolved
{
if (clsHnd != typedByRef._type)
{
ThrowInvalidCastException();
}

return ref typedByRef._value;

[DoesNotReturn]
static void ThrowInvalidCastException() => throw new InvalidCastException();
am11 marked this conversation as resolved.
Show resolved Hide resolved
}

private TypedReference(ref byte target, RuntimeType type)
{
_value = ref target;
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/inc/jithelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 5 additions & 2 deletions src/coreclr/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
18 changes: 0 additions & 18 deletions src/coreclr/vm/jithelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>@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).</TODO>
if (clsHnd != typedByRef.type) {
FCThrow(kInvalidCastException);
}

return(typedByRef.data);
}
HCIMPLEND


/*************************************************************/
HCIMPL2(BOOL, JIT_IsInstanceOfException, CORINFO_CLASS_HANDLE type, Object* obj)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas, there is a QCall ThrowInvalidCastException below this one which uses COMPlusThrowInvalidCastException. Should we:

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ThrowInvalidCastException QCall is fine as is. I do not see what we would gain by "return the intermediate result" or "pass a callback from managed" refactoring. It would just make the code more complicated.

in favor of asm helpers

I am not sure what you mean by this. This QCall is not related to any asm helpers.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

his QCall is not related to any asm helpers.

Ah, sorry I thought COMPlusThrow ends up using C++ exception and confusing it with your comment in earlier PR #109087 (comment).

{
Expand Down
Loading