JITManager.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  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. Assert(waitStatus == WAIT_FAILED);
  132. #ifdef DBG
  133. LPWSTR messageBuffer = nullptr;
  134. DWORD errorNumber = GetLastError();
  135. FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  136. NULL, errorNumber, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&messageBuffer, 0, NULL);
  137. Output::Print(_u("Last error was 0x%x (%s)"), errorNumber, messageBuffer);
  138. LocalFree(messageBuffer);
  139. #endif
  140. // wait operation failed for an unknown reason.
  141. Assert(false);
  142. status = HRESULT_FROM_WIN32(waitStatus);
  143. break;
  144. }
  145. attemptCount++;
  146. if (sleepInterval < 500)
  147. {
  148. sleepInterval += 100;
  149. }
  150. } while (status != RPC_S_OK); // redundant check, but compiler would not allow true here.
  151. *bindingHandle = localBindingHandle;
  152. return HRESULT_FROM_WIN32(status);
  153. }
  154. bool
  155. JITManager::IsJITServer() const
  156. {
  157. return m_isJITServer;
  158. }
  159. void
  160. JITManager::SetIsJITServer()
  161. {
  162. m_isJITServer = true;
  163. m_oopJitEnabled = true;
  164. }
  165. bool
  166. JITManager::IsConnected() const
  167. {
  168. Assert(IsOOPJITEnabled());
  169. return m_rpcBindingHandle != nullptr && !HasJITFailed();
  170. }
  171. void
  172. JITManager::EnableOOPJIT()
  173. {
  174. m_oopJitEnabled = true;
  175. if (CONFIG_FLAG(OOPCFGRegistration))
  176. {
  177. // Since this client has enabled OOPJIT, perform the one-way policy update
  178. // that will disable SetProcessValidCallTargets from being invoked.
  179. GlobalSecurityPolicy::DisableSetProcessValidCallTargets();
  180. }
  181. }
  182. void
  183. JITManager::SetJITFailed(HRESULT hr)
  184. {
  185. Assert(hr != S_OK);
  186. m_failingHRESULT = hr;
  187. }
  188. bool
  189. JITManager::HasJITFailed() const
  190. {
  191. return m_failingHRESULT != S_OK;
  192. }
  193. bool
  194. JITManager::IsOOPJITEnabled() const
  195. {
  196. return m_oopJitEnabled;
  197. }
  198. #pragma prefast(push)
  199. #pragma prefast(disable:__WARNING_RELEASING_UNHELD_LOCK_MEDIUM_CONFIDENCE, "Lock is correctly acquired and released by RAII class AutoCriticalSection")
  200. #pragma prefast(disable:__WARNING_CALLER_FAILING_TO_HOLD, "Lock is correctly acquired and released by RAII class AutoCriticalSection")
  201. HRESULT
  202. JITManager::ConnectRpcServer(__in HANDLE jitProcessHandle, __in_opt void* serverSecurityDescriptor, __in UUID connectionUuid)
  203. {
  204. // We might be trying to connect from multiple threads simultaneously
  205. AutoCriticalSection cs(&m_cs);
  206. Assert(IsOOPJITEnabled());
  207. if (m_rpcBindingHandle != nullptr)
  208. {
  209. return S_OK;
  210. }
  211. HRESULT hr = E_FAIL;
  212. RPC_BINDING_HANDLE bindingHandle;
  213. hr = CreateBinding(jitProcessHandle, serverSecurityDescriptor, &connectionUuid, &bindingHandle);
  214. if (FAILED(hr))
  215. {
  216. goto FailureCleanup;
  217. }
  218. hr = ConnectProcess(bindingHandle);
  219. HandleServerCallResult(hr, RemoteCallType::StateUpdate);
  220. // Only store the binding handle after JIT handshake, so other threads do not prematurely think we are ready to JIT
  221. m_rpcBindingHandle = bindingHandle;
  222. m_jitConnectionId = connectionUuid;
  223. return hr;
  224. FailureCleanup:
  225. if (m_rpcBindingHandle)
  226. {
  227. RpcBindingFree(&m_rpcBindingHandle);
  228. m_rpcBindingHandle = nullptr;
  229. }
  230. return hr;
  231. }
  232. #pragma prefast(pop)
  233. HRESULT
  234. JITManager::Shutdown()
  235. {
  236. // this is special case of shutdown called when runtime process is a parent of the server process
  237. // used for console host type scenarios
  238. HRESULT hr = S_OK;
  239. Assert(IsOOPJITEnabled());
  240. Assert(m_rpcBindingHandle != nullptr);
  241. RpcTryExcept
  242. {
  243. ClientShutdown(m_rpcBindingHandle);
  244. }
  245. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  246. {
  247. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  248. }
  249. RpcEndExcept;
  250. m_rpcBindingHandle = nullptr;
  251. return hr;
  252. }
  253. HRESULT
  254. JITManager::ConnectProcess(RPC_BINDING_HANDLE rpcBindingHandle)
  255. {
  256. Assert(IsOOPJITEnabled());
  257. #ifdef USE_RPC_HANDLE_MARSHALLING
  258. HANDLE processHandle;
  259. if (!DuplicateHandle(GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(), &processHandle, 0, false, DUPLICATE_SAME_ACCESS))
  260. {
  261. return HRESULT_FROM_WIN32(GetLastError());
  262. }
  263. #endif
  264. HRESULT hr = E_FAIL;
  265. RpcTryExcept
  266. {
  267. hr = ClientConnectProcess(
  268. rpcBindingHandle,
  269. #ifdef USE_RPC_HANDLE_MARSHALLING
  270. processHandle,
  271. #endif
  272. (intptr_t)AutoSystemInfo::Data.GetChakraBaseAddr(),
  273. (intptr_t)AutoSystemInfo::Data.GetCRTHandle());
  274. }
  275. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  276. {
  277. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  278. }
  279. RpcEndExcept;
  280. #ifdef USE_RPC_HANDLE_MARSHALLING
  281. CloseHandle(processHandle);
  282. #endif
  283. return hr;
  284. }
  285. HRESULT
  286. JITManager::InitializeThreadContext(
  287. __in ThreadContextDataIDL * data,
  288. __out PPTHREADCONTEXT_HANDLE threadContextInfoAddress,
  289. __out intptr_t * prereservedRegionAddr,
  290. __out intptr_t * jitThunkAddr)
  291. {
  292. Assert(IsOOPJITEnabled());
  293. HRESULT hr = E_FAIL;
  294. RpcTryExcept
  295. {
  296. hr = ClientInitializeThreadContext(
  297. m_rpcBindingHandle,
  298. data,
  299. threadContextInfoAddress,
  300. prereservedRegionAddr,
  301. jitThunkAddr);
  302. }
  303. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  304. {
  305. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  306. }
  307. RpcEndExcept;
  308. return hr;
  309. }
  310. HRESULT
  311. JITManager::CleanupThreadContext(
  312. __inout PPTHREADCONTEXT_HANDLE threadContextInfoAddress)
  313. {
  314. Assert(IsOOPJITEnabled());
  315. HRESULT hr = E_FAIL;
  316. RpcTryExcept
  317. {
  318. hr = ClientCleanupThreadContext(m_rpcBindingHandle, threadContextInfoAddress);
  319. }
  320. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  321. {
  322. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  323. }
  324. RpcEndExcept;
  325. return hr;
  326. }
  327. HRESULT
  328. JITManager::AddDOMFastPathHelper(
  329. __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
  330. __in intptr_t funcInfoAddr,
  331. __in int helper)
  332. {
  333. Assert(IsOOPJITEnabled());
  334. HRESULT hr = E_FAIL;
  335. RpcTryExcept
  336. {
  337. hr = ClientAddDOMFastPathHelper(m_rpcBindingHandle, scriptContextInfoAddress, funcInfoAddr, helper);
  338. }
  339. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  340. {
  341. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  342. }
  343. RpcEndExcept;
  344. return hr;
  345. }
  346. HRESULT
  347. JITManager::SetIsPRNGSeeded(
  348. __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
  349. __in boolean value)
  350. {
  351. HRESULT hr = E_FAIL;
  352. RpcTryExcept
  353. {
  354. hr = ClientSetIsPRNGSeeded(m_rpcBindingHandle, scriptContextInfoAddress, value);
  355. }
  356. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  357. {
  358. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  359. }
  360. RpcEndExcept;
  361. return hr;
  362. }
  363. HRESULT
  364. JITManager::DecommitInterpreterBufferManager(
  365. __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
  366. __in boolean asmJsThunk)
  367. {
  368. Assert(IsOOPJITEnabled());
  369. HRESULT hr = E_FAIL;
  370. RpcTryExcept
  371. {
  372. hr = ClientDecommitInterpreterBufferManager(m_rpcBindingHandle, scriptContextInfoAddress, asmJsThunk);
  373. }
  374. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  375. {
  376. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  377. }
  378. RpcEndExcept;
  379. return hr;
  380. }
  381. HRESULT
  382. JITManager::NewInterpreterThunkBlock(
  383. __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
  384. __in InterpreterThunkInputIDL * thunkInput,
  385. __out InterpreterThunkOutputIDL * thunkOutput)
  386. {
  387. Assert(IsOOPJITEnabled());
  388. HRESULT hr = E_FAIL;
  389. RpcTryExcept
  390. {
  391. hr = ClientNewInterpreterThunkBlock(m_rpcBindingHandle, scriptContextInfoAddress, thunkInput, thunkOutput);
  392. }
  393. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  394. {
  395. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  396. }
  397. RpcEndExcept;
  398. return hr;
  399. }
  400. HRESULT
  401. JITManager::AddModuleRecordInfo(
  402. /* [in] */ PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
  403. /* [in] */ unsigned int moduleId,
  404. /* [in] */ intptr_t localExportSlotsAddr)
  405. {
  406. Assert(IsOOPJITEnabled());
  407. HRESULT hr = E_FAIL;
  408. RpcTryExcept
  409. {
  410. hr = ClientAddModuleRecordInfo(m_rpcBindingHandle, scriptContextInfoAddress, moduleId, localExportSlotsAddr);
  411. }
  412. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  413. {
  414. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  415. }
  416. RpcEndExcept;
  417. return hr;
  418. }
  419. HRESULT
  420. JITManager::SetWellKnownHostTypeId(
  421. __in PTHREADCONTEXT_HANDLE threadContextRoot,
  422. __in int typeId)
  423. {
  424. Assert(IsOOPJITEnabled());
  425. HRESULT hr = E_FAIL;
  426. RpcTryExcept
  427. {
  428. hr = ClientSetWellKnownHostTypeId(m_rpcBindingHandle, threadContextRoot, typeId);
  429. }
  430. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  431. {
  432. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  433. }
  434. RpcEndExcept;
  435. return hr;
  436. }
  437. HRESULT
  438. JITManager::UpdatePropertyRecordMap(
  439. __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
  440. __in_opt BVSparseNodeIDL * updatedPropsBVHead)
  441. {
  442. Assert(IsOOPJITEnabled());
  443. HRESULT hr = E_FAIL;
  444. RpcTryExcept
  445. {
  446. hr = ClientUpdatePropertyRecordMap(m_rpcBindingHandle, threadContextInfoAddress, updatedPropsBVHead);
  447. }
  448. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  449. {
  450. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  451. }
  452. RpcEndExcept;
  453. return hr;
  454. }
  455. HRESULT
  456. JITManager::InitializeScriptContext(
  457. __in ScriptContextDataIDL * data,
  458. __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
  459. __out PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
  460. {
  461. Assert(IsOOPJITEnabled());
  462. HRESULT hr = E_FAIL;
  463. RpcTryExcept
  464. {
  465. hr = ClientInitializeScriptContext(m_rpcBindingHandle, data, threadContextInfoAddress, scriptContextInfoAddress);
  466. }
  467. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  468. {
  469. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  470. }
  471. RpcEndExcept;
  472. return hr;
  473. }
  474. HRESULT
  475. JITManager::CleanupScriptContext(
  476. __inout PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
  477. {
  478. Assert(IsOOPJITEnabled());
  479. HRESULT hr = E_FAIL;
  480. RpcTryExcept
  481. {
  482. hr = ClientCleanupScriptContext(m_rpcBindingHandle, scriptContextInfoAddress);
  483. }
  484. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  485. {
  486. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  487. }
  488. RpcEndExcept;
  489. return hr;
  490. }
  491. HRESULT
  492. JITManager::CloseScriptContext(
  493. __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
  494. {
  495. Assert(IsOOPJITEnabled());
  496. HRESULT hr = E_FAIL;
  497. RpcTryExcept
  498. {
  499. hr = ClientCloseScriptContext(m_rpcBindingHandle, scriptContextInfoAddress);
  500. }
  501. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  502. {
  503. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  504. }
  505. RpcEndExcept;
  506. return hr;
  507. }
  508. HRESULT
  509. JITManager::FreeAllocation(
  510. __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
  511. __in intptr_t codeAddress)
  512. {
  513. Assert(IsOOPJITEnabled());
  514. HRESULT hr = E_FAIL;
  515. RpcTryExcept
  516. {
  517. hr = ClientFreeAllocation(m_rpcBindingHandle, scriptContextInfoAddress, codeAddress);
  518. }
  519. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  520. {
  521. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  522. }
  523. RpcEndExcept;
  524. return hr;
  525. }
  526. HRESULT
  527. JITManager::IsNativeAddr(
  528. __in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
  529. __in intptr_t address,
  530. __out boolean * result)
  531. {
  532. Assert(IsOOPJITEnabled());
  533. HRESULT hr = E_FAIL;
  534. RpcTryExcept
  535. {
  536. hr = ClientIsNativeAddr(m_rpcBindingHandle, threadContextInfoAddress, address, result);
  537. }
  538. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  539. {
  540. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  541. }
  542. RpcEndExcept;
  543. return hr;
  544. }
  545. HRESULT
  546. JITManager::RemoteCodeGenCall(
  547. __in CodeGenWorkItemIDL *workItemData,
  548. __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
  549. __out JITOutputIDL *jitData)
  550. {
  551. Assert(IsOOPJITEnabled());
  552. HRESULT hr = E_FAIL;
  553. RpcTryExcept
  554. {
  555. hr = ClientRemoteCodeGen(m_rpcBindingHandle, scriptContextInfoAddress, workItemData, jitData);
  556. }
  557. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  558. {
  559. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  560. }
  561. RpcEndExcept;
  562. return hr;
  563. }
  564. #if DBG
  565. HRESULT
  566. JITManager::IsInterpreterThunkAddr(
  567. __in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
  568. __in intptr_t address,
  569. __in boolean asmjsThunk,
  570. __out boolean * result)
  571. {
  572. Assert(IsOOPJITEnabled());
  573. HRESULT hr = E_FAIL;
  574. RpcTryExcept
  575. {
  576. hr = ClientIsInterpreterThunkAddr(m_rpcBindingHandle, scriptContextInfoAddress, address, asmjsThunk, result);
  577. }
  578. RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
  579. {
  580. hr = HRESULT_FROM_WIN32(RpcExceptionCode());
  581. }
  582. RpcEndExcept;
  583. return hr;
  584. }
  585. #endif
  586. #ifdef ENABLE_DEBUG_CONFIG_OPTIONS
  587. HRESULT
  588. JITManager::DeserializeRPCData(
  589. _In_reads_(bufferSize) const byte* buffer,
  590. _In_ uint bufferSize,
  591. _Out_ CodeGenWorkItemIDL **workItemData
  592. )
  593. {
  594. RPC_STATUS status = RPC_S_OK;
  595. handle_t marshalHandle = nullptr;
  596. *workItemData = nullptr;
  597. __try
  598. {
  599. RpcTryExcept
  600. {
  601. status = MesDecodeBufferHandleCreate((char*)buffer, bufferSize, &marshalHandle);
  602. if (status != RPC_S_OK)
  603. {
  604. return HRESULT_FROM_WIN32(status);
  605. }
  606. pCodeGenWorkItemIDL_Decode(
  607. marshalHandle,
  608. workItemData);
  609. }
  610. RpcExcept(I_RpcExceptionFilter(RpcExceptionCode()))
  611. {
  612. status = RpcExceptionCode();
  613. }
  614. RpcEndExcept;
  615. }
  616. __finally
  617. {
  618. MesHandleFree(marshalHandle);
  619. }
  620. return HRESULT_FROM_WIN32(status);
  621. }
  622. HRESULT
  623. JITManager::SerializeRPCData(_In_ CodeGenWorkItemIDL *workItemData, _Out_ size_t* bufferSize, _Outptr_result_buffer_(*bufferSize) const byte** outBuffer)
  624. {
  625. handle_t marshalHandle = nullptr;
  626. *bufferSize = 0;
  627. *outBuffer = nullptr;
  628. RPC_STATUS status = RPC_S_OK;
  629. __try
  630. {
  631. RpcTryExcept
  632. {
  633. char* data = nullptr;
  634. unsigned long encodedSize;
  635. status = MesEncodeDynBufferHandleCreate(
  636. &data,
  637. &encodedSize,
  638. &marshalHandle);
  639. if (status != RPC_S_OK)
  640. {
  641. return HRESULT_FROM_WIN32(status);
  642. }
  643. MIDL_ES_CODE encodeType = MES_ENCODE;
  644. #if TARGET_64
  645. encodeType = MES_ENCODE_NDR64;
  646. // We only support encode syntax NDR64, however MesEncodeDynBufferHandleCreate doesn't allow to specify it
  647. status = MesBufferHandleReset(
  648. marshalHandle,
  649. MES_DYNAMIC_BUFFER_HANDLE,
  650. encodeType,
  651. &data,
  652. 0,
  653. &encodedSize
  654. );
  655. if (status != RPC_S_OK)
  656. {
  657. return HRESULT_FROM_WIN32(status);
  658. }
  659. #endif
  660. // Calculate how big we need to create the buffer
  661. size_t tmpBufSize = pCodeGenWorkItemIDL_AlignSize(marshalHandle, &workItemData);
  662. size_t alignedBufSize = Math::Align<size_t>(tmpBufSize, 16);
  663. data = HeapNewNoThrowArray(char, alignedBufSize);
  664. if (!data)
  665. {
  666. // Ran out of memory
  667. return E_OUTOFMEMORY;
  668. }
  669. // Reset the buffer handle to a fixed buffer
  670. status = MesBufferHandleReset(
  671. marshalHandle,
  672. MES_FIXED_BUFFER_HANDLE,
  673. encodeType,
  674. &data,
  675. (unsigned long)alignedBufSize,
  676. &encodedSize
  677. );
  678. if (status != RPC_S_OK)
  679. {
  680. return HRESULT_FROM_WIN32(status);
  681. }
  682. pCodeGenWorkItemIDL_Encode(
  683. marshalHandle,
  684. &workItemData);
  685. *bufferSize = alignedBufSize;
  686. *outBuffer = (byte*)data;
  687. }
  688. RpcExcept(I_RpcExceptionFilter(RpcExceptionCode()))
  689. {
  690. status = RpcExceptionCode();
  691. }
  692. RpcEndExcept;
  693. }
  694. __finally
  695. {
  696. MesHandleFree(marshalHandle);
  697. }
  698. return HRESULT_FROM_WIN32(status);
  699. }
  700. #endif