Skip to content

audio: aria: fix over-read of the internal ring buffer - #11499

Open
Shrusti-pk wants to merge 3 commits into
thesofproject:mainfrom
Shrusti-pk:aria-ring-wrap
Open

Shrusti-pk wants to merge 3 commits into
thesofproject:mainfrom
Shrusti-pk:aria-ring-wrap

Conversation

@Shrusti-pk

Copy link
Copy Markdown
Contributor

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):

  • each chunk is bounded by the distance from cd->data_ptr to the ring end instead of from the read pointer, so when the sink buffer wraps first, the next chunk still uses the old distance and runs past data_end. It needs a short period to move data_ptr off the ring start and a sink buffer that is not a multiple of the ring, for example obs set different from ibs. Both HiFi3 variants have the same line.
  • aria_algo_init() rounded the ring up to an even sample count and started it one sample in, so with 3, 5 or 7 channels and an odd number of frames per ms a frame straddles the ring end: up to channels - 1 extra samples are read and the output channels rotate by one sample each period
  • the over-read samples are scaled and written to the sink. With the functions copied verbatim under ASan, the worst case I found was 736 bytes past a 1536 byte ring (8 channels, obs of 13 frames, short first period)

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.

@sofci

sofci commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Can one of the admins verify this patch?

reply test this please to run this test once

@lgirdwood

Copy link
Copy Markdown
Member

test this please

@lgirdwood lgirdwood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also missing a SoB - pls add this to your Agents workflow.

Comment thread src/audio/aria/aria.c Resolved
@Shrusti-pk

Copy link
Copy Markdown
Contributor Author

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.

@lgirdwood

Copy link
Copy Markdown
Member

@Shrusti-pk I've run out of co-pilot credits, so gemini reviewing for any gaps.

@lgirdwood lgirdwood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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:

  1. Blocking (HiFi3/HiFi4 odd-channel bug — src/audio/aria/aria_hifi3.c:95): aria_algo_get_data_odd_channel() still calls inu = AE_LA64_PP(in) before a scalar AE_L32_XP loop that never uses inu. On HiFi3/HiFi4, AE_LA64_PP(in) increments in by +8 bytes whenever ((uintptr_t)in & 7) != 0 — which is now hit on odd-sample rings (chan_cnt * smpl_group_cnt odd) or odd-frame periods, causing AE_L32_XP to skip 2 samples and read 8 bytes past cd->data_end (and the (ae_int32 *)in cast-as-lvalue also breaks Xtensa LLVM Clang builds).
  2. Sink buffer over-write when snk_size is not a multiple of frame_bytes (src/audio/aria/aria_generic.c:70-75, aria_hifi3.c:91-96, 151-156): ipc4_comp_connect() sizes buffers as MAX(ibs, obs) * 2 in raw bytes; if m = cir_buf_samples_without_wrap_s32(out, sink->buf_end) is not divisible by ch_n (e.g. ch_n = 3 with a power-of-2 downstream ibs), for (i = 0; i < n; i += ch_n) writes up to ch_n - 1 samples past sink->buf_end and desynchronizes samples -= n.
  3. get_required_memory() (src/audio/aria/aria.c:53, 76-78): Dropping the leftover ALIGN_UP(..., 2) avoids 4 unused bytes after cd->data_end and places CI ASan's heap redzone directly at cd->data_end (564 bytes instead of 568 bytes) for odd-sample rings.
  4. Ztest coverage (test/ztest/unit/audio/aria/aria_ztest.c:254-267): Suggest adding a combined 3 ch + short first period + sink wrap case and an attenuation = 0 case to cover the bypass cir_buf_copy() path.

Comment on lines +93 to 96
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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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:

  1. skips 2 samples (rotating channels),
  2. reads 2 samples (8 bytes) past cd->data_end, and
  3. wraps in to cd->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 *').

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment on lines 70 to 75
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++) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread src/audio/aria/aria.c Outdated
Comment on lines +76 to +79
* 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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment on lines +254 to +267
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);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice test harness through module_adapter! Consider adding two more run_aria() calls (or separate ZTEST cases) to cover:

  1. Combined odd channels + short first period + sink wrap before ring wrap: run_aria(3, 47, 94, 70, 20, 1);
  2. attenuation = 0 (cd->att == 0 bypass cir_buf_copy() path in aria.c:99-103): run_aria(3, 47, 94, 70, 20, 0);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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>
@Shrusti-pk

Copy link
Copy Markdown
Contributor Author

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().

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