Skip to content

Commit 4194cdb

Browse files
committed
move cfg registration oop
1 parent e0b628d commit 4194cdb

File tree

7 files changed

+79
-93
lines changed

7 files changed

+79
-93
lines changed

lib/Backend/Encoder.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ Encoder::Encode()
341341

342342
m_func->GetJITOutput()->SetCodeAddress(m_func->GetJITOutput()->GetCodeAddress() | 0x1); // Set thumb mode
343343
#endif
344+
345+
m_func->GetThreadContextInfo()->SetValidCallTargetForCFG((PVOID)m_func->GetJITOutput()->GetCodeAddress());
346+
344347
const bool isSimpleJit = m_func->IsSimpleJit();
345348

346349
if (this->m_inlineeFrameMap->Count() > 0 &&

lib/Backend/NativeCodeGenerator.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,6 @@ NativeCodeGenerator::CodeGen(PageAllocator * pageAllocator, CodeGenWorkItem* wor
11001100
}
11011101
#endif
11021102

1103-
scriptContext->GetThreadContext()->SetValidCallTargetForCFG((PVOID)jitWriteData.codeAddress);
11041103
workItem->SetCodeAddress((size_t)jitWriteData.codeAddress);
11051104

11061105
workItem->GetEntryPoint()->SetCodeGenRecorded((Js::JavascriptMethod)jitWriteData.codeAddress, jitWriteData.codeSize);
@@ -3215,8 +3214,11 @@ NativeCodeGenerator::QueueFreeNativeCodeGenAllocation(void* address)
32153214
return;
32163215
}
32173216

3218-
//DeRegister Entry Point for CFG
3219-
ThreadContext::GetContextForCurrentThread()->SetValidCallTargetForCFG(address, false);
3217+
if (!JITManager::GetJITManager()->IsOOPJITEnabled())
3218+
{
3219+
//DeRegister Entry Point for CFG
3220+
ThreadContext::GetContextForCurrentThread()->SetValidCallTargetForCFG(address, false);
3221+
}
32203222

32213223
// The foreground allocators may have been used
32223224
ThreadContext * context = this->scriptContext->GetThreadContext();

lib/JITServer/JITServer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ ServerFreeAllocation(
317317
return RPC_S_INVALID_ARG;
318318
}
319319

320+
//DeRegister Entry Point for CFG
321+
context->SetValidCallTargetForCFG((PVOID)address, false);
322+
320323
bool succeeded = context->GetCodeGenAllocators()->emitBufferManager.FreeAllocation((void*)address);
321324
return succeeded ? S_OK : E_FAIL;
322325
}

lib/Runtime/Base/ThreadContext.cpp

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4311,14 +4311,6 @@ void ThreadContext::ClearThreadContextFlag(ThreadContextFlags contextFlag)
43114311
}
43124312

