Skip to content

Commit 3924d03

Browse files
authored
Fix BackoutJitData (#54711)
* 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.
1 parent de5dd0d commit 3924d03

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/coreclr/vm/codeman.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2685,7 +2685,11 @@ void EEJitManager::allocCode(MethodDesc* pMD, size_t blockSize, size_t reserveFo
26852685
pCodeHdr = ((CodeHeader *)pCode) - 1;
26862686

26872687
*pAllocatedSize = sizeof(CodeHeader) + totalSize;
2688-
#define FEATURE_WXORX
2688+
2689+
#if defined(HOST_OSX) && defined(HOST_ARM64)
2690+
#define FEATURE_WXORX
2691+
#endif
2692+
26892693
#ifdef FEATURE_WXORX
26902694
pCodeHdrRW = (CodeHeader *)new BYTE[*pAllocatedSize];
26912695
#else

src/coreclr/vm/jitinterface.cpp

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11211,6 +11211,27 @@ void CEEJitInfo::GetProfilingHandle(bool *pbHookFunction,
1121111211
*pbIndirectedHandles = false;
1121211212
}
1121311213

11214+
/*********************************************************************/
11215+
void CEEJitInfo::WriteCodeBytes()
11216+
{
11217+
LIMITED_METHOD_CONTRACT;
11218+
11219+
#ifdef USE_INDIRECT_CODEHEADER
11220+
if (m_pRealCodeHeader != NULL)
11221+
{
11222+
// Restore the read only version of the real code header
11223+
m_CodeHeaderRW->SetRealCodeHeader(m_pRealCodeHeader);
11224+
m_pRealCodeHeader = NULL;
11225+
}
11226+
#endif // USE_INDIRECT_CODEHEADER
11227+
11228+
if (m_CodeHeaderRW != m_CodeHeader)
11229+
{
11230+
ExecutableWriterHolder<void> codeWriterHolder((void *)m_CodeHeader, m_codeWriteBufferSize);
11231+
memcpy(codeWriterHolder.GetRW(), m_CodeHeaderRW, m_codeWriteBufferSize);
11232+
}
11233+
}
11234+
1121411235
/*********************************************************************/
1121511236
void CEEJitInfo::BackoutJitData(EEJitManager * jitMgr)
1121611237
{
@@ -11219,6 +11240,10 @@ void CEEJitInfo::BackoutJitData(EEJitManager * jitMgr)
1121911240
GC_TRIGGERS;
1122011241
} CONTRACTL_END;
1122111242

11243+
// The RemoveJitData call below requires the m_CodeHeader to be valid, so we need to write
11244+
// the code bytes to the target memory location.
11245+
WriteCodeBytes();
11246+
1122211247
CodeHeader* pCodeHeader = m_CodeHeader;
1122311248
if (pCodeHeader)
1122411249
jitMgr->RemoveJitData(pCodeHeader, m_GCinfo_len, m_EHinfo_len);
@@ -11232,20 +11257,7 @@ void CEEJitInfo::WriteCode(EEJitManager * jitMgr)
1123211257
GC_TRIGGERS;
1123311258
} CONTRACTL_END;
1123411259

11235-
#ifdef USE_INDIRECT_CODEHEADER
11236-
if (m_pRealCodeHeader != NULL)
11237-
{
11238-
// Restore the read only version of the real code header
11239-
m_CodeHeaderRW->SetRealCodeHeader(m_pRealCodeHeader);
11240-
m_pRealCodeHeader = NULL;
11241-
}
11242-
#endif // USE_INDIRECT_CODEHEADER
11243-
11244-
if (m_CodeHeaderRW != m_CodeHeader)
11245-
{
11246-
ExecutableWriterHolder<void> codeWriterHolder((void *)m_CodeHeader, m_codeWriteBufferSize);
11247-
memcpy(codeWriterHolder.GetRW(), m_CodeHeaderRW, m_codeWriteBufferSize);
11248-
}
11260+
WriteCodeBytes();
1124911261

1125011262
// Now that the code header was written to the final location, publish the code via the nibble map
1125111263
jitMgr->NibbleMapSet(m_pCodeHeap, m_CodeHeader->GetCodeStartAddress(), TRUE);

src/coreclr/vm/jitinterface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,8 @@ class CEEJitInfo : public CEEInfo
941941

942942
protected :
943943

944+
void WriteCodeBytes();
945+
944946
#ifdef FEATURE_PGO
945947
// PGO data
946948
struct ComputedPgoData

0 commit comments

Comments
 (0)