Skip to content

hle/xaudio2: enforce XAudio2 play/loop region invariants in clamp_regions and silent_buffer - #89

Merged
ivall merged 1 commit into
xcl-dev:mainfrom
certified-retart:fix/xaudio2-region-clamping
Nov 23, 2026
Merged

ivall merged 1 commit into
xcl-dev:mainfrom
certified-retart:fix/xaudio2-region-clamping

Conversation

@certified-retart

Copy link
Copy Markdown
Contributor

Summary

Hardens hle::xaudio2::clamp_regions and hle::xaudio2::silent_buffer so decoded and fallback buffers always satisfy XAudio2 IXAudio2SourceVoice::SubmitSourceBuffer (XAUDIO2_BUFFER) play/loop region invariants instead of failing with XAUDIO2_E_INVALID_CALL:

  • clamp_regions:
    • Compares play_length > total_frames - play_begin instead of play_begin.saturating_add(play_length) > total_frames, preventing a u32 overflow/panic when total_frames == u32::MAX.
    • Sets explicit play_length = total_frames - play_begin when play_begin > 0 && play_length == 0 (XAudio2 documents PlayLength == 0 requires PlayBegin == 0).
    • Clears stale loop_begin / loop_length when loop_count == 0.
    • Clamps loop regions against play_end (play_begin + play_length when play_length > 0, else total_frames) rather than just total_frames:
      • Disables the loop (loop_begin = loop_length = loop_count = 0) when loop_begin >= play_end or loop_begin + loop_length <= play_begin.
      • Sets explicit loop_length = play_end - loop_begin when loop_length == 0 and either loop_begin > 0 or play_end < total_frames (XAudio2 requires LoopBegin == 0 when LoopLength == 0).
      • Still allows valid pre-roll loops (loop_begin < play_begin with loop_begin + loop_length > play_begin && <= play_end).
  • silent_buffer:
    • Ensures loop_begin > 0 && loop_length == 0 allocates at least loop_begin + 1 frames.
    • Caps max_frames to XAUDIO2_MAX_BUFFER_BYTES (0x8000_0000) / frame_bytes.
    • Runs clamp_regions(&mut nb, frames as u32) before returning, so both normal and truncated/zero-byte silence buffers obey all XAUDIO2_BUFFER invariants (including clearing loop_count when max_bytes == 0).

Verification

  • cargo test -p xcl --lib hle::xaudio2 -- --test-threads=1: 9 passed; 0 failed (live Windows system XAudio2 2.9)
    • Added clamp_regions_enforces_play_end_and_loop_invariants (including u32::MAX overflow and loop_end == play_begin boundary).
    • Added silent_buffer_clamps_regions_and_zero_max_bytes.
    • Added clamped_and_silenced_buffers_accepted_by_windows_xaudio2, which creates a real Windows PCM16 IXAudio2SourceVoice and verifies SubmitSourceBuffer returns S_OK across all clamped and silenced edge cases.

@Lum7nt

Lum7nt commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested locally against the Windows 11 system XAudio2 2.9 engine — all 9 tests pass cleanly.

Key improvements verified:

  1. XAudio2 specification conformance: Real IXAudio2SourceVoice::SubmitSourceBuffer calls succeed across all boundary conditions (play length clamping to total frames, loop length clamped to play_end, and zeroing out stale loop parameters when loop_count == 0).
  2. Robust silence generation: silent_buffer correctly clamps bounds and respects XAUDIO2_MAX_BUFFER_BYTES, preventing XAUDIO2_E_INVALID_CALL crashes in audio-heavy titles.

Solid bugfix with thorough live-API test coverage.

@ivall
ivall merged commit ef6b342 into xcl-dev:main Nov 23, 2026
@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Merged. Tested on Windows 11 with an RX 9070 (RDNA4): cargo test --workspace green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with XCL_SPLIT_WAVE64=1; Unicorn Overlord and XDefiant run with this round merged. Thanks!

Michael-Glaser added a commit to Michael-Glaser/xcl that referenced this pull request Nov 24, 2026
…cl-dev#104)

