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 x86 InlinedCallFrame popping during EH #2215

Merged
merged 4 commits into from
Feb 3, 2020
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
5 changes: 0 additions & 5 deletions src/coreclr/src/vm/assemblynative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ void QCALLTYPE AssemblyNative::InternalLoad(QCall::ObjectHandleOnStack assemblyN

GCX_COOP();

// Workaround for https://github.com/dotnet/runtime/issues/2240
FrameWithCookie<ProtectValueClassFrame> workaround;

if (assemblyName.Get() == NULL)
{
COMPlusThrow(kArgumentNullException, W("ArgumentNull_AssemblyName"));
Expand Down Expand Up @@ -130,8 +127,6 @@ void QCALLTYPE AssemblyNative::InternalLoad(QCall::ObjectHandleOnStack assemblyN
retAssembly.Set(pAssembly->GetExposedObject());
}

workaround.Pop();

END_QCALL;
}

Expand Down
50 changes: 34 additions & 16 deletions src/coreclr/src/vm/i386/excepx86.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1868,21 +1868,6 @@ NOINLINE LPVOID COMPlusEndCatchWorker(Thread * pThread)
// Sync managed exception state, for the managed thread, based upon any active exception tracker
pThread->SyncManagedExceptionState(fIsDebuggerHelperThread);

if (InlinedCallFrame::FrameHasActiveCall(pThread->m_pFrame))
{
// When unwinding an exception in ReadyToRun, the JIT_PInvokeEnd helper which unlinks the ICF from
// the thread will be skipped. This is because unlike jitted code, each pinvoke is wrapped by calls
// to the JIT_PInvokeBegin and JIT_PInvokeEnd helpers, which push and pop the ICF on the thread. The
// ICF is not linked at the method prolog and unlined at the epilog when running R2R code. Since the
// JIT_PInvokeEnd helper will be skipped, we need to unlink the ICF here. If the executing method
// has another pinovoke, it will re-link the ICF again when the JIT_PInvokeBegin helper is called

if (ExecutionManager::IsReadyToRunCode(((InlinedCallFrame*)pThread->m_pFrame)->m_pCallerReturnAddress))
{
pThread->m_pFrame->Pop(pThread);
}
}

LOG((LF_EH, LL_INFO1000, "COMPlusPEndCatch: esp=%p\n", esp));

return esp;
Expand Down Expand Up @@ -3138,6 +3123,39 @@ void ResumeAtJitEH(CrawlFrame* pCf,
*pHandlerEnd = EHClausePtr->HandlerEndPC;
}

MethodDesc* pMethodDesc = pCf->GetCodeInfo()->GetMethodDesc();
TADDR startAddress = pCf->GetCodeInfo()->GetStartAddress();
if (InlinedCallFrame::FrameHasActiveCall(pThread->m_pFrame))
{
// When unwinding an exception in ReadyToRun, the JIT_PInvokeEnd helper which unlinks the ICF from
// the thread will be skipped. This is because unlike jitted code, each pinvoke is wrapped by calls
// to the JIT_PInvokeBegin and JIT_PInvokeEnd helpers, which push and pop the ICF on the thread. The
// ICF is not linked at the method prolog and unlinked at the epilog when running R2R code. Since the
// JIT_PInvokeEnd helper will be skipped, we need to unlink the ICF here. If the executing method
// has another pinvoke, it will re-link the ICF again when the JIT_PInvokeBegin helper is called.

// Check that the InlinedCallFrame is in the method with the exception handler. There can be other
// InlinedCallFrame somewhere up the call chain that is not related to the current exception
// handling.

#ifdef DEBUG
TADDR handlerFrameSP = pCf->GetRegisterSet()->SP;
#endif // DEBUG
// Find the ESP of the caller of the method with the exception handler.
bool unwindSuccess = pCf->GetCodeManager()->UnwindStackFrame(pCf->GetRegisterSet(),
pCf->GetCodeInfo(),
pCf->GetCodeManagerFlags(),
pCf->GetCodeManState(),
NULL /* StackwalkCacheUnwindInfo* */);
_ASSERTE(unwindSuccess);

if (((TADDR)pThread->m_pFrame < pCf->GetRegisterSet()->SP) && ExecutionManager::IsReadyToRunCode(((InlinedCallFrame*)pThread->m_pFrame)->m_pCallerReturnAddress))
{
_ASSERTE((TADDR)pThread->m_pFrame >= handlerFrameSP);
pThread->m_pFrame->Pop(pThread);
}
}

// save esp so that endcatch can restore it (it always restores, so want correct value)
ExInfo* pExInfo = &(pThread->GetExceptionState()->m_currentExInfo);
pExInfo->m_dEsp = (LPVOID)context.GetSP();
Expand Down Expand Up @@ -3251,7 +3269,7 @@ void ResumeAtJitEH(CrawlFrame* pCf,
// that the handle for the current ExInfo has been freed has been delivered
pExInfo->m_EHClauseInfo.SetManagedCodeEntered(TRUE);

ETW::ExceptionLog::ExceptionCatchBegin(pCf->GetCodeInfo()->GetMethodDesc(), (PVOID)pCf->GetCodeInfo()->GetStartAddress());
ETW::ExceptionLog::ExceptionCatchBegin(pMethodDesc, (PVOID)startAddress);

ResumeAtJitEHHelper(&context);
UNREACHABLE_MSG("Should never return from ResumeAtJitEHHelper!");
Expand Down