JITManager.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "JITClientPch.h"
  6. __bcount_opt(size) void * __RPC_USER midl_user_allocate(size_t size)
  7. {
  8. return (HeapAlloc(GetProcessHeap(), 0, size));
  9. }
  10. void __RPC_USER midl_user_free(__inout void * ptr)
  11. {
  12. if (ptr != NULL)
  13. {
  14. HeapFree(GetProcessHeap(), NULL, ptr);
  15. }
  16. }
  17. // This routine creates a binding with the server.
  18. HRESULT
  19. JITManager::CreateBinding(
  20. __in HANDLE serverProcessHandle,
  21. __in UUID * connectionUuid,
  22. __out RPC_BINDING_HANDLE * bindingHandle)
  23. {
  24. RPC_STATUS status;
  25. RPC_SECURITY_QOS_V4 securityQOS; // TODO: V5???
  26. DWORD attemptCount = 0;
  27. DWORD sleepInterval = 100; // in milliseconds
  28. RPC_BINDING_HANDLE localBindingHandle;
  29. RPC_BINDING_HANDLE_TEMPLATE_V1 bindingTemplate;
  30. RPC_BINDING_HANDLE_SECURITY_V1_W bindingSecurity;
  31. ZeroMemory(&securityQOS, sizeof(RPC_SECURITY_QOS_V4));
  32. securityQOS.Capabilities = RPC_C_QOS_CAPABILITIES_DEFAULT;
  33. securityQOS.IdentityTracking = RPC_C_QOS_IDENTITY_DYNAMIC;
  34. securityQOS.ImpersonationType = RPC_C_IMP_LEVEL_IDENTIFY;
  35. securityQOS.Version = 4;
  36. ZeroMemory(&bindingTemplate, sizeof(bindingTemplate));
  37. bindingTemplate.Version = 1;
  38. bindingTemplate.ProtocolSequence = RPC_PROTSEQ_LRPC;
  39. bindingTemplate.StringEndpoint = NULL;
  40. memcpy_s(&bindingTemplate.ObjectUuid, sizeof(UUID), connectionUuid, sizeof(UUID));
  41. bindingTemplate.Flags |= RPC_BHT_OBJECT_UUID_VALID;
  42. ZeroMemory(&bindingSecurity, sizeof(bindingSecurity));
  43. bindingSecurity.Version = 1;
  44. bindingSecurity.AuthnLevel = RPC_C_AUTHN_LEVEL_PKT_PRIVACY;
  45. bindingSecurity.AuthnSvc = RPC_C_AUTHN_KERNEL;
  46. bindingSecurity.SecurityQos = (RPC_SECURITY_QOS*)&securityQOS;
  47. status = RpcBindingCreate(&bindingTemplate, &bindingSecurity, NULL, &localBindingHandle);
  48. if (status != RPC_S_OK)
  49. {
  50. return HRESULT_FROM_WIN32(status);
  51. }
  52. // We keep attempting to connect to the server with increasing wait intervals in between.
  53. // This will wait close to 5 minutes before it finally gives up.
  54. do
  55. {
  56. DWORD waitStatus;
  57. status = RpcBindingBind(NULL, localBindingHandle, ClientIChakraJIT_v0_0_c_ifspec);
  58. if (status == RPC_S_OK)
  59. {
  60. break;
  61. }
  62. else if (status == EPT_S_NOT_REGISTERED)
  63. {
  64. // The Server side has not finished registering the RPC Server yet.
  65. // We should only breakout if we have reached the max attempt count.
  66. if (attemptCount > 600)
  67. {
  68. break;
  69. }
  70. }
  71. else
  72. {
  73. // Some unknown error occurred. We are not going to retry for arbitrary errors.
  74. break;
  75. }
  76. // When we come to this point, it means the server has not finished registration yet.
  77. // We should wait for a while and then reattempt to bind.
  78. waitStatus = WaitForSingleObject(serverProcessHandle, sleepInterval);
  79. if (waitStatus == WAIT_OBJECT_0)
  80. {
  81. DWORD exitCode = (DWORD)-1;
  82. // The server process died for some reason. No need to reattempt.
  83. // We use -1 as the exit code if GetExitCodeProcess fails.
  84. NT_VERIFY(GetExitCodeProcess(serverProcessHandle, &exitCode));
  85. status = RPC_S_SERVER_UNAVAILABLE;
  86. break;
  87. }
  88. else if (waitStatus == WAIT_TIMEOUT)
  89. {
  90. // Not an error. the server is still alive and we should reattempt.
  91. }
  92. else
  93. {
  94. // wait operation failed for an unknown reason.
  95. NT_ASSERT(false);
  96. status = HRESULT_FROM_WIN32(waitStatus);
  97. break;
  98. }
  99. attemptCount++;
  100. if (sleepInterval < 500)
  101. {
  102. sleepInterval += 100;
  103. }
  104. } while (status != RPC_S_OK); // redundant check, but compiler would not allow true here.
  105. if (status != RPC_S_OK)
  106. {
  107. RpcBindingFree(&localBindingHandle);
  108. return HRESULT_FROM_WIN32(status);
  109. }
  110. *bindingHandle = localBindingHandle;
  111. return S_OK;
  112. }
  113. HRESULT
  114. JITManager::ConnectRpcServer(DWORD proccessId, UUID connectionUuid)
  115. {
  116. HRESULT hr;
  117. RPC_STATUS status;
  118. HANDLE localServerProcessHandle = NULL;
  119. WCHAR* connectionUuidString = NULL;
  120. RPC_BINDING_HANDLE localBindingHandle;
  121. status = UuidToStringW(&connectionUuid, &connectionUuidString);
  122. if (status != S_OK)
  123. {
  124. return HRESULT_FROM_WIN32(status);
  125. }
  126. localServerProcessHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, proccessId);
  127. hr = CreateBinding(localServerProcessHandle, &connectionUuid, &localBindingHandle);
  128. if (SUCCEEDED(hr))
  129. {
  130. m_rpcBindingHandle = localBindingHandle;
  131. m_rpcServerProcessHandle = localServerProcessHandle;
  132. }
  133. else
  134. {
  135. CloseHandle(localServerProcessHandle);
  136. }
  137. // log an event
  138. return hr;
  139. }
  140. void
  141. JITManager::DisconnectRpcServer()
  142. {
  143. HRESULT hr = S_OK;
  144. if (m_rpcBindingHandle == NULL)
  145. {
  146. NT_ASSERT(m_rpcBindingHandle == NULL);
  147. NT_ASSERT(m_rpcServerProcessHandle == NULL);
  148. return;
  149. }
  150. RpcTryExcept
  151. {
  152. ClientShutdown(m_rpcBindingHandle);
  153. }
  154. RpcExcept(1)
  155. {
  156. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  157. }
  158. RpcEndExcept;
  159. RpcBindingFree(&m_rpcBindingHandle);
  160. m_rpcBindingHandle = NULL;
  161. // log an event
  162. CloseHandle(m_rpcServerProcessHandle);
  163. m_rpcServerProcessHandle = NULL;
  164. }
  165. HRESULT
  166. JITManager::InitializeThreadContext(
  167. __in ThreadContextData * data,
  168. __out intptr_t * threadContextInfoAddress)
  169. {
  170. HRESULT hr = E_FAIL;
  171. RpcTryExcept
  172. {
  173. hr = ClientInitializeThreadContext(m_rpcBindingHandle, data, threadContextInfoAddress);
  174. }
  175. RpcExcept(1)
  176. {
  177. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  178. }
  179. RpcEndExcept;
  180. return hr;
  181. }
  182. HRESULT
  183. JITManager::CleanupThreadContext(
  184. __in intptr_t threadContextInfoAddress)
  185. {
  186. HRESULT hr = E_FAIL;
  187. RpcTryExcept
  188. {
  189. hr = ClientCleanupThreadContext(m_rpcBindingHandle, threadContextInfoAddress);
  190. }
  191. RpcExcept(1)
  192. {
  193. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  194. }
  195. RpcEndExcept;
  196. return hr;
  197. }
  198. HRESULT
  199. JITManager::InitializeScriptContext(
  200. __in ScriptContextData * data,
  201. __out intptr_t * scriptContextInfoAddress)
  202. {
  203. HRESULT hr = E_FAIL;
  204. RpcTryExcept
  205. {
  206. hr = ClientInitializeScriptContext(m_rpcBindingHandle, data, scriptContextInfoAddress);
  207. }
  208. RpcExcept(1)
  209. {
  210. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  211. }
  212. RpcEndExcept;
  213. return hr;
  214. }
  215. HRESULT
  216. JITManager::CleanupScriptContext(
  217. __in intptr_t scriptContextInfoAddress)
  218. {
  219. HRESULT hr = E_FAIL;
  220. RpcTryExcept
  221. {
  222. hr = ClientCleanupScriptContext(m_rpcBindingHandle, scriptContextInfoAddress);
  223. }
  224. RpcExcept(1)
  225. {
  226. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  227. }
  228. RpcEndExcept;
  229. return hr;
  230. }
  231. HRESULT
  232. JITManager::FreeAllocation(
  233. __in intptr_t threadContextInfoAddress,
  234. __in intptr_t address)
  235. {
  236. HRESULT hr = E_FAIL;
  237. RpcTryExcept
  238. {
  239. hr = ClientFreeAllocation(m_rpcBindingHandle, threadContextInfoAddress, address);
  240. }
  241. RpcExcept(1)
  242. {
  243. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  244. }
  245. RpcEndExcept;
  246. return hr;
  247. }
  248. HRESULT
  249. JITManager::RemoteCodeGenCall(
  250. __in CodeGenWorkItemJITData *workItemData,
  251. __in ProfileData * profileData,
  252. __in intptr_t threadContextInfoAddress,
  253. __in intptr_t scriptContextInfoAddress,
  254. __out JITOutputData *jitData)
  255. {
  256. HRESULT hr = E_FAIL;
  257. RpcTryExcept
  258. {
  259. hr = ClientRemoteCodeGen(m_rpcBindingHandle, threadContextInfoAddress, scriptContextInfoAddress, workItemData, jitData, profileData);
  260. }
  261. RpcExcept(1)
  262. {
  263. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  264. }
  265. RpcEndExcept;
  266. return hr;
  267. }