Skip to content

Commit 68b7a26

Browse files
MikeHolmanwyrichte
authored andcommitted
use RPC handle marshalling when running on win8.1+
1 parent 9ea3919 commit 68b7a26

File tree

9 files changed

+121
-44
lines changed

9 files changed

+121
-44
lines changed

lib/Common/Core/SysInfo.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ AutoSystemInfo::Initialize()
8585
allocationGranularityPageCount = dwAllocationGranularity / dwPageSize;
8686

8787
isWindows8OrGreater = IsWindows8OrGreater();
88+
isWindows8Point1OrGreater = IsWindows8Point1OrGreater();
8889

8990
binaryName[0] = _u('\0');
9091

@@ -363,14 +364,28 @@ AutoSystemInfo::CheckForAtom() const
363364
bool
364365
AutoSystemInfo::IsWin8OrLater()
365366
{
367+
#if defined(WINVER) && WINVER >= _WIN32_WINNT_WIN8
368+
return true;
369+
#else
366370
return isWindows8OrGreater;
371+
#endif
372+
}
373+
374+
bool
375+
AutoSystemInfo::IsWin8Point1OrLater()
376+
{
377+
#if defined(WINVER) && WINVER >= _WIN32_WINNT_WINBLUE
378+
return true;
379+
#else
380+
return isWindows8Point1OrGreater;
381+
#endif
367382
}
368383

369384
#if defined(_CONTROL_FLOW_GUARD)
370385
bool
371386
AutoSystemInfo::IsWinThresholdOrLater()
372387
{
373-
#if defined(_M_ARM64)
388+
#if defined(_M_ARM64) || (defined(WINVER) && WINVER >= _WIN32_WINNT_WIN10)
374389
return true;
375390
#else
376391
return IsWindowsThresholdOrGreater();

lib/Common/Core/SysInfo.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class AutoSystemInfo : public SYSTEM_INFO
1616

1717
bool DisableDebugScopeCapture() const { return this->disableDebugScopeCapture; }
1818
bool IsWin8OrLater();
19+
bool IsWin8Point1OrLater();
1920
#if defined(_CONTROL_FLOW_GUARD)
2021
bool IsWinThresholdOrLater();
2122
#endif
@@ -88,6 +89,7 @@ class AutoSystemInfo : public SYSTEM_INFO
8889
AutoSystemInfo() : majorVersion(0), minorVersion(0), buildDateHash(0), buildTimeHash(0), crtSize(0) { Initialize(); }
8990
void Initialize();
9091
bool isWindows8OrGreater;
92+
bool isWindows8Point1OrGreater;
9193
uint allocationGranularityPageCount;
9294
HANDLE processHandle;
9395
DWORD crtSize;

lib/JITClient/JITClientStub.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
#pragma warning(disable: 28252)
99
#pragma warning(disable: 28253)
1010

11+
// HACK HACK HACK
12+
// MIDL gives compile error if using [system_handle] with stub targetting win8 or below,
13+
// but there is no issue unless the function using [system_handle] is actually called.
14+
// We have runtime check that prevents that function from being used on old OS,
15+
// so change #define here to bypass the error
16+
#if !(0x603 <= _WIN32_WINNT)
17+
#undef _WIN32_WINNT
18+
#define _WIN32_WINNT 0x603
19+
#endif
20+
1121
#include "ChakraJIT_c.c"
1222

1323
#pragma warning(pop)

lib/JITClient/JITManager.cpp

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -302,35 +302,55 @@ HRESULT
302302
JITManager::ConnectProcess(RPC_BINDING_HANDLE rpcBindingHandle)
303303
{
304304
Assert(IsOOPJITEnabled());
305+
HRESULT hr = E_FAIL;
305306

306-
#ifdef USE_RPC_HANDLE_MARSHALLING
307-
HANDLE processHandle;
308-
if (!DuplicateHandle(GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(), &processHandle, 0, false, DUPLICATE_SAME_ACCESS))
307+
if (AutoSystemInfo::Data.IsWin8Point1OrLater())
309308
{
310-
return HRESULT_FROM_WIN32(GetLastError());
311-
}
312-
#endif
309+
HANDLE processHandle = nullptr;
310+
// RPC handle marshalling is only available on 8.1+
311+
if (!DuplicateHandle(GetCurrentProcess(), GetCurrentProcess(), GetCurrentProcess(), &processHandle, 0, false, DUPLICATE_SAME_ACCESS))
312+
{
313+
return HRESULT_FROM_WIN32(GetLastError());
314+
}
313315

314-
HRESULT hr = E_FAIL;
315-
RpcTryExcept
316-
{
317-
hr = ClientConnectProcess(
318-
rpcBindingHandle,
319-
#ifdef USE_RPC_HANDLE_MARSHALLING
320-
processHandle,
321-
#endif
322-
(intptr_t)AutoSystemInfo::Data.GetChakraBaseAddr(),
323-
(intptr_t)AutoSystemInfo::Data.GetCRTHandle());
316+
RpcTryExcept
317+
{
318+
hr = ClientConnectProcessWithProcessHandle(
319+
rpcBindingHandle,
320+
processHandle,
321+
(intptr_t)AutoSystemInfo::Data.GetChakraBaseAddr(),
322+
(intptr_t)AutoSystemInfo::Data.GetCRTHandle());
323+
}
324+
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
325+
{
326+
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
327+
}
328+
RpcEndExcept;
329+
330+
if (processHandle)
331+
{
332+
CloseHandle(processHandle);
333+
}
324334
}
325-
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
335+
else
326336
{
327-
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
328-
}
329-
RpcEndExcept;
330-
331-
#ifdef USE_RPC_HANDLE_MARSHALLING
332-
CloseHandle(processHandle);
337+
#if (WINVER >= _WIN32_WINNT_WINBLUE)
338+
AssertOrFailFast(UNREACHED);
339+
#else
340+
RpcTryExcept
341+
{
342+
hr = ClientConnectProcess(
343+
rpcBindingHandle,
344+
(intptr_t)AutoSystemInfo::Data.GetChakraBaseAddr(),
345+
(intptr_t)AutoSystemInfo::Data.GetCRTHandle());
346+
}
347+
RpcExcept(RpcExceptionFilter(RpcExceptionCode()))
348+
{
349+
hr = HRESULT_FROM_WIN32(RpcExceptionCode());
350+
}
351+
RpcEndExcept;
333352
#endif
353+
}
334354

335355
return hr;
336356
}

