Repository navigation
posix: host stubs for the Zephyr atomic, cache and assert APIs - #11583
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
The assertion stub rejects calls without a message or with a format-string variable, causing shared code using these supported Zephyr forms to fail compilation in host builds.
1 open issue
What changed in this PR
Adds host-side Zephyr API stubs to support the migration away from SOF wrappers in #11517. Shared-code conversions remain for follow-up PRs; firmware behavior is unchanged.
Changes:
- Adds atomic stubs and aligns host atomic operations with Zephyr’s previous-value return semantics.
- Adds cache, assertion, and
k_panic()stubs. - Adds native_sim tests for atomic/cache compatibility and host assertion/panic behavior.
| File | Description |
|---|---|
test/ztest/unit/posix_stubs/testcase.yaml |
Registers the native_sim test suite. |
test/ztest/unit/posix_stubs/test_posix_stubs_ztest.c |
Checks stub results and panic behavior. |
test/ztest/unit/posix_stubs/prj.conf |
Configures the standalone ztest application. |
test/ztest/unit/posix_stubs/posix_stubs.h |
Defines the interface between host and Zephyr test code. |
test/ztest/unit/posix_stubs/posix_stubs.c |
Exercises host stubs and intercepts panics. |
test/ztest/unit/posix_stubs/CMakeLists.txt |
Compiles stub tests with isolated host include paths. |
src/kernel/arch/host/include/arch/atomic.h |
Adds atomic exchange and returns previous values. |
posix/include/zephyr/sys/atomic.h |
Exposes Zephyr-named atomic operations. |
posix/include/zephyr/sys/__assert.h |
Adds host assertion macros and diagnostics. |
posix/include/zephyr/cache.h |
Adds cache-range stubs returning -ENOTSUP. |
posix/include/rtos/panic.h |
Adds the host k_panic() mapping. |
posix/include/rtos/atomic.h |
Retains SOF compatibility wrappers over the new stubs. |
Review effort: Balanced
| #define __ASSERT(test, fmt, ...) \ | ||
| do { \ | ||
| if (!(test)) { \ | ||
| fprintf(stderr, "ASSERTION FAIL [%s] @ %s:%d\n\t" fmt "\n", \ | ||
| #test, RELATIVE_FILE, __LINE__, ##__VA_ARGS__); \ |
There was a problem hiding this comment.
Fixed in e506959: __ASSERT(test, ...) now takes an optional message, like Zephyr. The format and its arguments go to a small vfprintf() helper instead of being concatenated, so a format that is not a literal works too. The posix_stubs test now covers __ASSERT() with no message and with a variable format, in both the passing and the failing case.
af347b1 to
e506959
Compare
PR 11583: test resultsRun date: 2026-12-03 14:25 UTC Tested commit: 56fc1eeb3d6c567ab0ec2470a05b407ba2ec4230 |
There was a problem hiding this comment.
Not sure this series is correct. Are we dropping testbench and cmocka? If yes, then we should be able to use existing Zephyr posix layer to provide these functions.
The SOF side src/kernel/src is only needed for non-Zephyr builds like testbench and cmocka. I don't think we should make Zephyr posix builds depend on these.
UPDATE: ok, it seems the SOF host layer does already use the posix includes, so that's why they are mixer. I think the series is correct, although especially the last ztest commit is adding to the confusion between two host builds we support (one with Zephyr and one without).
| */ | ||
|
|
||
| #ifdef __SOF_ATOMIC_H__ | ||
| #if defined(__SOF_ATOMIC_H__) || defined(__POSIX_ZEPHYR_SYS_ATOMIC_H__) |
There was a problem hiding this comment.
I don't follow this. This host abstraction is only used by testbench and cmocka, should not be needed for Zephyr posix builds (SOF fuzzer build, ztests)
| @@ -0,0 +1,28 @@ | |||
| # SPDX-License-Identifier: BSD-3-Clause | |||
There was a problem hiding this comment.
This commit message has issues. Zephyr ztest builds should use Zephyr posix runtime and should not need these wrappers.
There was a problem hiding this comment.
Ok, this is specifically implementing a Zephyr ztest to implement the stubs that are not really needed by host builds that use Zephyr.
There was a problem hiding this comment.
Agreed, the naming and the commit message mixed up the two host builds. In 56fc1ee:
- The test is now
host_stubsinstead ofposix_stubs, so it no longer reads like the Zephyr posix arch. - The commit message and a comment at the top of
CMakeLists.txtsay what it is: the Zephyr host builds (these ztests, the IPC fuzzer) use Zephyr itself, and the test only uses ztest and native_sim as a harness to check theposix/includestubs of the non-Zephyr host library and testbench. No Zephyr build depends on the stubs.
kv2019i
left a comment
There was a problem hiding this comment.
I'll change my -1 to +1 , see updated comments
cmocka has gone. testbench is going to sof-utils (as FW repo will be FW code only), and via west testbench will sync the headers/source from sof repo. There is transitional changes as we move, but all non FW files will eventually be elsewhere when done. |
The non-Zephyr host builds (the host library and the testbench) compile the shared code against posix/include, which gives it the SOF atomic names from rtos/atomic.h. To switch that code to the Zephyr names, these builds need a <zephyr/sys/atomic.h> that behaves like Zephyr's. Zephyr builds, native_sim included, keep using the Zephyr header. 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>
In the non-Zephyr host builds (the host library and the testbench), shared code calls the SOF cache wrappers because posix/include 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. Zephyr builds, native_sim included, keep using the Zephyr header. 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 non-Zephyr host builds (the host library and the testbench) have neither <zephyr/sys/__assert.h> nor k_panic() in posix/include. Code that already uses the Zephyr names has to be hidden from these builds, such as the coherent API checks. Add <zephyr/sys/__assert.h> with __ASSERT() and __ASSERT_NO_MSG(). As in Zephyr, the __ASSERT() message is optional and its format does not have to be a string literal. The host checks its assertions in every build, as assert() does: a failed assertion prints the condition and any 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(). Zephyr builds, native_sim included, keep using the Zephyr macros. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
SOF has two kinds of host build. The Zephyr ones, such as the native_sim ztests and the IPC fuzzer, use Zephyr itself and never see the stubs in posix/include. The non-Zephyr host library and testbench compile the shared code against posix/include, and nothing tested its stubs of the Zephyr APIs. Add a test that compiles one source file the way the non-Zephyr host builds compile the shared code: with the host include paths only, so that <zephyr/...> resolves to the stubs. The ztest code, built normally against Zephyr, calls it and compares the results with the Zephyr implementations. ztest and native_sim are only the harness here; the Zephyr builds do not depend on the stubs. The test checks that: - 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 and panic with SOF_IPC_PANIC_ASSERT on a false one, with a literal message, a format that is not a literal and no message, and evaluate the condition once; - k_panic() panics with SOF_IPC_PANIC_ASSERT. It 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>
e506959 to
56fc1ee
Compare
|
CI unrelated |

Add host stubs for the Zephyr atomic, cache and assert APIs, so that shared code can use the Zephyr names instead of the SOF wrappers. Part of #11517 (step 3, "Host stubs with Zephyr names").
SOF has two kinds of host build. The Zephyr ones (the native_sim ztests, the IPC fuzzer) use Zephyr itself and are not affected by this PR. The non-Zephyr host builds (the host library and the testbench) compile
src/audio,src/ipc,src/libandsrc/moduleagainstposix/includeinstead of the Zephyr headers. Shared code can only switch to a Zephyr name whenposix/includehas a stub with the same name and the same behaviour. This PR adds those stubs and a test that compares them with Zephyr. The shared code is converted in follow-up PRs.Patches
posix:<zephyr/sys/atomic.h>stub:atomic_get(),atomic_set(),atomic_add()andatomic_sub(). As in Zephyr,atomic_set(),atomic_add()andatomic_sub()return the previous value.rtos/atomic.hkeeps onlyatomic_read()andatomic_init()on top of the stub, aszephyr/include/rtos/atomic.hdoes on Zephyr.posix:<zephyr/cache.h>stub: the foursys_cache_*_range()functions. The host has no cache to maintain, so they return-ENOTSUP, like Zephyr built withoutCONFIG_CACHE_MANAGEMENT.posix: assert andk_panic()stubs:<zephyr/sys/__assert.h>with__ASSERT()and__ASSERT_NO_MSG(). As in Zephyr, the__ASSERT()message is optional and its format does not have to be a string literal. The host checks its assertions in every build, asassert()does: a failed assertion prints the condition and any message, then panics withSOF_IPC_PANIC_ASSERT.k_panic()inrtos/panic.h. It takes no panic code, so it reportsSOF_IPC_PANIC_ASSERT, the code of a failedassert().test: ztest:host_stubs: compiles one source file the way the non-Zephyr host builds compile the shared code, with only the host include paths, so that<zephyr/...>resolves to the stubs. The ztest code, built normally against Zephyr, calls it and compares the results with the Zephyr implementations. ztest and native_sim are only the harness; the Zephyr builds do not depend on the stubs. The test checks that:-ENOTSUP, as on native_sim;SOF_IPC_PANIC_ASSERTon a false one, and evaluate the condition once, with a literal message, a non-literal format and no message;k_panic()panics withSOF_IPC_PANIC_ASSERT.Not in this PR
atomic_read/atomic_init, cache wrappers,assert()): follow-up PRs, one per API.sys_cache_cached_ptr_get()and friends: the host reports every pointer as uncached (is_uncached()is 1), which does not map directly onto the Zephyr calls. They come with their first user.<zephyr/pm/policy.h>: it needs a host implementation file for its lock counter, so it comes with the IPC handler change that uses it.Functional change
posix/includeor on the host arch headers (checked withninja -t depsin every configured firmware build of the test matrix).Testing
host_stubstest (6 of 6 cases).atomic_add()returns the new value again.posix/include,src/kernel/arch/host/includeandsrc/includeon its include path.__XCC__defined).testbench.ymlsteps in the SOF docker image (SOF alsa-lib/alsa-utils at the CI commits, test topologies, testbench build,host-testbench.sh): all 19 component tests pass.sof-hda-generictopology: boots, and the SOF smoke tests pass (11 of 11: analog playback at 48 kHz S16/S24/S32 and 44.1 kHz, deep-buffer playback, analog capture S16/S32, full duplex, concurrent analog and deep-buffer playback, 10 start/stop cycles, 3 runtime D3 suspend/resume cycles with playback).🤖 Generated with Claude Code