@@ -229,6 +229,16 @@ InterpreterPrecode* Precode::AllocateInterpreterPrecode(PCODE byteCode,
229229
230230 InterpreterPrecode* pPrecode = (InterpreterPrecode*)pamTracker->Track (pLoaderAllocator->GetNewStubPrecodeHeap ()->AllocStub ());
231231 pPrecode->Init (pPrecode, byteCode);
232+
233+ #ifdef FEATURE_CORECLR_FLUSH_INSTRUCTION_CACHE_TO_PROTECT_STUB_READS
234+ // While the allocation of a precode will use a memory mapping technique, and not actually write to the set of instructions,
235+ // the set of instructions in the stub has non-barrier protected reads from the StubPrecodeData structure. In order to protect those
236+ // reads we would either need barrier instructions in the stub, or we need to ensure that the precode is flushed in the instruction cache
237+ // which will have the side effect of ensuring that the reads within the stub will happen *after* the writes to the StubPrecodeData structure which
238+ // happened in the Init routine above.
239+ ClrFlushInstructionCache (pPrecode->GetCode (), Precode::SizeOf (t));
240+ #endif // FEATURE_CORECLR_FLUSH_INSTRUCTION_CACHE_TO_PROTECT_STUB_READS
241+
232242#ifdef FEATURE_PERFMAP
233243 PerfMap::LogStubs (__FUNCTION__, " UMEntryThunk" , (PCODE)pPrecode, sizeof (InterpreterPrecode), PerfMapStubType::IndividualWithinBlock);
234244#endif
@@ -265,6 +275,16 @@ Precode* Precode::Allocate(PrecodeType t, MethodDesc* pMD,
265275 ThisPtrRetBufPrecodeData *pData = (ThisPtrRetBufPrecodeData*)pamTracker->Track (pLoaderAllocator->GetLowFrequencyHeap ()->AllocMem (S_SIZE_T (sizeof (ThisPtrRetBufPrecodeData))));
266276 pThisPtrRetBufPrecode->Init (pData, pMD, pLoaderAllocator);
267277 pPrecode = (Precode*)pThisPtrRetBufPrecode;
278+
279+ #ifdef FEATURE_CORECLR_FLUSH_INSTRUCTION_CACHE_TO_PROTECT_STUB_READS
280+ // While the allocation of a precode will use a memory mapping technique, and not actually write to the set of instructions,
281+ // the set of instructions in the stub has non-barrier protected reads from the StubPrecodeData structure. In order to protect those
282+ // reads we would either need barrier instructions in the stub, or we need to ensure that the precode is flushed in the instruction cache
283+ // which will have the side effect of ensuring that the reads within the stub will happen *after* the writes to the StubPrecodeData structure which
284+ // happened in the Init routine above.
285+ ClrFlushInstructionCache (pPrecode->GetCode (), Precode::SizeOf (t));
286+ #endif // FEATURE_CORECLR_FLUSH_INSTRUCTION_CACHE_TO_PROTECT_STUB_READS
287+
268288#ifdef FEATURE_PERFMAP
269289 PerfMap::LogStubs (__FUNCTION__, " ThisPtrRetBuf" , (PCODE)pPrecode, sizeof (ThisPtrRetBufPrecodeData), PerfMapStubType::IndividualWithinBlock);
270290#endif
@@ -275,6 +295,16 @@ Precode* Precode::Allocate(PrecodeType t, MethodDesc* pMD,
275295 _ASSERTE (t == PRECODE_STUB || t == PRECODE_NDIRECT_IMPORT);
276296 pPrecode = (Precode*)pamTracker->Track (pLoaderAllocator->GetNewStubPrecodeHeap ()->AllocStub ());
277297 pPrecode->Init (pPrecode, t, pMD, pLoaderAllocator);
298+
299+ #ifdef FEATURE_CORECLR_FLUSH_INSTRUCTION_CACHE_TO_PROTECT_STUB_READS
300+ // While the allocation of a precode will use a memory mapping technique, and not actually write to the set of instructions,
301+ // the set of instructions in the stub has non-barrier protected reads from the StubPrecodeData structure. In order to protect those
302+ // reads we would either need barrier instructions in the stub, or we need to ensure that the precode is flushed in the instruction cache
303+ // which will have the side effect of ensuring that the reads within the stub will happen *after* the writes to the StubPrecodeData structure which
304+ // happened in the Init routine above.
305+ ClrFlushInstructionCache (pPrecode->GetCode (), Precode::SizeOf (t));
306+ #endif // FEATURE_CORECLR_FLUSH_INSTRUCTION_CACHE_TO_PROTECT_STUB_READS
307+
278308#ifdef FEATURE_PERFMAP
279309 PerfMap::LogStubs (__FUNCTION__, t == PRECODE_STUB ? " StubPrecode" : " PInvokeImportPrecode" , (PCODE)pPrecode, sizeof (StubPrecode), PerfMapStubType::IndividualWithinBlock);
280310#endif
0 commit comments