lib/JITIDL/Chakra.JITIDL.vcxproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@
5050
</ItemGroup>
5151
<Import Project="$(BuildConfigPropsPath)Chakra.Build.targets" Condition="exists('$(BuildConfigPropsPath)Chakra.Build.targets')" />
5252
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
53-
</Project>
53+
</Project>

lib/JITIDL/ChakraJIT.idl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,21 @@ interface IChakraJIT
2626
typedef CodeGenWorkItemIDL* pCodeGenWorkItemIDL;
2727
HRESULT Shutdown([in] handle_t binding);
2828

29-
HRESULT ConnectProcess(
29+
HRESULT ConnectProcessWithProcessHandle(
3030
[in] handle_t binding,
31-
#ifdef USE_RPC_HANDLE_MARSHALLING
3231
[in, system_handle(sh_process, PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_QUERY_LIMITED_INFORMATION)] HANDLE processHandle,
33-
#endif
3432
[in] CHAKRA_PTR chakraBaseAddress,
3533
[in] CHAKRA_PTR crtBaseAddress
3634
);
3735

36+
#if !(WINVER >= _WIN32_WINNT_WINBLUE)
37+
HRESULT ConnectProcess(
38+
[in] handle_t binding,
39+
[in] CHAKRA_PTR chakraBaseAddress,
40+
[in] CHAKRA_PTR crtBaseAddress
41+
);
42+
#endif
43+
3844
HRESULT InitializeThreadContext(
3945
[in] handle_t binding,
4046
[in] ThreadContextDataIDL * threadData,

lib/JITIDL/JITTypes.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,6 @@ import "wtypes.idl";
1212
#include "sdkddkver.h"
1313
#endif
1414

15-
16-
#if defined(WINVER) && WINVER >= _WIN32_WINNT_WINBLUE // on 8.1+, RPC can marshal process handle for us
17-
#ifdef __midl
18-
cpp_quote("#define USE_RPC_HANDLE_MARSHALLING 1")
19-
#endif
20-
#define USE_RPC_HANDLE_MARSHALLING 1
21-
#endif
22-
2315
#if defined(TARGET_32)
2416
#ifdef __midl
2517
#define CHAKRA_WB_PTR int

lib/JITServer/JITServer.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,9 @@ __RPC_USER PSCRIPTCONTEXT_HANDLE_rundown(__RPC__in PSCRIPTCONTEXT_HANDLE phConte
123123
}
124124

125125
HRESULT
126-
ServerConnectProcess(
126+
ServerConnectProcessWithProcessHandle(
127127
handle_t binding,
128-
#ifdef USE_RPC_HANDLE_MARSHALLING
129128
HANDLE processHandle,
130-
#endif
131129
intptr_t chakraBaseAddress,
132130
intptr_t crtBaseAddress
133131
)
@@ -138,23 +136,47 @@ ServerConnectProcess(
138136
{
139137
return hr;
140138
}
141-
#ifdef USE_RPC_HANDLE_MARSHALLING
142-
HANDLE targetHandle;
139+
HANDLE targetHandle = nullptr;
140+
// RPC handle marshalling is only available on 8.1+
143141
if (!DuplicateHandle(GetCurrentProcess(), processHandle, GetCurrentProcess(), &targetHandle, 0, false, DUPLICATE_SAME_ACCESS))
144142
{
145143
Assert(UNREACHED);
146144
return E_ACCESSDENIED;
147145
}
148-
#else
149-
HANDLE targetHandle = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_QUERY_LIMITED_INFORMATION, false, clientPid);
146+
return ProcessContextManager::RegisterNewProcess(clientPid, targetHandle, chakraBaseAddress, crtBaseAddress);
147+
}
148+
149+
#if !(WINVER >= _WIN32_WINNT_WINBLUE)
150+
HRESULT
151+
ServerConnectProcess(
152+
handle_t binding,
153+
intptr_t chakraBaseAddress,
154+
intptr_t crtBaseAddress
155+
)
156+
{
157+
// Should use ServerConnectProcessWithProcessHandle on 8.1+
158+
if (AutoSystemInfo::Data.IsWin8Point1OrLater())
159+
{
160+
Assert(UNREACHED);
161+
return E_ACCESSDENIED;
162+
}
163+
164+
DWORD clientPid;
165+
HRESULT hr = HRESULT_FROM_WIN32(I_RpcBindingInqLocalClientPID(binding, &clientPid));
166+
if (FAILED(hr))
167+
{
168+
return hr;
169+
}
170+
HANDLE targetHandle = nullptr;
171+
targetHandle = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_QUERY_INFORMATION, false, clientPid);
150172
if (!targetHandle)
151173
{
152174
Assert(UNREACHED);
153175
return E_ACCESSDENIED;
154176
}
155-
#endif
156177
return ProcessContextManager::RegisterNewProcess(clientPid, targetHandle, chakraBaseAddress, crtBaseAddress);
157178
}
179+
#endif
158180

159181
#pragma warning(push)
160182
#pragma warning(disable:6387 28196) // PREFast does not understand the out context can be null here

lib/JITServer/JITServerStub.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
#pragma warning(disable: 28252)
99
#pragma warning(disable: 28253)
1010

11+
// HACK HACK HACK
12+
// MIDL gives compile error if using [system_handle] with stub targetting win8 or below,
13+
// but there is no issue unless the function using [system_handle] is actually called.
14+
// We have runtime check that prevents that function from being used on old OS,
15+
// so change #define here to bypass the error
16+
#if !(0x603 <= _WIN32_WINNT)
17+
#undef _WIN32_WINNT
18+
#define _WIN32_WINNT 0x603
19+
#endif
20+
1121
#include "ChakraJIT_s.c"
1222

1323
#pragma warning(pop)

0 commit comments

Comments
 (0)