forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJITProcessManager.cpp
More file actions
186 lines (159 loc) · 5.1 KB
/
JITProcessManager.cpp
File metadata and controls
186 lines (159 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "stdafx.h"
#ifdef _WIN32
HANDLE JITProcessManager::s_rpcServerProcessHandle = 0; // 0 is the "invalid handle" value for process handles
UUID JITProcessManager::s_connectionId = GUID_NULL;
HRESULT JITProcessManager::StartRpcServer(int argc, __in_ecount(argc) LPWSTR argv[])
{
HRESULT hr = S_OK;
JITProcessManager::RemoveArg(_u("-dynamicprofilecache:"), &argc, &argv);
JITProcessManager::RemoveArg(_u("-dpc:"), &argc, &argv);
JITProcessManager::RemoveArg(_u("-dynamicprofileinput:"), &argc, &argv);
if (IsEqualGUID(s_connectionId, GUID_NULL))
{
RPC_STATUS status = UuidCreate(&s_connectionId);
if (status == RPC_S_OK || status == RPC_S_UUID_LOCAL_ONLY)
{
hr = CreateServerProcess(argc, argv);
}
else
{
hr = HRESULT_FROM_WIN32(status);
}
}
return hr;
}
/* static */
void
JITProcessManager::RemoveArg(LPCWSTR flag, int * argc, __in_ecount(*argc) LPWSTR * argv[])
{
size_t flagLen = wcslen(flag);
int flagIndex;
while ((flagIndex = HostConfigFlags::FindArg(*argc, *argv, flag, flagLen)) >= 0)
{
HostConfigFlags::RemoveArg(*argc, *argv, flagIndex);
}
}
HRESULT JITProcessManager::CreateServerProcess(int argc, __in_ecount(argc) LPWSTR argv[])
{
HRESULT hr;
PROCESS_INFORMATION processInfo = { 0 };
STARTUPINFOW si = { 0 };
// overallocate constant cmd line (jshost -jitserver:<guid>)
size_t cmdLineSize = (MAX_PATH + argc) * sizeof(WCHAR);
for (int i = 0; i < argc; ++i)
{
// calculate space requirement for each arg
cmdLineSize += wcslen(argv[i]) * sizeof(WCHAR);
}
WCHAR* cmdLine = (WCHAR*)malloc(cmdLineSize);
if (cmdLine == nullptr)
{
return E_OUTOFMEMORY;
}
RPC_WSTR connectionUuidString = NULL;
#pragma warning(suppress: 6386) // buffer overrun
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
hr = StringCchCopyW(cmdLine, cmdLineSize, L"ch.exe -OOPCFGRegistration- -CheckOpHelpers -jitserver:");
#else
hr = StringCchCopyW(cmdLine, cmdLineSize, L"ch.exe -jitserver:");
#endif
if (FAILED(hr))
{
return hr;
}
RPC_STATUS status = UuidToStringW(&s_connectionId, &connectionUuidString);
if (status != S_OK)
{
return HRESULT_FROM_WIN32(status);
}
hr = StringCchCatW(cmdLine, cmdLineSize, (WCHAR*)connectionUuidString);
if (FAILED(hr))
{
return hr;
}
for (int i = 1; i < argc; ++i)
{
hr = StringCchCatW(cmdLine, cmdLineSize, L" ");
if (FAILED(hr))
{
return hr;
}
hr = StringCchCatW(cmdLine, cmdLineSize, argv[i]);
if (FAILED(hr))
{
return hr;
}
}
if (!CreateProcessW(
NULL,
cmdLine,
NULL,
NULL,
FALSE,
NULL,
NULL,
NULL,
&si,
&processInfo))
{
return HRESULT_FROM_WIN32(GetLastError());
}
free(cmdLine);
CloseHandle(processInfo.hThread);
s_rpcServerProcessHandle = processInfo.hProcess;
if (HostConfigFlags::flags.EnsureCloseJITServer)
{
// create job object so if parent ch gets killed, server is killed as well
// under a flag because it's preferable to let server close naturally
// only useful in scenarios where ch is expected to be force terminated
HANDLE jobObject = CreateJobObject(nullptr, nullptr);
if (jobObject == nullptr)
{
return HRESULT_FROM_WIN32(GetLastError());
}
if (!AssignProcessToJobObject(jobObject, s_rpcServerProcessHandle))
{
return HRESULT_FROM_WIN32(GetLastError());
}
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobInfo = { 0 };
jobInfo.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
if (!SetInformationJobObject(jobObject, JobObjectExtendedLimitInformation, &jobInfo, sizeof(jobInfo)))
{
return HRESULT_FROM_WIN32(GetLastError());
}
}
return NOERROR;
}
typedef HRESULT(WINAPI *JsShutdownJITServerPtr)();
void JITProcessManager::StopRpcServer(HINSTANCE chakraLibrary)
{
if (s_rpcServerProcessHandle)
{
JsShutdownJITServerPtr shutdownJITServer = (JsShutdownJITServerPtr)GetProcAddress(chakraLibrary, "JsShutdownJITServer");
shutdownJITServer();
}
s_rpcServerProcessHandle = NULL;
}
void
JITProcessManager::TerminateJITServer()
{
if (s_rpcServerProcessHandle)
{
TerminateProcess(s_rpcServerProcessHandle, 1);
CloseHandle(s_rpcServerProcessHandle);
s_rpcServerProcessHandle = NULL;
}
}
HANDLE JITProcessManager::GetRpcProccessHandle()
{
return s_rpcServerProcessHandle;
}
UUID JITProcessManager::GetRpcConnectionId()
{
return s_connectionId;
}
#endif