testing/ostest: split the fork test into task_fork, vfork and fork - #3673
testing/ostest: split the fork test into task_fork, vfork and fork#3673casaroli wants to merge 1 commit into
Conversation
94d1583 to
e32964b
Compare
jerpelea
left a comment
There was a problem hiding this comment.
please replace
Co-Authored-By: Claude Opus 5 (1M context) noreply@anthropic.com
with
Assisted-by: Claude Opus 5 (1M context) noreply@anthropic.com
536349e to
2a694fe
Compare
|
|
||
| # See testing/ltp/Makefile. | ||
|
|
||
| if(NOT CONFIG_ARCH_HAVE_FORK AND NOT CONFIG_FORK_IS_TASK_FORK) |
There was a problem hiding this comment.
why need CONFIG_FORK_IS_TASK_FORK
There was a problem hiding this comment.
This is a retro compatibility flag disabled by default. It is available to alias fork() to task_fork() in non MMU systems, in case someone depends on the exact current NuttX behavior without needing to update their program source code.
There was a problem hiding this comment.
it's config FORK_IS_TASK_FORK in arch/Kconfig in apache/nuttx#19562 — default n, depends on TASK_FORK && !ARCH_HAVE_FORK
| */ | ||
|
|
||
| #if defined(CONFIG_TASK_FORK) || \ | ||
| (defined(CONFIG_ARCH_HAVE_FORK) && !defined(CONFIG_ARCH_HAVE_TASK_FORK)) |
There was a problem hiding this comment.
why need check CONFIG_ARCH_HAVE_FORK/CONFIG_ARCH_HAVE_TASK_FORK
There was a problem hiding this comment.
This is required for this to merge before the nuttx PR. We can remove this with a follow up pr after the companion pr in nuttx gets merged
| # define OSTEST_HAVE_FORK 1 | ||
| #endif | ||
|
|
||
| #if defined(OSTEST_HAVE_TASK_FORK) && !defined(CONFIG_TASK_FORK) |
There was a problem hiding this comment.
why need the above mapping?
it's better to just check CONFIG_ARCH_TASK_FORK/CONFIG_ARCH_FORK/CONFIG_ARCH_VFORK.
There was a problem hiding this comment.
This is required for this to merge before the nuttx PR. We can remove this with a follow up pr after the companion pr in nuttx gets merged
|
|
||
| ifeq ($(CONFIG_TASK_FORK),y) | ||
| CSRCS += task_fork.c | ||
| else ifeq ($(CONFIG_ARCH_HAVE_FORK)$(CONFIG_ARCH_HAVE_TASK_FORK),y) |
There was a problem hiding this comment.
replaced this with nested ifeq. This is required for this to merge before the nuttx PR. We can remove this with a follow up pr when the companion pr in nuttx gets merged
| CSRCS += vfork.c | ||
| endif | ||
|
|
||
| ifeq ($(CONFIG_ARCH_HAVE_FORK)$(CONFIG_ARCH_HAVE_VFORK),yy) |
There was a problem hiding this comment.
why need check CONFIG_ARCH_HAVE_VFORK for fork test
There was a problem hiding this comment.
if we do only CONFIG_ARCH_HAVE_FORK then all architectures will build that today, and lm3s6965-ek:qemu-protected is already almost over budget, it would not fit in .text. We can remove this with a follow up pr after the companion pr in nuttx gets merged.
In fact, we will need to decide how we deal with lm3s6965-ek:qemu-protected in the companion PR because I think .text will overflow there.
|
|
||
| #include "ostest.h" | ||
|
|
||
| #ifdef OSTEST_HAVE_FORK |
There was a problem hiding this comment.
already done in Makefile, why need check again?
| #include "ostest.h" | ||
|
|
||
| #if defined(CONFIG_ARCH_HAVE_FORK) && defined(CONFIG_SCHED_WAITPID) | ||
| #ifdef OSTEST_HAVE_VFORK |
nuttx implements fork() and vfork() as the same function, and is gaining the three separate primitives its issue #19540 describes: task_fork() (shares memory, private stack copy, both running), vfork() (shares memory, parent suspended) and POSIX fork() (child gets its own copy). This is the apps side of that, and it lands first: it works against nuttx with or without the split, so the tests keep running across the transition rather than silently compiling out. ostest's "vfork" test was never testing vfork(). It has the child write a global and the parent observe the write -- which is the defining property of *sharing*, not of vfork(), whose defining property is that the parent is suspended and whose contract forbids the child to write anything at all. It is renamed to task_fork.c, unchanged, because that is the primitive it has always described. vfork.c is rewritten to test what vfork() promises. The child does only what POSIX permits -- it calls _exit(42), and nothing else, not even exit(), which would run atexit handlers and flush stdio in the parent's address space. The observable is therefore the child's exit status rather than a memory write. Where child status is not retained -- ostest_main() sets SA_NOCLDWAIT for the whole run, deliberately -- waitpid() returning ECHILD is accepted as equally good evidence: it says the child was already gone when the parent asked. fork.c is new and tests POSIX fork(): the child's writes to .data, .bss and the heap are invisible to the parent and vice versa, a pointer to a stack local taken before the fork names the same object in both, and the child does everything a vfork() child may not -- calls malloc() and printf(), and returns from the function that called fork(). All three run at the top of user_main() rather than in the middle. They exercise the lowest-level machinery in the suite -- address environments, stack setup, the architecture's register context -- so a fault in one takes the process down instead of reporting a failure, and finding that out in seconds rather than after everything else has passed is the difference between a usable iteration and a coffee break when a port is being brought up. The other in-tree callers are audited for which primitive they actually meant. nand_sim wants a daemon that outlives its caller and shares its memory, which is task_fork(). bas's SHELL and EDIT statements, python's _posixsubprocess and libwebsockets' feature macros want the fork-then-exec path, which vfork() serves; python's os.fork() and libwebsockets' LWS_HAVE_FORK stay on fork() proper. fdsantest's vfork case follows vfork(). Two third-party suites need their source lists narrowed, because they call fork() from code that is compiled unconditionally: * system/libuv -- test-fork.c and test-pipe-close-stdout-read-stdin.c are filtered out of the test-*.c glob. Every test they define is already excluded from the task list on NuttX by 0001-libuv-port-for-nuttx.patch -- the nine fork_* entries and pipe_close_stdout_read_stdin -- so they were dead code being compiled only because fork() happened to be declared. * testing/ltp -- the open_posix_testsuite is filtered through the existing BLACKWORDS mechanism, which already drops tests for absent features and is already conditioned on configuration symbols. Where fork() is not provided this drops 278 of 1943 test files; the pattern is written to spare vfork() and task_fork(), which remain available. Where fork() is provided -- which today is everywhere -- nothing is dropped. Compatibility: the nuttx symbols this keys on do not exist yet. task_fork.c is built where CONFIG_TASK_FORK says task_fork() was built and, on a nuttx that has no CONFIG_ARCH_HAVE_TASK_FORK at all -- which is the pre-split one -- where CONFIG_ARCH_HAVE_FORK does. Today's fork() *is* task_fork(), so the test that has always covered that primitive keeps running, under its own name, and no coverage is lost across the transition. Both spellings are needed because CONFIG_TASK_FORK is optional on the nuttx side: ARCH_HAVE_TASK_FORK says the architecture can clone a task, TASK_FORK says this build asked for it. A follow-up removes the fallback once the split has landed. vfork_test() and fork_test() deliberately have no such fallback. Both check semantics a pre-split nuttx does not describe -- the parent suspension and the private copy -- and ARCH_HAVE_VFORK is the evidence that the split has landed. Mapping them onto ARCH_HAVE_FORK would also add a test to configurations that never had one, which is not free: vfork.c costs about 470 bytes of .text on armv7-m at -Os, and that is what put lm3s6965-ek:qemu-protected over its 128 KiB user flash region. Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com> Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2a694fe to
7aac6c5
Compare
|
Follow up pr #3685 |
Summary
This is the
appshalf of apache/nuttx#19540, and it must merge first. Companion PR: apache/nuttx#19562, whose CI cannot go green until this one lands, because NuttX PRs build againstappsmaster and three things here callfork()unconditionally.NuttX implements
fork()andvfork()as the same function, and is gaining three separate primitives:task_fork()(shares memory, private stack copy, both running),vfork()(shares memory, parent suspended until_exit()/exec()) and POSIXfork()(child gets its own copy). This PR works against NuttX with or without that change, so no test coverage is lost across the transition.ostest's "vfork" test was never testingvfork(). It has the child write a global and the parent observe the write — the defining property of sharing, not ofvfork(), whose defining property is that the parent is suspended and whose contract forbids the child to write anything at all. It is renamed totask_fork.c, unchanged, because that is the primitive it has always described. It is also the clearest single piece of evidence for the proposal: the test upstream has run for years is atask_fork()test wearingvfork()'s name.vfork.cis rewritten to test whatvfork()promises. The child does only what POSIX permits — it calls_exit(42)and nothing else, not evenexit(), which would runatexithandlers and flush stdio in the parent's address space. Since the child may not write memory and the parent cannot run while the child lives, the observable is the child's exit status: had the parent not been suspended, it would have reachedwaitpid()while the child was still alive. Where child status is not retained —ostest_main()setsSA_NOCLDWAITfor the whole run, deliberately —ECHILDis accepted as equally good evidence, since it says the child was already gone when the parent asked.fork.cis new and tests POSIXfork(): the child's writes to.data,.bssand the heap are invisible to the parent and vice versa, a pointer to a stack local taken before the fork names the same object in both, and the child does everything avfork()child may not — callsmalloc()andprintf(), and returns from the function that calledfork().All three run at the top of
user_main(). They exercise the lowest-level machinery in the suite — address environments, stack setup, the architecture's register context — so a fault in one takes the process down instead of reporting a failure. Learning that in seconds rather than after everything else has passed matters when a port is being brought up.The other in-tree callers are audited for which primitive they actually meant:
testing/drivers/nand_simwants a daemon that outlives its caller and shares its memory —task_fork().interpreters/python's_posixsubprocessandnetutils/libwebsockets'LWS_HAVE_WORKING_VFORKwant the fork-then-exec path —vfork().python'sos.fork()andlibwebsockets'LWS_HAVE_FORKmean realfork()and stay onCONFIG_ARCH_HAVE_FORK, so they become absent rather than silently wrong.testing/fs/fdsantest'svforkcase followsvfork().Two third-party suites need their source lists narrowed, because they call
fork()from code compiled unconditionally:system/libuv—test-fork.candtest-pipe-close-stdout-read-stdin.care filtered out of thetest-*.cglob. Every test they define is already excluded from the task list on NuttX by0001-libuv-port-for-nuttx.patch, so they were dead code compiled only becausefork()happened to be declared. Nothing is lost.testing/ltp— theopen_posix_testsuiteis filtered through LTP's existingBLACKWORDSmechanism, which already drops tests for absent features and is already conditioned on configuration symbols. Wherefork()is not provided this drops 278 of 1943 test files; the pattern[^v_]fork(sparesvfork()andtask_fork(). Wherefork()is provided — everywhere, today — nothing is dropped, so it is a no-op against current master.That 278-file loss is the honest price of the change: those tests exercise
fork(), and on a target withoutfork()they cannot link. They return per architecture as realfork()lands.Two other
fork()mentions need nothing:games/NXDoom's is inside#if 0 /* UNUSED */, andsystem/syslogdalready usesposix_spawn().interpreters/basis deliberately left alone. ItsSHELLandEDITstatements want the same treatment as the others, butcheckpatch.shchecks the whole of any file a patch touches, andbas_statement.cproduces 1681 pre-existing findings before this patch is applied at all — a one-newline commit against master fails CI identically. Migrating BAS has to follow a style cleanup of that file, and neither belongs here. The consequence is small:CONFIG_EXAMPLES_BAS_SHELLisEXPERIMENTALand alreadydepends on ARCH_HAVE_FORK, so it becomes unselectable rather than misbehaving.Impact
Against today's NuttX,
ostestbuilds and runs exactly the fork test it runs now.task_fork.cis that test, byte for byte, under the name of the primitive it describes. A NuttX withoutCONFIG_ARCH_HAVE_TASK_FORKis the pre-split one, and only there doesCONFIG_ARCH_HAVE_FORKstand in, withtask_fork()mapped tofork().vfork_test()andfork_test()have no such fallback, deliberately. Both check semantics a pre-split NuttX does not describe — the parent suspension and the private copy — so mapping them ontoCONFIG_ARCH_HAVE_FORKwould add tests to configurations that never had one. That is not free:vfork.ccosts about 470 bytes of.texton armv7-m at-Os, which putlm3s6965-ek:qemu-protectedover its 128 KiB user flash region. They activate on the symbol that announces the primitive,CONFIG_ARCH_HAVE_VFORK.task_fork.ckeys onCONFIG_TASK_FORK, not on the capability symbol. On the NuttX sideARCH_HAVE_TASK_FORKsays the architecture can clone a task whileTASK_FORKsays the build asked for it, andtask_fork()is only declared under the latter. Gating the test on the capability alone would fail to compile aTASK_FORK=nbuild.CONFIG_TESTING_NAND_SIMgains the same dependency; it calledfork()unconditionally before and would not have linked on a target without it.The whole compatibility layer is one
#ifpair and atask_fork() -> fork()shim inostest.h, plus the matching build-file conditions. A small follow-up removes it once the NuttX side is in; that follow-up must not merge before the NuttX PR.Testing
Host: macOS 15 (Darwin 25.5.0) on Apple Silicon. QEMU 11.0.3, xPack
riscv-none-elf-gcc14.2.0-3, Arm GNUarm-none-eabi-gcc14.2.Rel1.Against unmodified NuttX master (
5a7f1b5005) — the case this PR must not breakrv-virt:nsh64,ostest: config has onlyCONFIG_ARCH_HAVE_FORK=y, as expected — neither new symbol exists.nmon the image showstask_fork_testbuilt,vfork_testandfork_testabsent — exactly the intent. Run:task_fork_test: Child 5 ran successfully,ostest_main: Exiting with status 0.lm3s6965-ek:qemu-protected, the size-constrained configuration this PR must not overflow: builds clean, 596 bytes free of the 128 KiBuflashregion against a 708-byte baseline onappsmaster.The LTP filter, verified
Built
rv-virt:citest(the CI config that enables LTP) against the NuttX PR branch and mapped every object back to its source:Against the NuttX PR branch
Full
ostestsuite to exit status 0 onrv-virt:nsh64(FLAT),rv-virt:pnsh64(PROTECTED),rv-virt:knsh64(KERNEL),qemu-armv7a:nsh,qemu-armv8a:nshandqemu-intel64:nsh, withtask_fork_testandvfork_testpassing andfork_testcorrectly absent — no architecture provides POSIXfork()at that point in the series.sim:ostestalso builds and passes both.fork_test()itself is verified by the per-architecture PRs that follow, which are what turnCONFIG_ARCH_HAVE_FORKback on. It has been run to completion on RISC-V, arm64, armv7-a and x86_64 kernel builds on the development branch those PRs are cut from —fork_test: Parent and child had independent memory— so it is not being added untested; it is simply not reachable until the firstup_addrenv_fork()lands.Style
../nuttx/tools/checkpatch.sh -c -u -m -g <base>..HEAD, the exact command.github/workflows/check.ymlruns — ✔️ All checks pass, withcodespell,cvt2utf,cmake-formatandnxstyleall installed.