Skip to content

Commit

Permalink
Remove HMFs from JIT helpers (#111075)
Browse files Browse the repository at this point in the history
* Convert JIT helpers that throw specific
exceptions to QCalls.

JIT_ThrowMethodAccessException
JIT_ThrowFieldAccessException
JIT_ThrowClassAccessException
  • Loading branch information
AaronRobinsonMSFT authored Jan 3, 2025
1 parent 19c25fd commit fb7fe3e
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,47 @@ internal static void ThrowEntryPointNotFoundException(
{
ThrowEntryPointNotFoundException((MethodTable*)targetType, (MethodTable*)interfaceType, method);
}

[DoesNotReturn]
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ExceptionNative_ThrowMethodAccessException")]
private static partial void ThrowMethodAccessExceptionInternal(void* caller, void* callee);

// implementation of CORINFO_HELP_METHOD_ACCESS_EXCEPTION
[DoesNotReturn]
[DebuggerHidden]
internal static void ThrowMethodAccessException(
void* caller, // MethodDesc*
void* callee) // MethodDesc*
{
ThrowMethodAccessExceptionInternal(caller, callee);
}

[DoesNotReturn]
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ExceptionNative_ThrowFieldAccessException")]
private static partial void ThrowFieldAccessExceptionInternal(void* caller, void* callee);

// implementation of CORINFO_HELP_FIELD_ACCESS_EXCEPTION
[DoesNotReturn]
[DebuggerHidden]
internal static void ThrowFieldAccessException(
void* caller, // MethodDesc*
void* callee) // FieldDesc*
{
ThrowFieldAccessExceptionInternal(caller, callee);
}

[DoesNotReturn]
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ExceptionNative_ThrowClassAccessException")]
private static partial void ThrowClassAccessExceptionInternal(void* caller, void* callee);

// implementation of CORINFO_HELP_CLASS_ACCESS_EXCEPTION
[DoesNotReturn]
[DebuggerHidden]
internal static void ThrowClassAccessException(
void* caller, // MethodDesc*
void* callee) // Type handle
{
ThrowClassAccessExceptionInternal(caller, callee);
}
}
}
16 changes: 8 additions & 8 deletions src/coreclr/inc/jithelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@
DYNAMICJITHELPER(CORINFO_HELP_THROW, IL_Throw, METHOD__NIL)
DYNAMICJITHELPER(CORINFO_HELP_RETHROW, IL_Rethrow, METHOD__NIL)
JITHELPER(CORINFO_HELP_USER_BREAKPOINT, JIT_UserBreakpoint, METHOD__NIL)
DYNAMICJITHELPER_NOINDIRECT(CORINFO_HELP_RNGCHKFAIL, NULL, METHOD__THROWHELPERS__THROWINDEXOUTOFRANGEEXCEPTION)
DYNAMICJITHELPER_NOINDIRECT(CORINFO_HELP_OVERFLOW, NULL, METHOD__THROWHELPERS__THROWOVERFLOWEXCEPTION)
DYNAMICJITHELPER_NOINDIRECT(CORINFO_HELP_THROWDIVZERO, NULL, METHOD__THROWHELPERS__THROWDIVIDEBYZEROEXCEPTION)
DYNAMICJITHELPER_NOINDIRECT(CORINFO_HELP_THROWNULLREF, NULL, METHOD__THROWHELPERS__THROWNULLREFEXCEPTION)
DYNAMICJITHELPER_NOINDIRECT(CORINFO_HELP_VERIFICATION, NULL, METHOD__THROWHELPERS__THROWVERIFICATIONEXCEPTION)
DYNAMICJITHELPER_NOINDIRECT(CORINFO_HELP_RNGCHKFAIL, NULL, METHOD__THROWHELPERS__THROWINDEXOUTOFRANGEEXCEPTION)
DYNAMICJITHELPER_NOINDIRECT(CORINFO_HELP_OVERFLOW, NULL, METHOD__THROWHELPERS__THROWOVERFLOWEXCEPTION)
DYNAMICJITHELPER_NOINDIRECT(CORINFO_HELP_THROWDIVZERO, NULL, METHOD__THROWHELPERS__THROWDIVIDEBYZEROEXCEPTION)
DYNAMICJITHELPER_NOINDIRECT(CORINFO_HELP_THROWNULLREF, NULL, METHOD__THROWHELPERS__THROWNULLREFEXCEPTION)
DYNAMICJITHELPER_NOINDIRECT(CORINFO_HELP_VERIFICATION, NULL, METHOD__THROWHELPERS__THROWVERIFICATIONEXCEPTION)
JITHELPER(CORINFO_HELP_FAIL_FAST, JIT_FailFast, METHOD__NIL)
JITHELPER(CORINFO_HELP_METHOD_ACCESS_EXCEPTION,JIT_ThrowMethodAccessException, METHOD__NIL)
JITHELPER(CORINFO_HELP_FIELD_ACCESS_EXCEPTION,JIT_ThrowFieldAccessException, METHOD__NIL)
JITHELPER(CORINFO_HELP_CLASS_ACCESS_EXCEPTION,JIT_ThrowClassAccessException, METHOD__NIL)
DYNAMICJITHELPER(CORINFO_HELP_METHOD_ACCESS_EXCEPTION, NULL, METHOD__THROWHELPERS__THROWMETHODACCESSEXCEPTION)
DYNAMICJITHELPER(CORINFO_HELP_FIELD_ACCESS_EXCEPTION, NULL, METHOD__THROWHELPERS__THROWFIELDACCESSEXCEPTION)
DYNAMICJITHELPER(CORINFO_HELP_CLASS_ACCESS_EXCEPTION, NULL, METHOD__THROWHELPERS__THROWCLASSACCESSEXCEPTION)

