From dca35b691a4789b186dd2316cbb043610ad5b690 Mon Sep 17 00:00:00 2001 From: Tomasz Leman Date: Thu, 19 Nov 2026 11:29:25 +0100 Subject: [PATCH] platform: posix: add virtual DAI devices for native_sim native_sim has no audio hardware and no registered Zephyr DAI device, so zephyr_dev[] is empty and dai_get_device() can never match. Every DAI gateway request therefore failed at copier_dai_create -> dai_common_new -> dai_get -> dai_get_device which hid the whole generic SOF DAI construction, configuration and property layer from the IPC fuzzer. Add a devicetree-described virtual DAI backend for the POSIX platform and include it in the native DAI enumeration behind the new CONFIG_DAI_POSIX, enabled only by the fuzz feature configuration. The overlay declares SSP 0-3, DMIC 0-1, HDA 0-3, ALH 0-3 and UAOL 0-1, so a gateway node id carrying a realistic index resolves to a device. The model is deliberately configuration-only: probe/remove, config set/get, trigger, the timestamping ops and the properties SOF queries (FIFO address and depth, handshake id, stream id) are implemented, the type-specific blob is read through but never parsed, and no data is moved. Reading every byte of the host supplied blob lets the sanitizers validate the (pointer, size) pair the IPC layer hands down, which memcpy_s() cannot do because it only guards the destination. Reporting a rate and channel count of zero keeps dai_verify_params() from rejecting otherwise valid streams, and prepare still stops cleanly in the POSIX DMA model, which does not implement the transfer contract yet. The driver is placed under the kernel tier (src/kernel/platform/posix/) and hooked into zephyr_dev[] in src/kernel/lib/dai-kernel.c, following the user/kernel split introduced after the original change. Using the devicetree device model means the production dai_get_device() lookup is exercised unmodified, with no fuzz specific branching in shared firmware code. Verified on IPC3 and IPC4 builds under both ASan and UBSan. Crafted copier INIT_INSTANCE seeds covering nine gateway classes hit copier_dai_create() 36 times, dai_get_device() 20 times, dai_common_new() 20 times, posix_dai_probe() 18 times, posix_dai_config_set() 18 times and dai_set_config() 18 times. Signed-off-by: Tomasz Leman --- app/boards/native_sim.overlay | 129 ++++++++ app/configs/fuzz_features.conf | 4 + app/dts/bindings/dai/zephyr,posix-dai.yaml | 45 +++ src/kernel/lib/dai-kernel.c | 3 + src/kernel/platform/Kconfig | 12 + src/kernel/platform/posix/dai_virtual.c | 342 +++++++++++++++++++++ zephyr/CMakeLists.txt | 4 + 7 files changed, 539 insertions(+) create mode 100644 app/boards/native_sim.overlay create mode 100644 app/dts/bindings/dai/zephyr,posix-dai.yaml create mode 100644 src/kernel/platform/posix/dai_virtual.c diff --git a/app/boards/native_sim.overlay b/app/boards/native_sim.overlay new file mode 100644 index 000000000000..ed0f3d44439e --- /dev/null +++ b/app/boards/native_sim.overlay @@ -0,0 +1,129 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * + * Copyright(c) 2026 Intel Corporation. All rights reserved. + * + * Virtual DAI instances for the native_sim build. + * + * native_sim has no audio hardware. Without any registered DAI device + * dai_get_device() never matches and every DAI gateway request is rejected + * before the generic SOF DAI code runs, which hides those paths from the IPC + * fuzzer. These nodes describe the virtual links handled by + * src/kernel/platform/posix/dai_virtual.c (enabled with CONFIG_DAI_POSIX). + * + * The indices below mirror a plausible Intel platform so that a host request + * carrying a realistic gateway node id finds a device. + */ + +/ { + posix_dai_ssp0: posix-dai-ssp0 { + compatible = "zephyr,posix-dai"; + dai-type = "ssp"; + dai-index = <0>; + status = "okay"; + }; + + posix_dai_ssp1: posix-dai-ssp1 { + compatible = "zephyr,posix-dai"; + dai-type = "ssp"; + dai-index = <1>; + status = "okay"; + }; + + posix_dai_ssp2: posix-dai-ssp2 { + compatible = "zephyr,posix-dai"; + dai-type = "ssp"; + dai-index = <2>; + status = "okay"; + }; + + posix_dai_ssp3: posix-dai-ssp3 { + compatible = "zephyr,posix-dai"; + dai-type = "ssp"; + dai-index = <3>; + status = "okay"; + }; + + posix_dai_dmic0: posix-dai-dmic0 { + compatible = "zephyr,posix-dai"; + dai-type = "dmic"; + dai-index = <0>; + status = "okay"; + }; + + posix_dai_dmic1: posix-dai-dmic1 { + compatible = "zephyr,posix-dai"; + dai-type = "dmic"; + dai-index = <1>; + status = "okay"; + }; + + posix_dai_hda0: posix-dai-hda0 { + compatible = "zephyr,posix-dai"; + dai-type = "hda"; + dai-index = <0>; + status = "okay"; + }; + + posix_dai_hda1: posix-dai-hda1 { + compatible = "zephyr,posix-dai"; + dai-type = "hda"; + dai-index = <1>; + status = "okay"; + }; + + posix_dai_hda2: posix-dai-hda2 { + compatible = "zephyr,posix-dai"; + dai-type = "hda"; + dai-index = <2>; + status = "okay"; + }; + + posix_dai_hda3: posix-dai-hda3 { + compatible = "zephyr,posix-dai"; + dai-type = "hda"; + dai-index = <3>; + status = "okay"; + }; + + posix_dai_alh0: posix-dai-alh0 { + compatible = "zephyr,posix-dai"; + dai-type = "alh"; + dai-index = <0>; + status = "okay"; + }; + + posix_dai_alh1: posix-dai-alh1 { + compatible = "zephyr,posix-dai"; + dai-type = "alh"; + dai-index = <1>; + status = "okay"; + }; + + posix_dai_alh2: posix-dai-alh2 { + compatible = "zephyr,posix-dai"; + dai-type = "alh"; + dai-index = <2>; + status = "okay"; + }; + + posix_dai_alh3: posix-dai-alh3 { + compatible = "zephyr,posix-dai"; + dai-type = "alh"; + dai-index = <3>; + status = "okay"; + }; + + posix_dai_uaol0: posix-dai-uaol0 { + compatible = "zephyr,posix-dai"; + dai-type = "uaol"; + dai-index = <0>; + status = "okay"; + }; + + posix_dai_uaol1: posix-dai-uaol1 { + compatible = "zephyr,posix-dai"; + dai-type = "uaol"; + dai-index = <1>; + status = "okay"; + }; +}; diff --git a/app/configs/fuzz_features.conf b/app/configs/fuzz_features.conf index 0a2efca0a1b8..872f8e3e4887 100644 --- a/app/configs/fuzz_features.conf +++ b/app/configs/fuzz_features.conf @@ -37,6 +37,10 @@ CONFIG_DMA=y CONFIG_DAI=y +# Virtual (configuration-only) DAI devices. Without a registered DAI device +# native_sim rejects every DAI gateway before the generic SOF DAI code runs. +CONFIG_DAI_POSIX=y + CONFIG_PM_DEVICE=y CONFIG_POWER_DOMAIN=y diff --git a/app/dts/bindings/dai/zephyr,posix-dai.yaml b/app/dts/bindings/dai/zephyr,posix-dai.yaml new file mode 100644 index 000000000000..df9772ea2640 --- /dev/null +++ b/app/dts/bindings/dai/zephyr,posix-dai.yaml @@ -0,0 +1,45 @@ +# SPDX-License-Identifier: BSD-3-Clause +# +# Copyright(c) 2026 Intel Corporation. All rights reserved. + +description: | + Virtual DAI instance used by the SOF native_sim (POSIX) build. + + native_sim has no audio hardware, so this node describes a DAI that only + exists to make the generic SOF DAI construction, configuration and property + paths reachable (most importantly for the IPC fuzzer). It is handled by + src/kernel/platform/posix/dai_virtual.c and moves no audio data. + +compatible: "zephyr,posix-dai" + +include: base.yaml + +properties: + dai-type: + type: string + required: true + enum: + - "ssp" + - "dmic" + - "hda" + - "alh" + - "uaol" + description: | + DAI type this instance reports through dai_config_get(). It selects the + Zephyr type (DAI_INTEL_SSP, DAI_INTEL_DMIC, DAI_INTEL_HDA, + DAI_INTEL_ALH or DAI_INTEL_UAOL) that SOF matches a gateway request + against. + + dai-index: + type: int + required: true + description: | + DAI index this instance answers to. SOF derives it from the IPC gateway + node id, so only configured indices can be instantiated. + + fifo-depth: + type: int + default: 32 + description: | + FIFO depth reported through dai_get_properties(). Some DAI types use it + as the DMA burst size. diff --git a/src/kernel/lib/dai-kernel.c b/src/kernel/lib/dai-kernel.c index c12bfeafa24e..e12cabe8141e 100644 --- a/src/kernel/lib/dai-kernel.c +++ b/src/kernel/lib/dai-kernel.c @@ -73,6 +73,9 @@ const struct device *zephyr_dev[] = { #if DT_HAS_COMPAT_STATUS_OKAY(mediatek_afe) DT_FOREACH_STATUS_OKAY(mediatek_afe, GET_DEVICE_LIST) #endif +#if CONFIG_DAI_POSIX + DT_FOREACH_STATUS_OKAY(zephyr_posix_dai, GET_DEVICE_LIST) +#endif }; const struct device **dai_get_device_list(size_t *count) diff --git a/src/kernel/platform/Kconfig b/src/kernel/platform/Kconfig index bb3b4920a405..df476c6fbe04 100644 --- a/src/kernel/platform/Kconfig +++ b/src/kernel/platform/Kconfig @@ -88,6 +88,18 @@ config ZEPHYR_POSIX_FUZZ_TICKS following a unit-test style case, so the default is short to prevent interaction with regular timer workloads. +config DAI_POSIX + bool "Virtual DAI devices for the native_sim build" + depends on DAI && ZEPHYR_NATIVE_DRIVERS + help + Register the devicetree-described virtual DAI instances handled by + src/kernel/platform/posix/dai_virtual.c. native_sim has no audio hardware, + so without them dai_get_device() never matches and every DAI gateway + request is rejected before the generic SOF DAI construction, + configuration and property code runs. The virtual DAIs make those + paths reachable for the fuzzer. They are configuration-only: no + blob is parsed and no audio data is moved. + endif # ZEPHYR_POSIX && ARCH_POSIX_LIBFUZZER config IMX8 diff --git a/src/kernel/platform/posix/dai_virtual.c b/src/kernel/platform/posix/dai_virtual.c new file mode 100644 index 000000000000..6db93a7113a1 --- /dev/null +++ b/src/kernel/platform/posix/dai_virtual.c @@ -0,0 +1,342 @@ +// SPDX-License-Identifier: BSD-3-Clause +// +// Copyright(c) 2026 Intel Corporation. All rights reserved. + +/** + * \file + * \brief Virtual Zephyr DAI backend for the native_sim (POSIX) build. + * + * The native_sim build has no hardware DAI and no signed manifest, so every + * attempt to instantiate a DAI gateway used to fail early in dai_get(), which + * made the whole DAI construction and configuration chain unreachable for the + * fuzzer: + * + * copier_dai_create() -> dai_common_new() -> dai_get() -> dai_get_device() + * + * This driver registers a small set of devicetree-described virtual DAI + * instances so that dai_get_device() finds a match and the generic SOF code + * (dai_common_new(), comp_dai_config(), dai_set_config(), the property and + * trigger paths) runs for real. + * + * It is deliberately a *configuration-only* model: + * + * - The type-specific configuration blob passed to config_set() is accepted + * but never parsed. Blob parsing lives in the vendor DAI drivers, and + * decoding host-controlled data here would report defects against a device + * that does not exist on any product. The blob bytes *are* read once, so + * sanitizers can detect reads beyond the backing allocation. This does not + * validate the received message length when the blob resides in a larger + * IPC buffer; that boundary must be checked by the IPC layer. + * - config_get() reports a rate and channel count of zero, which SOF reads as + * "the hardware accepts any value" (see dai_verify_params()). A virtual DAI + * really does accept any stream, so no valid configuration is rejected. + * - No data is moved. The POSIX DMA model does not implement the transfer + * contract yet, so prepare still stops with a clean error there instead of + * pretending that audio flowed. + */ + +#include +#include +#include + +#include +#include +#include +#include +#include + +#define DT_DRV_COMPAT zephyr_posix_dai + +/** \brief Synthetic FIFO address of the first virtual DAI instance. + * + * The address is only handed to the DMA model as an endpoint identifier, it is + * never dereferenced. It is non-zero so that code treating a zero FIFO address + * as "unset" behaves as it does on hardware. + */ +#define POSIX_DAI_FIFO_BASE 0x00010000 + +/** \brief Address distance between two consecutive virtual FIFOs. */ +#define POSIX_DAI_FIFO_STRIDE 0x100 + +/* Map the devicetree 'dai-type' enum to the Zephyr DAI type. */ +#define POSIX_DAI_TYPE_SSP DAI_INTEL_SSP +#define POSIX_DAI_TYPE_DMIC DAI_INTEL_DMIC +#define POSIX_DAI_TYPE_HDA DAI_INTEL_HDA +#define POSIX_DAI_TYPE_ALH DAI_INTEL_ALH +#define POSIX_DAI_TYPE_UAOL DAI_INTEL_UAOL + +#define POSIX_DAI_TYPE(inst) \ + _CONCAT(POSIX_DAI_TYPE_, DT_INST_STRING_UPPER_TOKEN(inst, dai_type)) + +/** \brief Immutable identity of one virtual DAI instance. */ +struct posix_dai_cfg { + enum dai_type type; /**< Zephyr DAI type reported to SOF. */ + uint32_t dai_index; /**< Index matched against the gateway node id. */ + uint32_t fifo_address; /**< Synthetic FIFO address. */ + uint32_t fifo_depth; /**< FIFO depth reported to SOF. */ +}; + +/** \brief Mutable state of one virtual DAI instance. */ +struct posix_dai_data { + struct k_spinlock lock; /**< Protects the members below. */ + struct dai_config cfg; /**< Last configuration accepted. */ + struct dai_properties props; /**< Scratch for get_properties(). */ + int refcount; /**< probe()/remove() balance. */ +}; + +/** + * \brief Sink for the configuration blob read-through. + * + * Volatile so the compiler cannot elide the reads that make the sanitizers + * validate the host-supplied (pointer, size) pair. + */ +static volatile uint8_t posix_dai_blob_sum; + +/** + * \brief Check whether a configuration targets this instance's type. + * + * IPC4 passes blob configurations with the NHLT flavour of the type + * (e.g. DAI_INTEL_SSP_NHLT) while IPC3 passes the base type, see + * dai_set_config(). Both describe the same link, so accept both. + * + * \param[in] type Type of this instance. + * \param[in] req Type requested by the configuration. + * \return true when the requested type addresses this instance. + */ +static bool posix_dai_type_matches(enum dai_type type, enum dai_type req) +{ + if (type == req) + return true; + + switch (type) { + case DAI_INTEL_SSP: + return req == DAI_INTEL_SSP_NHLT; + case DAI_INTEL_DMIC: + return req == DAI_INTEL_DMIC_NHLT; + case DAI_INTEL_HDA: + return req == DAI_INTEL_HDA_NHLT; + case DAI_INTEL_ALH: + return req == DAI_INTEL_ALH_NHLT; + default: + return false; + } +} + +static int posix_dai_probe(const struct device *dev) +{ + struct posix_dai_data *data = dev->data; + k_spinlock_key_t key = k_spin_lock(&data->lock); + + data->refcount++; + k_spin_unlock(&data->lock, key); + + return 0; +} + +static int posix_dai_remove(const struct device *dev) +{ + struct posix_dai_data *data = dev->data; + k_spinlock_key_t key = k_spin_lock(&data->lock); + + if (data->refcount > 0) + data->refcount--; + + k_spin_unlock(&data->lock, key); + + return 0; +} + +static int posix_dai_config_set(const struct device *dev, const struct dai_config *cfg, + const void *bespoke_cfg, size_t size) +{ + const struct posix_dai_cfg *dev_cfg = dev->config; + struct posix_dai_data *data = dev->data; + k_spinlock_key_t key; + + if (!cfg) + return -EINVAL; + + if (!posix_dai_type_matches(dev_cfg->type, cfg->type)) + return -EINVAL; + + if (cfg->dai_index != dev_cfg->dai_index) + return -EINVAL; + + /* + * The type-specific blob is intentionally not parsed, see the file + * comment. Reading it once still validates the host-controlled + * (pointer, size) pair under the sanitizers, which is what a real + * driver would do on its first access to the blob. + */ + for (size_t i = 0; bespoke_cfg && i < size; i++) + posix_dai_blob_sum += ((const uint8_t *)bespoke_cfg)[i]; + + /* Only the generic part of the configuration is retained. */ + key = k_spin_lock(&data->lock); + data->cfg = *cfg; + data->cfg.type = dev_cfg->type; + k_spin_unlock(&data->lock, key); + + return 0; +} + +static int posix_dai_config_get(const struct device *dev, struct dai_config *cfg, + enum dai_dir dir) +{ + const struct posix_dai_cfg *dev_cfg = dev->config; + struct posix_dai_data *data = dev->data; + k_spinlock_key_t key; + + if (!cfg) + return -EINVAL; + + key = k_spin_lock(&data->lock); + *cfg = data->cfg; + k_spin_unlock(&data->lock, key); + + cfg->type = dev_cfg->type; + cfg->dai_index = dev_cfg->dai_index; + + /* + * Zero means "the hardware imposes no value" to the SOF parameter + * check, which is what a virtual DAI accepting any stream must report. + */ + cfg->rate = 0; + cfg->channels = 0; + + return 0; +} + +static int posix_dai_get_properties_copy(const struct device *dev, enum dai_dir dir, + int stream_id, struct dai_properties *prop) +{ + const struct posix_dai_cfg *dev_cfg = dev->config; + + if (!prop || stream_id < 0) + return -EINVAL; + + prop->fifo_address = dev_cfg->fifo_address; + prop->fifo_depth = dev_cfg->fifo_depth; + prop->dma_hs_id = dev_cfg->dai_index; + prop->reg_init_delay = 0; + prop->stream_id = dev_cfg->dai_index; + + return 0; +} + +static const struct dai_properties *posix_dai_get_properties(const struct device *dev, + enum dai_dir dir, int stream_id) +{ + struct posix_dai_data *data = dev->data; + struct dai_properties props; + + if (posix_dai_get_properties_copy(dev, dir, stream_id, &props) < 0) + return NULL; + + /* + * This API returns a pointer into driver memory, so publish a fully + * built copy instead of letting a concurrent query observe a partially + * updated object. + */ + k_spinlock_key_t key = k_spin_lock(&data->lock); + + data->props = props; + k_spin_unlock(&data->lock, key); + + return &data->props; +} + +static int posix_dai_trigger(const struct device *dev, enum dai_dir dir, + enum dai_trigger_cmd cmd) +{ + switch (cmd) { + case DAI_TRIGGER_START: + case DAI_TRIGGER_PRE_START: + case DAI_TRIGGER_STOP: + case DAI_TRIGGER_PAUSE: + case DAI_TRIGGER_POST_STOP: + case DAI_TRIGGER_DRAIN: + case DAI_TRIGGER_DROP: + case DAI_TRIGGER_PREPARE: + case DAI_TRIGGER_RESET: + case DAI_TRIGGER_COPY: + return 0; + default: + return -EINVAL; + } +} + +/** + * \brief Accept any timestamping configuration. + * + * SOF queries the DAI timestamp interface from the IPC time-info path. The + * virtual DAI has no counter, so the calls are accepted and report zero. + * + * \param[in] dev Virtual DAI instance. + * \param[in] cfg Timestamp configuration, not interpreted. + * \return 0 always. + */ +static int posix_dai_ts_config(const struct device *dev, struct dai_ts_cfg *cfg) +{ + return cfg ? 0 : -EINVAL; +} + +/** \brief Start timestamping. \see posix_dai_ts_config() */ +static int posix_dai_ts_start(const struct device *dev, struct dai_ts_cfg *cfg) +{ + return cfg ? 0 : -EINVAL; +} + +/** \brief Stop timestamping. \see posix_dai_ts_config() */ +static int posix_dai_ts_stop(const struct device *dev, struct dai_ts_cfg *cfg) +{ + return cfg ? 0 : -EINVAL; +} + +/** + * \brief Return a zeroed timestamp sample. + * + * \param[in] dev Virtual DAI instance. + * \param[in] cfg Timestamp configuration, not interpreted. + * \param[out] tsd Receives a zeroed sample. + * \return 0 on success, -EINVAL on a missing argument. + */ +static int posix_dai_ts_get(const struct device *dev, struct dai_ts_cfg *cfg, + struct dai_ts_data *tsd) +{ + if (!cfg || !tsd) + return -EINVAL; + + memset(tsd, 0, sizeof(*tsd)); + + return 0; +} + +static DEVICE_API(dai, posix_dai_api) = { + .probe = posix_dai_probe, + .remove = posix_dai_remove, + .config_set = posix_dai_config_set, + .config_get = posix_dai_config_get, + .trigger = posix_dai_trigger, + .get_properties = posix_dai_get_properties, + .get_properties_copy = posix_dai_get_properties_copy, + .ts_config = posix_dai_ts_config, + .ts_start = posix_dai_ts_start, + .ts_stop = posix_dai_ts_stop, + .ts_get = posix_dai_ts_get, +}; + +#define POSIX_DAI_DEVICE_INIT(inst) \ + static const struct posix_dai_cfg posix_dai_cfg_##inst = { \ + .type = POSIX_DAI_TYPE(inst), \ + .dai_index = DT_INST_PROP(inst, dai_index), \ + .fifo_address = POSIX_DAI_FIFO_BASE + (inst) * POSIX_DAI_FIFO_STRIDE, \ + .fifo_depth = DT_INST_PROP(inst, fifo_depth), \ + }; \ + static struct posix_dai_data posix_dai_data_##inst; \ + \ + DEVICE_DT_INST_DEFINE(inst, NULL, NULL, \ + &posix_dai_data_##inst, &posix_dai_cfg_##inst, \ + POST_KERNEL, CONFIG_DAI_INIT_PRIORITY, &posix_dai_api); + +DT_INST_FOREACH_STATUS_OKAY(POSIX_DAI_DEVICE_INIT) diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 9e4f19b15d86..09a246b00b59 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -559,6 +559,10 @@ zephyr_library_sources_ifdef(CONFIG_ARCH_POSIX_LIBFUZZER ${SOF_PLATFORM_PATH}/posix/fuzz.c ) +zephyr_library_sources_ifdef(CONFIG_DAI_POSIX + ${SOF_PLATFORM_PATH}/posix/dai_virtual.c +) + if (CONFIG_BOARD_QEMU_XTENSA) set(PLATFORM "qemu_xtensa") zephyr_library_sources(${SOF_PLATFORM_PATH}/qemu_xtensa/platform.c)