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
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,8 @@ add_test(NAME startup_runtime_gpa_patch COMMAND startup_runtime_gpa_patch)
add_test(NAME startup_runtime_gpa_patch_name_only_control COMMAND startup_runtime_gpa_patch --mutate name-only)
set_tests_properties(startup_runtime_gpa_patch_name_only_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.
# unrelated same-named export stays original, pre-attach cached pointer forwards via export trampoline.
# Notification only enqueues; rewrite runs after link.
if (WIN32)
set(X2W_PROXY_SCOPE_DIR "${CMAKE_CURRENT_BINARY_DIR}/startup_runtime_proxy_scope")
add_library(startup_runtime_proxy_carrier SHARED
Expand All @@ -988,6 +989,7 @@ if (WIN32)
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
startup_runtime_proxy_decoy startup_runtime_proxy_scope)
Expand Down
2 changes: 1 addition & 1 deletion docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ ctest --preset dev --no-tests=error -R '^startup_trap_header(_prefix_control|_pr
| `X2W_HIDE_TITLE_WINDOW=1` (`run.sh --hide-title-window`) | once a present has reached the output window, hides the title's own window; dialogs stay visible, and the window is shown again when a present misses the output window, until the next present reaches it ([Output window and fallback diagnostics](#output-window-and-fallback-diagnostics)). Keyboard input to the title after the hide is not measured. Off by default on every host |
| `X2W_DRAW_DUMP_FRAME=<n>`, `X2W_DRAW_DUMP_DIR` | Full-resolution RGB snapshots of single-sample RGBA8/BGRA8 UNORM or sRGB targets after translated draws. A later present writes completed queue-fenced copies. At most 48 copies are pending or written; a copy whose fence reports device removal (`UINT64_MAX`) is released unwritten and frees its slot (`src/startup_stubs/draw_dump_tracking.h`, `ctest --preset dev --no-tests=error -R '^startup_dump_fence_(removed_control_)?warp$' -V`; the control must fail). |
| `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_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 (and notes an `xgameruntime.dll` load). 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. When `xgameruntime.dll` is loaded the worker also installs an absolute-JMP trampoline at its `QueryApiImpl` export so a pointer cached before attach still enters the proxy; other modules that export the same name are not patched, and an unknown prologue is left alone. While that DLL is missing the worker logs `export trampoline pending` (rate-limited) and, after 30 s, a one-shot `never loaded after 30s attach window` (or `still incomplete` if the module was seen); `VirtualAlloc` / `VirtualProtect` failures log `aborted` instead of failing silently. 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_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_STORAGE_PATH`, `X2W_STORAGE_MB`, `X2W_TITLE_ID` | persistent local storage and the manifest values the runtime would return. The path is UTF-8, with a trailing separator added when missing. If the path is invalid or exceeds 259 bytes including the separator, the proxy logs the refusal and uses `S:\` (`src/startup_stubs/storage_root_utf8.h`, `ctest --preset release --no-tests=error -R '^startup_storage_root'`). |
Expand Down
146 changes: 146 additions & 0 deletions src/startup_stubs/runtime_gpa_patch.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
// 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.
//
// Also: absolute-JMP export trampoline helpers for a QueryApiImpl pointer resolved before the proxy
// attaches. IAT/GPA rewrites do not touch already-held function pointers; patching the export body
// so those calls enter the proxy does. Only xgameruntime.dll is in scope (see the stand-in).
#pragma once

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

// x64 absolute indirect jump: FF 25 00 00 00 00 + 8-byte absolute address (14 bytes).
constexpr unsigned abs_jmp_bytes = 14;

inline void write_abs_jmp(unsigned char* at, const void* target) noexcept
{
at[0] = 0xFF;
at[1] = 0x25;
at[2] = at[3] = at[4] = at[5] = 0;
const auto addr = reinterpret_cast<std::uint64_t>(target);
for (int i = 0; i < 8; ++i) at[6 + i] = static_cast<unsigned char>((addr >> (8 * i)) & 0xff);
}

inline const void* abs_jmp_target(const unsigned char* at) noexcept
{
if (!at || at[0] != 0xFF || at[1] != 0x25 || at[2] || at[3] || at[4] || at[5]) return nullptr;
std::uint64_t addr = 0;
for (int i = 0; i < 8; ++i) addr |= static_cast<std::uint64_t>(at[6 + i]) << (8 * i);
return reinterpret_cast<const void*>(addr);
}

inline bool is_abs_jmp_to(const unsigned char* at, const void* target) noexcept
{
return abs_jmp_target(at) == target;
}

// Length of one x64 instruction starting at `p`, or 0 when unknown / unsafe to relocate into a
// trampoline stub (relative call/jmp, rip-relative that would break, or incomplete buffer).
// Covers the MSVC x64 prologues seen on QueryApiImpl fixtures and typical gaming-runtime exports.
inline unsigned x64_insn_len(const unsigned char* p, unsigned max_bytes) noexcept
{
if (!p || max_bytes == 0) return 0;
unsigned i = 0;
bool operand66 = false;
// Legacy prefixes + REX.
while (i < max_bytes) {
const unsigned char b = p[i];
if (b == 0x66) {
operand66 = true;
++i;
continue;
}
if (b == 0xF2 || b == 0xF3 || b == 0x64 || b == 0x65 || b == 0x67 || b == 0x2E || b == 0x3E) {
++i;
continue;
}
break;
}
bool rex_w = false;
if (i < max_bytes && (p[i] & 0xF0) == 0x40) {
rex_w = (p[i] & 0x08) != 0;
++i;
}
if (i >= max_bytes) return 0;
const unsigned char op = p[i++];
auto modrm_len = [&](unsigned at) -> unsigned {
if (at >= max_bytes) return 0;
const unsigned char modrm = p[at];
const unsigned mod = modrm >> 6;
const unsigned rm = modrm & 7;
unsigned n = 1;
if (mod != 3 && rm == 4) { // SIB
if (at + n >= max_bytes) return 0;
++n;
}
if (mod == 1) {
if (at + n >= max_bytes) return 0;
++n;
} else if (mod == 2 || (mod == 0 && rm == 5) || (mod == 0 && rm == 4 && at + 1 < max_bytes && (p[at + 1] & 7) == 5)) {
// disp32, or [rip+disp32], or [sib with base=5]
if (at + n + 3 >= max_bytes) return 0;
n += 4;
}
return n;
};
// push/pop r64
if (op >= 0x50 && op <= 0x5F) return i;
// nop / int3 / ret — single byte; ret is fine as a whole instruction but useless for a hook site.
if (op == 0x90 || op == 0xCC || op == 0xC3) return i;
// imm pushes
if (op == 0x6A) return (i < max_bytes) ? i + 1 : 0;
if (op == 0x68) return (i + 3 < max_bytes) ? i + 4 : 0;
// mov r64, imm64 / mov r32, imm32
if (op >= 0xB8 && op <= 0xBF) {
const unsigned imm = rex_w ? 8u : (operand66 ? 2u : 4u);
return (i + imm - 1 < max_bytes) ? i + imm : 0;
}
if (op >= 0xB0 && op <= 0xB7) return (i < max_bytes) ? i + 1 : 0;
// Relative call/jmp: refuse (would need relocation in the stub).
if (op == 0xE8 || op == 0xE9 || op == 0xEB) return 0;
// ALU / mov / lea / test with ModRM
if (op == 0x01 || op == 0x03 || op == 0x09 || op == 0x0B || op == 0x11 || op == 0x13 || op == 0x19 || op == 0x1B ||
op == 0x21 || op == 0x23 || op == 0x29 || op == 0x2B || op == 0x31 || op == 0x33 || op == 0x39 || op == 0x3B ||
op == 0x85 || op == 0x87 || op == 0x88 || op == 0x89 || op == 0x8A || op == 0x8B || op == 0x8D) {
// rip-relative (mod=0, rm=5) breaks when relocated into the stub.
if (i < max_bytes && (p[i] & 0xC7) == 0x05) return 0;
const unsigned m = modrm_len(i);
return m ? i + m : 0;
}
// imm8 / imm32 groups: 80-83 / C6 / C7
if (op == 0x80 || op == 0x82 || op == 0x83 || op == 0xC6) {
if (i < max_bytes && (p[i] & 0xC7) == 0x05) return 0;
const unsigned m = modrm_len(i);
if (!m || i + m >= max_bytes) return 0;
return i + m + 1;
}
if (op == 0x81 || op == 0xC7) {
if (i < max_bytes && (p[i] & 0xC7) == 0x05) return 0;
const unsigned m = modrm_len(i);
const unsigned imm = operand66 ? 2u : 4u;
if (!m || i + m + imm - 1 >= max_bytes) return 0;
return i + m + imm;
}
// sub/add/xor/cmp r/m, imm already covered. 0F multi-byte: only short nops.
if (op == 0x0F) {
if (i >= max_bytes) return 0;
const unsigned char op2 = p[i++];
if (op2 == 0x1F) { // nop r/m
const unsigned m = modrm_len(i);
return m ? i + m : 0;
}
return 0;
}
// FF /0-/6: refuse call/jmp forms; allow /0-/6 only for inc/dec that are rare in prologues.
if (op == 0xFF) return 0;
return 0;
}

// How many whole prologue bytes to steal so an abs_jmp_bytes patch fits, or 0 if the prologue cannot
// be sized safely. Fills any leftover bytes in the live patch with NOPs at the call site.
inline unsigned stolen_bytes_for_abs_jmp(const unsigned char* code, unsigned max_scan) noexcept
{
if (!code || max_scan < abs_jmp_bytes) return 0;
unsigned total = 0;
while (total < abs_jmp_bytes) {
const unsigned n = x64_insn_len(code + total, max_scan - total);
if (!n) return 0;
total += n;
if (total > max_scan) return 0;
}
return total;
}

// 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
Loading
Loading