43134313
#ifdef ENABLE_GLOBALIZATION
4314-
#ifdef _CONTROL_FLOW_GUARD
4315-
Js::DelayLoadWinCoreMemory * ThreadContext::GetWinCoreMemoryLibrary()
4316-
{
4317-
delayLoadWinCoreMemoryLibrary.EnsureFromSystemDirOnly();
4318-
return &delayLoadWinCoreMemoryLibrary;
4319-
}
4320-
#endif
4321-
43224314
Js::DelayLoadWinRtString * ThreadContext::GetWinRTStringLibrary()
43234315
{
43244316
delayLoadWinRtString.EnsureFromSystemDirOnly();
@@ -4378,80 +4370,6 @@ Js::DelayLoadWinRtFoundation* ThreadContext::GetWinRtFoundationLibrary()
43784370
#endif
43794371
#endif // ENABLE_GLOBALIZATION
43804372

4381-
bool ThreadContext::IsCFGEnabled()
4382-
{
4383-
#if defined(_CONTROL_FLOW_GUARD)
4384-
PROCESS_MITIGATION_CONTROL_FLOW_GUARD_POLICY CfgPolicy;
4385-
BOOL isGetMitigationPolicySucceeded = GetWinCoreProcessThreads()->GetMitigationPolicyForProcess(
4386-
GetCurrentProcess(),
4387-
ProcessControlFlowGuardPolicy,
4388-
&CfgPolicy,
4389-
sizeof(CfgPolicy));
4390-
Assert(isGetMitigationPolicySucceeded || !AutoSystemInfo::Data.IsCFGEnabled());
4391-
return CfgPolicy.EnableControlFlowGuard && AutoSystemInfo::Data.IsCFGEnabled();
4392-
#else
4393-
return false;
4394-
#endif
4395-
}
4396-
4397-
4398-
//Masking bits according to AutoSystemInfo::PageSize
4399-
#define PAGE_START_ADDR(address) ((size_t)(address) & ~(size_t)(AutoSystemInfo::PageSize - 1))
4400-
#define IS_16BYTE_ALIGNED(address) (((size_t)(address) & 0xF) == 0)
4401-
#define OFFSET_ADDR_WITHIN_PAGE(address) ((size_t)(address) & (AutoSystemInfo::PageSize - 1))
4402-
4403-
void ThreadContext::SetValidCallTargetForCFG(PVOID callTargetAddress, bool isSetValid)
4404-
{
4405-
#ifdef _CONTROL_FLOW_GUARD
4406-
if (IsCFGEnabled())
4407-
{
4408-
AssertMsg(IS_16BYTE_ALIGNED(callTargetAddress), "callTargetAddress is not 16-byte page aligned?");
4409-
4410-
PVOID startAddressOfPage = (PVOID) (PAGE_START_ADDR(callTargetAddress));
4411-
size_t codeOffset = OFFSET_ADDR_WITHIN_PAGE(callTargetAddress);
4412-
4413-
CFG_CALL_TARGET_INFO callTargetInfo[1];
4414-
4415-
callTargetInfo[0].Offset = codeOffset;
4416-
callTargetInfo[0].Flags = (isSetValid? CFG_CALL_TARGET_VALID : 0);
4417-
4418-
AssertMsg((size_t)callTargetAddress - (size_t)startAddressOfPage <= AutoSystemInfo::PageSize - 1, "Only last bits corresponding to PageSize should be masked");
4419-
AssertMsg((size_t)startAddressOfPage + (size_t)codeOffset == (size_t)callTargetAddress, "Wrong masking of address?");
4420-
4421-
BOOL isCallTargetRegistrationSucceed = GetWinCoreMemoryLibrary()->SetProcessCallTargets(GetCurrentProcess(), startAddressOfPage, AutoSystemInfo::PageSize, 1, callTargetInfo);
4422-
4423-
if (!isCallTargetRegistrationSucceed)
4424-
{
4425-
if (GetLastError() == ERROR_COMMITMENT_LIMIT)
4426-
{
4427-
//Throw OOM, if there is not enough virtual memory for paging (required for CFG BitMap)
4428-
Js::Throw::OutOfMemory();
4429-
}
4430-
else
4431-
{
4432-
Js::Throw::InternalError();
4433-
}
4434-
}
4435-
#if DBG
4436-
if (isSetValid)
4437-
{
4438-
_guard_check_icall((uintptr_t) callTargetAddress);
4439-
}
4440-
4441-
if (PHASE_TRACE1(Js::CFGPhase))
4442-
{
4443-
if (!isSetValid)
4444-
{
4445-
Output::Print(_u("DEREGISTER:"));
4446-
}
4447-
Output::Print(_u("CFGRegistration: StartAddr: 0x%p , Offset: 0x%x, TargetAddr: 0x%x \n"), (char*) startAddressOfPage, callTargetInfo[0].Offset, ((size_t) startAddressOfPage + (size_t) callTargetInfo[0].Offset));
4448-
Output::Flush();
4449-
}
4450-
#endif
4451-
}
4452-
#endif // _CONTROL_FLOW_GUARD
4453-
}
4454-
44554373
// Despite the name, callers expect this to return the highest propid + 1.
44564374

44574375
uint ThreadContext::GetHighestPropertyNameIndex() const

lib/Runtime/Base/ThreadContext.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -798,9 +798,6 @@ class ThreadContext sealed :
798798
Js::DelayLoadWinRtFoundation delayLoadWinRtFoundationLibrary;
799799
Js::WindowsFoundationAdapter windowsFoundationAdapter;
800800
#endif
801-
#ifdef _CONTROL_FLOW_GUARD
802-
Js::DelayLoadWinCoreMemory delayLoadWinCoreMemoryLibrary;
803-
#endif
804801
#endif
805802

806803
// Number of script context attached with probe manager.
@@ -894,9 +891,6 @@ class ThreadContext sealed :
894891
Js::DelayLoadWinRtFoundation *GetWinRtFoundationLibrary();
895892
Js::WindowsFoundationAdapter *GetWindowsFoundationAdapter();
896893
#endif
897-
#ifdef _CONTROL_FLOW_GUARD
898-
Js::DelayLoadWinCoreMemory * GetWinCoreMemoryLibrary();
899-
#endif
900894
#endif
901895

902896
#ifdef ENABLE_BASIC_TELEMETRY
@@ -947,8 +941,6 @@ class ThreadContext sealed :
947941
return res;
948942
}
949943

950-
bool IsCFGEnabled();
951-
void SetValidCallTargetForCFG(PVOID callTargetAddress, bool isSetValid = true);
952944
BOOL HasPreviousHostScriptContext();
953945
HostScriptContext* GetPreviousHostScriptContext() ;
954946
void PushHostScriptContext(HostScriptContext* topProvider);

