Skip to content

Commit fffc08e

Browse files
Convert RuntimeMethodHandle.GetMethodBody() to QCall.
1 parent f9ff74a commit fffc08e

File tree

5 files changed

+120
-123
lines changed

5 files changed

+120
-123
lines changed

src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,8 +1321,17 @@ internal static IRuntimeMethodInfo StripMethodInstantiation(IRuntimeMethodInfo m
13211321
[MethodImpl(MethodImplOptions.InternalCall)]
13221322
internal static extern Resolver GetResolver(RuntimeMethodHandleInternal method);
13231323

1324-
[MethodImpl(MethodImplOptions.InternalCall)]
1325-
internal static extern RuntimeMethodBody? GetMethodBody(IRuntimeMethodInfo method, RuntimeType declaringType);
1324+
[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "RuntimeMethodHandle_GetMethodBody")]
1325+
private static partial void GetMethodBody(RuntimeMethodHandleInternal method, nint declaringType, ObjectHandleOnStack result);
1326+
1327+
internal static RuntimeMethodBody? GetMethodBody(IRuntimeMethodInfo method, RuntimeType declaringType)
1328+
{
1329+
RuntimeMethodBody? result = null;
1330+
GetMethodBody(method.Value, declaringType.GetUnderlyingNativeHandle(), ObjectHandleOnStack.Create(ref result));
1331+
GC.KeepAlive(method);
1332+
GC.KeepAlive(declaringType);
1333+
return result;
1334+
}
13261335

13271336
[MethodImpl(MethodImplOptions.InternalCall)]
13281337
internal static extern bool IsConstructor(RuntimeMethodHandleInternal method);

src/coreclr/vm/ecalllist.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ FCFuncStart(gRuntimeMethodHandle)
149149
FCFuncElement("GetStubIfNeededInternal", RuntimeMethodHandle::GetStubIfNeededInternal)
150150
FCFuncElement("GetMethodFromCanonical", RuntimeMethodHandle::GetMethodFromCanonical)
151151
FCFuncElement("IsDynamicMethod", RuntimeMethodHandle::IsDynamicMethod)
152-
FCFuncElement("GetMethodBody", RuntimeMethodHandle::GetMethodBody)
153152
FCFuncElement("IsConstructor", RuntimeMethodHandle::IsConstructor)
154153
FCFuncElement("GetResolver", RuntimeMethodHandle::GetResolver)
155154
FCFuncElement("GetLoaderAllocatorInternal", RuntimeMethodHandle::GetLoaderAllocatorInternal)

src/coreclr/vm/qcallentrypoints.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ static const Entry s_QCall[] =
163163
DllImportEntry(RuntimeMethodHandle_IsCAVisibleFromDecoratedType)
164164
DllImportEntry(RuntimeMethodHandle_Destroy)
165165
DllImportEntry(RuntimeMethodHandle_GetStubIfNeededSlow)
166+
DllImportEntry(RuntimeMethodHandle_GetMethodBody)
166167
DllImportEntry(RuntimeModule_GetScopeName)
167168
DllImportEntry(RuntimeModule_GetFullyQualifiedName)
168169
DllImportEntry(RuntimeModule_GetTypes)

src/coreclr/vm/runtimehandles.cpp

Lines changed: 107 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,170 +2039,160 @@ FCIMPL2(MethodDesc*, RuntimeMethodHandle::GetMethodFromCanonical, MethodDesc *pM
20392039
}
20402040
FCIMPLEND
20412041

2042-
2043-
FCIMPL2(RuntimeMethodBody *, RuntimeMethodHandle::GetMethodBody, ReflectMethodObject *pMethodUNSAFE, ReflectClassBaseObject *pDeclaringTypeUNSAFE)
2042+
extern "C" void QCALLTYPE RuntimeMethodHandle_GetMethodBody(MethodDesc* pMethod, EnregisteredTypeHandle pDeclaringType, QCall::ObjectHandleOnStack result)
20442043
{
2045-
CONTRACTL
2046-
{
2047-
FCALL_CHECK;
2048-
}
2049-
CONTRACTL_END;
2044+
QCALL_CONTRACT;
20502045

2051-
struct _gc
2046+
_ASSERTE(pMethod != NULL);
2047+
2048+
BEGIN_QCALL;
2049+
2050+
GCX_COOP();
2051+
2052+
struct
20522053
{
20532054
RUNTIMEMETHODBODYREF MethodBodyObj;
20542055
RUNTIMEEXCEPTIONHANDLINGCLAUSEREF EHClauseObj;
20552056
RUNTIMELOCALVARIABLEINFOREF RuntimeLocalVariableInfoObj;
2056-
U1ARRAYREF U1Array;
2057-
BASEARRAYREF TempArray;
2058-
REFLECTCLASSBASEREF declaringType;
2059-
REFLECTMETHODREF refMethod;
2057+
U1ARRAYREF U1Array;
2058+
BASEARRAYREF TempArray;
20602059
} gc;
2061-
20622060
gc.MethodBodyObj = NULL;
20632061
gc.EHClauseObj = NULL;
20642062
gc.RuntimeLocalVariableInfoObj = NULL;
20652063
gc.U1Array = NULL;
20662064
gc.TempArray = NULL;
2067-
gc.declaringType = (REFLECTCLASSBASEREF)ObjectToOBJECTREF(pDeclaringTypeUNSAFE);
2068-
gc.refMethod = (REFLECTMETHODREF)ObjectToOBJECTREF(pMethodUNSAFE);
2069-
2070-
2071-
if (!gc.refMethod)
2072-
FCThrowRes(kArgumentNullException, W("Arg_InvalidHandle"));
2073-
2074-
MethodDesc* pMethod = gc.refMethod->GetMethod();
2065+
GCPROTECT_BEGIN(gc);
20752066

2076-
TypeHandle declaringType = gc.declaringType == NULL ? TypeHandle() : gc.declaringType->GetType();
2067+
TypeHandle declaringType = TypeHandle::FromPtr(pDeclaringType);
20772068

2078-
if (!pMethod->IsIL())
2079-
return NULL;
2080-
2081-
HELPER_METHOD_FRAME_BEGIN_RET_PROTECT(gc);
2069+
COR_ILMETHOD* pILHeader = NULL;
2070+
if (pMethod->IsIL())
20822071
{
2083-
MethodDesc *pMethodIL = pMethod;
2072+
MethodDesc* pMethodIL = pMethod;
20842073
if (pMethod->IsWrapperStub())
20852074
pMethodIL = pMethod->GetWrappedMethodDesc();
20862075

2087-
COR_ILMETHOD* pILHeader = pMethodIL->GetILHeader();
2076+
pILHeader = pMethodIL->GetILHeader();
2077+
}
20882078

2089-
if (pILHeader)
2090-
{
2091-
MethodTable * pExceptionHandlingClauseMT = CoreLibBinder::GetClass(CLASS__RUNTIME_EH_CLAUSE);
2092-
TypeHandle thEHClauseArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pExceptionHandlingClauseMT), ELEMENT_TYPE_SZARRAY);
2079+
if (pILHeader)
2080+
{
2081+
MethodTable * pExceptionHandlingClauseMT = CoreLibBinder::GetClass(CLASS__RUNTIME_EH_CLAUSE);
2082+
TypeHandle thEHClauseArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pExceptionHandlingClauseMT), ELEMENT_TYPE_SZARRAY);
20932083

2094-
MethodTable * pLocalVariableMT = CoreLibBinder::GetClass(CLASS__RUNTIME_LOCAL_VARIABLE_INFO);
2095-
TypeHandle thLocalVariableArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pLocalVariableMT), ELEMENT_TYPE_SZARRAY);
2084+
MethodTable * pLocalVariableMT = CoreLibBinder::GetClass(CLASS__RUNTIME_LOCAL_VARIABLE_INFO);
2085+
TypeHandle thLocalVariableArray = ClassLoader::LoadArrayTypeThrowing(TypeHandle(pLocalVariableMT), ELEMENT_TYPE_SZARRAY);
20962086

2097-
Module* pModule = pMethod->GetModule();
2098-
COR_ILMETHOD_DECODER::DecoderStatus status;
2099-
COR_ILMETHOD_DECODER header(pILHeader, pModule->GetMDImport(), &status);
2087+
Module* pModule = pMethod->GetModule();
2088+
COR_ILMETHOD_DECODER::DecoderStatus status;
2089+
COR_ILMETHOD_DECODER header(pILHeader, pModule->GetMDImport(), &status);
21002090

