Skip to content

runtime: AcpHal.dll shim, the audio heap with a 32-bit address mapping and a no-op hardware object (RT-489) - #6885

Open
Jadiac5 wants to merge 4 commits into
ItsNotRexed:mainfrom
Jadiac5:runtime/acphal-shim
Open

Jadiac5 wants to merge 4 commits into
ItsNotRexed:mainfrom
Jadiac5:runtime/acphal-shim

Conversation

@Jadiac5

@Jadiac5 Jadiac5 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Item: AcpHal.dll shim (RT-5 follow-up), claimed in #6750 (comment)

What the imports are for

AcpHal.dll is the console's audio hardware layer. The game executable imports nine functions from it (the list in #6715). The Apu* functions manage a heap of memory the audio processor reads, with 32-bit APU addresses for it. AcpHalCreate returns the object the game sends commands through, and AcpHalAllocateShapeContexts hands out the per-voice context records.

What the call sites show (static read of the executable, nothing executed or mapped)

Each import has one thunk. There are 16 call sites in all:

  • ApuCreateHeap (1): a size in MiB shifted left by 20, then a byte count. The result is tested for failure, and failure stops the game.
  • ApuAlloc (1): five arguments. The output pointer comes first. The APU-address out pointer is NULL, the alignment is 0x800 and the flags are 2. The result is not tested; the caller reads the output pointer straight after the call. The size is the non-cached heap size minus 16 KiB.
  • ApuFree (1): one pointer, as a tail call at shutdown. The result is ignored.
  • ApuIsVirtualAddressValid (4): a pointer and an alignment of 0x40, 0x80 or 0x800. The result is a bool in AL.
  • ApuMapVirtualAddress (4): a pointer in, and a 32-bit value out that goes into a context field. One site can pass NULL.
  • ApuMapApuAddress (2): a 32-bit value in, and a pointer out. The pointer is tested for NULL and then written through.
  • AcpHalAllocateShapeContexts (1): one pointer to a struct. The caller zeroes six dwords and sets the fifth to 350. After the call it reads a pointer at +0x38 and clears count × 64 bytes there. No other code in the image touches the struct. Failure stops the game.
  • AcpHalCreate (1): one out pointer. Failure stops the game. The game calls four vtable slots on the object:
    • slot 0 with (0x1000, 0x1000), result tested;
    • slot 2 with a command id, two dwords, a pointer and a dword, result tested once at boot;
    • slot 1 with no arguments at shutdown;
    • slot 5 last. After it, the game sets the pointer to NULL.
    • Slots 3 and 4 are never called.
  • AcpHalReleaseShapeContexts (1): no argument registers are set up. The result is ignored.

What each function does now

  • ApuCreateHeap commits one block of ordinary memory for both sizes. A PC has no cached/non-cached split.
  • ApuAlloc and ApuFree manage that block first fit. A new allocation is zeroed. An APU address is a fixed nonzero base plus the offset into the block.
  • ApuMapVirtualAddress and ApuMapApuAddress convert between the two by arithmetic. Anything outside the heap maps to 0 or NULL.
  • ApuIsVirtualAddressValid returns TRUE only for an address inside a live block whose APU address is a multiple of the alignment.
  • AcpHalAllocateShapeContexts gives each nonzero count zeroed memory of count × 64 bytes and writes a NULL pointer for each zero count. AcpHalReleaseShapeContexts frees that memory.
  • AcpHalCreate returns an object:
    • Slots 0, 1 and 2 return S_OK. Slot 2 drops the command and logs this once.
    • Slots 3 and 4 return 0. The title never calls them; 0 is "no message" if they are a message poll and S_OK otherwise.
    • Slot 5 frees the object.
  • There is no audio processor, so this path produces no sound. Every boot-time call returns success because the game stops on any failure there.

Hypotheses:

  • the parameter names and meanings (cached/non-cached sizes, and flags 2 meaning non-cached);
  • the struct layout of six counts followed by six pointers. Only the fifth count and its pointer at +0x38 are measured;
  • 64 bytes per context, measured for the fifth type only;
  • slot 5 being Release;
  • the value of the APU address base.

