Skip to content

Commit

Permalink
Revert "Convert RuntimeMethodHandle.GetMethodBody() to QCall."
Browse files Browse the repository at this point in the history
This reverts commit fffc08e.
  • Loading branch information
AaronRobinsonMSFT committed Dec 12, 2024
1 parent 66627e3 commit e5c2882
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 120 deletions.
13 changes: 2 additions & 11 deletions src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1321,17 +1321,8 @@ internal static IRuntimeMethodInfo StripMethodInstantiation(IRuntimeMethodInfo m
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern Resolver GetResolver(RuntimeMethodHandleInternal method);

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "RuntimeMethodHandle_GetMethodBody")]
private static partial void GetMethodBody(RuntimeMethodHandleInternal method, nint declaringType, ObjectHandleOnStack result);

internal static RuntimeMethodBody? GetMethodBody(IRuntimeMethodInfo method, RuntimeType declaringType)
{
RuntimeMethodBody? result = null;
GetMethodBody(method.Value, declaringType.GetUnderlyingNativeHandle(), ObjectHandleOnStack.Create(ref result));
GC.KeepAlive(method);
GC.KeepAlive(declaringType);
return result;
}
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern RuntimeMethodBody? GetMethodBody(IRuntimeMethodInfo method, RuntimeType declaringType);

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern bool IsConstructor(RuntimeMethodHandleInternal method);
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/vm/ecalllist.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ FCFuncStart(gRuntimeMethodHandle)
FCFuncElement("GetStubIfNeededInternal", RuntimeMethodHandle::GetStubIfNeededInternal)
FCFuncElement("GetMethodFromCanonical", RuntimeMethodHandle::GetMethodFromCanonical)
FCFuncElement("IsDynamicMethod", RuntimeMethodHandle::IsDynamicMethod)
FCFuncElement("GetMethodBody", RuntimeMethodHandle::GetMethodBody)
FCFuncElement("IsConstructor", RuntimeMethodHandle::IsConstructor)
FCFuncElement("GetResolver", RuntimeMethodHandle::GetResolver)
FCFuncElement("GetLoaderAllocatorInternal", RuntimeMethodHandle::GetLoaderAllocatorInternal)
Expand Down
1 change: 0 additions & 1 deletion src/coreclr/vm/qcallentrypoints.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ static const Entry s_QCall[] =
DllImportEntry(RuntimeMethodHandle_IsCAVisibleFromDecoratedType)
DllImportEntry(RuntimeMethodHandle_Destroy)
DllImportEntry(RuntimeMethodHandle_GetStubIfNeededSlow)
DllImportEntry(RuntimeMethodHandle_GetMethodBody)
DllImportEntry(RuntimeModule_GetScopeName)
DllImportEntry(RuntimeModule_GetFullyQualifiedName)
DllImportEntry(RuntimeModule_GetTypes)
Expand Down
224 changes: 117 additions & 107 deletions src/coreclr/vm/runtimehandles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2039,160 +2039,170 @@ FCIMPL2(MethodDesc*, RuntimeMethodHandle::GetMethodFromCanonical, MethodDesc *pM
}
FCIMPLEND

extern "C" void QCALLTYPE RuntimeMethodHandle_GetMethodBody(MethodDesc* pMethod, EnregisteredTypeHandle pDeclaringType, QCall::ObjectHandleOnStack result)
{
QCALL_CONTRACT;

_ASSERTE(pMethod != NULL);

BEGIN_QCALL;

GCX_COOP();
FCIMPL2(RuntimeMethodBody *, RuntimeMethodHandle::GetMethodBody, ReflectMethodObject *pMethodUNSAFE, ReflectClassBaseObject *pDeclaringTypeUNSAFE)
{
CONTRACTL
{
FCALL_CHECK;
}
CONTRACTL_END;

struct
struct _gc
{
RUNTIMEMETHODBODYREF MethodBodyObj;
RUNTIMEEXCEPTIONHANDLINGCLAUSEREF EHClauseObj;
RUNTIMELOCALVARIABLEINFOREF RuntimeLocalVariableInfoObj;
U1ARRAYREF U1Array;
BASEARRAYREF TempArray;
U1ARRAYREF U1Array;
BASEARRAYREF TempArray;
REFLECTCLASSBASEREF declaringType;
REFLECTMETHODREF refMethod;
} gc;

gc.MethodBodyObj = NULL;
gc.EHClauseObj = NULL;
gc.RuntimeLocalVariableInfoObj = NULL;
gc.U1Array = NULL;
gc.TempArray = NULL;
GCPROTECT_BEGIN(gc);
gc.declaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
gc.refMethod = (REFLECTMETHODREF)ObjectToOBJECTREF(pMethodUNSAFE);

TypeHandle declaringType = TypeHandle::FromPtr(pDeclaringType);

COR_ILMETHOD* pILHeader = NULL;
if (pMethod->IsIL())
if (!gc.refMethod)
FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));

MethodDesc* pMethod = gc.refMethod->GetMethod();

TypeHandle declaringType = gc.declaringType == NULL ? TypeHandle() : gc.declaringType->GetType();

if (!pMethod->IsIL())
return NULL;

HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
{
MethodDesc* pMethodIL = pMethod;
MethodDesc *pMethodIL = pMethod;
if (pMethod->IsWrapperStub())
pMethodIL = pMethod->GetWrappedMethodDesc();

pILHeader = pMethodIL->GetILHeader();
}
COR_ILMETHOD* pILHeader = pMethodIL->GetILHeader();

if (pILHeader)
{
MethodTable * pExceptionHandlingClauseMT = CoreLibBinder::GetClass(CLASS__RUNTIME_EH_CLAUSE);
TypeHandle thEHClauseArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pExceptionHandlingClauseMT), ELEMENT_TYPE_SZARRAY);
if (pILHeader)
{
MethodTable * pExceptionHandlingClauseMT = CoreLibBinder::GetClass(CLASS__RUNTIME_EH_CLAUSE);
TypeHandle thEHClauseArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pExceptionHandlingClauseMT), ELEMENT_TYPE_SZARRAY);

MethodTable * pLocalVariableMT = CoreLibBinder::GetClass(CLASS__RUNTIME_LOCAL_VARIABLE_INFO);
TypeHandle thLocalVariableArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pLocalVariableMT), ELEMENT_TYPE_SZARRAY);
MethodTable * pLocalVariableMT = CoreLibBinder::GetClass(CLASS__RUNTIME_LOCAL_VARIABLE_INFO);
TypeHandle thLocalVariableArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pLocalVariableMT), ELEMENT_TYPE_SZARRAY);

Module* pModule = pMethod->GetModule();
COR_ILMETHOD_DECODER::DecoderStatus status;
COR_ILMETHOD_DECODER header(pILHeader, pModule->GetMDImport(), &status);
Module* pModule = pMethod->GetModule();
COR_ILMETHOD_DECODER::DecoderStatus status;
COR_ILMETHOD_DECODER header(pILHeader, pModule->GetMDImport(), &status);

if (status != COR_ILMETHOD_DECODER::SUCCESS)
{
if (status == COR_ILMETHOD_DECODER::VERIFICATION_ERROR)
{
// Throw a verification HR
COMPlusThrowHR(COR_E_VERIFICATION);
}
else
if (status != COR_ILMETHOD_DECODER::SUCCESS)
{
COMPlusThrowHR(COR_E_BADIMAGEFORMAT);
if (status == COR_ILMETHOD_DECODER::VERIFICATION_ERROR)
{
// Throw a verification HR
COMPlusThrowHR(COR_E_VERIFICATION);
}
else
{
COMPlusThrowHR(COR_E_BADIMAGEFORMAT);
}
}
}

gc.MethodBodyObj = (RUNTIMEMETHODBODYREF)AllocateObject(CoreLibBinder::GetClass(CLASS__RUNTIME_METHOD_BODY));
gc.MethodBodyObj = (RUNTIMEMETHODBODYREF)AllocateObject(CoreLibBinder::GetClass(CLASS__RUNTIME_METHOD_BODY));

gc.MethodBodyObj->_maxStackSize = header.GetMaxStack();
gc.MethodBodyObj->_initLocals = !!(header.GetFlags() & CorILMethod_InitLocals);
gc.MethodBodyObj->_maxStackSize = header.GetMaxStack();
gc.MethodBodyObj->_initLocals = !!(header.GetFlags() & CorILMethod_InitLocals);

if (header.IsFat())
gc.MethodBodyObj->_localVarSigToken = header.GetLocalVarSigTok();
else
gc.MethodBodyObj->_localVarSigToken = 0;
if (header.IsFat())
gc.MethodBodyObj->_localVarSigToken = header.GetLocalVarSigTok();
else
gc.MethodBodyObj->_localVarSigToken = 0;

// Allocate the array of IL and fill it in from the method header.
BYTE* pIL = const_cast<BYTE*>(header.Code);
COUNT_T cIL = header.GetCodeSize();
gc.U1Array = (U1ARRAYREF) AllocatePrimitiveArray(ELEMENT_TYPE_U1, cIL);
// Allocate the array of IL and fill it in from the method header.
BYTE* pIL = const_cast<BYTE*>(header.Code);
COUNT_T cIL = header.GetCodeSize();
gc.U1Array = (U1ARRAYREF) AllocatePrimitiveArray(ELEMENT_TYPE_U1, cIL);

SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_IL, gc.U1Array);
memcpyNoGCRefs(gc.MethodBodyObj->_IL->GetDataPtr(), pIL, cIL);
SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_IL, gc.U1Array);
memcpyNoGCRefs(gc.MethodBodyObj->_IL->GetDataPtr(), pIL, cIL);

// Allocate the array of exception clauses.
INT32 cEh = (INT32)header.EHCount();
const COR_ILMETHOD_SECT_EH* ehInfo = header.EH;
gc.TempArray = (BASEARRAYREF) AllocateSzArray(thEHClauseArray, cEh);
// Allocate the array of exception clauses.
INT32 cEh = (INT32)header.EHCount();
const COR_ILMETHOD_SECT_EH* ehInfo = header.EH;
gc.TempArray = (BASEARRAYREF) AllocateSzArray(thEHClauseArray, cEh);

SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_exceptionClauses, gc.TempArray);
SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_exceptionClauses, gc.TempArray);

for (INT32 i = 0; i < cEh; i++)
{
COR_ILMETHOD_SECT_EH_CLAUSE_FAT ehBuff;
const COR_ILMETHOD_SECT_EH_CLAUSE_FAT* ehClause =
(const COR_ILMETHOD_SECT_EH_CLAUSE_FAT*)ehInfo->EHClause(i, &ehBuff);
for (INT32 i = 0; i < cEh; i++)
{
COR_ILMETHOD_SECT_EH_CLAUSE_FAT ehBuff;
const COR_ILMETHOD_SECT_EH_CLAUSE_FAT* ehClause =
(const COR_ILMETHOD_SECT_EH_CLAUSE_FAT*)ehInfo->EHClause(i, &ehBuff);

gc.EHClauseObj = (RUNTIMEEXCEPTIONHANDLINGCLAUSEREF) AllocateObject(pExceptionHandlingClauseMT);
gc.EHClauseObj = (RUNTIMEEXCEPTIONHANDLINGCLAUSEREF) AllocateObject(pExceptionHandlingClauseMT);

gc.EHClauseObj->_flags = ehClause->GetFlags();
gc.EHClauseObj->_tryOffset = ehClause->GetTryOffset();
gc.EHClauseObj->_tryLength = ehClause->GetTryLength();
gc.EHClauseObj->_handlerOffset = ehClause->GetHandlerOffset();
gc.EHClauseObj->_handlerLength = ehClause->GetHandlerLength();
gc.EHClauseObj->_flags = ehClause->GetFlags();
gc.EHClauseObj->_tryOffset = ehClause->GetTryOffset();
gc.EHClauseObj->_tryLength = ehClause->GetTryLength();
gc.EHClauseObj->_handlerOffset = ehClause->GetHandlerOffset();
gc.EHClauseObj->_handlerLength = ehClause->GetHandlerLength();

if ((ehClause->GetFlags() & COR_ILEXCEPTION_CLAUSE_FILTER) == 0)
gc.EHClauseObj->_catchToken = ehClause->GetClassToken();
else
gc.EHClauseObj->_filterOffset = ehClause->GetFilterOffset();
if ((ehClause->GetFlags() & COR_ILEXCEPTION_CLAUSE_FILTER) == 0)
gc.EHClauseObj->_catchToken = ehClause->GetClassToken();
else
gc.EHClauseObj->_filterOffset = ehClause->GetFilterOffset();

gc.MethodBodyObj->_exceptionClauses->SetAt(i, (OBJECTREF) gc.EHClauseObj);
SetObjectReference((OBJECTREF*)&(gc.EHClauseObj->_methodBody), (OBJECTREF)gc.MethodBodyObj);
}
gc.MethodBodyObj->_exceptionClauses->SetAt(i, (OBJECTREF) gc.EHClauseObj);
SetObjectReference((OBJECTREF*)&(gc.EHClauseObj->_methodBody), (OBJECTREF)gc.MethodBodyObj);
}

if (header.LocalVarSig != NULL)
{
SigTypeContext sigTypeContext(pMethod, declaringType, pMethod->LoadMethodInstantiation());
MetaSig metaSig(header.LocalVarSig,
header.cbLocalVarSig,
pModule,
&sigTypeContext,
MetaSig::sigLocalVars);
INT32 cLocals = metaSig.NumFixedArgs();
gc.TempArray = (BASEARRAYREF) AllocateSzArray(thLocalVariableArray, cLocals);
SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_localVariables, gc.TempArray);

