startup: return the shared buffer from XApuConnect #2280

Merged
NiiRoZz merged 2 commits from upstream-pr/2288 into main 2026-11-25 10:22:45 +00:00
NiiRoZz commented 2026-11-25 10:12:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by NiiRoZz on 2026-11-25T10:12:43Z.


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

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6602116848 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2288). Originally posted by [NiiRoZz](https://github.com/NiiRoZz) on 2026-11-25T10:12:43Z. --- 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`): ```powershell 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](https://claude.com/claude-code)
upstream-sync closed this pull request 2026-11-25 11:02:20 +00:00
tedin7 commented 2026-11-25 11:02:21 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T10:15:41Z.

Review: COMMENTED


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 (8131b6f9 to a854f21d: 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6055661203 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2288#pullrequestreview-6055661203). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-25T10:15:41Z. **Review:** COMMENTED --- 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 (8131b6f9 to a854f21d: 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!2280
No description provided.