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

Fix Delegate dispatch. #2721

Merged
merged 2 commits into from
Oct 18, 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
34 changes: 23 additions & 11 deletions src/coreclr/vm/comdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1879,6 +1879,10 @@ Stub* COMDelegate::GetInvokeMethodStub(EEImplMethodDesc* pMD)
if (*pMD->GetSig() != (IMAGE_CEE_CS_CALLCONV_HASTHIS | IMAGE_CEE_CS_CALLCONV_DEFAULT))
COMPlusThrow(kInvalidProgramException);

PCCOR_SIGNATURE pSig;
DWORD cbSig;
pMD->GetSig(&pSig,&cbSig);

MetaSig sig(pMD);

BOOL fReturnVal = !sig.IsReturnTypeVoid();
Expand All @@ -1888,29 +1892,37 @@ Stub* COMDelegate::GetInvokeMethodStub(EEImplMethodDesc* pMD)

ILCodeStream *pCode = sl.NewCodeStream(ILStubLinker::kDispatch);

// This stub is only used for rare indirect cases, for example
// when Delegate.Invoke method is wrapped into another delegate.
// Direct invocation of delegate is expanded by JIT.
// Emit a recursive call here to let JIT handle complex cases like
// virtual dispatch and GC safety.

// Load the delegate object
pCode->EmitLoadThis();

// Load the arguments
for (UINT paramCount = 0; paramCount < sig.NumFixedArgs(); paramCount++)
pCode->EmitLDARG(paramCount);

// recursively call the delegate itself
#ifdef FEATURE_INTERPRETER
// Call the underlying method pointer.
pCode->EmitLoadThis();
pCode->EmitLDFLD(FIELD__DELEGATE__METHOD_PTR);

mdToken sigTok = pCode->GetSigToken(pSig, cbSig);
pCode->EmitCALLI(sigTok, sig.NumFixedArgs(), fReturnVal);
AaronRobinsonMSFT marked this conversation as resolved.
Show resolved Hide resolved

pCode->EmitLoadThis();
pCode->EmitCALL(METHOD__GC__KEEP_ALIVE, 1, 0);
#else
// This stub is only used for rare indirect cases, for example
// when Delegate.Invoke method is wrapped into another delegate.
// Direct invocation of delegate is expanded by JIT.
// Emit a recursive call here to let JIT handle complex cases like
// virtual dispatch and GC safety.

// Recursively call the delegate itself.
pCode->EmitCALL(pCode->GetToken(pMD), sig.NumFixedArgs(), fReturnVal);
#endif // !FEATURE_INTERPRETER

// return
pCode->EmitRET();

PCCOR_SIGNATURE pSig;
DWORD cbSig;
pMD->GetSig(&pSig,&cbSig);

MethodDesc* pStubMD = ILStubCache::CreateAndLinkNewILStubMethodDesc(pMD->GetLoaderAllocator(),
pMD->GetMethodTable(),
ILSTUB_DELEGATE_INVOKE_METHOD,
Expand Down
37 changes: 37 additions & 0 deletions src/coreclr/vm/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9289,6 +9289,11 @@ void Interpreter::DoCallWork(bool virtualCall, void* thisArg, CORINFO_RESOLVED_T
didIntrinsic = true;
break;

case NI_System_Runtime_CompilerServices_RuntimeHelpers_GetMethodTable:
DoGetMethodTable();
didIntrinsic = true;
break;

case NI_System_Threading_Interlocked_CompareExchange:
// Here and in other Interlocked.* intrinsics we use sigInfo.retType to be able
// to detect small-integer overloads.
Expand Down Expand Up @@ -10986,6 +10991,34 @@ void Interpreter::DoIsReferenceOrContainsReferences(CORINFO_METHOD_HANDLE method
m_curStackHt++;
}

void Interpreter::DoGetMethodTable()
{
CONTRACTL{
THROWS;
GC_TRIGGERS;
MODE_COOPERATIVE;
} CONTRACTL_END;

_ASSERTE(m_curStackHt > 0);
unsigned ind = m_curStackHt - 1;

#ifdef _DEBUG
_ASSERTE(OpStackTypeGet(ind).ToCorInfoType() == CORINFO_TYPE_CLASS);
#endif // _DEBUG

Object* obj = OpStackGet<Object*>(ind);

if (obj == NULL)
{
ThrowNullPointerException();
}

MethodTable* pMT = obj->RawGetMethodTable();

OpStackSet<MethodTable*>(m_curStackHt, pMT);
OpStackTypeSet(m_curStackHt, InterpreterType(CORINFO_TYPE_NATIVEINT));
}

bool Interpreter::DoInterlockedCompareExchange(CorInfoType retType)
{
CONTRACTL{
Expand Down Expand Up @@ -11992,6 +12025,10 @@ Interpreter::InterpreterNamedIntrinsics Interpreter::getNamedIntrinsicID(CEEInfo
{
result = NI_System_Runtime_CompilerServices_RuntimeHelpers_IsReferenceOrContainsReferences;
}
else if (strcmp(methodName, "GetMethodTable") == 0)
{
result = NI_System_Runtime_CompilerServices_RuntimeHelpers_GetMethodTable;
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/vm/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,7 @@ class Interpreter
NI_System_StubHelpers_GetStubContext,
NI_System_Runtime_InteropService_MemoryMarshal_GetArrayDataReference,
NI_System_Runtime_CompilerServices_RuntimeHelpers_IsReferenceOrContainsReferences,
NI_System_Runtime_CompilerServices_RuntimeHelpers_GetMethodTable,
NI_System_Threading_Interlocked_CompareExchange,
NI_System_Threading_Interlocked_Exchange,
NI_System_Threading_Interlocked_ExchangeAdd,
Expand Down Expand Up @@ -1797,6 +1798,7 @@ class Interpreter
void DoGetIsSupported();
void DoGetArrayDataReference();
void DoIsReferenceOrContainsReferences(CORINFO_METHOD_HANDLE method);
void DoGetMethodTable();
bool DoInterlockedCompareExchange(CorInfoType retType);
bool DoInterlockedExchange(CorInfoType retType);
bool DoInterlockedExchangeAdd(CorInfoType retType);
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/vm/stublink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1811,7 +1811,7 @@ bool StubLinker::EmitUnwindInfo(Stub* pStubRX, Stub* pStubRW, int globalsize, Lo
//

pHeaderRW->pNext = pStubHeapSegment->pUnwindHeaderList;
pStubHeapSegment->pUnwindHeaderList = pHeaderRW;
pStubHeapSegment->pUnwindHeaderList = pHeaderRX;

#ifdef TARGET_AMD64
// Publish Unwind info to ETW stack crawler
Expand All @@ -1826,7 +1826,7 @@ bool StubLinker::EmitUnwindInfo(Stub* pStubRX, Stub* pStubRW, int globalsize, Lo

#ifdef _DEBUG
_ASSERTE(pHeaderRW->IsRegistered());
_ASSERTE( &pHeaderRW->FunctionEntry
_ASSERTE( &pHeaderRX->FunctionEntry
== FindStubFunctionEntry((ULONG64)pCode, EncodeDynamicFunctionTableContext(pStubHeapSegment, DYNFNTABLE_STUB)));
#endif

Expand Down