Skip to content

testing/ostest: split the fork test into task_fork, vfork and fork - #3673

Draft
casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:fork-semantics-ostest
Draft

testing/ostest: split the fork test into task_fork, vfork and fork#3673
casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:fork-semantics-ostest

Conversation

@casaroli

@casaroli casaroli commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

Note: Please adhere to Contributing Guidelines.

Summary

This is the apps half of apache/nuttx#19540, and it lands first.

NuttX implements fork() and vfork() as the same function, and is gaining
the three separate primitives that issue describes: task_fork() (shares
memory, private stack copy, both running), vfork() (shares memory, parent
suspended until _exit()/exec()) and POSIX fork() (child gets its own
copy). 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 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. It is the clearest single piece of evidence for the whole
proposal: the test upstream has been running for years is a task_fork() test
wearing vfork()'s name, sitting under a CONFIG_ARCH_HAVE_FORK guard.

vfork.c is rewritten to test what vfork() actually 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
; that is exactly the misuse vfork()'s restrictions exist to
prevent, 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 child
status is not retained — ostest_main() sets SA_NOCLDWAIT for the whole run,
deliberately — ECHILD is accepted as equally good evidence, since 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. 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_sim wants a daemon that outlives its caller and shares
    its memory — task_fork().
  • interpreters/python's _posixsubprocess and netutils/libwebsockets'
    LWS_HAVE_WORKING_VFORK want the fork-then-exec path — vfork().
  • python's os.fork() (ac_cv_func_fork) and libwebsockets'
    LWS_HAVE_FORK / LWS_HAVE_WORKING_FORK mean real fork() and stay on
    CONFIG_ARCH_HAVE_FORK — which, correctly, is how they now become absent
    rather than silently wrong.
  • testing/fs/fdsantest's vfork case follows vfork().

Two third-party suites need their source lists narrowed, because they call
fork() from code compiled unconditionally:

  • system/libuvtest-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, compiled only because fork() happened to be declared. Nothing is
    lost.
  • testing/ltp — the open_posix_testsuite is filtered through LTP's
    existing BLACKWORDS mechanism, which already drops tests for absent
    features and is already conditioned on configuration symbols
    (CONFIG_FS_AIO, CONFIG_PTHREAD_SPINLOCKS). Where fork() is not
    provided this drops 278 of 1943 test files. The pattern [^v_]fork(
    deliberately spares vfork() and task_fork(), which remain available.
    Where fork() is provided — which today is everywhere — nothing is
    dropped, 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 without fork() they
cannot link. They come back per architecture as real fork() lands.

Two other fork() mentions in apps turned out to need nothing:
games/NXDoom's is inside #if 0 /* UNUSED */, and system/syslogd already
uses posix_spawn() — "fork()" survives there only in an error message.

One caller is deliberately left alone: interpreters/bas. Its SHELL and
EDIT statements call vfork() and are gated on CONFIG_ARCH_HAVE_FORK, so
they want the same treatment as the others — but checkpatch.sh checks the
whole of any file a patch touches, and bas_statement.c produces 1681
pre-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_SHELL is EXPERIMENTAL and already depends on ARCH_HAVE_FORK, so once NuttX lands the split it simply becomes unselectable
rather than misbehaving.

Impact

Merged on its own, against today's NuttX, nothing regresses.
CONFIG_ARCH_HAVE_TASK_FORK and CONFIG_ARCH_HAVE_VFORK do not exist yet, so
everything here also accepts the CONFIG_ARCH_HAVE_FORK that stands in for
them: today's fork() is task_fork(), and today's vfork() is that plus a
waitpid(). ostest therefore keeps building and passing both
task_fork_test and vfork_test exactly as it builds and passes the current
vfork test.

fork_test() deliberately has no such fallback. The copy semantics it
checks are precisely 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.
Against today's NuttX it is simply not built.

The compatibility layer is three #defines in ostest.h
(OSTEST_HAVE_TASK_FORK / _VFORK / _FORK), a task_fork() -> fork() shim,
and || ARCH_HAVE_FORK on four Kconfig/preprocessor guards elsewhere. A small
follow-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.yml runs — ✔️ All checks pass, with codespell,
cvt2utf, cmake-format and nxstyle all installed.

CONFIG_TESTING_NAND_SIM gains a dependency on
ARCH_HAVE_TASK_FORK || ARCH_HAVE_FORK. It called fork() unconditionally
before 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-gcc 14.2.0-3.

Against unmodified apache/nuttx master (7fd17c9d7c) — the case this PR must not break

rv-virt:nsh64, ostest:

  • Config has only CONFIG_ARCH_HAVE_FORK=y, as expected — neither new symbol
    exists.
  • nm on the image shows task_fork_test and vfork_test built, and
    fork_test absent, which is exactly the intent.
  • Run: 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 PR
branch and mapped every object back to its source:

fork-using LTP sources that produced an object: 0 (of 278)
other LTP objects built:                        896
implicit-declaration errors for fork():         0

Against the NuttX PR branch

Full ostest suite to exit status 0 on rv-virt:nsh64 (FLAT),
rv-virt:pnsh64 (PROTECTED), rv-virt:knsh64 (KERNEL), qemu-armv7a:nsh,
qemu-armv8a:nsh and qemu-intel64:nsh, with task_fork_test and
vfork_test passing and fork_test correctly absent — no architecture
provides POSIX fork() at that point in the series. sim:ostest also builds
and passes both.

fork_test() itself is verified by the per-architecture PRs that follow, which
are what turn CONFIG_ARCH_HAVE_FORK back 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 first
up_addrenv_fork() lands.

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
casaroli force-pushed the fork-semantics-ostest branch from 94d1583 to e32964b Compare July 28, 2026 07:18

@jerpelea jerpelea left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
casaroli force-pushed the fork-semantics-ostest branch from e32964b to 536349e Compare July 28, 2026 07:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants