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 GetCurrentMethod to QCALL #99852

Merged
merged 3 commits into from
Mar 17, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.

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

Expand Down Expand Up @@ -34,12 +36,16 @@ public abstract partial class MethodBase : MemberInfo
return RuntimeType.GetMethodBase(declaringType.GetRuntimeType(), handle.GetMethodInfo());
}

[LibraryImport(RuntimeHelpers.QCall, EntryPoint = "MethodBase_GetCurrentMethod")]
private static partial RuntimeMethodHandleInternal GetCurrentMethod(StackCrawlMarkHandle stackMark);

[RequiresUnreferencedCode("Metadata for the method might be incomplete or removed")]
[DynamicSecurityMethod] // Methods containing StackCrawlMark local var has to be marked DynamicSecurityMethod
public static MethodBase? GetCurrentMethod()
{
StackCrawlMark stackMark = StackCrawlMark.LookForMyCaller;
return RuntimeMethodInfo.InternalGetCurrentMethod(ref stackMark);
RuntimeMethodHandleInternal methodHandle = GetCurrentMethod(new StackCrawlMarkHandle(ref stackMark));
return methodHandle.IsNullHandle() ? null : RuntimeType.GetMethodBase(null, methodHandle);
}
#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,17 +488,5 @@ public override bool ContainsGenericParameters
}
}
#endregion

#region Legacy Internal
internal static MethodBase? InternalGetCurrentMethod(ref StackCrawlMark stackMark)
{
IRuntimeMethodInfo? method = RuntimeMethodHandle.GetCurrentMethod(ref stackMark);

if (method == null)
return null;

return RuntimeType.GetMethodBase(method);
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -844,13 +844,6 @@ internal static partial Interop.BOOL IsCAVisibleFromDecoratedType(
QCallTypeHandle sourceTypeHandle,
QCallModule sourceModule);

[MethodImpl(MethodImplOptions.InternalCall)]
private static extern IRuntimeMethodInfo? _GetCurrentMethod(ref StackCrawlMark stackMark);
internal static IRuntimeMethodInfo? GetCurrentMethod(ref StackCrawlMark stackMark)
{
return _GetCurrentMethod(ref stackMark);
}

[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern MethodAttributes GetAttributes(RuntimeMethodHandleInternal method);

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 @@ -196,7 +196,6 @@ FCFuncStart(gSignatureNative)
FCFuncEnd()

FCFuncStart(gRuntimeMethodHandle)
FCFuncElement("_GetCurrentMethod", RuntimeMethodHandle::GetCurrentMethod)
FCFuncElement("InvokeMethod", RuntimeMethodHandle::InvokeMethod)
FCFuncElement("ReboxFromNullable", RuntimeMethodHandle::ReboxFromNullable)
FCFuncElement("ReboxToNullable", RuntimeMethodHandle::ReboxToNullable)
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 @@ -118,6 +118,7 @@ static const Entry s_QCall[] =
DllImportEntry(RuntimeTypeHandle_GetActivationInfo)
DllImportEntry(RuntimeTypeHandle_AllocateTypeAssociatedMemory)
DllImportEntry(RuntimeTypeHandle_RegisterCollectibleTypeDependency)
DllImportEntry(MethodBase_GetCurrentMethod)
DllImportEntry(RuntimeMethodHandle_ConstructInstantiation)
DllImportEntry(RuntimeMethodHandle_GetFunctionPointer)
DllImportEntry(RuntimeMethodHandle_GetIsCollectible)
Expand Down
20 changes: 10 additions & 10 deletions src/coreclr/vm/reflectioninvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -890,11 +890,14 @@ static StackWalkAction SkipMethods(CrawlFrame* frame, VOID* data) {
}

// Return the MethodInfo that represents the current method (two above this one)
FCIMPL1(ReflectMethodObject*, RuntimeMethodHandle::GetCurrentMethod, StackCrawlMark* stackMark) {
FCALL_CONTRACT;
REFLECTMETHODREF pRet = NULL;
extern "C" MethodDesc* QCALLTYPE MethodBase_GetCurrentMethod(QCall::StackCrawlMarkHandle stackMark) {

QCALL_CONTRACT;

MethodDesc* pRet = nullptr;

BEGIN_QCALL;

HELPER_METHOD_FRAME_BEGIN_RET_0();
SkipStruct skip;
skip.pStackMark = stackMark;
skip.pMeth = 0;
Expand All @@ -905,15 +908,12 @@ FCIMPL1(ReflectMethodObject*, RuntimeMethodHandle::GetCurrentMethod, StackCrawlM
// is to return C<T>.m<P> and that's what LoadTypicalMethodDefinition will do for us.

if (skip.pMeth != NULL)
pRet = skip.pMeth->LoadTypicalMethodDefinition()->GetStubMethodInfo();
else
pRet = NULL;
pRet = skip.pMeth->LoadTypicalMethodDefinition();

HELPER_METHOD_FRAME_END();
END_QCALL;

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

static OBJECTREF DirectObjectFieldGet(FieldDesc *pField, TypeHandle fieldType, TypeHandle enclosingType, TypedByRef *pTarget, CLR_BOOL *pIsClassInitialized) {
CONTRACTL
Expand Down
5 changes: 3 additions & 2 deletions src/coreclr/vm/runtimehandles.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,6 @@ extern "C" void QCALLTYPE RuntimeTypeHandle_RegisterCollectibleTypeDependency(QC
class RuntimeMethodHandle {

public:
static FCDECL1(ReflectMethodObject*, GetCurrentMethod, StackCrawlMark* stackMark);

static FCDECL4(Object*, InvokeMethod, Object *target, PVOID* args, SignatureNative* pSig, CLR_BOOL fConstructor);

static FCDECL2(Object*, ReboxToNullable, Object *pBoxedValUNSAFE, ReflectClassBaseObject *pDestUNSAFE);
Expand Down Expand Up @@ -275,6 +273,9 @@ class RuntimeMethodHandle {
static FCDECL1(Object*, GetLoaderAllocator, MethodDesc *pMethod);
};


extern "C" MethodDesc* QCALLTYPE MethodBase_GetCurrentMethod(QCall::StackCrawlMarkHandle stackMark);

extern "C" BOOL QCALLTYPE RuntimeMethodHandle_IsCAVisibleFromDecoratedType(
QCall::TypeHandle targetTypeHandle,
MethodDesc * pTargetCtor,
Expand Down