mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 23:08:36 +00:00
amd_fidelityfx_upscaler_xbox.dll, measured with Ghidra 12.2 (through REA)
and run by fsr_dispatch_test on PC devices (RTX 2000 Ada and WARP) behind
the Xbox-ABI proxy.
Backend descriptor: the provider's create walks the chain for type
0x01000002, {header, device} with the device at +0x10, and ends in
FFX_API_RETURN_ERROR without it (what RT-3 got with 0x0200000x). With it
the DLL creates its backend through the proxy: device slots 14, 15, 18, 19,
27, 38 and 76 and heap slot 9, at the shapes the PC order and the measured
overrides predict; slot 76 takes the seven CreateCommittedResource
arguments. No earlier test here reached slot 76 or heap slot 9.
Two runtime bugs it found:
- XMemGetWorkingSetStatistics takes (kind, stats*): all seven call sites
pass kind 0x20000001 in ECX and a stack buffer in RDX. The one-argument
shim wrote to address 0x20000001.
- Device slot 67 is a console-only compute-pipeline call (a code blob in its
desc), not the PC CreateProtectedResourceSession1 the proxy forwarded it
to. The proxy now refuses it (E_NOTIMPL).
The context: on the RTX 2000 Ada the create stops at the DLL's HRESULT
check, because the driver refuses its R9G9B9E5_SHAREDEXP texture with
ALLOW_UNORDERED_ACCESS (WARP takes it). The test reports that stop and
retries with one PC stand-in (that texture as R32_UINT). On the context it
records one ffxDispatch into a logging command list that is never executed.
The console command list, from that run and the DLL's recorder (RVA
0x59a0), the same 13 calls on both devices: slots 14, 15, 25 and 26 at
their PC positions, then SetDescriptorHeaps at 32 (PC 28, seen in the run),
SetComputeRootDescriptorTable at 35 (PC 31), ClearUnorderedAccessViewUint
at 53 (PC 49, seen in the run), ExecuteIndirect at 61 (PC 59), and
console-only methods at 85 and 141. 33 and 41 fit SetComputeRootSignature
and SetComputeRootConstantBufferView by shape; the docs mark them inferred.
The logging list forwards only measured slots. The list object also carries
a write cursor and limits (+0x18, +0x28, +0x30) the DLL writes root
arguments and dispatches through while there is room; with the cursor at
the limit every command takes the vtable. These are the fields docs section
3.4 measured in the BVH builder without a meaning.
The dispatch stops at the DLL's compute pipeline: its 29 shaders are DXBC
with an ACCC part (payload tag FSPX) and no DXIL, and the PC runtime
refuses them at slot 62; the dispatch goes on with the null pipeline and
faults, which the test reports as the documented PC limit. Its C++ `throw`
also shows the loader gap: _CxxThrowException finds no image base for a
manually mapped image.
The test's fault report gives the faulting module and RVA, the access, the
C++ throw's ThrowInfo, image base and unwound stack, and the HRESULT the
DLL formatted with the DLL frames that made the failing call.
Docs: d3d12_vulkan.md Appendix A.3 and open question 2,
d3d12_command_objects.md 3.4 and 3.5a, design.md and runtime/README.md.
169 lines
6.6 KiB
C++
169 lines
6.6 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*);
|
|
// 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<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));
|
|
// 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<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;
|
|
}
|