Files
mandy/runtime/shims/xmem.cpp
Kam471 34d2e63c65 Runtime: match the measured Xbox xmem/d3d12_xs ABI
- xmem: XMemVirtualQuery(process, address, out64) fills the known MEMORY_BASIC_INFORMATION prefix and zeroes the rest; the working-set size goes at +8 with a TRUE return; allocator alignment and size-0 behaviour are documented.
- d3d12_xs: copy root-signature descriptors and clear the Xbox high flag byte (0x03000000) before forwarding to the PC serializer; add the legacy serializer; keep the deserializer.
- d3d12_xs: add D3D12XboxCreateDevice, create a PC device and log the 24-word params block. The Xbox ABI proxy is not implemented; measured slot overrides are documented as TODO and logged at runtime.
- test: root_sig_test serializes synthetic flags=0x03000000 descriptors through the PC d3d12.dll, checks the untouched copy is rejected and the cleared copy is accepted, and exercises the registered shim.

Clean-room: implemented from an ABI-facts spec derived from public write-ups; no source from americas-recomp or AnyPS5 was read or copied.
2026-11-21 17:23:05 +00:00

91 lines
3.9 KiB
C++

// xmem.dll: the GDK's tracked allocator. Mapped onto the CRT's aligned heap and VirtualQueryEx.
//
// The measured ABI (docs from the clean-room spec) shows four imports:
// XMemAlloc(size, attributes)
// XMemFree(ptr, attributes)
// XMemVirtualQuery(process, address, out64)
// XMemGetWorkingSetStatistics(out)
//
// ASSUMPTION: the attribute word is not decoded by the measured call sites (one passes 0x5e50000). Until a call
// site shows the alignment/type fields, every block is 64-byte aligned (covers cache lines and SIMD) and a size of
// zero is rounded up to one byte so the result is a stable, non-null pointer that XMemFree can release.
#include <windows.h>
#include <malloc.h>
#include <psapi.h>
#include <atomic>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include "shims.h"
namespace {
constexpr size_t kAlign = 64;
void* __stdcall XMemAlloc(size_t size, uint64_t /*attributes*/) { return _aligned_malloc(size ? size : 1, kAlign); }
void __stdcall XMemFree(void* p, uint64_t /*attributes*/) { _aligned_free(p); }
// Measured call sites pass a zeroed 64-byte structure. Only the MEMORY_BASIC_INFORMATION-compatible prefix is
// known: callers read at least the base and size fields. Fill that prefix from VirtualQueryEx and zero the rest.
constexpr size_t kVirtualQueryOutSize = 64;
static_assert(sizeof(MEMORY_BASIC_INFORMATION) <= kVirtualQueryOutSize,
"the 64-byte XMemVirtualQuery output must hold MEMORY_BASIC_INFORMATION");
// Returns the number of bytes written (the VirtualQuery convention) or 0 on failure. The exact return type and the
// meaning of every output field are not fully known; callers in the package only test for success.
SIZE_T __stdcall XMemVirtualQuery(HANDLE process, const void* address, void* out) {
if (!out) return 0;
std::memset(out, 0, kVirtualQueryOutSize);
if (!address) return 0;
static std::atomic<bool> warned{false};
if (!warned.exchange(true, std::memory_order_relaxed)) {
std::fprintf(stderr,
"[rt] xmem: XMemVirtualQuery: fields after MEMORY_BASIC_INFORMATION are unknown; "
"the 64-byte output is zero-filled there\n");
}
// A null handle means the current process; GetCurrentProcess() is the (HANDLE)-1 pseudo handle.
HANDLE target = process ? process : GetCurrentProcess();
MEMORY_BASIC_INFORMATION info{};
SIZE_T written = VirtualQueryEx(target, address, &info, sizeof(info));
if (!written) return 0;
std::memcpy(out, &info, written < kVirtualQueryOutSize ? written : kVirtualQueryOutSize);
return written;
}
// GDK: BOOL XMemGetWorkingSetStatistics(XMEM_WORKING_SET_STATISTICS*). The layout is not fully known. The FidelityFX
// upscaler reads a 64-bit size at +8 and treats FALSE as "no data", so only the first 16 bytes are touched: zero at
// +0 (unknown) and the process working set at +8. Do not claim more of the struct than is measured.
BOOL __stdcall XMemGetWorkingSetStatistics(void* stats) {
if (!stats) {
static std::atomic<bool> warned{false};
if (!warned.exchange(true, std::memory_order_relaxed)) {
std::fprintf(stderr, "[rt] xmem: XMemGetWorkingSetStatistics(nullptr) -> FALSE\n");
}
return FALSE;
}
PROCESS_MEMORY_COUNTERS pmc{};
if (!GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) return FALSE;
uint8_t* bytes = static_cast<uint8_t*>(stats);
std::memset(bytes, 0, 16);
uint64_t working_set = pmc.WorkingSetSize;
std::memcpy(bytes + 8, &working_set, sizeof(working_set));
return TRUE;
}
} // namespace
SHIM_TABLE(xmem,
{"xmem.dll", "XMemAlloc", reinterpret_cast<void*>(&XMemAlloc)},
{"xmem.dll", "XMemFree", reinterpret_cast<void*>(&XMemFree)},
{"xmem.dll", "XMemVirtualQuery", reinterpret_cast<void*>(&XMemVirtualQuery)},
{"xmem.dll", "XMemGetWorkingSetStatistics", reinterpret_cast<void*>(&XMemGetWorkingSetStatistics)},
);