Skip to content

treewide: use __ASSERT_NO_MSG() instead of assert() in shared code - #11597

Draft
lgirdwood wants to merge 10 commits into
thesofproject:mainfrom
lgirdwood:topic/zn-rtos-assert-shared
Draft

lgirdwood wants to merge 10 commits into
thesofproject:mainfrom
lgirdwood:topic/zn-rtos-assert-shared

Conversation

@lgirdwood

Copy link
Copy Markdown
Member

Use __ASSERT_NO_MSG() instead of assert() in the shared code. Part of #11517 (step 6). Depends on #11583, which adds the host <zephyr/sys/__assert.h> stub; the first four commits here are that PR.

On Zephyr, rtos/panic.h defines assert() as __ASSERT_NO_MSG(). The shared code kept assert() because the host had no Zephyr assert macros.

Patches

  1. posix: the host rtos/panic.h includes the <zephyr/sys/__assert.h> stub, as rtos/panic.h gets the Zephyr macros through <zephyr/kernel.h> on Zephyr. Shared code can then switch without new includes.
  2. audio, include, ipc, and lib, drivers, samples: assert() → __ASSERT_NO_MSG(), one commit per area (162 call sites, scripted). Continuation lines are re-aligned and no lines are added or removed, so __LINE__ in verbose asserts does not change.
  3. coherent: the debug check macros, which are on #define lines that the script leaves alone. The Zephyr and host versions of the context checks stay separate, so that no lines move.

Not in this PR

  • Kernel-only code: zephyr: use the Zephyr assert and panic macros in kernel-only code #11572.
  • Files that use the C library assert() on purpose: the LLEXT llext-wrap.c shims and the STFT process code include <assert.h>. Converting them would change what they call.
  • The IADK C++ module API headers, the LMDK headers (src/include/module) and host tools such as mt-dai-gen.c: they are built without the SOF headers.
  • sof_panic(): a separate change.

Functional change

None on Zephyr. On the host, these asserts always end in the SOF panic. assert() did too, unless <assert.h> had been included before rtos/panic.h.

Testing

  • Builds: tested as a stack of the four Wave C PRs on top of posix: host stubs for the Zephyr atomic, cache and assert APIs #11583, first on main 829c0ac: every commit built incrementally and the tip from scratch, for tgl, tgl-h, mtl, lnl, ptl, wcl, nvl, nvl-s, imx8, imx8x, imx8m, imx8m_cm7, imx8ulp, imx93, imx95, qemu_xtensa, qemu_xtensa_mmu, native_sim, and the PTL userspace, LL-userspace and LLEXT, MTL LLEXT, TGL/PTL debug-stream and TGL/MTL/LNL/PTL CI overlay configurations.
  • Objects: every object hashed with the debug sections stripped and compared commit by commit. No commit changes any object, apart from the objects that embed the git version.
  • Platforms that already fail on main (acp_6_0, acp_7_0, acp_7_x, mt8186, mt8188, mt8195, mt8196, mt8365, phyboard): same error sets.
  • After the rebase on main bfa5201: ptl, ptl-us, mtl, lnl, tgl, imx8m and native_sim build.
  • Cadence xt-clang: MTL, PTL and NVL compile.
  • ZTest: 38 of 38 configurations pass, run as in CI.
  • Host: the host library builds, and the CI testbench steps (SOF alsa-lib/alsa-utils, test topologies, testbench build, host-testbench.sh) pass all 19 component tests.

🤖 Generated with Claude Code

lrgirdwo and others added 10 commits December 1, 2026 22:40
The host builds of the shared code use the SOF atomic names from
posix/include/rtos/atomic.h. To switch that code to the Zephyr names,
the host needs a <zephyr/sys/atomic.h> that behaves like Zephyr's.

Add the stub with atomic_get(), atomic_set(), atomic_add() and
atomic_sub(). Like Zephyr, atomic_set(), atomic_add() and atomic_sub()
return the previous value. The GCC version of the host atomics returned
the new value from add and sub, while the xt-xcc version (for the xt-run
testbench) already returned the previous one. Make the GCC version
return the previous value too, and replace the set and init functions
with a swap that returns it. No host caller uses these return values.

