diff options
| author | andretuta <andreviniciusmerlia@gmail.com> | 2026-11-24 06:21:32 -0300 |
|---|---|---|
| committer | andretuta <andreviniciusmerlia@gmail.com> | 2026-11-24 06:21:32 -0300 |
| commit | 4e9407a409641b644393a66b41d35e431f1c5ddb (patch) | |
| tree | 4fd9f237b0074c1eec460011556350358cddca1c /runtime/test/acphal_test.cpp | |
| parent | 0fe9c0bdfb844678209c0d0ffdd37d2533d1f57e (diff) | |
acphal: isolate voice failures and keep input transitions ordered
Diffstat (limited to 'runtime/test/acphal_test.cpp')
| -rw-r--r-- | runtime/test/acphal_test.cpp | 134 |
1 files changed, 114 insertions, 20 deletions
diff --git a/runtime/test/acphal_test.cpp b/runtime/test/acphal_test.cpp index 540906ec8..3ef8df4cd 100644 --- a/runtime/test/acphal_test.cpp +++ b/runtime/test/acphal_test.cpp | |||
| @@ -21,11 +21,17 @@ | |||
| 21 | 21 | ||
| 22 | // Fail only the calling test thread's designated allocation. | 22 | // Fail only the calling test thread's designated allocation. |
| 23 | static thread_local int allocations_before_failure = -1; | 23 | static thread_local int allocations_before_failure = -1; |
| 24 | static std::atomic<bool> fail_worker_allocation{false}; | ||
| 25 | static DWORD test_thread_id; | ||
| 24 | void* operator new(std::size_t size) { | 26 | void* operator new(std::size_t size) { |
| 25 | if (allocations_before_failure == 0) { | 27 | if (allocations_before_failure == 0) { |
| 26 | allocations_before_failure = -1; | 28 | allocations_before_failure = -1; |
| 27 | throw std::bad_alloc(); | 29 | throw std::bad_alloc(); |
| 28 | } | 30 | } |
| 31 | if (fail_worker_allocation.load() && GetCurrentThreadId() != test_thread_id && | ||
| 32 | fail_worker_allocation.exchange(false)) { | ||
| 33 | throw std::bad_alloc(); | ||
| 34 | } | ||
| 29 | if (allocations_before_failure > 0) --allocations_before_failure; | 35 | if (allocations_before_failure > 0) --allocations_before_failure; |
| 30 | if (void* memory = std::malloc(size ? size : 1)) return memory; | 36 | if (void* memory = std::malloc(size ? size : 1)) return memory; |
| 31 | throw std::bad_alloc(); | 37 | throw std::bad_alloc(); |
| @@ -94,9 +100,13 @@ struct XmaWords { | |||
| 94 | } | 100 | } |
| 95 | }; | 101 | }; |
| 96 | 102 | ||
| 97 | // Synthetic mono XMA2 with a single positive spectral coefficient in each frame. | 103 | // Synthetic XMA2 with one nonzero spectral coefficient per channel and frame. |
| 98 | void write_xma_packet(uint8_t* packet, uint32_t frames, uint8_t packet_skip) { | 104 | void write_xma_packet(uint8_t* packet, uint32_t frames, uint8_t packet_skip, |
| 99 | constexpr uint32_t frame_bits = 82; | 105 | uint32_t rate_index = 3, bool stereo = false) { |
| 106 | constexpr uint32_t scale_bands[] = {23, 24, 23, 23}; | ||
| 107 | const uint32_t bands = scale_bands[rate_index]; | ||
| 108 | const uint32_t channels = stereo ? 2u : 1u; | ||
| 109 | const uint32_t frame_bits = stereo ? 90 + 2 * bands : 59 + bands; | ||
| 100 | std::memset(packet, 0xFF, 2048); | 110 | std::memset(packet, 0xFF, 2048); |
| 101 | std::size_t at = 0; | 111 | std::size_t at = 0; |
| 102 | auto put = [&](uint32_t value, std::size_t width) { | 112 | auto put = [&](uint32_t value, std::size_t width) { |
| @@ -110,17 +120,24 @@ void write_xma_packet(uint8_t* packet, uint32_t frames, uint8_t packet_skip) { | |||
| 110 | for (uint32_t frame = 0; frame < frames; ++frame) { | 120 | for (uint32_t frame = 0; frame < frames; ++frame) { |
| 111 | put(frame_bits, 15); | 121 | put(frame_bits, 15); |
| 112 | put(0b10u, 2); // Fixed layout, one 512-sample subframe. | 122 | put(0b10u, 2); // Fixed layout, one 512-sample subframe. |
| 123 | if (stereo) put(0, 1); // No optional decorrelation matrix. | ||
| 113 | put(0, 11); // Gain, trim, fill and subframe error flag. | 124 | put(0, 11); // Gain, trim, fill and subframe error flag. |
| 114 | put(1, 1); // Coefficients are present. | 125 | if (stereo) put(0b010u, 3); // Two channels without decorrelation. |
| 126 | put((1u << channels) - 1, channels); // Coefficients are present in each channel. | ||
| 115 | put(1, 1); // Explicit vector coefficient count. | 127 | put(1, 1); // Explicit vector coefficient count. |
| 116 | put(0, 8); // Use only run/level coding. | 128 | put(0, 8 * channels); // Use only run/level coding. |
| 117 | put(0, 6); // Quantizer adjustment. | 129 | put(0, 6); // Quantizer adjustment. |
| 118 | put(0, 2); // Scale factor step. | 130 | if (stereo) put(0, 5); // No per-channel quantizer modifiers. |
| 119 | put((1u << 23) - 1, 23); // One zero delta per 48 kHz scale band. | 131 | for (uint32_t channel = 0; channel < channels; ++channel) { |
| 120 | put(0, 1); // Run/level table 0. | 132 | put(0, 2); // Scale factor step. |
| 121 | put(0b00u, 2); // Run 0, level 1. | 133 | put((1u << bands) - 1, bands); // One zero delta per scale band. |
| 122 | put(1, 1); // Positive sign. | 134 | } |
| 123 | put(0b0101011u, 7); // End of coefficients. | 135 | for (uint32_t channel = 0; channel < channels; ++channel) { |
| 136 | put(0, 1); // Run/level table 0. | ||
| 137 | put(0b00u, 2); // Run 0, level 1. | ||
| 138 | put(channel == 0 ? 1u : 0u, 1); | ||
| 139 | put(0b0101011u, 7); // End of coefficients. | ||
| 140 | } | ||
| 124 | put(0, 1); // Frame padding bit. | 141 | put(0, 1); // Frame padding bit. |
| 125 | put(frame + 1 < frames ? 1 : 0, 1); | 142 | put(frame + 1 < frames ? 1 : 0, 1); |
| 126 | } | 143 | } |
| @@ -130,6 +147,7 @@ void write_xma_packet(uint8_t* packet, uint32_t frames, uint8_t packet_skip) { | |||
| 130 | 147 | ||
| 131 | int main() { | 148 | int main() { |
| 132 | std::setvbuf(stdout, nullptr, _IONBF, 0); | 149 | std::setvbuf(stdout, nullptr, _IONBF, 0); |
| 150 | test_thread_id = GetCurrentThreadId(); | ||
| 133 | std::printf("acphal_test: registered acphal.dll shim verification\n"); | 151 | std::printf("acphal_test: registered acphal.dll shim verification\n"); |
| 134 | 152 | ||
| 135 | const auto create = reinterpret_cast<AcpHalCreateFn>( | 153 | const auto create = reinterpret_cast<AcpHalCreateFn>( |
| @@ -174,12 +192,19 @@ int main() { | |||
| 174 | "AcpHalCreate returns S_OK with a non-null object"); | 192 | "AcpHalCreate returns S_OK with a non-null object"); |
| 175 | check(create(nullptr) == E_POINTER, | 193 | check(create(nullptr) == E_POINTER, |
| 176 | "AcpHalCreate(nullptr) returns E_POINTER"); | 194 | "AcpHalCreate(nullptr) returns E_POINTER"); |
| 195 | void* failed_hal = acp; | ||
| 196 | allocations_before_failure = 0; | ||
| 197 | const HRESULT create_failure = create(&failed_hal); | ||
| 198 | allocations_before_failure = -1; | ||
| 199 | check(create_failure == E_OUTOFMEMORY && failed_hal == nullptr, | ||
| 200 | "AcpHalCreate returns E_OUTOFMEMORY and clears the output"); | ||
| 177 | 201 | ||
| 178 | if (acp) { | 202 | if (acp) { |
| 179 | using Slot0Fn = HRESULT(__stdcall*)(void*, uint32_t, uint32_t); | 203 | using Slot0Fn = HRESULT(__stdcall*)(void*, uint32_t, uint32_t); |
| 180 | using Slot1Fn = HRESULT(__stdcall*)(void*); | 204 | using Slot1Fn = HRESULT(__stdcall*)(void*); |
| 181 | using Slot2Fn = HRESULT(__stdcall*)(void*, uint32_t, uint32_t, uint32_t, const void*, uint32_t); | 205 | using Slot2Fn = HRESULT(__stdcall*)(void*, uint32_t, uint64_t, uint32_t, const void*, uint32_t); |
| 182 | using Slot3Fn = uint32_t(__stdcall*)(void*); | 206 | using Slot3Fn = uint32_t(__stdcall*)(void*, void*); |
| 207 | using Slot4Fn = uint32_t(__stdcall*)(void*); | ||
| 183 | using Slot5Fn = HRESULT(__stdcall*)(void*); | 208 | using Slot5Fn = HRESULT(__stdcall*)(void*); |
| 184 | 209 | ||
| 185 | auto vtable = *static_cast<void* const* const*>(acp); | 210 | auto vtable = *static_cast<void* const* const*>(acp); |
| @@ -200,8 +225,8 @@ int main() { | |||
| 200 | "IAcpHal slot 2 (submit_command) returns S_OK (vital boot unblocker)"); | 225 | "IAcpHal slot 2 (submit_command) returns S_OK (vital boot unblocker)"); |
| 201 | 226 | ||
| 202 | // Slots 3 and 4: Message polling returns 0 | 227 | // Slots 3 and 4: Message polling returns 0 |
| 203 | check(reinterpret_cast<Slot3Fn>(vtable[3])(acp) == 0, "IAcpHal slot 3 returns 0"); | 228 | check(reinterpret_cast<Slot3Fn>(vtable[3])(acp, nullptr) == 0, "IAcpHal slot 3 returns 0"); |
| 204 | check(reinterpret_cast<Slot3Fn>(vtable[4])(acp) == 0, "IAcpHal slot 4 returns 0"); | 229 | check(reinterpret_cast<Slot4Fn>(vtable[4])(acp) == 0, "IAcpHal slot 4 returns 0"); |
| 205 | 230 | ||
| 206 | // Slot 1: Disconnect returns S_OK | 231 | // Slot 1: Disconnect returns S_OK |
| 207 | check(reinterpret_cast<Slot1Fn>(vtable[1])(acp) == S_OK, "IAcpHal slot 1 returns S_OK"); | 232 | check(reinterpret_cast<Slot1Fn>(vtable[1])(acp) == S_OK, "IAcpHal slot 1 returns S_OK"); |
| @@ -211,8 +236,8 @@ int main() { | |||
| 211 | } | 236 | } |
| 212 | 237 | ||
| 213 | // 2. ApuCreateHeap: title passes (cachedSize, nonCachedSize=0x226e00) | 238 | // 2. ApuCreateHeap: title passes (cachedSize, nonCachedSize=0x226e00) |
| 214 | check(create_heap(4 * 1024 * 1024, 0x226e00) == S_OK, | 239 | check(create_heap(4 * 1024 * 1024 + 0x400, 0x226e00) == S_OK, |
| 215 | "ApuCreateHeap(4 MiB, 0x226e00) succeeds"); | 240 | "ApuCreateHeap succeeds with an unaligned cached heap size"); |
| 216 | check(create_heap(4 * 1024 * 1024, 0x226e00) != S_OK, | 241 | check(create_heap(4 * 1024 * 1024, 0x226e00) != S_OK, |
| 217 | "ApuCreateHeap second call fails (heap already created)"); | 242 | "ApuCreateHeap second call fails (heap already created)"); |
| 218 | 243 | ||
| @@ -329,8 +354,12 @@ int main() { | |||
| 329 | "SHAPE allocation can be retried after memory pressure"); | 354 | "SHAPE allocation can be retried after memory pressure"); |
| 330 | 355 | ||
| 331 | // Release shape contexts | 356 | // Release shape contexts |
| 357 | std::memset(&ctx_all, 0x55, sizeof(ctx_all)); | ||
| 332 | check(release_ctx(&ctx_all) == S_OK, "AcpHalReleaseShapeContexts succeeds"); | 358 | check(release_ctx(&ctx_all) == S_OK, "AcpHalReleaseShapeContexts succeeds"); |
| 333 | check(ctx_all.xmaContextArray == nullptr, "xmaContextArray is reset to null"); | 359 | const auto* released_argument = reinterpret_cast<const uint8_t*>(&ctx_all); |
| 360 | check(std::all_of(released_argument, released_argument + sizeof(ctx_all), | ||
| 361 | [](uint8_t b) { return b == 0x55; }), | ||
| 362 | "SHAPE release does not write through an opaque or recycled argument"); | ||
| 334 | check(release_ctx(nullptr) == S_OK, "AcpHalReleaseShapeContexts(nullptr) succeeds"); | 363 | check(release_ctx(nullptr) == S_OK, "AcpHalReleaseShapeContexts(nullptr) succeeds"); |
| 335 | 364 | ||
| 336 | // 7. ACP command/message queue protocol (register, complete, overflow drops, disconnect, notify word) | 365 | // 7. ACP command/message queue protocol (register, complete, overflow drops, disconnect, notify word) |
| @@ -410,6 +439,11 @@ int main() { | |||
| 410 | std::memcpy(&fg_id, msg.data, 4); | 439 | std::memcpy(&fg_id, msg.data, 4); |
| 411 | check(fg_id == 0x42u, "FLOWGRAPH_COMPLETED carries flowgraph word"); | 440 | check(fg_id == 0x42u, "FLOWGRAPH_COMPLETED carries flowgraph word"); |
| 412 | 441 | ||
| 442 | const uint32_t command_completed_mask = 0x10u; | ||
| 443 | check(fn_submit(q_acp, 3, 0x100u, 0xffffffffu, &command_completed_mask, 0) == S_OK && | ||
| 444 | fn_count(q_acp) == 0u, | ||
| 445 | "UNREGISTER_MESSAGES suppresses command-completed messages"); | ||
| 446 | |||
| 413 | // Disconnect posts DISCONNECTED. | 447 | // Disconnect posts DISCONNECTED. |
| 414 | check(fn_disconnect(q_acp) == S_OK, "Disconnect succeeds when connected"); | 448 | check(fn_disconnect(q_acp) == S_OK, "Disconnect succeeds when connected"); |
| 415 | check(fn_count(q_acp) == 1u, "Disconnect queued DISCONNECTED message"); | 449 | check(fn_count(q_acp) == 1u, "Disconnect queued DISCONNECTED message"); |
| @@ -534,8 +568,9 @@ int main() { | |||
| 534 | 568 | ||
| 535 | void* in_va = nullptr; | 569 | void* in_va = nullptr; |
| 536 | uint32_t in_apu = 0; | 570 | uint32_t in_apu = 0; |
| 537 | check(alloc(&in_va, &in_apu, 0x2000, 0x800, 1) == S_OK && in_va != nullptr, | 571 | check(alloc(&in_va, &in_apu, 0x2000, 0x800, 2) == S_OK && in_va != nullptr && |
| 538 | "ApuAlloc 4-packet loop and refill input buffer"); | 572 | (in_apu & 0x7FFu) == 0 && map_apu(in_apu) == in_va, |
| 573 | "Non-cached XMA input keeps its requested alignment and round trips"); | ||
| 539 | auto* packets = static_cast<uint8_t*>(in_va); | 574 | auto* packets = static_cast<uint8_t*>(in_va); |
| 540 | write_xma_packet(packets, 1, 1); // Packet 1 belongs to another stream. | 575 | write_xma_packet(packets, 1, 1); // Packet 1 belongs to another stream. |
| 541 | write_xma_packet(packets + 0x800, 2, 0); | 576 | write_xma_packet(packets + 0x800, 2, 0); |
| @@ -651,6 +686,10 @@ int main() { | |||
| 651 | write_xma_packet(packets, 3, 0); | 686 | write_xma_packet(packets, 3, 0); |
| 652 | check(fn_submit(x_acp, 5, 0xf6, 0xffffffffu, &voice_index, 0) == S_OK && wait_for_ring(), | 687 | check(fn_submit(x_acp, 5, 0xf6, 0xffffffffu, &voice_index, 0) == S_OK && wait_for_ring(), |
| 653 | "First packet fills the ring and leaves queued PCM"); | 688 | "First packet fills the ring and leaves queued PCM"); |
| 689 | TestShapeContexts source_only{}; | ||
| 690 | source_only.numSrcContexts = 1; | ||
| 691 | check(alloc_ctx(&source_only) == S_OK && source_only.srcContextArray != nullptr, | ||
| 692 | "A non-XMA allocation can coexist with the active XMA table"); | ||
| 654 | words[0].fetch_or(1u << 20); | 693 | words[0].fetch_or(1u << 20); |
| 655 | check(fn_submit(x_acp, 5, 0xf7, 0xffffffffu, &voice_index, 0) == S_OK, | 694 | check(fn_submit(x_acp, 5, 0xf7, 0xffffffffu, &voice_index, 0) == S_OK, |
| 656 | "Re-enable while refilling the current input"); | 695 | "Re-enable while refilling the current input"); |
| @@ -681,6 +720,61 @@ int main() { | |||
| 681 | check(fn_submit(x_acp, 7, 0xfc, 0xffffffffu, &voice_index, 0) == S_OK, | 720 | check(fn_submit(x_acp, 7, 0xfc, 0xffffffffu, &voice_index, 0) == S_OK, |
| 682 | "Disable the two-buffer refill voice"); | 721 | "Disable the two-buffer refill voice"); |
| 683 | 722 | ||
| 723 | // Both sides were empty. When both are refilled, the title's first input wins. | ||
| 724 | write_xma_packet(packets, 1, 0); | ||
| 725 | configure_voice(0, 3, 4, 0, 0); | ||
| 726 | words[0] = 1u | (1u << 20) | (4u << 22); | ||
| 727 | const uint32_t voice_bit = 1u << voice_index; | ||
| 728 | check(fn_submit(x_acp, 6, 0, 0xffffffffu, &voice_bit, 0) == S_OK && wait_for_ring(), | ||
| 729 | "ENABLE_XMAS starts the selected voice and exhausts the first input"); | ||
| 730 | write_xma_packet(packets, 2, 0); | ||
| 731 | write_xma_packet(packets + 0x800, 1, 0); | ||
| 732 | words[6] = in_apu + 0x800u; | ||
| 733 | words[1].fetch_or(1u); | ||
| 734 | words[0].fetch_or(3u << 20); | ||
| 735 | check(fn_submit(x_acp, 18, 0, 0xffffffffu, refill_read_args, 0) == S_OK && wait_for_ring(), | ||
| 736 | "A double refill supplies the next PCM blocks"); | ||
| 737 | check(((words[0] >> 20) & 3u) == 2u && (words[4] >> 31) == 1u, | ||
| 738 | "A double refill plays input 0 before input 1"); | ||
| 739 | #if defined(LEONIDA_HAVE_XMA2_DECODER) | ||
| 740 | check(has_signal(ring_bytes), "Rust decodes non-silent PCM after a double refill"); | ||
| 741 | #endif | ||
| 742 | |||
| 743 | // Reconfigure the still-enabled slot and verify that no queued PCM or old format survives. | ||
| 744 | bool reconfigured = true; | ||
| 745 | for (uint32_t rate = 0; rate < 4; ++rate) { | ||
| 746 | const bool stereo = (rate & 1u) == 0; | ||
| 747 | write_xma_packet(packets, 1, 0, rate, stereo); | ||
| 748 | words[0] = 1u | (1u << 20) | (16u << 22); | ||
| 749 | words[2] = 32u; | ||
| 750 | words[4] = 0u; | ||
| 751 | words[9] = 12u; | ||
| 752 | std::memset(out_va, 0, 0x1000); | ||
| 753 | // Enable applies the new format before making the output writable. | ||
| 754 | words[1] = ((rate | (stereo ? 4u : 0u)) << 27); | ||
| 755 | reconfigured &= fn_submit(x_acp, 5, 0, 0xffffffffu, &voice_index, 0) == S_OK; | ||
| 756 | reconfigured &= fn_submit(x_acp, 18, 0, 0xffffffffu, refill_read_args, 0) == S_OK; | ||
| 757 | for (int wait = 0; wait < 1000 && (words[2] >> 31) == 0u; ++wait) Sleep(1); | ||
| 758 | reconfigured &= (words[2] >> 31) == 1u && ((words[0] >> 27) & 0x1Fu) == (stereo ? 8u : 4u); | ||
| 759 | #if defined(LEONIDA_HAVE_XMA2_DECODER) | ||
| 760 | reconfigured &= has_signal(ring_bytes); | ||
| 761 | #endif | ||
| 762 | } | ||
| 763 | check(reconfigured, "Re-enabled voices select mono or stereo PCM at all four sample rates"); | ||
| 764 | check(fn_submit(x_acp, 8, 0, 0xffffffffu, &voice_bit, 0) == S_OK, | ||
| 765 | "DISABLE_XMAS stops the selected voice"); | ||
| 766 | |||
| 767 | configure_voice(0, 3, 4, 0, 0); | ||
| 768 | write_xma_packet(packets, 1, 1); | ||
| 769 | fail_worker_allocation.store(true); | ||
| 770 | check(fn_submit(x_acp, 5, 0, 0xffffffffu, &voice_index, 0) == S_OK, | ||
| 771 | "Enable a voice before an injected worker allocation failure"); | ||
| 772 | for (int wait = 0; wait < 1000 && (words[2] >> 31) == 0u; ++wait) Sleep(1); | ||
| 773 | check(!fail_worker_allocation.exchange(false) && (words[2] >> 31) != 0u, | ||
| 774 | "Worker allocation failure surfaces an XMA error without terminating the process"); | ||
| 775 | check(fn_submit(x_acp, 5, 0, 0xffffffffu, &voice_index, 0) == S_OK && wait_for_ring(), | ||
| 776 | "A voice can run again after a worker allocation failure"); | ||
| 777 | |||
| 684 | check(release_ctx(x_acp) == S_OK, "Release SHAPE contexts through the opaque AcpHal handle"); | 778 | check(release_ctx(x_acp) == S_OK, "Release SHAPE contexts through the opaque AcpHal handle"); |
| 685 | check(fn_release(x_acp) == S_OK, "Release packet loop AcpHal succeeds"); | 779 | check(fn_release(x_acp) == S_OK, "Release packet loop AcpHal succeeds"); |
| 686 | free_mem(in_va); | 780 | free_mem(in_va); |