From b59aa9701442ccea03a28eb034121eeffce6d293 Mon Sep 17 00:00:00 2001 From: Shrushti P K Date: Fri, 20 Nov 2026 18:08:52 +0530 Subject: [PATCH] audio: aria: fix over-read of the internal ring buffer 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. 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 with 8-byte aligned pointers, since the allocation is cache line aligned and the pointers advance by whole frames. The allocation size is unchanged. Add an aria ztest that runs the module through the module adapter for both cases and checks the delayed output frame by frame. Without the fix ASan reports a heap-buffer-overflow in aria_algo_get_data(). Signed-off-by: Shrushti P K --- src/audio/aria/aria.c | 16 +- src/audio/aria/aria.h | 3 - src/audio/aria/aria_generic.c | 2 +- src/audio/aria/aria_hifi3.c | 4 +- test/ztest/unit/audio/aria/CMakeLists.txt | 67 ++++++ test/ztest/unit/audio/aria/aria_ztest.c | 269 ++++++++++++++++++++++ test/ztest/unit/audio/aria/prj.conf | 10 + test/ztest/unit/audio/aria/testcase.yaml | 16 ++ 8 files changed, 377 insertions(+), 10 deletions(-) create mode 100644 test/ztest/unit/audio/aria/CMakeLists.txt create mode 100644 test/ztest/unit/audio/aria/aria_ztest.c create mode 100644 test/ztest/unit/audio/aria/prj.conf create mode 100644 test/ztest/unit/audio/aria/testcase.yaml diff --git a/src/audio/aria/aria.c b/src/audio/aria/aria.c index c2e18597ce13..79f21a14f7e2 100644 --- a/src/audio/aria/aria.c +++ b/src/audio/aria/aria.c @@ -66,12 +66,20 @@ static int aria_algo_init(struct aria_data *cd, void *buffer_desc, { cd->chan_cnt = chan_cnt; cd->smpl_group_cnt = smpl_group_cnt; - /* ensures buffer size is aligned to 8 bytes */ - cd->buff_size = ALIGN_UP(cd->chan_cnt * cd->smpl_group_cnt, 2); - cd->offset = (cd->chan_cnt * cd->smpl_group_cnt) & 1; + /* The ring holds exactly one chunk, a whole number of frames, so a + * frame never straddles its end and the delay stays smpl_group_cnt + * frames. The sample count is odd only for an odd channel count, + * and the odd channel HiFi paths access the ring with 32-bit scalar + * loads and stores. The even channel paths load two samples at a + * time; there the ring is a multiple of 8 bytes, the allocation is + * cache line aligned and the pointers advance by whole frames, so + * 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; cd->att = att; cd->data_addr = buffer_desc; - cd->data_ptr = cd->data_addr + cd->offset; + cd->data_ptr = cd->data_addr; cd->data_end = cd->data_addr + cd->buff_size; cd->buff_pos = 0; diff --git a/src/audio/aria/aria.h b/src/audio/aria/aria.h index cc29ea28af3b..a86d8f8c0655 100644 --- a/src/audio/aria/aria.h +++ b/src/audio/aria/aria.h @@ -73,9 +73,6 @@ struct aria_data { /* cyclic buffer start address of data */ int32_t *data_addr; - /* internal buffer offset helps to keep algorithmic delay constant */ - size_t offset; - aria_get_data_func aria_get_data; }; diff --git a/src/audio/aria/aria_generic.c b/src/audio/aria/aria_generic.c index c277726dbfe5..a56814884ac1 100644 --- a/src/audio/aria/aria_generic.c +++ b/src/audio/aria/aria_generic.c @@ -69,7 +69,7 @@ static void aria_algo_get_data(struct processing_module *mod, while (samples) { 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++) { diff --git a/src/audio/aria/aria_hifi3.c b/src/audio/aria/aria_hifi3.c index 43e337d33fe6..f4517e4a8309 100644 --- a/src/audio/aria/aria_hifi3.c +++ b/src/audio/aria/aria_hifi3.c @@ -90,7 +90,7 @@ static void aria_algo_get_data_odd_channel(struct processing_module *mod, while (samples) { 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); inu = AE_LA64_PP(in); for (i = 0; i < n; i += ch_n) { @@ -150,7 +150,7 @@ static void aria_algo_get_data_even_channel(struct processing_module *mod, while (samples) { 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); inu = AE_LA64_PP(in); for (i = 0; i < n; i += ch_n) { diff --git a/test/ztest/unit/audio/aria/CMakeLists.txt b/test/ztest/unit/audio/aria/CMakeLists.txt new file mode 100644 index 000000000000..a781f61cbc1a --- /dev/null +++ b/test/ztest/unit/audio/aria/CMakeLists.txt @@ -0,0 +1,67 @@ +# SPDX-License-Identifier: BSD-3-Clause + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(sof_unit_audio_aria) + +set(SOF_ROOT "${PROJECT_SOURCE_DIR}/../../../../../") +set(sof_top_dir ${SOF_ROOT}) + +include(${SOF_ROOT}/scripts/cmake/misc.cmake) +include(${SOF_ROOT}/scripts/cmake/uuid-registry.cmake) + +target_include_directories(app PRIVATE + ${SOF_ROOT}/zephyr/include + ${SOF_ROOT}/src/include + ${SOF_ROOT}/src/kernel/platform/posix/include + ${SOF_ROOT}/src/platform/posix/include + ${SOF_ROOT}/src/audio/aria + ${SOF_ROOT}/tools/rimage/src/include + ${PROJECT_BINARY_DIR}/include/generated +) + +target_compile_definitions(app PRIVATE + -DCONFIG_ZEPHYR_POSIX=1 + -DCONFIG_LIBRARY=1 + -DUNIT_TEST=1 +) + +target_sources(app PRIVATE + aria_ztest.c + ${SOF_ROOT}/test/ztest/unit/common/alloc.c + ${SOF_ROOT}/test/ztest/unit/common/sof_context.c + ${SOF_ROOT}/src/audio/aria/aria.c + ${SOF_ROOT}/src/audio/aria/aria_generic.c + ${SOF_ROOT}/src/audio/module_adapter/module_adapter.c + ${SOF_ROOT}/src/audio/module_adapter/module_adapter_ipc4.c + ${SOF_ROOT}/src/audio/module_adapter/module/generic.c + ${SOF_ROOT}/src/audio/buffers/comp_buffer.c + ${SOF_ROOT}/src/audio/buffers/audio_buffer.c + ${SOF_ROOT}/src/audio/source_api_helper.c + ${SOF_ROOT}/src/audio/sink_api_helper.c + ${SOF_ROOT}/src/audio/sink_source_utils.c + ${SOF_ROOT}/src/audio/audio_stream.c + ${SOF_ROOT}/src/audio/component.c + ${SOF_ROOT}/src/audio/data_blob.c + ${SOF_ROOT}/src/module/audio/source_api.c + ${SOF_ROOT}/src/module/audio/sink_api.c + ${SOF_ROOT}/src/ipc/ipc4/helper.c + ${SOF_ROOT}/src/ipc/ipc-common.c + ${SOF_ROOT}/src/ipc/ipc-helper.c + ${SOF_ROOT}/src/lib/objpool.c + ${SOF_ROOT}/src/math/numbers.c +) + +target_link_libraries(app PRIVATE + "-Wl,--wrap=rzalloc,--wrap=rfree,--wrap=sof_heap_alloc,--wrap=sof_heap_free,--wrap=sof_sys_user_heap_get" +) + +# The standalone target compiles the full IPC4 helper translation unit while +# linking only its format helpers; other helper paths leave one local ID unused. +set_source_files_properties( + ${SOF_ROOT}/src/ipc/ipc4/helper.c + PROPERTIES COMPILE_OPTIONS "-Wno-unused-variable" +) + +sof_append_relative_path_definitions(app) diff --git a/test/ztest/unit/audio/aria/aria_ztest.c b/test/ztest/unit/audio/aria/aria_ztest.c new file mode 100644 index 000000000000..6dc2842b7f1e --- /dev/null +++ b/test/ztest/unit/audio/aria/aria_ztest.c @@ -0,0 +1,269 @@ +// SPDX-License-Identifier: BSD-3-Clause + +/** + * @file aria_ztest.c + * @brief Aria internal ring buffer tests. + * + * Aria keeps the last smpl_group_cnt frames in an internal ring and outputs + * them delayed by that amount, scaled by its gain. These tests run the real + * module through the module adapter with buffer sizes and short periods that + * make the ring wrap at a different point than the sink buffer, and check the + * output frame by frame. + */ + +#include + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "aria.h" + +#define TEST_RATE 48000 +#define TEST_PERIODS 40 +/* aria ramps its gain across the gain states, compare once it has settled */ +#define TEST_SETTLED (2 * ARIA_MAX_GAIN_STATES) + +struct aria_fixture { + struct comp_dev *dev; + struct comp_buffer *source; + struct comp_buffer *sink; + struct sof_source *sources[1]; + struct sof_sink *sinks[1]; +}; + +/** + * @brief Test sample for a frame and channel, small enough to keep full gain. + * @param frame Input frame index. + * @param ch Channel index. + * @return S24 sample value. + */ +static int32_t test_sample(size_t frame, unsigned int ch) +{ + return (int32_t)(((frame & 0xfff) << 4) | ch); +} + +/** + * @brief Allocate a buffer and connect it to the aria component. + * @param dev Aria component. + * @param source True to connect as the aria source, false for the sink. + * @param size Buffer size in bytes. + * @param channels Channels count. + * @return Connected buffer. + */ +static struct comp_buffer *create_test_buffer(struct comp_dev *dev, bool source, size_t size, + unsigned int channels) +{ + struct sof_ipc_buffer desc = { + .size = size, + }; + struct comp_buffer *buffer = buffer_new(NULL, &desc, BUFFER_USAGE_NOT_SHARED); + + zassert_not_null(buffer, "Failed to allocate test buffer"); + memset(audio_stream_get_addr(&buffer->stream), 0, size); + audio_stream_set_frm_fmt(&buffer->stream, SOF_IPC_FRAME_S32_LE); + audio_stream_set_valid_fmt(&buffer->stream, SOF_IPC_FRAME_S24_4LE); + audio_stream_set_rate(&buffer->stream, TEST_RATE); + audio_stream_set_channels(&buffer->stream, channels); + + if (source) { + buffer->source = calloc(1, sizeof(*buffer->source)); + zassert_not_null(buffer->source); + buffer->source->state = COMP_STATE_PREPARE; + list_item_append(&buffer->sink_list, &dev->bsource_list); + } else { + buffer->sink = calloc(1, sizeof(*buffer->sink)); + zassert_not_null(buffer->sink); + buffer->sink->state = COMP_STATE_PREPARE; + list_item_append(&buffer->source_list, &dev->bsink_list); + } + + return buffer; +} + +/** + * @brief Create an aria instance from an IPC4 init payload and prepare it. + * @param channels Channels count in the base config. + * @param ibs Input buffer size in bytes, sets the 1 ms chunk. + * @param source_frames Source buffer size in frames. + * @param sink_frames Sink buffer size in frames. + * @param attenuation Aria attenuation parameter. + * @return Prepared fixture. + */ +static struct aria_fixture *create_fixture(unsigned int channels, uint32_t ibs, + size_t source_frames, size_t sink_frames, + uint32_t attenuation) +{ + const struct sof_uuid uuid = SOF_REG_UUID(aria); + const struct comp_driver *driver = comp_driver_find(&uuid, SOF_COMP_MODULE_ADAPTER); + const size_t frame_bytes = channels * sizeof(int32_t); + struct ipc4_aria_module_cfg cfg = { + .base_cfg = + { + .ibs = ibs, + .obs = ibs, + .audio_fmt = + { + .sampling_frequency = TEST_RATE, + .depth = IPC4_DEPTH_32BIT, + .ch_map = 0x76543210, + .ch_cfg = IPC4_CHANNEL_CONFIG_INVALID, + .interleaving_style = IPC4_CHANNELS_INTERLEAVED, + .channels_count = channels, + .valid_bit_depth = IPC4_DEPTH_24BIT, + .s_type = IPC4_TYPE_SIGNED_INTEGER, + }, + }, + .attenuation = attenuation, + }; + struct ipc_config_process spec = { + .size = sizeof(cfg), + .data = (const uint8_t *)&cfg, + }; + struct comp_ipc_config ipc_cfg = { + .id = 1, + .proc_domain = COMP_PROCESSING_DOMAIN_LL, + .type = SOF_COMP_MODULE_ADAPTER, + .ipc_config_size = spec.size, + }; + struct aria_fixture *fixture; + struct processing_module *mod; + const struct module_interface *ops; + + zassert_not_null(driver, "Aria driver not registered"); + fixture = calloc(1, sizeof(*fixture)); + zassert_not_null(fixture); + fixture->dev = driver->ops.create(driver, &ipc_cfg, &spec); + zassert_not_null(fixture->dev, "Failed to create aria"); + mod = comp_mod(fixture->dev); + + fixture->source = + create_test_buffer(fixture->dev, true, source_frames * frame_bytes, channels); + fixture->sink = + create_test_buffer(fixture->dev, false, sink_frames * frame_bytes, channels); + fixture->sources[0] = audio_buffer_get_source(&fixture->source->audio_buffer); + fixture->sinks[0] = audio_buffer_get_sink(&fixture->sink->audio_buffer); + + ops = fixture->dev->drv->adapter_ops; + zassert_ok(ops->prepare(mod, fixture->sources, 1, fixture->sinks, 1)); + + return fixture; +} + +/** + * @brief Free all resources owned by an aria fixture. + * @param fixture Fixture to free. + */ +static void destroy_fixture(struct aria_fixture *fixture) +{ + free(fixture->source->source); + buffer_free(fixture->source); + free(fixture->sink->sink); + buffer_free(fixture->sink); + fixture->dev->drv->ops.free(fixture->dev); + free(fixture); +} + +/** + * @brief Run aria period by period and check the delayed, scaled output. + * @param channels Channels count. + * @param chunk_frames Frames per 1 ms chunk, ibs in frames. + * @param source_frames Source buffer size in frames. + * @param sink_frames Sink buffer size in frames. + * @param first_frames Frames available in the first period. + * @param attenuation Aria attenuation parameter. + */ +static void run_aria(unsigned int channels, size_t chunk_frames, size_t source_frames, + size_t sink_frames, size_t first_frames, uint32_t attenuation) +{ + const size_t frame_bytes = channels * sizeof(int32_t); + struct aria_fixture *fixture = create_fixture(channels, chunk_frames * frame_bytes, + source_frames, sink_frames, attenuation); + struct processing_module *mod = comp_mod(fixture->dev); + const struct module_interface *ops = fixture->dev->drv->adapter_ops; + struct audio_stream *source = &fixture->source->stream; + struct audio_stream *sink = &fixture->sink->stream; + size_t in_frame = 0; + size_t out_frame = 0; + + for (int period = 0; period < TEST_PERIODS; period++) { + size_t frames = period ? chunk_frames : first_frames; + int32_t *w = audio_stream_get_wptr(source); + int32_t *r; + size_t avail; + + for (size_t f = 0; f < frames; f++) { + for (unsigned int ch = 0; ch < channels; ch++) { + *w = test_sample(in_frame + f, ch); + w = audio_stream_wrap(source, w + 1); + } + } + audio_stream_produce(source, frames * frame_bytes); + in_frame += frames; + + zassert_ok(ops->process(mod, fixture->sources, 1, fixture->sinks, 1)); + + avail = audio_stream_get_avail_frames(sink); + zassert_equal(avail, frames, "period %d: %zu frames out, %zu in", period, avail, + frames); + + r = audio_stream_get_rptr(sink); + for (size_t f = 0; f < avail; f++, out_frame++) { + for (unsigned int ch = 0; ch < channels; ch++) { + int32_t expected = test_sample(out_frame - chunk_frames, ch) + << attenuation; + + if (period >= TEST_SETTLED) { + zassert_equal(*r, expected, + "frame %zu ch %u: got %#x expected %#x", + out_frame, ch, *r, expected); + } + r = audio_stream_wrap(sink, r + 1); + } + } + audio_stream_consume(sink, avail * frame_bytes); + } + + destroy_fixture(fixture); +} + +/** + * @brief Initialize the aria component registry for the suite. + * @return Suite fixture, unused. + */ +static void *aria_suite_setup(void) +{ + sys_comp_init(sof_get()); + sys_comp_module_aria_interface_init(); + return NULL; +} + +/* + * A short first period leaves the read position inside the ring, and with a + * 70 frame sink the sink wraps before the ring in later periods. The ring + * wrap distance must follow the read pointer across the sink wrap. + */ +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); +} + +ZTEST_SUITE(aria_suite, NULL, aria_suite_setup, NULL, NULL, NULL); diff --git a/test/ztest/unit/audio/aria/prj.conf b/test/ztest/unit/audio/aria/prj.conf new file mode 100644 index 000000000000..d999cf26c9ef --- /dev/null +++ b/test/ztest/unit/audio/aria/prj.conf @@ -0,0 +1,10 @@ +CONFIG_ZTEST=y +CONFIG_LOG=y +CONFIG_SOF_FULL_ZEPHYR_APPLICATION=n +CONFIG_LIBRARY=y +CONFIG_IPC_MAJOR_4=y +CONFIG_COMP_ARIA=y +CONFIG_ARIA_HIFI_NONE=y +CONFIG_STATIC_PIPELINE=n +CONFIG_FORMAT_S24LE=y +CONFIG_FORMAT_S32LE=y diff --git a/test/ztest/unit/audio/aria/testcase.yaml b/test/ztest/unit/audio/aria/testcase.yaml new file mode 100644 index 000000000000..76d5eae7b62a --- /dev/null +++ b/test/ztest/unit/audio/aria/testcase.yaml @@ -0,0 +1,16 @@ +# SPDX-License-Identifier: BSD-3-Clause + +common: + tags: + - SOF + - unit_test + - audio + - aria + integration_platforms: + - native_sim + arch_exclude: xtensa + +tests: + sof.unit.audio.aria: + platform_allow: + - native_sim