rtos/atomic.h now keeps only atomic_read() and atomic_init() on top of
the stub, as on Zephyr.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Shared code calls the SOF cache wrappers because the host has no
<zephyr/cache.h>. Add a stub with sys_cache_data_flush_range(),
sys_cache_data_invd_range(), sys_cache_data_flush_and_invd_range() and
sys_cache_instr_invd_range(). The host has no cache to maintain, so they
return -ENOTSUP, like Zephyr built without CONFIG_CACHE_MANAGEMENT.

The cached and uncached pointer conversions are not stubbed yet: the
host currently reports every pointer as uncached, which does not map
directly onto the Zephyr calls. They come with their first user.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
Shared code uses assert() and sof_panic() because the host has neither
<zephyr/sys/__assert.h> nor k_panic(). Code that already uses the Zephyr
names has to be hidden from the host builds, such as the coherent API
checks.

Add <zephyr/sys/__assert.h> with __ASSERT() and __ASSERT_NO_MSG(). The
host checks its assertions in every build, as assert() does: a failed
assertion prints the condition and the message, then panics with
SOF_IPC_PANIC_ASSERT.

Add k_panic() to rtos/panic.h, which already defines sof_panic(). It
takes no panic code, so it reports SOF_IPC_PANIC_ASSERT, the code of a
failed assert(): Zephyr also ends a failed assertion with k_panic().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
The host builds use the posix/include stubs instead of the Zephyr
headers, and no test covered them. Add a test that compiles a source
file the way the host builds do, with only the host include paths, so
that <zephyr/...> resolves to the stubs. The ztest code calls it and
compares the results with the Zephyr implementations it is linked with:

- atomic_set(), atomic_add() and atomic_sub() return the previous value,
  and the values match Zephyr's;
- the cache functions return -ENOTSUP, as on native_sim;
- __ASSERT() and __ASSERT_NO_MSG() pass on a true condition, panic with
  SOF_IPC_PANIC_ASSERT on a false one, and evaluate the condition once;
- k_panic() panics with SOF_IPC_PANIC_ASSERT.

The test catches panics by replacing the host __panic() with a longjmp()
back to the test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
On Zephyr, rtos/panic.h includes <zephyr/kernel.h>, so code that
includes it for assert() also gets __ASSERT() and __ASSERT_NO_MSG().
Include the <zephyr/sys/__assert.h> stub from the host rtos/panic.h, so
that shared code can switch to the Zephyr macros without new includes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
On Zephyr, assert() is defined as __ASSERT_NO_MSG(). The host builds
now have the Zephyr assert macros too, so use __ASSERT_NO_MSG() directly
in the audio code.

No lines are added or removed, so __LINE__ in verbose asserts does not
change.

No functional change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
On Zephyr, assert() is defined as __ASSERT_NO_MSG(). The host builds
now have the Zephyr assert macros too, so use __ASSERT_NO_MSG() directly
in the shared headers.

No lines are added or removed, so __LINE__ in verbose asserts does not
change.

No functional change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
On Zephyr, assert() is defined as __ASSERT_NO_MSG(). The host builds
now have the Zephyr assert macros too, so use __ASSERT_NO_MSG() directly
in the IPC code.

No lines are added or removed, so __LINE__ in verbose asserts does not
change.

No functional change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
On Zephyr, assert() is defined as __ASSERT_NO_MSG(). The host builds
now have the Zephyr assert macros too, so use __ASSERT_NO_MSG() directly
in the library, DMA driver and sample code.

No lines are added or removed, so __LINE__ in verbose asserts does not
change.

No functional change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
The coherent API debug check macros still used assert(): the address
alias and core checks, and the host versions of the sleep and atomic
context checks. Use __ASSERT_NO_MSG() like the rest of the header.

The Zephyr and host versions of the context checks stay separate, so
that no lines move and __LINE__ in verbose asserts does not change.

No functional change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
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.

2 participants