Skip to content

runtime/shims: shim api-ms-win-core-com-l1-1-0..3 lifecycle and helpers - #9516

Closed
piyonchu wants to merge 2 commits into
ProjectLeonida:mainfrom
piyonchu:fix/com-init-isolation
Closed

piyonchu wants to merge 2 commits into
ProjectLeonida:mainfrom
piyonchu:fix/com-init-isolation

Conversation

@piyonchu

@piyonchu piyonchu commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claim: #9065 (comment)

Why

Mapped executables import CoInitializeEx, CoUninitialize, CoCreateInstance, CoCreateGuid, CoTaskMemFree, PropVariantClear, and StringFromGUID2 through api-ms-win-core-com-l1-1-0.dll. Bound directly to host COM:

  • When a host thread (or host library) has already initialized STA, a guest call to CoInitializeEx(nullptr, COINIT_MULTITHREADED) fails with RPC_E_CHANGED_MODE (0x80010106).
  • Guest CoUninitialize() calls that are unbalanced or paired with mismatched init can tear down host COM early.

What changed

  • runtime/shims/com_shim.cpp:
    • Tracks per-thread guest COM depth and owned host init count in a TlsAlloc() slot initialized via InitOnceExecuteOnce (0 _Init_thread_header guards or thread_local destructors).
    • Absorbs host/guest apartment mismatches cleanly (S_OK for initial guest init under host COM while detecting the thread's real apartment via CoGetApartmentType and retaining an owned host CoInitializeEx reference so guest COM outlives host-first teardown; matching calls on that real apartment return S_FALSE, while true intra-guest mismatches still return RPC_E_CHANGED_MODE).
    • Protects host COM from unbalanced CoUninitialize(), validates null/invalid pointers in CoInitializeEx, CoCreateInstance, CoCreateGuid, and StringFromGUID2, and keeps ole32.dll host-owned (rt::shims::owns_dll("ole32.dll") == false).
  • runtime/test/com_shim_test.cpp, runtime/CMakeLists.txt, scripts/build_runtime.ps1:
    • Added com_shim_test (com_shim CTest) covering fresh-thread balance, host-STA + guest-MTA isolation, host-first teardown ordering, intra-guest mismatch, helper validation, and zero ole32.dll pseudo-ownership.

Verification (Windows 11, MSVC 19.44, CPU-only build — no Vulkan headers or SDL3)

  • powershell -ExecutionPolicy Bypass -File scripts/build_runtime.ps1 -> exit 0 (0 warnings)
  • dumpbin /symbols on com_shim.cpp.obj -> 0 _Init_thread_header references
  • ctest --test-dir runtime\build --output-on-failure -V -R "^com_shim$" -> 42 check(s), 0 failure(s) after commit 464bb23d9
  • python -I scripts/check_no_game_data.py ... & git diff --check -> exit 0
  • gta6rt.exe --dry-run verified all 7 api-ms-win-core-com-l1-1-0.dll imports bind as shim.

AI-assisted: yes (implemented and verified on Windows CPU suite; Vulkan/SDL3 GPU paths not built locally).

@milas6669

Copy link
Copy Markdown

Checked head ced87bf25 merged onto main 867d922f0, clean (4 files, +313/-1). Native Windows 11 26200, MSVC 19.44 RelWithDebInfo. com_shim_test: 41 check(s), 0 failure(s). com_shim.cpp is a new file, so there is no negative control against main.

New data: I ran the same CoInitializeEx sequences through the shim and through the real COM, each on its own fresh thread (a throwaway probe added to the test, removed afterwards; the host thread's own init is noted per row). Three calls per row, results in order:

sequence shim real COM
fresh thread: MTA, MTA, STA S_OK S_FALSE RPC_E_CHANGED_MODE same
fresh thread: STA, STA, MTA S_OK S_FALSE RPC_E_CHANGED_MODE same
host already STA; guest MTA, STA, MTA S_OK RPC_E_CHANGED_MODE S_FALSE RPC_E_CHANGED_MODE S_FALSE RPC_E_CHANGED_MODE
host already STA; guest STA, STA, MTA S_OK S_FALSE RPC_E_CHANGED_MODE S_FALSE S_FALSE RPC_E_CHANGED_MODE
host already MTA; guest MTA, STA, MTA S_OK RPC_E_CHANGED_MODE S_FALSE S_FALSE RPC_E_CHANGED_MODE S_FALSE

Fresh-thread behaviour is identical. The differences are all on a thread the host has already initialised. Two are intended and harmless: the first guest call returns S_OK where real COM says S_FALSE (same mode as the host), and S_OK where real COM says RPC_E_CHANGED_MODE (the mismatch the PR chooses to absorb). One is not intended: after the shim absorbs a guest MTA request on a host STA thread, a later guest STA request returns RPC_E_CHANGED_MODE, although the thread really is STA and real COM answers S_FALSE. The shim decides by the guest's own first request (first_coinit), not by the thread's actual apartment. A title that asks for MTA first and STA second on such a thread would now see a failure it would not see on a PC; remembering the real apartment (from the host CoInitializeEx result or CoGetApartmentType) would remove it.

No defect in what the tests cover. No game data. AI-assisted (Claude Code).

@OliverMarcusson

Copy link
Copy Markdown

Tested ced87bf2 (merged cleanly onto main 5d74c272) on Windows 11 10.0.26200 (RTX 5070 Ti), MSVC 19.44 (RelWithDebInfo, Windows SDK 10.0.26100.0): pass.

  • Built com_shim_test and gta6rt cleanly.
  • ctest -C RelWithDebInfo -R ^com_shim$: 1/1 PASS (0 failures)

AI-assisted (Gemini).

@illusionart453-max illusionart453-max left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Native Windows review of ced87bf, MSVC 19.44 / Windows SDK 10.0.26100 / RelWithDebInfo:

The supplied com_shim_test builds and passes 41 checks. I added a local native regression for a guest initialization outliving the host's earlier initialization; the result is 42 checks, one failure.

At runtime/shims/com_shim.cpp:62-65, absorbing RPC_E_CHANGED_MODE records host_inits_owned = 0. This borrows the host's STA without retaining any native COM initialization for the successful guest call. On a fresh std::thread:

  1. Host CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED) returns S_OK.
  2. Shim CoInitializeEx(nullptr, COINIT_MULTITHREADED) returns S_OK.
  3. Host CoUninitialize balances its own successful call, while guest depth is still 1.
  4. Native CoGetApartmentType returns CO_E_NOTINITIALIZED (0x800401f0).
  5. Shim CoCreateInstance(GUID_NULL, ..., IID_IUnknown, &out) also returns CO_E_NOTINITIALIZED rather than reaching class lookup.
  6. Guest CoUninitialize cleans its logical state, but there was no owned native init keeping COM available.

Please retain an actual compatible host-apartment initialization for the guest lifetime when absorbing the mismatch, and balance only the reference the shim owns, or otherwise ensure the successful guest init remains usable when the host balances its separate ownership. Add this host-first teardown ordering alongside the current guest-first test. The current test only proves the host survives guest teardown.

This is an executed CPU/native-COM reproduction, no GPU or game files. Only the review test file was changed locally; production shim code remains this head. AI-assisted (Codex).

@BatSmacker84

Copy link
Copy Markdown

Tested ced87bf25 merged onto main 5d74c2723 on AMD Radeon RX 7800 XT, driver 26.11.2, Windows 11 26200, MSVC 19.44 + Ninja RelWithDebInfo: pass.

  • ctest -R '^com_shim$': 41/41.
  • Control: a registry-only check against main misses all 28 COM function registrations.
  • The PR description stops mid-sentence before any test section, so I took the test command from CMake; worth completing it.

AI-assisted: Codex GPT-6.2 Sol ran the builds and tests, Claude Opus 5.5 reviewed the report. Synthetic fixtures only, no game data.

@piyonchu

Copy link
Copy Markdown
Contributor Author

Addressed feedback from @illusionart453-max and @milas6669: on RPC_E_CHANGED_MODE, co_initialize_ex now detects the thread's actual existing apartment (CoGetApartmentType) and acquires an owned host CoInitializeEx reference in that apartment so guest COM remains initialized even if host COM balances first, and subsequent guest requests matching the real thread apartment return S_FALSE. Added the host-first teardown regression in com_shim_test (42 check(s), 0 failure(s); 0 _Init_thread_header guards).

@sgonzalez-glitch

Copy link
Copy Markdown

Tested 464bb23d9 (as part of #9516, #9549, #9614, #9653, #9657, #9660, #9678 and #9689 merged together onto main 5d74c2723) on NVIDIA GeForce RTX 2080, driver 621.24 (32.0.16.2124), Windows 11, MSVC 19.44 RelWithDebInfo: pass.

AI-assisted: yes

@BatSmacker84

Copy link
Copy Markdown

Retested 464bb23d9 merged onto main 5d74c2723 on Windows 11 26200, MSVC 19.44 + Ninja RelWithDebInfo (AMD Radeon RX 7800 XT, driver 26.11.2): pass.

  • ctest -R '^com_shim$': 42/42 checks.
  • The current test against the previous head's com_shim.cpp (ced87bf25): 40/42. The two new failures are the host-first teardown check and the updated host-STA / guest-MTA check, so the new tests catch the change.
  • Control: a registry-only check against main misses all 28 registrations.

AI-assisted: Codex GPT-6.2 Sol ran the builds and tests, Claude Opus 5.5 reviewed the report. Synthetic fixtures only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants