JITManager.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. _Must_inspect_result_
  7. _Ret_maybenull_ _Post_writable_byte_size_(size)
  8. void * __RPC_USER midl_user_allocate(
  9. #if defined(_WIN32_WINNT_WIN10)
  10. _In_ // starting win10, _In_ is in the signature
  11. #endif
  12. size_t size)
  13. {
  14. return (HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size));
  15. }
  16. void __RPC_USER midl_user_free(_Pre_maybenull_ _Post_invalid_ void * ptr)
  17. {
  18. if (ptr != NULL)
  19. {
  20. HeapFree(GetProcessHeap(), NULL, ptr);
  21. }
  22. }
  23. JITManager JITManager::s_jitManager = JITManager();
  24. JITManager::JITManager() :
  25. m_rpcBindingHandle(nullptr),
  26. m_oopJitEnabled(false),
  27. m_isJITServer(false),
  28. m_failingHRESULT(S_OK),
  29. m_jitConnectionId()
  30. {
  31. }
  32. JITManager::~JITManager()
  33. {
  34. if (m_rpcBindingHandle)
  35. {
  36. RpcBindingFree(&m_rpcBindingHandle);
  37. }
  38. }
  39. /* static */
  40. JITManager *
  41. JITManager::GetJITManager()
  42. {
  43. return &s_jitManager;
  44. }
  45. // This routine creates a binding with the server.
  46. HRESULT
  47. JITManager::CreateBinding(
  48. __in HANDLE serverProcessHandle,
  49. __in_opt void * serverSecurityDescriptor,
  50. __in UUID * connectionUuid,
  51. __out RPC_BINDING_HANDLE * bindingHandle)
  52. {
  53. Assert(IsOOPJITEnabled());
  54. RPC_STATUS status;
  55. DWORD attemptCount = 0;
  56. DWORD sleepInterval = 100; // in milliseconds
  57. RPC_BINDING_HANDLE localBindingHandle;
  58. RPC_BINDING_HANDLE_TEMPLATE_V1 bindingTemplate;
  59. RPC_BINDING_HANDLE_SECURITY_V1_W bindingSecurity;
  60. #ifndef NTBUILD
  61. RPC_SECURITY_QOS_V4 securityQOS;
  62. ZeroMemory(&securityQOS, sizeof(RPC_SECURITY_QOS_V4));
  63. securityQOS.Capabilities = RPC_C_QOS_CAPABILITIES_DEFAULT;
  64. securityQOS.IdentityTracking = RPC_C_QOS_IDENTITY_DYNAMIC;
  65. securityQOS.ImpersonationType = RPC_C_IMP_LEVEL_IDENTIFY;
  66. securityQOS.Version = 4;
  67. #else
  68. RPC_SECURITY_QOS_V5 securityQOS;
  69. ZeroMemory(&securityQOS, sizeof(RPC_SECURITY_QOS_V5));
  70. securityQOS.Capabilities = RPC_C_QOS_CAPABILITIES_DEFAULT;
  71. securityQOS.IdentityTracking = RPC_C_QOS_IDENTITY_DYNAMIC;
  72. securityQOS.ImpersonationType = RPC_C_IMP_LEVEL_IDENTIFY;
  73. securityQOS.Version = 5;
  74. securityQOS.ServerSecurityDescriptor = serverSecurityDescriptor;
  75. #endif // NTBUILD
  76. ZeroMemory(&bindingTemplate, sizeof(bindingTemplate));
  77. bindingTemplate.Version = 1;
  78. bindingTemplate.ProtocolSequence = RPC_PROTSEQ_LRPC;
  79. bindingTemplate.StringEndpoint = NULL;
  80. memcpy_s(&bindingTemplate.ObjectUuid, sizeof(UUID), connectionUuid, sizeof(UUID));
  81. bindingTemplate.Flags |= RPC_BHT_OBJECT_UUID_VALID;
  82. ZeroMemory(&bindingSecurity, sizeof(bindingSecurity));
  83. bindingSecurity.Version = 1;
  84. bindingSecurity.AuthnLevel = RPC_C_AUTHN_LEVEL_PKT_PRIVACY;
  85. bindingSecurity.AuthnSvc = RPC_C_AUTHN_KERNEL;
  86. bindingSecurity.SecurityQos = (RPC_SECURITY_QOS*)&securityQOS;
  87. status = RpcBindingCreate(&bindingTemplate, &bindingSecurity, NULL, &localBindingHandle);
  88. if (status != RPC_S_OK)
  89. {
  90. return HRESULT_FROM_WIN32(status);
  91. }
  92. // We keep attempting to connect to the server with increasing wait intervals in between.
  93. // This will wait close to 5 minutes before it finally gives up.
  94. do
  95. {
  96. DWORD waitStatus;
  97. status = RpcBindingBind(NULL, localBindingHandle, ClientIChakraJIT_v0_0_c_ifspec);
  98. if (status == RPC_S_OK)
  99. {
  100. break;
  101. }
  102. else if (status == EPT_S_NOT_REGISTERED)
  103. {
  104. // The Server side has not finished registering the RPC Server yet.
  105. // We should only breakout if we have reached the max attempt count.
  106. if (attemptCount > 600)
  107. {
  108. break;
  109. }
  110. }
  111. else
  112. {
  113. // Some unknown error occurred. We are not going to retry for arbitrary errors.
  114. break;
  115. }
  116. // When we come to this point, it means the server has not finished registration yet.
  117. // We should wait for a while and then reattempt to bind.
  118. waitStatus = WaitForSingleObject(serverProcessHandle, sleepInterval);
  119. if (waitStatus == WAIT_OBJECT_0)
  120. {
  121. // The server process died for some reason. No need to reattempt.
  122. status = RPC_S_SERVER_UNAVAILABLE;
  123. break;
  124. }
  125. else if (waitStatus == WAIT_TIMEOUT)
  126. {
  127. // Not an error. the server is still alive and we should reattempt.
  128. }
  129. else
  130. {
  131. // wait operation failed for an unknown reason.
  132. Assert(false);
  133. status = HRESULT_FROM_WIN32(waitStatus);
  134. break;
  135. }
  136. attemptCount++;
  137. if (sleepInterval < 500)
  138. {
  139. sleepInterval += 100;
  140. }
  141. } while (status != RPC_S_OK); // redundant check, but compiler would not allow true here.
  142. *bindingHandle = localBindingHandle;
  143. return HRESULT_FROM_WIN32(status);
  144. }
  145. bool
  146. JITManager::IsJITServer() const
  147. {
  148. return m_isJITServer;
  149. }
  150. void
  151. JITManager::SetIsJITServer()
  152. {
  153. m_isJITServer = true;
  154. m_oopJitEnabled = true;
  155. }
  156. bool
  157. JITManager::IsConnected() const
  158. {
  159. Assert(IsOOPJITEnabled());
  160. return m_rpcBindingHandle != nullptr && !HasJITFailed();
  161. }
  162. void
  163. JITManager::EnableOOPJIT()
  164. {
  165. m_oopJitEnabled = true;
  166. if (CONFIG_FLAG(OOPCFGRegistration))
  167. {
  168. // Since this client has enabled OOPJIT, perform the one-way policy update
  169. // that will disable SetProcessValidCallTargets from being invoked.
  170. GlobalSecurityPolicy::DisableSetProcessValidCallTargets();
  171. }
  172. }
  173. void
  174. JITManager::SetJITFailed(HRESULT hr)
  175. {
  176. Assert(hr != S_OK);
  177. m_failingHRESULT = hr;
  178. }
  179. bool
  180. JITManager::HasJITFailed() const
  181. {
  182. return m_failingHRESULT != S_OK;
  183. }
  184. bool
  185. JITManager::IsOOPJITEnabled() const
  186. {
  187. return m_oopJitEnabled;
  188. }
  189. HRESULT
  190. JITManager::ConnectRpcServer(__in HANDLE jitProcessHandle, __in_opt void* serverSecurityDescriptor, __in UUID connectionUuid)
  191. {
  192. Assert(IsOOPJITEnabled());
  193. if(m_rpcBindingHandle != nullptr)
  194. {
  195. // TODO: change this to allow connecting a new JIT process to new ThreadContexts
  196. return E_FAIL;
  197. }
  198. HRESULT hr = E_FAIL;
  199. if (IsConnected())
  200. {
  201. Assert(UNREACHED);
  202. return E_FAIL;
  203. }
  204. hr = CreateBinding(jitProcessHandle, serverSecurityDescriptor, &connectionUuid, &m_rpcBindingHandle);
  205. if (FAILED(hr))
  206. {
  207. goto FailureCleanup;
  208. }
  209. m_jitConnectionId = connectionUuid;
  210. return hr;
  211. FailureCleanup:
  212. if (m_rpcBindingHandle)
  213. {
  214. RpcBindingFree(&m_rpcBindingHandle);
  215. m_rpcBindingHandle = nullptr;
  216. }
  217. return hr;
  218. }
  219. HRESULT
  220. JITManager::Shutdown()
  221. {
  222. // this is special case of shutdown called when runtime process is a parent of the server process
  223. // used for console host type scenarios
  224. HRESULT hr = S_OK;
  225. Assert(IsOOPJITEnabled());
  226. Assert(m_rpcBindingHandle != nullptr);
  227. RpcTryExcept
  228. {
  229. ClientShutdown(m_rpcBindingHandle);
  230. }
  231. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  232. {
  233. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  234. }
  235. RpcEndExcept;
  236. m_rpcBindingHandle = nullptr;
  237. return hr;
  238. }
  239. HRESULT
  240. JITManager::InitializeThreadContext(
  241. __in ThreadContextDataIDL * data,
  242. #ifdef USE_RPC_HANDLE_MARSHALLING
  243. __in HANDLE processHandle,
  244. #endif
  245. __out PPTHREADCONTEXT_HANDLE threadContextInfoAddress,
  246. __out intptr_t * prereservedRegionAddr,
  247. __out intptr_t * jitThunkAddr)
  248. {
  249. Assert(IsOOPJITEnabled());
  250. HRESULT hr = E_FAIL;
  251. RpcTryExcept
  252. {
  253. hr = ClientInitializeThreadContext(
  254. m_rpcBindingHandle,
  255. data,
  256. #ifdef USE_RPC_HANDLE_MARSHALLING
  257. processHandle,
  258. #endif
  259. threadContextInfoAddress,
  260. prereservedRegionAddr,
  261. jitThunkAddr);
  262. }
  263. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  264. {
  265. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  266. }
  267. RpcEndExcept;
  268. return hr;
  269. }
  270. HRESULT
  271. JITManager::CleanupThreadContext(
  272. __inout PPTHREADCONTEXT_HANDLE threadContextInfoAddress)
  273. {
  274. Assert(IsOOPJITEnabled());
  275. HRESULT hr = E_FAIL;
  276. RpcTryExcept
  277. {
  278. hr = ClientCleanupThreadContext(m_rpcBindingHandle, threadContextInfoAddress);
  279. }
  280. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  281. {
  282. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  283. }
  284. RpcEndExcept;
  285. return hr;
  286. }
  287. HRESULT
  288. JITManager::AddDOMFastPathHelper(
  289. __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
  290. __in intptr_t funcInfoAddr,
  291. __in int helper)
  292. {
  293. Assert(IsOOPJITEnabled());
  294. HRESULT hr = E_FAIL;
  295. RpcTryExcept
  296. {
  297. hr = ClientAddDOMFastPathHelper(m_rpcBindingHandle, scriptContextInfoAddress, funcInfoAddr, helper);
  298. }
  299. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  300. {
  301. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  302. }
  303. RpcEndExcept;
  304. return hr;
  305. }
  306. HRESULT
  307. JITManager::SetIsPRNGSeeded(
  308. __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
  309. __in boolean value)
  310. {
  311. HRESULT hr = E_FAIL;
  312. RpcTryExcept
  313. {
  314. hr = ClientSetIsPRNGSeeded(m_rpcBindingHandle, scriptContextInfoAddress, value);
  315. }
  316. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  317. {
  318. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  319. }
  320. RpcEndExcept;
  321. return hr;
  322. }
  323. HRESULT
  324. JITManager::DecommitInterpreterBufferManager(
  325. __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
  326. __in boolean asmJsThunk)
  327. {
  328. Assert(IsOOPJITEnabled());
  329. HRESULT hr = E_FAIL;
  330. RpcTryExcept
  331. {
  332. hr = ClientDecommitInterpreterBufferManager(m_rpcBindingHandle, scriptContextInfoAddress, asmJsThunk);
  333. }
  334. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  335. {
  336. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  337. }
  338. RpcEndExcept;
  339. return hr;
  340. }
  341. HRESULT
  342. JITManager::NewInterpreterThunkBlock(
  343. __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
  344. __in InterpreterThunkInputIDL * thunkInput,
  345. __out InterpreterThunkOutputIDL * thunkOutput)
  346. {
  347. Assert(IsOOPJITEnabled());
  348. HRESULT hr = E_FAIL;
  349. RpcTryExcept
  350. {
  351. hr = ClientNewInterpreterThunkBlock(m_rpcBindingHandle, scriptContextInfoAddress, thunkInput, thunkOutput);
  352. }
  353. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  354. {
  355. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  356. }
  357. RpcEndExcept;
  358. return hr;
  359. }
  360. HRESULT
  361. JITManager::AddModuleRecordInfo(
  362. /* [in] */ PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
  363. /* [in] */ unsigned int moduleId,
  364. /* [in] */ intptr_t localExportSlotsAddr)
  365. {
  366. Assert(IsOOPJITEnabled());
  367. HRESULT hr = E_FAIL;
  368. RpcTryExcept
  369. {
  370. hr = ClientAddModuleRecordInfo(m_rpcBindingHandle, scriptContextInfoAddress, moduleId, localExportSlotsAddr);
  371. }
  372. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  373. {
  374. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  375. }
  376. RpcEndExcept;
  377. return hr;
  378. }
  379. HRESULT
  380. JITManager::SetWellKnownHostTypeId(
  381. __in PTHREADCONTEXT_HANDLE threadContextRoot,
  382. __in int typeId)
  383. {
  384. Assert(IsOOPJITEnabled());
  385. HRESULT hr = E_FAIL;
  386. RpcTryExcept
  387. {
  388. hr = ClientSetWellKnownHostTypeId(m_rpcBindingHandle, threadContextRoot, typeId);
  389. }
  390. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  391. {
  392. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  393. }
  394. RpcEndExcept;
  395. return hr;
  396. }
  397. HRESULT
  398. JITManager::UpdatePropertyRecordMap(
  399. __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
  400. __in_opt BVSparseNodeIDL * updatedPropsBVHead)
  401. {
  402. Assert(IsOOPJITEnabled());
  403. HRESULT hr = E_FAIL;
  404. RpcTryExcept
  405. {
  406. hr = ClientUpdatePropertyRecordMap(m_rpcBindingHandle, threadContextInfoAddress, updatedPropsBVHead);
  407. }
  408. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  409. {
  410. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  411. }
  412. RpcEndExcept;
  413. return hr;
  414. }
  415. HRESULT
  416. JITManager::InitializeScriptContext(
  417. __in ScriptContextDataIDL * data,
  418. __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
  419. __out PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
  420. {
  421. Assert(IsOOPJITEnabled());
  422. HRESULT hr = E_FAIL;
  423. RpcTryExcept
  424. {
  425. hr = ClientInitializeScriptContext(m_rpcBindingHandle, data, threadContextInfoAddress, scriptContextInfoAddress);
  426. }
  427. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  428. {
  429. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  430. }
  431. RpcEndExcept;
  432. return hr;
  433. }
  434. HRESULT
  435. JITManager::CleanupScriptContext(
  436. __inout PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
  437. {
  438. Assert(IsOOPJITEnabled());
  439. HRESULT hr = E_FAIL;
  440. RpcTryExcept
  441. {
  442. hr = ClientCleanupScriptContext(m_rpcBindingHandle, scriptContextInfoAddress);
  443. }
  444. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  445. {
  446. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  447. }
  448. RpcEndExcept;
  449. return hr;
  450. }
  451. HRESULT
  452. JITManager::CloseScriptContext(
  453. __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
  454. {
  455. Assert(IsOOPJITEnabled());
  456. HRESULT hr = E_FAIL;
  457. RpcTryExcept
  458. {
  459. hr = ClientCloseScriptContext(m_rpcBindingHandle, scriptContextInfoAddress);
  460. }
  461. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  462. {
  463. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  464. }
  465. RpcEndExcept;
  466. return hr;
  467. }
  468. HRESULT
  469. JITManager::FreeAllocation(
  470. __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
  471. __in intptr_t codeAddress,
  472. __in intptr_t thunkAddress)
  473. {
  474. Assert(IsOOPJITEnabled());
  475. HRESULT hr = E_FAIL;
  476. RpcTryExcept
  477. {
  478. hr = ClientFreeAllocation(m_rpcBindingHandle, threadContextInfoAddress, codeAddress, thunkAddress);
  479. }
  480. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  481. {
  482. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  483. }
  484. RpcEndExcept;
  485. return hr;
  486. }
  487. HRESULT
  488. JITManager::IsNativeAddr(
  489. __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
  490. __in intptr_t address,
  491. __out boolean * result)
  492. {
  493. Assert(IsOOPJITEnabled());
  494. HRESULT hr = E_FAIL;
  495. RpcTryExcept
  496. {
  497. hr = ClientIsNativeAddr(m_rpcBindingHandle, threadContextInfoAddress, address, result);
  498. }
  499. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  500. {
  501. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  502. }
  503. RpcEndExcept;
  504. return hr;
  505. }
  506. HRESULT
  507. JITManager::RemoteCodeGenCall(
  508. __in CodeGenWorkItemIDL *workItemData,
  509. __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
  510. __out JITOutputIDL *jitData)
  511. {
  512. Assert(IsOOPJITEnabled());
  513. HRESULT hr = E_FAIL;
  514. RpcTryExcept
  515. {
  516. hr = ClientRemoteCodeGen(m_rpcBindingHandle, scriptContextInfoAddress, workItemData, jitData);
  517. }
  518. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  519. {
  520. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  521. }
  522. RpcEndExcept;
  523. return hr;
  524. }
  525. #if DBG
  526. HRESULT
  527. JITManager::IsInterpreterThunkAddr(
  528. __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
  529. __in intptr_t address,
  530. __in boolean asmjsThunk,
  531. __out boolean * result)
  532. {
  533. Assert(IsOOPJITEnabled());
  534. HRESULT hr = E_FAIL;
  535. RpcTryExcept
  536. {
  537. hr = ClientIsInterpreterThunkAddr(m_rpcBindingHandle, scriptContextInfoAddress, address, asmjsThunk, result);
  538. }
  539. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  540. {
  541. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  542. }
  543. RpcEndExcept;
  544. return hr;
  545. }
  546. #endif