Skip to content

runtime: xapu.dll and chad.dll shims, connections that succeed with an empty result queue (RT-489) - #6981

Open
Jadiac5 wants to merge 3 commits into
ItsNotRexed:mainfrom
Jadiac5:runtime/xapu-chad-shims
Open

Jadiac5 wants to merge 3 commits into
ItsNotRexed:mainfrom
Jadiac5:runtime/xapu-chad-shims

Conversation

@Jadiac5

@Jadiac5 Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Item: RT-489, shims for xapu.dll and chad.dll (claimed in #6750 (comment)).

What the imports are for

xapu.dll is the console's audio processing unit (APU) command queue and chad.dll its audio DSP. #6715 lists
their ten imports among the 30 the retail executable calls with no binding. A PC has neither unit.

What the call sites show

Read statically from the executable (never run or mapped). Each import is reached through one jump stub; the
direct callers:

  • XApuConnect: 1 site, three pointer arguments (two outputs of 24 and 16 bytes, and a handle). Success is read as
    SUCCEEDED. The first qword of the 16-byte output is used later as the base of a CPU-visible buffer: stream input
    at a 0x280-byte stride from the base, stream output at a 0x1E00-byte stride from base + 0x14000. The 24-byte
    output is only ever zeroed.
  • XApuEnqueueCommand: 4 sites, (handle, pointer to a stack-built command, 0). The command's first dword is a type
    (1, 2 and 3 seen), the second a stream slot. The return value is never read.
  • XApuGetResultCount: 1 site, (handle, 0). 0 skips the dequeue loop.
  • XApuDequeueResult: 1 site, (handle, pointer to a zeroed 0x50-byte local), called count times; a negative
    result ends the loop.
  • XApuDisconnect: 1 site, one argument (the handle global, cleared after); the return value is not read.
  • XDspConnectWithMaximumStreamLimit: 1 site, six arguments (a buffer from the title's XMemAlloc, two dwords,
    0x80, 1, a handle output). Success is a return of 0.
  • XDspActivate: 1 site, five arguments, the handle first. Success is a return of 0, else the caller returns -1.
  • XDspSubmitCommand: 1 site, two arguments (the wrapper swaps them). Success is a return of 0.
  • XDspDeactivate, XDspDisconnect: 1 tail jump each; the caller returns what they return.

What each function does now

New file runtime/shims/xapu_chad.cpp, registered like xmem.dll (so owns_dll covers both DLLs for run-time
LoadLibrary too):

  • XApuConnect: allocates a zeroed shared buffer, writes {base, size} to the 16-byte output, zeroes the 24-byte
    output and returns S_OK with a handle. E_INVALIDARG without a handle pointer.
  • XApuEnqueueCommand: S_OK, the command is dropped.
  • XApuGetResultCount: 0. XApuDequeueResult: E_FAIL without writing.
  • XApuDisconnect: frees the buffer and the handle, S_OK.
  • XDspConnectWithMaximumStreamLimit: a handle and S_OK. XDspActivate, XDspSubmitCommand, XDspDeactivate:
    S_OK. XDspDisconnect: frees the handle, S_OK.
  • With --trace, the first call of each function and the first command of each type is an audio_queue event,
    with the caller.

The effect is silent audio: the title's buffers are accepted and nothing is rendered into them.

Hypotheses (also in the file's comment):

  • The buffer size, 0x14000 + 128 × 0x1E00 bytes. 128 slots is what fits 0x14000 at the 0x280 input stride, and the
    DSP connect passes 0x80 as its stream limit; no call site shows a bound.
  • The second qword of the 16-byte output as the size; only the base is read.
  • The empty result queue: no result layout is known beyond its first two dwords, so none is made up.
  • Which of XDspSubmitCommand's two arguments is the handle; neither is checked.

Tests

  • xapu_chad_test (new, in ctest as xapu_chad): 28 checks, 0 failures. It resolves all ten through the registry,
    checks owns_dll, the connect outputs, that the buffer is zeroed and writable at the end of slot 127, three
    command types, the empty result queue, and the DSP calls in their call-site shapes.
  • ctest xapu_chad, xmem, process_cmdline, file_system, unimplemented_policy, winhttp_offline: 6/6 passed.
    gta6rt builds with the new file.
  • python -I tests/test_docs_repo_paths.py: OK. python -I scripts/check_no_game_data.py: exit 0.

Not tested

  • Not run against the retail executable.
  • Whether the title waits for an APU result that never comes; the call sites read do not show a wait.

Related: #6899 documents the same call sites. It is docs only, so this PR does not depend on it. One difference:
it reads a second argument 0x20 for XApuDisconnect; here that 0x20 goes to an XMemFree call just before, and
the disconnect site loads only the handle.

Tested on Windows 11, MSVC 19.44, AMD Radeon RX 9070 XT, driver 32.0.32029.2010 (no GPU is used by these tests).

AI-assisted (Claude Code).

@Razuri0

Razuri0 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Static check at 8f8fb2b6, read from the executable only (SHA-256 e95020b2…8993); nothing executed or mapped. The call-site counts and shapes in the PR hold:

  • XApuConnect 1 site (0x43aee69): rcx = 0x149ae9890, rdx = 0x149ae98a8 (the second block starts 0x18 after the first), r8 = 0x149ae8960 (the handle). Return read as SUCCEEDED.
  • XApuEnqueueCommand 4, XApuGetResultCount 1, XApuDequeueResult 1.
  • XDspConnectWithMaximumStreamLimit 1 (0x5b5262e): rcx = [this+0x490], rdx = [this+0x498], r8 = the caller dword, r9 = 0x80, stack 1, stack this+0x488; success is eax == 0.
  • XDspActivate 1 (0x11d475d): handle first, five arguments; eax != 0 returns -1 to the caller.
  • XDspSubmitCommand 1 (0x4e5b20d): the wrapper swaps rcx/rdx before the call; success is eax == 0.
  • XDspDeactivate and XDspDisconnect are the two tail jumps, as stated.

The XApuDisconnect point is confirmed: the only site (0x43ae930) loads the handle into rcx and calls; it does not set rdx. The mov $0x20, %edx just before (0x43ae90a) feeds the XMemFree call on the path above, so this PR's correction to #6899 is the right reading of the call site.

No Windows ctest here, so this is static confirmation only. No game data: counts, offsets and RVAs only. AI-assisted.

@mrp1no

mrp1no commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 8f8fb2b6 on Windows 11, MSVC RelWithDebInfo, merged onto main bd3f061ad, against my own copy of the retail executable (outcomes only):

Details: #6910 (comment). AI-assisted (Claude Code).

…n empty result queue

The executable's ten APU and audio DSP imports (one call site each, four for XApuEnqueueCommand) now bind to runtime/shims/xapu_chad.cpp instead of the unimplemented thunk. Both connects return a handle; the APU connect also returns a zeroed shared buffer. Commands are accepted and dropped, the result count is 0 and dequeuing fails. xapu_chad_test calls all ten in the call-site shapes.
@Jadiac5
Jadiac5 force-pushed the runtime/xapu-chad-shims branch from 8f8fb2b to e6515ea Compare November 23, 2026 15:50
seblessa added a commit to seblessa/Leonida that referenced this pull request Nov 23, 2026
…d.dll shims)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

# Conflicts:
#	docs/runtime_imports.md
#	runtime/CMakeLists.txt
seblessa added a commit to seblessa/Leonida that referenced this pull request Nov 23, 2026
On top of ItsNotRexed#6981, from the public GDK reference (xapu.h, XDspAudio.h on
docs.xbox.com) checked against the executable's call sites (static read):

