|
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> |
| 26 | +#include "SharedUtil.h" |
23 | 27 |
|
24 | | -// #define CEF_ENABLE_SANDBOX |
25 | 28 | #ifdef CEF_ENABLE_SANDBOX |
26 | | - #pragma comment(lib, "cef_sandbox.lib") |
| 29 | +#include <cef3/cef/include/cef_sandbox_win.h> |
| 30 | +#pragma comment(lib, "cef_sandbox.lib") |
27 | 31 | #endif |
28 | 32 |
|
29 | | -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; |
30 | 37 |
|
31 | | -int _declspec(dllexport) InitCEF() |
32 | | -{ |
33 | | - // Get absolute CEFLauncher.exe path |
34 | | - TCHAR buffer[MAX_PATH]; |
35 | | - GetModuleFileName(NULL, buffer, MAX_PATH); |
36 | | - std::wstring currentFileName(buffer); |
| 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"; |
37 | 41 |
|
38 | | - // Extract MTA path and set DLL directory (absolute path is required here) |
39 | | - size_t pos = currentFileName.find_last_of(L'\\'); |
40 | | - std::wstring mtaPath = currentFileName.substr(0, pos - 3); // Strip "CEF" |
41 | | - SetDllDirectory(mtaPath.c_str()); |
| 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; |
42 | 45 |
|
43 | | - // Load libcef.dll from the DLL directory |
44 | | - assert(SUCCEEDED(__HrLoadAllImportsForDll("libcef.dll"))); |
| 46 | +using NtQueryInformationProcessFunc = NTSTATUS(NTAPI*)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG); |
45 | 47 |
|
46 | | - // Load CEF |
47 | | - CefMainArgs mainArgs(GetModuleHandle(NULL)); |
48 | | - CefRefPtr<CCefApp> app{new CCefApp}; |
| 48 | +// Safe parent monitor thread shutdown |
| 49 | +std::atomic<bool> g_bShouldTerminateMonitor{false}; |
| 50 | +std::atomic<HANDLE> g_hMonitorThread{nullptr}; |
49 | 51 |
|
50 | | - void* sandboxInfo = nullptr; |
51 | | -#ifdef CEF_ENABLE_SANDBOX |
52 | | - CefScopedSandboxInfo scopedSandbox; |
53 | | - sandboxInfo = scopedSandbox.sandbox_info(); |
54 | | -#endif |
| 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 | + } |
55 | 66 |
|
56 | | - const HANDLE parentCheckThread = CreateThread(nullptr, 0, CheckParentProcessAliveness, nullptr, 0, nullptr); |
| 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 | + } |
57 | 77 |
|
58 | | - const int exitCode = CefExecuteProcess(mainArgs, app, sandboxInfo); |
| 78 | + return static_cast<DWORD>(reinterpret_cast<ULONG_PTR>(info.Reserved3)); |
| 79 | + } |
59 | 80 |
|
60 | | - if (parentCheckThread != nullptr) |
| 81 | + void MonitorParentProcess(HANDLE parentProcess) noexcept |
61 | 82 | { |
62 | | - TerminateThread(parentCheckThread, 0); |
63 | | - 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 | + } |
64 | 102 | } |
| 103 | +} // namespace |
65 | 104 |
|
66 | | - return exitCode; |
67 | | -} |
| 105 | +DWORD WINAPI CheckParentProcessAliveness(LPVOID) noexcept; |
68 | 106 |
|
69 | | -static DWORD WINAPI CheckParentProcessAliveness(LPVOID) |
| 107 | +BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, [[maybe_unused]] LPVOID lpReserved) |
70 | 108 | { |
71 | | - NTSTATUS(NTAPI * queryInformation)(HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG) = nullptr; |
72 | | - |
73 | | - if (HMODULE const ntdll = GetModuleHandleW(L"ntdll.dll"); ntdll != nullptr) |
| 109 | + if (dwReason == DLL_PROCESS_ATTACH) |
| 110 | + { |
| 111 | + DisableThreadLibraryCalls(hModule); |
| 112 | + g_bShouldTerminateMonitor.store(false, std::memory_order_relaxed); |
| 113 | + g_hMonitorThread.store(nullptr, std::memory_order_relaxed); |
| 114 | + } |
| 115 | + else if (dwReason == DLL_PROCESS_DETACH) |
74 | 116 | { |
75 | | - queryInformation = reinterpret_cast<decltype(queryInformation)>(GetProcAddress(ntdll, "NtQueryInformationProcess")); |
| 117 | + g_bShouldTerminateMonitor.store(true, std::memory_order_release); |
76 | 118 | } |
| 119 | + |
| 120 | + return TRUE; |
| 121 | +} |
77 | 122 |
|
78 | | - if (queryInformation == nullptr) |
79 | | - 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)); |
80 | 131 |
|
81 | | - PROCESS_BASIC_INFORMATION info{}; |
| 132 | + if (FAILED(__HrLoadAllImportsForDll(CEF_DLL_NAME))) |
| 133 | + return CEF_INIT_ERROR_DLL_LOAD_FAILED; |
82 | 134 |
|
83 | | - ULONG returnLength = 0; |
84 | | - NTSTATUS status = queryInformation(GetCurrentProcess(), ProcessBasicInformation, &info, sizeof(info), &returnLength); |
| 135 | + const CefMainArgs mainArgs(GetModuleHandleW(nullptr)); |
| 136 | + const CefRefPtr<CCefApp> app{new CCefApp}; |
85 | 137 |
|
86 | | - if (!NT_SUCCESS(status) || returnLength < sizeof(PROCESS_BASIC_INFORMATION)) |
87 | | - return 2; |
| 138 | + void* sandboxInfo = nullptr; |
| 139 | +#ifdef CEF_ENABLE_SANDBOX |
| 140 | + const CefScopedSandboxInfo scopedSandbox; |
| 141 | + sandboxInfo = scopedSandbox.sandbox_info(); |
| 142 | +#endif |
88 | 143 |
|
89 | | - const auto parentProcessId = static_cast<DWORD>(reinterpret_cast<ULONG_PTR>(info.Reserved3)); |
90 | | - 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); |
91 | 147 |
|
92 | | - if (parentProcess == nullptr) |
93 | | - { |
94 | | - if (GetLastError() == ERROR_INVALID_PARAMETER) |
95 | | - ExitProcess(0); |
| 148 | + return CefExecuteProcess(mainArgs, app, sandboxInfo); |
| 149 | +} |
96 | 150 |
|
97 | | - return 3; |
98 | | - } |
| 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; |
99 | 156 |
|
100 | | - while (true) |
101 | | - { |
102 | | - DWORD exitCode{}; |
| 157 | + const auto parentProcessId = GetParentProcessId(queryFunc); |
| 158 | + if (parentProcessId == 0) |
| 159 | + return PARENT_CHECK_ERROR_QUERY_FAILED; |
103 | 160 |
|
104 | | - 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) |
105 | 167 | { |
106 | | - CloseHandle(parentProcess); |
107 | | - ExitProcess(exitCode); |
| 168 | + MonitorParentProcess(parentProcessFallback); |
| 169 | + CloseHandle(parentProcessFallback); |
| 170 | + return CEF_INIT_SUCCESS; |
108 | 171 | } |
109 | | - |
110 | | - Sleep(1000); |
| 172 | + return PARENT_CHECK_ERROR_OPEN_FAILED; |
111 | 173 | } |
112 | 174 |
|
113 | | - return 0; |
| 175 | + MonitorParentProcess(parentProcess); |
| 176 | + CloseHandle(parentProcess); |
| 177 | + return CEF_INIT_SUCCESS; |
114 | 178 | } |
0 commit comments