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 type check of MD array to managed #103414

Merged
merged 4 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -462,5 +462,37 @@ private static void StelemRef_Helper_NoCacheLookup(ref object? element, void* el

WriteBarrier(ref element, obj);
}

#if FEATURE_ARRAYSTUB_AS_IL
[DebuggerHidden]
private static unsafe void ArrayTypeCheck(object obj, Array array)
{
Debug.Assert(obj != null);

void* elementType = RuntimeHelpers.GetMethodTable(array)->ElementType;
Debug.Assert(elementType != RuntimeHelpers.GetMethodTable(obj)); // Should be handled by caller

CastResult result = CastCache.TryGet(s_table!, (nuint)RuntimeHelpers.GetMethodTable(obj), (nuint)elementType);
if (result == CastResult.CanCast)
{
return;
}

ArrayTypeCheck_Helper(obj, elementType);
}

[DebuggerHidden]
[MethodImpl(MethodImplOptions.NoInlining)]
private static unsafe void ArrayTypeCheck_Helper(object obj, void* elementType)
{
Debug.Assert(obj != null);

obj = IsInstanceOfAny_NoCacheLookup(elementType, obj);
if (obj == null)
{
ThrowArrayMismatchException();
}
}
#endif
}
}
5 changes: 0 additions & 5 deletions src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1616,11 +1616,6 @@ internal static unsafe void LayoutDestroyNativeInternal(object obj, byte* pNativ
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern IntPtr GetStubContext();

#if FEATURE_ARRAYSTUB_AS_IL
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void ArrayTypeCheck(object o, object[] arr);
#endif

#if FEATURE_MULTICASTSTUB_AS_IL
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern void MulticastDebuggerTraceHelper(object o, int count);
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,7 @@ class ArrayOpLinker : public ILStubLinker
// Call type check helper
m_pCode->EmitLDARG(rank);
m_pCode->EmitLoadThis();
m_pCode->EmitCALL(METHOD__STUBHELPERS__ARRAY_TYPE_CHECK,2,0);
m_pCode->EmitCALL(METHOD__CASTHELPERS__ARRAYTYPECHECK,2,0);

m_pCode->EmitLabel(pTypeCheckOK);

Expand Down
7 changes: 3 additions & 4 deletions src/coreclr/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -939,10 +939,6 @@ DEFINE_METHOD(STUBHELPERS, PROFILER_BEGIN_TRANSITION_CALLBACK, Profiler
DEFINE_METHOD(STUBHELPERS, PROFILER_END_TRANSITION_CALLBACK, ProfilerEndTransitionCallback, SM_IntPtr_IntPtr_RetVoid)
#endif

#ifdef FEATURE_ARRAYSTUB_AS_IL
DEFINE_METHOD(STUBHELPERS, ARRAY_TYPE_CHECK, ArrayTypeCheck, SM_Obj_ArrObject_RetVoid)
#endif

#ifdef FEATURE_MULTICASTSTUB_AS_IL
DEFINE_METHOD(STUBHELPERS, MULTICAST_DEBUGGER_TRACE_HELPER, MulticastDebuggerTraceHelper, SM_Obj_Int_RetVoid)
#endif
Expand Down Expand Up @@ -1169,6 +1165,9 @@ DEFINE_METHOD(CASTHELPERS, CHKCASTCLASSSPECIAL, ChkCastClassSpecial, SM_Ptr
DEFINE_METHOD(CASTHELPERS, UNBOX, Unbox, SM_PtrVoid_Obj_RetRefByte)
DEFINE_METHOD(CASTHELPERS, STELEMREF, StelemRef, SM_ArrObject_IntPtr_Obj_RetVoid)
DEFINE_METHOD(CASTHELPERS, LDELEMAREF, LdelemaRef, SM_ArrObject_IntPtr_PtrVoid_RetRefObj)
#ifdef FEATURE_ARRAYSTUB_AS_IL
Copy link
Member Author

Choose a reason for hiding this comment

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

It seems that we are ready to enable it for all platforms. Just separating to another PR.

Copy link
Member

Choose a reason for hiding this comment

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

I agree it would be nice to enable this on all platforms and delete the #define.

DEFINE_METHOD(CASTHELPERS, ARRAYTYPECHECK, ArrayTypeCheck, SM_Obj_Array_RetVoid)
#endif

#ifdef FEATURE_EH_FUNCLETS
DEFINE_CLASS(EH, Runtime, EH)
Expand Down
3 changes: 0 additions & 3 deletions src/coreclr/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -479,9 +479,6 @@ FCFuncStart(gStubHelperFuncs)
FCFuncElement("ValidateByref", StubHelpers::ValidateByref)
FCFuncElement("LogPinnedArgument", StubHelpers::LogPinnedArgument)
FCFuncElement("GetStubContext", StubHelpers::GetStubContext)
#ifdef FEATURE_ARRAYSTUB_AS_IL
FCFuncElement("ArrayTypeCheck", StubHelpers::ArrayTypeCheck)
#endif //FEATURE_ARRAYSTUB_AS_IL
#ifdef FEATURE_MULTICASTSTUB_AS_IL
FCFuncElement("MulticastDebuggerTraceHelper", StubHelpers::MulticastDebuggerTraceHelper)
#endif //FEATURE_MULTICASTSTUB_AS_IL
Expand Down
2 changes: 1 addition & 1 deletion src/coreclr/vm/metasig.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ DEFINE_METASIG(SM(IntPtr_IntPtr_IntPtr_UShrt_IntPtr_RetVoid, I I I H I, v))
DEFINE_METASIG(SM(IntPtr_Int_IntPtr_RetIntPtr, I i I, I))
DEFINE_METASIG(SM(IntPtr_IntPtr_Int_Bool_IntPtr_RetVoid, I I i F I, v))
DEFINE_METASIG(SM(IntPtr_IntPtr_Obj_RetVoid, I I j, v))
DEFINE_METASIG(SM(Obj_ArrObject_RetVoid, j a(j), v))
DEFINE_METASIG_T(SM(Obj_Array_RetVoid, j C(ARRAY), v))
DEFINE_METASIG(SM(Obj_IntPtr_Obj_RetVoid, j I j, v))
DEFINE_METASIG(SM(RetUIntPtr, _, U))
DEFINE_METASIG(SM(RetIntPtr, _, I))
Expand Down
26 changes: 0 additions & 26 deletions src/coreclr/vm/stubhelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,32 +808,6 @@ FCIMPL1(DWORD, StubHelpers::CalcVaListSize, VARARGS *varargs)
}
FCIMPLEND

#ifdef FEATURE_ARRAYSTUB_AS_IL
NOINLINE static void ArrayTypeCheckSlow(Object* element, PtrArray* arr)
{
FC_INNER_PROLOG(StubHelpers::ArrayTypeCheck);
HELPER_METHOD_FRAME_BEGIN_ATTRIB(Frame::FRAME_ATTR_EXACT_DEPTH|Frame::FRAME_ATTR_CAPTURE_DEPTH_2);

if (!ObjIsInstanceOf(element, arr->GetArrayElementTypeHandle()))
COMPlusThrow(kArrayTypeMismatchException);

HELPER_METHOD_FRAME_END();

FC_INNER_EPILOG();
}

FCIMPL2(void, StubHelpers::ArrayTypeCheck, Object* element, PtrArray* arr)
{
FCALL_CONTRACT;

if (ObjIsInstanceOfCached(element, arr->GetArrayElementTypeHandle()) == TypeHandle::CanCast)
return;

FC_INNER_RETURN_VOID(ArrayTypeCheckSlow(element, arr));
}
FCIMPLEND
#endif // FEATURE_ARRAYSTUB_AS_IL

#ifdef FEATURE_MULTICASTSTUB_AS_IL
FCIMPL2(void, StubHelpers::MulticastDebuggerTraceHelper, Object* element, INT32 count)
{
Expand Down
4 changes: 0 additions & 4 deletions src/coreclr/vm/stubhelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ class StubHelpers
static FCDECL2(void, ProfilerEndTransitionCallback, MethodDesc* pRealMD, Thread* pThread);
#endif

#ifdef FEATURE_ARRAYSTUB_AS_IL
static FCDECL2(void, ArrayTypeCheck, Object*, PtrArray*);
#endif

#ifdef FEATURE_MULTICASTSTUB_AS_IL
static FCDECL2(void, MulticastDebuggerTraceHelper, Object*, INT32);
#endif
Expand Down
Loading