Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1200,8 +1200,13 @@ add_test(NAME startup_console_mode_control
add_test(NAME startup_console_mode_provider_control
COMMAND startup_console_mode --control provider --decoy "$<TARGET_FILE:startup_console_mode_decoy>")
set_tests_properties(startup_console_mode_control startup_console_mode_provider_control PROPERTIES PASS_REGULAR_EXPRESSION "FAIL: ")
# EXE + late-loaded package helper through the real Windows loader: proxied xgameruntime QueryApiImpl,
# unrelated same-named export stays original. Notification only enqueues; rewrite runs after link.
# EXE + package helpers through the real Windows loader: proxied xgameruntime QueryApiImpl,
# unrelated same-named export stays original. One helper binds its delay QueryApiImpl slot to the
# real export before the proxy attaches; the scan must then store the proxy. A second helper is
# never called. It loads after the proxy, so its GetProcAddress hook needs the load-driven rescan,
# and its thunk must still lie inside its own image. A pre-attach cached pointer stays the real
# export. Notification only enqueues; rewrite runs after link. The
# gpa-patch check counts a replacement only when the slot still holds that export at the write.
if (WIN32)
set(X2W_PROXY_SCOPE_DIR "${CMAKE_CURRENT_BINARY_DIR}/startup_runtime_proxy_scope")
add_library(startup_runtime_proxy_carrier SHARED
Expand All @@ -1210,30 +1215,43 @@ if (WIN32)
target_include_directories(startup_runtime_proxy_carrier PRIVATE src)
target_compile_features(startup_runtime_proxy_carrier PRIVATE cxx_std_17)
target_link_libraries(startup_runtime_proxy_carrier PRIVATE psapi)
add_library(startup_runtime_proxy_helper SHARED
tests/startup_runtime_proxy_scope/helper.cpp
tests/startup_runtime_proxy_scope/helper.def)
target_compile_features(startup_runtime_proxy_helper PRIVATE cxx_std_17)
add_library(startup_runtime_proxy_xgr SHARED
tests/startup_runtime_proxy_scope/xgameruntime.cpp
tests/startup_runtime_proxy_scope/xgameruntime.def)
target_compile_features(startup_runtime_proxy_xgr PRIVATE cxx_std_17)
set_target_properties(startup_runtime_proxy_xgr PROPERTIES OUTPUT_NAME xgameruntime)
add_library(startup_runtime_proxy_helper SHARED
tests/startup_runtime_proxy_scope/helper.cpp
tests/startup_runtime_proxy_scope/helper.def)
target_compile_features(startup_runtime_proxy_helper PRIVATE cxx_std_17)
# Delay-load the fake xgameruntime so the helper's QueryApiImpl slot starts as a thunk and only
# holds the real export after first use (the bound-delay rewrite path).
target_link_libraries(startup_runtime_proxy_helper PRIVATE startup_runtime_proxy_xgr delayimp)
target_link_options(startup_runtime_proxy_helper PRIVATE "/DELAYLOAD:xgameruntime.dll")
# Same delay import, never called, loaded after the carrier. Its slot stays the in-image thunk.
add_library(startup_runtime_proxy_helper_unbound SHARED
tests/startup_runtime_proxy_scope/helper.cpp
tests/startup_runtime_proxy_scope/helper_unbound.def)
target_compile_features(startup_runtime_proxy_helper_unbound PRIVATE cxx_std_17)
target_link_libraries(startup_runtime_proxy_helper_unbound PRIVATE startup_runtime_proxy_xgr delayimp)
target_link_options(startup_runtime_proxy_helper_unbound PRIVATE "/DELAYLOAD:xgameruntime.dll")
add_library(startup_runtime_proxy_decoy SHARED
tests/startup_runtime_proxy_scope/decoy.cpp
tests/startup_runtime_proxy_scope/decoy.def)
target_compile_features(startup_runtime_proxy_decoy PRIVATE cxx_std_17)
add_executable(startup_runtime_proxy_scope tests/startup_runtime_proxy_scope/host.cpp)
target_include_directories(startup_runtime_proxy_scope PRIVATE src)
target_compile_features(startup_runtime_proxy_scope PRIVATE cxx_std_17)
foreach(tgt IN ITEMS startup_runtime_proxy_carrier startup_runtime_proxy_helper startup_runtime_proxy_xgr
foreach(tgt IN ITEMS startup_runtime_proxy_carrier startup_runtime_proxy_helper
startup_runtime_proxy_helper_unbound startup_runtime_proxy_xgr
startup_runtime_proxy_decoy startup_runtime_proxy_scope)
set_target_properties(${tgt} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${X2W_PROXY_SCOPE_DIR}"
LIBRARY_OUTPUT_DIRECTORY "${X2W_PROXY_SCOPE_DIR}"
ARCHIVE_OUTPUT_DIRECTORY "${X2W_PROXY_SCOPE_DIR}")
endforeach()
add_dependencies(startup_runtime_proxy_scope startup_runtime_proxy_carrier startup_runtime_proxy_helper
startup_runtime_proxy_xgr startup_runtime_proxy_decoy)
startup_runtime_proxy_helper_unbound startup_runtime_proxy_xgr startup_runtime_proxy_decoy)
add_test(NAME startup_runtime_proxy_scope COMMAND "$<TARGET_FILE:startup_runtime_proxy_scope>")
set_tests_properties(startup_runtime_proxy_scope PROPERTIES TIMEOUT 60 RUN_SERIAL TRUE)
endif()
Expand Down
2 changes: 1 addition & 1 deletion docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -1664,7 +1664,7 @@ ctest --preset dev --no-tests=error -R '^startup_hook_(window|overlap)$' -V
| `X2W_TEARDOWN_REPORT=1`, `X2W_TEARDOWN_NO_GUARD=<pages, readbacks or all>` | Test accounting for the caches whose destructors run during the module's CRT teardown (`src/startup_stubs/d3d12_teardown.h`, #2572). With `X2W_TEARDOWN_REPORT`, each destroyed cache writes one `x2w-teardown <cache> detached=<n> released=<n>` line to stderr with `WriteFile`, because a stdio buffer would not be flushed from a CRT teardown. `X2W_TEARDOWN_NO_GUARD` names a cache whose detach at process exit becomes an ordinary release again: the negative control `startup_teardown_unload` runs against itself, where the termination check has to fail. Unset, both change nothing and no line is written. |
| `X2W_AUDIO_PERMISSIVE=1` | enables the audio stand-ins. Without a DSP provider, `XDspActivate` clears valid outputs and reports unavailable resources, and failed `XDspSubmitCommand` calls leave the sequence unchanged. `XDspDeactivate` and `XDspDisconnect` refuse null and unknown handles with `E_INVALIDARG`. |
| `X2W_AUDIO_ACTIVITY=1` | with `X2W_AUDIO_PERMISSIVE=1`, counts XMA context changes, HAL submits and XApu commands, and logs at most a line per second when a count moved ([Activity counter](#activity-counter)) |
| `X2W_RUNTIME_PROXY=1` | proxies the gaming runtime and logs every call. Rewrites regular-IAT `GetProcAddress` and a direct `xgameruntime.dll!QueryApiImpl` import on the main EXE and on DLLs that share its directory (package helpers). Dynamic `QueryApiImpl` / `InitializeApiImpl*` are wrapped only when the provider module's basename is `xgameruntime.dll`. `LdrRegisterDllNotification` only flags that another scan is needed. A worker holds each matching module live while rewriting after linking. Loading a helper and immediately calling it can precede the scan. The enabled proxy stays loaded until process exit. Delay-import IAT slots are not rewritten. Skips this stand-in's own `GetProcAddress` IAT so the hook does not recurse (`src/startup_stubs/runtime_gpa_patch.h`, `ctest --preset dev --no-tests=error -R '^startup_runtime_gpa_patch(_name_only_control)?$'`, and the control must print `FAIL:`; Windows also runs `startup_runtime_proxy_scope` for a late-loaded helper through the real loader) |
| `X2W_RUNTIME_PROXY=1` | proxies the gaming runtime and logs every call. Rewrites regular-IAT `GetProcAddress` and a direct `xgameruntime.dll!QueryApiImpl` import on the main EXE and on DLLs that share its directory (package helpers). Dynamic `QueryApiImpl` / `InitializeApiImpl*` are wrapped only when the provider module's basename is `xgameruntime.dll`. `LdrRegisterDllNotification` only flags that another scan is needed. A worker holds each matching module live while rewriting after linking. Loading a helper and immediately calling it can precede the scan. The enabled proxy stays loaded until process exit. Unbound delay-import IAT slots are left alone. A later scan stores the proxy in a delay `QueryApiImpl` slot only when a compare-exchange still sees the real export; a thunk restored after the eligibility read stays in place and is not counted. The source-built check covers both transitions. `GetProcAddress("QueryApiImpl")` is always logged (generic GPA lines stay capped). Skips this stand-in's own `GetProcAddress` IAT so the hook does not recurse (`src/startup_stubs/runtime_gpa_patch.h`, `ctest --preset dev --no-tests=error -R '^startup_runtime_gpa_patch(_name_only_control)?$'`, and the control must print `FAIL:`; Windows also runs `startup_runtime_proxy_scope`: one helper binds the delay slot to the real export before the proxy attaches, and that slot must then become the proxy; a second helper loads after the proxy and is never called, so its `GetProcAddress` hook needs the load-driven rescan, and its thunk must stay inside its own image) |
| `X2W_FAKE_USER=1` | answers the user, gamertag and save APIs for one local user, and creates one offline store context for that user |
| `X2W_RUNTIME_LOCAL=1` | with `X2W_RUNTIME_PROXY` and `X2W_FAKE_USER`, for a host where the system gaming runtime cannot initialize (no Gaming Services). The first failed `InitializeApiImpl*` is reported as `S_OK`, and every later initialization call returns `S_OK` without calling the real export. Once initialization failure is latched, a failed real `QueryApiImpl` call can fall back only with an exact match of both GUIDs to a supported provider/interface pair for user (including gamertag), game save, package, title, connectivity or store. Persistent local storage uses its existing fake-user gate and requires its exact provider GUID and IID. Unmatched failed queries keep the real HRESULT. The proxy clears a writable output on failure only when fake user is enabled and the initialization failure is latched. A local object's `QueryInterface` accepts only `IID_IUnknown` and the IID it was created for. The local answers above apply, calls that carry the local user or store handle fail as before, and other unanswered methods return `E_NOTIMPL`. `XUserAddAsync` and async operations completed on behalf of a local answer require the real runtime's `QueryApiImpl` to return the XAsync provider (`073b7dcb`). Those operations complete with `S_OK` through XAsync, or return the query failure if XAsync is unavailable (`ctest --preset dev --no-tests=error -R '^startup_runtime_local(_control)?$'`, and the control must print `FAIL:`). |
| `X2W_DEVICE_TYPE=<n>` | with `X2W_RUNTIME_PROXY=1`, answers the title's reads of the OEM `ConsoleMode` registry value: `6` = 0x60 (Series S), `7` = 0x64 (Series X), `1` = value missing (PC); unset, the real registry answers. See [Console profile](#console-profile) |
Expand Down
43 changes: 38 additions & 5 deletions src/startup_stubs/runtime_gpa_patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
// resolves QueryApiImpl through a package helper's GetProcAddress (or that imports QueryApiImpl from
// xgameruntime.dll by name) never hits that hook, so FakeUser / X2W_TRACE_API see no providers.
// This header lists the import shapes to notice — regular and delay-import tables. The stand-in
// rewrites only regular IAT slots on the EXE and package-directory helpers: a delay IAT holds a
// delay-load thunk until first use, so treating it as GetProcAddress or QueryApiImpl is wrong.
// Ordinal imports are ignored. Pure walk helpers — no Windows calls — so a source-built control can
// check them.
// rewrites regular GetProcAddress / QueryApiImpl IAT slots on the EXE and package-directory helpers.
// A delay IAT holds a delay-load thunk until first use; rewriting that thunk as if it were the
// export is wrong. After the delay helper binds, the same slot holds the real export address.
// replace_bound_delay_query stores the proxy only when a compare-exchange still sees that export,
// so a thunk restored after the eligibility read stays in place and is not counted. Ordinal
// imports are ignored.
// Pure walk helpers — no Windows calls — so a source-built control can check them.
#pragma once

#include <cstddef>
Expand Down Expand Up @@ -50,6 +53,35 @@ inline bool import_is_query_api_impl(const char* name) noexcept
return name && std::strcmp(name, "QueryApiImpl") == 0;
}

// True when a delay-import QueryApiImpl IAT slot already holds the real export (post-bind) and is
// not yet the proxy. A still-unbound thunk (any other non-zero value, typically inside the
// importer) must stay untouched so the delay helper can run.
inline bool delay_query_slot_is_bound(std::uint64_t slot_value, std::uint64_t real_query,
std::uint64_t proxy) noexcept
{
return real_query != 0 && slot_value != 0 && slot_value != proxy && slot_value == real_query;
}

// True when `address` lies in [image_base, image_base + image_size). A delay thunk lives in the
// importing image; the real export and the proxy do not.
inline bool address_in_image(std::uint64_t address, std::uint64_t image_base, std::uint64_t image_size) noexcept
{
return image_size != 0 && address >= image_base && (address - image_base) < image_size;
}

// Store `proxy` only when `exchange` still observes `real_query`. `exchange(slot, desired, expected)`
// returns the previous value and writes `desired` only when the slot equals `expected`
// (InterlockedCompareExchange). An earlier read can see the export and the delay loader can then
// put the thunk back; that exchange leaves the thunk and this function returns false, so the caller
// does not count it. Returns true only when the proxy was stored.
template <class Exchange>
inline bool replace_bound_delay_query(std::uint64_t* slot, std::uint64_t real_query, std::uint64_t proxy,
Exchange exchange) noexcept
{
if (!slot || !delay_query_slot_is_bound(*slot, real_query, proxy)) return false;
return exchange(slot, proxy, real_query) == real_query;
}

// Decide whether one named import of `dll_name` should be noticed. Ordinal imports are ignored.
inline bool classify(const char* dll_name, const char* import_name, Kind* kind) noexcept
{
Expand Down Expand Up @@ -173,7 +205,8 @@ int visit_imports_with(const unsigned char* base, std::size_t image_size, Classi
}
}

// DataDirectory[13] = delay imports (reported with delay_import=true; callers must not rewrite the slot as a real export).
// DataDirectory[13] = delay imports (reported with delay_import=true; rewrite only when
// replace_bound_delay_query still sees the real export at the store).
const auto& delay_dir = dirs[13];
if (delay_dir.virtual_address && delay_dir.size) {
for (std::uint32_t at = delay_dir.virtual_address;; at += sizeof(DelayDesc)) {
Expand Down
Loading
Loading