runtime: trampoline xgameruntime!QueryApiImpl for pre-attach GPA caches #3880

Open
github-bot wants to merge 1 commit from github-bot/gh-3878 into main AGit
Owner

@⁠Ammmae0x on GitHub · 2026-12-02 17:37 UTC · original
Ammmae0x:runtime/queryapiimpl-trampoline → main · +888 −20 · 7 files · 1 commits · draft

Refs #2350, #870, #2349

A QueryApiImpl pointer cached via GetProcAddress before the proxy attaches keeps pointing at the export, so IAT/GPA rewrites never reach it and FakeUser / X2W_TRACE_API never see providers on that path. This change trampolines xgameruntime.dll!QueryApiImpl at the export itself once the real export is known.

What landed (single commit, 7 files, +888/-20):

  • src/startup_stubs/runtime_gpa_patch.h — pure x64 prologue length decoder and relocator (no Windows calls), so the source-built check tests it directly: whole-instruction boundaries for a 12-byte absolute patch (mov rax, imm64; jmp rax) and a 14-byte indirect patch (jmp [rip+0] + slot), RIP-relative disp32 and relative-branch fixups, and refusal (zero bytes written) for a prologue that is too short, contains RET/UD2/VEX/unknown opcodes, or has a relative branch / RIP-relative access targeting the stolen bytes themselves. A resume stub more than 2 GiB away is refused when the adjusted displacement no longer fits.
  • src/startup_stubs/runtime_proxy.cpp — once real_query is learned (via export, gpa or iat), the export is trampolined once, idempotently (trampoline_state + wait-for-settle in remember_real_query). The resume stub (relocated prologue + jump back to real + stolen, allocated near the export so RIP-relative fixups fit, VirtualAlloc + FlushInstructionCache) is allocated and published as forward_query before the export entry is patched, so proxy_query (and the XAsync helpers, now also on forward_query) never recurse into their own trampoline. real_query keeps the original export address, so bound delay-IAT comparisons and logging still work. The install is refused (logged, export left unpatched) when the export's module is not xgameruntime.dll — a forwarded export and the decoy's same-named export both fail that guard — or when the prologue cannot be relocated safely. Protection is restored and the instruction cache flushed. No-op in the source-built fixture (X2W_RUNTIME_PROXY_FIXTURE).
  • tests/startup_runtime_proxy_scope/ — the fake xgameruntime now answers one test provider/IID pair with a fake object (query_object / object_calls exports). host.cpp verifies the cached pointer keeps its original numerical address and calling it after attach goes through proxy_query (returns a wrapper, not the real object; a method through the wrapper reaches the real object; the real export runs once), plus that decoy.dll!QueryApiImpl stays unpatched (answers its own 0xA11C0001, never enters the proxy). No new test targets.
  • tests/startup_runtime_gpa_patch_check.cpp — unit tests for instruction lengths, 12/14-byte plans and relocation (including a load-bearing unfixed-copy control), plus refusal cases; --mutate name-only is unchanged, and --mutate prologue-reloc (unregistered manual control) also prints FAIL: with exit 1.
  • docs/startup-runtime.md — one sentence on the X2W_RUNTIME_PROXY=1 row describing the export trampoline and its limits.

Linux verification (this host has no mingw/MSVC, so no Windows compile check):

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 (all checks, incl. 18 length, 17 plan and 11 relocation checks)
/tmp/gpa_patch_check --mutate name-only
# -> FAIL: name-only accepts QueryApiImpl from other.dll (exit 1)
/tmp/gpa_patch_check --mutate prologue-reloc
# -> FAIL: prologue-reloc copies a RIP-relative displacement without fixing it (exit 1)
./out/pe-env/bin/python -B tests/form_coverage_check.py
# -> form coverage checks passed
./out/pe-env/bin/python -B tests/docs_links_check.py
# -> Self-test passed. Checked 251 Markdown files and 954 links.
./out/pe-env/bin/python -B tests/bidi_controls_check.py
# -> Self-test passed. Checked 2996 tracked source, script and build/CI files: no bidirectional controls.
git diff --check origin/main...HEAD
# -> clean
git merge-tree --write-tree origin/main HEAD
# -> exits 0

Note: form_coverage_check.py needs iced_x86 in the venv (out/pe-env, pip install iced-x86); the system python3 lacks it and fails with ModuleNotFoundError before any check runs. That is environmental, not this change.

Caveats: the Windows-only code (runtime_proxy.cpp trampoline install, startup_runtime_proxy_scope) could not be compiled or run on this Linux host. The fixture's fake export prologue is covered only by the decoder's instruction set (register saves, stack adjustment, RIP-relative LEA / indirect call, relative calls/jumps); a real xgameruntime.dll prologue outside that set is refused safely and logged, which the Windows run will show. WIN-VERIFY is the arbiter.

WIN-VERIFY: pending | sha=23848272803f463784eab7065124aecd44d509d1 | targets=startup_runtime_proxy_scope startup_runtime_gpa_patch | ctest=^(startup_runtime_proxy_scope|startup_runtime_gpa_patch(_name_only_control)?)$ | release=true

