Repository navigation
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.
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>
af347b1 to
e506959
Compare
PR 11583: test resultsRun date: 2026-12-02 21:29 UTC Tested commit: e506959ecbf45897dbe6d5bb0eb0f43d1b0cf9f6 |

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").
The host builds (host library, testbench) compile
src/audio,src/ipc,src/libandsrc/moduleagainstposix/includeinstead of the Zephyr headers. Shared code can only switch to a Zephyr name when the host has 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:posix_stubs: compiles one 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:-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
posix_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.🤖 Generated with Claude Code