mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 16:08:37 +00:00
164 lines
6.2 KiB
C++
164 lines
6.2 KiB
C++
// 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 <windows.h>
|
|
#include <psapi.h>
|
|
|
|
#include <cstdint>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
|
|
#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*);
|
|
using XMemGetWorkingSetStatisticsFn = BOOL(__stdcall*)(void*);
|
|
|
|
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<uintptr_t>(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<uint8_t*>(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<uint8_t*>(big);
|
|
for (size_t i = 0; i < 4096; ++i) bytes[i] = static_cast<uint8_t>((i * 37u + 11u) & 0xffu);
|
|
for (size_t i = 0; i < 4096; ++i) {
|
|
if (bytes[i] != static_cast<uint8_t>((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));
|
|
const BOOL ok = get_stats(stats);
|
|
check(ok != FALSE, "XMemGetWorkingSetStatistics(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(nullptr) == FALSE, "XMemGetWorkingSetStatistics(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<uintptr_t>(info.BaseAddress);
|
|
const uintptr_t address = reinterpret_cast<uintptr_t>(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<const void*>(~static_cast<uintptr_t>(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<XMemAllocFn>(rt::shims::find("xmem.dll", "XMemAlloc"));
|
|
const auto free_mem = reinterpret_cast<XMemFreeFn>(rt::shims::find("xmem.dll", "XMemFree"));
|
|
const auto query = reinterpret_cast<XMemVirtualQueryFn>(rt::shims::find("xmem.dll", "XMemVirtualQuery"));
|
|
const auto get_stats =
|
|
reinterpret_cast<XMemGetWorkingSetStatisticsFn>(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;
|
|
}
|