**@⁠Ammmae0x** on GitHub · 2026-12-02 17:37 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/3878) `Ammmae0x:runtime/queryapiimpl-trampoline` → `main` · +888 −20 · 7 files · 1 commits · draft Refs #2350, #870, #2349 A `QueryApiImpl` pointer cached via `GetProcAddress` before the proxy attaches keeps pointing at the export, so IAT/GPA rewrites never reach it and FakeUser / `X2W_TRACE_API` never see providers on that path. This change trampolines `xgameruntime.dll!QueryApiImpl` at the export itself once the real export is known. What landed (single commit, 7 files, +888/-20): - `src/startup_stubs/runtime_gpa_patch.h` — pure x64 prologue length decoder and relocator (no Windows calls), so the source-built check tests it directly: whole-instruction boundaries for a 12-byte absolute patch (`mov rax, imm64; jmp rax`) and a 14-byte indirect patch (`jmp [rip+0]` + slot), RIP-relative `disp32` and relative-branch fixups, and refusal (zero bytes written) for a prologue that is too short, contains RET/UD2/VEX/unknown opcodes, or has a relative branch / RIP-relative access targeting the stolen bytes themselves. A resume stub more than 2 GiB away is refused when the adjusted displacement no longer fits. - `src/startup_stubs/runtime_proxy.cpp` — once `real_query` is learned (via `export`, `gpa` or `iat`), the export is trampolined once, idempotently (`trampoline_state` + wait-for-settle in `remember_real_query`). The resume stub (relocated prologue + jump back to `real + stolen`, allocated near the export so RIP-relative fixups fit, `VirtualAlloc` + `FlushInstructionCache`) is allocated and published as `forward_query` *before* the export entry is patched, so `proxy_query` (and the XAsync helpers, now also on `forward_query`) never recurse into their own trampoline. `real_query` keeps the original export address, so bound delay-IAT comparisons and logging still work. The install is refused (logged, export left unpatched) when the export's module is not `xgameruntime.dll` — a forwarded export and the decoy's same-named export both fail that guard — or when the prologue cannot be relocated safely. Protection is restored and the instruction cache flushed. No-op in the source-built fixture (`X2W_RUNTIME_PROXY_FIXTURE`). - `tests/startup_runtime_proxy_scope/` — the fake `xgameruntime` now answers one test provider/IID pair with a fake object (`query_object` / `object_calls` exports). `host.cpp` verifies the cached pointer keeps its original numerical address *and* calling it after attach goes through `proxy_query` (returns a wrapper, not the real object; a method through the wrapper reaches the real object; the real export runs once), plus that `decoy.dll!QueryApiImpl` stays unpatched (answers its own `0xA11C0001`, never enters the proxy). No new test targets. - `tests/startup_runtime_gpa_patch_check.cpp` — unit tests for instruction lengths, 12/14-byte plans and relocation (including a load-bearing unfixed-copy control), plus refusal cases; `--mutate name-only` is unchanged, and `--mutate prologue-reloc` (unregistered manual control) also prints `FAIL:` with exit 1. - `docs/startup-runtime.md` — one sentence on the `X2W_RUNTIME_PROXY=1` row describing the export trampoline and its limits. Linux verification (this host has no mingw/MSVC, so no Windows compile check): ```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 (all checks, incl. 18 length, 17 plan and 11 relocation checks) /tmp/gpa_patch_check --mutate name-only # -> FAIL: name-only accepts QueryApiImpl from other.dll (exit 1) /tmp/gpa_patch_check --mutate prologue-reloc # -> FAIL: prologue-reloc copies a RIP-relative displacement without fixing it (exit 1) ./out/pe-env/bin/python -B tests/form_coverage_check.py # -> form coverage checks passed ./out/pe-env/bin/python -B tests/docs_links_check.py # -> Self-test passed. Checked 251 Markdown files and 954 links. ./out/pe-env/bin/python -B tests/bidi_controls_check.py # -> Self-test passed. Checked 2996 tracked source, script and build/CI files: no bidirectional controls. git diff --check origin/main...HEAD # -> clean git merge-tree --write-tree origin/main HEAD # -> exits 0 ``` Note: `form_coverage_check.py` needs `iced_x86` in the venv (`out/pe-env`, `pip install iced-x86`); the system `python3` lacks it and fails with `ModuleNotFoundError` before any check runs. That is environmental, not this change. Caveats: the Windows-only code (`runtime_proxy.cpp` trampoline install, `startup_runtime_proxy_scope`) could not be compiled or run on this Linux host. The fixture's fake export prologue is covered only by the decoder's instruction set (register saves, stack adjustment, RIP-relative LEA / indirect call, relative calls/jumps); a real `xgameruntime.dll` prologue outside that set is refused safely and logged, which the Windows run will show. `WIN-VERIFY` is the arbiter. WIN-VERIFY: pending | sha=23848272803f463784eab7065124aecd44d509d1 | targets=startup_runtime_proxy_scope startup_runtime_gpa_patch | ctest=^(startup_runtime_proxy_scope|startup_runtime_gpa_patch(_name_only_control)?)$ | release=true
github-bot locked as GitHub sync and limited conversation to collaborators 2026-12-02 19:11:29 +00:00
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3880/head:github-bot/gh-3878'
git switch 'github-bot/gh-3878'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3878'
git switch 'github-bot/gh-3878'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3878'
git switch 'github-bot/gh-3878'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3878'
git switch 'main'
git merge --squash 'github-bot/gh-3878'
git switch 'main'
git merge --ff-only 'github-bot/gh-3878'
git switch 'main'
git merge 'github-bot/gh-3878'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.