Skip to content

startup: return the shared buffer from XApuConnect - #2288

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
NiiRoZz:audio/xapu-connect-buffer
Nov 25, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
NiiRoZz:audio/xapu-connect-buffer

Conversation

@NiiRoZz

@NiiRoZz NiiRoZz commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Refs #1887, #1145.

In one title's calls, the caller of XApuConnect(input*, output*, handle*) reads one pointer from output +0x00. Its later decode commands point into that buffer. The size comes from the 32-bit value at input +0x0C (0x104000 in those calls). The permissive stand-in never wrote the pointer.

#1887 reports a 160-byte copy to a null destination after the first typed result. In the same calls, each decode command copies a 160-byte packet. A local matched replay below reproduces that stop without this change and does not stop with it. I have not traced the copy instruction itself beyond the faulting call stack.

Change (X2W_AUDIO_PERMISSIVE=1 only)

  • XApuConnect allocates a zeroed buffer of the requested size, writes its address to output +0x00 and leaves output +0x08 onward untouched. XApuDisconnect frees the buffer.
  • These calls are refused without creating a handle, and the output is left untouched:
    • a size of 0, or over a 64 MiB sanity cap (E_INVALIDARG). The cap guards against a bad size and is not a measured limit.
    • a null or unreadable input, or a null or unwritable output (E_POINTER). This is a defensive choice, not an observed contract.
  • Typed completions stay opt-in (X2W_XAPU_TYPED_COMPLETIONS=1). The default and trap paths are unchanged.

Tests

Windows 11 build 26300, MSVC Release, head a854f21d (base 342d3fa9):

cmake --build --preset release --target startup_stub_xapu startup_xapu_queue_check
ctest --preset release --no-tests=error -R '^startup_xapu_queue$' -V
python -B tests/docs_links_check.py
python -B tests/bidi_controls_check.py
git diff --check

All passed. The new checks cover:

  • the size refusals (0 and 64 MiB + 1) and the null input and output refusals, each with an untouched-output guard;
  • the buffer appearing at output +0x00 with +0x08 onward untouched;
  • the whole buffer starting zeroed, and being writable at both ends;
  • a distinct buffer for a second connection.

Negative control: the same test executable against xapu.dll built from unmodified 342d3fa9 exits 1 with 6 failures. The first is FAIL: XApuConnect(null input) -> E_POINTER, followed by:

  • the two size refusals;
  • XApuConnect writes the shared buffer to output +0 and leaves +8 alone;
  • each connection gets its own shared buffer.

Matched replay (local observation, one title, one run each)

Same machine (Windows 11 build 26300) and replay path. The only difference between the builds is xapu.dll. Both runs used X2W_AUDIO_PERMISSIVE=1 and X2W_XAPU_TYPED_COMPLETIONS=1.

xapu.dll First dequeue Result
342d3fa9 (no buffer) returned S_OK Second-chance access violation 0xC0000005. It is a write to address 0 inside vcruntime140.dll (memcpy), called from the title's audio decode path about 80 s after launch. No screen after the first.
342d3fa9 + this PR returned S_OK The log shows XApuConnect ... buffer 0x... (0x104000 bytes), S_OK. No exception. The run then slowed and stalled at an early screen.

The stall with typed completions looks like a separate decode contract. 23 of the first 24 logged commands were decodes (type 3) on one slot. My hypothesis is that a typed decode result reporting zero decoded bytes makes the caller request the next decode at once. Decoding stays unimplemented here and typed completions stay opt-in, so that is out of scope.

With typed completions unset, this PR also returned the buffer and raised no exception. That run shared the machine with other load and stalled at a later screen, so I don't attribute any timing to this change.

I'm not claiming anything audible. #2107 also touches xapu.cpp and docs/startup-runtime.md; whichever lands second will need a merge.

🤖 Generated with Claude Code

