From dce4ce6da0208bf65816bf1fbbbebb7f739d51b0 Mon Sep 17 00:00:00 2001 From: triggered02 <153171514+triggered02@users.noreply.github.com> Date: Tue, 24 Nov 2026 13:34:23 +0530 Subject: [PATCH 1/3] startup: trace declared provider outputs behind an opt-in switch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds X2W_TRACE_API=1, one line per provider call with the return code, whether the answer came from the stand-in or the real runtime, and which declared output argument was written. Only declared outputs are read. The previous revision of this trace probed every argument that looked like a readable pointer, which cannot distinguish an out-argument from a handle or size that happens to look like an address, and reports "none" for calls whose outputs sit outside the probed slots. The contracts are now a small table (user add result, id, guest flag, state, gamertag buffer and used count, age group, privilege result and deny reason, connectivity hint); a slot with no declared contract reports out=? and nothing is read. Nothing changes unless the switch is set. Measured with the switch on over a 70 s offline run (7,250 provider calls): x2w-api 37e56907::slot15 -> 0x00000000 local out=unchanged x2w-api 37e56907::slot2 -> 0x00000001 real out=? Outcomes: 3,833 undeclared (out=?), 3,400 unchanged, 15 arg3, 2 arg1. The 3,400 "unchanged" are the connectivity hint, whose four words are constant, so a repeat call leaves the same bytes — which is why "unchanged" is not by itself evidence that an output was skipped, and the log now says so per call instead of leaving it to be inferred. --- src/startup_stubs/runtime_proxy.cpp | 74 +++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/startup_stubs/runtime_proxy.cpp b/src/startup_stubs/runtime_proxy.cpp index e4ec7a6a21..aa80d0bf10 100644 --- a/src/startup_stubs/runtime_proxy.cpp +++ b/src/startup_stubs/runtime_proxy.cpp @@ -67,11 +67,63 @@ u64 complete_async(u64 async_block); void* storage_object(); constexpr unsigned long save_provider_data1 = 0x704c3f58; // hypothesis: the game-save API (first seen: slot 4 with the user handle) +// X2W_TRACE_API=1: one line per provider call with the return code, whether the answer came from the +// stand-in or the real runtime, and which declared output argument it wrote. Only declared outputs are +// read: arguments are not probed as pointers, because a handle or size that happens to look like an +// address would be read as if it were one. A slot with no declared contract reports "out=?" instead. +bool trace_api() noexcept +{ + static const bool on = x2w_local::env_on("X2W_TRACE_API"); + return on; +} + +// Declared output arguments, as 0-based argument indices. These follow the answers in user_call and +// area_call below; anything not listed has no known contract here and is not read back. +struct DeclaredOut { + unsigned long provider; + int slot; + unsigned char first; // argument index of the first output + unsigned char second; // second output, or 0xff when the slot has one +}; + +constexpr unsigned char no_second = 0xff; +constexpr DeclaredOut declared_outs[] = { + {0x01acd177, 8, 1, no_second}, // user add result: &handle + {0x01acd177, 12, 1, no_second}, // user id: &id + {0x01acd177, 13, 1, no_second}, // is guest: &flag + {0x01acd177, 14, 1, no_second}, // state: &state + {0x01acd177, 15, 3, 4}, // gamertag: buffer, &used + {0x01acd177, 19, 1, no_second}, // age group: &group + {0x01acd177, 20, 3, 4}, // privilege: &has, &deny reason + {0x37e56907, 15, 0, no_second}, // connectivity hint: &words +}; + +const DeclaredOut* declared_out(unsigned long provider, int slot) +{ + for (const DeclaredOut& entry : declared_outs) + if (entry.provider == provider && entry.slot == slot) return &entry; + return nullptr; +} + template __declspec(noinline) u64 forward(Proxy* self, u64 a, u64 b, u64 c, u64 s0, u64 s1, u64 s2, u64 s3, u64 s4, u64 s5, u64 s6, u64 s7) { const bool log_it = N >= 3 && InterlockedIncrement(&call_counts[self->provider][N]) <= 6; u64 args[11] = {a, b, c, s0, s1, s2, s3, s4, s5, s6, s7}; + // Declared outputs are snapshotted so the write is seen rather than assumed; nothing else is read. + u64 before[2] = {}; + bool readable_argument[2] = {}; + const DeclaredOut* declared = trace_api() ? declared_out(provider_ids[self->provider].Data1, N) : nullptr; + if (declared) { + const int index = declared->first; + readable_argument[0] = args[index] && x2w_local::readable(reinterpret_cast(args[index]), 8); + if (readable_argument[0]) std::memcpy(&before[0], reinterpret_cast(args[index]), 8); + if (declared->second != no_second) { + const int other = declared->second; + readable_argument[1] = args[other] && x2w_local::readable(reinterpret_cast(args[other]), 8); + if (readable_argument[1]) std::memcpy(&before[1], reinterpret_cast(args[other]), 8); + } + } u64 result = 0; bool handled = self->provider == user_provider && fake_user() && user_call(N, args, &result); if (!handled && self->provider == gamertag_provider && N == 3 && fake_user()) { @@ -107,6 +159,28 @@ __declspec(noinline) u64 forward(Proxy* self, u64 a, u64 b, u64 c, u64 s0, u64 s const bool about_tracked = N >= 3 && (tracked(a) || tracked(b) || tracked(c)); const u64 raw_result = result; if (about_tracked && !handled && fake_user() && (result & 0xffffffffu) == borrowed_failure) result = 0; + if (trace_api()) { + char out[24] = "?"; + if (declared) { + int written = 0; + for (int i = 0; i < 2; ++i) { + const int index = i == 0 ? declared->first : declared->second; + if (index == no_second || !readable_argument[i]) continue; + if (!x2w_local::readable(reinterpret_cast(args[index]), 8)) continue; + u64 now = 0; + std::memcpy(&now, reinterpret_cast(args[index]), 8); + if (now != before[i] && !written) written = index; + } + if (written) { + std::snprintf(out, sizeof(out), "arg%d", written); + } else { + const bool any = readable_argument[0] || readable_argument[1]; + std::snprintf(out, sizeof(out), "%s", any ? "unchanged" : "unreadable"); + } + } + x2w_local::logf("x2w-api %08lx::slot%d -> 0x%08x %s out=%s", provider_ids[self->provider].Data1, N, + static_cast(result & 0xffffffffu), handled ? "local" : "real", out); + } if (log_it || (about_tracked && InterlockedIncrement(&tracked_logged) <= 120)) x2w_local::logf("x2w-runtime %08lx::slot%d(0x%llx, 0x%llx, 0x%llx | 0x%llx, 0x%llx) -> 0x%llx%s%s from +0x%llx", provider_ids[self->provider].Data1, N, a, b, c, s0, s1, result, handled ? " (local user)" : "", From 8d6c5477745a1dcdc58c8a54c16c4e297872bfad Mon Sep 17 00:00:00 2001 From: triggered02 <153171514+triggered02@users.noreply.github.com> Date: Tue, 24 Nov 2026 13:49:18 +0530 Subject: [PATCH 2/3] startup: bound the API trace by each declared output's width Addresses the review of #1324. The trace took eight bytes from every output. A readable page does not establish the object's extent, so a one-byte flag or a DWORD could be read past its end or be classified unreadable when it straddles a page. The contract now carries each output's width: 1 for the guest flag, 4 for the state, age group and deny reason, 8 for handles, ids and used counts, 16 for the connectivity hint, and a caller-sized buffer for the gamertag, where the capacity comes from the size argument and the snapshot is bounded by it (and by a small cap). Also from the review: - Argument index 0 is a real output (the connectivity hint), so index 0 can no longer double as "no output changed". The trace uses a separate changed flag; before this, a changed hint was logged as `out=unchanged`. - XUserGetId is slot 11, not 12; slot 12 is XUserFindUserById. Both are declared, along with the duplicate-handle, local-id and find-for-device slots. - The dedicated gamertag interface (provider 0xcef4fac0, slot 3) has its own declaration, so its buffer and used count are no longer logged as unknown. Measured with X2W_TRACE_API=1 over a 60 s offline run (5,638 calls): 2,893 undeclared, 2,601 arg0 (the connectivity hint now correctly reported), 84 arg3 (gamertag buffer), 56 arg1, 4 unchanged. Because the widths are now correct, "unchanged" drops from thousands to four - the constant hint is compared over its real 16 bytes and reported as written. Nothing changes unless the switch is set; `startup_stubs` passes. --- src/startup_stubs/runtime_proxy.cpp | 100 ++++++++++++++++++---------- 1 file changed, 66 insertions(+), 34 deletions(-) diff --git a/src/startup_stubs/runtime_proxy.cpp b/src/startup_stubs/runtime_proxy.cpp index aa80d0bf10..aed30195fd 100644 --- a/src/startup_stubs/runtime_proxy.cpp +++ b/src/startup_stubs/runtime_proxy.cpp @@ -77,25 +77,36 @@ bool trace_api() noexcept return on; } -// Declared output arguments, as 0-based argument indices. These follow the answers in user_call and -// area_call below; anything not listed has no known contract here and is not read back. +// Declared output arguments, as 0-based argument indices, with the width each one actually has. These +// follow the answers in user_call, the gamertag branch below and area_call; anything not listed has no +// known contract here and is not read back. A width of 0 means the caller supplies the capacity in +// `size_argument` (a string buffer), and the snapshot is bounded by it. struct DeclaredOut { unsigned long provider; int slot; - unsigned char first; // argument index of the first output - unsigned char second; // second output, or 0xff when the slot has one + unsigned char first; // argument index of the first output + unsigned char first_bytes; // its width, or 0 when the caller supplies the capacity + unsigned char size_argument; // argument index holding that capacity, or no_argument + unsigned char second; // second output, or no_argument + unsigned char second_bytes; // its width }; -constexpr unsigned char no_second = 0xff; +constexpr unsigned char no_argument = 0xff; constexpr DeclaredOut declared_outs[] = { - {0x01acd177, 8, 1, no_second}, // user add result: &handle - {0x01acd177, 12, 1, no_second}, // user id: &id - {0x01acd177, 13, 1, no_second}, // is guest: &flag - {0x01acd177, 14, 1, no_second}, // state: &state - {0x01acd177, 15, 3, 4}, // gamertag: buffer, &used - {0x01acd177, 19, 1, no_second}, // age group: &group - {0x01acd177, 20, 3, 4}, // privilege: &has, &deny reason - {0x37e56907, 15, 0, no_second}, // connectivity hint: &words + {0x01acd177, 3, 1, 8, no_argument, no_argument, 0}, // duplicate handle + {0x01acd177, 8, 1, 8, no_argument, no_argument, 0}, // user add result: &handle + {0x01acd177, 9, 1, 8, no_argument, no_argument, 0}, // local id + {0x01acd177, 10, 1, 8, no_argument, no_argument, 0}, // find by local id + {0x01acd177, 11, 1, 8, no_argument, no_argument, 0}, // XUserGetId: &id + {0x01acd177, 12, 1, 8, no_argument, no_argument, 0}, // XUserFindUserById + {0x01acd177, 13, 1, 1, no_argument, no_argument, 0}, // is guest: one byte + {0x01acd177, 14, 1, 4, no_argument, no_argument, 0}, // state: DWORD + {0x01acd177, 15, 3, 0, 2, 4, 8}, // gamertag buffer (capacity in arg 2), &used + {0x01acd177, 19, 1, 4, no_argument, no_argument, 0}, // age group: DWORD + {0x01acd177, 20, 3, 1, no_argument, 4, 4}, // privilege: &has, &deny reason + {0x01acd177, 40, 1, 8, no_argument, no_argument, 0}, // find for device + {0xcef4fac0, 3, 3, 0, 2, 4, 8}, // gamertag interface: buffer, &used + {0x37e56907, 15, 0, 16, no_argument, no_argument, 0}, // connectivity hint: four words }; const DeclaredOut* declared_out(unsigned long provider, int slot) @@ -105,24 +116,44 @@ const DeclaredOut* declared_out(unsigned long provider, int slot) return nullptr; } +// The widest output a declaration can describe; the gamertag buffer is bounded by the caller's +// capacity but only its first bytes are compared. +constexpr std::size_t max_snapshot = 64; + template __declspec(noinline) u64 forward(Proxy* self, u64 a, u64 b, u64 c, u64 s0, u64 s1, u64 s2, u64 s3, u64 s4, u64 s5, u64 s6, u64 s7) { const bool log_it = N >= 3 && InterlockedIncrement(&call_counts[self->provider][N]) <= 6; u64 args[11] = {a, b, c, s0, s1, s2, s3, s4, s5, s6, s7}; - // Declared outputs are snapshotted so the write is seen rather than assumed; nothing else is read. - u64 before[2] = {}; - bool readable_argument[2] = {}; + // Declared outputs are snapshotted so the write is seen rather than assumed; nothing else is read. The + // snapshot is bounded by the declared width, so a one-byte flag, a DWORD and a caller-sized buffer + // are each read within their own extent rather than as eight bytes. + struct Output { + int index; + std::size_t bytes; + bool readable; + unsigned char data[max_snapshot]; + }; + Output before[2] = {}; const DeclaredOut* declared = trace_api() ? declared_out(provider_ids[self->provider].Data1, N) : nullptr; if (declared) { - const int index = declared->first; - readable_argument[0] = args[index] && x2w_local::readable(reinterpret_cast(args[index]), 8); - if (readable_argument[0]) std::memcpy(&before[0], reinterpret_cast(args[index]), 8); - if (declared->second != no_second) { - const int other = declared->second; - readable_argument[1] = args[other] && x2w_local::readable(reinterpret_cast(args[other]), 8); - if (readable_argument[1]) std::memcpy(&before[1], reinterpret_cast(args[other]), 8); - } + const auto take = [&args](Output& slot, unsigned char index, unsigned char fixed_bytes, unsigned char size_index) { + slot.index = index; + if (index == no_argument || !args[index]) return; + std::size_t bytes = fixed_bytes; + if (!bytes) { + if (size_index == no_argument) return; + const u64 capacity = args[size_index]; + bytes = capacity < sizeof(slot.data) ? static_cast(capacity) : sizeof(slot.data); + } + if (!bytes || bytes > sizeof(slot.data)) return; + if (!x2w_local::readable(reinterpret_cast(args[index]), bytes)) return; + std::memcpy(slot.data, reinterpret_cast(args[index]), bytes); + slot.bytes = bytes; + slot.readable = true; + }; + take(before[0], declared->first, declared->first_bytes, declared->size_argument); + take(before[1], declared->second, declared->second_bytes, no_argument); } u64 result = 0; bool handled = self->provider == user_provider && fake_user() && user_call(N, args, &result); @@ -162,19 +193,20 @@ __declspec(noinline) u64 forward(Proxy* self, u64 a, u64 b, u64 c, u64 s0, u64 s if (trace_api()) { char out[24] = "?"; if (declared) { - int written = 0; - for (int i = 0; i < 2; ++i) { - const int index = i == 0 ? declared->first : declared->second; - if (index == no_second || !readable_argument[i]) continue; - if (!x2w_local::readable(reinterpret_cast(args[index]), 8)) continue; - u64 now = 0; - std::memcpy(&now, reinterpret_cast(args[index]), 8); - if (now != before[i] && !written) written = index; + // A separate flag: argument index 0 is a real output (the connectivity hint), so zero + // cannot double as "nothing changed". + int written = -1; + for (int i = 0; i < 2 && written < 0; ++i) { + const Output& snapshot = before[i]; + if (snapshot.index == no_argument || !snapshot.readable) continue; + if (!x2w_local::readable(reinterpret_cast(args[snapshot.index]), snapshot.bytes)) continue; + if (std::memcmp(reinterpret_cast(args[snapshot.index]), snapshot.data, snapshot.bytes) != 0) + written = snapshot.index; } - if (written) { + if (written >= 0) { std::snprintf(out, sizeof(out), "arg%d", written); } else { - const bool any = readable_argument[0] || readable_argument[1]; + const bool any = before[0].readable || before[1].readable; std::snprintf(out, sizeof(out), "%s", any ? "unchanged" : "unreadable"); } } From 9731fae2d3a005e90d681bd76a9c276abc8e29ef Mon Sep 17 00:00:00 2001 From: triggered02 <153171514+triggered02@users.noreply.github.com> Date: Tue, 24 Nov 2026 14:04:15 +0530 Subject: [PATCH 3/3] startup: report every changed output and cover the API trace with a control Addresses the second review of #1324. The trace no longer stops at the first changed output: it reports every declared output that differs ("changed:0", "changed:3,4"), and it distinguishes three outcomes that were previously collapsed into one word: - "same" - every declared output was compared and held the same value; - "same,partial" - at least one output was compared and held the same value while another declared output could not be compared; - "unreadable" - no declared output could be compared at all. The wording is now about what was observed, not about what the provider did: a provider may legitimately write the value that was already there, so "same" never means "did not write". The decision is moved into src/startup_stubs/api_trace.h with the contract table, so it can be checked without a running title, and tests/api_trace_check.cpp covers the four cases the review asked for plus the surrounding ones: - argument index 0 changed is reported as "changed:0" (index 0 cannot double as "nothing changed"); - two changed outputs are both listed ("changed:3,4"); - a one-byte output is compared over one byte, and a byte past the declared width is not its change; - a caller buffer shorter than the declared width is compared over the caller's capacity only; - identical bytes report "same"; - one comparable output beside an uncomparable one reports "same,partial"; - no comparable output reports "unreadable", and an undeclared slot reports "?" and reads nothing; - the table answers for XUserGetId (11), XUserFindUserById (12), the secondary gamertag interface (0xcef4fac0 slot 3) and the connectivity hint (0x37e56907 slot 15). api_trace passes (13 cases) and startup_stubs passes; nothing changes unless X2W_TRACE_API is set. --- CMakeLists.txt | 6 ++ src/startup_stubs/api_trace.h | 118 ++++++++++++++++++++++ src/startup_stubs/runtime_proxy.cpp | 117 ++++++++-------------- tests/api_trace_check.cpp | 148 ++++++++++++++++++++++++++++ 4 files changed, 311 insertions(+), 78 deletions(-) create mode 100644 src/startup_stubs/api_trace.h create mode 100644 tests/api_trace_check.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e554c8e016..163ad6fc74 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -278,6 +278,12 @@ foreach(adapter IN ITEMS hardware warp) add_test(NAME startup_null_views_${adapter} COMMAND startup_null_views_check ${adapter}) endforeach() set_tests_properties(startup_null_views_hardware PROPERTIES SKIP_RETURN_CODE 77) +# The opt-in API trace reads only declared outputs; this checks the decision about what it observed +# (argument index 0, two outputs, exact and short buffers, same value, partial comparison). +add_executable(api_trace_check tests/api_trace_check.cpp) +target_compile_features(api_trace_check PRIVATE cxx_std_17) +target_include_directories(api_trace_check PRIVATE src) +add_test(NAME api_trace COMMAND api_trace_check) # A view heap larger than the adapter accepts: the stand-in creates the desktop heap at the measured # limit and keeps the title's full-size memory. Skips where the adapter accepts the full request. add_executable(startup_view_heap_check tests/startup_view_heap_check.cpp) diff --git a/src/startup_stubs/api_trace.h b/src/startup_stubs/api_trace.h new file mode 100644 index 0000000000..eb29198ad0 --- /dev/null +++ b/src/startup_stubs/api_trace.h @@ -0,0 +1,118 @@ +// Declared provider outputs for the opt-in API trace (X2W_TRACE_API), kept in one place so the +// decision about what was observed can be checked without a running title. +// +// The contracts below follow the answers in runtime_proxy.cpp. Only declared outputs are ever read: +// arguments are not probed as pointers, because a handle or a size that happens to look like an +// address would be read as if it were one. +// +// A width of 0 means the caller supplies the capacity in `size_argument`, so the comparison is bounded +// by that capacity. "same" therefore means every declared output was compared and held the same value; +// it does not mean the provider skipped its write, because a provider may legitimately write the value +// that was already there. +#pragma once + +#include +#include +#include + +namespace x2w_api_trace { + +constexpr unsigned char no_argument = 0xff; +constexpr int max_outputs = 2; + +// The widest output a declaration describes; a caller-sized buffer is compared within this bound. +constexpr std::size_t max_snapshot = 64; + +struct DeclaredOut { + unsigned long provider; + int slot; + unsigned char first; // argument index of the first output + unsigned char first_bytes; // its width, or 0 when the caller supplies the capacity + unsigned char size_argument; // argument index holding that capacity, or no_argument + unsigned char second; // second output, or no_argument + unsigned char second_bytes; // its width +}; + +inline const DeclaredOut* declared(unsigned long provider, int slot) +{ + static constexpr DeclaredOut table[] = { + {0x01acd177, 3, 1, 8, no_argument, no_argument, 0}, // duplicate handle + {0x01acd177, 8, 1, 8, no_argument, no_argument, 0}, // user add result: &handle + {0x01acd177, 9, 1, 8, no_argument, no_argument, 0}, // local id + {0x01acd177, 10, 1, 8, no_argument, no_argument, 0}, // find by local id + {0x01acd177, 11, 1, 8, no_argument, no_argument, 0}, // XUserGetId: &id + {0x01acd177, 12, 1, 8, no_argument, no_argument, 0}, // XUserFindUserById + {0x01acd177, 13, 1, 1, no_argument, no_argument, 0}, // is guest: one byte + {0x01acd177, 14, 1, 4, no_argument, no_argument, 0}, // state: DWORD + {0x01acd177, 15, 3, 0, 2, 4, 8}, // gamertag buffer (capacity in arg 2), &used + {0x01acd177, 19, 1, 4, no_argument, no_argument, 0}, // age group: DWORD + {0x01acd177, 20, 3, 1, no_argument, 4, 4}, // privilege: &has, &deny reason + {0x01acd177, 40, 1, 8, no_argument, no_argument, 0}, // find for device + {0xcef4fac0, 3, 3, 0, 2, 4, 8}, // gamertag interface: buffer, &used + {0x37e56907, 15, 0, 16, no_argument, no_argument, 0}, // connectivity hint: four words + }; + for (const DeclaredOut& entry : table) + if (entry.provider == provider && entry.slot == slot) return &entry; + return nullptr; +} + +enum class Outcome { + Unknown, // no declared contract: nothing was read + Unreadable, // a contract exists but no declared output could be compared + Same, // every declared output was compared and held the same value + Changed, // at least one declared output differs +}; + +struct Report { + Outcome outcome = Outcome::Unknown; + int changed_count = 0; + int changed[max_outputs] = {}; + bool partial = false; // some declared output could not be compared +}; + +// `compared` says whether each declared output's bytes were captured; `before` and `after` hold the +// bytes for the outputs that were captured, and `bytes` their widths. +inline Report decide(const DeclaredOut* contract, const bool* compared, const unsigned char (*before)[max_snapshot], + const unsigned char (*after)[max_snapshot], const std::size_t* bytes) +{ + Report report{}; + if (!contract) return report; + const int declared_count = contract->second == no_argument ? 1 : 2; + int checked = 0; + for (int i = 0; i < declared_count; ++i) { + if (!compared[i] || !bytes[i]) continue; + ++checked; + if (std::memcmp(before[i], after[i], bytes[i]) != 0) report.changed[report.changed_count++] = static_cast(i); + } + report.partial = checked != declared_count; + if (!checked) report.outcome = Outcome::Unreadable; + else if (report.changed_count) report.outcome = Outcome::Changed; + else report.outcome = Outcome::Same; + return report; +} + +// Renders the report: "?", "unreadable", "same", "changed", or "changed:0,1" with ",partial" appended +// when some declared output could not be compared. The argument indices in the text are the ones the +// caller passed, so index 0 is listed like any other. +inline void format(const Report& report, const DeclaredOut* contract, char* out, std::size_t size) +{ + switch (report.outcome) { + case Outcome::Unknown: std::snprintf(out, size, "?"); return; + case Outcome::Unreadable: std::snprintf(out, size, "unreadable"); return; + case Outcome::Same: std::snprintf(out, size, "same%s", report.partial ? ",partial" : ""); return; + case Outcome::Changed: { + const int first = contract ? contract->first : 0; + const int second = contract && contract->second != no_argument ? contract->second : -1; + const int a0 = report.changed[0] == 0 ? first : second; + if (report.changed_count > 1) { + const int a1 = report.changed[1] == 0 ? first : second; + std::snprintf(out, size, "changed:%d,%d%s", a0, a1, report.partial ? ",partial" : ""); + } else { + std::snprintf(out, size, "changed:%d%s", a0, report.partial ? ",partial" : ""); + } + return; + } + } +} + +} // namespace x2w_api_trace \ No newline at end of file diff --git a/src/startup_stubs/runtime_proxy.cpp b/src/startup_stubs/runtime_proxy.cpp index aed30195fd..9e047f0f06 100644 --- a/src/startup_stubs/runtime_proxy.cpp +++ b/src/startup_stubs/runtime_proxy.cpp @@ -7,6 +7,7 @@ // methods log their first calls ("x2w-runtime") and forward to the real object. // // X2W_FAKE_USER=1 additionally answers the user API for one local user (see fake_user below). +#include "startup_stubs/api_trace.h" #include "startup_stubs/local_log.h" #include @@ -77,48 +78,14 @@ bool trace_api() noexcept return on; } -// Declared output arguments, as 0-based argument indices, with the width each one actually has. These -// follow the answers in user_call, the gamertag branch below and area_call; anything not listed has no -// known contract here and is not read back. A width of 0 means the caller supplies the capacity in -// `size_argument` (a string buffer), and the snapshot is bounded by it. -struct DeclaredOut { - unsigned long provider; - int slot; - unsigned char first; // argument index of the first output - unsigned char first_bytes; // its width, or 0 when the caller supplies the capacity - unsigned char size_argument; // argument index holding that capacity, or no_argument - unsigned char second; // second output, or no_argument - unsigned char second_bytes; // its width -}; - -constexpr unsigned char no_argument = 0xff; -constexpr DeclaredOut declared_outs[] = { - {0x01acd177, 3, 1, 8, no_argument, no_argument, 0}, // duplicate handle - {0x01acd177, 8, 1, 8, no_argument, no_argument, 0}, // user add result: &handle - {0x01acd177, 9, 1, 8, no_argument, no_argument, 0}, // local id - {0x01acd177, 10, 1, 8, no_argument, no_argument, 0}, // find by local id - {0x01acd177, 11, 1, 8, no_argument, no_argument, 0}, // XUserGetId: &id - {0x01acd177, 12, 1, 8, no_argument, no_argument, 0}, // XUserFindUserById - {0x01acd177, 13, 1, 1, no_argument, no_argument, 0}, // is guest: one byte - {0x01acd177, 14, 1, 4, no_argument, no_argument, 0}, // state: DWORD - {0x01acd177, 15, 3, 0, 2, 4, 8}, // gamertag buffer (capacity in arg 2), &used - {0x01acd177, 19, 1, 4, no_argument, no_argument, 0}, // age group: DWORD - {0x01acd177, 20, 3, 1, no_argument, 4, 4}, // privilege: &has, &deny reason - {0x01acd177, 40, 1, 8, no_argument, no_argument, 0}, // find for device - {0xcef4fac0, 3, 3, 0, 2, 4, 8}, // gamertag interface: buffer, &used - {0x37e56907, 15, 0, 16, no_argument, no_argument, 0}, // connectivity hint: four words -}; - -const DeclaredOut* declared_out(unsigned long provider, int slot) -{ - for (const DeclaredOut& entry : declared_outs) - if (entry.provider == provider && entry.slot == slot) return &entry; - return nullptr; -} +// The contract table and the decision about what was observed live in api_trace.h so the decision can +// be checked by a source-built control without a running title. +using x2w_api_trace::declared; +using x2w_api_trace::DeclaredOut; +using x2w_api_trace::max_outputs; +using x2w_api_trace::max_snapshot; +using x2w_api_trace::no_argument; -// The widest output a declaration can describe; the gamertag buffer is bounded by the caller's -// capacity but only its first bytes are compared. -constexpr std::size_t max_snapshot = 64; template __declspec(noinline) u64 forward(Proxy* self, u64 a, u64 b, u64 c, u64 s0, u64 s1, u64 s2, u64 s3, u64 s4, u64 s5, u64 s6, u64 s7) @@ -127,33 +94,32 @@ __declspec(noinline) u64 forward(Proxy* self, u64 a, u64 b, u64 c, u64 s0, u64 s u64 args[11] = {a, b, c, s0, s1, s2, s3, s4, s5, s6, s7}; // Declared outputs are snapshotted so the write is seen rather than assumed; nothing else is read. The // snapshot is bounded by the declared width, so a one-byte flag, a DWORD and a caller-sized buffer - // are each read within their own extent rather than as eight bytes. - struct Output { - int index; - std::size_t bytes; - bool readable; - unsigned char data[max_snapshot]; - }; - Output before[2] = {}; - const DeclaredOut* declared = trace_api() ? declared_out(provider_ids[self->provider].Data1, N) : nullptr; - if (declared) { - const auto take = [&args](Output& slot, unsigned char index, unsigned char fixed_bytes, unsigned char size_index) { - slot.index = index; + // Declared outputs are snapshotted so a write can be observed rather than assumed; nothing else is + // read. Each snapshot is bounded by the declared width, so a one-byte flag, a DWORD and a + // caller-sized buffer are each compared within their own extent. + bool compared[2] = {}; + std::size_t compared_bytes[2] = {}; + int compared_index[2] = {no_argument, no_argument}; + unsigned char before[2][max_snapshot] = {}; + const DeclaredOut* contract = trace_api() ? declared(provider_ids[self->provider].Data1, N) : nullptr; + if (contract) { + const auto take = [&](int slot, unsigned char index, unsigned char fixed_bytes, unsigned char size_index) { + compared_index[slot] = index; if (index == no_argument || !args[index]) return; std::size_t bytes = fixed_bytes; if (!bytes) { if (size_index == no_argument) return; const u64 capacity = args[size_index]; - bytes = capacity < sizeof(slot.data) ? static_cast(capacity) : sizeof(slot.data); + bytes = capacity < max_snapshot ? static_cast(capacity) : max_snapshot; } - if (!bytes || bytes > sizeof(slot.data)) return; + if (!bytes || bytes > max_snapshot) return; if (!x2w_local::readable(reinterpret_cast(args[index]), bytes)) return; - std::memcpy(slot.data, reinterpret_cast(args[index]), bytes); - slot.bytes = bytes; - slot.readable = true; + std::memcpy(before[slot], reinterpret_cast(args[index]), bytes); + compared_bytes[slot] = bytes; + compared[slot] = true; }; - take(before[0], declared->first, declared->first_bytes, declared->size_argument); - take(before[1], declared->second, declared->second_bytes, no_argument); + take(0, contract->first, contract->first_bytes, contract->size_argument); + take(1, contract->second, contract->second_bytes, no_argument); } u64 result = 0; bool handled = self->provider == user_provider && fake_user() && user_call(N, args, &result); @@ -191,25 +157,20 @@ __declspec(noinline) u64 forward(Proxy* self, u64 a, u64 b, u64 c, u64 s0, u64 s const u64 raw_result = result; if (about_tracked && !handled && fake_user() && (result & 0xffffffffu) == borrowed_failure) result = 0; if (trace_api()) { - char out[24] = "?"; - if (declared) { - // A separate flag: argument index 0 is a real output (the connectivity hint), so zero - // cannot double as "nothing changed". - int written = -1; - for (int i = 0; i < 2 && written < 0; ++i) { - const Output& snapshot = before[i]; - if (snapshot.index == no_argument || !snapshot.readable) continue; - if (!x2w_local::readable(reinterpret_cast(args[snapshot.index]), snapshot.bytes)) continue; - if (std::memcmp(reinterpret_cast(args[snapshot.index]), snapshot.data, snapshot.bytes) != 0) - written = snapshot.index; - } - if (written >= 0) { - std::snprintf(out, sizeof(out), "arg%d", written); - } else { - const bool any = before[0].readable || before[1].readable; - std::snprintf(out, sizeof(out), "%s", any ? "unchanged" : "unreadable"); - } + unsigned char after[2][max_snapshot] = {}; + bool current[2] = {}; + for (int i = 0; i < 2; ++i) { + if (!compared[i] || !compared_bytes[i]) continue; + if (!x2w_local::readable(reinterpret_cast(args[compared_index[i]]), compared_bytes[i])) continue; + std::memcpy(after[i], reinterpret_cast(args[compared_index[i]]), compared_bytes[i]); + current[i] = true; } + const x2w_api_trace::Report report = x2w_api_trace::decide(contract, current, + const_cast(before), + const_cast(after), + compared_bytes); + char out[48] = "?"; + x2w_api_trace::format(report, contract, out, sizeof(out)); x2w_local::logf("x2w-api %08lx::slot%d -> 0x%08x %s out=%s", provider_ids[self->provider].Data1, N, static_cast(result & 0xffffffffu), handled ? "local" : "real", out); } diff --git a/tests/api_trace_check.cpp b/tests/api_trace_check.cpp new file mode 100644 index 0000000000..2946c830be --- /dev/null +++ b/tests/api_trace_check.cpp @@ -0,0 +1,148 @@ +// Source-built control for the API trace's decision about what a provider call wrote. +// +// The trace compares only the outputs a contract declares, over each one's own width, and reports what +// it observed. The four cases here are the ones a reviewer asked for: argument index 0 (which must not +// double as "nothing changed"), two outputs changed at once (both indices reported), an exact and a +// short caller buffer (the width must come from the declaration and from the caller's capacity), and +// the same-value case (a provider may write the value that was already there, which is "same", not +// "not written"). +#include "startup_stubs/api_trace.h" + +#include +#include + +namespace trace = x2w_api_trace; + +namespace { +int failures; +using Bytes = unsigned char[trace::max_snapshot]; + +void check(const char* label, bool condition) +{ + std::printf("%s: %s\n", label, condition ? "ok" : "MISMATCH"); + if (!condition) ++failures; +} + +// One declared output at argument index 0 with a 16-byte width (the connectivity hint's shape). +void index_zero() +{ + const trace::DeclaredOut contract{0x37e56907, 15, 0, 16, trace::no_argument, trace::no_argument, 0}; + trace::Report report{}; + bool compared[2] = {true, false}; + std::size_t bytes[2] = {16, 0}; + unsigned char before[2][trace::max_snapshot] = {}; + unsigned char after[2][trace::max_snapshot] = {}; + after[0][0] = 1; // only the output changed + report = trace::decide(&contract, compared, before, after, bytes); + char text[48] = {}; + trace::format(report, &contract, text, sizeof(text)); + check("index 0 change is reported", report.outcome == trace::Outcome::Changed && std::strcmp(text, "changed:0") == 0); +} + +// Two declared outputs whose bytes both change: both argument indices must be listed. +void two_outputs() +{ + const trace::DeclaredOut contract{0x01acd177, 20, 3, 1, trace::no_argument, 4, 4}; + bool compared[2] = {true, true}; + std::size_t bytes[2] = {1, 4}; + unsigned char before[2][trace::max_snapshot] = {}; + unsigned char after[2][trace::max_snapshot] = {}; + after[0][0] = 1; + after[1][0] = 2; + const trace::Report report = trace::decide(&contract, compared, before, after, bytes); + char text[48] = {}; + trace::format(report, &contract, text, sizeof(text)); + check("both changed outputs are listed", report.changed_count == 2 && std::strcmp(text, "changed:3,4") == 0); +} + +// A one-byte output must be compared over one byte: a difference in the byte after it is not its change. +void exact_and_short_buffer() +{ + const trace::DeclaredOut flag{0x01acd177, 13, 1, 1, trace::no_argument, trace::no_argument, 0}; + bool compared[2] = {true, false}; + std::size_t bytes[2] = {1, 0}; + unsigned char before[2][trace::max_snapshot] = {}; + unsigned char after[2][trace::max_snapshot] = {}; + after[0][1] = 0x7f; // outside the declared width + trace::Report report = trace::decide(&flag, compared, before, after, bytes); + check("a byte past the width is not this output's change", report.outcome == trace::Outcome::Same); + + // A caller buffer shorter than the declared width is compared over the caller's capacity only. + const trace::DeclaredOut string{0x01acd177, 15, 3, 0, 2, 4, 8}; + std::size_t short_bytes[2] = {4, 8}; + unsigned char short_before[2][trace::max_snapshot] = {}; + unsigned char short_after[2][trace::max_snapshot] = {}; + short_after[0][4] = 0x11; // past the four bytes the caller offered + report = trace::decide(&string, compared, short_before, short_after, short_bytes); + check("a short caller buffer is compared over its capacity", report.outcome == trace::Outcome::Same); +} + +// A provider that writes the value already there is "same", and a declared output that could not be +// compared is "partial", not "same". +void same_value_and_partial() +{ + const trace::DeclaredOut contract{0x01acd177, 20, 3, 1, trace::no_argument, 4, 4}; + bool compared[2] = {true, true}; + std::size_t bytes[2] = {1, 4}; + unsigned char before[2][trace::max_snapshot] = {}; + unsigned char after[2][trace::max_snapshot] = {}; + trace::Report report = trace::decide(&contract, compared, before, after, bytes); + char text[48] = {}; + trace::format(report, &contract, text, sizeof(text)); + check("identical bytes are reported as same", report.outcome == trace::Outcome::Same && std::strcmp(text, "same") == 0); + + bool partial_compared[2] = {true, false}; + std::size_t partial_bytes[2] = {1, 0}; + report = trace::decide(&contract, partial_compared, before, after, partial_bytes); + trace::format(report, &contract, text, sizeof(text)); + check("one comparable output is partial", report.outcome == trace::Outcome::Same && std::strcmp(text, "same,partial") == 0); + + bool none[2] = {false, false}; + std::size_t none_bytes[2] = {0, 0}; + report = trace::decide(&contract, none, before, after, none_bytes); + trace::format(report, &contract, text, sizeof(text)); + check("no comparable output is unreadable", report.outcome == trace::Outcome::Unreadable && + std::strcmp(text, "unreadable") == 0); +} + +// A slot with no contract is not read at all and reports "?". +void unknown_contract() +{ + trace::Report report{}; + bool compared[2] = {}; + std::size_t bytes[2] = {}; + unsigned char before[2][trace::max_snapshot] = {}; + unsigned char after[2][trace::max_snapshot] = {}; + report = trace::decide(nullptr, compared, before, after, bytes); + char text[48] = {}; + trace::format(report, nullptr, text, sizeof(text)); + check("undeclared slot reports unknown", report.outcome == trace::Outcome::Unknown && std::strcmp(text, "?") == 0); +} + +// The declared table must answer for the slots the stand-in actually answers, including the two the +// review found missing. +void declared_table() +{ + check("XUserGetId is slot 11", trace::declared(0x01acd177, 11) != nullptr); + check("XUserFindUserById is slot 12", trace::declared(0x01acd177, 12) != nullptr); + check("secondary gamertag interface is declared", trace::declared(0xcef4fac0, 3) != nullptr); + check("connectivity hint is declared", trace::declared(0x37e56907, 15) != nullptr); + check("undeclared slot has no contract", trace::declared(0x01acd177, 33) == nullptr); +} +} // namespace + +int main() +{ + index_zero(); + two_outputs(); + exact_and_short_buffer(); + same_value_and_partial(); + unknown_contract(); + declared_table(); + if (failures) { + std::printf("FAIL\n"); + return 1; + } + std::printf("PASS\n"); + return 0; +} \ No newline at end of file