On newer GDK runtimes (QUEUE_SHIFTED), 20 command list console methods shift up by +2 via NEWER_LIST_SLOTS, leaving 11 older slots (xcl-dev#59, xcl-dev#60, xcl-dev#68, xcl-dev#69, xcl-dev#78, xcl-dev#81, xcl-dev#84, xcl-dev#88, xcl-dev#89, xcl-dev#93, xcl-dev#94) without another shifted method taking their old index. Those slots remained wired directly to the older layout's handlers, so a newer-layout call hit the wrong method.

Wrap those orphaned slots in make_layout_split so newer-layout title calls go to a logged stub while older-layout titles keep the handler and non-title callers keep their PC target (including cl_execute_indirect at xcl-dev#59 and the original base-table stub for slots >= scanned). Slot xcl-dev#10 (Reset) stays Reset on both layouts and is not orphaned.

Assisted-by: Antigravity (Gemini 4 Neon)
vincentes added a commit to vincentes/xcl-wasm that referenced this pull request Nov 24, 2026
The null device becomes `win::d3d12x`, a device that does the memory side
of D3D12.X for real and still renders nothing:

- Unified memory, as on the console: a resource's GPU address is its
  address in the title's address space. Committed resources and heaps get
  graphics memory, placed resources lie in their heap, and
  CreatePlacedResourceX (xcl-dev#44) puts one on memory the title allocated; Map
  returns that address. Read-back buffers up to 64 MiB come from the
  fourth 4 GiB of the region, where XCL's Windows runtime keeps them.
- Command lists record; a queue runs what it is given, in order, at every
  turn of the scheduler (`pump`): buffer copies, the console's memory
  commands (CopyMemoryToMemoryX xcl-dev#60, FillMemoryWith32BitValueX xcl-dev#61,
  Write32 / 64BitValue…X xcl-dev#69 / xcl-dev#70 / xcl-dev#89 with the GPU timestamp) and
  Wait32 / 64BitValueX (xcl-dev#71 / xcl-dev#72), which hold the queue until another
  thread writes the value. KickoffX (xcl-dev#93) starts a list before it is
  closed. A fence moves, and its events are signalled, when the queue
  gets to its Signal.
- The console's layouts: device extensions from slot 44
  (CreateCommandQueueX, CreateCommandListX with the DMA engine's own list
  interface, page pools, pipeline-state creation as objects, …), the
  graphics command list's console slots, AllocateUploadMemory (xcl-dev#148).
- CheckFeatureSupport zeroes the whole structure; a raytracing
  acceleration structure view writes the console's descriptor words.

Draws, pipelines, other views, PresentX and the frame clock do nothing
or are missing. Dev builds optimise `xcl-emu` (the title test: 37 s →
3.3 s).

Measured on macOS (aarch64): the test title passes 40 of its checks (9 of
D3D12.X's 16); the 6 that fail before it aborts in its XG texture check
need HLSL compilation and rendering, the frame clock, or DirectStorage.
`cargo test -p xcl-guest`: 38 passed, 0 failed (the title test now checks
every check by name; the staged tree was built and tested apart from
the working tree). Inside the wasm64 module (1.2 MB, Node 24) the same
run takes 4.4 s: 115 M instructions, most of them the 80 MiB upload check.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Michael-Glaser added a commit to Michael-Glaser/xcl that referenced this pull request Nov 24, 2026
…vall#104)

On newer GDK runtimes (QUEUE_SHIFTED), 20 command list console methods shift up by +2 via NEWER_LIST_SLOTS, leaving 11 older slots (ivall#59, ivall#60, ivall#68, ivall#69, ivall#78, ivall#81, ivall#84, ivall#88, ivall#89, ivall#93, ivall#94) without another shifted method taking their old index. Those slots remained wired directly to the older layout's handlers, so a newer-layout call hit the wrong method.

Wrap those orphaned slots in make_layout_split so newer-layout title calls go to a logged stub while older-layout titles keep the handler and non-title callers keep their PC target (including cl_execute_indirect at ivall#59 and the original base-table stub for slots >= scanned). Slot ivall#10 (Reset) stays Reset on both layouts and is not orphaned.

Assisted-by: Antigravity (Gemini 4 Neon)
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 27, 2026
The null device's plain lists ran the console memory slots (xcl-dev#60/xcl-dev#61/
xcl-dev#69-xcl-dev#72/xcl-dev#89/xcl-dev#93) as logging stubs, so the title's console_list_methods
check read back 0x0 on the null backend while the identical immediate
host writes already passed on Vulkan lists. The vtable now wires the
same VulList_* functions into Kind::CommandList; GetDevice (xcl-dev#7),
the UAV barrier (xcl-dev#146) and AllocateUploadMemory (xcl-dev#148) stay
Vulkan-only with their slices (barrier/upload ride with raytracing,
which still fails on null at the OPTIONS5 tail-zero assert).

Failing-first: the new null_list_shares_the_console_memory_methods
test fails pre-route (slot 69 stub writes nothing) and passes now
(write32/fill/copy/timestamp land, pre-satisfied wait returns,
kickoff is a silent no-op), mirroring the title's asserts.
Gates: check -p xcl --tests EXIT 0; check --workspace EXIT 0; lib 797
passed/0 failed/1 ignored (--test-threads=4). Title e2e, fresh roots:
null 12->11 FAILs (Write32 fixed, byte-identical remainder), Vulkan 9
unchanged with the real present (640x360 token 0x1 on Intel Arc).
Retail HOLDS (SIGSEGV exe+0x2237320, exit 139, called_stubs 20,
0 d3d12).
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