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

Convert exception throwing JIT helpers to managed code. #105671

Merged
merged 13 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -231,6 +231,7 @@
<Compile Include="$(BclSourcesRoot)\System\Threading\ThreadPool.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Threading\WaitHandle.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\Type.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\ThrowHelper.cs" />
<Compile Include="$(BclSourcesRoot)\System\TypedReference.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\TypeLoadException.CoreCLR.cs" />
<Compile Include="$(BclSourcesRoot)\System\ValueType.cs" />
Expand Down
38 changes: 38 additions & 0 deletions src/coreclr/System.Private.CoreLib/src/System/ThrowHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace System
{
internal static unsafe partial class ThrowHelper
{
[DoesNotReturn]
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ExceptionNative_ThrowAmbiguousResolutionException")]
private static partial void ThrowAmbiguousResolutionException(MethodTable* targetType, MethodTable* interfaceType, void* methodDesc);

[DoesNotReturn]
internal static void ThrowAmbiguousResolutionException(
void* method, // MethodDesc*
void* interfaceType, // MethodTable*
void* targetType) // MethodTable*
{
ThrowAmbiguousResolutionException((MethodTable*)targetType, (MethodTable*)interfaceType, method);
}

[DoesNotReturn]
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ExceptionNative_ThrowEntryPointNotFoundException")]
private static partial void ThrowEntryPointNotFoundException(MethodTable* targetType, MethodTable* interfaceType, void* methodDesc);

[DoesNotReturn]
internal static void ThrowEntryPointNotFoundException(
void* method, // MethodDesc*
void* interfaceType, // MethodTable*
void* targetType) // MethodTable*
{
ThrowEntryPointNotFoundException((MethodTable*)targetType, (MethodTable*)interfaceType, method);
}
}
}
6 changes: 3 additions & 3 deletions src/coreclr/debug/daccess/dacdbiimplstackwalk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -882,15 +882,15 @@ void DacDbiInterfaceImpl::InitFrameData(StackFrameIterator * pIter,
pJITFuncData->nativeOffset = pCF->GetRelOffset();

// Here we detect (and set the appropriate flag) if the nativeOffset in the current frame points to the return address of IL_Throw()
// (or other exception related JIT helpers like IL_Throw, IL_Rethrow, JIT_RngChkFail, IL_VerificationError, JIT_Overflow etc).
// Since return addres point to the next(!) instruction after [call IL_Throw] this sometimes can lead to incorrect exception stacktraces
// (or other exception related JIT helpers like IL_Throw, IL_Rethrow etc).
// Since return address point to the next(!) instruction after [call IL_Throw] this sometimes can lead to incorrect exception stacktraces
// where a next source line is spotted as an exception origin. This happens when the next instruction after [call IL_Throw] belongs to
// a sequence point and a source line different from a sequence point and a source line of [call IL_Throw].
// Later on this flag is used in order to adjust nativeOffset and make ICorDebugILFrame::GetIP return IL offset within
// the same sequence point as an actual IL throw instruction.

// Here is how we detect it:
// We can assume that nativeOffset points to an the instruction after [call IL_Throw] when these conditioins are met:
// We can assume that nativeOffset points to an the instruction after [call IL_Throw] when these conditions are met:
// 1. pCF->IsInterrupted() - Exception has been thrown by this managed frame (frame attr FRAME_ATTR_EXCEPTION)
// 2. !pCF->HasFaulted() - It wasn't a "hardware" exception (Access violation, dev by 0, etc.)
// 3. !pCF->IsIPadjusted() - It hasn't been previously adjusted to point to [call IL_Throw]
Expand Down
24 changes: 12 additions & 12 deletions src/coreclr/inc/jithelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@
DYNAMICJITHELPER(CORINFO_HELP_THROW, IL_Throw, CORINFO_HELP_SIG_REG_ONLY)
DYNAMICJITHELPER(CORINFO_HELP_RETHROW, IL_Rethrow, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_USER_BREAKPOINT, JIT_UserBreakpoint, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_RNGCHKFAIL, JIT_RngChkFail, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_OVERFLOW, JIT_Overflow, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_THROWDIVZERO, JIT_ThrowDivZero, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_THROWNULLREF, JIT_ThrowNullRef, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_VERIFICATION, IL_VerificationError,CORINFO_HELP_SIG_REG_ONLY)
DYNAMICJITHELPER(CORINFO_HELP_RNGCHKFAIL, NULL, CORINFO_HELP_SIG_REG_ONLY)
DYNAMICJITHELPER(CORINFO_HELP_OVERFLOW, NULL, CORINFO_HELP_SIG_REG_ONLY)
DYNAMICJITHELPER(CORINFO_HELP_THROWDIVZERO, NULL, CORINFO_HELP_SIG_REG_ONLY)
DYNAMICJITHELPER(CORINFO_HELP_THROWNULLREF, NULL, CORINFO_HELP_SIG_REG_ONLY)
DYNAMICJITHELPER(CORINFO_HELP_VERIFICATION, NULL, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_FAIL_FAST, JIT_FailFast, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_METHOD_ACCESS_EXCEPTION,JIT_ThrowMethodAccessException, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_FIELD_ACCESS_EXCEPTION,JIT_ThrowFieldAccessException, CORINFO_HELP_SIG_REG_ONLY)
Expand Down Expand Up @@ -287,13 +287,13 @@
JITHELPER(CORINFO_HELP_LOOP_CLONE_CHOICE_ADDR, JIT_LoopCloneChoiceAddr, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_DEBUG_LOG_LOOP_CLONING, JIT_DebugLogLoopCloning, CORINFO_HELP_SIG_REG_ONLY)

JITHELPER(CORINFO_HELP_THROW_ARGUMENTEXCEPTION, JIT_ThrowArgumentException, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_THROW_ARGUMENTOUTOFRANGEEXCEPTION, JIT_ThrowArgumentOutOfRangeException, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_THROW_NOT_IMPLEMENTED, JIT_ThrowNotImplementedException, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_THROW_PLATFORM_NOT_SUPPORTED, JIT_ThrowPlatformNotSupportedException, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_THROW_TYPE_NOT_SUPPORTED, JIT_ThrowTypeNotSupportedException, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_THROW_AMBIGUOUS_RESOLUTION_EXCEPTION, JIT_ThrowAmbiguousResolutionException, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_THROW_ENTRYPOINT_NOT_FOUND_EXCEPTION, JIT_ThrowEntryPointNotFoundException, CORINFO_HELP_SIG_REG_ONLY)
DYNAMICJITHELPER(CORINFO_HELP_THROW_ARGUMENTEXCEPTION, NULL, CORINFO_HELP_SIG_REG_ONLY)
DYNAMICJITHELPER(CORINFO_HELP_THROW_ARGUMENTOUTOFRANGEEXCEPTION, NULL, CORINFO_HELP_SIG_REG_ONLY)
DYNAMICJITHELPER(CORINFO_HELP_THROW_NOT_IMPLEMENTED, NULL, CORINFO_HELP_SIG_REG_ONLY)
DYNAMICJITHELPER(CORINFO_HELP_THROW_PLATFORM_NOT_SUPPORTED, NULL, CORINFO_HELP_SIG_REG_ONLY)
DYNAMICJITHELPER(CORINFO_HELP_THROW_TYPE_NOT_SUPPORTED, NULL, CORINFO_HELP_SIG_REG_ONLY)
DYNAMICJITHELPER(CORINFO_HELP_THROW_AMBIGUOUS_RESOLUTION_EXCEPTION, NULL, CORINFO_HELP_SIG_REG_ONLY)
DYNAMICJITHELPER(CORINFO_HELP_THROW_ENTRYPOINT_NOT_FOUND_EXCEPTION, NULL, CORINFO_HELP_SIG_REG_ONLY)

JITHELPER(CORINFO_HELP_JIT_PINVOKE_BEGIN, JIT_PInvokeBegin, CORINFO_HELP_SIG_REG_ONLY)
JITHELPER(CORINFO_HELP_JIT_PINVOKE_END, JIT_PInvokeEnd, CORINFO_HELP_SIG_REG_ONLY)
Expand Down
28 changes: 28 additions & 0 deletions src/coreclr/vm/comutilnative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,34 @@ extern "C" void QCALLTYPE ExceptionNative_GetMethodFromStackTrace(QCall::ObjectH
END_QCALL;
}

extern "C" void QCALLTYPE ExceptionNative_ThrowAmbiguousResolutionException(
MethodTable* pTargetClass,
MethodTable* pInterfaceMT,
MethodDesc* pInterfaceMD)
{
QCALL_CONTRACT;

BEGIN_QCALL;

ThrowAmbiguousResolutionException(pTargetClass, pInterfaceMT, pInterfaceMD);

END_QCALL;
}

extern "C" void QCALLTYPE ExceptionNative_ThrowEntryPointNotFoundException(
MethodTable* pTargetClass,
MethodTable* pInterfaceMT,
MethodDesc* pInterfaceMD)
{
QCALL_CONTRACT;

BEGIN_QCALL;

ThrowEntryPointNotFoundException(pTargetClass, pInterfaceMT, pInterfaceMD);

END_QCALL;
}

extern "C" void QCALLTYPE Buffer_Clear(void *dst, size_t length)
{
QCALL_CONTRACT;
Expand Down
10 changes: 10 additions & 0 deletions src/coreclr/vm/comutilnative.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ extern "C" void QCALLTYPE ExceptionNative_GetMessageFromNativeResources(Exceptio

extern "C" void QCALLTYPE ExceptionNative_GetMethodFromStackTrace(QCall::ObjectHandleOnStack array, QCall::ObjectHandleOnStack retMethodInfo);

extern "C" void QCALLTYPE ExceptionNative_ThrowAmbiguousResolutionException(
MethodTable* pTargetClass,
MethodTable* pInterfaceMT,
MethodDesc* pInterfaceMD);

extern "C" void QCALLTYPE ExceptionNative_ThrowEntryPointNotFoundException(
MethodTable* pTargetClass,
MethodTable* pInterfaceMT,
MethodDesc* pInterfaceMD);

//
// Buffer
//
Expand Down
14 changes: 14 additions & 0 deletions src/coreclr/vm/corelib.h
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,20 @@ DEFINE_METHOD(SPAN_HELPERS, MEMSET, Fill, SM_RefByte_Byt
DEFINE_METHOD(SPAN_HELPERS, MEMZERO, ClearWithoutReferences, SM_RefByte_UIntPtr_RetVoid)
DEFINE_METHOD(SPAN_HELPERS, MEMCOPY, Memmove, SM_RefByte_RefByte_UIntPtr_RetVoid)

DEFINE_CLASS(THROW_HELPERS, System, ThrowHelper)
DEFINE_METHOD(THROW_HELPERS, THROWARGUMENTEXCEPTION, ThrowArgumentException, SM_RetVoid)
DEFINE_METHOD(THROW_HELPERS, THROWARGUMENTOUTOFRANGEEXCEPTION, ThrowArgumentOutOfRangeException, SM_RetVoid)
DEFINE_METHOD(THROW_HELPERS, THROWINDEXOUTOFRANGEEXCEPTION, ThrowIndexOutOfRangeException, SM_RetVoid)
DEFINE_METHOD(THROW_HELPERS, THROWNOTIMPLEMENTEDEXCEPTION, ThrowNotImplementedException, SM_RetVoid)
DEFINE_METHOD(THROW_HELPERS, THROWPLATFORMNOTSUPPORTEDEXCEPTION, ThrowPlatformNotSupportedException, SM_RetVoid)
DEFINE_METHOD(THROW_HELPERS, THROWTYPENOTSUPPORTED, ThrowTypeNotSupportedException, SM_RetVoid)
DEFINE_METHOD(THROW_HELPERS, THROWOVERFLOWEXCEPTION, ThrowOverflowException, SM_RetVoid)
DEFINE_METHOD(THROW_HELPERS, THROWDIVIDEBYZEROEXCEPTION, ThrowDivideByZeroException, SM_RetVoid)
DEFINE_METHOD(THROW_HELPERS, THROWNULLREFEXCEPTION, ThrowNullReferenceException, SM_RetVoid)
DEFINE_METHOD(THROW_HELPERS, THROWVERIFICATIONEXCEPTION, ThrowVerificationException, SM_Int_RetVoid)
DEFINE_METHOD(THROW_HELPERS, THROWAMBIGUOUSRESOLUTIONEXCEPTION, ThrowAmbiguousResolutionException, SM_PtrVoid_PtrVoid_PtrVoid_RetVoid)
DEFINE_METHOD(THROW_HELPERS, THROWENTRYPOINTNOTFOUNDEXCEPTION, ThrowEntryPointNotFoundException, SM_PtrVoid_PtrVoid_PtrVoid_RetVoid)

DEFINE_CLASS(UNSAFE, CompilerServices, Unsafe)
DEFINE_METHOD(UNSAFE, AS_POINTER, AsPointer, NoSig)
DEFINE_METHOD(UNSAFE, BYREF_IS_NULL, IsNullRef, NoSig)
Expand Down
48 changes: 48 additions & 0 deletions src/coreclr/vm/ecall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,54 @@ void ECall::PopulateManagedHelpers()
pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__MATH__CONVERT_TO_UINT64_CHECKED));
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_DBL2ULNG_OVF, pDest);

pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__THROW_HELPERS__THROWARGUMENTEXCEPTION));
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_THROW_ARGUMENTEXCEPTION, pDest);

pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__THROW_HELPERS__THROWARGUMENTOUTOFRANGEEXCEPTION));
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_THROW_ARGUMENTOUTOFRANGEEXCEPTION, pDest);

pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__THROW_HELPERS__THROWINDEXOUTOFRANGEEXCEPTION));
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_RNGCHKFAIL, pDest);

pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__THROW_HELPERS__THROWNOTIMPLEMENTEDEXCEPTION));
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_THROW_NOT_IMPLEMENTED, pDest);

pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__THROW_HELPERS__THROWPLATFORMNOTSUPPORTEDEXCEPTION));
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_THROW_PLATFORM_NOT_SUPPORTED, pDest);

pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__THROW_HELPERS__THROWTYPENOTSUPPORTED));
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_THROW_TYPE_NOT_SUPPORTED, pDest);

pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__THROW_HELPERS__THROWOVERFLOWEXCEPTION));
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_OVERFLOW, pDest);

pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__THROW_HELPERS__THROWDIVIDEBYZEROEXCEPTION));
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_THROWDIVZERO, pDest);

pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__THROW_HELPERS__THROWNULLREFEXCEPTION));
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_THROWNULLREF, pDest);

pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__THROW_HELPERS__THROWVERIFICATIONEXCEPTION));
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_VERIFICATION, pDest);

pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__THROW_HELPERS__THROWAMBIGUOUSRESOLUTIONEXCEPTION));
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_THROW_AMBIGUOUS_RESOLUTION_EXCEPTION, pDest);

pMD = CoreLibBinder::GetMethod((BinderMethodID)(METHOD__THROW_HELPERS__THROWENTRYPOINTNOTFOUNDEXCEPTION));
pDest = pMD->GetMultiCallableAddrOfCode();
SetJitHelperFunction(CORINFO_HELP_THROW_ENTRYPOINT_NOT_FOUND_EXCEPTION, pDest);
}

static CrstStatic gFCallLock;
Expand Down
Loading
Loading