- XApuConnect takes the 24-byte block as input and leaves it alone; base data
  is baseDataLength bytes, zeroed; no result-ready event (the title does not
  ask for one).
- Every XApu command gets exactly one result with its id, visible from the
  second XApuGetResultCount after the enqueue (the title stores a stream's
  state only after the enqueue returns and polls from another job). Without
  results the title's stream slots never return to their free list.
- A process command is answered with one frame of silence in its output
  buffer, bounded by its capacity and the base data; the 0x50-byte result is
  the packed XApuResult, outputDataLength at +0x34. An empty queue is
  XAPU_E_PENDING_RESULTS.
- XDspActivate hands out a zeroed XDspStatus per stream (the voice path reads
  it with no NULL check); XDspSubmitCommand returns sequences 1, 2, ... and
  marks each processed in that block, which the title waits for before the
  next submit. Status blocks are pooled, never freed.
- The documented error codes for refused calls (stream limit, clients,
  invalid command or stream index, handles still active).
- build_runtime.ps1's cl.exe fallback lists acphal.cpp and xapu_chad.cpp,
  which tests/test_build_runtime_fallback.py requires.

xapu_chad_test drives all ten in the title's shapes, with commands against a
no-access page, and one thread enqueuing 20000 commands while another polls.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Jadiac5

Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main 5f47ed2b3 to clear the conflict in docs/runtime_imports.md (main added a section at the same place); no code change. Rebuilt and rerun on the new base: xapu_chad_test 28 checks, 0 failures. Windows 11, MSVC 19.44. Not run against the retail executable.

AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor

Tested 8f8fb2b merged with main e6190e7 on Windows 11 / RTX 4080 SUPER against own copy of the retail exe: pass. The new head e6515ea (the rebase) has the same runtime/ diff byte for byte and merges cleanly with e6190e7, so the result carries over.

  • Build: MSVC 14.51 (VS 18), Ninja, RelWithDebInfo, gta6rt + xapu_chad_test.
  • ctest -R xapu_chad: 1/1 passed.
  • gta6rt --dry-run (LEONIDA_D3D12=vulkan, fresh --trace, --trace-d3d12, --overlay to a scratch folder): main has 30 unimplemented imports (shim 53) and the PR has 20 (shim 63). The only differences are xapu.dll going from 5 to 0 and chad.dll from 5 to 0; the other 20 are unchanged.
  • Negative control: the same dry run on main lists all ten xapu.dll/chad.dll imports as unimplemented.
  • gta6rt --run with LEONIDA_UNIMPLEMENTED=stop: main and the PR stop in the same place. Both exit 3 at the first unimplemented import (d3d12_xs.dll!D3D12ConfigureVirtualMemoryX, d3d12_xs: D3D12ConfigureVirtualMemoryX shim (first unimplemented call of the title) #6792), after 2 TLS callbacks and 4/4 DllMain OK. A stop-mode run on main doesn't reach the xapu/chad calls yet, so the PR alone doesn't move --run.

@Spacecer2

Copy link
Copy Markdown

Tested e6515ea4 on Linux (clang-cl 23.1.5 + xwin Win11 SDK 10.0.26100 + MSVC 14.44, wine-11.22), applied alone to main 07f1d7e: builds, xapu_chad_test 28 checks 0 failures under wine, dry-run 30 → 20 unimplemented with the xapu/chad names gone. The APU buffer arithmetic inverts exactly (0x14000 = 128 * 0x280, 0xF0000 = 128 * 0x1E00) and is page-aligned, so a wrong slot count faults at the first over-range slot rather than corrupting a neighbour. One defect to fix before merge: *handle = new ApuConnection{buffer}; and *handle = new DspConnection{}; are unchecked, so a failed allocation returns S_OK with a null handle — the title reads SUCCEEDED and calls through it. Check the allocation, free the buffer, return E_OUTOFMEMORY with *handle = nullptr. Details and the comparison with the other three PRs are in the coordination issue.

@Jadiac5

Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the test and the finding. Fixed in b9540161.

  • XApuConnect and XDspConnectWithMaximumStreamLimit now allocate with new (std::nothrow). On failure they return E_OUTOFMEMORY and leave *handle null. XApuConnect also frees the buffer and no longer writes the two output blocks before the handle exists.
  • One correction to the description: the old plain new would not have returned null. It would have thrown std::bad_alloc into the title's frames, which is worse. The fix covers both.
  • xapu_chad_test: 28 checks, 0 failures on Windows 11, MSVC 19.44. gta6rt builds. No test forces the allocation to fail, so the new branch is checked by reading only.
  • Not run against the retail executable.

AI-assisted (Claude Code).

ErRusso added a commit to ErRusso/Leonida that referenced this pull request Nov 23, 2026
…shapes

- XGCreateTextureComputer: fix signature from 4 args (desc, device, riid,
  out) to the measured 2 args (desc, out**). The out pointer arrives in
  RDX, not R9 — the old 4-arg version wrote through a stale register
  as textureComputer, which the two callers that ignore the HRESULT
  would use as a null/wild vtable call. Now returns a static computer
  with a proper vtable (AddRef/Release for slot 0/1, E_NOTIMPL for
  all other slots with a one-time log).

- XGSuggestSwizzleMode: fix signature from 2 args (desc, mode*) to the
  measured 3 args (desc, flags, mode*). The mode* arrives in R8, not
  RDX, so *mode = 0 was previously writing to a wrong register. Now
  returns E_NOTIMPL and writes nothing (the caller already zeroed the
  field).

- AcpHalCreate: replace the non-dereferenceable id<<4 handle with a
  static object with a proper vtable (IAcpHal COM-like interface).
  Any method call through it logs once per slot and returns E_NOTIMPL
  instead of faulting.

Both fixes match the measured call shapes documented in the overlapping
PRs ItsNotRexed#6891 and ItsNotRexed#6981 and verified by reviewers.

AI-assisted: yes
@AirKyzzZ

Copy link
Copy Markdown
Contributor

Tested b95401617 (the head after the allocation fix) on RTX 2060 (NVIDIA 566.14), Windows 10 22H2, clang-cl 23.1.6 + lld-link (MSVC 14.29 STL), SDK 10.0.26100: pass

Built gta6rt and xapu_chad_test at the head as is, no clang-cl warnings from xapu_chad.cpp or its test. ctest -R "^(xapu_chad|winhttp_offline)$": 2/2 pass, xapu_chad_test 28 checks, 0 failures. The same test linked without shims/xapu_chad.cpp (as on main): all ten exports unregistered and the ownership check fails, so it is not model-only. Not run against the retail executable. No game files. AI-assisted (Claude Code).

@memorypressure

Copy link
Copy Markdown

Verified at your rebased head e6515ea: merges clean into 8575d36 (the old docs/runtime_imports.md conflict vs #6715 is gone). The shims register through SHIM_TABLE so owns_dll picks both DLLs up without loader changes, and the call shapes match the independently measured docs/audio_hal_exe_calls.md (#6899, tools/import_call_sites.py): 8 xapu + 5 chad sites, the 0x80 stream limit, result queue semantics all consistent; buffer-size guesses are honestly marked HYPOTHESIS. Three things before merge: (1) your own docs/runtime_imports.md:141-142 still says "the other 22 are in five DLLs ... with no shim at all" — that sentence now contradicts this PR (xapu and chad are shimmed here); fix its count and list in the same PR; (2) cite tools/import_call_sites.py for the "read statically from the executable's call sites" claim, matching #6899's convention; (3) XApuDisconnect's shape comment omits the measured second argument 0x20 that audio_hal_exe_calls.md records. C++ not buildable here; xapu_chad_test runs in the windows CI job. Checks pending. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run, C++ review-only).

@Spacecer2

Copy link
Copy Markdown

Re-tested at b9540161, same host as my first check (Linux, clang-cl 23.1.5 + xwin Windows SDK 10.0.26100, wine 11.22), applied alone to main e0816d6: builds, xapu_chad_test 28 checks 0 failures under wine, retail dry-run shim=64 ... unimplemented=19. The defect I flagged is fixed in the source: both handles now come from new (std::nothrow) with return *handle ? S_OK : E_OUTOFMEMORY, and XApuConnect frees the buffer on the failure path. With #6891 that leaves the xmem seven and DStorageGetFactory. Nothing else from me on this one.

ErRusso added a commit to ErRusso/Leonida that referenced this pull request Nov 23, 2026
Update xapu.cpp and chad.cpp to use the measured argument shapes from
the executable (PR ItsNotRexed#6981) instead of GDK-documented struct-based
signatures that mismatch the real call site register layout.

xapu.cpp:
- XApuConnect: (void* out24, void* out16, void** handle) — 3 raw
  pointers matching the measured call, not the GDK struct types.
  Removed the XApuConnectInputParameters/OutputParameters structs
  that were reading garbage from arg1.
- XApuEnqueueCommand: (handle, cmd*, uint64_t flags) — 3rd arg is
  flags (always 0 at call sites), not totalCommandCount*.
- XApuGetResultCount: (handle, uint64_t flags) — 2nd arg is flags,
  not totalResultCount*.
- XApuDequeueResult: (handle, out*) — no arity change.
- XApuDisconnect: (handle) — no arity change.

chad.cpp:
- XDspActivate: (handle, u64, u64, u64, u64) — 5 args (was 4).
  The measured call passes 4 extra uint64_t values; our old 4-arg
  version read status/streamHandle from the wrong registers.
- XDspDeactivate: (handle, u64, u64, u64) — 4 args (was 1).
- XDspSubmitCommand: (void*, void*) — 2 args (was 3 with commandSeq).

Also fix chad_test.cpp to pass baseBufferLength >= 0x10000 so the
shim's validation doesn't reject it, and update error code expectations
(E_POINTER for null handle, not E_INVALIDARG).

Fix acphal_test.cpp APU_ADDRESS struct ordering (must be defined
before the using declarations that reference it).

AI-assisted: yes
@Jadiac5

Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

New head 28f013c2: one commit on top, no force-push. It also removes the conflict with main's new xmem.dll notes in docs/runtime_imports.md (the paragraph moved into the RT-5 section). Answers to memorypressure's three points:

  1. The paragraph now sits in the RT-5 section and ends with: the table above was measured before this shim and still lists these ten as unimplemented; they are served now. I did not rewrite the "other 22 are in five DLLs" sentence itself: it is a measurement at 1cc6e3c1, main added its own "served now" line right under it, and runtime: AcpHal.dll shim, the audio heap with a 32-bit address mapping and a no-op hardware object (RT-489) #6885 and runtime: xg_xs.dll shim, XGCreateTextureComputer and XGSuggestSwizzleMode as traced E_NOTIMPL (RT-489) #6891 would each have to edit the same sentence and conflict. The counts are best re-measured once after the shims are in.
  2. The paragraph now says the call shapes agree with docs/audio_hal_exe_calls.md, measured with tools/import_call_sites.py.
  3. The shape comment in runtime/shims/xapu_chad.cpp now reads XApuDisconnect(handle, 0x20) and says the constant second argument is not declared because nothing reads it. No code changed.
  • Merges cleanly with main 51e7c9dc (git merge-tree).
  • Built the same code merged on 51e7c9dc on Windows 11, MSVC 19.44: xapu_chad (28 checks), xmem, process_cmdline, file_system, unimplemented_policy, winhttp_offline: 6/6 pass.

Not run against the retail executable.

AI-assisted (Claude Code).

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.

7 participants