Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,11 @@ releases the request. This affects scheduling and preserves the existing audio
routing and sample format.

With `X2W_AUDIO_PERMISSIVE=1`, `xapu.dll` tracks active handles across `XApuConnect`
and `XApuDisconnect`, refuses null or unreadable command headers (`E_POINTER`), null outputs
and `XApuDisconnect`. `XApuConnect` allocates a zeroed shared buffer of the size given by the
32-bit value at input `+0x0C` and writes its address to output `+0x00`. In one title's calls the caller read only that
pointer, and its later decode commands pointed into the buffer. A size of 0, or over a 64 MiB sanity cap, is refused
(`E_INVALIDARG`) without a handle, and the buffer is released on `XApuDisconnect`. It refuses null
or unreadable command headers (`E_POINTER`), null or unreadable inputs and null or unwritable outputs
(`E_POINTER`), and unknown or disconnected handles (`E_HANDLE`), and records one `(type, slot)`
entry per `XApuEnqueueCommand` call (reading the 32-bit command type at `+0x00` and 32-bit slot
index at `+0x04`) in a per-connection FIFO ring buffer capped at 16,384 entries (128 KiB per
Expand Down Expand Up @@ -2044,7 +2048,7 @@ code 77 means no usable render endpoint (or an unsupported mix format). It print
format and Windows build. A skip is neither a pass nor a failure. Every wait is bounded.

Not established: `XApuEnqueueCommand`/`XApuDequeueResult` payload fields beyond `(+0x00, +0x04, +0x34)`,
the 16-byte second output argument of `XApuConnect`, what is audible (no capture-path check), and any
the shared buffer's layout and output bytes past `+0x08` of `XApuConnect`, what is audible (no capture-path check), and any
hardware or driver glitch count.

## APU heap stand-in
Expand Down
32 changes: 26 additions & 6 deletions src/startup_stubs/xapu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

// Experimental (docs/startup-runtime.md). With X2W_AUDIO_PERMISSIVE=1 the exports
// stand in for the audio command queue; otherwise they trap. Call shapes observed:
// XApuConnect(input*, output*, handle*) -> HRESULT (the handle is later passed to the others)
// XApuConnect(input*, output*, handle*) -> HRESULT (the handle is later passed to the others). In one
// title's calls, the caller reads one pointer from output +0 and its later decode commands point into that
// buffer; the size is taken from the u32 at input +0x0C (0x104000 there). Not traced to every consumer.
// XApuGetResultCount(handle, 0) -> count
// XApuDequeueResult(handle, result*) -> HRESULT; zeroes 0x58 bytes. With the experimental
// X2W_XAPU_TYPED_COMPLETIONS=1, also echoes the command type at +0 and slot at +4.
Expand All @@ -22,10 +24,12 @@ constexpr long hr_ok = 0;
constexpr long hr_pointer = static_cast<long>(0x80004003); // E_POINTER
constexpr long hr_handle = static_cast<long>(0x80070006); // E_HANDLE
constexpr long hr_out_of_memory = static_cast<long>(0x8007000E); // E_OUTOFMEMORY
constexpr long hr_invalid_arg = static_cast<long>(0x80070057); // E_INVALIDARG
constexpr long hr_pending_results = static_cast<long>(0x8AC81001); // XAPU_E_PENDING_RESULTS
constexpr std::size_t command_header_size = 8;
constexpr std::size_t result_size = 0x58;
constexpr std::size_t max_queue_entries = 16384; // 128 KiB cap per connection for undrained commands
constexpr std::uint32_t max_buffer_bytes = 64u << 20; // sanity cap against a bad size, not a measured limit

bool permissive() noexcept { return x2w_local::env_on("X2W_AUDIO_PERMISSIVE"); }

Expand All @@ -41,6 +45,7 @@ struct Connection {
std::size_t head = 0;
std::size_t count = 0;
std::size_t capacity = 0;
void* buffer = nullptr; // returned in output +0
};

SRWLOCK connection_lock = SRWLOCK_INIT;
Expand Down Expand Up @@ -106,12 +111,25 @@ extern "C" __declspec(noinline) long XApuConnect(u64 rcx, u64 rdx, u64 r8, u64 r
char input[3 * 0x18 + 1], output[3 * 0x10 + 1];
hex(reinterpret_cast<const void*>(rcx), 0x18, input, sizeof(input));
hex(reinterpret_cast<const void*>(rdx), 0x10, output, sizeof(output));
if (!r8 || !x2w_local::writable(reinterpret_cast<const void*>(r8), sizeof(void*))) {
if (!r8 || !x2w_local::writable(reinterpret_cast<const void*>(r8), sizeof(void*)) || !rcx ||
!x2w_local::readable(reinterpret_cast<const void*>(rcx), 0x10) || !rdx ||
!x2w_local::writable(reinterpret_cast<const void*>(rdx), sizeof(void*))) {
x2w_local::logf("x2w-audio XApuConnect input=[%s] output(before)=[%s] -> E_POINTER", input, output);
return hr_pointer;
}
auto* conn = new (std::nothrow) Connection;
if (!conn) return hr_out_of_memory;
std::uint32_t buffer_bytes = 0;
std::memcpy(&buffer_bytes, reinterpret_cast<const unsigned char*>(rcx) + 0x0C, sizeof(buffer_bytes));
if (buffer_bytes == 0 || buffer_bytes > max_buffer_bytes) {
x2w_local::logf("x2w-audio XApuConnect input=[%s] buffer bytes 0x%x -> E_INVALIDARG", input, buffer_bytes);
return hr_invalid_arg;
}
void* buffer = VirtualAlloc(nullptr, buffer_bytes, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
auto* conn = buffer ? new (std::nothrow) Connection : nullptr;
if (!conn) {
if (buffer) VirtualFree(buffer, 0, MEM_RELEASE);
return hr_out_of_memory;
}
conn->buffer = buffer;
AcquireSRWLockExclusive(&connection_lock);
const u64 handle = next_handle++;
if (next_handle == 0) next_handle = 0x5841500000000001ULL;
Expand All @@ -125,9 +143,10 @@ extern "C" __declspec(noinline) long XApuConnect(u64 rcx, u64 rdx, u64 r8, u64 r
if (!timer_active) x2w_local::logf("x2w-audio 1 ms mixer timer request unavailable");
}
ReleaseSRWLockExclusive(&connection_lock);
*reinterpret_cast<void**>(rdx) = buffer;
*reinterpret_cast<void**>(r8) = reinterpret_cast<void*>(handle);
x2w_local::logf("x2w-audio XApuConnect input=[%s] output(before)=[%s] -> handle %p, S_OK", input, output,
reinterpret_cast<void*>(handle));
x2w_local::logf("x2w-audio XApuConnect input=[%s] output(before)=[%s] -> handle %p, buffer %p (0x%x bytes), S_OK",
input, output, reinterpret_cast<void*>(handle), buffer, buffer_bytes);
return hr_ok;
}

Expand Down Expand Up @@ -233,6 +252,7 @@ extern "C" __declspec(noinline) long XApuDisconnect(u64 rcx, u64 rdx, u64 r8, u6
x2w_local::logf("x2w-audio XApuDisconnect handle=0x%llx -> E_HANDLE", rcx);
return hr_handle;
}
VirtualFree(conn->buffer, 0, MEM_RELEASE);
delete[] conn->entries;
delete conn;
x2w_local::logf("x2w-audio XApuDisconnect handle=0x%llx -> S_OK", rcx);
Expand Down
40 changes: 39 additions & 1 deletion tests/startup_xapu_queue_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ constexpr long hr_ok = 0;
constexpr long hr_pointer = static_cast<long>(0x80004003);
constexpr long hr_handle = static_cast<long>(0x80070006);
constexpr long hr_out_of_memory = static_cast<long>(0x8007000E);
constexpr long hr_invalid_arg = static_cast<long>(0x80070057);
constexpr long hr_pending_results = static_cast<long>(0x8AC81001);
constexpr DWORD trap_connect_exit = 0xE2570300u;
constexpr std::size_t result_bytes = 0x58;
Expand Down Expand Up @@ -153,14 +154,33 @@ int main(int argc, char** argv)
return std::printf("FAIL: missing export\n"), 2;
}

const std::array<unsigned char, 0x18> input{};
// XApuConnect input as the observed caller passes it: the shared buffer size is the u32 at +0x0C.
constexpr std::uint32_t buffer_bytes = 0x104000;
const auto sized_input = [](std::uint32_t bytes) {
std::array<unsigned char, 0x18> in{};
std::memcpy(in.data() + 0x0C, &bytes, sizeof(bytes));
return in;
};
const auto input = sized_input(buffer_bytes);
std::array<unsigned char, 0x10> output{};
std::array<unsigned char, 0x60> cmd{};
std::array<unsigned char, result_bytes> result{};
void* bogus = reinterpret_cast<void*>(static_cast<std::uintptr_t>(0xDEADBEEFULL));

// Invalid-handle and null-pointer negative controls before any connection exists.
check(connect(input.data(), output.data(), nullptr) == hr_pointer, "XApuConnect(null handle*) -> E_POINTER");
void* refused = nullptr;
check(connect(nullptr, output.data(), &refused) == hr_pointer && !refused, "XApuConnect(null input) -> E_POINTER");
check(connect(input.data(), nullptr, &refused) == hr_pointer && !refused, "XApuConnect(null output) -> E_POINTER");
for (std::uint32_t bad_size : {0u, (64u << 20) + 1}) {
const auto bad_input = sized_input(bad_size);
output.fill(0x5A);
check(connect(bad_input.data(), output.data(), &refused) == hr_invalid_arg && !refused,
"XApuConnect(buffer size 0 or over 64 MiB) -> E_INVALIDARG without a handle");
bool untouched = true;
for (unsigned char b : output) untouched &= b == 0x5A;
check(untouched, "refused XApuConnect leaves its output alone");
}
check(get_count(nullptr, 0) == 0, "XApuGetResultCount(null) -> 0");
check(get_count(bogus, 0) == 0, "XApuGetResultCount(bogus) -> 0");
check(enqueue(nullptr, cmd.data(), 0, 0) == hr_handle, "XApuEnqueueCommand(null) -> E_HANDLE");
Expand All @@ -175,7 +195,22 @@ int main(int argc, char** argv)
std::array<unsigned char, result_bytes> sentinel;
sentinel.fill(0xA5);
void* h1 = nullptr;
output.fill(0x5A);
std::memset(output.data(), 0, sizeof(void*)); // an unwritten pointer reads as null, not as the 0x5A fill
check(connect(input.data(), output.data(), &h1) == hr_ok && h1 != nullptr, "XApuConnect h1 succeeds");
// The caller reads only output +0 and copies packets and samples into that buffer.
unsigned char* shared = nullptr;
std::memcpy(&shared, output.data(), sizeof(shared));
bool second_untouched = true;
for (std::size_t i = 8; i < output.size(); ++i) second_untouched &= output[i] == 0x5A;
check(shared != nullptr && second_untouched, "XApuConnect writes the shared buffer to output +0 and leaves +8 alone");
if (shared) {
bool zeroed = true;
for (std::uint32_t i = 0; i < buffer_bytes; ++i) zeroed &= shared[i] == 0;
check(zeroed, "shared buffer starts zeroed");
shared[0] = 1;
shared[buffer_bytes - 1] = 1;
}
check(get_count(h1, 0) == 0, "initial XApuGetResultCount == 0");
result = sentinel;
check(dequeue(h1, result.data()) == hr_pending_results, "empty queue XApuDequeueResult -> XAPU_E_PENDING_RESULTS");
Expand Down Expand Up @@ -322,6 +357,9 @@ int main(int argc, char** argv)
// Second independent handle + disconnect invalidation.
void* h2 = nullptr;
check(connect(input.data(), output.data(), &h2) == hr_ok && h2 != nullptr && h2 != h1, "XApuConnect h2 distinct");
unsigned char* shared2 = nullptr;
std::memcpy(&shared2, output.data(), sizeof(shared2));
check(shared2 != nullptr && shared2 != shared, "each connection gets its own shared buffer");
write_cmd(cmd, 1, 11);
check(enqueue(h1, cmd.data(), 0, 0) == hr_ok, "enqueue on h1 before disconnect");
write_cmd(cmd, 3, 21);
Expand Down
Loading