From 8131b6f93230a2285d8f24e7034052255cc2f7ce Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Wed, 25 Nov 2026 10:53:38 +0100 Subject: [PATCH 1/2] xapu: return the shared buffer from XApuConnect The observed caller reads one pointer from XApuConnect's output +0 and copies packets and samples into it; the buffer size is the u32 at input +0x0C. The permissive stand-in never wrote that pointer. This is the likely cause of the null-destination copy reported after the first typed result (#2242, #2268); one local replay with typed completions and this change did not reproduce that stop. Under X2W_AUDIO_PERMISSIVE=1, XApuConnect now allocates a zeroed buffer of that size, returns it in output +0 and frees it on XApuDisconnect. A size of 0 or over 64 MiB, or a null or unreadable input or null output, is refused without a handle. Typed completions stay opt-in. Refs #1887 Co-Authored-By: Claude Opus 5.5 --- docs/startup-runtime.md | 8 +++++-- src/startup_stubs/xapu.cpp | 32 +++++++++++++++++++++----- tests/startup_xapu_queue_check.cpp | 37 +++++++++++++++++++++++++++++- 3 files changed, 68 insertions(+), 9 deletions(-) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 3ec7ab2308..316834096f 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -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`; the observed caller reads only +that pointer and copies packets and samples into the buffer. A size of 0 or over 64 MiB 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 @@ -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 diff --git a/src/startup_stubs/xapu.cpp b/src/startup_stubs/xapu.cpp index 260db86338..7873195160 100644 --- a/src/startup_stubs/xapu.cpp +++ b/src/startup_stubs/xapu.cpp @@ -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). The caller +// reads one pointer from output +0, a shared buffer it copies packets and samples into; its size is the +// u32 at input +0x0C. // 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. @@ -22,10 +24,12 @@ constexpr long hr_ok = 0; constexpr long hr_pointer = static_cast(0x80004003); // E_POINTER constexpr long hr_handle = static_cast(0x80070006); // E_HANDLE constexpr long hr_out_of_memory = static_cast(0x8007000E); // E_OUTOFMEMORY +constexpr long hr_invalid_arg = static_cast(0x80070057); // E_INVALIDARG constexpr long hr_pending_results = static_cast(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; // the observed caller asks for 0x104000 bool permissive() noexcept { return x2w_local::env_on("X2W_AUDIO_PERMISSIVE"); } @@ -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; @@ -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(rcx), 0x18, input, sizeof(input)); hex(reinterpret_cast(rdx), 0x10, output, sizeof(output)); - if (!r8 || !x2w_local::writable(reinterpret_cast(r8), sizeof(void*))) { + if (!r8 || !x2w_local::writable(reinterpret_cast(r8), sizeof(void*)) || !rcx || + !x2w_local::readable(reinterpret_cast(rcx), 0x10) || !rdx || + !x2w_local::writable(reinterpret_cast(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(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; @@ -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(rdx) = buffer; *reinterpret_cast(r8) = reinterpret_cast(handle); - x2w_local::logf("x2w-audio XApuConnect input=[%s] output(before)=[%s] -> handle %p, S_OK", input, output, - reinterpret_cast(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(handle), buffer, buffer_bytes); return hr_ok; } @@ -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); diff --git a/tests/startup_xapu_queue_check.cpp b/tests/startup_xapu_queue_check.cpp index 4c39bbea93..77fa9bb17a 100644 --- a/tests/startup_xapu_queue_check.cpp +++ b/tests/startup_xapu_queue_check.cpp @@ -25,6 +25,7 @@ constexpr long hr_ok = 0; constexpr long hr_pointer = static_cast(0x80004003); constexpr long hr_handle = static_cast(0x80070006); constexpr long hr_out_of_memory = static_cast(0x8007000E); +constexpr long hr_invalid_arg = static_cast(0x80070057); constexpr long hr_pending_results = static_cast(0x8AC81001); constexpr DWORD trap_connect_exit = 0xE2570300u; constexpr std::size_t result_bytes = 0x58; @@ -153,7 +154,14 @@ int main(int argc, char** argv) return std::printf("FAIL: missing export\n"), 2; } - const std::array 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 in{}; + std::memcpy(in.data() + 0x0C, &bytes, sizeof(bytes)); + return in; + }; + const auto input = sized_input(buffer_bytes); std::array output{}; std::array cmd{}; std::array result{}; @@ -161,6 +169,18 @@ int main(int argc, char** argv) // 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"); @@ -175,7 +195,19 @@ int main(int argc, char** argv) std::array sentinel; sentinel.fill(0xA5); void* h1 = nullptr; + output.fill(0x5A); 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) { + check(shared[0] == 0 && shared[buffer_bytes - 1] == 0, "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"); @@ -322,6 +354,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); From a854f21d74b3132ea5c18181b4883c573b2c076e Mon Sep 17 00:00:00 2001 From: NiiRoZz Date: Wed, 25 Nov 2026 11:15:11 +0100 Subject: [PATCH 2/2] xapu: hedge the shared-buffer notes and fail the test cleanly without it Word the buffer contract as observed in one title's calls, call the 64 MiB limit a sanity cap, check the whole buffer is zeroed, and start the output pointer at null so a stand-in that never writes it fails a named check instead of the test dereferencing its fill pattern. Co-Authored-By: Claude Opus 5.5 --- docs/startup-runtime.md | 4 ++-- src/startup_stubs/xapu.cpp | 8 ++++---- tests/startup_xapu_queue_check.cpp | 5 ++++- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 316834096f..1a6582891d 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -2002,8 +2002,8 @@ routing and sample format. With `X2W_AUDIO_PERMISSIVE=1`, `xapu.dll` tracks active handles across `XApuConnect` 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`; the observed caller reads only -that pointer and copies packets and samples into the buffer. A size of 0 or over 64 MiB is refused +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)` diff --git a/src/startup_stubs/xapu.cpp b/src/startup_stubs/xapu.cpp index 7873195160..258b18ed84 100644 --- a/src/startup_stubs/xapu.cpp +++ b/src/startup_stubs/xapu.cpp @@ -10,9 +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). The caller -// reads one pointer from output +0, a shared buffer it copies packets and samples into; its size is the -// u32 at input +0x0C. +// 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. @@ -29,7 +29,7 @@ constexpr long hr_pending_results = static_cast(0x8AC81001); // XAPU_E_PEN 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; // the observed caller asks for 0x104000 +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"); } diff --git a/tests/startup_xapu_queue_check.cpp b/tests/startup_xapu_queue_check.cpp index 77fa9bb17a..af6d001579 100644 --- a/tests/startup_xapu_queue_check.cpp +++ b/tests/startup_xapu_queue_check.cpp @@ -196,6 +196,7 @@ int main(int argc, char** argv) 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; @@ -204,7 +205,9 @@ int main(int argc, char** argv) 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) { - check(shared[0] == 0 && shared[buffer_bytes - 1] == 0, "shared buffer starts zeroed"); + 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; }