Tests

  • acphal_test (new, ctest acphal): 29 checks, 28 pass. It resolves every export through rt::shims::find. It calls them in the shapes and order of the call sites, and it calls the object through raw vtable slots 0, 2, 3, 4, 1 and 5.
  • ctest -R "^(acphal|xmem|unimplemented_policy|pe_loader)$": 4 of 4 pass, run after the rebase on 548ac1032.
  • gta6rt builds with the new table.
  • python -I tests/test_docs_repo_paths.py: OK.
  • python -I scripts/check_no_game_data.py: exit 0.

Not tested: it has not been run against the retail executable. No audio output either, because there is none.

Tested on Windows 11, MSVC 19.44.

AI-assisted (Claude Code).

@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

A review note on the AcpHal hardware object, from facts in sammwyy/xvalkyrie (GPL-3.0; src/runtime/modules/acphal.cpp at 53af7df77c10). xvalkyrie measured them on two other titles, not on this exe, so they are hypotheses to check against your call sites:

  • Slots 3 and 4 returning E_NOTIMPL is the main risk. In xvalkyrie, slot 3 is PopMessage (it returns a bool) and slot 4 is GetNumMessages (it returns a count). E_NOTIMPL is non-zero, so a mixer polling them would see a message waiting, or a huge count, and read a message that was never written. Returning 0 / FALSE there is the safe no-op.
  • Slot order: Connect, Disconnect, SubmitCommand, PopMessage, GetNumMessages, Release. Microsoft's public IAcpHal reference names the same five methods; I couldn't confirm the order there.
  • If the title waits on completions: a message is 32 bytes (type, reserved, value, id, frame). A finished command is type 0x10 with the command type in value, a frame tick is type 0x02 (every 2.667 ms, i.e. 128 samples at 48 kHz), and Disconnect is type 0x80. A command submitted with id 0 gets its 1-based submission number as its id; xvalkyrie says a title waited for "completion 1". SubmitCommand before Connect returns E_UNEXPECTED.
  • Where xvalkyrie disagrees with your measurement: ApuMapVirtualAddress. You measured UINT32 ApuMapVirtualAddress(void* va), while xvalkyrie uses (UINT32* apu, void* va, UINT32 size). Your call-site measurement should win.

AI-assisted.

@Jadiac5

Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, taken in 280ee42: slots 3 and 4 now return 0 instead of E_NOTIMPL. This title has no call site for either slot, so I could not measure them; 0 is right under your reading (no message, count 0) and is S_OK under any other. The test calls both (28 checks pass).

  • Slot order: the call sites here fit Connect, Disconnect, SubmitCommand at 0-2 and a last call at slot 5 before the pointer is dropped.
  • Completions: I found no site that reads a message back, so the shim queues none. If a run shows the title waiting on one, that is the next step.
  • ApuMapVirtualAddress: kept at one argument. All 4 call sites set up only the first argument register and store the 32-bit result.

Not run against the retail executable. AI-assisted (Claude Code).

@Roberto0953

Copy link
Copy Markdown
Contributor

Tested on Windows 11 26300, MSVC 14.44, RelWithDebInfo, CPU only, no game files.

Results

  • 280ee42a6 (current head):
    • acphal_test: 28 checks, 0 failures.
    • No warning in the acphal_test build.
  • 25517cfe4 (the commit before; the last one touches only acphal.cpp and its test):
    • acphal_test 27/0, xmem_test OK (100), xgameruntime_offline_test 44/0, xgameruntime_test OK (143), unimplemented_policy_test 96/0.
    • gta6rt builds.
    • The four warnings in the build are in xgameruntime_test.cpp (lines 746, 766, 827, 862), and main has the same ones.
  • Negative control: acphal_test.cpp built with registry.cpp but without acphal.cpp fails as it should: FAIL the registry owns acphal.dll, FAIL an AcpHal.dll export is not registered, exit 1.

