runtime: patch GetProcAddress in every loaded module #2158

Merged
SimSalabimse merged 4 commits from upstream-pr/2166 into main 2026-11-25 03:25:50 +00:00
SimSalabimse commented 2026-11-25 02:32:48 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by SimSalabimse on 2026-11-25T02:32:48Z.


Change

Account-picker TRACE_API (X2W_TRACE_API=1) showed Y reaching GameInput (buttons 0x20) and GetKeyState, but zero QueryApiImpl / user-provider / x2w-api lines. The proxy had rewritten only the main EXE GetProcAddress IAT (1 GetProcAddress import(s) of the main module patched), so a helper DLL or delay-load path never hit the hook and FakeUser never wrapped a user provider.

This change rewrites, in every loaded module (and later loads via LdrRegisterDllNotification):

  • GetProcAddress by name (regular and delay-import IAT)
  • a direct xgameruntime.dll!QueryApiImpl named import

The GameInput stand-in’s own GetProcAddress slot is left alone so the hook does not recurse.

Refs #870. Does not complete FakeUser XUserAddAsync (slot 7); that remains unjustified until a re-run shows provider calls after Y.

AI-assisted.

Verification

g++ -std=c++17 -Wall -Wextra -Werror -Isrc -o /tmp/gpa_patch_check tests/startup_runtime_gpa_patch_check.cpp
/tmp/gpa_patch_check
# -> startup_runtime_gpa_patch: ok
/tmp/gpa_patch_check --mutate name-only
# -> FAIL: name-only accepts QueryApiImpl from other.dll (exit 1)

On Windows after configure:

ctest --preset dev --no-tests=error -R '^startup_runtime_gpa_patch(_name_only_control)?$'

Windows runtime / D3D12 rebuild of startup_stub_gameinput was not run on this Linux host.

Evidence and follow-up

Synthetic PE32+ image only (no game files). Negative control: name-only mutate must print FAIL:.

docs/startup-runtime.md X2W_RUNTIME_PROXY row updated.

