Repository navigation
runtime: rewrite bound delay QueryApiImpl and always log it - #2349
SimSalabimse wants to merge 7 commits into
Conversation
Account-picker TRACE_API still shows zero providers after the scoped package
GPA patch. Delay-imported QueryApiImpl binds through the CRT helper (unhooked
GetProcAddress), so the title keeps calling the real export. Rewrite that slot
only after it equals the remembered export; leave unbound thunks alone. Always
log GetProcAddress("QueryApiImpl") past the generic 40-line GPA cap, and record
how the real export was first remembered.
Synthetic PE control covers bound vs unbound delay slots; the Windows scope
fixture delay-loads QueryApiImpl with unbound and pre-attach-cache negatives.
Refs R0Y00#870.
Co-authored-by: SimSalabimse <SimSalabimse@users.noreply.github.com>
tedin7
left a comment
There was a problem hiding this comment.
Review of head 95f0f67562aaff1610d639b55dd0a603928ec4f5 (comment only).
Scope read: runtime_gpa_patch.h (new delay_query_slot_is_bound), runtime_proxy.cpp (delay-slot rewrite, always-log QueryApiImpl GPA, periodic quiet rescan), CMakeLists.txt (helper now delay-loads the fake xgameruntime), the gpa-patch check and the proxy-scope host/helper.
Commands executed locally (Linux):
git diff --stat origin/main...HEAD-> 8 files, +212/-53git diff --check origin/main...HEAD-> cleanpython3 tests/docs_links_check.py-> self-test passed, 19 files / 453 links okpython3 tests/bidi_controls_check.py-> 1630 files, no bidi controlsg++ -std=c++17 -Isrc tests/startup_runtime_gpa_patch_check.cpp+ run ->startup_runtime_gpa_patch: ok;--mutate name-only->FAIL: name-only accepts QueryApiImpl from other.dll(control fails as intended)- Mutation: dropped
&& slot_value == real_queryfromdelay_query_slot_is_bound->synthetic delay QueryApiImpl still unbound: MISMATCH,startup_runtime_gpa_patch: FAIL (2). The new test kills it. Reverted withgit checkout -- .. - CI at this head: Windows build+test pass, Linux Python checks pass, gfx1013 fixtures pass. No separate Windows VM run from me; the new Windows-loader test (
startup_runtime_proxy_scope) is covered by the passing CI Windows job.
Findings
- P1: none.
- P2: none.
- nit (
runtime_proxy.cpp, delay branch ofpatch_module):remember_real_query_from_export()runs on every delay hit on every quiet scan (every ~250 ms per module). It is cheap oncereal_queryis set, but an earlyif (load_real_query())skip would avoid a repeatedGetModuleHandleW+GetProcAddress. - nit: the periodic scan relies on
slot == real exportto detect the bind. A package helper that caches the export elsewhere before the first scan is covered by the negative control (pre-attach cached pointer), so behavior is as documented; worth keeping that control. - nit: the quiet pass logs only when a slot was rewritten, which is a good choice. The first-call status line (
log_query_status_once) is a useful diagnostic.
Semantics check: unbound delay thunks are never touched (value != real export). Only an exact match to the remembered real export is rewritten, with VirtualProtect around the 8-byte aligned store. The delay-GetProcAddress path stays unhooked, as before. Clean-room and title are neutral. PR is mergeable.
AI-assisted; commands listed were executed locally.
R0Y00
left a comment
There was a problem hiding this comment.
The independent Windows run passed the three GPA and loader-scope tests on 95f0f675. The background delay-slot rewrite still needs to preserve a value changed by the delay loader after the eligibility check. It reads the real export, calls VirtualProtect, and then writes the proxy for any value other than the proxy. If the slot was restored to its delay thunk in that interval, the fallback can overwrite the restored state.
Please make the replacement conditional on the slot still holding the exact real export at the write, and count only successful replacements. A source-built check can exercise both transitions deterministically: a real export that remains installed is replaced, and a slot changed back to the thunk before the replacement stays untouched. Keep the existing unbound, same-name decoy and pre-attach pointer controls.
|
APPROVED. Rewrites bound delay QueryApiImpl with logging. |
Keep main's registry-import and save-path behavior, and keep the bound delay QueryApiImpl rewrite on top of it. Co-authored-by: SimSalabimse <SimSalabimse@users.noreply.github.com> # Conflicts: # src/startup_stubs/runtime_proxy.cpp # tests/startup_runtime_proxy_scope/host.cpp
…lds the export The write compare-exchanges the slot with the remembered export and counts the replacement only when that exchange stores the proxy. The source-built check covers a slot that stays the export and a slot restored to its thunk before the store. Quiet scans reuse an export that was already remembered. Co-authored-by: SimSalabimse <SimSalabimse@users.noreply.github.com>
|
Addressed the review in Verification (Windows 11 25H2 26200, MSVC 19.42, dev preset): AI-Assisted |
pigalot
left a comment
There was a problem hiding this comment.
Independently reviewed f8b51edb integrated over main e582a9a4 (tree 3784ee7e). The merge is clean (CMakeLists.txt and docs/startup-runtime.md auto-merged) and git diff --cached --check origin/main is clean.
Earlier finding (R0Y00, changes requested on 95f0f675): the code is addressed, the end-to-end evidence is not. replace_bound_delay_query now stores the proxy with InterlockedCompareExchange64 against the remembered real export, and patch_module counts only a successful exchange. Because the store compares at the moment it writes, a thunk the delay loader puts back after the eligibility read stays as it is. startup_runtime_gpa_patch covers both transitions with a fake exchange. Restoring the old unconditional store fails it at slot restored to the thunk stays the thunk: MISMATCH and restored thunk not counted: MISMATCH.
What I ran.
. .\tools\dev.ps1; cmake --preset <preset>
cmake --build --preset <preset> --target startup_runtime_gpa_patch startup_runtime_proxy_scope startup_stub_gameinput startup_stub_consumer startup_runtime_local startup_console_mode startup_console_mode_decoy
ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R '^(startup_runtime_(local|local_control|gpa_patch|gpa_patch_name_only_control|proxy_scope)|startup_console_mode(_control|_provider_control)?)$'
ctest --test-dir out/build/<preset> --no-tests=error -j 1 --repeat until-fail:10 -R '^startup_runtime_proxy_scope$'- Results: 8/8 pass in
devand inrelease/O2, with no skips. - Repeats:
startup_runtime_proxy_scopepasses 10 of 10 repeats in each preset. - Other stand-ins:
startup_stub_gameinputandstartup_stub_consumer, which also compileruntime_proxy.cpp, build in both presets.
Finding: startup_runtime_proxy_scope does not exercise the new delay-slot rewrite, and its unbound-thunk control does not hold. Each mutant below was applied alone to src/startup_stubs/runtime_proxy.cpp, startup_runtime_proxy_scope (with the carrier) rebuilt in dev, the test rerun, and the file restored from its raw bytes. git diff --quiet was clean afterwards.
| Mutant | startup_runtime_proxy_scope |
|---|---|
delay branch returns before the rewrite (no delay slot is ever rewritten, the same delay-slot handling as main) |
passes |
real_bits = *slot (any non-proxy delay slot counts as bound, so the unbound thunk is rewritten) |
passes |
- The rewrite path never runs. With the rewrite removed, the helper log shows only its
GetProcAddressimport being patched:x2w-runtime proxy: 1 import(s) patched in package module startup_runtime_proxy_helper.dll. Yetbound delay slot rewritten to proxystill passes. The CRT delay helper resolvesQueryApiImplthrough the helper's ownGetProcAddressimport, which the scan has already hooked. So the bind itself stores the proxy, and the slot never holds the real export for the new code to replace. That is also whydelay bind wrote real export or proxyaccepts either value. - The negative control reads too late. With the unbound thunk rewritten, the log shows
2 import(s) patched in package module startup_runtime_proxy_helper.dll. The thunk is replaced in the same scan that hooks the helper'sGetProcAddress.wait_for_helper_hookwaits for that scan, sobefore_bindalready reads the proxy.delay_query_slot_is_bound(proxy, real, proxy)is then false and the slot never changes after that, so bothdelay slot unbound before first useandunbound delay slot not rewrittenpass.
The device-free check stays sound. But the one test that runs the real loader, which the body and docs/startup-runtime.md give as coverage for the rewrite, passes for this PR, for main's handling and for the unsafe rewrite R0Y00 asked to rule out.
Suggested direction (unconfirmed):
- Make the slot bind to the real export first. Load the helper and call
call_delay_querybefore the proxy carrier attaches. The bind then goes through the unhookedGetProcAddress. Check that the slot equalsxgr_query, then load the carrier and wait for the slot to become the proxy. - Make the unbound control state its expectation. After the helper's scan has run, require the slot to still point inside the helper image (between the module base and
SizeOfImage), not just to equal an earlier read of itself. A second helper that is never called would give a clean unbound slot for this.
Other mutants (same procedure; startup_runtime_gpa_patch for the header):
| Mutant | Result |
|---|---|
| proxy stored without the compare-exchange (the pre-fix store) | killed: slot restored to the thunk stays the thunk: MISMATCH |
| every exchange attempt counted | killed: restored thunk not counted: MISMATCH |
| bound test ignores the real export | killed: unbound thunk refused: MISMATCH |
| export not remembered on a delay hit | survives; in this fixture real_query is already remembered through the hooked GetProcAddress |
QueryApiImpl lookups capped with the generic 40-line log |
survives; no test reads the always-on log line |
Non-blocking, a hypothesis I did not reproduce. In a module built with /guard:cf, the delay IAT can be protected. The MSVC delay-load helper then keeps that section read-only and unprotects it under its own lock only while it writes a slot. The background pass calls VirtualProtect on the slot's page, compare-exchanges, then restores the protection it saw, without that lock. Suppose another thread's first call to a different delay import in the same module takes the helper through its unprotect/write/reprotect at the same time. This pass could then restore read-only between the helper's unprotect and its write, and the helper's write would fault. In the other order, it could leave the section writable. Nothing in this repository builds with /guard:cf, so the fixtures cannot show it. Leaving delay slots alone in an image whose load config sets IMAGE_GUARD_PROTECT_DELAYLOAD_IAT would avoid the question.
Verdict: changes requested on f8b51edb, for the loader fixture above. The compare-exchange itself looks right to me.
Environment: Windows 11 25H2 build 26200.9974, MSVC 19.52.37029, Python 3.12.10. No GPU is involved.
CI: Build and test on Windows, Assemble and translate gfx1013 fixtures and Run platform-independent Python checks on Linux pass on f8b51edb.
This reply was AI-assisted.
Keep main's behavior where the files overlap, and keep the bound delay QueryApiImpl compare-exchange on top of it. Co-authored-by: SimSalabimse <SimSalabimse@users.noreply.github.com>
The loader test called the delay import after GetProcAddress was already hooked, so the bind stored the proxy and the slot rewrite never ran. The helper now binds through the unhooked lookup first, and the test waits for that real export to become the proxy. A second helper is never called; after the scan its thunk must still lie inside its own image. Co-authored-by: SimSalabimse <SimSalabimse@users.noreply.github.com>
|
The loader test was calling the delay import after GetProcAddress was already hooked, so the bind wrote the proxy and the rewrite never got a real export to replace. The helper now calls it before the proxy attaches. The test checks the slot is the real export, then waits for that slot to become the proxy. A second helper is never called, and after the scan its thunk has to still sit inside its own image. I built the source-built check, then ran it again with the exchange removed. g++ -std=c++17 -Wall -Wextra -Werror -Isrc -o /tmp/gpa_patch_check tests/startup_runtime_gpa_patch_check.cpp
/tmp/gpa_patch_check
/tmp/gpa_patch_check --mutate name-only
./out/pe-env/bin/python -B tools/run_checks_parallel.py --from-dir tests/checks/python-linuxThe check printed Windows test PC: the three tests passed twice, ctest exit 0. AI-Assisted |
pigalot
left a comment
There was a problem hiding this comment.
Retested eafec814 integrated over main 2e12dd8e (tree 987cb1c4). The merge is clean (CMakeLists.txt and docs/startup-runtime.md auto-merged) and git diff --cached --check origin/main is clean.
My earlier finding on f8b51edb (the loader fixture did not exercise the rewrite): addressed. The helper now binds its delay slot before the proxy attaches, and the test checks that the slot is the real export first. A second, never-called helper must keep its thunk inside its own image. My two earlier survivors are now killed:
| Mutant | f8b51edb |
eafec814 |
|---|---|---|
delay slot never rewritten (main's handling) |
passed | killed: bound delay slot rewritten to proxy: MISMATCH |
| any non-proxy delay slot counted as bound (unbound thunk rewritten) | passed | killed: unbound delay thunk stays inside its image: MISMATCH |
| proxy stored without the compare-exchange | killed | killed |
| every exchange attempt counted | killed | killed |
| bound test ignores the real export | killed | killed |
What I ran. I built startup_runtime_gpa_patch, startup_runtime_proxy_scope, startup_stub_gameinput, startup_stub_consumer, startup_runtime_local, startup_console_mode and startup_console_mode_decoy in dev and release, then ran:
ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R '^(startup_runtime_(local|local_control|gpa_patch|gpa_patch_name_only_control|proxy_scope)|startup_console_mode(_control|_provider_control)?)$'
ctest --test-dir out/build/<preset> --no-tests=error -j 1 --repeat until-fail:10 -R '^startup_runtime_proxy_scope$'8 of 8 pass in Debug and Release /O2, with no skips. startup_runtime_proxy_scope passes 10 of 10 repeats in each preset.
New finding: the loader test no longer loads anything after the proxy attaches, so the late-load path that main tests is now uncovered. On main, host.cpp loads the carrier first and then the helper ("late-load package helper"). It then waits for the helper's GetProcAddress import to be hooked by the worker. At this head both helpers, the decoy and the fake xgameruntime.dll all load before the carrier, so the initial scan finds everything. This came from my suggested direction, and I didn't flag the cost at the time.
The mutant: the worker runs only its first patch_package_modules and never rescans.
- On
main2e12dd8e(if (rescan_package_modules.exchange(...)) patch_package_modules();changed to run once):startup_runtime_proxy_scopefails athelper sees proxied xgameruntime QueryApiImpl: MISMATCH. - At
eafec814(if (tick == 0) patch_package_modules(true);): the test passes. Two narrower mutants also pass:dll_loadedignoring the load notification, and the new periodic quiet pass removed.
AGENTS.md asks not to weaken existing tests. The load-notification path is also the one docs/startup-runtime.md and the CMakeLists.txt comment still describe ("Notification only enqueues; rewrite runs after link").
Suggested direction (unconfirmed): load startup_runtime_proxy_helper_unbound.dll after the carrier, instead of before it. Keep the bound helper where it is. The unbound helper then needs the worker's load-driven scan to get its GetProcAddress hooked, which a resolve_query wait like wait_for_helper_hook would check. Its delay thunk is still never called, so the in-image check stays the negative control. The scan-once mutant should then fail again.
The periodic quiet pass is new in this PR. With the bind moved before attach, no test now needs it. A slot bound after the load-driven scan through an unhooked lookup would need it, but I don't see a cheap way to build that through the real loader. I'm not asking for it.
Verdict: changes requested on eafec814, for the lost late-load coverage only. The compare-exchange and the new bound and unbound controls look right to me. R0Y00's changes-requested review on 95f0f675 also still stands. This supersedes my changes-requested review on f8b51edb.
Environment: AMD Ryzen 7 9800X3D, Windows 11 25H2 build 26200.9974, MSVC 19.52.37029, Python 3.12.10. No GPU is involved.
CI: Build and test on Windows, Assemble and translate gfx1013 fixtures and Run platform-independent Python checks on Linux pass on eafec814.
This reply was AI-assisted.
|
Thanks for the compare-exchange and the bound and unbound helpers. I reviewed head
Also worth fixing:
Environment: Linux. I reviewed the git objects statically, ran This comment was AI-assisted. |
Keep the audio-activity row from main and the updated runtime-proxy row from this branch. Co-authored-by: SimSalabimse <SimSalabimse@users.noreply.github.com> # Conflicts: # docs/startup-runtime.md
The bound helper still binds QueryApiImpl to the real export before the carrier loads. The second helper loads after that scan, so its GetProcAddress hook needs the load-driven rescan and its thunk stays inside its image. Regular import slots are compared before VirtualProtect so a quiet pass does not change protection on a slot that is already patched. Co-authored-by: SimSalabimse <SimSalabimse@users.noreply.github.com>
pigalot
left a comment
There was a problem hiding this comment.
Retested 3ceef4a4 integrated over main ec71c290 (tree bb3f0da7). The merge is clean and git diff --cached --check origin/main is clean. The author's merge of main (9d2d0700) resolves the one conflicting table row in docs/startup-runtime.md by keeping main's X2W_AUDIO_ACTIVITY=1 row and the PR's X2W_RUNTIME_PROXY=1 row.
My earlier finding on eafec814 (late-load coverage lost): addressed. host.cpp now loads startup_runtime_proxy_helper_unbound.dll after the carrier and waits for its GetProcAddress hook (late-load helper sees proxied xgameruntime QueryApiImpl). The bound helper still binds before the carrier loads, and the unbound thunk must still lie inside its own image after the scan. The scan-once mutant that passed at eafec814 now fails.
What I ran. I built startup_runtime_gpa_patch, startup_runtime_proxy_scope, startup_stub_gameinput, startup_stub_consumer, startup_runtime_local, startup_console_mode and startup_console_mode_decoy in dev and release, then ran:
ctest --test-dir out/build/<preset> --no-tests=error -j 1 -R '^(startup_runtime_(local|local_control|gpa_patch|gpa_patch_name_only_control|proxy_scope)|startup_console_mode(_control|_provider_control)?)$'
ctest --test-dir out/build/<preset> --no-tests=error -j 1 --repeat until-fail:10 -R '^startup_runtime_proxy_scope$'8 of 8 pass in Debug and Release /O2, with no skips. startup_runtime_proxy_scope passes 10 of 10 repeats in each preset.
Mutants. Each was applied alone to src/startup_stubs/runtime_proxy.cpp (the last one to runtime_gpa_patch.h), the two tests rebuilt in dev and run with --repeat until-fail:5, and the file restored from its raw bytes. git diff --quiet was clean afterwards.
Killed:
| Mutant | First mismatch |
|---|---|
| worker scans once and never rescans | late-load helper sees proxied xgameruntime QueryApiImpl: MISMATCH |
| load notification ignored and the quiet pass removed | late-load helper sees proxied xgameruntime QueryApiImpl: MISMATCH |
delay slot never rewritten (main's handling) |
bound delay slot rewritten to proxy: MISMATCH |
| any non-proxy delay slot counted as bound | unbound delay thunk stays inside its image: MISMATCH |
new compare-first check inverted for GetProcAddress slots |
host GetProcAddress(xgameruntime) is proxied: MISMATCH |
| proxy stored without the compare-exchange | slot restored to the thunk stays the thunk: MISMATCH, restored thunk not counted: MISMATCH |
Surviving:
| Mutant | Why |
|---|---|
| load notification ignored (quiet pass kept) | the 250 ms quiet pass hooks the late helper inside the test's 2 s wait |
| quiet pass removed (notification kept) | the load-driven scan does the same work |
new compare-first check inverted for regular QueryApiImpl slots |
no loader fixture has a regular xgameruntime.dll!QueryApiImpl import; main has the same gap for that branch |
Equivalent:
| Mutant | Why |
|---|---|
new compare-first check removed for GetProcAddress slots |
the store below is already guarded by the same comparison; only the VirtualProtect pair differs, which no test observes |
The first two survivors are the two rescan paths covering for each other: either one alone gets the late helper hooked, and removing both fails. I'm not asking for a test that tells them apart.
Other notes, none blocking.
- The compare before
VirtualProtecton regular slots means a quiet pass no longer changes page protection on slots that are already patched. I read it; no test observes it (see the equivalent mutant). - The
/guard:cfprotected delay IAT hypothesis from my review onf8b51edbis unchanged. I still have not reproduced it and nothing in this repository builds that way. - CI's
ctest --preset prdoes not runstartup_runtime_proxy_scopeorstartup_runtime_gpa_patch, so the results above are from my local runs.
Verdict: approved on 3ceef4a4. This supersedes my changes-requested review on eafec814. R0Y00's changes-requested review on 95f0f675 still stands on the PR; the compare-exchange it asked for is in the code and covered by startup_runtime_gpa_patch. I am not merging this.
Environment: AMD Ryzen 7 9800X3D, Windows 11 25H2 build 26200.9974, MSVC 19.52.37029, Python 3.12.10. No GPU is involved.
CI: Build and test on Windows, Assemble and translate gfx1013 fixtures and Run platform-independent Python checks on Linux pass on 3ceef4a4.
This reply was AI-assisted.
|
The second helper loads after the carrier again, with the late-load package helper check, and the test waits for that helper's GetProcAddress hook. The first helper still binds the delay slot to the real export before the proxy attaches. Current main is merged, with the audio-activity switch row kept above the runtime-proxy row. Regular import slots are compared before VirtualProtect, so the quiet pass leaves an already patched slot's protection unchanged.
Windows test PC, head 3ceef4a (upstream main 3926a28 merged): passed 3/3 twice, exit 0. passed 6/6 once, exit 0. AI-Assisted |
Change
Account-picker TRACE_API on tip-of-main still shows zero
QueryApiImpl/provider … proxiedafter the scoped package GPA patch (#2166). Y reaches GameInput (0x20); #2201 never gets a chance. Do not treat this as FakeUser slot-7 evidence.Delay-imported
xgameruntime.dll!QueryApiImplbinds through the CRT delay helper (its own unhookedGetProcAddress), so the title keeps calling the real export while the proxy only remembered it. This change:QueryApiImplIAT slot only when it already equals the remembered real export (delay_query_slot_is_bound); unbound thunks stay untouchedGetProcAddress("QueryApiImpl")past the generic 40-line GPA capQueryApiImpl remembered via {gpa|iat|export}and a one-shotQueryApiImpl statuswhenxgameruntimeis loadedSynthetic PE control + Windows
startup_runtime_proxy_scopecover the bound rewrite; unbound delay and a pre-attach cached pointer are negative controls (the cache still calls the real export — claimable follow-up).Refs #870.
AI-assisted.
Verification
On Windows after configure:
Evidence and follow-up
Source-built / synthetic only; no game files.
docs/startup-runtime.mdX2W_RUNTIME_PROXYrow updated.User re-test: rebuild from this branch with
$env:X2W_TRACE_API='1', press Y. Look forQueryApiImpl remembered via, always-on GPA for that name,bound delay QueryApiImpl import(s) rewritten, and anyprovider … proxied.Checklist