#ifdef FEATURE_EH_FUNCLETS
JITHELPER(CORINFO_HELP_ENDCATCH, NULL, METHOD__NIL)
Expand Down
39 changes: 39 additions & 0 deletions src/coreclr/vm/comutilnative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,45 @@ extern "C" void QCALLTYPE ExceptionNative_ThrowEntryPointNotFoundException(
END_QCALL;
}

extern "C" void QCALLTYPE ExceptionNative_ThrowMethodAccessException(MethodDesc* caller, MethodDesc* callee)
{
QCALL_CONTRACT;

BEGIN_QCALL;

_ASSERTE(caller != NULL);
AccessCheckContext accessContext(caller);
ThrowMethodAccessException(&accessContext, callee);

END_QCALL;
}

extern "C" void QCALLTYPE ExceptionNative_ThrowFieldAccessException(MethodDesc* caller, FieldDesc* callee)
{
QCALL_CONTRACT;

BEGIN_QCALL;

_ASSERTE(caller != NULL);
AccessCheckContext accessContext(caller);
ThrowFieldAccessException(&accessContext, callee);

END_QCALL;
}

extern "C" void QCALLTYPE ExceptionNative_ThrowClassAccessException(MethodDesc* caller, EnregisteredTypeHandle callee)
{
QCALL_CONTRACT;

BEGIN_QCALL;

_ASSERTE(caller != NULL);
AccessCheckContext accessContext(caller);
ThrowTypeAccessException(&accessContext, TypeHandle::FromPtr(callee).GetMethodTable());

END_QCALL;
}

