Skip to content
Merged
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
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
118 changes: 118 additions & 0 deletions src/startup_stubs/api_trace.h
Original file line number Diff line number Diff line change
@@ -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 <cstddef>
#include <cstdio>
#include <cstring>

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<int>(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
67 changes: 67 additions & 0 deletions src/startup_stubs/runtime_proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstdio>
Expand Down Expand Up @@ -67,11 +68,59 @@ 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;
}

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


template <int N>
__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. The
// snapshot is bounded by the declared width, so a one-byte flag, a DWORD and a caller-sized buffer
// 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 < max_snapshot ? static_cast<std::size_t>(capacity) : max_snapshot;
}
if (!bytes || bytes > max_snapshot) return;
if (!x2w_local::readable(reinterpret_cast<const void*>(args[index]), bytes)) return;
std::memcpy(before[slot], reinterpret_cast<const void*>(args[index]), bytes);
compared_bytes[slot] = bytes;
compared[slot] = true;
};
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);
if (!handled && self->provider == gamertag_provider && N == 3 && fake_user()) {
Expand Down Expand Up @@ -107,6 +156,24 @@ __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()) {
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<const void*>(args[compared_index[i]]), compared_bytes[i])) continue;
std::memcpy(after[i], reinterpret_cast<const void*>(args[compared_index[i]]), compared_bytes[i]);
current[i] = true;
}
const x2w_api_trace::Report report = x2w_api_trace::decide(contract, current,
const_cast<const unsigned char (*)[max_snapshot]>(before),
const_cast<const unsigned char (*)[max_snapshot]>(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<unsigned>(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)" : "",
Expand Down
148 changes: 148 additions & 0 deletions tests/api_trace_check.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdio>
#include <cstring>

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;
}