for (INT32 i = 0; i < cLocals; i ++)
if (header.LocalVarSig != NULL)
{
gc.RuntimeLocalVariableInfoObj = (RUNTIMELOCALVARIABLEINFOREF)AllocateObject(pLocalVariableMT);
SigTypeContext sigTypeContext(pMethod, declaringType, pMethod->LoadMethodInstantiation());
MetaSig metaSig(header.LocalVarSig,
header.cbLocalVarSig,
pModule,
&sigTypeContext,
MetaSig::sigLocalVars);
INT32 cLocals = metaSig.NumFixedArgs();
gc.TempArray = (BASEARRAYREF) AllocateSzArray(thLocalVariableArray, cLocals);
SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_localVariables, gc.TempArray);

for (INT32 i = 0; i < cLocals; i ++)
{
gc.RuntimeLocalVariableInfoObj = (RUNTIMELOCALVARIABLEINFOREF)AllocateObject(pLocalVariableMT);

gc.RuntimeLocalVariableInfoObj->_localIndex = i;
gc.RuntimeLocalVariableInfoObj->_localIndex = i;

metaSig.NextArg();
metaSig.NextArg();

CorElementType eType;
IfFailThrow(metaSig.GetArgProps().PeekElemType(&eType));
if (ELEMENT_TYPE_PINNED == eType)
gc.RuntimeLocalVariableInfoObj->_isPinned = TRUE;
CorElementType eType;
IfFailThrow(metaSig.GetArgProps().PeekElemType(&eType));
if (ELEMENT_TYPE_PINNED == eType)
gc.RuntimeLocalVariableInfoObj->_isPinned = TRUE;

TypeHandle tempType= metaSig.GetArgProps().GetTypeHandleThrowing(pModule, &sigTypeContext);
OBJECTREF refLocalType = tempType.GetManagedClassObject();
gc.RuntimeLocalVariableInfoObj->SetType(refLocalType);
gc.MethodBodyObj->_localVariables->SetAt(i, (OBJECTREF) gc.RuntimeLocalVariableInfoObj);
TypeHandle tempType= metaSig.GetArgProps().GetTypeHandleThrowing(pModule, &sigTypeContext);
OBJECTREF refLocalType = tempType.GetManagedClassObject();
gc.RuntimeLocalVariableInfoObj->SetType(refLocalType);
gc.MethodBodyObj->_localVariables->SetAt(i, (OBJECTREF) gc.RuntimeLocalVariableInfoObj);
}
}
else
{
INT32 cLocals = 0;
gc.TempArray = (BASEARRAYREF) AllocateSzArray(thLocalVariableArray, cLocals);
SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_localVariables, gc.TempArray);
}
}
else
{
INT32 cLocals = 0;
gc.TempArray = (BASEARRAYREF) AllocateSzArray(thLocalVariableArray, cLocals);
SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_localVariables, gc.TempArray);
}
}
HELPER_METHOD_FRAME_END();

result.Set(gc.MethodBodyObj);

GCPROTECT_END();
END_QCALL;
return (RuntimeMethodBody*)OBJECTREFToObject(gc.MethodBodyObj);
}
FCIMPLEND

FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsConstructor, MethodDesc *pMethod)
{
Expand Down
4 changes: 3 additions & 1 deletion src/coreclr/vm/runtimehandles.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ class RuntimeMethodHandle
static
FCDECL1(Object*, GetResolver, MethodDesc * pMethod);


static FCDECL2(RuntimeMethodBody*, GetMethodBody, ReflectMethodObject *pMethodUNSAFE, PTR_ReflectClassBaseObject pDeclaringType);

static FCDECL1(FC_BOOL_RET, IsConstructor, MethodDesc *pMethod);

static FCDECL1(Object*, GetLoaderAllocatorInternal, MethodDesc *pMethod);
Expand Down Expand Up @@ -254,7 +257,6 @@ extern "C" void QCALLTYPE RuntimeMethodHandle_GetTypicalMethodDefinition(MethodD
extern "C" void QCALLTYPE RuntimeMethodHandle_StripMethodInstantiation(MethodDesc * pMethod, QCall::ObjectHandleOnStack refMethod);
extern "C" void QCALLTYPE RuntimeMethodHandle_Destroy(MethodDesc * pMethod);
extern "C" MethodDesc* QCALLTYPE RuntimeMethodHandle_GetStubIfNeededSlow(MethodDesc* pMethod, QCall::TypeHandle declaringTypeHandle, QCall::ObjectHandleOnStack methodInstantiation);
extern "C" void QCALLTYPE RuntimeMethodHandle_GetMethodBody(MethodDesc* pMethod, EnregisteredTypeHandle pDeclaringType, QCall::ObjectHandleOnStack result);

class RuntimeFieldHandle
{
Expand Down

0 comments on commit e5c2882

Please sign in to comment.