2101-
if (status != COR_ILMETHOD_DECODER::SUCCESS)
2091+
if (status != COR_ILMETHOD_DECODER::SUCCESS)
2092+
{
2093+
if (status == COR_ILMETHOD_DECODER::VERIFICATION_ERROR)
21022094
{
2103-
if (status == COR_ILMETHOD_DECODER::VERIFICATION_ERROR)
2104-
{
2105-
// Throw a verification HR
2106-
COMPlusThrowHR(COR_E_VERIFICATION);
2107-
}
2108-
else
2109-
{
2110-
COMPlusThrowHR(COR_E_BADIMAGEFORMAT);
2111-
}
2095+
// Throw a verification HR
2096+
COMPlusThrowHR(COR_E_VERIFICATION);
21122097
}
2098+
else
2099+
{
2100+
COMPlusThrowHR(COR_E_BADIMAGEFORMAT);
2101+
}
2102+
}
21132103

2114-
gc.MethodBodyObj = (RUNTIMEMETHODBODYREF)AllocateObject(CoreLibBinder::GetClass(CLASS__RUNTIME_METHOD_BODY));
2104+
gc.MethodBodyObj = (RUNTIMEMETHODBODYREF)AllocateObject(CoreLibBinder::GetClass(CLASS__RUNTIME_METHOD_BODY));
21152105

2116-
gc.MethodBodyObj->_maxStackSize = header.GetMaxStack();
2117-
gc.MethodBodyObj->_initLocals = !!(header.GetFlags() & CorILMethod_InitLocals);
2106+
gc.MethodBodyObj->_maxStackSize = header.GetMaxStack();
2107+
gc.MethodBodyObj->_initLocals = !!(header.GetFlags() & CorILMethod_InitLocals);
21182108

2119-
if (header.IsFat())
2120-
gc.MethodBodyObj->_localVarSigToken = header.GetLocalVarSigTok();
2121-
else
2122-
gc.MethodBodyObj->_localVarSigToken = 0;
2109+
if (header.IsFat())
2110+
gc.MethodBodyObj->_localVarSigToken = header.GetLocalVarSigTok();
2111+
else
2112+
gc.MethodBodyObj->_localVarSigToken = 0;
21232113

2124-
// Allocate the array of IL and fill it in from the method header.
2125-
BYTE* pIL = const_cast<BYTE*>(header.Code);
2126-
COUNT_T cIL = header.GetCodeSize();
2127-
gc.U1Array = (U1ARRAYREF) AllocatePrimitiveArray(ELEMENT_TYPE_U1, cIL);
2114+
// Allocate the array of IL and fill it in from the method header.
2115+
BYTE* pIL = const_cast<BYTE*>(header.Code);
2116+
COUNT_T cIL = header.GetCodeSize();
2117+
gc.U1Array = (U1ARRAYREF) AllocatePrimitiveArray(ELEMENT_TYPE_U1, cIL);
21282118

2129-
SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_IL, gc.U1Array);
2130-
memcpyNoGCRefs(gc.MethodBodyObj->_IL->GetDataPtr(), pIL, cIL);
2119+
SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_IL, gc.U1Array);
2120+
memcpyNoGCRefs(gc.MethodBodyObj->_IL->GetDataPtr(), pIL, cIL);
21312121

2132-
// Allocate the array of exception clauses.
2133-
INT32 cEh = (INT32)header.EHCount();
2134-
const COR_ILMETHOD_SECT_EH* ehInfo = header.EH;
2135-
gc.TempArray = (BASEARRAYREF) AllocateSzArray(thEHClauseArray, cEh);
2122+
// Allocate the array of exception clauses.
2123+
INT32 cEh = (INT32)header.EHCount();
2124+
const COR_ILMETHOD_SECT_EH* ehInfo = header.EH;
2125+
gc.TempArray = (BASEARRAYREF) AllocateSzArray(thEHClauseArray, cEh);
21362126

2137-
SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_exceptionClauses, gc.TempArray);
2127+
SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_exceptionClauses, gc.TempArray);
21382128

