diff options
| author | piyonchu <156166227+piyonchu@users.noreply.github.com> | 2026-11-24 04:21:05 +0700 |
|---|---|---|
| committer | piyonchu <156166227+piyonchu@users.noreply.github.com> | 2026-11-24 04:21:05 +0700 |
| commit | ced87bf258bcb9d89f9ab5b622758d82a4b768e3 (patch) | |
| tree | 265160dda632f458ae7fefda96b5f41e5512299c /runtime/shims/com_shim.cpp | |
| parent | 867d922f09b86583859796a174df89555fb32da8 (diff) | |
runtime/shims: shim api-ms-win-core-com-l1-1-0..3 lifecycle and helpers
Refs: https://github.com/ItsNotRexed/Leonida/issues/9065#issuecomment-7114294575
Diffstat (limited to 'runtime/shims/com_shim.cpp')
| -rw-r--r-- | runtime/shims/com_shim.cpp | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/runtime/shims/com_shim.cpp b/runtime/shims/com_shim.cpp new file mode 100644 index 000000000..5ad96787e --- /dev/null +++ b/runtime/shims/com_shim.cpp | |||
| @@ -0,0 +1,150 @@ | |||
| 1 | // COM lifecycle and helper shims for api-ms-win-core-com-l1-1-0..3.dll. | ||
| 2 | // | ||
| 3 | // game_scarlett_master.exe imports CoInitializeEx, CoUninitialize, CoCreateInstance, CoCreateGuid, CoTaskMemFree, | ||
| 4 | // PropVariantClear, and StringFromGUID2 through api-ms-win-core-com-l1-1-0.dll. When the host (or a host library) has | ||
| 5 | // already initialized a thread as STA, a guest call to CoInitializeEx(nullptr, COINIT_MULTITHREADED) fails with | ||
| 6 | // RPC_E_CHANGED_MODE (0x80010106) and an unbalanced CoUninitialize can tear down host COM early. | ||
| 7 | // | ||
| 8 | // Per-thread guest COM depth and host init ownership are tracked via a TlsAlloc slot (constant-initialized via | ||
| 9 | // InitOnceExecuteOnce with zero _Init_thread_header TLS guards or C++ thread_local destructors). | ||
| 10 | #include <windows.h> | ||
| 11 | #include <objbase.h> | ||
| 12 | #include <propidl.h> | ||
| 13 | |||
| 14 | #include <cstdint> | ||
| 15 | #include <new> | ||
| 16 | |||
| 17 | #include "shims.h" | ||
| 18 | |||
| 19 | namespace rt::com_shim { | ||
| 20 | namespace { | ||
| 21 | |||
| 22 | struct ThreadComState { | ||
| 23 | uint32_t guest_depth = 0; | ||
| 24 | uint32_t host_inits_owned = 0; | ||
| 25 | DWORD first_coinit = COINIT_MULTITHREADED; | ||
| 26 | bool mismatch_absorbed = false; | ||
| 27 | }; | ||
| 28 | |||
| 29 | INIT_ONCE g_tls_once = INIT_ONCE_STATIC_INIT; | ||
| 30 | DWORD g_tls_index = TLS_OUT_OF_INDEXES; | ||
| 31 | |||
| 32 | BOOL CALLBACK init_tls_once(PINIT_ONCE, PVOID, PVOID*) { | ||
| 33 | g_tls_index = TlsAlloc(); | ||
| 34 | return TRUE; | ||
| 35 | } | ||
| 36 | |||
| 37 | DWORD tls_slot() { | ||
| 38 | InitOnceExecuteOnce(&g_tls_once, init_tls_once, nullptr, nullptr); | ||
| 39 | return g_tls_index; | ||
| 40 | } | ||
| 41 | |||
| 42 | bool apartment_differs(DWORD a, DWORD b) { | ||
| 43 | return (a & COINIT_APARTMENTTHREADED) != (b & COINIT_APARTMENTTHREADED); | ||
| 44 | } | ||
| 45 | |||
| 46 | HRESULT WINAPI co_initialize_ex(LPVOID reserved, DWORD coinit) { | ||
| 47 | if (reserved != nullptr) return E_INVALIDARG; | ||
| 48 | |||
| 49 | const DWORD slot = tls_slot(); | ||
| 50 | if (slot == TLS_OUT_OF_INDEXES) return ::CoInitializeEx(nullptr, coinit); | ||
| 51 | |||
| 52 | auto* state = static_cast<ThreadComState*>(TlsGetValue(slot)); | ||
| 53 | if (!state) { | ||
| 54 | const HRESULT hr = ::CoInitializeEx(nullptr, coinit); | ||
| 55 | if (FAILED(hr) && hr != RPC_E_CHANGED_MODE) return hr; | ||
| 56 | |||
| 57 | auto* created = new (std::nothrow) ThreadComState{}; | ||
| 58 | if (!created) { | ||
| 59 | if (SUCCEEDED(hr)) ::CoUninitialize(); | ||
| 60 | return E_OUTOFMEMORY; | ||
| 61 | } | ||
| 62 | created->guest_depth = 1; | ||
| 63 | created->first_coinit = coinit; | ||
| 64 | if (hr == RPC_E_CHANGED_MODE) { | ||
| 65 | created->host_inits_owned = 0; | ||
| 66 | created->mismatch_absorbed = true; | ||
| 67 | } else { | ||
| 68 | created->host_inits_owned = 1; | ||
| 69 | created->mismatch_absorbed = false; | ||
| 70 | } | ||
| 71 | TlsSetValue(slot, created); | ||
| 72 | return S_OK; | ||
| 73 | } | ||
| 74 | |||
| 75 | if (apartment_differs(state->first_coinit, coinit)) return RPC_E_CHANGED_MODE; | ||
| 76 | |||
| 77 | if (!state->mismatch_absorbed) { | ||
| 78 | const HRESULT hr = ::CoInitializeEx(nullptr, coinit); | ||
| 79 | if (FAILED(hr)) return hr; | ||
| 80 | ++state->host_inits_owned; | ||
| 81 | } | ||
| 82 | ++state->guest_depth; | ||
| 83 | return S_FALSE; | ||
| 84 | } | ||
| 85 | |||
| 86 | void WINAPI co_uninitialize() { | ||
| 87 | const DWORD slot = tls_slot(); | ||
| 88 | if (slot == TLS_OUT_OF_INDEXES) { | ||
| 89 | ::CoUninitialize(); | ||
| 90 | return; | ||
| 91 | } | ||
| 92 | auto* state = static_cast<ThreadComState*>(TlsGetValue(slot)); | ||
| 93 | if (!state || state->guest_depth == 0) return; | ||
| 94 | |||
| 95 | --state->guest_depth; | ||
| 96 | if (state->guest_depth == 0) { | ||
| 97 | for (uint32_t i = 0; i < state->host_inits_owned; ++i) { | ||
| 98 | ::CoUninitialize(); | ||
| 99 | } | ||
| 100 | TlsSetValue(slot, nullptr); | ||
| 101 | delete state; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | HRESULT WINAPI co_create_instance(REFCLSID rclsid, LPUNKNOWN outer, DWORD cls_context, REFIID riid, LPVOID* ppv) { | ||
| 106 | if (!ppv) return E_POINTER; | ||
| 107 | *ppv = nullptr; | ||
| 108 | return ::CoCreateInstance(rclsid, outer, cls_context, riid, ppv); | ||
| 109 | } | ||
| 110 | |||
| 111 | HRESULT WINAPI co_create_guid(GUID* guid) { | ||
| 112 | if (!guid) return E_INVALIDARG; | ||
| 113 | return ::CoCreateGuid(guid); | ||
| 114 | } | ||
| 115 | |||
| 116 | void WINAPI co_task_mem_free(LPVOID pv) { ::CoTaskMemFree(pv); } | ||
| 117 | |||
| 118 | HRESULT WINAPI prop_variant_clear(PROPVARIANT* pvar) { return ::PropVariantClear(pvar); } | ||
| 119 | |||
| 120 | int WINAPI string_from_guid2(REFGUID rguid, LPOLESTR lpsz, int cch_max) { | ||
| 121 | if (!lpsz || cch_max <= 0) return 0; | ||
| 122 | return ::StringFromGUID2(rguid, lpsz, cch_max); | ||
| 123 | } | ||
| 124 | |||
| 125 | } // namespace | ||
| 126 | } // namespace rt::com_shim | ||
| 127 | |||
| 128 | namespace { | ||
| 129 | |||
| 130 | using namespace rt::com_shim; | ||
| 131 | |||
| 132 | #define COM_L1_API(dll) \ | ||
| 133 | {dll, "CoInitializeEx", reinterpret_cast<void*>(&co_initialize_ex)}, \ | ||
| 134 | {dll, "CoUninitialize", reinterpret_cast<void*>(&co_uninitialize)}, \ | ||
| 135 | {dll, "CoCreateInstance", reinterpret_cast<void*>(&co_create_instance)}, \ | ||
| 136 | {dll, "CoCreateGuid", reinterpret_cast<void*>(&co_create_guid)}, \ | ||
| 137 | {dll, "CoTaskMemFree", reinterpret_cast<void*>(&co_task_mem_free)}, \ | ||
| 138 | {dll, "PropVariantClear", reinterpret_cast<void*>(&prop_variant_clear)}, \ | ||
| 139 | {dll, "StringFromGUID2", reinterpret_cast<void*>(&string_from_guid2)} | ||
| 140 | |||
| 141 | } // namespace | ||
| 142 | |||
| 143 | // Registered strictly on the api-ms-win-core-com API sets (not ole32.dll/combase.dll) so rt::shims::owns_dll keeps | ||
| 144 | // host COM DLLs unbound to pseudo-handles while answering all mapped api-ms-win-core-com-* imports. | ||
| 145 | SHIM_TABLE(com_shim, | ||
| 146 | COM_L1_API("api-ms-win-core-com-l1-1-0.dll"), | ||
| 147 | COM_L1_API("api-ms-win-core-com-l1-1-1.dll"), | ||
| 148 | COM_L1_API("api-ms-win-core-com-l1-1-2.dll"), | ||
| 149 | COM_L1_API("api-ms-win-core-com-l1-1-3.dll"), | ||
| 150 | ); | ||