Fix DynamicMethodDesc::Destroy vs code heap enumeration race (#713)
authorJan Vorlicek <janvorli@microsoft.com>
Tue, 10 Dec 2019 11:40:49 +0000 (12:40 +0100)
committerGitHub <noreply@github.com>
Tue, 10 Dec 2019 11:40:49 +0000 (12:40 +0100)
commitbd920acbd6fedb86cc91376c9fc39aae9243a370
tree2c50f6e726f49364be536db661177d5e641e2613
parent20ab2fa963934b5823e0b8afbba858169dd27169
Fix DynamicMethodDesc::Destroy vs code heap enumeration race (#713)

There is a race between DynamicMethodDesc::Destroy called from
the finalizer thread and the MethodDescs enumeration called from
ETW::MethodLog::SendEventsForJitMethods at process exit.
DynamicMethodDesc::Destroy cleanos up its members m_pSig and
m_pszMethodName and then it calls GetLCGMethodResolver()->Destroy();
That calls EEJitManager::FreeCodeMemory, which tries to take the
m_CodeHeapCritSec lock. But this lock is already held by
the ETW::MethodLog::SendEventsForJitMethods.
So the iterator can see half-destroyed DynamicMethodDesc and
a crash happens when trying to get the dynamic method name
from the m_pszMethodName for the ETW event purposes.

The fix is to call the GetLCGMethodResolver()->Destroy() before
destroying the m_pSig and m_pszMethodName.
src/coreclr/src/vm/dynamicmethod.cpp