Skip to content

Commit

Permalink
Fix BackoutJitData (#54711)
Browse files Browse the repository at this point in the history
* Fix BackoutJitData

The RemoveJitData that the BackoutJitData calls requires the code
header to be copied to the final location. This change fixes it.

I've also found that in one of my previous changes, I've accidentally
enabled jitting into a scratch buffer by default by adding the
FEATURE_WXORX define unconditionally. So I am removing it in this change
for non Apple Silicon, it will be replaced by a dynamic check whether W^X 
is enabled in the final W^X change.
  • Loading branch information
janvorli authored Jun 25, 2021
1 parent de5dd0d commit 3924d03
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/coreclr/vm/codeman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2685,7 +2685,11 @@ void EEJitManager::allocCode(MethodDesc* pMD, size_t blockSize, size_t reserveFo
pCodeHdr = ((CodeHeader *)pCode) - 1;

*pAllocatedSize = sizeof(CodeHeader) + totalSize;
#define FEATURE_WXORX

#if defined(HOST_OSX) && defined(HOST_ARM64)
#define FEATURE_WXORX
#endif

#ifdef FEATURE_WXORX
pCodeHdrRW = (CodeHeader *)new BYTE[*pAllocatedSize];
#else
Expand Down
40 changes: 26 additions & 14 deletions src/coreclr/vm/jitinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11211,6 +11211,27 @@ void CEEJitInfo::GetProfilingHandle(bool *pbHookFunction,
*pbIndirectedHandles = false;
}

/*********************************************************************/
void CEEJitInfo::WriteCodeBytes()
{
LIMITED_METHOD_CONTRACT;

#ifdef USE_INDIRECT_CODEHEADER
if (m_pRealCodeHeader != NULL)
{
// Restore the read only version of the real code header
m_CodeHeaderRW->SetRealCodeHeader(m_pRealCodeHeader);
m_pRealCodeHeader = NULL;
}
#endif // USE_INDIRECT_CODEHEADER

if (m_CodeHeaderRW != m_CodeHeader)
{
ExecutableWriterHolder<void> codeWriterHolder((void *)m_CodeHeader, m_codeWriteBufferSize);
memcpy(codeWriterHolder.GetRW(), m_CodeHeaderRW, m_codeWriteBufferSize);
}
}

/*********************************************************************/
void CEEJitInfo::BackoutJitData(EEJitManager * jitMgr)
{
Expand All @@ -11219,6 +11240,10 @@ void CEEJitInfo::BackoutJitData(EEJitManager * jitMgr)
GC_TRIGGERS;
} CONTRACTL_END;

// The RemoveJitData call below requires the m_CodeHeader to be valid, so we need to write
// the code bytes to the target memory location.
WriteCodeBytes();

CodeHeader* pCodeHeader = m_CodeHeader;
if (pCodeHeader)
jitMgr->RemoveJitData(pCodeHeader, m_GCinfo_len, m_EHinfo_len);
Expand All @@ -11232,20 +11257,7 @@ void CEEJitInfo::WriteCode(EEJitManager * jitMgr)
GC_TRIGGERS;
} CONTRACTL_END;

#ifdef USE_INDIRECT_CODEHEADER
if (m_pRealCodeHeader != NULL)
{
// Restore the read only version of the real code header
m_CodeHeaderRW->SetRealCodeHeader(m_pRealCodeHeader);
m_pRealCodeHeader = NULL;
}
#endif // USE_INDIRECT_CODEHEADER

if (m_CodeHeaderRW != m_CodeHeader)
{
ExecutableWriterHolder<void> codeWriterHolder((void *)m_CodeHeader, m_codeWriteBufferSize);
memcpy(codeWriterHolder.GetRW(), m_CodeHeaderRW, m_codeWriteBufferSize);
}
WriteCodeBytes();

// Now that the code header was written to the final location, publish the code via the nibble map
jitMgr->NibbleMapSet(m_pCodeHeap, m_CodeHeader->GetCodeStartAddress(), TRUE);
Expand Down
2 changes: 2 additions & 0 deletions src/coreclr/vm/jitinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,8 @@ class CEEJitInfo : public CEEInfo

protected :

void WriteCodeBytes();

#ifdef FEATURE_PGO
// PGO data
struct ComputedPgoData
Expand Down

0 comments on commit 3924d03

Please sign in to comment.