Skip to content

Convert GetMethodFromStackTrace to QCall #105103

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

Merged
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
34 changes: 12 additions & 22 deletions src/coreclr/System.Private.CoreLib/src/System/Exception.CoreCLR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,28 @@ private IDictionary CreateDataContainer()
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool IsImmutableAgileException(Exception e);

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern IRuntimeMethodInfo GetMethodFromStackTrace(object stackTrace);
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "ExceptionNative_GetMethodFromStackTrace")]
private static partial void GetMethodFromStackTrace(ObjectHandleOnStack stackTrace, ObjectHandleOnStack method);

private MethodBase? GetExceptionMethodFromStackTrace()
{
Debug.Assert(_stackTrace != null, "_stackTrace shouldn't be null when this method is called");
IRuntimeMethodInfo method = GetMethodFromStackTrace(_stackTrace!);

// Under certain race conditions when exceptions are re-used, this can be null
if (method == null)
object? stackTraceLocal = _stackTrace;
if (stackTraceLocal == null)
{
return null;
}

return RuntimeType.GetMethodBase(method);
IRuntimeMethodInfo? methodInfo = null;
GetMethodFromStackTrace(ObjectHandleOnStack.Create(ref stackTraceLocal), ObjectHandleOnStack.Create(ref methodInfo));
Debug.Assert(methodInfo != null);

return RuntimeType.GetMethodBase(methodInfo);
}

public MethodBase? TargetSite
{
[RequiresUnreferencedCode("Metadata for the method might be incomplete or removed")]
get
{
if (_exceptionMethod != null)
{
return _exceptionMethod;
}
if (_stackTrace == null)
{
return null;
}

_exceptionMethod = GetExceptionMethodFromStackTrace();
return _exceptionMethod;
}
get => _exceptionMethod ??= GetExceptionMethodFromStackTrace();
}

// This method will clear the _stackTrace of the exception object upon deserialization
Expand Down
37 changes: 0 additions & 37 deletions src/coreclr/classlibnative/bcltype/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,43 +88,6 @@ FCIMPL0(INT32, SystemNative::GetExitCode)
}
FCIMPLEND

// Return a method info for the method were the exception was thrown
FCIMPL1(ReflectMethodObject*, SystemNative::GetMethodFromStackTrace, ArrayBase* pStackTraceUNSAFE)
{
FCALL_CONTRACT;

// The pStackTraceUNSAFE can be either I1Array or Object[]. In the latter case, the first entry is the actual stack trace I1Array,
// the rest are pointers to the method info objects. We only care about the first entry here.
if (pStackTraceUNSAFE->GetArrayElementType() != ELEMENT_TYPE_I1)
{
PtrArray *combinedArray = (PtrArray*)pStackTraceUNSAFE;
pStackTraceUNSAFE = (ArrayBase*)OBJECTREFToObject(combinedArray->GetAt(0));
}
I1ARRAYREF pArray(static_cast<I1Array *>(pStackTraceUNSAFE));
StackTraceArray stackArray(pArray);

if (!stackArray.Size())
return NULL;

// The managed stacktrace classes always returns typical method definition, so we don't need to bother providing exact instantiation.
// Generics::GetExactInstantiationsOfMethodAndItsClassFromCallInformation(pElements[0].pFunc, pElements[0].pExactGenericArgsToken, pTypeHandle, &pMD);

MethodDesc* pFunc = stackArray[0].pFunc;

// Strip the instantiation to make sure that the reflection never gets a bad method desc back.
REFLECTMETHODREF refRet = NULL;

HELPER_METHOD_FRAME_BEGIN_RET_0()
pFunc = pFunc->LoadTypicalMethodDefinition();
refRet = pFunc->GetStubMethodInfo();
_ASSERTE(pFunc->IsRuntimeMethodHandle());

HELPER_METHOD_FRAME_END();

return (ReflectMethodObject*)OBJECTREFToObject(refRet);
}
FCIMPLEND

extern "C" INT32 QCALLTYPE Environment_GetProcessorCount()
{
QCALL_CONTRACT;
Expand Down
3 changes: 0 additions & 3 deletions src/coreclr/classlibnative/bcltype/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ class SystemNative
static FCDECL0(INT32, GetExitCode);

static FCDECL0(FC_BOOL_RET, IsServerGC);

// Return a method info for the method were the exception was thrown
static FCDECL1(ReflectMethodObject*, GetMethodFromStackTrace, ArrayBase* pStackTraceUNSAFE);
};