User re-run after this build: same run.ps1 with $env:X2W_TRACE_API='1', press Y, and check for multi-module patch lines plus any QueryApiImpl / provider … proxied / slot 7/8. If still zero providers, resolution is outside the patched IAT/GPA path.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6595842303 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2166). Originally posted by [SimSalabimse](https://github.com/SimSalabimse) on 2026-11-25T02:32:48Z. --- ## Change Account-picker TRACE_API (`X2W_TRACE_API=1`) showed Y reaching GameInput (`buttons 0x20`) and `GetKeyState`, but **zero** `QueryApiImpl` / user-provider / `x2w-api` lines. The proxy had rewritten only the **main EXE** `GetProcAddress` IAT (`1 GetProcAddress import(s) of the main module patched`), so a helper DLL or delay-load path never hit the hook and FakeUser never wrapped a user provider. This change rewrites, in every loaded module (and later loads via `LdrRegisterDllNotification`): - `GetProcAddress` by name (regular and delay-import IAT) - a direct `xgameruntime.dll!QueryApiImpl` named import The GameInput stand-in’s own `GetProcAddress` slot is left alone so the hook does not recurse. Refs #870. Does **not** complete FakeUser `XUserAddAsync` (slot 7); that remains unjustified until a re-run shows provider calls after Y. AI-assisted. ## Verification g++ -std=c++17 -Wall -Wextra -Werror -Isrc -o /tmp/gpa_patch_check tests/startup_runtime_gpa_patch_check.cpp /tmp/gpa_patch_check # -> startup_runtime_gpa_patch: ok /tmp/gpa_patch_check --mutate name-only # -> FAIL: name-only accepts QueryApiImpl from other.dll (exit 1) On Windows after configure: ctest --preset dev --no-tests=error -R '^startup_runtime_gpa_patch(_name_only_control)?$' Windows runtime / D3D12 rebuild of `startup_stub_gameinput` was not run on this Linux host. ## Evidence and follow-up Synthetic PE32+ image only (no game files). Negative control: name-only mutate must print `FAIL:`. `docs/startup-runtime.md` `X2W_RUNTIME_PROXY` row updated. **User re-run after this build:** same `run.ps1` with `$env:X2W_TRACE_API='1'`, press Y, and check for multi-module patch lines plus any `QueryApiImpl` / `provider … proxied` / slot 7/8. If still zero providers, resolution is outside the patched IAT/GPA path. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets.
tedin7 commented 2026-11-25 03:01:15 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T02:39:08Z.

Review: COMMENTED


Review of head fbc106173da1b94496f12494a590cf63c77ee6ba.

CI: Linux checks, gfx1013 fixtures and Windows build/test pass. Mergeable. Title is neutral, no game data.

Commands executed locally (Linux):

g++ -std=c++17 -Wall -Wextra -Werror -Isrc -o /tmp/gpa_sm7 tests/startup_runtime_gpa_patch_check.cpp && /tmp/gpa_sm7
# -> startup_runtime_gpa_patch: ok
/tmp/gpa_sm7 --mutate name-only   # -> FAIL: name-only accepts QueryApiImpl from other.dll
python3 tests/docs_links_check.py       # -> Checked 18 Markdown files and 401 links.
python3 tests/bidi_controls_check.py    # -> no bidirectional controls
git diff --check origin/main...HEAD     # -> clean

Mutations on the new logic:

  • Skip the delay-import directory (dirs[13] -> dirs[0]): test FAILS (3 mismatches). Killed.
  • Remove the ordinal-import skip in visit_named_thunks: test still passes. SURVIVED (see P2-2).

P1

  1. src/startup_stubs/runtime_proxy.cpp (patch_module, QueryApiImpl branch, ~line 437-443) plus runtime_gpa_patch.h (delay-import walk). Delay-import IAT slots do not hold the real function before the first call. They hold the delay-load thunk. The delay helper later writes the resolved address over the slot.
    • For a delay-imported xgameruntime.dll!QueryApiImpl, real_query can be set to the delay thunk address (when real_query is still null). Later real_query(...) calls (lines ~713, 743, 871 and proxy_query) then jump into the thunk, not the export.
    • For a delay-imported GetProcAddress, the slot is replaced by the hook, then the helper overwrites it at first use. The hook is lost, so the delay path the PR text claims to cover is not actually hooked.
    • Suggested fix: for delay imports, resolve the real export with GetProcAddress(GetModuleHandle("xgameruntime.dll"), "QueryApiImpl") and do not read *slot. Or only record real_query from a non-delay slot. Hook delay GetProcAddress through the delay-load hook (__pfnDliNotifyHook), or state that delay imports are not covered.
    • The test only checks that the walk finds the delay hit. It does not check that patching it works.

P2

  1. runtime_proxy.cpp patch_imports/patch_all_loaded_modules: the enumeration runs on a separate thread while dll_loaded runs in loader context. Both call patch_module and write real_query (plain global, no atomic or lock). The slot compare avoids double patching, but real_query can race. Make it std::atomic or set it once under a lock. Also, a module loaded between registration and enumeration is covered twice, which is fine, but a module loaded before registration and after the main-module patch is only covered by the thread, so a title that calls GetProcAddress right at startup can still miss the hook. Say so in the docs.
  2. tests/startup_runtime_gpa_patch_check.cpp: no ordinal-import case. Mutation removing the ordinal skip survived. Add an ordinal thunk (high bit set) beside the named ones and expect no hit and no out-of-range read.
  3. patch_all_loaded_modules: fixed HMODULE modules[512]. If needed exceeds the buffer, the first 512 are patched and the rest are skipped silently. The log prints count as if all were handled. Log a truncation or loop with a bigger buffer.

Nits

  • runtime_gpa_patch.h visit_imports: continue on a descriptor with a missing field still advances; fine, but a descriptor table without a terminator relies only on the range check. OK.
  • Win32 runtime was not exercised (PR says so). The delay-import P1 needs a Windows run or a delay-load fixture before this goes Ready.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6050975426 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2166#pullrequestreview-6050975426). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T02:39:08Z. **Review:** COMMENTED --- Review of head `fbc106173da1b94496f12494a590cf63c77ee6ba`. CI: Linux checks, gfx1013 fixtures and Windows build/test pass. Mergeable. Title is neutral, no game data. Commands executed locally (Linux): g++ -std=c++17 -Wall -Wextra -Werror -Isrc -o /tmp/gpa_sm7 tests/startup_runtime_gpa_patch_check.cpp && /tmp/gpa_sm7 # -> startup_runtime_gpa_patch: ok /tmp/gpa_sm7 --mutate name-only # -> FAIL: name-only accepts QueryApiImpl from other.dll python3 tests/docs_links_check.py # -> Checked 18 Markdown files and 401 links. python3 tests/bidi_controls_check.py # -> no bidirectional controls git diff --check origin/main...HEAD # -> clean Mutations on the new logic: - Skip the delay-import directory (`dirs[13]` -> `dirs[0]`): test FAILS (3 mismatches). Killed. - Remove the ordinal-import skip in `visit_named_thunks`: test still passes. SURVIVED (see P2-2). ## P1 1. `src/startup_stubs/runtime_proxy.cpp` (`patch_module`, QueryApiImpl branch, ~line 437-443) plus `runtime_gpa_patch.h` (delay-import walk). Delay-import IAT slots do not hold the real function before the first call. They hold the delay-load thunk. The delay helper later writes the resolved address over the slot. - For a delay-imported `xgameruntime.dll!QueryApiImpl`, `real_query` can be set to the delay thunk address (when `real_query` is still null). Later `real_query(...)` calls (lines ~713, 743, 871 and `proxy_query`) then jump into the thunk, not the export. - For a delay-imported `GetProcAddress`, the slot is replaced by the hook, then the helper overwrites it at first use. The hook is lost, so the delay path the PR text claims to cover is not actually hooked. - Suggested fix: for delay imports, resolve the real export with `GetProcAddress(GetModuleHandle("xgameruntime.dll"), "QueryApiImpl")` and do not read `*slot`. Or only record `real_query` from a non-delay slot. Hook delay `GetProcAddress` through the delay-load hook (`__pfnDliNotifyHook`), or state that delay imports are not covered. - The test only checks that the walk finds the delay hit. It does not check that patching it works. ## P2 1. `runtime_proxy.cpp` `patch_imports`/`patch_all_loaded_modules`: the enumeration runs on a separate thread while `dll_loaded` runs in loader context. Both call `patch_module` and write `real_query` (plain global, no atomic or lock). The slot compare avoids double patching, but `real_query` can race. Make it `std::atomic` or set it once under a lock. Also, a module loaded between registration and enumeration is covered twice, which is fine, but a module loaded before registration and after the main-module patch is only covered by the thread, so a title that calls `GetProcAddress` right at startup can still miss the hook. Say so in the docs. 2. `tests/startup_runtime_gpa_patch_check.cpp`: no ordinal-import case. Mutation removing the ordinal skip survived. Add an ordinal thunk (high bit set) beside the named ones and expect no hit and no out-of-range read. 3. `patch_all_loaded_modules`: fixed `HMODULE modules[512]`. If `needed` exceeds the buffer, the first 512 are patched and the rest are skipped silently. The log prints `count` as if all were handled. Log a truncation or loop with a bigger buffer. ## Nits - `runtime_gpa_patch.h` `visit_imports`: `continue` on a descriptor with a missing field still advances; fine, but a descriptor table without a terminator relies only on the range check. OK. - Win32 runtime was not exercised (PR says so). The delay-import P1 needs a Windows run or a delay-load fixture before this goes Ready. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-25 03:01:15 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T02:48:22Z.

Review: COMMENTED


Re-review of head 1f1b770f211b94496f12494a590cf63c77ee6ba (previous review was on fbc10617).

CI at review time: Linux Python checks pass. Windows build/test and gfx1013 fixtures were still pending. Mergeable. Title is neutral, no game data.

Previous findings

  • P1 delay-import IAT treated as exports: RESOLVED. visit_imports now reports delay hits with delay_import=true. patch_module (runtime_proxy.cpp ~455-459) never rewrites or reads those slots. A delay QueryApiImpl seeds real_query through GetProcAddress(GetModuleHandle("xgameruntime.dll"), "QueryApiImpl"). Delay GetProcAddress stays unhooked, and the header and docs now say so.
  • P2-1 real_query race: RESOLVED. It is std::atomic with compare-exchange (first writer wins) and all readers use load_real_query(). Not re-checked: the docs note about a title calling GetProcAddress before the thread finishes.
  • P2-2 ordinal case missing: RESOLVED. The test now has an ordinal thunk whose low bits alias a hint RVA.
  • P2-3 fixed 512 module buffer: RESOLVED. It grows once, and logs if it is still truncated.

Commands executed locally

g++ -std=c++17 -Wall -Wextra -Werror -Isrc -o /tmp/gpa_sm5 tests/startup_runtime_gpa_patch_check.cpp && /tmp/gpa_sm5
# -> startup_runtime_gpa_patch: ok (11 checks ok)
/tmp/gpa_sm5 --mutate name-only   # -> FAIL as expected (exit 1)
python3 tests/docs_links_check.py       # -> Checked 18 Markdown files and 401 links.
python3 tests/bidi_controls_check.py    # -> no bidirectional controls
git diff --check origin/main...HEAD     # -> clean

Mutations (reverted with git checkout -- .):

  • Disable the ordinal skip in visit_named_thunks: test FAILS (5 mismatches). Killed. This is the mutant that survived last time.
  • Always report delay_import=false: test FAILS ("delay vs regular hits"). Killed.

P1

None.

P2

None.

Nits

  • runtime_proxy.cpp is Windows-only. It was not compiled or run here. It needs the Windows CI job (pending when I looked) to go green before Ready. The test covers the header logic only, not the VirtualProtect rewrite or the module enumeration.
  • A delay-imported GetProcAddress is still not hooked. This is documented and acceptable for this slice. A later slice could use the delay-load notify hook.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6051083992 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2166#pullrequestreview-6051083992). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T02:48:22Z. **Review:** COMMENTED --- Re-review of head `1f1b770f211b94496f12494a590cf63c77ee6ba` (previous review was on `fbc10617`). CI at review time: Linux Python checks pass. Windows build/test and gfx1013 fixtures were still pending. Mergeable. Title is neutral, no game data. ## Previous findings - P1 delay-import IAT treated as exports: RESOLVED. `visit_imports` now reports delay hits with `delay_import=true`. `patch_module` (`runtime_proxy.cpp` ~455-459) never rewrites or reads those slots. A delay `QueryApiImpl` seeds `real_query` through `GetProcAddress(GetModuleHandle("xgameruntime.dll"), "QueryApiImpl")`. Delay `GetProcAddress` stays unhooked, and the header and docs now say so. - P2-1 `real_query` race: RESOLVED. It is `std::atomic` with compare-exchange (first writer wins) and all readers use `load_real_query()`. Not re-checked: the docs note about a title calling `GetProcAddress` before the thread finishes. - P2-2 ordinal case missing: RESOLVED. The test now has an ordinal thunk whose low bits alias a hint RVA. - P2-3 fixed 512 module buffer: RESOLVED. It grows once, and logs if it is still truncated. ## Commands executed locally g++ -std=c++17 -Wall -Wextra -Werror -Isrc -o /tmp/gpa_sm5 tests/startup_runtime_gpa_patch_check.cpp && /tmp/gpa_sm5 # -> startup_runtime_gpa_patch: ok (11 checks ok) /tmp/gpa_sm5 --mutate name-only # -> FAIL as expected (exit 1) python3 tests/docs_links_check.py # -> Checked 18 Markdown files and 401 links. python3 tests/bidi_controls_check.py # -> no bidirectional controls git diff --check origin/main...HEAD # -> clean Mutations (reverted with `git checkout -- .`): - Disable the ordinal skip in `visit_named_thunks`: test FAILS (5 mismatches). Killed. This is the mutant that survived last time. - Always report `delay_import=false`: test FAILS ("delay vs regular hits"). Killed. ## P1 None. ## P2 None. ## Nits - `runtime_proxy.cpp` is Windows-only. It was not compiled or run here. It needs the Windows CI job (pending when I looked) to go green before Ready. The test covers the header logic only, not the `VirtualProtect` rewrite or the module enumeration. - A delay-imported `GetProcAddress` is still not hooked. This is documented and acceptable for this slice. A later slice could use the delay-load notify hook. AI-assisted; commands listed were executed locally.
R0Y00 commented 2026-11-25 03:01:15 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T02:56:42Z.

Review: CHANGES_REQUESTED


I would hold this revision on two points.

  • The load callback calls patch_module, which calls VirtualProtect and can call GetModuleHandleW / GetProcAddress on the delay-import path. For a newly loaded helper it also treats the regular IAT as already bound. Windows documents that this notification is pre-link and that calling into another module from it is unsafe. Please move the work to a point after linking and outside the notification, with the module held live while its slots are used. Windows loader callback contract
  • Please scope interception to the EXE and explicit package helpers and verify the provider module as well as the symbol for dynamic QueryApiImpl. The current hook can wrap an unrelated DLL's same-named export and let it become the first cached provider address. The synthetic import-table walk covers a direct-import collision only. A source-built EXE and helper should exercise a late-loaded helper through the actual loader, verify the intended call is observed, and show an unrelated same-name export still reaches its original target. The zero-provider trace is an observation; which module and lookup path produced it remains open.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6051182008 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2166#pullrequestreview-6051182008). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-25T02:56:42Z. **Review:** CHANGES_REQUESTED --- I would hold this revision on two points. - The load callback calls `patch_module`, which calls `VirtualProtect` and can call `GetModuleHandleW` / `GetProcAddress` on the delay-import path. For a newly loaded helper it also treats the regular IAT as already bound. Windows documents that this notification is pre-link and that calling into another module from it is unsafe. Please move the work to a point after linking and outside the notification, with the module held live while its slots are used. [Windows loader callback contract](https://learn.microsoft.com/en-us/windows/win32/devnotes/ldrdllnotification) - Please scope interception to the EXE and explicit package helpers and verify the provider module as well as the symbol for dynamic `QueryApiImpl`. The current hook can wrap an unrelated DLL's same-named export and let it become the first cached provider address. The synthetic import-table walk covers a direct-import collision only. A source-built EXE and helper should exercise a late-loaded helper through the actual loader, verify the intended call is observed, and show an unrelated same-name export still reaches its original target. The zero-provider trace is an observation; which module and lookup path produced it remains open.
SimSalabimse commented 2026-11-25 03:01:23 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by SimSalabimse on 2026-11-25T02:46:18Z.


Addressed @tedin7’s review on head 1f1b770f (follow-up commit on this branch).

P1 — delay-import

  • Hit now carries delay_import.
  • Delay IAT slots are not rewritten (they hold a delay-load thunk until first use).
  • Delay xgameruntime.dll!QueryApiImpl only seeds real_query via GetProcAddress(GetModuleHandleW(L"xgameruntime.dll"), "QueryApiImpl") (this stand-in’s own GPA IAT stays unpatched).
  • Delay-imported GetProcAddress is not hooked; documented in the file header and the X2W_RUNTIME_PROXY row in docs/startup-runtime.md.

P2

  1. real_query is std::atomic with compare-exchange first-writer; callers use load_real_query().
  2. Synthetic image includes an ordinal thunk whose low bits alias the GetProcAddress hint RVA — removing the ordinal skip makes the test fail (confirmed locally).
  3. EnumProcessModules: grow the buffer when truncated and log; if still truncated after resize, log and patch the first N.
  4. Docs note the attach→enum-thread window where an early GetProcAddress can miss the hook.

Verification (Linux)

g++ -std=c++17 -Wall -Wextra -Werror -Isrc -o /tmp/gpa_patch_check tests/startup_runtime_gpa_patch_check.cpp
/tmp/gpa_patch_check   # -> startup_runtime_gpa_patch: ok
/tmp/gpa_patch_check --mutate name-only   # -> FAIL: name-only …
# ordinal-skip mutation: FAIL (5)
python3 -B tests/docs_links_check.py
python3 -B tests/bidi_controls_check.py

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7151165813 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2166#issuecomment-7151165813). Originally posted by [SimSalabimse](https://github.com/SimSalabimse) on 2026-11-25T02:46:18Z. --- Addressed @tedin7’s review on head `1f1b770f` (follow-up commit on this branch). ### P1 — delay-import - `Hit` now carries `delay_import`. - **Delay IAT slots are not rewritten** (they hold a delay-load thunk until first use). - Delay `xgameruntime.dll!QueryApiImpl` only seeds `real_query` via `GetProcAddress(GetModuleHandleW(L"xgameruntime.dll"), "QueryApiImpl")` (this stand-in’s own GPA IAT stays unpatched). - Delay-imported `GetProcAddress` is **not** hooked; documented in the file header and the `X2W_RUNTIME_PROXY` row in `docs/startup-runtime.md`. ### P2 1. `real_query` is `std::atomic` with compare-exchange first-writer; callers use `load_real_query()`. 2. Synthetic image includes an ordinal thunk whose low bits alias the `GetProcAddress` hint RVA — removing the ordinal skip makes the test fail (confirmed locally). 3. `EnumProcessModules`: grow the buffer when truncated and log; if still truncated after resize, log and patch the first N. 4. Docs note the attach→enum-thread window where an early `GetProcAddress` can miss the hook. ### Verification (Linux) ```bash g++ -std=c++17 -Wall -Wextra -Werror -Isrc -o /tmp/gpa_patch_check tests/startup_runtime_gpa_patch_check.cpp /tmp/gpa_patch_check # -> startup_runtime_gpa_patch: ok /tmp/gpa_patch_check --mutate name-only # -> FAIL: name-only … # ordinal-skip mutation: FAIL (5) python3 -B tests/docs_links_check.py python3 -B tests/bidi_controls_check.py ``` AI-assisted.
upstream-sync closed this pull request 2026-11-25 04:04:01 +00:00
R0Y00 commented 2026-11-25 04:04:02 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T03:09:44Z.

Review: CHANGES_REQUESTED


The new loader fixture and moving Win32 calls out of the notification address the first review. Two lifetime issues remain in this head.

  • The callback always writes modulo the ring size even if the consumer has not read the slot. If notifications lap the worker, the producer can write the same non-atomic slot concurrently with the read. The wrap log runs afterward and does not prevent this. Use bounded publication that never overwrites unread entries, or coalesce notifications into a post-link rescan request. Add an overflow or burst control.
  • The registered callback, polling thread and patched IAT entries refer to the shim indefinitely. If the shim is unloaded, code addresses may outlive the DLL. Give the enabled proxy an explicit lifetime covering all three, or handle teardown and outstanding users. Close the thread handle if it is not kept for coordination, and test the supported lifetime with the source-built carrier. The helper test currently retries until patching; it establishes eventual interception, so keep that distinction for the first-call startup case.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6051283564 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2166#pullrequestreview-6051283564). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-25T03:09:44Z. **Review:** CHANGES_REQUESTED --- The new loader fixture and moving Win32 calls out of the notification address the first review. Two lifetime issues remain in this head. - The callback always writes modulo the ring size even if the consumer has not read the slot. If notifications lap the worker, the producer can write the same non-atomic slot concurrently with the read. The wrap log runs afterward and does not prevent this. Use bounded publication that never overwrites unread entries, or coalesce notifications into a post-link rescan request. Add an overflow or burst control. - The registered callback, polling thread and patched IAT entries refer to the shim indefinitely. If the shim is unloaded, code addresses may outlive the DLL. Give the enabled proxy an explicit lifetime covering all three, or handle teardown and outstanding users. Close the thread handle if it is not kept for coordination, and test the supported lifetime with the source-built carrier. The helper test currently retries until patching; it establishes eventual interception, so keep that distinction for the first-call startup case.
tedin7 commented 2026-11-25 04:04:02 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T03:11:57Z.

Review: COMMENTED


Re-review of head efaaaebb402c26e932b61300fb061976380da24c (previous review was on 1f1b770f; the branch was rewritten, now 3 commits).

CI at review time: Linux Python checks pass, Windows build/test pass, gfx1013 fixtures pass. /O2 job skipped. Mergeable, not Draft. Title is neutral, no game data.

What changed since the last review

  • Load patches are deferred. dll_loaded (LdrDllNotification) only enqueues the base into a static ring (runtime_proxy.cpp ~540-555). A worker thread drains it, pins the module with GetModuleHandleExW, rewrites, then drops the pin. This matches the rule that the pre-link callback must not call other modules.
  • Scope is the main EXE plus DLLs in the EXE's directory (module_is_exe_or_package_helper). System modules are out.
  • Dynamic QueryApiImpl is wrapped only when the provider basename is xgameruntime.dll.
  • The proxy's own GetProcAddress IAT is left alone (self check), so the hook cannot recurse.
  • New Windows loader test (tests/startup_runtime_proxy_scope/*) passes in CI.

Earlier findings (delay-import IAT, real_query race, ordinal case, fixed module buffer) remain resolved.

Commands executed locally

g++ -std=c++17 -Wall -Wextra -Werror -Isrc -o /tmp/gpa_sm5b tests/startup_runtime_gpa_patch_check.cpp && /tmp/gpa_sm5b
# -> startup_runtime_gpa_patch: ok
/tmp/gpa_sm5b --mutate name-only   # -> "FAIL: name-only accepts QueryApiImpl from other.dll" (expected)
python3 tests/docs_links_check.py       # -> Checked 18 Markdown files and 404 links.
python3 tests/bidi_controls_check.py    # -> no bidirectional controls
git diff --check origin/main...HEAD     # -> clean

Mutation (reverted with git checkout -- .): remove the ordinal skip at runtime_gpa_patch.h:128. Test FAILS. Killed.

Not run locally: runtime_proxy.cpp and the loader-scope test are Windows-only. I rely on the green Windows CI job for them.

P1

None.

P2

None.

Nits

  • Ring overflow: if more than 256 loads queue before the worker drains, dll_loaded overwrites unread slots. The wrap log in drain_pending_loads fires only when published - read > pending_cap, and the worker already lost those bases. The 25 ms poll makes this unlikely. A fallback rescan of EnumProcessModules on that log line would close it.
  • A base address queued, unloaded, then reused by another module is patched only if it passes the package-directory check. Harmless today.
  • The worker thread never exits and is never joined. Fine for a process-lifetime stand-in.
  • Delay-imported GetProcAddress is still unhooked (documented).

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6051298891 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2166#pullrequestreview-6051298891). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T03:11:57Z. **Review:** COMMENTED --- Re-review of head `efaaaebb402c26e932b61300fb061976380da24c` (previous review was on `1f1b770f`; the branch was rewritten, now 3 commits). CI at review time: Linux Python checks pass, Windows build/test pass, gfx1013 fixtures pass. `/O2` job skipped. Mergeable, not Draft. Title is neutral, no game data. ## What changed since the last review - Load patches are deferred. `dll_loaded` (LdrDllNotification) only enqueues the base into a static ring (`runtime_proxy.cpp` ~540-555). A worker thread drains it, pins the module with `GetModuleHandleExW`, rewrites, then drops the pin. This matches the rule that the pre-link callback must not call other modules. - Scope is the main EXE plus DLLs in the EXE's directory (`module_is_exe_or_package_helper`). System modules are out. - Dynamic `QueryApiImpl` is wrapped only when the provider basename is `xgameruntime.dll`. - The proxy's own `GetProcAddress` IAT is left alone (`self` check), so the hook cannot recurse. - New Windows loader test (`tests/startup_runtime_proxy_scope/*`) passes in CI. Earlier findings (delay-import IAT, `real_query` race, ordinal case, fixed module buffer) remain resolved. ## Commands executed locally g++ -std=c++17 -Wall -Wextra -Werror -Isrc -o /tmp/gpa_sm5b tests/startup_runtime_gpa_patch_check.cpp && /tmp/gpa_sm5b # -> startup_runtime_gpa_patch: ok /tmp/gpa_sm5b --mutate name-only # -> "FAIL: name-only accepts QueryApiImpl from other.dll" (expected) python3 tests/docs_links_check.py # -> Checked 18 Markdown files and 404 links. python3 tests/bidi_controls_check.py # -> no bidirectional controls git diff --check origin/main...HEAD # -> clean Mutation (reverted with `git checkout -- .`): remove the ordinal skip at `runtime_gpa_patch.h:128`. Test FAILS. Killed. Not run locally: `runtime_proxy.cpp` and the loader-scope test are Windows-only. I rely on the green Windows CI job for them. ## P1 None. ## P2 None. ## Nits - Ring overflow: if more than 256 loads queue before the worker drains, `dll_loaded` overwrites unread slots. The wrap log in `drain_pending_loads` fires only when `published - read > pending_cap`, and the worker already lost those bases. The 25 ms poll makes this unlikely. A fallback rescan of `EnumProcessModules` on that log line would close it. - A base address queued, unloaded, then reused by another module is patched only if it passes the package-directory check. Harmless today. - The worker thread never exits and is never joined. Fine for a process-lifetime stand-in. - Delay-imported `GetProcAddress` is still unhooked (documented). AI-assisted; commands listed were executed locally.
R0Y00 commented 2026-11-25 04:04:02 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T03:22:48Z.

Review: APPROVED


The revised source-built carrier test verifies the post-link helper and provider lifetime on this head. The coalesced rescan has no per-notification buffer, and the build collision is gone. Application first-call coverage remains unverified.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6051385298 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2166#pullrequestreview-6051385298). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-25T03:22:48Z. **Review:** APPROVED --- The revised source-built carrier test verifies the post-link helper and provider lifetime on this head. The coalesced rescan has no per-notification buffer, and the build collision is gone. Application first-call coverage remains unverified.
tedin7 commented 2026-11-25 04:04:02 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T03:26:28Z.

Review: COMMENTED


Re-review of head 4b1853f9be8f1e78eb7ac2e9ae878219e1b626f0 (delta since efaaaebb: "Coalesce runtime loader notifications and keep proxy targets live").

Scope checked: src/startup_stubs/runtime_proxy.cpp, tests/startup_runtime_proxy_scope/host.cpp, docs/startup-runtime.md, CMake wiring.

Commands executed locally (Linux):

  • git diff --stat origin/main...HEAD -> 14 files, +857/-40
  • git diff --check origin/main...HEAD -> clean
  • python3 -B tests/docs_links_check.py -> Checked 18 Markdown files and 404 links.
  • python3 -B tests/bidi_controls_check.py -> no bidirectional controls
  • gh pr checks 2166 -> Windows build+test, Linux Python checks, gfx1013 fixtures: pass; /O2 job skipping.

What looks right

  • The pre-link notification now only sets an atomic<bool> (lock-free, static_asserted). It calls nothing in other modules and no longer has a fixed-size ring that could overwrite pending work. A burst of loads collapses to one rescan.
  • The worker clears the flag before scanning, so loads that arrive mid-scan trigger another pass. The scan's GetModuleHandleExW waits on the loader lock, so a module is not touched before it is linked.
  • remember_real_query / keep_forward_target pin the forward target before a proxy is handed out. If a different real target was already cached, the hook now returns the original pointer instead of a proxy that would forward to the wrong provider. The same pin guard covers the InitializeApiImpl* wrappers.
  • GetModuleFileNameW truncation (length >= MAX_PATH) is now treated as failure in all three call sites, so a truncated path cannot match the basename or directory gate.
  • New host assertions (release caller's carrier/runtime reference, module still loaded) cover the "stay live until process exit" claim.

P1: none.

P2: none.

Nits

  1. runtime_proxy.cpp worker loop (patch_imports): polls Sleep(25) forever even when the flag is idle. Harmless, but an event/WaitOnAddress would avoid a permanent wakeup every 25 ms. Not blocking.
  2. keep_forward_target under X2W_RUNTIME_PROXY_FIXTURE (runtime_proxy.cpp:64) returns target != nullptr without pinning, so the "target stays live" guarantee is only exercised by the scope fixture (real code path), not by startup_runtime_local. Fine for stub targets; worth a comment.
  3. When needed stays above the grown buffer in patch_package_modules, later modules are silently skipped (a log line is emitted). Acceptable given the log.

Not done / limits

  • Mutation test: not run. The changed code is MSVC/Win32-only and the Linux job cannot build it, so I could not observe a test failing on a mutated line. The new host.cpp assertions are the guard I would mutate (drop the PIN flag in keep_forward_target) on Windows.
  • No Win2025 run from me: the change is runtime-proxy C++ with no shader/GPU path, and Windows CI is green on this head.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6051412740 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2166#pullrequestreview-6051412740). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T03:26:28Z. **Review:** COMMENTED --- Re-review of head `4b1853f9be8f1e78eb7ac2e9ae878219e1b626f0` (delta since `efaaaebb`: "Coalesce runtime loader notifications and keep proxy targets live"). **Scope checked:** `src/startup_stubs/runtime_proxy.cpp`, `tests/startup_runtime_proxy_scope/host.cpp`, `docs/startup-runtime.md`, CMake wiring. **Commands executed locally (Linux):** - `git diff --stat origin/main...HEAD` -> 14 files, +857/-40 - `git diff --check origin/main...HEAD` -> clean - `python3 -B tests/docs_links_check.py` -> `Checked 18 Markdown files and 404 links.` - `python3 -B tests/bidi_controls_check.py` -> `no bidirectional controls` - `gh pr checks 2166` -> Windows build+test, Linux Python checks, gfx1013 fixtures: pass; /O2 job skipping. **What looks right** - The pre-link notification now only sets an `atomic<bool>` (lock-free, `static_assert`ed). It calls nothing in other modules and no longer has a fixed-size ring that could overwrite pending work. A burst of loads collapses to one rescan. - The worker clears the flag before scanning, so loads that arrive mid-scan trigger another pass. The scan's `GetModuleHandleExW` waits on the loader lock, so a module is not touched before it is linked. - `remember_real_query` / `keep_forward_target` pin the forward target before a proxy is handed out. If a different real target was already cached, the hook now returns the original pointer instead of a proxy that would forward to the wrong provider. The same pin guard covers the `InitializeApiImpl*` wrappers. - `GetModuleFileNameW` truncation (`length >= MAX_PATH`) is now treated as failure in all three call sites, so a truncated path cannot match the basename or directory gate. - New host assertions (release caller's carrier/runtime reference, module still loaded) cover the "stay live until process exit" claim. **P1:** none. **P2:** none. **Nits** 1. `runtime_proxy.cpp` worker loop (`patch_imports`): polls `Sleep(25)` forever even when the flag is idle. Harmless, but an event/`WaitOnAddress` would avoid a permanent wakeup every 25 ms. Not blocking. 2. `keep_forward_target` under `X2W_RUNTIME_PROXY_FIXTURE` (`runtime_proxy.cpp:64`) returns `target != nullptr` without pinning, so the "target stays live" guarantee is only exercised by the scope fixture (real code path), not by `startup_runtime_local`. Fine for stub targets; worth a comment. 3. When `needed` stays above the grown buffer in `patch_package_modules`, later modules are silently skipped (a log line is emitted). Acceptable given the log. **Not done / limits** - Mutation test: not run. The changed code is MSVC/Win32-only and the Linux job cannot build it, so I could not observe a test failing on a mutated line. The new `host.cpp` assertions are the guard I would mutate (drop the `PIN` flag in `keep_forward_target`) on Windows. - No Win2025 run from me: the change is runtime-proxy C++ with no shader/GPU path, and Windows CI is green on this head. AI-assisted; commands listed were executed locally.
SimSalabimse commented 2026-11-25 04:04:17 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by SimSalabimse on 2026-11-25T03:07:13Z.


Addressed @R0Y00 CHANGES_REQUESTED (review 6051182008) on head efaaaebb.

  • The load callback only stores DllBase in a static ring and advances a published counter. No VirtualProtect, GetModuleHandle, GetProcAddress, logging, or other cross-module calls (loader callback contract).
  • A worker (outside the notification / after attach) pins the module with GetModuleHandleEx(FROM_ADDRESS), rewrites regular IAT slots, then FreeLibrarys the pin so the image stays live for the rewrite.
  • Initial package scan runs once on that worker; later loads are drained from the queue after linking has finished (LoadLibrary has returned on the loading thread before our drain observes a usable pin).

2. Scope + provider module gate + loader fixture

  • IAT rewrite is limited to the main EXE and DLLs whose path sits in the EXE directory (package helpers). System / injector modules are skipped.
  • Dynamic QueryApiImpl / InitializeApiImpl* are wrapped only when the provider module basename is xgameruntime.dll (an unrelated same-named export is returned unchanged and cannot become the first cached provider).
  • New Windows test startup_runtime_proxy_scope: source-built EXE + carrier + fake xgameruntime.dll + decoy + late-loaded helper through the real loader; asserts the helper sees the proxied xgameruntime entry and the decoy’s QueryApiImpl still hits the original. (X2W_RUNTIME_PROXY_FIXTURE keeps startup_runtime_local’s stubbed GPA path working.)

Verification

g++ -std=c++17 -Wall -Wextra -Werror -Isrc -o /tmp/gpa_patch_check tests/startup_runtime_gpa_patch_check.cpp && /tmp/gpa_patch_check
# -> startup_runtime_gpa_patch: ok
python3 -B tests/docs_links_check.py
python3 -B tests/bidi_controls_check.py

Windows CI / local: ctest --preset dev --no-tests=error -R '^startup_runtime_proxy_scope$' (and the existing startup_runtime_gpa_patch* / startup_runtime_local* checks).

Zero-provider account-picker traces remain observation only; this change does not claim that path is identified.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7151608259 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2166#issuecomment-7151608259). Originally posted by [SimSalabimse](https://github.com/SimSalabimse) on 2026-11-25T03:07:13Z. --- Addressed @R0Y00 CHANGES_REQUESTED (review 6051182008) on head `efaaaebb`. ### 1. LdrDllNotification (pre-link / no cross-module calls) - The load callback **only** stores `DllBase` in a static ring and advances a published counter. No `VirtualProtect`, `GetModuleHandle`, `GetProcAddress`, logging, or other cross-module calls ([loader callback contract](https://learn.microsoft.com/en-us/windows/win32/devnotes/ldrdllnotification)). - A worker (outside the notification / after attach) **pins** the module with `GetModuleHandleEx(FROM_ADDRESS)`, rewrites regular IAT slots, then `FreeLibrary`s the pin so the image stays live for the rewrite. - Initial package scan runs once on that worker; later loads are drained from the queue after linking has finished (LoadLibrary has returned on the loading thread before our drain observes a usable pin). ### 2. Scope + provider module gate + loader fixture - IAT rewrite is limited to the **main EXE** and DLLs whose path sits in the **EXE directory** (package helpers). System / injector modules are skipped. - Dynamic `QueryApiImpl` / `InitializeApiImpl*` are wrapped only when the provider module basename is `xgameruntime.dll` (an unrelated same-named export is returned unchanged and cannot become the first cached provider). - New Windows test `startup_runtime_proxy_scope`: source-built EXE + carrier + fake `xgameruntime.dll` + decoy + late-loaded helper through the real loader; asserts the helper sees the proxied xgameruntime entry and the decoy’s `QueryApiImpl` still hits the original. (`X2W_RUNTIME_PROXY_FIXTURE` keeps `startup_runtime_local`’s stubbed GPA path working.) ### Verification ```bash g++ -std=c++17 -Wall -Wextra -Werror -Isrc -o /tmp/gpa_patch_check tests/startup_runtime_gpa_patch_check.cpp && /tmp/gpa_patch_check # -> startup_runtime_gpa_patch: ok python3 -B tests/docs_links_check.py python3 -B tests/bidi_controls_check.py ``` Windows CI / local: `ctest --preset dev --no-tests=error -R '^startup_runtime_proxy_scope$'` (and the existing `startup_runtime_gpa_patch*` / `startup_runtime_local*` checks). Zero-provider account-picker traces remain observation only; this change does not claim that path is identified. AI-assisted.
R0Y00 commented 2026-11-25 04:04:18 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T03:21:55Z.


The native build of efaaaebb failed because the load ring's pending_read collided with the save-read function already in the translation unit. This revision replaces the ring with a coalesced rescan flag and keeps the enabled proxy and its cached forwarding target live. The source-built loader fixture drops both caller references before making the calls, and a same-name decoy remains untouched. Release passed all five runtime checks and 30 consecutive runs of the loader fixture. The worker still provides eventual interception and first-call application coverage remains open.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7151845557 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2166#issuecomment-7151845557). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-25T03:21:55Z. --- The native build of efaaaebb failed because the load ring's pending_read collided with the save-read function already in the translation unit. This revision replaces the ring with a coalesced rescan flag and keeps the enabled proxy and its cached forwarding target live. The source-built loader fixture drops both caller references before making the calls, and a same-name decoy remains untouched. Release passed all five runtime checks and 30 consecutive runs of the loader fixture. The worker still provides eventual interception and first-call application coverage remains open.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!2158
No description provided.