mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 20:48:38 +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.
95 lines
4.3 KiB
C++
95 lines
4.3 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(kind, out) (kind 0x20000001 at all seven FSR call sites)
|
|
//
|
|
// 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;
|
|
}
|
|
|
|
// BOOL XMemGetWorkingSetStatistics(UINT32 kind, XMEM_WORKING_SET_STATISTICS* stats). Measured at all seven call sites
|
|
// in amd_fidelityfx_upscaler_xbox.dll (1.0.2.1; calls at RVAs 0x26b3, 0x2ea1, 0x39a9, 0x7452, 0x74d2, 0x7568,
|
|
// 0x75b1): ECX is the constant 0x20000001 (its meaning is not known) and RDX points at a stack buffer (0x80 bytes
|
|
// where the frame shows it). The caller reads a 64-bit size at +8 and treats a zero return as "no data". Taking the
|
|
// pointer from the first argument wrote to address 0x20000001.
|
|
// 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(uint32_t /*kind*/, 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)},
|
|
);
|