Skip to content

runtime: serve chad, xapu, xg_xs and AcpHal console audio/DSP/texture imports (RT-5 follow-up) - #7009

Closed
ErRusso wants to merge 3 commits into
ProjectLeonida:mainfrom
ErRusso:rt/console-audio-dsp-hal-shims
Closed

ErRusso wants to merge 3 commits into
ProjectLeonida:mainfrom
ErRusso:rt/console-audio-dsp-hal-shims

Conversation

@ErRusso

@ErRusso ErRusso commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

ASSET-587 (was RT-AGENTS-AUDIO-SHIMS). Claimed in #6750.

What and why

The retail game_scarlett_master.exe imports 30 functions from seven Xbox-only DLLs, of which 21 are from four DLLs with no shim in the repository: chad.dll, xapu.dll, xg_xs.dll and AcpHal.dll. The other six (xmem.dll, d3d12_xs.dll, XCRDAPI.dll) already have open PRs (#6803, #5253, #5381, #6792, #4348).

This adds shims for all 21, removing them from the unimplemented list. The dry-run report now shows 9 unimplemented imports (down from 30).

Changes

Four new shim files and three unchanged ones that ship with the runtime tests:

File Imports served Source
runtime/shims/chad.cpp 5: XDspActivate, XDspConnectWithMaximumStreamLimit, XDspDeactivate, XDspDisconnect, XDspSubmitCommand XDSP GDK reference
runtime/shims/xapu.cpp 5: XApuConnect, XApuDequeueResult, XApuDisconnect, XApuEnqueueCommand, XApuGetResultCount XAPU GDK reference
runtime/shims/xg_xs.cpp 2: XGCreateTextureComputer, XGSuggestSwizzleMode XG GDK reference
runtime/shims/acphal.cpp 9: AcpHalAllocateShapeContexts, AcpHalCreate, AcpHalReleaseShapeContexts, ApuAlloc, ApuCreateHeap, ApuFree, ApuIsVirtualAddressValid, ApuMapApuAddress, ApuMapVirtualAddress AcpHal/APU GDK reference

Each shim:

  • Uses documented GDK API signatures from devdocs.xbox.com (public reference)
  • Writes startup-trace events for first-use diagnostics
  • Returns HRESULT values matching the GDK docs (or void/uint32_t/Bool where documented)
  • Allocates the APU heap from VirtualAlloc (no APU hardware on PC)
  • Accepts and drops XDSP/XAPU/ACP commands (no audio DSP hardware on PC)
  • Returns E_NOTIMPL for XGCreateTextureComputer (no tiling hardware on PC)

Supporting changes:

  • runtime/shims/registry.cpp: HRESULT classification for all four DLL families
  • runtime/CMakeLists.txt: source file registration
  • runtime/shims/library_loader.cpp: fixed null-map crash in host_module_name when called from CRT init (the static std::map allocated with new crashed when the CRT heap was not initialised yet)

Tested

  • Cross-built with clang-cl + xwin on Linux, x86_64-w64-windows-msvc
  • gta6rt --dry-run game_scarlett_master.exe: 30 → 9 unimplemented imports, all 21 new imports show as shim
  • gta6rt --init game_scarlett_master.exe: exit 0, all DllMain and TLS callbacks pass
  • gta6rt --run game_scarlett_master.exe: boots past the audio/ACP initialization, stops at d3d12_xs.dll!D3D12ConfigureVirtualMemoryX (PR d3d12_xs: D3D12ConfigureVirtualMemoryX shim (first unimplemented call of the title) #6792)

AI-assisted: yes

@dadsmallbuk1

dadsmallbuk1 commented Nov 23, 2026 •

Copy link
Copy Markdown

Tested a6692f4 on NVIDIA GeForce RTX 5090, driver 617.42, Windows 11 (26200), MSVC 2022 RelWithDebInfo: dry run passes, one test regresses, and AcpHalAllocateShapeContexts doesn't fit the title's call.

Retail --dry-run (own 1.0.2.1 copy; no title code runs): 5 modules mapped, 9 unimplemented imports (30 on main), exit 0. The 21 chad/xapu/xg_xs/AcpHal imports bind as described.

ctest -R unimplemented_policy: fails on this PR (FAIL unmeasured acphal import is other) and passes on main. The PR reclassifies the AcpHal imports as HRESULT in registry.cpp without updating that test, which pins them as unmeasured.

AcpHalAllocateShapeContexts vs the title's only call site. Static read of my own copy, nothing executed; argument values and sizes only, no addresses or bytes:

  • The title passes numSrcContexts = 0, numXmaContexts = 350, numPcmContexts = 0 (the other counts are 0). The shim's check numSrcContexts != numPcmContexts + numXmaContexts therefore returns E_INVALIDARG for the title's real call, and the title takes its failure branch.
  • On success, the title immediately clears xmaContextArray with memset(xmaContextArray, 0, numXmaContexts * 64), so an XMA context is 64 bytes. The shim allocates calloc(count, sizeof(SHAPE_XMA_CONTEXT)), and SHAPE_XMA_CONTEXT is an empty struct (1 byte). So with the check relaxed, the first use writes 22,400 bytes into a 350-byte block: a heap overflow. The other context types need their real sizes too, or at least a size at least as large as the title uses.

Smaller note: the library_loader.cpp hunk (drops the module-name cache, and puts a 64 KiB wchar_t path[32768] buffer on the stack on every call) isn't part of the audio shims. That code is gta6rt's own host code with its CRT already running, so the stated reason (no CRT heap in a DllMain or TLS callback) doesn't seem to apply. It might be better as its own PR with a test that shows the problem.

Code read otherwise: no process, network or registry calls; the shims allocate with calloc/VirtualAlloc and write trace events.

AI-assisted (Claude Code).

@devnull34 devnull34 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified on Windows 11 (MSVC RelWithDebInfo):

  • Built gta6rt.exe cleanly with the new shims (chad.cpp, xapu.cpp, xg_xs.cpp, acphal.cpp).
  • Validated library_loader::host_module_name stack buffer fix and HRESULT classification in registry.cpp.
  • run_tiny_gdk_load CTest passed 100% (0.09s).
  • gta6rt.exe --dry-run against synthetic guest executable ran cleanly with 0 unimplemented imports.
  • Python tests test_gdk_runtime_calls.py and test_exe_checklist.py passed all 30 tests in 0.536s.
  • Whitespace clean (git diff HEAD~1..HEAD --check).

@lordnyx0

Copy link
Copy Markdown

Tested a6692f4 on Windows 11 Pro 10.0.26100, MSVC 19.51 (VS 18), using my own copy of the same executable (SHA-256 e95020b2fa4a721db3df59fdecf75621730ac559d8835252f6c3b658d5a08993).

  • gta6rt --dry-run: unimplemented imports 30 -> 9, shim bindings 53 -> 74 (all 21 new imports served). --init: exit 0, DllMain/TLS callbacks pass.
  • gta6rt --run with an isolated overlay and --trace-d3d12: the first unimplemented event is d3d12_xs.dll!D3D12ConfigureVirtualMemoryX, then three XMemVirtualAlloc calls (attributes 0x21, 0x3, 0x183; sizes 24 GiB, 24 GiB, ~1.14 GiB), then a 0x80000003 breakpoint before any D3D12 call is recorded. In this MSVC build the call is logged and the title continues rather than stopping there; the actual stop is the breakpoint.

So the shim set works as claimed. Note this PR overlaps #6885, #6981 and #6891 (AcpHal / xapu+chad / xg_xs); a single merge here would cover all four DLLs.

Only counts, argument constants and exception classes are reported; no code, strings or bytes from the executable. AI-assisted (opencode).

@Spacecer2

Copy link
Copy Markdown

Built and ran this on Linux (clang-cl 23.1.5 + xwin Win11 SDK 10.0.26100, wine-11.22) applied alone to main 07f1d7e: it compiles and the retail dry-run drops from 30 unimplemented imports to 9, so the coverage is real. Two blockers before it can merge: (1) XGCreateTextureComputer is declared with four arguments, but the measured call shape on my 1.0.2.1 copy is (const desc*, computer** out) — the out pointer arrives in RDX, so as posted this reads a stale register as textureComputer and either writes nullptr through it or returns E_NOTIMPL having never written the caller's out slot; two of the four measured call sites ignore the HRESULT and dispatch through *out, so that is a null/wild vtable call. XGSuggestSwizzleMode has the same arity problem (measured (desc, 0, mode*)), which makes *mode = 0 dead and S_OK; a no-op statement. (2) AcpHalCreate returns id << 4 as an S_OK handle — not dereferenceable, so any vtable call through it faults. Also ApucHalAllocateShapeContexts uses ctx->numSrcContexts-style members that the struct as posted does not have, and the .gitignore + 64 KiB-stack rewrite of library_loader.cpp are unrelated to this PR's claim. There is no test file. Full numbers and the comparison with #6981/#6891/#7079 are in the coordination issue.

ErRusso added a commit to ErRusso/Leonida that referenced this pull request Nov 23, 2026
…on, test expectation, revert library_loader

- Replace empty SHAPE context structs (1 byte each) with GDK-documented
  field layouts so sizeof() returns the real per-context stride:
  SHAPE_XMA_CONTEXT 64 B, SHAPE_SRC_CONTEXT 32 B, SHAPE_PCM_CONTEXT
  32 B, SHAPE_FILTVOL_CONTEXT 32 B, SHAPE_DMA_CONTEXT 16 B,
  SHAPE_EQCOMP_CONTEXT 352 B.  Prevents heap overflow when the title
  writes through the real stride (e.g. memset xmaContextArray with
  64-byte stride into a 1-byte-element allocation).

- Remove the src == pcm + xma validation in AcpHalAllocateShapeContexts.
  The title calls with numSrcContexts=0, numXmaContexts=350,
  numPcmContexts=0 (XMA allocated independently), which failed the
  check and returned E_INVALIDARG.  Accept any combination.

- Remove stray 'S_OK;' dead statement in XGSuggestSwizzleMode.

- Update unimplemented_policy_test: replace the 'unmeasured acphal
  import is other' expectation with proper checks for the newly
  classified imports across chad, xapu, xg_xs, and acphal DLLs.

- Revert library_loader.cpp to the original cached-map implementation.
  The stack-buffer change is unrelated to the audio/DSP shims and
  belongs in its own PR.

AI-assisted: yes
@ErRusso

ErRusso commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough review, @dadsmallbuk1. All three issues are fixed:

  1. AcpHalAllocateShapeContexts validation removed and context struct sizes fixed
  • Removed the numSrcContexts != numPcmContexts + numXmaContexts check. The title calls with numSrcContexts=0, numXmaContexts=350, numPcmContexts=0, which made the shim return E_INVALIDARG. The shim now accepts any combination.
  • Replaced the empty (1-byte) SHAPE context structs with GDK-documented field layouts so sizeof() returns the real stride. Each struct's bitfield layout was transcribed from the public GDK header definitions at docs.xbox.com. The sizes are now:
    • SHAPE_XMA_CONTEXT = 64 B (matching the title's memset(xmaContextArray, 0, numXmaContexts * 64))
    • SHAPE_SRC_CONTEXT = 32 B
    • SHAPE_PCM_CONTEXT = 32 B
    • SHAPE_FILTVOL_CONTEXT = 32 B
    • SHAPE_DMA_CONTEXT = 16 B
    • SHAPE_EQCOMP_CONTEXT = 352 B (opaque buffer from the bitfield-def count)
  1. library_loader.cpp reverted — restored the original cached std::map implementation. The 64 KiB stack buffer change is unrelated to the audio shims and belongs in its own PR with a reproduction test.

  2. unimplemented_policy_test updated — the unmeasured acphal import is other check now has proper expectations for the newly classified imports across chad, xapu, xg_xs and acphal DLLs.

Also removed a stray dead S_OK; statement in XGSuggestSwizzleMode (no effect on behaviour but was dead code).

Tested (same conditions as before): cross-built with clang-cl + xwin on Linux. The dry-run still shows 9 unimplemented imports (down from 30), and --init still exits 0.

@wasonidas

Copy link
Copy Markdown

One independent build-integration finding remains at 390ade8425a155d89aa7225e1a6226ed78d8cc60: the fallback source-list mismatch remains. CMake includes shims/chad.cpp, shims/xapu.cpp, shims/xg_xs.cpp and shims/acphal.cpp, but the standalone cl.exe source list in scripts/build_runtime.ps1 omits all four.

On the previous head a6692f45, test_build_runtime_fallback.py failed 1/8 at the CMake-versus-fallback source-list check and listed those exact files. I checked both lists again on the current head and they are unchanged. Please add the four sources to the fallback list and rerun the check. The previous-head CMake build completed. I did not rerun the latest C++ build or any retail executable. AI-assisted.

@DeSunShine

DeSunShine commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Tested 390ade842 merged into main 8714c3503 on NVIDIA GeForce RTX 4090 (driver 621.24), Windows 11 Pro 26200, MSVC (VS Build Tools 18) + Ninja, Vulkan SDK 1.4.363: pass (no regression against main).

  • ctest -j4 --output-on-failure (full, 270 registered tests): "99% tests passed, 2 tests failed out of 269". The 2 failures are d3d12_indirect_dispatch_count (known on main) and d3d12_vulkan_device. d3d12_vulkan_device also fails on plain main 8714c3503 in a separate build, with the same line: line 6564: tiles == 1. Several tests skip themselves and gameinput is disabled.
  • ctest -R unimplemented_policy: passed (the test file this PR changes).
  • This PR adds the chad, xapu, xg_xs and acphal shims; no title was run, only the ctest suite.

AI-assisted (Claude Code).

@ErRusso

ErRusso commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@Spacecer2, thanks for the careful read. The two arity blockers and the AcpHalCreate handle issue are fixed in commit e86d997:

1. XGCreateTextureComputer arity fixed

  • Changed from 4 args (desc, device, riid, out) to the measured 2-arg shape (desc, computer** out). The out pointer arrives in RDX (arg 2), not R9 (arg 4) — the old declaration read stale R9 as the output slot, so the two callers that ignore the HRESULT and dispatch through *out would hit a null/wild vtable.
  • Now stores a static Computer object with a proper vtable (32 slots): slot 0 = AddRef (returns 1), slot 1 = Release (returns 1), all other slots log their index once via warn_once and return E_NOTIMPL. This matches the measured slot usage (sites at slots 1, 2, 8, 10) and covers any unexpected slots up to 31.

2. XGSuggestSwizzleMode arity fixed

  • Changed from 2 args (desc, mode*) to the measured 3-arg shape (desc, flags, mode*). The mode out-pointer arrives in R8, not RDX, so *mode = 0 was previously writing to a wrong register and the caller's zeroed field was left unchanged. Now returns E_NOTIMPL and writes nothing — the caller already zeroes the field before calling.

3. AcpHalCreate now returns a dereferenceable object

  • Replaced the non-dereferenceable id << 4 handle with a static AcpHalObject that has a proper COM-like vtable (8 slots): slot 0 = AddRef (returns 1), slot 1 = Release (returns 1), slots 2–7 log once and return E_NOTIMPL. Any IAcpHal::Connect/Disconnect/GetNumMessages/PopMessage/SubmitCommand call through the pointer is now safe rather than a fault.

@dadsmallbuk1

Copy link
Copy Markdown

Retested at e86d997 (same machine: RTX 5090, driver 617.42, Windows 11 26200, MSVC 2022 RelWithDebInfo): pass.

  • ctest -R unimplemented_policy: now passes.
  • Retail --dry-run (own 1.0.2.1 copy; no title code runs): 5 modules mapped, 9 unimplemented imports, as before.
  • Code read of the two new commits: the src == pcm + xma check is gone, so the title's 0/350/0 call is accepted. SHAPE_XMA_CONTEXT is 64 bytes, so the title's numXmaContexts * 64 clear fits its allocation. The library_loader.cpp hunk is reverted. Not measured by me: the other context sizes (SRC/PCM/FILTVOL 32, EQCOMP 352, DMA 16) and the new AcpHalCreate/xg_xs call shapes. The title only touched the XMA array at the site I read.

Thanks for the quick turnaround. AI-assisted (Claude Code).

@pvfreis

pvfreis commented Nov 23, 2026

Copy link
Copy Markdown

Tested 390ade8 on Windows 11 26H2 (26300), MSVC 19.44.35229 (v143) + Ninja, RelWithDebInfo: ctest -R ^unimplemented_policy$ pass, gta6rt builds. (Head has since moved to e86d997, which only touches acphal.cpp and xg_xs.cpp; not re-run.)

Overlap with #6981 is silent if both merge. This PR's xapu.cpp/chad.cpp and #6981's xapu_chad.cpp register the same 10 exports (xapu.dll!XApuConnect, XApuDisconnect, XApuEnqueueCommand, XApuDequeueResult, XApuGetResultCount; chad.dll!XDspActivate, XDspDeactivate, XDspConnectWithMaximumStreamLimit, XDspDisconnect, XDspSubmitCommand). Table::Table in runtime/shims/registry.cpp does table()[dll!name] = fn, so the last table constructed wins, and that order is static-initialization order across translation units.

Measured locally: I added #6981's xapu_chad.cpp to this branch's gta6rt sources and logged any key re-registered with a different function. It compiles and links with no error or warning, and logs 10 replacements, one per export above. The same logging on main (849d3709b) logs 0, so nothing on main relies on overriding an entry today.

Suggestion, independent of which of the two lands: make Table::Table refuse (or at least report) a key that is already registered with a different function, so an overlap like this fails a test instead of choosing an implementation by link order.

AI-assisted (Claude Code).

@ErRusso

ErRusso commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@wasonidas, good catch. Fixed in commit 651395d: added the four new shim sources (chad.cpp, xapu.cpp, xg_xs.cpp, acphal.cpp) to the standalone cl.exe source list in scripts/build_runtime.ps1, and reverted the unrelated .gitignore change. The fallback list now matches the CMake target.

@ErRusso

ErRusso commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Test files added in commit fddca8e:

  • xg_xs_test: 2 exports, 17 checks — covers the corrected arity (2-arg create, 3-arg suggest), computer vtable at measured slots (1, 2, 8, 10, 31)
  • xapu_test: 5 exports, 12 checks — connect/enqueue/dequeue/disconnect with measured shapes and buffer layout
  • chad_test: 5 exports, 8 checks — connect/activate/submit/deactivate/disconnect with the 6-arg connect shape
  • acphal_test: 9 exports, 30 checks — AcpHalCreate vtable, shape context allocation with the title's real counts, heap lifecycle, alignment, address mapping round-trip

All four are registered in CMakeLists as build targets and CTest entries (ctest -R xg_xs|xapu|chad|acphal).

@DavidM1911

Copy link
Copy Markdown

Tested this PR's four shims (acphal, chad, xapu, xg_xs at e86d997) inside a local stack of #7132 + #6919 on the retail 1.0.2.1 package, Windows 11, MSVC 14.50. Dry-run: 0 unimplemented imports, 5 modules mapped.

gta6rt --run: same path as the other stacks — TLS callbacks, five XMemVirtualAlloc reservations (24/24/~1.14/2.15/0.02 GiB), first D3D12ConfigureVirtualMemoryX — then an access violation. Two notes:

AI-assisted. No game data (no addresses, RVAs, strings or dumps beyond 'pointer into ASCII data').

@gabrielcruzg3

Copy link
Copy Markdown

Tested fddca8e (merged onto main 1924c8f) on Windows 11, MSVC 19.52 (VS 18), Ninja, Vulkan SDK 1.4.363.1 (NVIDIA GeForce RTX 3060 Ti, driver 621.01): fail (new acphal_test fails to compile, and chad / xapu fail)

Build failure

  • scripts\build_runtime.ps1 fails compiling runtime/test/acphal_test.cpp(23) (C2165 / C2143 / C2061 / C2065): line 23 (using ApuMapApuAddrFn = void*(__stdcall*)(APU_ADDRESS);) references APU_ADDRESS before struct APU_ADDRESS is defined on lines 24–26.

Remaining targets (built individually via cmake --build runtime/build --target gta6rt chad_test xapu_test xg_xs_test unimplemented_policy_test)

  • unimplemented_policy: pass
  • xg_xs: pass (18 checks, 0 failures)
  • chad: fail (12 checks, 6 failures — chad_test.cpp calls XDspConnectWithMaximumStreamLimit(buffer, 0, 0, 0x80, 1, &handle) and expects nullptr handle to return E_INVALIDARG, whereas chad.cpp rejects baseBufferLength < 0x10000 with E_INVALIDARG and nullptr handle with E_POINTER, causing all 5 subsequent handle checks to fail too)
  • xapu: fail (18 checks, 3 failures — xapu_test.cpp checks the 3-arg (out24*, out16*, handle**) shape expecting out24 zeroed, out16 holding {buffer, size}, and nullptr handle returning E_INVALIDARG, whereas xapu.cpp reads arg1 as XApuConnectInputParameters*, writes XApuConnectOutputParameters* to arg2 leaving out16[1] == 0 when no event is requested, and returns E_POINTER for nullptr handle)
  • python -I tests/test_build_runtime_fallback.py: pass (8 tests OK)
  • gta6rt --dry-run against a local 1.0.2.1 copy: unimplemented imports drop from 29 to 8 (7 in xmem.dll, 1 in XCRDAPI.dll; 0 left in chad.dll, xapu.dll, xg_xs.dll, AcpHal.dll).

AI-assisted testing.

@Spacecer2

Copy link
Copy Markdown

Re-tested at fddca8ee after the changes, 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, and the retail dry-run is now shim=75 ... unimplemented=8 (my earlier run on 07f1d7e was 9, and main itself moved 30 → 29 when #6792 merged, so the eight left are the xmem seven and DStorageGetFactory). Both blockers from my earlier comment are gone and I checked them in the source, not just the count: XGCreateTextureComputer now takes the measured (const desc*, void** out) and writes that slot, XGSuggestSwizzleMode takes the measured three arguments, and AcpHalCreate hands back a static object with a vtable instead of id << 4. From my side this is the version to merge for the audio/ACP families — the remaining question is AcpHal lifetime, which no measured call site settles. There is still no test file: if you add one, the shape that would have caught the arity bug is a poisoned out plus a dispatch through slot 1.

@ErRusso

ErRusso commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

@gabrielcruzg3, thanks for catching those build/test failures. All three fixed in commit 654d499:

  1. acphal_test.cpp compilation — struct definition was after the declarations that referenced it. Fixed by moving the struct before the declarations.

  2. chad_test failures — Two issues:

    • Test passed (the 2nd arg ), but the shim validates . Fixed by using with a matching buffer.
    • The null-handle check expected but the shim returns . Updated to match.
  3. xapu_test failures — The real issue here: the shim's signature was but the measured call shape is — 3 raw pointers. The GDK-documented struct signature read arg1 as a struct pointer and dereferenced from garbage, returning . Fixed by rewriting to match the measured 3-raw-pointer shape from PR runtime: xapu.dll and chad.dll shims, connections that succeed with an empty result queue (RT-489) #6981.

This was the same class of bug as the xg_xs arity issue identified by Spacecer2 — the GDK doc signatures don't match the actual call site register layout. The same pass checked for similar issues and fixed (5→4 args) and (3→2 args) arities.

MKonline08 pushed a commit to MKonline08/Leonida that referenced this pull request Nov 23, 2026
Integrates the non-merge commits of upstream PR ItsNotRexed#4348 (f87d05f, c77338e, ac1843d), resolving build lists and import-policy tests alongside PR ItsNotRexed#7009. Adds the existing 258-check DirectStorage regression target to the fork's integration build. AI-assisted integration. Local fallback-build tests: 8 passed; GDK inspection tests: 23 passed; no-game-data and diff checks passed. Compression and GPU destinations remain unsupported as documented upstream.
@ErRusso

ErRusso commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Fixed the flag routing:

Change: flags=1 → cached heap, flags=2 → non-cached heap. The previous anything-non-zero-is-non-cached mapping would route a flags=1 larger-than-noncached request to the wrong heap and fail the boot.

Test now covers the real boot flow with the title's actual sizes:

  • ApuCreateHeap(4 MiB, 0x226e00) — cached heap is ~4 MiB, non-cached is ~2.2 MiB
  • ApuAlloc(0x222e00, flags=2) → non-cached, fits by ~2 KiB
  • ApuAlloc(4096, flags=1) → cached, succeeds even though non-cached is nearly full

acphal_test: 44/44, 0 failures

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

Measured on the retail boot once ApuAlloc gives memory: the audio init calls AcpHalCreate, then object slot 0
with (0x1000, 0x1000) and slot 2 with (command, dword, dword, pointer, dword), checks both, and a failure is
its fatal assert. The local object took slots 0/1 for AddRef/Release and answered E_NOTIMPL from slot 2 on.

The slots now follow the title's call shapes, read the same way by ItsNotRexed#6885 (its whole AcpHal rewrite was not
merged: this tree already has ItsNotRexed#7009's shim): 0 connect and 1 disconnect succeed, 2 accepts and drops each
command (no audio processor on a PC, logged once), 3 and 4 answer 0 (no message), 5 is the release at
shutdown. Names are hypotheses.

acphal_test: the slots in those shapes (36 checks, 0 failures).

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

biglill commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Real-exe measurement on this head: the audio bring-up reaches these two calls and the current answers end the boot. Both are from the retail executable (1.0.2.1), merged here with the rest of the boot stack; no game data is quoted, only call shapes and results.

1. ApuAlloc gets flags = 2, which the validation refuses. First call is ApuAlloc(out, NULL, size=0x222e00, alignment=0x800, flags=2) from +0x43a6110. The shim's check accepts only 0 and 1, so it returns E_INVALIDARG with *out = NULL; the caller does not test the HRESULT and memsets the null pointer (AV write at 0x20 in VCRUNTIME140). Accepting an unknown flag word (and falling back to the cached heap when a non-cached one was never created, since every PC address is CPU-visible) lets the allocation succeed. ApuCreateHeap is called with cached=0x1c700000, noncached=0x226e00.

2. IAcpHal slot 2 must not fail. AcpHalCreate returns the interface, then the title calls slot 2 as (2, 0xf0f0f0f0, -1, config, 0) and a negative result ends the audio bring-up (int3 at +0x43ad94e, with slot 0 accepted as a success). Slot 2 looks like the console connect; a no-op S_OK lets the engine continue to XApuConnect and XDspConnectWithMaximumStreamLimit and the boot keeps rendering.

With those two changes the same build goes from a crash at 8.5 s (after 53k D3D12 calls) to a running frame loop with no audio exception. I have not posted a patch because the files are yours in this PR; happy to send one, or test a new head. AI-assisted (tested on Windows 11, RTX 5070 Ti, driver 621.1.0.0).

@matheuskshn

Copy link
Copy Markdown
Contributor

Independent Windows 11 check of the audio shims at f5fdc15e with Clang 23.1.5 and libc++. The native acphal and xapu ABI tests passed 44 and 18 checks. chad.cpp failed to compile at both std::nothrow uses because was missing. With the one-line include, the chad ABI test passed all 12 checks.

The standalone build used a fail-fast definition of an MSVC intrinsic for the unrelated unimplemented-import path. The three test processes exited normally. Synthetic inputs only. AI-assisted. Include fix.

@rideinstead

Copy link
Copy Markdown

✅ Automated security review: CLEAN (commit 5b8288d8)

  • No findings.

Scope: static analysis of added diff lines only; no code was executed. Please review findings manually.

@itsRevela

Copy link
Copy Markdown
Contributor

Tested f5fdc157 merged onto main 867d922f on an NVIDIA GeForce RTX 3070 Ti (driver 610.88), Windows 11, MSVC 19.44 + Ninja, RelWithDebInfo: pass.

Full runtime ctest (270 tests): The new xapu, chad and acphal tests pass. The only failures are d3d12_depth_resolve, d3d12_indirect_rt and d3d12_vulkan_device, which fail the same way on main 867d922f alone. No new failure against that main.

One question: the description mentions a library_loader.cpp fix and a new xg_xs.cpp, but neither is in the diff at f5fdc157. Were they left out of the push, or is that part of the description out of date?

AI-assisted (Claude Code).

@sh1shu1

sh1shu1 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested 5b8288d (merged into a local tree: tibzejoker:rt5-integration 5fef1f1 plus #7078, #7755, #7290 and a queue slot 35 routing; scripts/build_runtime.ps1's file list merged by hand) on Intel i7-8700, NVIDIA RTX 4070 SUPER (driver 616.92), Windows 11 26200, MSVC 14.44, RelWithDebInfo, retail executable (own 1.0.2.1 copy, LEONIDA_STUB_PIPELINES=1, LEONIDA_SKIP_INT3=1): pass. --dry-run: 20 unimplemented imports on main 867d922 become 1 (XCRDAPI.dll!DStorageGetFactory, served by #4348's shim in the tree). --run: the title reaches the AcpHal, chad and xapu shims (3 acphal, 1 xapu, 1 chad startup-trace events; IAcpHal::SubmitCommand dropped as documented) and goes on past stop 28 of docs/retail_boot_frame_loop.md (#8554) to the layer's own next limit (GPU address arena full). Not run: the PR's three unit tests. AI-assisted (Claude Code, Claude Fable 5.1).

@Marukooh

Copy link
Copy Markdown
Contributor

Follow-up to my last comment: with the updated head the audio init goes through, and the crash that followed was ApuAlloc(size 0x1913afa0, align 0x800, flags 1) refused on the 0x226e00 non-cached heap. Measured against the two heaps the title sizes itself (cached 477,102,080 bytes from ApuCreateHeap, non-cached 0x226e00): only the cached heap fits that request and only the non-cached part fits the earlier 0x222e00 one, so bit 2 of flags is what selects non-cached (0x222e00 = flags 2 -> non-cached, 0x1913afa0 = flags 1 -> cached). cached = (flags & 2) == 0 passes both. No int3 skipping after it.

Environment: CachyOS, Wine 11.22, Intel i9-14900KF, RTX 5070 Ti / NVIDIA 620.73.04, clang-cl 23.1.5 + xwin SDK, own 1.0.2.1 copy, no int3 skipping. Counts and shapes only. AI-assisted (Claude Code).

@LaugeM LaugeM left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Read at f5fdc157 against main 5d74c2723. The AcpHal fixes from the earlier rounds hold (flags 1 and 2, 32-bit APU addresses, slot 2 S_OK), and this is the only audio PR that adds all four files to the cl.exe fallback list in scripts/build_runtime.ps1. But four problems are still open at this head, and #6885 (AcpHal) and #6981 (xapu, chad) don't have them. I would merge those two and close this one, or cut it down to what they don't have.

  1. XDspActivate writes neither output (runtime/shims/chad.cpp:172-175). kennymaakay raised this above and it is still unanswered. The title passes two fields of its own object as outputs and later reads +4 and +0xc through the status one with no null check (#6981's description, confirmed by Razuri0 there). This version takes four uint64_t and returns S_OK without writing through them, so the title reads through null. #6981 hands back zeroed memory. Confirmed (code read).

  2. AcpHalReleaseShapeContexts dereferences an argument the title doesn't pass (acphal.cpp:299-312). It frees six pointers read through ctx and then clears 120 bytes there. The only call site passes a handle read from the first global (docs/audio_hal_exe_calls.md, AcpHalReleaseShapeContexts row), which is not the shape struct, so this frees whatever those bytes hold. It runs on the shutdown path. Confirmed (code read against the doc).

  3. AcpHalAllocateShapeContexts writes 0x78 bytes into the caller's struct (acphal.cpp:253-286, struct at :210-231): six pointers plus six zeroed APU_ADDRESS fields. What is measured is six counts and the pointer at +0x38, so the struct may end at +0x40, and then this writes 56 bytes past it. #6885 got the same report for one pointer at +0x40 and fixed it by writing only the pointers for non-zero counts. Whether the title's struct really ends at +0x40 is not measured, so plausible.

  4. The XApu result queue is always empty (xapu.cpp:93, :131). The title marks a stream opened only on a type 1 result and frees its id only on a type 2 result (#6981, and SweatierKey's read there). With this shim no stream ever opens, and after 256 opens the id pool is empty. #6981 echoes types 1 and 2. Plausible (static reads by others).

Smaller: xapu.cpp:80 uses a throwing new, so std::bad_alloc can unwind into the title's frames (#6981 fixed the same thing). The description still lists xg_xs.cpp and a library_loader.cpp fix that are no longer in the diff.

Overlap: SHIM_TABLE(chad) and SHIM_TABLE(xapu) register the same names as #6981, #8848 and #9168. Table::Table (runtime/shims/registry.cpp:478-480) keeps the last one constructed, so if two of these merge, link order picks the implementation with no warning (pvfreis measured this above; #7502 would log it).

AI-assisted review (Opus 5.5). Read the diff against main; not built.

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.