|
9 | 9 | * |
10 | 10 | *****************************************************************************/ |
11 | 11 |
|
| 12 | +// #define CEF_ENABLE_SANDBOX |
| 13 | + |
| 14 | +#include <atomic> |
| 15 | + |
12 | 16 | #define WIN32_NO_STATUS |
13 | 17 | #define WIN32_LEAN_AND_MEAN |
14 | 18 | #include <Windows.h> |
| 19 | + |
15 | 20 | #undef WIN32_NO_STATUS |
16 | 21 | #include <ntstatus.h> |
17 | | -#include <winnt.h> |
18 | 22 | #include <winternl.h> |
19 | 23 | #include <delayimp.h> |
| 24 | + |
20 | 25 | #include "CCefApp.h" |
21 | | -#include <string> |
22 | | -#include <cef3/cef/include/cef_sandbox_win.h> |
23 | | -#include <SharedUtil.h> |
| 26 | +#include "SharedUtil.h" |
24 | 27 |
|
25 | | -// #define CEF_ENABLE_SANDBOX |
26 | 28 | #ifdef CEF_ENABLE_SANDBOX |
27 | | - #pragma comment(lib, "cef_sandbox.lib") |
| 29 | +#include <cef3/cef/include/cef_sandbox_win.h> |
| 30 | +#pragma comment(lib, "cef_sandbox.lib") |
28 | 31 | #endif |
29 | 32 |
|
30 | | -DWORD WINAPI CheckParentProcessAliveness(LPVOID); |
| 33 | +// Return codes |
| 34 | +inline constexpr int CEF_INIT_SUCCESS = 0; |
| 35 | +inline constexpr int CEF_INIT_ERROR_NO_BASE_DIR = -1; |
| 36 | +inline constexpr int CEF_INIT_ERROR_DLL_LOAD_FAILED = -2; |
31 | 37 |
|
32 | | -int _declspec(dllexport) InitCEF() |
33 | | -{ |
34 | | - // Get MTA base directory and set DLL directory to MTA folder |
35 | | - const SString strBaseDir = SharedUtil::GetMTAProcessBaseDir(); |
36 | | - |
37 | | - if (strBaseDir.empty()) |
38 | | - { |
39 | | - // Unable to determine base directory - CEF cannot initialize |
40 | | - return -1; |
41 | | - } |
42 | | - |
43 | | - const SString strMTADir = SharedUtil::PathJoin(strBaseDir, "MTA"); |
44 | | - |
45 | | - SetDllDirectoryW(SharedUtil::FromUTF8(strMTADir)); |
| 38 | +inline constexpr DWORD CEF_PARENT_CHECK_INTERVAL = 1000; |
| 39 | +inline constexpr const char* CEF_DLL_NAME = "libcef.dll"; |
| 40 | +inline constexpr const char* CEF_MTA_SUBDIR = "MTA"; |
46 | 41 |
|
47 | | - // Load libcef.dll from the DLL directory |
48 | | - assert(SUCCEEDED(__HrLoadAllImportsForDll("libcef.dll"))); |
| 42 | +inline constexpr DWORD PARENT_CHECK_ERROR_NO_QUERY_FUNC = 1; |
| 43 | +inline constexpr DWORD PARENT_CHECK_ERROR_QUERY_FAILED = 2; |
| 44 | +inline constexpr DWORD PARENT_CHECK_ERROR_OPEN_FAILED = 3; |
49 | 45 |
|
50 | | - // Load CEF |
51 | | - CefMainArgs mainArgs(GetModuleHandle(NULL)); |
52 | | - CefRefPtr<CCefApp> app{new CCefApp}; |
| 46 | +using NtQueryInformationProcessFunc = NTSTATUS(NTAPI*)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG); |
53 | 47 |
|
54 | | - void* sandboxInfo = nullptr; |
55 | | -#ifdef CEF_ENABLE_SANDBOX |
56 | | - CefScopedSandboxInfo scopedSandbox; |
57 | | - sandboxInfo = scopedSandbox.sandbox_info(); |
58 | | -#endif |
| 48 | +// Safe parent monitor thread shutdown |
| 49 | +std::atomic<bool> g_bShouldTerminateMonitor{false}; |
| 50 | +std::atomic<HANDLE> g_hMonitorThread{nullptr}; |
59 | 51 |
|
60 | | - const HANDLE parentCheckThread = CreateThread(nullptr, 0, CheckParentProcessAliveness, nullptr, 0, nullptr); |
| 52 | +namespace |
| 53 | +{ |
| 54 | + [[nodiscard]] auto GetNtQueryInformationProcess() noexcept -> NtQueryInformationProcessFunc |
| 55 | + { |
| 56 | + const auto ntdll = GetModuleHandleW(L"ntdll.dll"); |
| 57 | + if (!ntdll) |
| 58 | + return nullptr; |
| 59 | + |
| 60 | + const auto procAddr = GetProcAddress(ntdll, "NtQueryInformationProcess"); |
| 61 | + if (!procAddr) |
| 62 | + return nullptr; |
| 63 | + |
| 64 | + return reinterpret_cast<NtQueryInformationProcessFunc>(procAddr); |
| 65 | + } |
61 | 66 |
|
62 | | - const int exitCode = CefExecuteProcess(mainArgs, app, sandboxInfo); |
| 67 | + [[nodiscard]] auto GetParentProcessId(NtQueryInformationProcessFunc queryFunc) noexcept -> DWORD |
| 68 | + { |
| 69 | + PROCESS_BASIC_INFORMATION info{}; |
| 70 | + ULONG returnLength = 0; |
| 71 | + |
| 72 | + if (const auto status = queryFunc(GetCurrentProcess(), ProcessBasicInformation, &info, sizeof(info), &returnLength); |
| 73 | + !NT_SUCCESS(status) || returnLength < sizeof(PROCESS_BASIC_INFORMATION)) |
| 74 | + { |
| 75 | + return 0; |
| 76 | + } |
63 | 77 |
|
64 | | - if (parentCheckThread != nullptr) |
| 78 | + return static_cast<DWORD>(reinterpret_cast<ULONG_PTR>(info.Reserved3)); |
| 79 | + } |
| 80 | + |
| 81 | + void MonitorParentProcess(HANDLE parentProcess) noexcept |
65 | 82 | { |
66 | | - TerminateThread(parentCheckThread, 0); |
67 | | - CloseHandle(parentCheckThread); |
| 83 | + while (!g_bShouldTerminateMonitor.load(std::memory_order_acquire)) |
| 84 | + { |
| 85 | + const DWORD result = WaitForSingleObject(parentProcess, CEF_PARENT_CHECK_INTERVAL); |
| 86 | + |
| 87 | + if (result == WAIT_OBJECT_0) |
| 88 | + { |
| 89 | + DWORD exitCode = 0; |
| 90 | + if (GetExitCodeProcess(parentProcess, &exitCode)) |
| 91 | + ExitProcess(exitCode); |
| 92 | + else |
| 93 | + ExitProcess(0); |
| 94 | + } |
| 95 | + else if (result == WAIT_FAILED) |
| 96 | + { |
| 97 | + // Wine/Proton compatibility: Exit thread instead of terminating process |
| 98 | + // Wine's handle implementation dont support all wait operations reliably |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
68 | 102 | } |
| 103 | +} // namespace |
69 | 104 |
|
70 | | - return exitCode; |
71 | | -} |
| 105 | +DWORD WINAPI CheckParentProcessAliveness(LPVOID) noexcept; |
72 | 106 |
|
73 | | -static DWORD WINAPI CheckParentProcessAliveness(LPVOID) |
| 107 | +BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, [[maybe_unused]] LPVOID lpReserved) |
74 | 108 | { |
75 | | - NTSTATUS(NTAPI * queryInformation)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG) = nullptr; |
76 | | - |
77 | | - if (auto ntdll = GetModuleHandleW(L"ntdll.dll"); ntdll != nullptr) |
| 109 | + if (dwReason == DLL_PROCESS_ATTACH) |
78 | 110 | { |
79 | | - auto procAddr = GetProcAddress(ntdll, "NtQueryInformationProcess"); |
80 | | - queryInformation = reinterpret_cast<decltype(queryInformation)>(reinterpret_cast<void*>(procAddr)); |
| 111 | + DisableThreadLibraryCalls(hModule); |
| 112 | + g_bShouldTerminateMonitor.store(false, std::memory_order_relaxed); |
| 113 | + g_hMonitorThread.store(nullptr, std::memory_order_relaxed); |
81 | 114 | } |
| 115 | + else if (dwReason == DLL_PROCESS_DETACH) |
| 116 | + { |
| 117 | + g_bShouldTerminateMonitor.store(true, std::memory_order_release); |
| 118 | + } |
| 119 | + |
| 120 | + return TRUE; |
| 121 | +} |
82 | 122 |
|
83 | | - if (queryInformation == nullptr) |
84 | | - return 1; |
| 123 | +extern "C" [[nodiscard]] __declspec(dllexport) auto InitCEF() noexcept -> int |
| 124 | +{ |
| 125 | + const auto baseDir = SharedUtil::GetMTAProcessBaseDir(); |
| 126 | + if (baseDir.empty()) |
| 127 | + return CEF_INIT_ERROR_NO_BASE_DIR; |
| 128 | + |
| 129 | + const auto mtaDir = SharedUtil::PathJoin(baseDir, CEF_MTA_SUBDIR); |
| 130 | + SetDllDirectoryW(SharedUtil::FromUTF8(mtaDir)); |
85 | 131 |
|
86 | | - PROCESS_BASIC_INFORMATION info{}; |
| 132 | + if (FAILED(__HrLoadAllImportsForDll(CEF_DLL_NAME))) |
| 133 | + return CEF_INIT_ERROR_DLL_LOAD_FAILED; |
87 | 134 |
|
88 | | - ULONG returnLength = 0; |
89 | | - NTSTATUS status = queryInformation(GetCurrentProcess(), ProcessBasicInformation, &info, sizeof(info), &returnLength); |
| 135 | + const CefMainArgs mainArgs(GetModuleHandleW(nullptr)); |
| 136 | + const CefRefPtr<CCefApp> app{new CCefApp}; |
90 | 137 |
|
91 | | - if (!NT_SUCCESS(status) || returnLength < sizeof(PROCESS_BASIC_INFORMATION)) |
92 | | - return 2; |
| 138 | + void* sandboxInfo = nullptr; |
| 139 | +#ifdef CEF_ENABLE_SANDBOX |
| 140 | + const CefScopedSandboxInfo scopedSandbox; |
| 141 | + sandboxInfo = scopedSandbox.sandbox_info(); |
| 142 | +#endif |
93 | 143 |
|
94 | | - const auto parentProcessId = static_cast<DWORD>(reinterpret_cast<ULONG_PTR>(info.Reserved3)); |
95 | | - const HANDLE parentProcess = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, parentProcessId); |
| 144 | + const auto hThread = CreateThread(nullptr, 0, CheckParentProcessAliveness, nullptr, 0, nullptr); |
| 145 | + if (hThread) |
| 146 | + g_hMonitorThread.store(hThread, std::memory_order_release); |
96 | 147 |
|
97 | | - if (parentProcess == nullptr) |
98 | | - { |
99 | | - if (GetLastError() == ERROR_INVALID_PARAMETER) |
100 | | - ExitProcess(0); |
| 148 | + return CefExecuteProcess(mainArgs, app, sandboxInfo); |
| 149 | +} |
101 | 150 |
|
102 | | - return 3; |
103 | | - } |
| 151 | +static auto WINAPI CheckParentProcessAliveness([[maybe_unused]] LPVOID) noexcept -> DWORD |
| 152 | +{ |
| 153 | + const auto queryFunc = GetNtQueryInformationProcess(); |
| 154 | + if (!queryFunc) |
| 155 | + return PARENT_CHECK_ERROR_NO_QUERY_FUNC; |
104 | 156 |
|
105 | | - while (true) |
106 | | - { |
107 | | - DWORD exitCode{}; |
| 157 | + const auto parentProcessId = GetParentProcessId(queryFunc); |
| 158 | + if (parentProcessId == 0) |
| 159 | + return PARENT_CHECK_ERROR_QUERY_FAILED; |
108 | 160 |
|
109 | | - if (!GetExitCodeProcess(parentProcess, &exitCode) || exitCode != STILL_ACTIVE) |
| 161 | + const auto parentProcess = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, false, parentProcessId); |
| 162 | + if (!parentProcess) |
| 163 | + { |
| 164 | + // Wine/Proton fallback: PROCESS_QUERY_LIMITED_INFORMATION may not be implemented |
| 165 | + const auto parentProcessFallback = OpenProcess(SYNCHRONIZE, false, parentProcessId); |
| 166 | + if (parentProcessFallback) |
110 | 167 | { |
111 | | - CloseHandle(parentProcess); |
112 | | - ExitProcess(exitCode); |
| 168 | + MonitorParentProcess(parentProcessFallback); |
| 169 | + CloseHandle(parentProcessFallback); |
| 170 | + return CEF_INIT_SUCCESS; |
113 | 171 | } |
114 | | - |
115 | | - Sleep(1000); |
| 172 | + return PARENT_CHECK_ERROR_OPEN_FAILED; |
116 | 173 | } |
117 | 174 |
|
118 | | - return 0; |
| 175 | + MonitorParentProcess(parentProcess); |
| 176 | + CloseHandle(parentProcess); |
| 177 | + return CEF_INIT_SUCCESS; |
119 | 178 | } |
0 commit comments