extern "C" void QCALLTYPE Environment_Exit(INT32 exitcode);
Expand Down
44 changes: 42 additions & 2 deletions src/coreclr/vm/comutilnative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ FCIMPL1(Object *, ExceptionNative::GetFrozenStackTrace, Object* pExceptionObject
{
gc.result = gc.stackTrace.Get();
}

HELPER_METHOD_FRAME_END();

return OBJECTREFToObject(gc.result);
Expand Down Expand Up @@ -415,6 +415,46 @@ extern "C" void QCALLTYPE ExceptionNative_GetMessageFromNativeResources(Exceptio
END_QCALL;
}

extern "C" void QCALLTYPE ExceptionNative_GetMethodFromStackTrace(QCall::ObjectHandleOnStack stacktrace, QCall::ObjectHandleOnStack retMethodInfo)
{
QCALL_CONTRACT;

BEGIN_QCALL;

GCX_COOP();

MethodDesc* pMD = NULL;
// See ExceptionObject::GetStackTrace() and ExceptionObject::SetStackTrace()
// for details on the stacktrace array.
{
ARRAYBASEREF arrayBaseRef = (ARRAYBASEREF)stacktrace.Get();
_ASSERTE(arrayBaseRef != NULL);

// The stacktrace can be either sbyte[] or Object[]. In the latter case,
// the first entry is the actual stack trace sbyte[], the rest are pointers
// to the method info objects. We only care about the first entry here.
if (arrayBaseRef->GetArrayElementType() != ELEMENT_TYPE_I1)
{
_ASSERTE(arrayBaseRef->GetArrayElementType() == ELEMENT_TYPE_OBJECT);
PTRARRAYREF ptrArrayRef = (PTRARRAYREF)arrayBaseRef;
arrayBaseRef = (ARRAYBASEREF)OBJECTREFToObject(ptrArrayRef->GetAt(0));
}

I1ARRAYREF arrayRef = (I1ARRAYREF)arrayBaseRef;
StackTraceArray stackArray(arrayRef);
_ASSERTE(stackArray.Size() > 0);
pMD = stackArray[0].pFunc;
}

// The managed stack trace classes always return typical method definition,
// so we don't need to bother providing exact instantiation.
MethodDesc* pMDTypical = pMD->LoadTypicalMethodDefinition();
retMethodInfo.Set(pMDTypical->GetStubMethodInfo());
_ASSERTE(pMDTypical->IsRuntimeMethodHandle());

END_QCALL;
}

extern "C" void QCALLTYPE Buffer_Clear(void *dst, size_t length)
{
QCALL_CONTRACT;
Expand Down Expand Up @@ -1784,7 +1824,7 @@ extern "C" BOOL QCALLTYPE TypeHandle_CanCastTo_NoCacheLookup(void* fromTypeHnd,

TypeHandle fromTH = TypeHandle::FromPtr(fromTypeHnd);
TypeHandle toTH = TypeHandle::FromPtr(toTypeHnd);

if (fromTH.IsTypeDesc())
{
ret = fromTH.AsTypeDesc()->CanCastTo(toTH, NULL);
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/vm/comutilnative.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ enum class ExceptionMessageKind {
};
extern "C" void QCALLTYPE ExceptionNative_GetMessageFromNativeResources(ExceptionMessageKind kind, QCall::StringHandleOnStack retMesg);

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

//
// Buffer
//
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ FCFuncEnd()

FCFuncStart(gExceptionFuncs)
FCFuncElement("IsImmutableAgileException", ExceptionNative::IsImmutableAgileException)
FCFuncElement("GetMethodFromStackTrace", SystemNative::GetMethodFromStackTrace)
FCFuncElement("PrepareForForeignExceptionRaise", ExceptionNative::PrepareForForeignExceptionRaise)
FCFuncElement("GetFrozenStackTrace", ExceptionNative::GetFrozenStackTrace)
FCFuncElement("GetExceptionCount", ExceptionNative::GetExceptionCount)
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/vm/qcallentrypoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ static const Entry s_QCall[] =
DllImportEntry(Environment_FailFast)
DllImportEntry(Environment_GetProcessorCount)
DllImportEntry(ExceptionNative_GetMessageFromNativeResources)
DllImportEntry(ExceptionNative_GetMethodFromStackTrace)
DllImportEntry(RuntimeTypeHandle_CreateInstanceForAnotherGenericParameter)
DllImportEntry(QCall_GetGCHandleForTypeHandle)
DllImportEntry(QCall_FreeGCHandleForTypeHandle)
Expand Down
Loading