Skip to content

Commit e80c1fe

Browse files
committed
Bug fixes for Section API
1 parent bc52eb0 commit e80c1fe

9 files changed

Lines changed: 46 additions & 121 deletions

File tree

lib/Backend/ServerThreadContext.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "JITServer/JITServer.h"
1010

1111
ServerThreadContext::ServerThreadContext(ThreadContextDataIDL * data) :
12+
m_autoProcessHandle((HANDLE)data->processHandle),
1213
m_threadContextData(*data),
1314
m_refCount(0),
1415
m_numericPropertyBV(nullptr),

lib/Backend/ServerThreadContext.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ class ServerThreadContext : public ThreadContextInfo
5252
intptr_t GetRuntimeChakraBaseAddress() const;
5353
intptr_t GetRuntimeCRTBaseAddress() const;
5454

55+
class AutoCloseHandle
56+
{
57+
public:
58+
AutoCloseHandle(HANDLE handle) : handle(handle) { Assert(handle != GetCurrentProcess()); }
59+
~AutoCloseHandle() { CloseHandle(this->handle); }
60+
private:
61+
HANDLE handle;
62+
};
63+
64+
AutoCloseHandle m_autoProcessHandle;
65+
5566
BVSparse<HeapAllocator> * m_numericPropertyBV;
5667

5768
PreReservedSectionAllocWrapper m_preReservedSectionAllocator;

lib/Common/Memory/CustomHeap.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,10 @@ Allocation* Heap<TAlloc, TPreReservedAlloc>::AllocLargeObject(size_t bytes, usho
402402
}
403403

404404
char* localAddr = this->codePageAllocators->AllocLocal(address, pages*AutoSystemInfo::PageSize, segment);
405+
if (!localAddr)
406+
{
407+
return nullptr;
408+
}
405409
FillDebugBreak((BYTE*)localAddr, pages*AutoSystemInfo::PageSize);
406410
this->codePageAllocators->FreeLocal(localAddr, segment);
407411

@@ -660,6 +664,10 @@ Page* Heap<TAlloc, TPreReservedAlloc>::AllocNewPage(BucketId bucket, bool canAll
660664
}
661665

662666
char* localAddr = this->codePageAllocators->AllocLocal(address, AutoSystemInfo::PageSize, pageSegment);
667+
if (!localAddr)
668+
{
669+
return nullptr;
670+
}
663671
FillDebugBreak((BYTE*)localAddr, AutoSystemInfo::PageSize);
664672
this->codePageAllocators->FreeLocal(localAddr, pageSegment);
665673

@@ -816,6 +824,11 @@ bool Heap<TAlloc, TPreReservedAlloc>::FreeAllocation(Allocation* object)
816824

817825
// Fill the old buffer with debug breaks
818826
char* localAddr = this->codePageAllocators->AllocLocal(object->address, object->size, page->segment);
827+
if (!localAddr)
828+
{
829+
MemoryOperationLastError::CheckProcessAndThrowFatalError(this->processHandle);
830+
return false;
831+
}
819832
FillDebugBreak((BYTE*)localAddr, object->size);
820833
this->codePageAllocators->FreeLocal(localAddr, page->segment);
821834

