// Checks the registered xmem.dll shim against the measured Xbox ABI. // // Every function is resolved through rt::shims::find, so the test exercises the same shim table and calling // convention the runtime installs for xmem.dll imports instead of a private copy of the implementation. The checks // cover what the measured call sites use: 64-byte-aligned allocations, XMemFree, working-set statistics with the // size at +8, and XMemVirtualQuery's MEMORY_BASIC_INFORMATION prefix inside a zeroed 64-byte output. #include #include #include #include #include #include "shims.h" namespace { using XMemAllocFn = void*(__stdcall*)(size_t, uint64_t); using XMemFreeFn = void(__stdcall*)(void*, uint64_t); using XMemVirtualQueryFn = SIZE_T(__stdcall*)(HANDLE, const void*, void*); // BOOL XMemGetWorkingSetStatistics(UINT32 kind, stats*): the FSR DLL passes kind 0x20000001 at every call site. using XMemGetWorkingSetStatisticsFn = BOOL(__stdcall*)(uint32_t, void*); constexpr uint32_t kMeasuredKind = 0x20000001; int g_checks = 0; int g_failures = 0; void check(bool condition, const char* what) { ++g_checks; std::printf("%s %s\n", condition ? "PASS" : "FAIL", what); if (!condition) ++g_failures; } bool aligned_64(const void* p) { return p && (reinterpret_cast(p) & 63u) == 0; } void test_alloc(XMemAllocFn alloc, XMemFreeFn free_mem) { void* const zero = alloc(0, 0); check(zero != nullptr, "XMemAlloc(0, 0) returns non-null"); check(aligned_64(zero), "XMemAlloc(0, 0) returns a 64-byte aligned block"); bool zero_readable = false; if (zero) { auto* bytes = static_cast(zero); bytes[0] = 0xa5; zero_readable = bytes[0] == 0xa5; } check(zero_readable, "XMemAlloc(0, 0) block is writable and readable"); void* const big = alloc(4096, 0x5e50000); check(big != nullptr, "XMemAlloc(4096, 0x5e50000) returns non-null"); check(aligned_64(big), "XMemAlloc(4096, 0x5e50000) returns a 64-byte aligned block"); bool pattern_ok = big != nullptr; if (big) { auto* bytes = static_cast(big); for (size_t i = 0; i < 4096; ++i) bytes[i] = static_cast((i * 37u + 11u) & 0xffu); for (size_t i = 0; i < 4096; ++i) { if (bytes[i] != static_cast((i * 37u + 11u) & 0xffu)) { pattern_ok = false; break; } } } check(pattern_ok, "XMemAlloc(4096, 0x5e50000) block holds a write/read pattern"); free_mem(zero, 0); free_mem(big, 0x5e50000); check(true, "XMemFree accepts both blocks (no crash)"); } void test_working_set(XMemGetWorkingSetStatisticsFn get_stats) { alignas(64) uint8_t stats[64]; std::memset(stats, 0xa5, sizeof(stats)); // The pointer is the second argument: the first (0x20000001 here, as the FSR DLL passes it) must never be written // through, which is what the one-argument form did. const BOOL ok = get_stats(kMeasuredKind, stats); check(ok != FALSE, "XMemGetWorkingSetStatistics(kind, 64-byte out) returns TRUE"); uint64_t working_set = 0; std::memcpy(&working_set, stats + 8, sizeof(working_set)); check(ok != FALSE && working_set != 0, "XMemGetWorkingSetStatistics writes a non-zero size at +8"); bool tail_untouched = true; for (size_t i = 16; i < sizeof(stats); ++i) { if (stats[i] != 0xa5) { tail_untouched = false; break; } } check(ok != FALSE && tail_untouched, "XMemGetWorkingSetStatistics leaves bytes 16..63 untouched"); check(get_stats(kMeasuredKind, nullptr) == FALSE, "XMemGetWorkingSetStatistics(kind, nullptr) returns FALSE without crashing"); } void test_virtual_query(XMemVirtualQueryFn query) { uint8_t local = 0; const void* const local_address = &local; alignas(64) uint8_t out[64]; std::memset(out, 0xa5, sizeof(out)); const SIZE_T written = query(GetCurrentProcess(), local_address, out); check(written != 0, "XMemVirtualQuery(current process, stack local) returns non-zero"); MEMORY_BASIC_INFORMATION info{}; std::memcpy(&info, out, sizeof(info)); const uintptr_t base = reinterpret_cast(info.BaseAddress); const uintptr_t address = reinterpret_cast(local_address); const bool contains_local = base != 0 && info.RegionSize != 0 && address >= base && address - base < info.RegionSize; check(written != 0 && contains_local, "XMemVirtualQuery fills MEMORY_BASIC_INFORMATION with a region containing the local"); bool tail_zero = true; for (size_t i = sizeof(info); i < sizeof(out); ++i) { if (out[i] != 0) { tail_zero = false; break; } } check(written != 0 && tail_zero, "XMemVirtualQuery leaves the 64-byte output tail zero"); alignas(64) uint8_t bad_out[64]; std::memset(bad_out, 0xa5, sizeof(bad_out)); // Address 1 is queryable (it falls in the free region starting at 0), so use a non-canonical address that // VirtualQueryEx rejects. const void* const unmappable = reinterpret_cast(~static_cast(0)); const SIZE_T bad = query(GetCurrentProcess(), unmappable, bad_out); check(bad == 0, "XMemVirtualQuery on an invalid address returns 0 without crashing"); } void print_result() { if (g_failures == 0) { std::printf("xmem_test: OK (%d checks)\n", g_checks); } else { std::printf("xmem_test: FAILED (%d checks, %d failure%s)\n", g_checks, g_failures, g_failures == 1 ? "" : "s"); } } } // namespace int main() { std::printf("xmem_test: registered xmem.dll shim ABI\n"); const auto alloc = reinterpret_cast(rt::shims::find("xmem.dll", "XMemAlloc")); const auto free_mem = reinterpret_cast(rt::shims::find("xmem.dll", "XMemFree")); const auto query = reinterpret_cast(rt::shims::find("xmem.dll", "XMemVirtualQuery")); const auto get_stats = reinterpret_cast(rt::shims::find("xmem.dll", "XMemGetWorkingSetStatistics")); check(alloc != nullptr, "registry exposes xmem.dll!XMemAlloc"); check(free_mem != nullptr, "registry exposes xmem.dll!XMemFree"); check(query != nullptr, "registry exposes xmem.dll!XMemVirtualQuery"); check(get_stats != nullptr, "registry exposes xmem.dll!XMemGetWorkingSetStatistics"); if (!alloc || !free_mem || !query || !get_stats) { print_result(); return 1; } test_alloc(alloc, free_mem); test_working_set(get_stats); test_virtual_query(query); print_result(); return g_failures ? 1 : 0; }