lib/Runtime/Base/ThreadContextInfo.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,13 @@ ThreadContextInfo::GetWinCoreProcessThreads()
325325
m_delayLoadWinCoreProcessThreads.EnsureFromSystemDirOnly();
326326
return &m_delayLoadWinCoreProcessThreads;
327327
}
328+
329+
Js::DelayLoadWinCoreMemory *
330+
ThreadContextInfo::GetWinCoreMemoryLibrary()
331+
{
332+
m_delayLoadWinCoreMemoryLibrary.EnsureFromSystemDirOnly();
333+
return &m_delayLoadWinCoreMemoryLibrary;
334+
}
328335
#endif
329336

330337
bool
@@ -346,6 +353,64 @@ ThreadContextInfo::IsCFGEnabled()
346353
}
347354
#endif // ENABLE_GLOBALIZATION
348355

356+
//Masking bits according to AutoSystemInfo::PageSize
357+
#define PAGE_START_ADDR(address) ((size_t)(address) & ~(size_t)(AutoSystemInfo::PageSize - 1))
358+
#define IS_16BYTE_ALIGNED(address) (((size_t)(address) & 0xF) == 0)
359+
#define OFFSET_ADDR_WITHIN_PAGE(address) ((size_t)(address) & (AutoSystemInfo::PageSize - 1))
360+
361+
void
362+
ThreadContextInfo::SetValidCallTargetForCFG(PVOID callTargetAddress, bool isSetValid)
363+
{
364+
#ifdef _CONTROL_FLOW_GUARD
365+
if (IsCFGEnabled())
366+
{
367+
AssertMsg(IS_16BYTE_ALIGNED(callTargetAddress), "callTargetAddress is not 16-byte page aligned?");
368+
369+
PVOID startAddressOfPage = (PVOID)(PAGE_START_ADDR(callTargetAddress));
370+
size_t codeOffset = OFFSET_ADDR_WITHIN_PAGE(callTargetAddress);
371+
372+
CFG_CALL_TARGET_INFO callTargetInfo[1];
373+
374+
callTargetInfo[0].Offset = codeOffset;
375+
callTargetInfo[0].Flags = (isSetValid ? CFG_CALL_TARGET_VALID : 0);
376+
377+
AssertMsg((size_t)callTargetAddress - (size_t)startAddressOfPage <= AutoSystemInfo::PageSize - 1, "Only last bits corresponding to PageSize should be masked");
378+
AssertMsg((size_t)startAddressOfPage + (size_t)codeOffset == (size_t)callTargetAddress, "Wrong masking of address?");
379+
380+
BOOL isCallTargetRegistrationSucceed = GetWinCoreMemoryLibrary()->SetProcessCallTargets(GetProcessHandle(), startAddressOfPage, AutoSystemInfo::PageSize, 1, callTargetInfo);
381+
382+
if (!isCallTargetRegistrationSucceed)
383+
{
384+
if (GetLastError() == ERROR_COMMITMENT_LIMIT)
385+
{
386+
//Throw OOM, if there is not enough virtual memory for paging (required for CFG BitMap)
387+
Js::Throw::OutOfMemory();
388+
}
389+
else
390+
{
391+
Js::Throw::InternalError();
392+
}
393+
}
394+
#if DBG
395+
if (isSetValid && !JITManager::GetJITManager()->IsOOPJITEnabled())
396+
{
397+
_guard_check_icall((uintptr_t)callTargetAddress);
398+
}
399+
400+
if (PHASE_TRACE1(Js::CFGPhase))
401+
{
402+
if (!isSetValid)
403+
{
404+
Output::Print(_u("DEREGISTER:"));
405+
}
406+
Output::Print(_u("CFGRegistration: StartAddr: 0x%p , Offset: 0x%x, TargetAddr: 0x%x \n"), (char*)startAddressOfPage, callTargetInfo[0].Offset, ((size_t)startAddressOfPage + (size_t)callTargetInfo[0].Offset));
407+
Output::Flush();
408+
}
409+
#endif
410+
}
411+
#endif // _CONTROL_FLOW_GUARD
412+
}
413+
349414
void
350415
ThreadContextInfo::BeginJIT()
351416
{

lib/Runtime/Base/ThreadContextInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class ThreadContextInfo
6767
intptr_t GetStringMatchNameAddr() const;
6868
#endif
6969

70+
void SetValidCallTargetForCFG(PVOID callTargetAddress, bool isSetValid = true);
7071
void ResetIsAllJITCodeInPreReservedRegion();
7172
bool IsAllJITCodeInPreReservedRegion() const;
7273

@@ -104,8 +105,10 @@ class ThreadContextInfo
104105
bool IsJITActive();
105106

106107
#if defined(ENABLE_GLOBALIZATION) && defined(_CONTROL_FLOW_GUARD)
108+
Js::DelayLoadWinCoreMemory * GetWinCoreMemoryLibrary();
107109
Js::DelayLoadWinCoreProcessThreads * GetWinCoreProcessThreads();
108110

111+
Js::DelayLoadWinCoreMemory m_delayLoadWinCoreMemoryLibrary;
109112
Js::DelayLoadWinCoreProcessThreads m_delayLoadWinCoreProcessThreads;
110113
#endif
111114
protected:

0 commit comments

Comments
 (0)