1010// ----------------------------------------------------------------------------
1111template <typename TAlloc, typename TPreReservedAlloc, typename SyncObject>
1212EmitBufferManager<TAlloc, TPreReservedAlloc, SyncObject>::EmitBufferManager(ArenaAllocator * allocator, CustomHeap::CodePageAllocators<TAlloc, TPreReservedAlloc> * codePageAllocators,
13- Js::ScriptContext * scriptContext, LPCWSTR name, HANDLE processHandle) :
13+ Js::ScriptContext * scriptContext, ThreadContextInfo * threadContext, LPCWSTR name, HANDLE processHandle) :
1414 allocationHeap (allocator, codePageAllocators, processHandle),
1515 allocator(allocator),
1616 allocations(nullptr ),
1717 scriptContext(scriptContext),
18+ threadContext(threadContext),
1819 processHandle(processHandle)
1920{
2021#if DBG_DUMP
@@ -193,12 +194,14 @@ bool
193194EmitBufferManager<TAlloc, TPreReservedAlloc, SyncObject>::FreeAllocation(void * address)
194195{
195196 AutoRealOrFakeCriticalSection<SyncObject> autoCs (&this ->criticalSection );
196-
197+ #if _M_ARM
198+ address = (void *)((uintptr_t )address & ~0x1 ); // clear the thumb bit
199+ #endif
197200 TEmitBufferAllocation* previous = nullptr ;
198201 TEmitBufferAllocation* allocation = allocations;
199202 while (allocation != nullptr )
200203 {
201- if (address >= allocation->allocation ->address && address < (allocation-> allocation -> address + allocation-> bytesUsed ) )
204+ if (address == allocation->allocation ->address )
202205 {
203206 if (previous == nullptr )
204207 {
@@ -214,6 +217,26 @@ EmitBufferManager<TAlloc, TPreReservedAlloc, SyncObject>::FreeAllocation(void* a
214217 this ->scriptContext ->GetThreadContext ()->SubCodeSize (allocation->bytesCommitted );
215218 }
216219
220+ #if defined(_CONTROL_FLOW_GUARD) && (_M_IX86 || _M_X64)
221+ if (allocation->allocation ->thunkAddress )
222+ {
223+ if (JITManager::GetJITManager ()->IsJITServer ())
224+ {
225+ ((ServerThreadContext*)this ->threadContext )->GetJITThunkEmitter ()->FreeThunk (allocation->allocation ->thunkAddress );
226+ }
227+ else
228+ {
229+ ((ThreadContext*)this ->threadContext )->GetJITThunkEmitter ()->FreeThunk (allocation->allocation ->thunkAddress );
230+ }
231+ }
232+ else
233+ #endif
234+ {
235+ if (!JITManager::GetJITManager ()->IsJITServer () || CONFIG_FLAG (OOPCFGRegistration))
236+ {
237+ threadContext->SetValidCallTargetForCFG (address, false );
238+ }
239+ }
217240 VerboseHeapTrace (_u (" Freeing 0x%p, allocation: 0x%p\n " ), address, allocation->allocation ->address );
218241
219242 this ->allocationHeap .Free (allocation->allocation );
0 commit comments