@@ -883,9 +896,15 @@ void Heap<TAlloc, TPreReservedAlloc>::FreeAllocationHelper(Allocation* object, B
883896

884897
// Fill the old buffer with debug breaks
885898
char* localAddr = this->codePageAllocators->AllocLocal(object->address, object->size, page->segment);
886-
FillDebugBreak((BYTE*)localAddr, object->size);
887-
this->codePageAllocators->FreeLocal(localAddr, page->segment);
888-
899+
if (localAddr)
900+
{
901+
FillDebugBreak((BYTE*)localAddr, object->size);
902+
this->codePageAllocators->FreeLocal(localAddr, page->segment);
903+
}
904+
else
905+
{
906+
MemoryOperationLastError::CheckProcessAndThrowFatalError(this->processHandle);
907+
}
889908
VerboseHeapTrace(_u("Setting %d bits starting at bit %d, Free bit vector in page was "), length, index);
890909
#if VERBOSE_HEAP
891910
page->freeBitVector.DumpWord();

lib/JITClient/JITManager.cpp

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,13 @@ JITManager::JITManager() :
3030
m_rpcBindingHandle(nullptr),
3131
m_oopJitEnabled(false),
3232
m_isJITServer(false),
33-
m_targetHandle(nullptr),
3433
m_serverHandle(nullptr),
3534
m_jitConnectionId()
3635
{
3736
}
3837

3938
JITManager::~JITManager()
4039
{
41-
if (m_targetHandle)
42-
{
43-
CleanupProcess();
44-
}
4540
if (m_rpcBindingHandle)
4641
{
4742
RpcBindingFree(&m_rpcBindingHandle);
@@ -192,7 +187,7 @@ bool
192187
JITManager::IsConnected() const
193188
{
194189
Assert(IsOOPJITEnabled());
195-
return m_rpcBindingHandle != nullptr && m_targetHandle != nullptr;
190+
return m_rpcBindingHandle != nullptr;
196191
}
197192

198193
HANDLE
@@ -213,23 +208,11 @@ JITManager::IsOOPJITEnabled() const
213208
return m_oopJitEnabled;
214209
}
215210

216-
HANDLE
217-
JITManager::GetJITTargetHandle() const
218-
{
219-
if (!IsOOPJITEnabled())
220-
{
221-
return GetCurrentProcess();
222-
}
223-
Assert(m_targetHandle != nullptr);
224-
return m_targetHandle;
225-
}
226-
227211
HRESULT
228212
JITManager::ConnectRpcServer(__in HANDLE jitProcessHandle, __in_opt void* serverSecurityDescriptor, __in UUID connectionUuid)
229213
{
230214
Assert(IsOOPJITEnabled());
231215
Assert(m_rpcBindingHandle == nullptr);
232-
Assert(m_targetHandle == nullptr);
233216
Assert(m_serverHandle == nullptr);
234217

235218
HRESULT hr = E_FAIL;
@@ -246,12 +229,6 @@ JITManager::ConnectRpcServer(__in HANDLE jitProcessHandle, __in_opt void* server
246229
goto FailureCleanup;
247230
}
248231

249-
if (!DuplicateHandle(GetCurrentProcess(), GetCurrentProcess(), jitProcessHandle, &m_targetHandle, 0, FALSE, DUPLICATE_SAME_ACCESS))
250-
{
251-
hr = HRESULT_FROM_WIN32(GetLastError());
252-
goto FailureCleanup;
253-
}
254-
255232
hr = CreateBinding(jitProcessHandle, serverSecurityDescriptor, &connectionUuid, &m_rpcBindingHandle);
256233
if (FAILED(hr))
257234
{
@@ -263,11 +240,6 @@ JITManager::ConnectRpcServer(__in HANDLE jitProcessHandle, __in_opt void* server
263240
return hr;
264241

265242
FailureCleanup:
266-
if (m_targetHandle)
267-
{
268-
CloseHandle(m_targetHandle);
269-
m_targetHandle = nullptr;
270-
}
271243
if (m_serverHandle)
272244
{
273245
CloseHandle(m_serverHandle);
@@ -282,28 +254,6 @@ JITManager::ConnectRpcServer(__in HANDLE jitProcessHandle, __in_opt void* server
282254
return hr;
283255
}
284256

285-
HRESULT
286-
JITManager::CleanupProcess()
287-
{
288-
Assert(JITManager::IsOOPJITEnabled());
289-
Assert(m_targetHandle != nullptr);
290-
291-
HRESULT hr = E_FAIL;
292-
RpcTryExcept
293-
{
294-
hr = ClientCleanupProcess(m_rpcBindingHandle, (intptr_t)m_targetHandle);
295-
}
296-
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
297-
{
298-
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
299-
}
300-
RpcEndExcept;
301-
302-
m_targetHandle = nullptr;
303-
304-
return hr;
305-
}
306-
307257
HRESULT
308258
JITManager::Shutdown()
309259
{
@@ -313,8 +263,6 @@ JITManager::Shutdown()
313263
Assert(IsOOPJITEnabled());
314264
Assert(m_rpcBindingHandle != nullptr);
315265

316-
CleanupProcess();
317-
318266
RpcTryExcept
319267
{
320268
ClientShutdown(m_rpcBindingHandle);

lib/JITClient/JITManager.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class JITManager
2828
bool IsOOPJITEnabled() const;
2929
void EnableOOPJIT();
3030

31-
HANDLE GetJITTargetHandle() const;
3231
HANDLE GetServerHandle() const;
3332

3433
HRESULT InitializeThreadContext(
@@ -67,8 +66,6 @@ class JITManager
6766
__in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
6867
__out PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress);
6968

70-
HRESULT CleanupProcess();
71-
7269
HRESULT CleanupScriptContext(
7370
__inout PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress);
7471

@@ -109,7 +106,6 @@ class JITManager
109106
__out RPC_BINDING_HANDLE* bindingHandle);
110107

111108
RPC_BINDING_HANDLE m_rpcBindingHandle;
112-
HANDLE m_targetHandle;
113109
HANDLE m_serverHandle;
114110
UUID m_jitConnectionId;
115111
bool m_oopJitEnabled;
@@ -132,8 +128,6 @@ class JITManager
132128
bool IsOOPJITEnabled() const { return false; }
133129
void EnableOOPJIT() { Assert(false); }
134130

135-
HANDLE GetJITTargetHandle() const
136-
{ Assert(false); return HANDLE(); }
137131
HANDLE GetServerHandle() const
138132
{
139133
Assert(false); return HANDLE();
@@ -177,9 +171,6 @@ class JITManager
177171
__out PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
178172
{ Assert(false); return E_FAIL; }
179173

180-
HRESULT CleanupProcess()
181-
{ Assert(false); return E_FAIL; }
182-
183174
HRESULT CleanupScriptContext(
184175
__inout PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
185176
{ Assert(false); return E_FAIL; }

lib/JITIDL/ChakraJIT.idl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ interface IChakraJIT
1919
[out] PPTHREADCONTEXT_HANDLE threadContextInfoAddress,
2020
[out] CHAKRA_PTR * prereservedRegionAddr);
2121

22-
HRESULT CleanupProcess(
23-
[in] handle_t binding,
24-
[in] CHAKRA_PTR processHandle);
25-
2622
HRESULT CleanupThreadContext(
2723
[in] handle_t binding,
2824
[in, out] PPTHREADCONTEXT_HANDLE threadContextInfoAddress);

lib/JITServer/JITServer.cpp

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,6 @@ ServerShutdown(
123123
return ShutdownCommon();
124124
}
125125

126-
HRESULT
127-
ServerCleanupProcess(
128-
/* [in] */ handle_t binding,
129-
/* [in] */ intptr_t processHandle)
130-
{
131-
ServerContextManager::CleanUpForProcess((HANDLE)processHandle);
132-
CloseHandle((HANDLE)processHandle);
133-
return S_OK;
134-
}
135-
136-
137126
void
138127
__RPC_USER PTHREADCONTEXT_HANDLE_rundown(__RPC__in PTHREADCONTEXT_HANDLE phContext)
139128
{
@@ -174,6 +163,7 @@ ServerInitializeThreadContext(
174163
}
175164
catch (Js::OutOfMemoryException)
176165
{
166+
CloseHandle((HANDLE)threadContextData->processHandle);
177167
return E_OUTOFMEMORY;
178168
}
179169

@@ -730,42 +720,6 @@ void ServerContextManager::UnRegisterThreadContext(ServerThreadContext* threadCo
730720
}
731721
}
732722

733-
void ServerContextManager::CleanUpForProcess(HANDLE hProcess)
734-
{
735-
// there might be multiple thread context(webworker)
736-
AutoCriticalSection autoCS(&cs);
737-
738-
auto iterScriptCtx = scriptContexts.GetIteratorWithRemovalSupport();
739-
while (iterScriptCtx.IsValid())
740-
{
741-
ServerScriptContext* scriptContext = iterScriptCtx.Current().Key();
742-
if (scriptContext->GetThreadContext()->GetProcessHandle() == hProcess)
743-
{
744-
if (!scriptContext->IsClosed())
745-
{
746-
scriptContext->Close();
747-
}
748-
iterScriptCtx.RemoveCurrent();
749-
}
750-
iterScriptCtx.MoveNext();
751-
}
752-
753-
auto iterThreadCtx = threadContexts.GetIteratorWithRemovalSupport();
754-
while (iterThreadCtx.IsValid())
755-
{
756-
ServerThreadContext* threadContext = iterThreadCtx.Current().Key();
757-
if (threadContext->GetProcessHandle() == hProcess)
758-
{
759-
if (!threadContext->IsClosed())
760-
{
761-
threadContext->Close();
762-
}
763-
iterThreadCtx.RemoveCurrent();
764-
}
765-
iterThreadCtx.MoveNext();
766-
}
767-
}
768-
769723
void ServerContextManager::RegisterScriptContext(ServerScriptContext* scriptContext)
770724
{
771725
AutoCriticalSection autoCS(&cs);

lib/JITServer/JITServer.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ class ServerContextManager
99
static void RegisterThreadContext(ServerThreadContext* threadContext);
1010
static void UnRegisterThreadContext(ServerThreadContext* threadContext);
1111

12-
static void CleanUpForProcess(HANDLE hProcess);
13-
1412
static void RegisterScriptContext(ServerScriptContext* scriptContext);
15-
static void UnRegisterScriptContext(ServerScriptContext* scriptContext);
16-
13+
static void UnRegisterScriptContext(ServerScriptContext* scriptContext);
1714
static bool CheckLivenessAndAddref(ServerScriptContext* context);
1815
static bool CheckLivenessAndAddref(ServerThreadContext* context);
1916

lib/Runtime/Base/ThreadContext.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1959,7 +1959,15 @@ ThreadContext::EnsureJITThreadContext(bool allowPrereserveAlloc)
19591959
}
19601960

19611961
ThreadContextDataIDL contextData;
1962-
contextData.processHandle = (intptr_t)JITManager::GetJITManager()->GetJITTargetHandle();
1962+
HANDLE serverHandle = JITManager::GetJITManager()->GetServerHandle();
1963+
1964+
HANDLE jitTargetHandle = nullptr;
1965+
if (!DuplicateHandle(GetCurrentProcess(), GetCurrentProcess(), serverHandle, &jitTargetHandle, 0, FALSE, DUPLICATE_SAME_ACCESS))
1966+
{
1967+
return;
1968+
}
1969+
1970+
contextData.processHandle = (intptr_t)jitTargetHandle;
19631971

19641972
contextData.chakraBaseAddress = (intptr_t)AutoSystemInfo::Data.GetChakraBaseAddr();
19651973
contextData.crtBaseAddress = (intptr_t)GetModuleHandle(UCrtC99MathApis::LibraryName);

0 commit comments

Comments
 (0)