2139-
for (INT32 i = 0; i < cEh; i++)
2140-
{
2141-
COR_ILMETHOD_SECT_EH_CLAUSE_FAT ehBuff;
2142-
const COR_ILMETHOD_SECT_EH_CLAUSE_FAT* ehClause =
2143-
(const COR_ILMETHOD_SECT_EH_CLAUSE_FAT*)ehInfo->EHClause(i, &ehBuff);
2129+
for (INT32 i = 0; i < cEh; i++)
2130+
{
2131+
COR_ILMETHOD_SECT_EH_CLAUSE_FAT ehBuff;
2132+
const COR_ILMETHOD_SECT_EH_CLAUSE_FAT* ehClause =
2133+
(const COR_ILMETHOD_SECT_EH_CLAUSE_FAT*)ehInfo->EHClause(i, &ehBuff);
21442134

2145-
gc.EHClauseObj = (RUNTIMEEXCEPTIONHANDLINGCLAUSEREF) AllocateObject(pExceptionHandlingClauseMT);
2135+
gc.EHClauseObj = (RUNTIMEEXCEPTIONHANDLINGCLAUSEREF) AllocateObject(pExceptionHandlingClauseMT);
21462136

2147-
gc.EHClauseObj->_flags = ehClause->GetFlags();
2148-
gc.EHClauseObj->_tryOffset = ehClause->GetTryOffset();
2149-
gc.EHClauseObj->_tryLength = ehClause->GetTryLength();
2150-
gc.EHClauseObj->_handlerOffset = ehClause->GetHandlerOffset();
2151-
gc.EHClauseObj->_handlerLength = ehClause->GetHandlerLength();
2137+
gc.EHClauseObj->_flags = ehClause->GetFlags();
2138+
gc.EHClauseObj->_tryOffset = ehClause->GetTryOffset();
2139+
gc.EHClauseObj->_tryLength = ehClause->GetTryLength();
2140+
gc.EHClauseObj->_handlerOffset = ehClause->GetHandlerOffset();
2141+
gc.EHClauseObj->_handlerLength = ehClause->GetHandlerLength();
21522142

2153-
if ((ehClause->GetFlags() & COR_ILEXCEPTION_CLAUSE_FILTER) == 0)
2154-
gc.EHClauseObj->_catchToken = ehClause->GetClassToken();
2155-
else
2156-
gc.EHClauseObj->_filterOffset = ehClause->GetFilterOffset();
2143+
if ((ehClause->GetFlags() & COR_ILEXCEPTION_CLAUSE_FILTER) == 0)
2144+
gc.EHClauseObj->_catchToken = ehClause->GetClassToken();
2145+
else
2146+
gc.EHClauseObj->_filterOffset = ehClause->GetFilterOffset();
21572147

2158-
gc.MethodBodyObj->_exceptionClauses->SetAt(i, (OBJECTREF) gc.EHClauseObj);
2159-
SetObjectReference((OBJECTREF*)&(gc.EHClauseObj->_methodBody), (OBJECTREF)gc.MethodBodyObj);
2160-
}
2148+
gc.MethodBodyObj->_exceptionClauses->SetAt(i, (OBJECTREF) gc.EHClauseObj);
2149+
SetObjectReference((OBJECTREF*)&(gc.EHClauseObj->_methodBody), (OBJECTREF)gc.MethodBodyObj);
2150+
}
21612151

