Repository navigation
audio: aria: fix over-read of the internal ring buffer - #11499
Shrusti-pk wants to merge 3 commits into
Conversation
|
Can one of the admins verify this patch?
|
|
test this please |
lgirdwood
left a comment
There was a problem hiding this comment.
Also missing a SoB - pls add this to your Agents workflow.
004a5c7 to
b59aa97
Compare
|
Added the sign-off. The ZTest failure is sof.unit.audio.buffer not finding sof/kernel/drivers/interrupt-map.h after the drivers header move; it fails the same way on the other PRs today and the aria test itself passed in that run. |
|
@Shrusti-pk I've run out of co-pilot credits, so gemini reviewing for any gaps. |
lgirdwood
left a comment
There was a problem hiding this comment.
Great root-cause writeup and standalone module_adapter ztest suite (sof.unit.audio.aria)! Note that rebasing onto current main will pick up d9b1a7879 ("test: ztest: unit: audio: buffer: add src/kernel/include to include path") and clear the unrelated sof.unit.audio.buffer CI failure.
Found two remaining buffer/SIMD bugs exposed by odd-sample rings and non-frame-multiple sink buffers, plus two small test/allocation improvements:
- Blocking (HiFi3/HiFi4 odd-channel bug —
src/audio/aria/aria_hifi3.c:95):aria_algo_get_data_odd_channel()still callsinu = AE_LA64_PP(in)before a scalarAE_L32_XPloop that never usesinu. On HiFi3/HiFi4,AE_LA64_PP(in)incrementsinby+8bytes whenever((uintptr_t)in & 7) != 0— which is now hit on odd-sample rings (chan_cnt * smpl_group_cntodd) or odd-frame periods, causingAE_L32_XPto skip 2 samples and read 8 bytes pastcd->data_end(and the(ae_int32 *)incast-as-lvalue also breaks Xtensa LLVM Clang builds). - Sink buffer over-write when
snk_sizeis not a multiple offrame_bytes(src/audio/aria/aria_generic.c:70-75,aria_hifi3.c:91-96, 151-156):ipc4_comp_connect()sizes buffers asMAX(ibs, obs) * 2in raw bytes; ifm = cir_buf_samples_without_wrap_s32(out, sink->buf_end)is not divisible bych_n(e.g.ch_n = 3with a power-of-2 downstreamibs),for (i = 0; i < n; i += ch_n)writes up toch_n - 1samples pastsink->buf_endand desynchronizessamples -= n. get_required_memory()(src/audio/aria/aria.c:53, 76-78): Dropping the leftoverALIGN_UP(..., 2)avoids 4 unused bytes aftercd->data_endand places CI ASan's heap redzone directly atcd->data_end(564bytes instead of568bytes) for odd-sample rings.- Ztest coverage (
test/ztest/unit/audio/aria/aria_ztest.c:254-267): Suggest adding a combined3 ch + short first period + sink wrapcase and anattenuation = 0case to cover the bypasscir_buf_copy()path.
| m = cir_buf_samples_without_wrap_s32(in, cd->data_end); | ||
| n = MIN(m, n); | ||
| inu = AE_LA64_PP(in); | ||
| for (i = 0; i < n; i += ch_n) { |
There was a problem hiding this comment.
Bug on HiFi3/HiFi4 when in is 4-byte-only aligned (((uintptr_t)in & 7) == 4):
aria_algo_get_data_odd_channel() uses scalar 32-bit AE_L32_XP / AE_S32_L_XP in the inner loop (inu and outu are never used), so inu = AE_LA64_PP(in) at line 95 and AE_SA64POS_FP(outu, out) at line 108 are leftover from aria_algo_get_data_even_channel().
However, AE_LA64_PP(in) (ae_la64.pp) modifies in in place when in is not 8-byte aligned: for ((uintptr_t)in & 7) == 4, it primes inu from in & ~7 and increments in by +8 bytes (2 samples). With an odd-sample ring (chan_cnt * smpl_group_cnt odd, e.g. 3 * 47 = 141) or after any odd-frame period / sink wrap, in enters this loop at 4 mod 8. AE_LA64_PP(in) then advances in by 2 samples after m = cir_buf_samples_without_wrap_s32(in, cd->data_end) was computed, so the AE_L32_XP loop:
- skips 2 samples (rotating channels),
- reads 2 samples (
8bytes) pastcd->data_end, and - wraps
intocd->data_addr + 2(off frame boundary).
Also note that typing in / out as const ae_int32 *in = cd->data_ptr; and ae_int32 *out = sink->ptr; (matching aria_hifi5.c:83-84) and removing inu, outu, AE_LA64_PP(in), and AE_SA64POS_FP(outu, out) avoids the (ae_int32 *)in / (ae_int32 *)out cast-as-lvalue extension on lines 99 & 104, which fails to compile under Xtensa LLVM Clang (cannot take the address of an rvalue of type 'ae_int32 *').
There was a problem hiding this comment.
AE_LA64_PP takes the pointer by value, so it can't move in; the prime and the AE_SA64POS_FP flush were dead code left over from the even channel path. The casts on the AE_L32_XP and AE_S32_L_XP operands are a real problem for clang though, so the odd channel path now uses ae_int32 pointers like the HiFi5 one and drops the align registers. It's a separate commit since it's a cleanup with no behaviour change. I still can't build the HiFi3 file here.
| m = cir_buf_samples_without_wrap_s32(out, sink->buf_end); | ||
| n = MIN(m, samples); | ||
| m = cir_buf_samples_without_wrap_s32(cd->data_ptr, cd->data_end); | ||
| m = cir_buf_samples_without_wrap_s32(in, cd->data_end); | ||
| n = MIN(m, n); | ||
| for (i = 0; i < n; i += ch_n) { | ||
| for (ch = 0; ch < ch_n; ch++) { |
There was a problem hiding this comment.
While cd->data_end - cd->data_addr is now guaranteed to be a multiple of ch_n, sink->buf_end - sink->buf_start is not guaranteed to be a multiple of frame_bytes (ipc4_comp_connect() sizes inter-module buffers as MAX(ibs, obs) * 2 in raw bytes).
For ch_n = 3, 5, 6, 7, if a connected module specifies a power-of-2 ibs/obs (e.g. 1024 -> 2048 bytes = 512 int32_t samples, 512 % 3 == 2), m = cir_buf_samples_without_wrap_s32(out, sink->buf_end) near the end of the sink buffer is not divisible by ch_n (e.g. n = 2 or n = 5 for ch_n = 3). for (i = 0; i < n; i += ch_n) will then execute a full ch_n-sample inner iteration on the tail, writing up to ch_n - 1 samples past sink->buf_end while samples -= n only subtracts n (same loop bound in aria_hifi3.c:96, 156).
Either keep ch across outer-loop iterations (for (i = 0; i < n; i++) stepping gain += step every ch_n samples) or reject non-frame-aligned snk_size / src_size (snk_size % frame_bytes != 0, which aria_hifi5.c's hardware circular buffer AE_SETCEND1(sink->buf_end) also requires).
There was a problem hiding this comment.
Went with rejecting it at prepare: aria_prepare() now checks that the source and sink buffers hold a whole number of frames and fails with -EINVAL otherwise. Handling a partial frame at the sink wrap would have meant restructuring all three get_data loops, and the HiFi5 circular addressing needs frame sized buffers anyway. It's its own commit, with a ztest case that connects a sink of 70 frames plus one sample and a source of 94 frames plus one sample and expects prepare to fail.
| * they stay 8-byte aligned. The allocation may be one sample larger, | ||
| * see get_required_memory(). | ||
| */ | ||
| cd->buff_size = cd->chan_cnt * cd->smpl_group_cnt; |
There was a problem hiding this comment.
Now that cd->buff_size = cd->chan_cnt * cd->smpl_group_cnt and cd->offset is removed, ALIGN_UP(num_of_ms * chan_cnt * smpl_group_cnt, 2) in get_required_memory() (line 53) is dead padding: for odd chan_cnt * smpl_group_cnt, those extra 4 bytes past cd->data_end are never initialized or accessed.
More importantly, in sof.unit.audio.aria (which CI runs with --enable-asan via malloc(bytes) in test/ztest/unit/common/alloc.c), get_required_memory(3, 47) allocates 568 bytes (142 samples) instead of 564 bytes (141 samples), putting ASan's heap redzone 4 bytes past cd->data_end and hiding any 1-sample (4-byte) over-read in test_odd_samples_per_chunk. Removing ALIGN_UP(..., 2) in get_required_memory() puts the ASan redzone directly at cd->data_end.
There was a problem hiding this comment.
Dropped. get_required_memory() now returns exactly chan_cnt * smpl_group_cnt samples, so the 3 channel ring in the test is 564 bytes with the ASan redzone right at data_end.
| ZTEST(aria_suite, test_sink_wrap_before_ring_wrap) | ||
| { | ||
| run_aria(2, 48, 96, 70, 20, 1); | ||
| } | ||
|
|
||
| /* | ||
| * 3 channels and a 47 frame chunk give an odd number of samples per chunk. | ||
| * The ring must still hold whole frames, so no frame straddles its end and | ||
| * channels stay in place. | ||
| */ | ||
| ZTEST(aria_suite, test_odd_samples_per_chunk) | ||
| { | ||
| run_aria(3, 47, 94, 94, 47, 1); | ||
| } |
There was a problem hiding this comment.
Nice test harness through module_adapter! Consider adding two more run_aria() calls (or separate ZTEST cases) to cover:
- Combined odd channels + short first period + sink wrap before ring wrap:
run_aria(3, 47, 94, 70, 20, 1); attenuation = 0(cd->att == 0bypasscir_buf_copy()path inaria.c:99-103):run_aria(3, 47, 94, 70, 20, 0);
There was a problem hiding this comment.
Added both. The combined case fails on the old code with the same over-read, and the attenuation 0 case fails as well, with the wrong frames rather than a crash, since the padded ring put the bypass copy one sample out of step.
aria keeps the last smpl_group_cnt frames of input in an internal ring and outputs them delayed by that amount. aria_algo_get_data() bounds each chunk by the distance from cd->data_ptr to the ring end, but when the sink buffer wraps first the read pointer has already moved past data_ptr, and the next chunk is still bounded by the old distance, so it reads past data_end. This happens once a short period has moved data_ptr away from the ring start and the sink buffer is not a multiple of the ring size, for example when the host sets obs different from ibs in the module init payload. Both HiFi3 variants have the same code. aria_algo_init() also rounded the ring up to an even number of samples and started it one sample in. With an odd channel count and an odd number of frames per chunk the ring then does not hold whole frames, so the frame loop reads up to channels - 1 samples past the end and the output channels rotate by one sample every period. Take the wrap distance from the read pointer, and size the ring to exactly chan_cnt * smpl_group_cnt samples from the start of the allocation. get_required_memory() allocates exactly that, without the old rounding to an even sample count that only left one unused sample after the ring. The HiFi5 variant uses circular addressing and only needed the ring size. Configurations with matching ibs and obs and an even number of samples per chunk produce the same output as before. The ring is an odd number of samples only for an odd channel count, and the odd channel HiFi3 and HiFi5 paths access it with 32-bit scalar loads and stores. The even channel paths load two samples at a time, and there the ring stays a multiple of 8 bytes and the pointers advance by whole frames, so they keep the alignment of the allocation. Add an aria ztest that runs the module through the module adapter and checks the delayed output frame by frame: a sink that wraps before the ring, an odd number of samples per chunk, both at once, and the attenuation 0 bypass path. Without the fix ASan reports a heap-buffer-overflow in aria_algo_get_data() and the bypass case returns the wrong frames. Signed-off-by: Shrushti P K <shrusthi@labs.digiscrypt.com>
aria_algo_get_data_odd_channel() reads and writes single samples with AE_L32_XP and AE_S32_L_XP, but it still declared its pointers as ae_int32x2 and kept the AE_LA64_PP prime and AE_SA64POS_FP flush of the even channel path. The align registers are never used in the scalar loop, and the (ae_int32 *) casts on the pointer operands are not lvalues in standard C, which the Xtensa LLVM toolchain rejects. Type the pointers as ae_int32 like the HiFi5 odd channel path and drop the unused align register handling. No functional change. Signed-off-by: Shrushti P K <shrusthi@labs.digiscrypt.com>
The get_data paths bound each chunk by the distance from the write pointer to the sink buffer end in samples, but the inner loop always writes a whole frame. ipc4_comp_connect() sizes the buffer between two modules as twice the larger of their ibs and obs in bytes, so if the neighbouring module declares a size that is not a multiple of the frame size, the sink wraps inside a frame and the last frame before the wrap writes up to channels - 1 samples past the buffer end. The HiFi5 path wraps both buffers with hardware circular addressing and has the same requirement. Check in aria_prepare() that the source and sink buffers hold a whole number of frames and fail with -EINVAL otherwise, and cover the check in the aria ztest. Signed-off-by: Shrushti P K <shrusthi@labs.digiscrypt.com>
b59aa97 to
4fb50f9
Compare
|
Rebased onto main for the include path fix and pushed the review changes as three commits: the ring fix with the extra test cases and the get_required_memory() change, the HiFi3 odd channel pointer cleanup, and the whole-frame buffer check at prepare. The aria suite passes under ASan and LSan with the fix, and with the aria sources from main the new cases fail with the heap-buffer-overflow in aria_algo_get_data(). |
aria_algo_get_data() can read past aria's internal ring, which holds the last smpl_group_cnt frames (set from ibs in the IPC4 init payload):
The wrap distance now comes from the read pointer, and the ring is exactly chan_cnt * smpl_group_cnt samples from the start of the allocation. HiFi5 uses circular addressing, so only the ring size applies there. Configurations with matching ibs/obs and an even sample count per chunk give the same output as before.
The new aria ztest runs the module through the module adapter and fails without the fix (heap-buffer-overflow READ in aria_algo_get_data(), 0 bytes past the 384 and 568 byte rings). host-testbench.sh, including the aria test, and test-all-defconfigs.sh pass. I could not build the HiFi3 file locally; it gets the same one-line change as the generic path.