extern "C" void QCALLTYPE Buffer_Clear(void *dst, size_t length)
{
QCALL_CONTRACT;
Expand Down
4 changes: 4 additions & 0 deletions src/coreclr/vm/comutilnative.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ extern "C" void QCALLTYPE ExceptionNative_ThrowEntryPointNotFoundException(
MethodTable* pInterfaceMT,
MethodDesc* pInterfaceMD);

extern "C" void QCALLTYPE ExceptionNative_ThrowMethodAccessException(MethodDesc* caller, MethodDesc* callee);
extern "C" void QCALLTYPE ExceptionNative_ThrowFieldAccessException(MethodDesc* caller, FieldDesc* callee);
extern "C" void QCALLTYPE ExceptionNative_ThrowClassAccessException(MethodDesc* caller, EnregisteredTypeHandle callee);

//
// Buffer
//
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,9 @@ DEFINE_METHOD(THROWHELPERS, THROWNULLREFEXCEPTION, ThrowNullRef
DEFINE_METHOD(THROWHELPERS, THROWVERIFICATIONEXCEPTION, ThrowVerificationException, SM_Int_RetVoid)
DEFINE_METHOD(THROWHELPERS, THROWAMBIGUOUSRESOLUTIONEXCEPTION, ThrowAmbiguousResolutionException, SM_PtrVoid_PtrVoid_PtrVoid_RetVoid)
DEFINE_METHOD(THROWHELPERS, THROWENTRYPOINTNOTFOUNDEXCEPTION, ThrowEntryPointNotFoundException, SM_PtrVoid_PtrVoid_PtrVoid_RetVoid)
DEFINE_METHOD(THROWHELPERS, THROWMETHODACCESSEXCEPTION, ThrowMethodAccessException, SM_PtrVoid_PtrVoid_RetVoid)
DEFINE_METHOD(THROWHELPERS, THROWFIELDACCESSEXCEPTION, ThrowFieldAccessException, SM_PtrVoid_PtrVoid_RetVoid)
DEFINE_METHOD(THROWHELPERS, THROWCLASSACCESSEXCEPTION, ThrowClassAccessException, SM_PtrVoid_PtrVoid_RetVoid)

DEFINE_CLASS(UNSAFE, CompilerServices, Unsafe)
DEFINE_METHOD(UNSAFE, AS_POINTER, AsPointer, NoSig)
Expand Down
57 changes: 0 additions & 57 deletions src/coreclr/vm/jithelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2129,63 +2129,6 @@ HCIMPL0(void, JIT_FailFast)
}
HCIMPLEND

HCIMPL2(void, JIT_ThrowMethodAccessException, CORINFO_METHOD_HANDLE caller, CORINFO_METHOD_HANDLE callee)
{
FCALL_CONTRACT;

FC_GC_POLL_NOT_NEEDED(); // throws always open up for GC

HELPER_METHOD_FRAME_BEGIN_ATTRIB_NOPOLL(Frame::FRAME_ATTR_EXCEPTION); // Set up a frame

MethodDesc* pCallerMD = GetMethod(caller);

_ASSERTE(pCallerMD != NULL);
AccessCheckContext accessContext(pCallerMD);

ThrowMethodAccessException(&accessContext, GetMethod(callee));

HELPER_METHOD_FRAME_END();
}
HCIMPLEND

HCIMPL2(void, JIT_ThrowFieldAccessException, CORINFO_METHOD_HANDLE caller, CORINFO_FIELD_HANDLE callee)
{
FCALL_CONTRACT;

FC_GC_POLL_NOT_NEEDED(); // throws always open up for GC

HELPER_METHOD_FRAME_BEGIN_ATTRIB_NOPOLL(Frame::FRAME_ATTR_EXCEPTION); // Set up a frame

MethodDesc* pCallerMD = GetMethod(caller);

_ASSERTE(pCallerMD != NULL);
AccessCheckContext accessContext(pCallerMD);

ThrowFieldAccessException(&accessContext, reinterpret_cast<FieldDesc *>(callee));

HELPER_METHOD_FRAME_END();
}
HCIMPLEND;

HCIMPL2(void, JIT_ThrowClassAccessException, CORINFO_METHOD_HANDLE caller, CORINFO_CLASS_HANDLE callee)
{
FCALL_CONTRACT;

FC_GC_POLL_NOT_NEEDED(); // throws always open up for GC

HELPER_METHOD_FRAME_BEGIN_ATTRIB_NOPOLL(Frame::FRAME_ATTR_EXCEPTION); // Set up a frame

MethodDesc* pCallerMD = GetMethod(caller);

_ASSERTE(pCallerMD != NULL);
AccessCheckContext accessContext(pCallerMD);

ThrowTypeAccessException(&accessContext, TypeHandle(callee).GetMethodTable());

HELPER_METHOD_FRAME_END();
}
HCIMPLEND;

//========================================================================
//
// DEBUGGER/PROFILER HELPERS
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 @@ -353,6 +353,7 @@ DEFINE_METASIG(SM(IntPtr_IntPtr_RetIntPtr, I I, I))
DEFINE_METASIG(SM(IntPtr_IntPtr_Int_RetIntPtr, I I i, I))
DEFINE_METASIG(SM(PtrVoid_RetVoid, P(v), v))
DEFINE_METASIG(SM(PtrVoid_PtrVoid_RetVoid, P(v) P(v), v))
DEFINE_METASIG(SM(PtrVoid_PtrVoid_PtrVoid_RetVoid, P(v) P(v) P(v), v))
DEFINE_METASIG(SM(PtrVoid_PtrVoid_UInt_RetVoid, P(v) P(v) K, v))
DEFINE_METASIG(IM(Obj_RetBool, j, F))
DEFINE_METASIG(SM(Obj_RetVoid, j, v))
Expand Down Expand Up @@ -570,7 +571,6 @@ DEFINE_METASIG_T(SM(RefCleanupWorkListElement_SafeHandle_RetIntPtr, r(C(CLEANUP_
DEFINE_METASIG_T(SM(RefCleanupWorkListElement_Obj_RetVoid, r(C(CLEANUP_WORK_LIST_ELEMENT)) j, v))

DEFINE_METASIG(SM(PtrVoid_RetPtrVoid, P(v), P(v)))
DEFINE_METASIG(SM(PtrVoid_PtrVoid_PtrVoid_RetVoid, P(v) P(v) P(v), v))
DEFINE_METASIG(IM(PtrVoid_RetVoid, P(v), v))

DEFINE_METASIG_T(SM(IDynamicInterfaceCastable_RuntimeType_Bool_RetBool, C(IDYNAMICINTERFACECASTABLE) C(CLASS) F, F))
Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/vm/qcallentrypoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ static const Entry s_QCall[] =
DllImportEntry(ExceptionNative_GetMethodFromStackTrace)
DllImportEntry(ExceptionNative_ThrowAmbiguousResolutionException)
DllImportEntry(ExceptionNative_ThrowEntryPointNotFoundException)
DllImportEntry(ExceptionNative_ThrowMethodAccessException)
DllImportEntry(ExceptionNative_ThrowFieldAccessException)
DllImportEntry(ExceptionNative_ThrowClassAccessException)
DllImportEntry(QCall_GetGCHandleForTypeHandle)
DllImportEntry(QCall_FreeGCHandleForTypeHandle)
DllImportEntry(MethodTable_AreTypesEquivalent)
Expand Down

0 comments on commit fb7fe3e

Please sign in to comment.