Files
betterxen/tests/conformance/test_xcrdapi_xbox/main.cpp
T
ates 2736f24ca2 XCRDAPI: Xbox DirectStorage slots follow dstorage_xs.h; partial zlib reads are bounded (#769)
Factory slots 3 CreateQueue, 6 SetCpuAffinity, 7 SetDebugFlags, 9
GetStats and 10 CreateQueue1; file slot 4 GetHandle; queue slots 10
RetrieveErrorRecord, 11 Query, 12 EnqueueSetEvent and 13 Query1, with
the X/X1/X2 IIDs. Requests are read as the 0x48-byte DSTORAGE_REQUEST.
A partial zlib read decodes through a 32 KiB window instead of a buffer
of 64 times the destination.

Signed-off-by: ates <ahmetrizaates@hotmail.com>
2026-12-02 05:08:20 +03:00

652 lines
30 KiB
C++

/*
* Synthetic conformance test for the inferred Xbox Series DirectStorage COM
* interfaces (IDStorageFactoryX, IDStorageFileX, IDStorageStatusArrayX,
* IDStorageQueueX) in XCRDAPI.dll.
*/
#include <windows.h>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
#include "bx_log.h"
/* Public DirectStorage header (SDK 1.4.0, MIT): used here only for the
* PC-sized canary and the existing public DirectStorage error constants. */
#include "dstorage.h"
extern "C" __declspec(dllimport) HRESULT WINAPI DStorageGetFactory(REFIID riid, void** ppv);
struct XboxQueueDesc {
UINT64 source_type;
UINT16 capacity;
INT8 priority;
UINT8 reserved_pad[5];
PCSTR name;
UINT64 flags;
};
struct XboxRequest {
UINT64 options;
void* destination;
UINT64 destination_pad;
UINT32 destination_size;
UINT32 reserved1c;
void* source_ptr;
UINT64 file_offset;
UINT32 source_size;
UINT32 reserved34;
UINT64 cancellation_tag; /* DSTORAGE_REQUEST is 0x48 bytes (dstorage_xs.h) */
PCSTR name;
};
struct IXboxDStorageFile : IUnknown {
virtual void STDMETHODCALLTYPE Close() = 0;
virtual HANDLE STDMETHODCALLTYPE GetHandle() = 0;
};
struct IXboxDStorageStatusArray : IUnknown {
virtual bool STDMETHODCALLTYPE IsComplete(UINT32 index) = 0;
virtual HRESULT STDMETHODCALLTYPE GetHResult(UINT32 index) = 0;
};
struct IXboxDStorageQueue : IUnknown {
virtual void STDMETHODCALLTYPE EnqueueRequest(const XboxRequest* request) = 0;
virtual void STDMETHODCALLTYPE EnqueueStatus(IXboxDStorageStatusArray* status_array, UINT32 index) = 0;
virtual void STDMETHODCALLTYPE EnqueueSignal(void* fence, UINT64 value) = 0;
virtual void STDMETHODCALLTYPE Submit() = 0;
virtual void STDMETHODCALLTYPE CancelRequestsWithTag(UINT64 mask, UINT64 value) = 0;
virtual void STDMETHODCALLTYPE Close() = 0;
virtual HANDLE STDMETHODCALLTYPE GetErrorEvent() = 0;
virtual void STDMETHODCALLTYPE RetrieveErrorRecord(void* record) = 0;
virtual void STDMETHODCALLTYPE Query(void* info) = 0;
};
struct IXboxDStorageFactory : IUnknown {
virtual HRESULT STDMETHODCALLTYPE CreateQueue(const void* desc, REFIID riid, void** ppv) = 0;
virtual HRESULT STDMETHODCALLTYPE OpenFile(const WCHAR* path, REFIID riid, void** ppv) = 0;
virtual HRESULT STDMETHODCALLTYPE CreateStatusArray(UINT32 capacity, PCSTR name, REFIID riid, void** ppv) = 0;
virtual HRESULT STDMETHODCALLTYPE SetCpuAffinity(UINT64 affinity) = 0;
virtual void STDMETHODCALLTYPE SetDebugFlags(UINT32 flags) = 0;
virtual HRESULT STDMETHODCALLTYPE SetStagingBufferSize(UINT32 size) = 0;
virtual void STDMETHODCALLTYPE GetStats(void* stats) = 0;
virtual HRESULT STDMETHODCALLTYPE CreateQueueX(const XboxQueueDesc* desc, REFIID riid, void** ppv) = 0;
};
namespace {
constexpr IID kXboxFactoryIid{0x62959d11, 0xd45f, 0x4884, {0xa0, 0xa3, 0xf7, 0x86, 0x2e, 0xa2, 0x43, 0xae}};
constexpr IID kXboxFileIid{0x43704f9f, 0x27c6, 0x4a9f, {0x88, 0x15, 0xd1, 0x65, 0x27, 0x89, 0x4d, 0xf4}};
constexpr IID kXboxStatusArrayIid{0xcd66d752, 0x2786, 0x44f7, {0x90, 0x70, 0xe2, 0xa6, 0xfd, 0xad, 0x3f, 0x1b}};
constexpr IID kXboxQueueIid{0x3c02cb0c, 0xc050, 0x4195, {0x99, 0xa1, 0xa1, 0xce, 0x94, 0xba, 0x33, 0xc5}};
int g_failed = 0;
void check(bool cond, const char* fmt, ...) {
if (cond) return;
++g_failed;
va_list args;
va_start(args, fmt);
std::fprintf(stderr, "FAIL: ");
std::vfprintf(stderr, fmt, args);
std::fprintf(stderr, "\n");
va_end(args);
}
bool wait_complete(IXboxDStorageStatusArray* status, UINT32 index) {
for (int i = 0; i < 500; ++i) {
if (status->IsComplete(index)) return true;
Sleep(10);
}
return false;
}
using GuardCounts = uint64_t (*)();
struct GuardObserver {
HMODULE module = nullptr;
GuardCounts counts = nullptr;
bool load() {
wchar_t executable[MAX_PATH]{};
const DWORD length = GetModuleFileNameW(nullptr, executable, MAX_PATH);
if (!length || length >= MAX_PATH || GetModuleHandleW(L"xmem.dll")) return false;
const std::wstring path(executable, length);
const size_t separator = path.find_last_of(L"\\/");
if (separator == std::wstring::npos) return false;
const std::wstring library = path.substr(0, separator + 1) + L"xcrdapi_guard_fixture\\xmem.dll";
module = LoadLibraryExW(library.c_str(), nullptr,
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
if (!module) return false;
counts = reinterpret_cast<GuardCounts>(reinterpret_cast<void*>(GetProcAddress(module, "bx_test_guard_counts")));
return counts != nullptr;
}
~GuardObserver() {
if (module) FreeLibrary(module);
}
void expect(uint64_t before, uint32_t expected, const char* operation) const {
const uint64_t after = counts();
const auto entered = static_cast<uint32_t>((after >> 32) - (before >> 32));
const auto left = static_cast<uint32_t>(after) - static_cast<uint32_t>(before);
check(entered == expected && left == expected, "%s guard count: enter=%u leave=%u expected=%u",
operation, entered, left, expected);
}
};
/* Reads only what this run appended: bx-cli run appends to a supplied BX_LOG_FILE,
* so records from an earlier run must not satisfy these checks. */
long g_log_start = 0;
long log_size(const char* path) {
FILE* f = std::fopen(path, "rb");
if (!f) return 0;
std::fseek(f, 0, SEEK_END);
const long size = std::ftell(f);
std::fclose(f);
return size < 0 ? 0 : size;
}
bool file_contains(const char* path, const char* needle) {
FILE* f = std::fopen(path, "rb");
if (!f) return false;
std::fseek(f, g_log_start, SEEK_SET);
std::string text;
char buf[4096];
size_t n = 0;
while ((n = std::fread(buf, 1, sizeof(buf), f)) > 0) {
text.append(buf, n);
}
std::fclose(f);
return text.find(needle) != std::string::npos;
}
} // namespace
/* "tile %04d ok;" for i % 97, i = 0..399 (5200 bytes), zlib-compressed at level 9 by CPython's
* zlib module; a dynamic-Huffman deflate block. */
constexpr size_t kZlibPlainSize = 5200;
const uint8_t kZlibStream[] = {
0x78, 0xda, 0xed, 0xd4, 0x2b, 0x4e, 0x05, 0x40, 0x10, 0x44, 0xd1, 0xad, 0xbc, 0x25, 0xd0, 0xf3,
0xe9, 0x9e, 0x09, 0xeb, 0x41, 0x10, 0x48, 0x30, 0x6f, 0xff, 0x01, 0x81, 0x38, 0x68, 0x6c, 0x97,
0x2b, 0x77, 0xd4, 0x7d, 0xbe, 0x7f, 0xbe, 0x3d, 0x5e, 0x7e, 0xf6, 0xf8, 0xfa, 0x78, 0x7d, 0xfe,
0x9e, 0xf0, 0x0c, 0xcf, 0xf4, 0x2c, 0xcf, 0xf6, 0xa4, 0xa7, 0x3c, 0xc7, 0x73, 0x39, 0xa1, 0x20,
0x14, 0x84, 0x82, 0x50, 0x10, 0x0a, 0x42, 0x41, 0x28, 0x08, 0x05, 0xa1, 0x20, 0x14, 0x0c, 0x05,
0x43, 0xc1, 0x50, 0x30, 0x14, 0x0c, 0x05, 0x43, 0xc1, 0x50, 0x30, 0x14, 0x0c, 0x05, 0x43, 0xc1,
0x54, 0x30, 0x15, 0x4c, 0x05, 0x53, 0xc1, 0x54, 0x30, 0x15, 0x4c, 0x05, 0x53, 0xc1, 0x54, 0x30,
0x15, 0x2c, 0x05, 0x4b, 0xc1, 0x52, 0xb0, 0x14, 0x2c, 0x05, 0x4b, 0xc1, 0x52, 0xb0, 0x14, 0x2c,
0x05, 0x4b, 0xc1, 0x56, 0xb0, 0x15, 0x6c, 0x05, 0x5b, 0xc1, 0x56, 0xb0, 0x15, 0x6c, 0x05, 0x5b,
0xc1, 0x56, 0xb0, 0x15, 0xa4, 0x82, 0x54, 0x90, 0x0a, 0x52, 0x41, 0x2a, 0x48, 0x05, 0xa9, 0x20,
0x15, 0xa4, 0x82, 0x54, 0x50, 0x0a, 0x4a, 0x41, 0x29, 0x28, 0x05, 0xa5, 0xa0, 0x14, 0x94, 0x82,
0x52, 0x50, 0x0a, 0x4a, 0xc1, 0x51, 0x70, 0x14, 0x1c, 0x05, 0x47, 0xc1, 0x51, 0x70, 0x14, 0x1c,
0x05, 0x47, 0xc1, 0x51, 0x70, 0x14, 0x5c, 0x05, 0x57, 0xc1, 0x55, 0x70, 0x15, 0x5c, 0x05, 0x57,
0xc1, 0xfd, 0x53, 0xa4, 0x8e, 0x5d, 0xc7, 0xae, 0x63, 0xd7, 0xb1, 0xeb, 0xd8, 0x75, 0xec, 0x3a,
0x76, 0x1d, 0xbb, 0x8e, 0x5d, 0xc7, 0xae, 0x63, 0xd7, 0xb1, 0xeb, 0xd8, 0x75, 0xec, 0x3a, 0x76,
0xff, 0x8a, 0xdd, 0x37, 0x11, 0xdb, 0xee, 0x70,
};
int main() {
/* BX_LOG_FILE must name the log before launch (bx-cli run --log): the log checks
* below are the only evidence for the BX_VTABLE_STUB_LOG records. */
char log_path[MAX_PATH] = {};
const DWORD log_len = GetEnvironmentVariableA("BX_LOG_FILE", log_path, sizeof(log_path));
if (log_len == 0 || log_len >= sizeof(log_path)) {
std::fprintf(stderr, "FAIL: BX_LOG_FILE must be set before launch (run under bx-cli run --log)\n");
return 1;
}
g_log_start = log_size(log_path);
GuardObserver guard;
if (!guard.load() || !SetEnvironmentVariableW(L"BX_DSTORAGE_TRACE", L"1")) {
std::fprintf(stderr, "FAIL: unable to initialize the DirectStorage guard observer\n");
return 1;
}
IXboxDStorageFactory* factory = nullptr;
HRESULT hr = DStorageGetFactory(kXboxFactoryIid, reinterpret_cast<void**>(&factory));
check(hr == S_OK && factory != nullptr, "DStorageGetFactory(Xbox) must succeed (hr=0x%08lx)",
static_cast<unsigned long>(hr));
if (!factory) return 1;
/* Slot 8: SetStagingBufferSize (32 MiB, the size observed at target-build startup). */
hr = factory->SetStagingBufferSize(0x2000000u);
check(hr == S_OK, "SetStagingBufferSize(32 MiB) must return S_OK (hr=0x%08lx)", static_cast<unsigned long>(hr));
/* Negative queue creation checks. */
XboxQueueDesc bad_cap{};
bad_cap.source_type = 0;
bad_cap.capacity = 1;
IXboxDStorageQueue* bad_q = nullptr;
check(FAILED(factory->CreateQueueX(&bad_cap, kXboxQueueIid, reinterpret_cast<void**>(&bad_q))) && !bad_q,
"CreateQueueX with capacity 1 must fail");
XboxQueueDesc bad_src{};
bad_src.source_type = 2;
bad_src.capacity = 64;
check(FAILED(factory->CreateQueueX(&bad_src, kXboxQueueIid, reinterpret_cast<void**>(&bad_q))) && !bad_q,
"CreateQueueX with source_type 2 must fail");
/* Create temp file for file-to-memory read test. */
WCHAR temp_dir[MAX_PATH] = {};
WCHAR temp_file[MAX_PATH] = {};
check(GetTempPathW(MAX_PATH, temp_dir) > 0, "GetTempPathW must succeed");
check(GetTempFileNameW(temp_dir, L"bxx", 0, temp_file) != 0, "GetTempFileNameW must succeed");
uint8_t payload[256];
for (size_t i = 0; i < sizeof(payload); ++i) {
payload[i] = static_cast<uint8_t>((i * 37u + 11u) & 0xFFu);
}
HANDLE raw = CreateFileW(temp_file, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
check(raw != INVALID_HANDLE_VALUE, "CreateFileW for test payload must succeed");
if (raw != INVALID_HANDLE_VALUE) {
DWORD written = 0;
check(WriteFile(raw, payload, sizeof(payload), &written, nullptr) && written == sizeof(payload),
"WriteFile must write full payload");
CloseHandle(raw);
}
/* Slot 4: OpenFile. */
IXboxDStorageFile* file = nullptr;
hr = factory->OpenFile(temp_file, kXboxFileIid, reinterpret_cast<void**>(&file));
check(hr == S_OK && file != nullptr, "OpenFile(Xbox) must succeed (hr=0x%08lx)", static_cast<unsigned long>(hr));
if (file) {
/* Slot 4: GetHandle, a new GENERIC_READ handle the caller closes (dstorage_xs.h). */
HANDLE own = file->GetHandle();
LARGE_INTEGER size{};
check(own && own != INVALID_HANDLE_VALUE && GetFileSizeEx(own, &size) && size.QuadPart == sizeof(payload),
"GetHandle must return a handle reporting the exact payload size");
if (own && own != INVALID_HANDLE_VALUE) CloseHandle(own);
}
/* Slot 5: CreateStatusArray. */
IXboxDStorageStatusArray* status = nullptr;
hr = factory->CreateStatusArray(2, "xbox_test_status", kXboxStatusArrayIid, reinterpret_cast<void**>(&status));
check(hr == S_OK && status != nullptr, "CreateStatusArray(Xbox) must succeed (hr=0x%08lx)",
static_cast<unsigned long>(hr));
if (status) {
check(!status->IsComplete(0) && status->GetHResult(0) == E_PENDING,
"fresh status slot must be pending");
}
/* Slot 10: CreateQueueX (file source) and file-to-memory transfer. */
XboxQueueDesc file_q_desc{};
file_q_desc.source_type = 0;
file_q_desc.capacity = 64;
file_q_desc.priority = 0;
file_q_desc.name = "xbox_file_queue";
file_q_desc.flags = 1;
IXboxDStorageQueue* file_q = nullptr;
hr = factory->CreateQueueX(&file_q_desc, kXboxQueueIid, reinterpret_cast<void**>(&file_q));
check(hr == S_OK && file_q != nullptr, "CreateQueueX(file) must succeed (hr=0x%08lx)",
static_cast<unsigned long>(hr));
if (file_q && file && status) {
uint8_t dest[128] = {};
XboxRequest req{};
req.options = 0;
req.destination = dest;
req.destination_size = sizeof(dest);
req.source_ptr = file;
req.file_offset = 64;
req.source_size = sizeof(dest);
const uint64_t file_read_guards = guard.counts();
file_q->EnqueueRequest(&req);
file_q->EnqueueStatus(status, 0);
file_q->Submit();
check(wait_complete(status, 0), "file read status must complete");
check(status->GetHResult(0) == S_OK, "file read status must be S_OK (hr=0x%08lx)",
static_cast<unsigned long>(status->GetHResult(0)));
check(std::memcmp(dest, payload + 64, sizeof(dest)) == 0, "file read bytes must match payload[64..192]");
guard.expect(file_read_guards, 2, "file read and destination trace");
/* DSTORAGE_REQUEST_OPTIONS.BcpackMode (bits 1-4) has no decoder: E_NOTIMPL and a stub log. */
XboxRequest bcpack_req = req;
bcpack_req.options = 1ull << 1;
file_q->EnqueueRequest(&bcpack_req);
file_q->EnqueueStatus(status, 1);
file_q->Submit();
check(wait_complete(status, 1), "BCPack request status must complete");
check(status->GetHResult(1) == E_NOTIMPL, "BCPack request must report E_NOTIMPL (hr=0x%08lx)",
static_cast<unsigned long>(status->GetHResult(1)));
/* A zlib request must be smaller than its output (GDK DirectStorage compression rules). */
XboxRequest same_size = req;
same_size.options = 1;
file_q->EnqueueRequest(&same_size);
file_q->EnqueueStatus(status, 1);
file_q->Submit();
check(wait_complete(status, 1), "same-size zlib request status must complete");
check(status->GetHResult(1) == E_DSTORAGE_INVALID_DESTINATION_SIZE,
"a zlib request whose source is not smaller than its destination must be rejected (hr=0x%08lx)",
static_cast<unsigned long>(status->GetHResult(1)));
/* A zlib request whose source is a file, at a non-zero offset (how titles stream packed
* data): the stream is read from the file and inflated into the destination. */
WCHAR zlib_file[MAX_PATH] = {};
check(GetTempFileNameW(temp_dir, L"bxz", 0, zlib_file) != 0, "GetTempFileNameW (zlib) must succeed");
HANDLE zraw = CreateFileW(zlib_file, GENERIC_WRITE, 0, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);
check(zraw != INVALID_HANDLE_VALUE, "CreateFileW for the zlib payload must succeed");
if (zraw != INVALID_HANDLE_VALUE) {
uint8_t prefix[100];
std::memset(prefix, 0xCC, sizeof(prefix));
DWORD written = 0;
check(WriteFile(zraw, prefix, sizeof(prefix), &written, nullptr) && written == sizeof(prefix) &&
WriteFile(zraw, kZlibStream, sizeof(kZlibStream), &written, nullptr) &&
written == sizeof(kZlibStream),
"WriteFile must write the zlib payload");
CloseHandle(zraw);
}
IXboxDStorageFile* zfile = nullptr;
hr = factory->OpenFile(zlib_file, kXboxFileIid, reinterpret_cast<void**>(&zfile));
check(hr == S_OK && zfile, "OpenFile(zlib payload) must succeed (hr=0x%08lx)", static_cast<unsigned long>(hr));
if (zfile) {
static char expected_text[kZlibPlainSize + 1];
for (int i = 0, at = 0; i < 400; ++i) {
at += std::snprintf(expected_text + at, sizeof(expected_text) - at, "tile %04d ok;", i % 97);
}
static uint8_t file_inflated[kZlibPlainSize];
XboxRequest fz{};
fz.options = 1; // ZlibDecompress, file source
fz.destination = file_inflated;
fz.destination_size = kZlibPlainSize;
fz.source_ptr = zfile;
fz.file_offset = 100;
fz.source_size = sizeof(kZlibStream);
const uint64_t file_inflate_guards = guard.counts();
file_q->EnqueueRequest(&fz);
file_q->EnqueueStatus(status, 1);
file_q->Submit();
check(wait_complete(status, 1) && status->GetHResult(1) == S_OK,
"file-source zlib request must succeed (hr=0x%08lx)", static_cast<unsigned long>(status->GetHResult(1)));
check(std::memcmp(file_inflated, expected_text, kZlibPlainSize) == 0,
"file-source zlib request must inflate to the expected text");
guard.expect(file_inflate_guards, 2, "file-source inflate and destination trace");
zfile->Close();
zfile->Release();
}
DeleteFileW(zlib_file);
/* DSTORAGE_ERROR_RECORD (dstorage_xs.h) is 656 bytes: FailureCount, then FirstFailure's
* HResult and CommandType. The corrupted zlib request above failed on this queue. */
unsigned char record[656 + 16];
std::memset(record, 0xa5, sizeof(record));
file_q->RetrieveErrorRecord(record);
UINT32 failures = 0;
HRESULT first_hr = S_OK;
std::memcpy(&failures, record, sizeof(failures));
std::memcpy(&first_hr, record + 4, sizeof(first_hr));
check(failures >= 1 && FAILED(first_hr), "Xbox RetrieveErrorRecord must report the failed request (count=%u hr=0x%08lx)",
failures, static_cast<unsigned long>(first_hr));
bool tail_untouched = true;
for (size_t i = 656; i < sizeof(record); ++i) tail_untouched = tail_untouched && record[i] == 0xa5;
check(tail_untouched, "Xbox RetrieveErrorRecord must write no more than 656 bytes");
file_q->Close();
file_q->Release();
}
/* Slot 10: CreateQueueX (memory source) and memory-to-memory transfer. */
XboxQueueDesc mem_q_desc{};
mem_q_desc.source_type = 1;
mem_q_desc.capacity = 64;
mem_q_desc.priority = 1;
mem_q_desc.name = "xbox_mem_queue";
mem_q_desc.flags = 1;
IXboxDStorageQueue* mem_q = nullptr;
hr = factory->CreateQueueX(&mem_q_desc, kXboxQueueIid, reinterpret_cast<void**>(&mem_q));
check(hr == S_OK && mem_q != nullptr, "CreateQueueX(memory) must succeed (hr=0x%08lx)",
static_cast<unsigned long>(hr));
if (mem_q && status) {
uint8_t dest[64] = {};
XboxRequest req{};
req.options = 0x400000ull;
req.destination = dest;
req.destination_size = sizeof(dest);
req.source_ptr = payload + 32;
req.source_size = sizeof(dest);
mem_q->EnqueueRequest(&req);
mem_q->EnqueueStatus(status, 0);
mem_q->Submit();
check(wait_complete(status, 0), "memory copy status must complete");
check(status->GetHResult(0) == S_OK, "memory copy status must be S_OK (hr=0x%08lx)",
static_cast<unsigned long>(status->GetHResult(0)));
check(std::memcmp(dest, payload + 32, sizeof(dest)) == 0, "memory copy bytes must match payload[32..96]");
/* ZlibDecompress (bit 0): an RFC 1950 stream produced by an independent zlib encoder,
* checked against plain text this test builds itself. */
static char expected[kZlibPlainSize + 1];
for (int i = 0, at = 0; i < 400; ++i) at += std::snprintf(expected + at, sizeof(expected) - at, "tile %04d ok;", i % 97);
static uint8_t inflated[kZlibPlainSize];
XboxRequest zreq{};
zreq.options = 0x400000ull | 1ull;
zreq.destination = inflated;
zreq.destination_size = kZlibPlainSize;
zreq.source_ptr = const_cast<uint8_t*>(kZlibStream);
zreq.source_size = sizeof(kZlibStream);
const uint64_t mem_inflate_guards = guard.counts();
mem_q->EnqueueRequest(&zreq);
mem_q->EnqueueStatus(status, 1);
mem_q->Submit();
check(wait_complete(status, 1) && status->GetHResult(1) == S_OK, "zlib request must succeed (hr=0x%08lx)",
static_cast<unsigned long>(status->GetHResult(1)));
check(std::memcmp(inflated, expected, kZlibPlainSize) == 0, "zlib request must inflate to the expected text");
guard.expect(mem_inflate_guards, 3, "compressed memory read, inflate, and destination trace");
/* DSTORAGE_QUEUE_OPTIONS.AutoSubmitDisabled (flags bit 0, authorized GDK documentation):
* enqueued work waits for Submit. Without it the queue submits by itself once half full
* (public DirectStorage behaviour). Observed through the destination bytes. */
auto copied = [&](const uint8_t* dst, DWORD wait_ms) {
for (DWORD waited = 0;; waited += 10) {
if (std::memcmp(dst, payload + 32, 16) == 0) return true;
if (waited >= wait_ms) return false;
Sleep(10);
}
};
for (const UINT64 flags : {UINT64{1}, UINT64{0}}) {
XboxQueueDesc qd{};
qd.source_type = 1;
qd.capacity = 4;
qd.priority = 1;
qd.name = flags ? "xbox_held_queue" : "xbox_auto_queue";
qd.flags = flags;
IXboxDStorageQueue* q = nullptr;
check(factory->CreateQueueX(&qd, kXboxQueueIid, reinterpret_cast<void**>(&q)) == S_OK && q,
"CreateQueueX(capacity 4, flags %llu) must succeed", static_cast<unsigned long long>(flags));
if (!q) continue;
static uint8_t held_dst[2][16];
std::memset(held_dst, 0, sizeof(held_dst));
for (auto& dst : held_dst) {
XboxRequest r{};
r.options = 0x400000ull;
r.destination = dst;
r.destination_size = sizeof(dst);
r.source_ptr = payload + 32;
r.source_size = sizeof(dst);
q->EnqueueRequest(&r);
}
if (flags) {
check(!copied(held_dst[1], 300), "AutoSubmitDisabled: two of four slots filled must not run before Submit");
q->Submit();
}
check(copied(held_dst[0], 5000) && copied(held_dst[1], 5000),
flags ? "AutoSubmitDisabled: requests must run after Submit"
: "without AutoSubmitDisabled a half-full queue must submit by itself");
q->Close();
q->Release();
}
/* A full AutoSubmitDisabled queue stalls the enqueuer until Submit is called on another
* thread; it must not submit by itself (documented DSTORAGE_QUEUE_OPTIONS behaviour). A
* capacity-4 queue holds 3 requests (one slot stays free, as for the public queues). A
* worker enqueues 5: the fourth waits, and no destination changes until this thread calls
* Submit. */
{
XboxQueueDesc qd{};
qd.source_type = 1;
qd.capacity = 4;
qd.priority = 1;
qd.name = "xbox_full_held_queue";
qd.flags = 1;
IXboxDStorageQueue* q = nullptr;
check(factory->CreateQueueX(&qd, kXboxQueueIid, reinterpret_cast<void**>(&q)) == S_OK && q,
"CreateQueueX(capacity 4, AutoSubmitDisabled) must succeed");
if (q) {
static uint8_t full_dst[5][16];
std::memset(full_dst, 0, sizeof(full_dst));
struct Job {
IXboxDStorageQueue* q;
const uint8_t* src;
volatile LONG enqueued;
} job{q, payload + 32, 0};
auto worker = [](void* arg) -> DWORD {
auto* j = static_cast<Job*>(arg);
for (auto& dst : full_dst) {
XboxRequest r{};
r.options = 0x400000ull;
r.destination = dst;
r.destination_size = sizeof(dst);
r.source_ptr = const_cast<uint8_t*>(j->src);
r.source_size = sizeof(dst);
j->q->EnqueueRequest(&r);
InterlockedIncrement(&j->enqueued);
}
return 0;
};
HANDLE thread = CreateThread(nullptr, 0, worker, &job, 0, nullptr);
Sleep(500);
bool untouched = true;
for (auto& dst : full_dst) untouched = untouched && !copied(dst, 0);
check(untouched, "a full AutoSubmitDisabled queue must not run anything before Submit");
check(job.enqueued == 3, "the enqueue past capacity must wait for Submit (enqueued=%ld)",
static_cast<long>(job.enqueued));
q->Submit(); // runs the three queued requests, freeing slots for the last two
check(thread && WaitForSingleObject(thread, 5000) == WAIT_OBJECT_0,
"Submit from another thread must release the waiting enqueue");
q->Submit(); // the last two requests
bool all = true;
for (auto& dst : full_dst) all = all && copied(dst, 5000);
check(all, "all five requests must run after Submit");
if (thread) CloseHandle(thread);
q->Close();
q->Release();
}
}
/* A corrupted stream fails with the DirectStorage decompression error, not silently. */
static uint8_t corrupted[sizeof(kZlibStream)];
std::memcpy(corrupted, kZlibStream, sizeof(corrupted));
corrupted[sizeof(corrupted) - 1] ^= 0xFF; // the Adler-32 trailer
zreq.source_ptr = corrupted;
mem_q->EnqueueRequest(&zreq);
mem_q->EnqueueStatus(status, 1);
mem_q->Submit();
check(wait_complete(status, 1) && status->GetHResult(1) == E_DSTORAGE_DECOMPRESSION_ERROR,
"a corrupted zlib stream must report E_DSTORAGE_DECOMPRESSION_ERROR (hr=0x%08lx)",
static_cast<unsigned long>(status->GetHResult(1)));
/* A rejected null fence still signals the queue's error event. */
mem_q->EnqueueSignal(nullptr, 1);
mem_q->Submit();
const HANDLE error_event = mem_q->GetErrorEvent();
check(error_event != nullptr && WaitForSingleObject(error_event, 5000) == WAIT_OBJECT_0,
"the error event must signal after the rejected signal is processed");
XboxRequest null_src = req;
null_src.source_ptr = nullptr;
mem_q->EnqueueRequest(&null_src);
mem_q->EnqueueStatus(status, 1);
mem_q->Submit();
check(wait_complete(status, 1) && status->GetHResult(1) == E_INVALIDARG,
"a null memory source must report E_INVALIDARG through queue status");
mem_q->Close();
mem_q->Release();
}
/* Error paths on a fresh file queue. The fixture checks exact rejected
* methods and HRESULTs in the fresh log without using an Xbox record layout. */
IXboxDStorageQueue* err_q = nullptr;
XboxQueueDesc err_desc = file_q_desc;
err_desc.name = "xbox_error_queue";
hr = factory->CreateQueueX(&err_desc, kXboxQueueIid, reinterpret_cast<void**>(&err_q));
check(hr == S_OK && err_q != nullptr, "CreateQueueX(error paths) must succeed (hr=0x%08lx)",
static_cast<unsigned long>(hr));
if (err_q && status) {
const HANDLE error_event = err_q->GetErrorEvent();
/* A memory-source request (option 0x400000, as on the memory queue above) on a
* file queue is rejected before its source pointer is touched. */
uint8_t dest[16] = {};
XboxRequest bad_src{};
bad_src.options = 0x400000ull;
bad_src.destination = dest;
bad_src.destination_size = sizeof(dest);
bad_src.source_ptr = payload;
bad_src.source_size = sizeof(dest);
err_q->EnqueueRequest(&bad_src);
err_q->Submit();
check(error_event != nullptr && WaitForSingleObject(error_event, 5000) == WAIT_OBJECT_0,
"error event must signal for a memory request on a file queue");
/* A source/destination size mismatch on an otherwise valid file request. */
XboxRequest mismatch{};
mismatch.destination = dest;
mismatch.destination_size = sizeof(dest);
mismatch.source_ptr = file;
mismatch.source_size = sizeof(dest) - 1;
err_q->EnqueueRequest(&mismatch);
err_q->Submit();
/* A status index past the array's capacity (2). */
err_q->EnqueueStatus(status, 5);
err_q->Submit();
/* An enqueue after Close is dropped and logged, not run. */
err_q->Close();
uint8_t after_close[16] = {};
XboxRequest late{};
late.destination = after_close;
late.destination_size = sizeof(after_close);
late.source_ptr = file;
late.source_size = sizeof(after_close);
err_q->EnqueueRequest(&late);
err_q->Release();
}
if (status) status->Release();
if (file) {
file->Close();
file->Release();
}
factory->Release();
DeleteFileW(temp_file);
{
check(file_contains(log_path, "\"event\":\"dstorage_get_factory\",\"detail\":\"dispatch xbox_com=IDStorageFactoryX\""),
"log must record Xbox factory dispatch");
check(file_contains(log_path, "xbox size=33554432 has no effect"),
"log must record that Xbox SetStagingBufferSize has no effect");
check(file_contains(log_path, "dstorage_queue_closed"),
"log must record the request enqueued after Close as dropped");
check(file_contains(log_path, "IDStorageQueueX[3]::EnqueueRequest_bcpack"),
"log must record the BCPack request stub");
check(file_contains(log_path, "dstorage_zlib_failed"), "log must record the corrupted zlib stream");
const auto check_rejection = [&](const char* method, HRESULT hr) {
char needle[96];
std::snprintf(needle, sizeof(needle), "xbox %s hr=0x%08lx", method, static_cast<unsigned long>(hr));
check(file_contains(log_path, needle), "log must record: %s", needle);
};
check_rejection("EnqueueSignal", E_DSTORAGE_INVALID_FENCE);
check_rejection("EnqueueRequest", E_DSTORAGE_INVALID_SOURCE_TYPE);
check_rejection("EnqueueRequest", E_DSTORAGE_INVALID_DESTINATION_SIZE);
check_rejection("EnqueueStatus", E_DSTORAGE_INDEX_BOUND);
}
if (g_failed != 0) {
std::fprintf(stderr, "FAIL: test_xcrdapi_xbox (%d failed)\n", g_failed);
return 1;
}
std::printf("PASS: test_xcrdapi_xbox\n");
return 0;
}