1. AcpHalAllocateShapeContexts writes past the measured part of the caller's struct.

  • The shim writes all six pointers, so it writes up to +0x48.
  • What is measured is the six counts and the pointer read at +0x38, so the measured struct ends at +0x40. The sixth pointer lands at +0x40..+0x47.
  • If the caller's block is 0x40 bytes (on its stack, for instance), that write clobbers the next 8 bytes.

Checked with a scratch guest that calls the registered function on a 0x40-byte block followed by 16 guard bytes, with the fifth count set to 350:

  • result S_OK, and the pointer at +0x38 is set;
  • 8 guard bytes at +0x40..+0x47 are overwritten (with zeros, the NULL for count 0).

Writing s->contexts[i] only when s->count[i] != 0 keeps every write inside what is measured. It gives the same result for the measured call site, and acphal_test still passes, because its struct is the full 0x48 bytes and zero-initialised.

2. The branch also carries 548ac1032, which is the head of #6810 (XAccessibilityImpl / XLauncherImpl). The description has no Depends on: #6810, so merging this one would also merge #6810. The xgameruntime tests above cover that commit, and they pass.

AI-assisted (Claude Code).

@Razuri0

Razuri0 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Static check of the call sites at 280ee42a, read from the executable only (SHA-256 e95020b2…8993, same file as #6715/#6751); nothing executed or mapped:

  • 9 imports and 16 call sites: AcpHalCreate, AcpHalAllocateShapeContexts, AcpHalReleaseShapeContexts, ApuCreateHeap, ApuAlloc and ApuFree once each (the last through its shutdown tail helper), ApuIsVirtualAddressValid 4, ApuMapVirtualAddress 4, ApuMapApuAddress 2.
  • ApuCreateHeap: ecx = MiB << 20, edx = 0x226e00; the return is tested and a negative value reaches int3.
  • ApuAlloc wrapper at RVA 0x43a6110: rcx = output, rdx = 0, r8 = 0x222e00 (the 0x226e00 heap minus 0x4000), r9 = 0x800, stack argument 2. The caller reads the output pointer without testing the return.
  • AcpHalAllocateShapeContexts: the struct is at 0x149ae98d0, its fifth dword is 0x15e, the pointer is at +0x38, and the caller clears count << 6 bytes there — the 64-byte stride is confirmed.
  • AcpHalCreate: slot 0 gets (0x1000, 0x1000); slot 2 gets (2, 0xf0f0f0f0, 0xffffffff, &0xc0, 0); the shutdown path calls slot 5 and then clears the object pointer.

That matches the PR's "What the call sites show" section. I cannot run the Windows ctest here, so this is a static confirmation only; the 28-check run remains the runtime evidence. No game data: counts, offsets and RVAs only. AI-assisted.

@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.

Hardware & Environment

  • Host: Windows 11 Build 26300 x64
  • Toolchain: MSVC 19.44 (Visual Studio 2022), CMake 4.4, Python 3.15

Verification Summary

Targets compiled with MSVC 19.44 and executed natively:

  1. **\�cphal_test.exe**:

    • Passed with 28 checks, 0 failures.
    • Verified audio heap creation (\ApuCreateHeap), aligned allocations (\ApuAlloc), address validity checks, 32-bit APU-to-virtual address translations (\ApuMapVirtualAddress\ / \ApuMapApuAddress), shape context allocation/release, and no-op \AcpHalCreate\ object lifecycle (with message poll slots 3 and 4 answering 0 when empty).
  2. **\xgameruntime_offline_test.exe\ & \xgameruntime_test.exe**:

    • Passed with 143 checks OK.
    • Verified \XAccessibilityImpl\ (closed captions, high contrast, synthesizer voice enumeration / creation stubs) and \XLauncherImpl\ (URI and title launches) offline interfaces.
  3. CTest & Regression Tests:

    • \�cphal: Passed (0.01s)
    • \xgameruntime_offline: Passed (0.01s)
    • \xgameruntime_offline_hdr: Passed (0.01s)
    • Total: 3/3 passed (100%).
    • \ ests/test_gdk_runtime_calls.py: 22/22 tests passed.
  4. Hygiene & Compliance:

    • Clean-room compliant: pure synthetic unit tests; zero game assets or proprietary code.
    • \git diff origin/main..HEAD --check: Clean (0 whitespace/formatting defects).

LGTM!

@Jadiac5
Jadiac5 force-pushed the runtime/acphal-shim branch from 280ee42 to 4bc4661 Compare November 23, 2026 15:41
@Jadiac5

Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, both points were right and are fixed at HEAD:

  1. AcpHalAllocateShapeContexts now writes a pointer only for a non-zero count, so nothing past +0x40 is written at the measured call site. acphal_test has a check for it (a guard value in the sixth pointer stays untouched): 29 checks, 0 failures; ctest acphal, xmem, unimplemented_policy, pe_loader 4/4.
  2. The branch was rebased onto main without 548ac1032 (xgameruntime: serve XAccessibilityImpl and XLauncherImpl offline (speech synthesis, launcher) #6810's commit); it now holds only the three AcpHal commits. This PR does not depend on xgameruntime: serve XAccessibilityImpl and XLauncherImpl offline (speech synthesis, launcher) #6810.

Tested on Windows 11, MSVC 19.44 (AMD Radeon RX 9070 XT, driver 32.0.32029.2010; no GPU used). Not run against the retail executable.

AI-assisted (Claude Code).

@shayderrr shayderrr 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.

Summary

Adds runtime/shims/acphal.cpp and runtime/test/acphal_test.cpp: nine AcpHal.dll imports (APU heap allocation/mapping, shape contexts, and a no-op AcpHal object) are registered, wired into CMake, and covered by tests. Also adds offline XAccessibilityImpl and XLauncherImpl classes, updates the shim registry and feature table, and documents all of it.

Why merge

This fills concrete executable imports that otherwise fail at runtime, modeling the APU heap and audio object with call shapes read from the executable while dropping commands on a PC. The companion offline GDK classes let early boot calls succeed, and both paths ship with dedicated tests and documentation.

Verdict: Approve

@shayderrr

Copy link
Copy Markdown
Collaborator

Summary

Adds runtime/shims/acphal.cpp and runtime/test/acphal_test.cpp: nine AcpHal.dll imports (APU heap allocation/mapping, shape contexts, and a no-op AcpHal object) are registered, wired into CMake, and covered by tests. Also adds offline XAccessibilityImpl and XLauncherImpl classes, updates the shim registry and feature table, and documents all of it.

Why merge

This fills concrete executable imports that otherwise fail at runtime, modeling the APU heap and audio object with call shapes read from the executable while dropping commands on a PC. The companion offline GDK classes let early boot calls succeed, and both paths ship with dedicated tests and documentation.

Verdict: Approve

@Jadiac5
Jadiac5 force-pushed the runtime/acphal-shim branch from 4bc4661 to df9745b Compare November 23, 2026 15:50
@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: acphal_test 29 checks, 0 failures. Windows 11, MSVC 19.44. Not run against the retail executable.

AI-assisted (Claude Code).

@itaskaev2

Copy link
Copy Markdown

Tested 4bc46619 (contains main 1cc6e3c1) on Windows 11, NVIDIA GeForce RTX 4080 SUPER, driver 616.92, MSVC 14.51 (19.51), RelWithDebInfo: pass. AI-assisted (Claude Code), no game files.

The rebase to df9745bd came in while this ran. Its runtime/ changes are line for line the same as 4bc46619's, and acphal.cpp and acphal_test.cpp are byte-identical. Only the base moved. So this applies to the current head, but the run itself was on 4bc46619.

  • acphal_test: 29 checks, 0 failures.
  • Negative control for the +0x40 fix: acphal.cpp put back to c6039e9a with the new test kept gives FAIL a zero count leaves its pointer untouched, 29 checks, 1 failure. Restored to the head, it passes again. So the guard check does catch the sixth-pointer write.
  • Full ctest: 259 of 262 pass, 11 skipped (no Bink, FSR or game inputs). acphal and all 8 xgameruntime* tests pass.
  • The 3 failures are d3d12_indirect_dispatch_count, d3d12_indirect_rt and xbox_present. All three also fail on this machine on main 561b65a8, so they are not from this PR.
  • The full runtime, gta6rt included, builds under 19.51 with no warning from acphal.cpp (/W4 /permissive-).

@quackottu

quackottu commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Tested df9745bd against the retail engine exe (1.0.2.1): merged onto main d2c2ab778, Windows 11, MSVC 19.44, RelWithDebInfo. These results do not depend on the boot. The full gta6rt --run boot test is on hold until the game copy here is complete.

  • acphal_test: 29 checks, 0 failures.
  • gta6rt --dry-run on the exe: all nine AcpHal.dll imports now bind to the shim. Unimplemented imports across the 5 mapped modules drop from 29 on main to 20. The only other change is that those nine are gone. No import becomes newly unimplemented.

@crastinator

Copy link
Copy Markdown
Contributor

Wine data point for #6885: pass.

Built gta6rt from this head merged onto main 87c6c2b2 (clean merge), cross-compiled on Linux with clang-cl 23.1.5 + xwin (SDK 10.0.26100), run under Wine 11.22 (CachyOS, Linux 7.3.5). Checked against the retail executable with gta6rt --dry-run (SHA-256 e95020b2…, the same file as in #6715; no package DLLs on this machine, so the package-module imports stay unimplemented in both columns and only the 30 Xbox-only imports are compared). Counts and API names only.

main 87c6c2b2 main + df9745bd
--dry-run exit / stderr 0 / empty 0 / empty
shim / unimplemented 53 / 102 62 / 93
AcpHal.dll unimplemented 9 0

All nine AcpHal.dll imports (AcpHalCreate, AcpHalAllocateShapeContexts, AcpHalReleaseShapeContexts, ApuCreateHeap, ApuAlloc, ApuFree, ApuIsVirtualAddressValid, ApuMapApuAddress, ApuMapVirtualAddress) now bind to shims, and nothing else in the import list changed.

Full ctest under Wine: 18 failures out of 266 tests with the PR, against 18 out of 265 on main. The extra test is the new acphal, which passes. The 18 failures are identical on both: Wine-side failures that main already has on this machine (gameinput_host, device_proxy, file_system, several d3d12_*, dxgi_swapchain, xbox_present).

Nothing from the executable was run: --dry-run maps and binds only. AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor

Tested df9745b merged with main e6190e7 on Windows 11 / RTX 4080 SUPER against own copy of the retail exe: pass

  • Build: MSVC 14.51 (VS 18), Ninja, RelWithDebInfo, gta6rt + acphal_test. The merge is clean.
  • ctest -R acphal: 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 21 (shim 62). The only difference is AcpHal.dll going from 9 to 0; the other 21 are unchanged. (The earlier head 4bc4661 gave the same counts.)
  • Negative control: the same dry run on main lists all nine AcpHal.dll imports as unimplemented.
  • gta6rt --run with LEONIDA_UNIMPLEMENTED=stop: when it gets to the stop, the PR stops in the same place as main. That is 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. The AcpHal calls are not reached yet, so the PR alone doesn't move --run.
  • Some runs crash before that stop, but main does the same, so the PR doesn't cause it. PR: 2 of 6 runs reached the stop; the other 4 ended with C0000005/C0000374. Main e6190e7 alone: 5 of 8 reached the stop; the other 3 ended with C0000005. These runs were interleaved with the PR's. When the trace records the crash, it is an access violation in ntdll under the loader's TLS host DLL, before any game import is called.

@Jadiac5

Jadiac5 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

New head 921fa777: one docs-only commit, no force-push. Main's new xmem.dll notes landed at the same spot in docs/runtime_imports.md, so the PR conflicted. The AcpHal.dll section now comes after the RT-5 section, with one added sentence saying the RT-5 table predates this shim. No code changed.

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.

10 participants