2162-
if (header.LocalVarSig != NULL)
2152+
if (header.LocalVarSig != NULL)
2153+
{
2154+
SigTypeContext sigTypeContext(pMethod, declaringType, pMethod->LoadMethodInstantiation());
2155+
MetaSig metaSig(header.LocalVarSig,
2156+
header.cbLocalVarSig,
2157+
pModule,
2158+
&sigTypeContext,
2159+
MetaSig::sigLocalVars);
2160+
INT32 cLocals = metaSig.NumFixedArgs();
2161+
gc.TempArray = (BASEARRAYREF) AllocateSzArray(thLocalVariableArray, cLocals);
2162+
SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_localVariables, gc.TempArray);
2163+
2164+
for (INT32 i = 0; i < cLocals; i ++)
21632165
{
2164-
SigTypeContext sigTypeContext(pMethod, declaringType, pMethod->LoadMethodInstantiation());
2165-
MetaSig metaSig(header.LocalVarSig,
2166-
header.cbLocalVarSig,
2167-
pModule,
2168-
&sigTypeContext,
2169-
MetaSig::sigLocalVars);
2170-
INT32 cLocals = metaSig.NumFixedArgs();
2171-
gc.TempArray = (BASEARRAYREF) AllocateSzArray(thLocalVariableArray, cLocals);
2172-
SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_localVariables, gc.TempArray);
2173-
2174-
for (INT32 i = 0; i < cLocals; i ++)
2175-
{
2176-
gc.RuntimeLocalVariableInfoObj = (RUNTIMELOCALVARIABLEINFOREF)AllocateObject(pLocalVariableMT);
2166+
gc.RuntimeLocalVariableInfoObj = (RUNTIMELOCALVARIABLEINFOREF)AllocateObject(pLocalVariableMT);
21772167

2178-
gc.RuntimeLocalVariableInfoObj->_localIndex = i;
2168+
gc.RuntimeLocalVariableInfoObj->_localIndex = i;
21792169

2180-
metaSig.NextArg();
2170+
metaSig.NextArg();
21812171

2182-
CorElementType eType;
2183-
IfFailThrow(metaSig.GetArgProps().PeekElemType(&eType));
2184-
if (ELEMENT_TYPE_PINNED == eType)
2185-
gc.RuntimeLocalVariableInfoObj->_isPinned = TRUE;
2172+
CorElementType eType;
2173+
IfFailThrow(metaSig.GetArgProps().PeekElemType(&eType));
2174+
if (ELEMENT_TYPE_PINNED == eType)
2175+
gc.RuntimeLocalVariableInfoObj->_isPinned = TRUE;
21862176

2187-
TypeHandle tempType= metaSig.GetArgProps().GetTypeHandleThrowing(pModule, &sigTypeContext);
2188-
OBJECTREF refLocalType = tempType.GetManagedClassObject();
2189-
gc.RuntimeLocalVariableInfoObj->SetType(refLocalType);
2190-
gc.MethodBodyObj->_localVariables->SetAt(i, (OBJECTREF) gc.RuntimeLocalVariableInfoObj);
2191-
}
2192-
}
2193-
else
2194-
{
2195-
INT32 cLocals = 0;
2196-
gc.TempArray = (BASEARRAYREF) AllocateSzArray(thLocalVariableArray, cLocals);
2197-
SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_localVariables, gc.TempArray);
2177+
TypeHandle tempType= metaSig.GetArgProps().GetTypeHandleThrowing(pModule, &sigTypeContext);
2178+
OBJECTREF refLocalType = tempType.GetManagedClassObject();
2179+
gc.RuntimeLocalVariableInfoObj->SetType(refLocalType);
2180+
gc.MethodBodyObj->_localVariables->SetAt(i, (OBJECTREF) gc.RuntimeLocalVariableInfoObj);
21982181
}
21992182
}
2183+
else
2184+
{
2185+
INT32 cLocals = 0;
2186+
gc.TempArray = (BASEARRAYREF) AllocateSzArray(thLocalVariableArray, cLocals);
2187+
SetObjectReference((OBJECTREF*)&gc.MethodBodyObj->_localVariables, gc.TempArray);
2188+
}
22002189
}
2201-
HELPER_METHOD_FRAME_END();
22022190

2203-
return (RuntimeMethodBody*)OBJECTREFToObject(gc.MethodBodyObj);
2191+
result.Set(gc.MethodBodyObj);
2192+
2193+
GCPROTECT_END();
2194+
END_QCALL;
22042195
}
2205-
FCIMPLEND
22062196

22072197
FCIMPL1(FC_BOOL_RET, RuntimeMethodHandle::IsConstructor, MethodDesc *pMethod)
22082198
{

src/coreclr/vm/runtimehandles.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,6 @@ class RuntimeMethodHandle
225225
static
226226
FCDECL1(Object*, GetResolver, MethodDesc * pMethod);
227227

228-
229-
static FCDECL2(RuntimeMethodBody*, GetMethodBody, ReflectMethodObject *pMethodUNSAFE, PTR_ReflectClassBaseObject pDeclaringType);
230-
231228
static FCDECL1(FC_BOOL_RET, IsConstructor, MethodDesc *pMethod);
232229

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

261259
class RuntimeFieldHandle
262260
{

0 commit comments

Comments
 (0)