testing/ostest: split the fork test into task_fork, vfork and fork - #3673
Draft
casaroli wants to merge 1 commit into
Draft
testing/ostest: split the fork test into task_fork, vfork and fork#3673casaroli wants to merge 1 commit into
casaroli wants to merge 1 commit into
Conversation
casaroli
force-pushed
the
fork-semantics-ostest
branch
2 times, most recently
from
July 28, 2026 04:58
a34eda0 to
94d1583
Compare
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: CONFIG_ARCH_HAVE_TASK_FORK and CONFIG_ARCH_HAVE_VFORK do not exist in nuttx yet, so everything here also accepts the CONFIG_ARCH_HAVE_FORK that stands in for them today -- today's fork() *is* task_fork(), and today's vfork() is that plus a waitpid(). fork_test() deliberately has no such fallback: the copy semantics it checks are exactly what today's fork() does not provide, so it is gated on ARCH_HAVE_VFORK, whose existence is the evidence that the split has landed. A follow-up removes the fallbacks once it has. Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com> Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
casaroli
force-pushed
the
fork-semantics-ostest
branch
from
July 28, 2026 07:18
94d1583 to
e32964b
Compare
jerpelea
requested changes
Jul 28, 2026
jerpelea
left a comment
Contributor
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
casaroli
force-pushed
the
fork-semantics-ostest
branch
from
July 28, 2026 07:53
e32964b to
536349e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note: Please adhere to Contributing Guidelines.
Summary
This is the
appshalf of apache/nuttx#19540, and it lands first.NuttX implements
fork()andvfork()as the same function, and is gainingthe three separate primitives that issue describes:
task_fork()(sharesmemory, private stack copy, both running),
vfork()(shares memory, parentsuspended until
_exit()/exec()) and POSIXfork()(child gets its owncopy). This PR is written to work against NuttX with or without that
change, so the tests keep running across the transition instead of silently
compiling out. Companion PR: apache/nuttx#19562.
ostest's "vfork" test was never testingvfork(). It has the child writea global and the parent observe the write — which is the defining property of
sharing, not of
vfork(), whose defining property is that the parent issuspended 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 hasalways described. It is the clearest single piece of evidence for the whole
proposal: the test upstream has been running for years is a
task_fork()testwearing
vfork()'s name, sitting under aCONFIG_ARCH_HAVE_FORKguard.vfork.cis rewritten to test whatvfork()actually promises. The childdoes only what POSIX permits — it calls
_exit(42)and nothing else, not evenexit(), which would runatexithandlers and flush stdio in the parent'saddress space; that is exactly the misuse
vfork()'s restrictions exist toprevent, and a test that did it would be testing the wrong thing. Because 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 reached
waitpid()while the child was still alive. Where childstatus is not retained —
ostest_main()setsSA_NOCLDWAITfor the whole run,deliberately —
ECHILDis accepted as equally good evidence, since it says thechild 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 astack local taken before the fork names the same object in both; and the child
does everything a
vfork()child may not — callsmalloc()andprintf(),and returns from the function that called
fork().All three run at the top of
user_main()rather than in the middle. Theyexercise 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. 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:
testing/drivers/nand_simwants a daemon that outlives its caller and sharesits memory —
task_fork().interpreters/python's_posixsubprocessandnetutils/libwebsockets'LWS_HAVE_WORKING_VFORKwant the fork-then-exec path —vfork().python'sos.fork()(ac_cv_func_fork) andlibwebsockets'LWS_HAVE_FORK/LWS_HAVE_WORKING_FORKmean realfork()and stay onCONFIG_ARCH_HAVE_FORK— which, correctly, is how they now become absentrather 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 the
test-*.cglob. Every test they define is alreadyexcluded from the task list on NuttX by
0001-libuv-port-for-nuttx.patch—the nine
fork_*entries andpipe_close_stdout_read_stdin— so they weredead code, compiled only because
fork()happened to be declared. Nothing islost.
testing/ltp— theopen_posix_testsuiteis filtered through LTP'sexisting
BLACKWORDSmechanism, which already drops tests for absentfeatures and is already conditioned on configuration symbols
(
CONFIG_FS_AIO,CONFIG_PTHREAD_SPINLOCKS). Wherefork()is notprovided this drops 278 of 1943 test files. The pattern
[^v_]fork(deliberately spares
vfork()andtask_fork(), which remain available.Where
fork()is provided — which today is everywhere — nothing isdropped, so this is a no-op against current master.
That 278-file loss is the honest price of the change and it is worth stating
plainly: those tests exercise
fork(), and on a target withoutfork()theycannot link. They come back per architecture as real
fork()lands.Two other
fork()mentions inappsturned out to need nothing:games/NXDoom's is inside#if 0 /* UNUSED */, andsystem/syslogdalreadyuses
posix_spawn()— "fork()" survives there only in an error message.One caller is deliberately left alone:
interpreters/bas. ItsSHELLandEDITstatements callvfork()and are gated onCONFIG_ARCH_HAVE_FORK, sothey want the same treatment as the others — but
checkpatch.shchecks thewhole of any file a patch touches, and
bas_statement.cproduces 1681pre-existing nxstyle and codespell findings before this patch is applied at
all. A one-newline commit against master fails CI identically. Migrating BAS
therefore has to follow a style cleanup of that file, and neither belongs in
this series. The practical consequence is small and self-consistent:
CONFIG_EXAMPLES_BAS_SHELLisEXPERIMENTALand alreadydepends on ARCH_HAVE_FORK, so once NuttX lands the split it simply becomes unselectablerather than misbehaving.
Impact
Merged on its own, against today's NuttX, nothing regresses.
CONFIG_ARCH_HAVE_TASK_FORKandCONFIG_ARCH_HAVE_VFORKdo not exist yet, soeverything here also accepts the
CONFIG_ARCH_HAVE_FORKthat stands in forthem: today's
fork()istask_fork(), and today'svfork()is that plus awaitpid().ostesttherefore keeps building and passing bothtask_fork_testandvfork_testexactly as it builds and passes the currentvforktest.fork_test()deliberately has no such fallback. The copy semantics itchecks are precisely what today's
fork()does not provide, so it is gated onARCH_HAVE_VFORK, whose existence is the evidence that the split has landed.Against today's NuttX it is simply not built.
The compatibility layer is three
#defines inostest.h(
OSTEST_HAVE_TASK_FORK/_VFORK/_FORK), atask_fork() -> fork()shim,and
|| ARCH_HAVE_FORKon four Kconfig/preprocessor guards elsewhere. A smallfollow-up removes all of it once the NuttX side is in; that follow-up must
not merge before the NuttX PR.
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.CONFIG_TESTING_NAND_SIMgains a dependency onARCH_HAVE_TASK_FORK || ARCH_HAVE_FORK. It calledfork()unconditionallybefore and would not have linked on a target without it.
Testing
Host: macOS 15 (Darwin 25.5.0) on Apple Silicon. QEMU 11.0.3, xPack
riscv-none-elf-gcc14.2.0-3.Against unmodified
apache/nuttxmaster (7fd17c9d7c) — the case this PR must not breakrv-virt:nsh64,ostest:CONFIG_ARCH_HAVE_FORK=y, as expected — neither new symbolexists.
nmon the image showstask_fork_testandvfork_testbuilt, andfork_testabsent, which is exactly the intent.task_fork_test: Child 5 ran successfully,vfork_test: Child 6 ran and exited before the parent resumed,ostest_main: Exiting with status 0.The LTP filter, verified
Built
rv-virt:citest(the CI config that enables LTP) against the NuttX PRbranch 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 architectureprovides POSIX
fork()at that point in the series.sim:ostestalso buildsand passes both.
fork_test()itself is verified by the per-architecture PRs that follow, whichare what turn
CONFIG_ARCH_HAVE_FORKback on. It has been run to completion onRISC-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 itis not being added untested; it is simply not reachable until the first
up_addrenv_fork()lands.