upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/com_shim.cpp
diff options
context:
space:
mode:
authorpiyonchu <156166227+piyonchu@users.noreply.github.com>2026-11-24 04:43:36 +0700
committerpiyonchu <156166227+piyonchu@users.noreply.github.com>2026-11-24 04:43:36 +0700
commit464bb23d9f09623cb27a8ff7b6ea986a50c3d827 (patch)
tree8bd697706dfe45b3e1c5c81b6809a7030f46db31 /runtime/shims/com_shim.cpp
parentced87bf258bcb9d89f9ab5b622758d82a4b768e3 (diff)
com_shim: retain host apartment reference when absorbing RPC_E_CHANGED_MODE
Diffstat (limited to 'runtime/shims/com_shim.cpp')
-rw-r--r--runtime/shims/com_shim.cpp55
1 files changed, 39 insertions, 16 deletions
diff --git a/runtime/shims/com_shim.cpp b/runtime/shims/com_shim.cpp
index 5ad96787e..55962e215 100644
--- a/runtime/shims/com_shim.cpp
+++ b/runtime/shims/com_shim.cpp
@@ -22,7 +22,8 @@ namespace {
22struct ThreadComState { 22struct ThreadComState {
23 uint32_t guest_depth = 0; 23 uint32_t guest_depth = 0;
24 uint32_t host_inits_owned = 0; 24 uint32_t host_inits_owned = 0;
25 DWORD first_coinit = COINIT_MULTITHREADED; 25 DWORD requested_coinit = COINIT_MULTITHREADED;
26 DWORD host_coinit = COINIT_MULTITHREADED;
26 bool mismatch_absorbed = false; 27 bool mismatch_absorbed = false;
27}; 28};
28 29
@@ -43,6 +44,17 @@ bool apartment_differs(DWORD a, DWORD b) {
43 return (a & COINIT_APARTMENTTHREADED) != (b & COINIT_APARTMENTTHREADED); 44 return (a & COINIT_APARTMENTTHREADED) != (b & COINIT_APARTMENTTHREADED);
44} 45}
45 46
47DWORD detect_existing_host_coinit(DWORD attempted_coinit) {
48 APTTYPE apt = APTTYPE_CURRENT;
49 APTTYPEQUALIFIER qual = APTTYPEQUALIFIER_NONE;
50 DWORD base = (attempted_coinit & COINIT_APARTMENTTHREADED) ? COINIT_MULTITHREADED : COINIT_APARTMENTTHREADED;
51 if (SUCCEEDED(::CoGetApartmentType(&apt, &qual))) {
52 if (apt == APTTYPE_STA || apt == APTTYPE_MAINSTA) base = COINIT_APARTMENTTHREADED;
53 else if (apt == APTTYPE_MTA) base = COINIT_MULTITHREADED;
54 }
55 return base | (attempted_coinit & (COINIT_DISABLE_OLE1DDE | COINIT_SPEED_OVER_MEMORY));
56}
57
46HRESULT WINAPI co_initialize_ex(LPVOID reserved, DWORD coinit) { 58HRESULT WINAPI co_initialize_ex(LPVOID reserved, DWORD coinit) {
47 if (reserved != nullptr) return E_INVALIDARG; 59 if (reserved != nullptr) return E_INVALIDARG;
48 60
@@ -54,31 +66,42 @@ HRESULT WINAPI co_initialize_ex(LPVOID reserved, DWORD coinit) {
54 const HRESULT hr = ::CoInitializeEx(nullptr, coinit); 66 const HRESULT hr = ::CoInitializeEx(nullptr, coinit);
55 if (FAILED(hr) && hr != RPC_E_CHANGED_MODE) return hr; 67 if (FAILED(hr) && hr != RPC_E_CHANGED_MODE) return hr;
56 68
69 DWORD owned_mode = coinit;
70 bool absorbed = false;
71 uint32_t owned_count = 1;
72 if (hr == RPC_E_CHANGED_MODE) {
73 // Retain an actual host COM reference in the thread's existing apartment so guest COM stays alive even if
74 // the host calls CoUninitialize() while the guest still holds an active initialization.
75 owned_mode = detect_existing_host_coinit(coinit);
76 const HRESULT hold_hr = ::CoInitializeEx(nullptr, owned_mode);
77 if (FAILED(hold_hr)) return hold_hr;
78 absorbed = true;
79 }
80
57 auto* created = new (std::nothrow) ThreadComState{}; 81 auto* created = new (std::nothrow) ThreadComState{};
58 if (!created) { 82 if (!created) {
59 if (SUCCEEDED(hr)) ::CoUninitialize(); 83 if (owned_count) ::CoUninitialize();
60 return E_OUTOFMEMORY; 84 return E_OUTOFMEMORY;
61 } 85 }
62 created->guest_depth = 1; 86 created->guest_depth = 1;
63 created->first_coinit = coinit; 87 created->host_inits_owned = owned_count;
64 if (hr == RPC_E_CHANGED_MODE) { 88 created->requested_coinit = coinit;
65 created->host_inits_owned = 0; 89 created->host_coinit = owned_mode;
66 created->mismatch_absorbed = true; 90 created->mismatch_absorbed = absorbed;
67 } else {
68 created->host_inits_owned = 1;
69 created->mismatch_absorbed = false;
70 }
71 TlsSetValue(slot, created); 91 TlsSetValue(slot, created);
72 return S_OK; 92 return S_OK;
73 } 93 }
74 94
75 if (apartment_differs(state->first_coinit, coinit)) return RPC_E_CHANGED_MODE; 95 const bool matches_host = !apartment_differs(state->host_coinit, coinit);
96 const bool matches_absorbed_guest = state->mismatch_absorbed && !apartment_differs(state->requested_coinit, coinit);
97 if (!matches_host && !matches_absorbed_guest) return RPC_E_CHANGED_MODE;
76 98
77 if (!state->mismatch_absorbed) { 99 const DWORD forward_mode =
78 const HRESULT hr = ::CoInitializeEx(nullptr, coinit); 100 matches_host ? coinit : (state->host_coinit | (coinit & (COINIT_DISABLE_OLE1DDE | COINIT_SPEED_OVER_MEMORY)));
79 if (FAILED(hr)) return hr; 101 const HRESULT hr = ::CoInitializeEx(nullptr, forward_mode);
80 ++state->host_inits_owned; 102 if (FAILED(hr)) return hr;
81 } 103
104 ++state->host_inits_owned;
82 ++state->guest_depth; 105 ++state->guest_depth;
83 return S_FALSE; 106 return S_FALSE;
84} 107}