NiiRoZz and others added 2 commits November 25, 2026 11:12
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 (R0Y00#2242, R0Y00#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 R0Y00#1887

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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 <noreply@anthropic.com>

@tedin7 tedin7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of head a854f21d74b3132ea5c18181b4883c573b2c076e.

Scope: XApuConnect now allocates a zeroed buffer (size from input +0x0C), writes it to output +0x00, and XApuDisconnect frees it. Permissive mode only.

Commands run locally (Linux, in a worktree of the PR head):

  • git diff --stat origin/main...HEAD: 3 files, +68/-9 (docs, xapu.cpp, startup_xapu_queue_check.cpp).
  • python3 tests/docs_links_check.py: self-test passed, 18 Markdown files and 435 links.
  • python3 tests/bidi_controls_check.py: 1576 files, no bidirectional controls.
  • git diff --check origin/main...HEAD: clean.
  • git merge-tree of this head with current main: clean. Against open #2107 (also edits xapu.cpp, xapu.def, docs/startup-runtime.md): also clean, no textual conflict.
  • gh pr checks: Linux Python checks pass; the Windows build/test jobs were still pending when I looked.

Not run: the C++ test is Windows-only (VirtualAlloc, xapu.dll), so I could not run it or do a logic mutation on Linux. I did not queue the Windows VM. The Windows CI jobs and the PR's own negative control are the evidence for the test.

Findings

  • P1: none.
  • P2: none.
  • nit 1 (tests/startup_xapu_queue_check.cpp): no check covers that XApuDisconnect releases the buffer. A regression that drops the VirtualFree (src/startup_stubs/xapu.cpp, in XApuDisconnect) would leak up to 64 MiB per connection and no test would fail. A connect/disconnect loop that watches the private bytes or commit charge would guard it. Not blocking.
  • nit 2 (src/startup_stubs/xapu.cpp, XApuConnect): the buffer pointer is written to rdx only after the connection is published and the lock is released. The handle is not yet returned to the caller at that point, so this is safe. A short comment would keep a later edit from moving the publish earlier.
  • nit 3: the head moved during review (8131b6f to a854f21: hedged comments and docs, full-buffer zero check, null-pointer reset in the test). I re-read that delta and re-ran the docs and diff checks on the new head; no new findings.

Overlap check: #2268 (merged) made typed completions opt-in, and this PR does not change that default. #2107 (open) adds XMA/XApu activity counters in the same files. There is no textual conflict, but whichever lands second should rebase and re-run the xapu CTest targets, because both touch XApuConnect/XApuDisconnect.

Clean-room and wording: title and body are neutral, with no private-source or local-probe references. The PR's claims are limited to what the diff does. The unverified decode-stall observation is clearly marked out of scope.

Correctness: the size bounds (0 and over 64 MiB give E_INVALIDARG), the pointer checks (E_POINTER with output untouched), the cleanup of the buffer when the Connection allocation fails, and the VirtualAlloc zero-fill all match the PR description.

AI-assisted; commands listed were executed locally.

@R0Y00
R0Y00 merged commit 5adfdba into R0Y00:main Nov 25, 2026
5 checks passed
JohnConnorNPC added a commit to JohnConnorNPC/Xbox2Windows that referenced this pull request Nov 25, 2026
… size

XApuConnect now refuses a size of 0 at input +0x0C (R0Y00#2288), so the fixture's
commands all landed on an unknown handle.
R0Y00 pushed a commit that referenced this pull request Nov 28, 2026
…nd-ins (#2107)

* docs: close an open code fence in experiments.md

The cpu_native_acphal_hal example block had no closing fence, so the
ApuFree section after it, and the text up to the next bare fence, rendered
as code and their links were not checked.

* gh #1145 atom 1: count XMA context and XApu command activity in the stand-ins

Opt-in with X2W_AUDIO_ACTIVITY=1. AcpHal watches the XMA context array it
allocates and counts contexts filled, changed and cleared, plus HAL slot2
submits by type; xapu counts XApuEnqueueCommand calls by command class with
no cap. Each DLL logs a line per second when a count moved and has an
experimental export with the totals. Off, nothing runs.

* gh #1145 atom 2: add startup_acphal_xma_activity

A source-built caller fills, changes and clears XMA contexts, submits HAL
commands and enqueues XApu commands. The on case checks the exact counts
from the exports and the logged lines, and that a freed and reused array
is no longer counted; the none case writes only next to the contexts and
must count 0; the off case reports nothing. Results, outputs, last errors
and context bytes must match with the counter on and off.

* gh #1145 atom 3: document the audio activity counter

Adds the X2W_AUDIO_ACTIVITY switch, an Activity counter section under the
APU heap stand-in with the line formats and limits, and an experiments
entry with the reproduction command for startup_acphal_xma_activity.

* gh #1145 review round 1: assert block reuse, share expectations, fit CTest timeout

The on and off cases now check that ApuAlloc of the same size returns the
freed context block, so the not-counted-after-reuse check cannot pass on
some other block. expect_on() replaces the repeated on/off expectation
lists without changing them. Each case waits 15 s, so all three fit in
the 60 s CTest timeout.

* gh #1145 review round 2107-1: observe activity inside calls, no sampler threads; add unload case

* gh #1145 review round 2107-2: start the line interval at the last written line; fix comments

* audio: count unreadable XApu commands on the early E_POINTER path

main's XApuEnqueueCommand now refuses a null or unreadable command header
with E_POINTER before the activity counter runs, so the unreadable count
never moved. Count those calls as unreadable on that path. A call refused
for a full ring (E_OUTOFMEMORY) is still counted by its command class.

* gh #1145 rebase: give the fixture's XApuConnect a nonzero buffer size

XApuConnect now refuses a size of 0 at input +0x0C (#2288), so the fixture's
commands all landed on an unknown handle.

* audio: count the short decode command that XApuEnqueueCommand refuses before the queue

In an X2W_WITH_OPUS build with typed completions, a type 3 command too short
to decode returned E_POINTER before count_command. Count it by its class, as
the other E_POINTER refusals after the header read are, and pin it in
startup_acphal_xma_activity with a type 3 header that ends where free address
space begins (X2W_PROBE_PRECHECK=1 refuses the tail without a fault).

---------

Co-authored-by: lewisalex86 <pigalot@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants