Repository navigation
audio: aria: fix over-read of the internal ring buffer #11499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Shrusti-pk
wants to merge
1
commit into
thesofproject:main
Choose a base branch
from
Shrusti-pk:aria-ring-wrap
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 <zephyr/ztest.h> | ||
|
|
||
| #include <rtos/sof.h> | ||
| #include <sof/audio/buffer.h> | ||
| #include <sof/audio/component.h> | ||
| #include <sof/audio/component_ext.h> | ||
| #include <sof/audio/module_adapter/module/generic.h> | ||
| #include <ipc/stream.h> | ||
| #include <ipc4/base-config.h> | ||
|
|
||
| #include <stdint.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
|
|
||
| #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); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pls check as the buffers need to work on SIMD vector size alignment not just frame alignments/sizes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked. The ring is only an odd number of samples when the channel count is odd, and the odd channel paths in both aria_hifi3.c and aria_hifi5.c access it with 32-bit scalar loads and stores (AE_L32_XP and AE_L32_XC), so 4-byte alignment is all they need. The even channel paths are the ones doing 64-bit vector loads, and there the ring is always a multiple of 8 bytes: the allocation comes from mod_balloc, which is cache line aligned, and the pointers only ever advance by whole frames, so they stay 8-byte aligned. The old ALIGN_UP only padded the ring end anyway; with an odd sample count data_ptr started one sample in and changed alignment every period, so it didn't give vector alignment either. The allocation size in get_required_memory() is unchanged. I've expanded the comment here to spell that out.