Skip to content

Commit bee1e24

Browse files
MikeHolmanMSLaguana
authored andcommitted
[CVE-2018-8130] [CVE-2018-0946] move allocators to ServerScriptContext, add missing marshalling code
1 parent e664e18 commit bee1e24

13 files changed

+52
-39
lines changed

lib/Backend/NativeCodeGenerator.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3257,8 +3257,7 @@ NativeCodeGenerator::FreeNativeCodeGenAllocation(void* codeAddress)
32573257
#if PDATA_ENABLED && defined(_WIN32)
32583258
DelayDeletingFunctionTable::Clear();
32593259
#endif
3260-
ThreadContext * context = this->scriptContext->GetThreadContext();
3261-
HRESULT hr = JITManager::GetJITManager()->FreeAllocation(context->GetRemoteThreadContextAddr(), (intptr_t)codeAddress);
3260+
HRESULT hr = JITManager::GetJITManager()->FreeAllocation(this->scriptContext->GetRemoteScriptAddr(), (intptr_t)codeAddress);
32623261
JITManager::HandleServerCallResult(hr, RemoteCallType::MemFree);
32633262
}
32643263
else if(this->backgroundAllocators)

lib/Backend/ServerScriptContext.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,19 @@ ServerScriptContext::ServerScriptContext(ScriptContextDataIDL * contextData, Ser
2626
m_asmJsInterpreterThunkBufferManager(&m_sourceCodeArena, threadContextInfo->GetThunkPageAllocators(), nullptr, threadContextInfo, _u("Asm.js interpreter thunk buffer"), GetThreadContext()->GetProcessHandle()),
2727
m_domFastPathHelperMap(nullptr),
2828
m_moduleRecords(&HeapAllocator::Instance),
29+
m_codeGenAlloc(nullptr, nullptr, threadContextInfo, threadContextInfo->GetCodePageAllocators(), threadContextInfo->GetProcessHandle()),
2930
m_globalThisAddr(0),
3031
#ifdef PROFILE_EXEC
3132
m_codeGenProfiler(nullptr),
3233
#endif
3334
m_refCount(0),
3435
m_isClosed(false)
3536
{
37+
38+
#if !TARGET_64 && _CONTROL_FLOW_GUARD
39+
m_codeGenAlloc.canCreatePreReservedSegment = threadContextInfo->CanCreatePreReservedSegment();
40+
#endif
41+
3642
#ifdef PROFILE_EXEC
3743
if (Js::Configuration::Global.flags.IsEnabled(Js::ProfileFlag))
3844
{
@@ -357,7 +363,9 @@ ServerScriptContext::Close()
357363
{
358364
Assert(!IsClosed());
359365
m_isClosed = true;
360-
366+
367+
m_codeGenAlloc.emitBufferManager.Decommit();
368+
361369
#ifdef STACK_BACK_TRACE
362370
ServerContextManager::RecordCloseContext(this);
363371
#endif
@@ -381,6 +389,12 @@ ServerScriptContext::Release()
381389
}
382390
}
383391

392+
OOPCodeGenAllocators *
393+
ServerScriptContext::GetCodeGenAllocators()
394+
{
395+
return &m_codeGenAlloc;
396+
}
397+
384398
Field(Js::Var)*
385399
ServerScriptContext::GetModuleExportSlotArrayAddress(uint moduleIndex, uint slotIndex)
386400
{

lib/Backend/ServerScriptContext.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class ServerScriptContext : public ScriptContextInfo
8787
Js::ScriptContextProfiler * GetCodeGenProfiler() const;
8888
ServerThreadContext* GetThreadContext() { return threadContextHolder.threadContextInfo; }
8989

90+
OOPCodeGenAllocators * GetCodeGenAllocators();
9091
ArenaAllocator * GetSourceCodeArena();
9192
void Close();
9293
void AddRef();
@@ -107,6 +108,8 @@ class ServerScriptContext : public ScriptContextInfo
107108

108109
uint m_refCount;
109110

111+
OOPCodeGenAllocators m_codeGenAlloc;
112+
110113
bool m_isPRNGSeeded;
111114
bool m_isClosed;
112115
#endif

lib/Backend/ServerThreadContext.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,21 @@ ServerThreadContext::ServerThreadContext(ThreadContextDataIDL* data, ProcessCont
1414
m_numericPropertyBV(nullptr),
1515
m_preReservedSectionAllocator(processContext->processHandle),
1616
m_sectionAllocator(processContext->processHandle),
17-
m_thunkPageAllocators(nullptr, /* allocXData */ false, &m_sectionAllocator, nullptr, processContext->processHandle),
1817
m_codePageAllocators(nullptr, ALLOC_XDATA, &m_sectionAllocator, &m_preReservedSectionAllocator, processContext->processHandle),
18+
m_thunkPageAllocators(nullptr, /* allocXData */ false, &m_sectionAllocator, nullptr, processContext->processHandle),
1919
#if defined(_CONTROL_FLOW_GUARD) && !defined(_M_ARM)
2020
m_jitThunkEmitter(this, &m_sectionAllocator, processContext->processHandle),
2121
#endif
22-
m_codeGenAlloc(nullptr, nullptr, this, &m_codePageAllocators, processContext->processHandle),
2322
m_pageAlloc(nullptr, Js::Configuration::Global.flags, PageAllocatorType_BGJIT,
2423
AutoSystemInfo::Data.IsLowMemoryProcess() ?
2524
PageAllocator::DefaultLowMaxFreePageCount :
2625
PageAllocator::DefaultMaxFreePageCount
2726
),
28-
processContext(processContext)
27+
processContext(processContext),
28+
m_canCreatePreReservedSegment(data->allowPrereserveAlloc != FALSE)
2929
{
3030
m_pid = GetProcessId(processContext->processHandle);
3131

32-
#if !TARGET_64 && _CONTROL_FLOW_GUARD
33-
m_codeGenAlloc.canCreatePreReservedSegment = data->allowPrereserveAlloc != FALSE;
34-
#endif
3532
m_numericPropertyBV = HeapNew(BVSparse<HeapAllocator>, &HeapAllocator::Instance);
3633
}
3734

@@ -121,22 +118,16 @@ ServerThreadContext::GetThunkPageAllocators()
121118
return &m_thunkPageAllocators;
122119
}
123120

124-
CustomHeap::OOPCodePageAllocators *
125-
ServerThreadContext::GetCodePageAllocators()
126-
{
127-
return &m_codePageAllocators;
128-
}
129-
130121
SectionAllocWrapper *
131122
ServerThreadContext::GetSectionAllocator()
132123
{
133124
return &m_sectionAllocator;
134125
}
135126

136-
OOPCodeGenAllocators *
137-
ServerThreadContext::GetCodeGenAllocators()
127+
CustomHeap::OOPCodePageAllocators *
128+
ServerThreadContext::GetCodePageAllocators()
138129
{
139-
return &m_codeGenAlloc;
130+
return &m_codePageAllocators;
140131
}
141132

142133
#if defined(_CONTROL_FLOW_GUARD) && !defined(_M_ARM)
@@ -172,6 +163,12 @@ ServerThreadContext::GetForegroundPageAllocator()
172163
return &m_pageAlloc;
173164
}
174165

166+
bool
167+
ServerThreadContext::CanCreatePreReservedSegment() const
168+
{
169+
return m_canCreatePreReservedSegment;
170+
}
171+
175172
bool
176173
ServerThreadContext::IsNumericProperty(Js::PropertyId propertyId)
177174
{

lib/Backend/ServerThreadContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ class ServerThreadContext : public ThreadContextInfo
5555
virtual ptrdiff_t GetChakraBaseAddressDifference() const override;
5656
virtual ptrdiff_t GetCRTBaseAddressDifference() const override;
5757

58-
OOPCodeGenAllocators * GetCodeGenAllocators();
5958
#if defined(_CONTROL_FLOW_GUARD) && !defined(_M_ARM)
6059
OOPJITThunkEmitter * GetJITThunkEmitter();
6160
#endif
@@ -72,6 +71,7 @@ class ServerThreadContext : public ThreadContextInfo
7271

7372
intptr_t GetRuntimeChakraBaseAddress() const;
7473
intptr_t GetRuntimeCRTBaseAddress() const;
74+
bool CanCreatePreReservedSegment() const;
7575

7676
static intptr_t GetJITCRTBaseAddress();
7777

@@ -84,7 +84,6 @@ class ServerThreadContext : public ThreadContextInfo
8484
SectionAllocWrapper m_sectionAllocator;
8585
CustomHeap::OOPCodePageAllocators m_thunkPageAllocators;
8686
CustomHeap::OOPCodePageAllocators m_codePageAllocators;
87-
OOPCodeGenAllocators m_codeGenAlloc;
8887
#if defined(_CONTROL_FLOW_GUARD) && !defined(_M_ARM)
8988
OOPJITThunkEmitter m_jitThunkEmitter;
9089
#endif
@@ -96,5 +95,6 @@ class ServerThreadContext : public ThreadContextInfo
9695

9796
CriticalSection m_cs;
9897
uint m_refCount;
98+
bool m_canCreatePreReservedSegment;
9999
};
100100
#endif

lib/JITClient/JITManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,15 +586,15 @@ JITManager::CloseScriptContext(
586586

587587
HRESULT
588588
JITManager::FreeAllocation(
589-
__in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
589+
__in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
590590
__in intptr_t codeAddress)
591591
{
592592
Assert(IsOOPJITEnabled());
593593

594594
HRESULT hr = E_FAIL;
595595
RpcTryExcept
596596
{
597-
hr = ClientFreeAllocation(m_rpcBindingHandle, threadContextInfoAddress, codeAddress);
597+
hr = ClientFreeAllocation(m_rpcBindingHandle, scriptContextInfoAddress, codeAddress);
598598
}
599599
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
600600
{

lib/JITClient/JITManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class JITManager
7979
__in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress);
8080

8181
HRESULT FreeAllocation(
82-
__in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
82+
__in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
8383
__in intptr_t codeAddress);
8484

8585
HRESULT SetIsPRNGSeeded(
@@ -213,7 +213,7 @@ class JITManager
213213
{ Assert(false); return E_FAIL; }
214214

215215
HRESULT FreeAllocation(
216-
__in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
216+
__in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
217217
__in intptr_t codeAddress)
218218
{ Assert(false); return E_FAIL; }
219219

lib/JITIDL/ChakraJIT.idl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ interface IChakraJIT
8585

8686
HRESULT FreeAllocation(
8787
[in] handle_t binding,
88-
[in] PTHREADCONTEXT_HANDLE threadContextInfoAddress,
88+
[in] PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
8989
[in] CHAKRA_PTR codeAddress);
9090

9191
HRESULT NewInterpreterThunkBlock(

lib/JITServer/JITServer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -676,10 +676,10 @@ ServerIsInterpreterThunkAddr(
676676
HRESULT
677677
ServerFreeAllocation(
678678
/* [in] */ handle_t binding,
679-
/* [in] */ __RPC__in PTHREADCONTEXT_HANDLE threadContextInfo,
679+
/* [in] */ __RPC__in PSCRIPTCONTEXT_HANDLE scriptContextInfo,
680680
/* [in] */ intptr_t codeAddress)
681681
{
682-
ServerThreadContext * context = (ServerThreadContext*)DecodePointer(threadContextInfo);
682+
ServerScriptContext* context = (ServerScriptContext*)DecodePointer(scriptContextInfo);
683683

684684
if (context == nullptr)
685685
{
@@ -709,7 +709,7 @@ ServerIsNativeAddr(
709709

710710
*result = false;
711711

712-
ServerThreadContext * context = (ServerThreadContext*)DecodePointer(threadContextInfo);
712+
ServerThreadContext* context = (ServerThreadContext*)DecodePointer(threadContextInfo);
713713
if (context == nullptr)
714714
{
715715
Assert(false);
@@ -850,7 +850,7 @@ ServerRemoteCodeGen(
850850
nullptr,
851851
nullptr,
852852
jitWorkItem->GetPolymorphicInlineCacheInfo(),
853-
threadContextInfo->GetCodeGenAllocators(),
853+
scriptContextInfo->GetCodeGenAllocators(),
854854
#if !FLOATVAR
855855
nullptr, // number allocator
856856
#endif

lib/Runtime/Base/ScriptContext.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,13 @@ namespace Js
701701
}
702702
#endif
703703

704+
#if ENABLE_NATIVE_CODEGEN
705+
if (m_remoteScriptContextAddr)
706+
{
707+
JITManager::GetJITManager()->CloseScriptContext(m_remoteScriptContextAddr);
708+
}
709+
#endif
710+
704711
#ifdef ENABLE_SCRIPT_PROFILING
705712
// Stop profiling if present
706713
DeRegisterProfileProbe(S_OK, nullptr);
@@ -1452,12 +1459,6 @@ namespace Js
14521459
#endif
14531460
}
14541461

1455-
#if ENABLE_NATIVE_CODEGEN
1456-
if (m_remoteScriptContextAddr)
1457-
{
1458-
JITManager::GetJITManager()->CloseScriptContext(m_remoteScriptContextAddr);
1459-
}
1460-
#endif
14611462
this->PrintStats();
14621463
}
14631464
}

0 commit comments

Comments
 (0)