forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJITManager.cpp
More file actions
809 lines (706 loc) · 20.8 KB
/
JITManager.cpp
File metadata and controls
809 lines (706 loc) · 20.8 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "JITClientPch.h"
_Must_inspect_result_
_Ret_maybenull_ _Post_writable_byte_size_(size)
void * __RPC_USER midl_user_allocate(
#if defined(_WIN32_WINNT_WIN10)
_In_ // starting win10, _In_ is in the signature
#endif
size_t size)
{
return (HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size));
}
void __RPC_USER midl_user_free(_Pre_maybenull_ _Post_invalid_ void * ptr)
{
if (ptr != NULL)
{
HeapFree(GetProcessHeap(), NULL, ptr);
}
}
JITManager JITManager::s_jitManager = JITManager();
JITManager::JITManager() :
m_rpcBindingHandle(nullptr),
m_oopJitEnabled(false),
m_isJITServer(false),
m_failingHRESULT(S_OK),
m_jitConnectionId()
{
}
JITManager::~JITManager()
{
if (m_rpcBindingHandle)
{
RpcBindingFree(&m_rpcBindingHandle);
}
}
/* static */
JITManager *
JITManager::GetJITManager()
{
return &s_jitManager;
}
typedef struct _CHAKRA_RPC_SECURITY_QOS_V5 {
unsigned long Version;
unsigned long Capabilities;
unsigned long IdentityTracking;
unsigned long ImpersonationType;
unsigned long AdditionalSecurityInfoType;
union
{
RPC_HTTP_TRANSPORT_CREDENTIALS_W* HttpCredentials;
} u;
void* Sid;
unsigned int EffectiveOnly;
void* ServerSecurityDescriptor;
} CHAKRA_RPC_SECURITY_QOS_V5;
// This routine creates a binding with the server.
HRESULT
JITManager::CreateBinding(
__in HANDLE serverProcessHandle,
__in_opt void * serverSecurityDescriptor,
__in UUID * connectionUuid,
__out RPC_BINDING_HANDLE * bindingHandle)
{
Assert(IsOOPJITEnabled());
RPC_STATUS status;
DWORD attemptCount = 0;
DWORD sleepInterval = 100; // in milliseconds
RPC_BINDING_HANDLE localBindingHandle;
RPC_BINDING_HANDLE_TEMPLATE_V1 bindingTemplate;
RPC_BINDING_HANDLE_SECURITY_V1_W bindingSecurity;
CHAKRA_RPC_SECURITY_QOS_V5 securityQOS;
ZeroMemory(&securityQOS, sizeof(CHAKRA_RPC_SECURITY_QOS_V5));
securityQOS.Capabilities = RPC_C_QOS_CAPABILITIES_DEFAULT;
securityQOS.IdentityTracking = RPC_C_QOS_IDENTITY_DYNAMIC;
securityQOS.ImpersonationType = RPC_C_IMP_LEVEL_IDENTIFY;
securityQOS.Version = AutoSystemInfo::Data.IsWin8OrLater() ? 5 : 4;
securityQOS.ServerSecurityDescriptor = serverSecurityDescriptor;
ZeroMemory(&bindingTemplate, sizeof(bindingTemplate));
bindingTemplate.Version = 1;
bindingTemplate.ProtocolSequence = RPC_PROTSEQ_LRPC;
bindingTemplate.StringEndpoint = NULL;
memcpy_s(&bindingTemplate.ObjectUuid, sizeof(UUID), connectionUuid, sizeof(UUID));
bindingTemplate.Flags |= RPC_BHT_OBJECT_UUID_VALID;
ZeroMemory(&bindingSecurity, sizeof(bindingSecurity));
bindingSecurity.Version = 1;
bindingSecurity.AuthnLevel = RPC_C_AUTHN_LEVEL_PKT_PRIVACY;
bindingSecurity.AuthnSvc = RPC_C_AUTHN_KERNEL;
bindingSecurity.SecurityQos = (RPC_SECURITY_QOS*)&securityQOS;
status = RpcBindingCreate(&bindingTemplate, &bindingSecurity, NULL, &localBindingHandle);
if (status != RPC_S_OK)
{
return HRESULT_FROM_WIN32(status);
}
// We keep attempting to connect to the server with increasing wait intervals in between.
// This will wait close to 5 minutes before it finally gives up.
do
{
DWORD waitStatus;
status = RpcBindingBind(NULL, localBindingHandle, ClientIChakraJIT_v0_0_c_ifspec);
if (status == RPC_S_OK)
{
break;
}
else if (status == EPT_S_NOT_REGISTERED)
{
// The Server side has not finished registering the RPC Server yet.
// We should only breakout if we have reached the max attempt count.
if (attemptCount > 600)
{
break;
}
}
else
{
// Some unknown error occurred. We are not going to retry for arbitrary errors.
break;
}
// When we come to this point, it means the server has not finished registration yet.
// We should wait for a while and then reattempt to bind.
waitStatus = WaitForSingleObject(serverProcessHandle, sleepInterval);
if (waitStatus == WAIT_OBJECT_0)
{
// The server process died for some reason. No need to reattempt.
status = RPC_S_SERVER_UNAVAILABLE;
break;
}
else if (waitStatus == WAIT_TIMEOUT)
{
// Not an error. the server is still alive and we should reattempt.
}
else
{
Assert(waitStatus == WAIT_FAILED);
#ifdef DBG
LPWSTR messageBuffer = nullptr;
DWORD errorNumber = GetLastError();
FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorNumber, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&messageBuffer, 0, NULL);
Output::Print(_u("Last error was 0x%x (%s)"), errorNumber, messageBuffer);
LocalFree(messageBuffer);
#endif
// wait operation failed for an unknown reason.
Assert(false);
status = HRESULT_FROM_WIN32(waitStatus);
break;
}
attemptCount++;
if (sleepInterval < 500)
{
sleepInterval += 100;
}
} while (status != RPC_S_OK); // redundant check, but compiler would not allow true here.
*bindingHandle = localBindingHandle;
return HRESULT_FROM_WIN32(status);
}
bool
JITManager::IsJITServer() const
{
return m_isJITServer;
}
void
JITManager::SetIsJITServer()
{
m_isJITServer = true;
m_oopJitEnabled = true;
}
bool
JITManager::IsConnected() const
{
Assert(IsOOPJITEnabled());
return m_rpcBindingHandle != nullptr && !HasJITFailed();
}
void
JITManager::EnableOOPJIT()
{
m_oopJitEnabled = true;
if (CONFIG_FLAG(OOPCFGRegistration))
{
// Since this client has enabled OOPJIT, perform the one-way policy update
// that will disable SetProcessValidCallTargets from being invoked.
GlobalSecurityPolicy::DisableSetProcessValidCallTargets();
}
}
void
JITManager::SetJITFailed(HRESULT hr)
{
Assert(hr != S_OK);
m_failingHRESULT = hr;
}
bool
JITManager::HasJITFailed() const
{
return m_failingHRESULT != S_OK;
}
bool
JITManager::IsOOPJITEnabled() const
{
return m_oopJitEnabled;
}
#pragma prefast(push)
#pragma prefast(disable:__WARNING_RELEASING_UNHELD_LOCK_MEDIUM_CONFIDENCE, "Lock is correctly acquired and released by RAII class AutoCriticalSection")
#pragma prefast(disable:__WARNING_CALLER_FAILING_TO_HOLD, "Lock is correctly acquired and released by RAII class AutoCriticalSection")
HRESULT
JITManager::ConnectRpcServer(__in HANDLE jitProcessHandle, __in_opt void* serverSecurityDescriptor, __in UUID connectionUuid)
{
// We might be trying to connect from multiple threads simultaneously
AutoCriticalSection cs(&m_cs);
Assert(IsOOPJITEnabled());
if (m_rpcBindingHandle != nullptr)
{
return S_OK;
}
HRESULT hr = E_FAIL;
RPC_BINDING_HANDLE bindingHandle;
hr = CreateBinding(jitProcessHandle, serverSecurityDescriptor, &connectionUuid, &bindingHandle);
if (FAILED(hr))
{
goto FailureCleanup;
}
hr = ConnectProcess(bindingHandle);
HandleServerCallResult(hr, RemoteCallType::StateUpdate);
// Only store the binding handle after JIT handshake, so other threads do not prematurely think we are ready to JIT
m_rpcBindingHandle = bindingHandle;
m_jitConnectionId = connectionUuid;
return hr;
FailureCleanup:
if (m_rpcBindingHandle)
{
RpcBindingFree(&m_rpcBindingHandle);
m_rpcBindingHandle = nullptr;
}
return hr;
}
#pragma prefast(pop)
HRESULT
JITManager::Shutdown()
{
// this is special case of shutdown called when runtime process is a parent of the server process
// used for console host type scenarios
HRESULT hr = S_OK;
Assert(IsOOPJITEnabled());
Assert(m_rpcBindingHandle != nullptr);
RpcTryExcept
{
ClientShutdown(m_rpcBindingHandle);
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
m_rpcBindingHandle = nullptr;
return hr;
}
HRESULT
JITManager::ConnectProcess(RPC_BINDING_HANDLE rpcBindingHandle)
{
Assert(IsOOPJITEnabled());
HRESULT hr = E_FAIL;
if (AutoSystemInfo::Data.IsWin8Point1OrLater())
{
HANDLE processHandle = nullptr;
// RPC handle marshalling is only available on 8.1+
if (!DuplicateHandle(GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(), &processHandle, 0, false, DUPLICATE_SAME_ACCESS))
{
return HRESULT_FROM_WIN32(GetLastError());
}
RpcTryExcept
{
hr = ClientConnectProcessWithProcessHandle(
rpcBindingHandle,
processHandle,
(intptr_t)AutoSystemInfo::Data.GetChakraBaseAddr(),
(intptr_t)AutoSystemInfo::Data.GetCRTHandle());
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
if (processHandle)
{
CloseHandle(processHandle);
}
}
else
{
#if (WINVER >= _WIN32_WINNT_WINBLUE)
AssertOrFailFast(UNREACHED);
#else
RpcTryExcept
{
hr = ClientConnectProcess(
rpcBindingHandle,
(intptr_t)AutoSystemInfo::Data.GetChakraBaseAddr(),
(intptr_t)AutoSystemInfo::Data.GetCRTHandle());
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
#endif
}
return hr;
}
HRESULT
JITManager::InitializeThreadContext(
__in ThreadContextDataIDL * data,
__out PPTHREADCONTEXT_HANDLE threadContextInfoAddress,
__out intptr_t * prereservedRegionAddr,
__out intptr_t * jitThunkAddr)
{
Assert(IsOOPJITEnabled());
HRESULT hr = E_FAIL;
RpcTryExcept
{
hr = ClientInitializeThreadContext(
m_rpcBindingHandle,
data,
threadContextInfoAddress,
prereservedRegionAddr,
jitThunkAddr);
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
return hr;
}
HRESULT
JITManager::CleanupThreadContext(
__inout PPTHREADCONTEXT_HANDLE threadContextInfoAddress)
{
Assert(IsOOPJITEnabled());
HRESULT hr = E_FAIL;
RpcTryExcept
{
hr = ClientCleanupThreadContext(m_rpcBindingHandle, threadContextInfoAddress);
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
return hr;
}
HRESULT
JITManager::SetIsPRNGSeeded(
__in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
__in boolean value)
{
HRESULT hr = E_FAIL;
RpcTryExcept
{
hr = ClientSetIsPRNGSeeded(m_rpcBindingHandle, scriptContextInfoAddress, value);
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
return hr;
}
HRESULT
JITManager::DecommitInterpreterBufferManager(
__in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
__in boolean asmJsThunk)
{
Assert(IsOOPJITEnabled());
HRESULT hr = E_FAIL;
RpcTryExcept
{
hr = ClientDecommitInterpreterBufferManager(m_rpcBindingHandle, scriptContextInfoAddress, asmJsThunk);
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
return hr;
}
HRESULT
JITManager::NewInterpreterThunkBlock(
__in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
__in InterpreterThunkInputIDL * thunkInput,
__out InterpreterThunkOutputIDL * thunkOutput)
{
Assert(IsOOPJITEnabled());
HRESULT hr = E_FAIL;
RpcTryExcept
{
hr = ClientNewInterpreterThunkBlock(m_rpcBindingHandle, scriptContextInfoAddress, thunkInput, thunkOutput);
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
return hr;
}
HRESULT
JITManager::AddModuleRecordInfo(
/* [in] */ PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
/* [in] */ unsigned int moduleId,
/* [in] */ intptr_t localExportSlotsAddr)
{
Assert(IsOOPJITEnabled());
HRESULT hr = E_FAIL;
RpcTryExcept
{
hr = ClientAddModuleRecordInfo(m_rpcBindingHandle, scriptContextInfoAddress, moduleId, localExportSlotsAddr);
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
return hr;
}
HRESULT
JITManager::SetWellKnownHostTypeId(
__in PTHREADCONTEXT_HANDLE threadContextRoot,
__in int typeId)
{
Assert(IsOOPJITEnabled());
HRESULT hr = E_FAIL;
RpcTryExcept
{
hr = ClientSetWellKnownHostTypeId(m_rpcBindingHandle, threadContextRoot, typeId);
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
return hr;
}
HRESULT
JITManager::UpdatePropertyRecordMap(
__in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
__in_opt BVSparseNodeIDL * updatedPropsBVHead)
{
Assert(IsOOPJITEnabled());
HRESULT hr = E_FAIL;
RpcTryExcept
{
hr = ClientUpdatePropertyRecordMap(m_rpcBindingHandle, threadContextInfoAddress, updatedPropsBVHead);
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
return hr;
}
HRESULT
JITManager::InitializeScriptContext(
__in ScriptContextDataIDL * data,
__in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
__out PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
{
Assert(IsOOPJITEnabled());
HRESULT hr = E_FAIL;
RpcTryExcept
{
hr = ClientInitializeScriptContext(m_rpcBindingHandle, data, threadContextInfoAddress, scriptContextInfoAddress);
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
return hr;
}
HRESULT
JITManager::CleanupScriptContext(
__inout PPSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
{
Assert(IsOOPJITEnabled());
HRESULT hr = E_FAIL;
RpcTryExcept
{
hr = ClientCleanupScriptContext(m_rpcBindingHandle, scriptContextInfoAddress);
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
return hr;
}
HRESULT
JITManager::CloseScriptContext(
__in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress)
{
Assert(IsOOPJITEnabled());
HRESULT hr = E_FAIL;
RpcTryExcept
{
hr = ClientCloseScriptContext(m_rpcBindingHandle, scriptContextInfoAddress);
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
return hr;
}
HRESULT
JITManager::FreeAllocation(
__in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
__in intptr_t codeAddress)
{
Assert(IsOOPJITEnabled());
HRESULT hr = E_FAIL;
RpcTryExcept
{
hr = ClientFreeAllocation(m_rpcBindingHandle, scriptContextInfoAddress, codeAddress);
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
return hr;
}
HRESULT
JITManager::IsNativeAddr(
__in PTHREADCONTEXT_HANDLE threadContextInfoAddress,
__in intptr_t address,
__out boolean * result)
{
Assert(IsOOPJITEnabled());
HRESULT hr = E_FAIL;
RpcTryExcept
{
hr = ClientIsNativeAddr(m_rpcBindingHandle, threadContextInfoAddress, address, result);
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
return hr;
}
HRESULT
JITManager::RemoteCodeGenCall(
__in CodeGenWorkItemIDL *workItemData,
__in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
__out JITOutputIDL *jitData)
{
Assert(IsOOPJITEnabled());
HRESULT hr = E_FAIL;
RpcTryExcept
{
hr = ClientRemoteCodeGen(m_rpcBindingHandle, scriptContextInfoAddress, workItemData, jitData);
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
return hr;
}
#if DBG
HRESULT
JITManager::IsInterpreterThunkAddr(
__in PSCRIPTCONTEXT_HANDLE scriptContextInfoAddress,
__in intptr_t address,
__in boolean asmjsThunk,
__out boolean * result)
{
Assert(IsOOPJITEnabled());
HRESULT hr = E_FAIL;
RpcTryExcept
{
hr = ClientIsInterpreterThunkAddr(m_rpcBindingHandle, scriptContextInfoAddress, address, asmjsThunk, result);
}
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
{
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
}
RpcEndExcept;
return hr;
}
#endif
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
HRESULT
JITManager::DeserializeRPCData(
_In_reads_(bufferSize) const byte* buffer,
_In_ uint bufferSize,
_Out_ CodeGenWorkItemIDL **workItemData
)
{
RPC_STATUS status = RPC_S_OK;
handle_t marshalHandle = nullptr;
*workItemData = nullptr;
__try
{
RpcTryExcept
{
status = MesDecodeBufferHandleCreate((char*)buffer, bufferSize, &marshalHandle);
if (status != RPC_S_OK)
{
return HRESULT_FROM_WIN32(status);
}
pCodeGenWorkItemIDL_Decode(
marshalHandle,
workItemData);
}
RpcExcept(I_RpcExceptionFilter(RpcExceptionCode()))
{
status = RpcExceptionCode();
}
RpcEndExcept;
}
__finally
{
MesHandleFree(marshalHandle);
}
return HRESULT_FROM_WIN32(status);
}
HRESULT
JITManager::SerializeRPCData(_In_ CodeGenWorkItemIDL *workItemData, _Out_ size_t* bufferSize, _Outptr_result_buffer_(*bufferSize) const byte** outBuffer)
{
handle_t marshalHandle = nullptr;
*bufferSize = 0;
*outBuffer = nullptr;
RPC_STATUS status = RPC_S_OK;
__try
{
RpcTryExcept
{
char* data = nullptr;
unsigned long encodedSize;
status = MesEncodeDynBufferHandleCreate(
&data,
&encodedSize,
&marshalHandle);
if (status != RPC_S_OK)
{
return HRESULT_FROM_WIN32(status);
}
MIDL_ES_CODE encodeType = MES_ENCODE;
#if TARGET_64
encodeType = MES_ENCODE_NDR64;
// We only support encode syntax NDR64, however MesEncodeDynBufferHandleCreate doesn't allow to specify it
status = MesBufferHandleReset(
marshalHandle,
MES_DYNAMIC_BUFFER_HANDLE,
encodeType,
&data,
0,
&encodedSize
);
if (status != RPC_S_OK)
{
return HRESULT_FROM_WIN32(status);
}
#endif
// Calculate how big we need to create the buffer
size_t tmpBufSize = pCodeGenWorkItemIDL_AlignSize(marshalHandle, &workItemData);
size_t alignedBufSize = Math::Align<size_t>(tmpBufSize, 16);
data = HeapNewNoThrowArray(char, alignedBufSize);
if (!data)
{
// Ran out of memory
return E_OUTOFMEMORY;
}
// Reset the buffer handle to a fixed buffer
status = MesBufferHandleReset(
marshalHandle,
MES_FIXED_BUFFER_HANDLE,
encodeType,
&data,
(unsigned long)alignedBufSize,
&encodedSize
);
if (status != RPC_S_OK)
{
return HRESULT_FROM_WIN32(status);
}
pCodeGenWorkItemIDL_Encode(
marshalHandle,
&workItemData);
*bufferSize = alignedBufSize;
*outBuffer = (byte*)data;
}
RpcExcept(I_RpcExceptionFilter(RpcExceptionCode()))
{
status = RpcExceptionCode();
}
RpcEndExcept;
}
__finally
{
MesHandleFree(marshalHandle);
}
return HRESULT_FROM_WIN32(status);
}
#endif