Skip to content

testing/ostest: Split the fork test into vfork and fork. - #3685

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

testing/ostest: Split the fork test into vfork and fork.#3685
casaroli wants to merge 1 commit into
apache:masterfrom
casaroli:fork-semantics-ostest-cleanup

Conversation

@casaroli

@casaroli casaroli commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Depends-On: apache/nuttx#19837

Summary

apache/nuttx#19562 separates fork() and vfork(), which NuttX implements as the same function. This gives each one a test of its own.

ostest's "vfork" test was never testing vfork(). It has the child write a global and the parent observe the write — 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 passed because both names resolved to the same sharing primitive.

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. 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 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().

Both 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. That matters more than usual right now, because ostest does not currently run to completion on any target: it aborts later in timedmutex_timeout_regression_test() at timedmutex.c:185, added by master eea8384 and unrelated to this PR. Running first is the only reason the fork tests run at all.

Each test gates on the one primitive it tests, ARCH_HAVE_VFORK and ARCH_HAVE_FORK respectively. There is no compatibility layer and no mapping between symbols. vfork.c no longer requires SCHED_WAITPID: the suspension lives in the kernel primitive now, so the test's core assertion holds without it and only the status check is conditional.

The other in-tree callers are audited for which primitive they actually meant:

  • interpreters/python's _posixsubprocess and netutils/libwebsockets' LWS_HAVE_WORKING_VFORK want the fork-then-exec path — ARCH_HAVE_VFORK.
  • python's os.fork() and libwebsockets' LWS_HAVE_FORK mean real fork() and stay on ARCH_HAVE_FORK, so they become absent rather than silently wrong.
  • testing/fs/fdsantest's vfork case follows ARCH_HAVE_VFORK.

interpreters/bas is deliberately left alone. Its SHELL and EDIT statements reach for vfork() under an ARCH_HAVE_FORK guard and want the same treatment, but checkpatch.sh checks the whole of any file a patch touches and bas_statement.c produces 1681 pre-existing findings against master, so a one-line change there fails CI on its own. The consequence is small: EXAMPLES_BAS_SHELL is EXPERIMENTAL and already depends on ARCH_HAVE_FORK, so it becomes unselectable rather than misbehaving.

Ordering

apache/nuttx#19562 merged on 2026-08-10, so the symbols each test keys on now exist.

This PR must merge after apache/nuttx#19837. vfork.c keys on ARCH_HAVE_VFORK now, so it is built again on lm3s6965-ek:qemu-protected, and that configuration has no room left: CI fails there with region uflash overflowed by 128 bytes. #19837 makes the configuration fit the part again. Its kernel and user images already overlapped by 1384 bytes, unseen, because the kernel was linked against a script that declares the whole flash instead of its own half.

fork_test() costs nothing on size-constrained configurations, because it is not built on them. It keys on ARCH_HAVE_FORK, which no architecture sets until up_addrenv_fork() lands for it. It is exercised by the per-architecture PRs that follow, which are what turn ARCH_HAVE_FORK back on.

Until this PR merges, ostest has no fork test. That is the deliberate cost of carrying no compatibility layer.

Testing

Host: macOS 15 (Darwin 25.5.0) on Apple Silicon. QEMU 11.0.3, xPack riscv-none-elf-gcc 14.2.0-3, Arm GNU arm-none-eabi-gcc/aarch64-none-elf-gcc 14.2.rel1, xtensa-esp32s3-elf-gcc 12.2.0.

../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 installed.

vfork_test() passes on raspberrypi-pico-2:nsh (real RP2350), qemu-armv7a:nsh, qemu-armv8a:nsh, rv-virt:nsh64 and sim:nsh, built against apache/nuttx#19562:

user_main: vfork() test
vfork_test: Started
vfork_test: Child 5 ran and exited before the parent resumed

esp32s3-devkit:ostest on real ESP32-S3 hardware builds and boots clean with neither test compiled in — Xtensa selects neither ARCH_HAVE_VFORK nor ARCH_HAVE_FORK and has no fork entry point, so it is the "architecture this does not touch" case.

fork_test() is not exercised yet by construction: no architecture selects ARCH_HAVE_FORK until a per-architecture PR implements up_addrenv_fork().

@casaroli
casaroli force-pushed the fork-semantics-ostest-cleanup branch from d3b7722 to bbc21fe Compare August 2, 2026 10:35
@casaroli casaroli changed the title testing/ostest: drop the pre-split fork() fallbacks testing/ostest: split the fork test into task_fork, vfork and fork Aug 2, 2026
acassis
acassis previously approved these changes Aug 2, 2026
Comment thread interpreters/python/Makefile
Comment thread netutils/libwebsockets/lws_config_private.h
Comment thread testing/fs/fdsantest/fdsantest_simple.c
@casaroli
casaroli force-pushed the fork-semantics-ostest-cleanup branch 3 times, most recently from 1f61625 to ae340d5 Compare August 8, 2026 07:48
@github-actions github-actions Bot added Size: M and removed Size: L labels Aug 8, 2026
@casaroli casaroli changed the title testing/ostest: split the fork test into task_fork, vfork and fork testing/ostest: Split the fork test into vfork and fork. Aug 8, 2026
@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx-apps/actions/runs/31246994111

@casaroli
casaroli force-pushed the fork-semantics-ostest-cleanup branch from ae340d5 to 2a43c6d Compare August 10, 2026 11:46
@casaroli
casaroli marked this pull request as ready for review August 10, 2026 11:53
@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx-apps/actions/runs/31384974151

@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@casaroli the change run out of flash space:

====================================================================================
Configuration/Tool: lm3s6965-ek/qemu-protected,CONFIG_ARM_TOOLCHAIN_GNU_EABI
2026-08-10 14:59:03
------------------------------------------------------------------------------------
  Cleaning...
  Configuring...
  Disabling CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Enabling CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Building NuttX...
arm-none-eabi-ld: nuttx_user.elf section `.data' will not fit in region `uflash'
arm-none-eabi-ld: region `uflash' overflowed by 128 bytes
make[1]: *** [Makefile:61: nuttx_user.elf] Error 1
make[1]: Target 'all' not remade because of errors.
make: *** [tools/Unix.mk:561: nuttx] Error 2
make: Target 'all' not remade because of errors.
/github/workspace/sources/nuttx/tools/testbuild.sh: line 397: /github/workspace/sources/nuttx/../nuttx/nuttx.manifest: No such file or directory
  [1/1] Normalize lm3s6965-ek/qemu-protected
/github/workspace/sources/nuttx /github/workspace/sources/nuttx
On branch master
Your branch is ahead of 'origin/master' by 6 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
/github/workspace/sources/nuttx
/github/workspace/sources/apps /github/workspace/sources/nuttx
HEAD detached at pull/3685/merge
nothing to commit, working tree clean
/github/workspace/sources/nuttx
Wait 29 seconds (60 backoff)
Build Attempt 2 of 4
====================================================================================
Configuration/Tool: lm3s6965-ek/qemu-protected,CONFIG_ARM_TOOLCHAIN_GNU_EABI
2026-08-10 15:00:52
------------------------------------------------------------------------------------
  Cleaning...
  Configuring...
  Disabling CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Enabling CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Building NuttX...
arm-none-eabi-ld: nuttx_user.elf section `.data' will not fit in region `uflash'
arm-none-eabi-ld: region `uflash' overflowed by 128 bytes
make[1]: *** [Makefile:61: nuttx_user.elf] Error 1
make[1]: Target 'all' not remade because of errors.
make: *** [tools/Unix.mk:561: nuttx] Error 2
make: Target 'all' not remade because of errors.
/github/workspace/sources/nuttx/tools/testbuild.sh: line 397: /github/workspace/sources/nuttx/../nuttx/nuttx.manifest: No such file or directory
  [1/1] Normalize lm3s6965-ek/qemu-protected
/github/workspace/sources/nuttx /github/workspace/sources/nuttx
On branch master
Your branch is ahead of 'origin/master' by 6 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
/github/workspace/sources/nuttx
/github/workspace/sources/apps /github/workspace/sources/nuttx
HEAD detached at pull/3685/merge
nothing to commit, working tree clean
/github/workspace/sources/nuttx
Wait 7 seconds (120 backoff)
Build Attempt 3 of 4
====================================================================================
Configuration/Tool: lm3s6965-ek/qemu-protected,CONFIG_ARM_TOOLCHAIN_GNU_EABI
2026-08-10 15:01:33
------------------------------------------------------------------------------------
  Cleaning...
  Configuring...
  Disabling CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Enabling CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Building NuttX...
arm-none-eabi-ld: nuttx_user.elf section `.data' will not fit in region `uflash'
arm-none-eabi-ld: region `uflash' overflowed by 128 bytes
make[1]: *** [Makefile:61: nuttx_user.elf] Error 1
make[1]: Target 'all' not remade because of errors.
make: *** [tools/Unix.mk:561: nuttx] Error 2
make: Target 'all' not remade because of errors.
/github/workspace/sources/nuttx/tools/testbuild.sh: line 397: /github/workspace/sources/nuttx/../nuttx/nuttx.manifest: No such file or directory
  [1/1] Normalize lm3s6965-ek/qemu-protected
/github/workspace/sources/nuttx /github/workspace/sources/nuttx
On branch master
Your branch is ahead of 'origin/master' by 6 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
/github/workspace/sources/nuttx
/github/workspace/sources/apps /github/workspace/sources/nuttx
HEAD detached at pull/3685/merge
nothing to commit, working tree clean
/github/workspace/sources/nuttx
Wait 105 seconds (240 backoff)
Build Attempt 4 of 4
====================================================================================
Configuration/Tool: lm3s6965-ek/qemu-protected,CONFIG_ARM_TOOLCHAIN_GNU_EABI
2026-08-10 15:03:53
------------------------------------------------------------------------------------
  Cleaning...
  Configuring...
  Disabling CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Enabling CONFIG_ARM_TOOLCHAIN_GNU_EABI
  Building NuttX...
arm-none-eabi-ld: nuttx_user.elf section `.data' will not fit in region `uflash'
arm-none-eabi-ld: region `uflash' overflowed by 128 bytes
make[1]: *** [Makefile:61: nuttx_user.elf] Error 1
make[1]: Target 'all' not remade because of errors.
make: *** [tools/Unix.mk:561: nuttx] Error 2
make: Target 'all' not remade because of errors.
/github/workspace/sources/nuttx/tools/testbuild.sh: line 397: /github/workspace/sources/nuttx/../nuttx/nuttx.manifest: No such file or directory
  [1/1] Normalize lm3s6965-ek/qemu-protected
/github/workspace/sources/nuttx /github/workspace/sources/nuttx
On branch master
Your branch is ahead of 'origin/master' by 6 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
/github/workspace/sources/nuttx
/github/workspace/sources/apps /github/workspace/sources/nuttx
HEAD detached at pull/3685/merge
nothing to commit, working tree clean
/github/workspace/sources/nuttx
Build Attempt 1 of 1

we need optimize either the implementation or link script/defconfig

@casaroli

Copy link
Copy Markdown
Contributor Author

@xiaoxiang781216 I made a PR that will temporarily fix the issue by disabling some unused drivers in that config, apache/nuttx#19837, however this will bite us again, and the proper fix in my opinion is to stop building protected build for this board as it does not have enough flash for it and find another armv7m build with enough flash and qemu support (spoiler: apache/nuttx#19765 has 1MB of flash) and test protected mode there instead.

In fact, I think we should model real powerful boards in Renode, and test what we can there, instead of qemu. Say the word and I can add some renode configs to our CI.

Thank you for your time and patience in my latest PRs. 🙏 ❤️

@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx-apps/actions/runs/31723719540

ostest's "vfork" test was never testing vfork().  It has the child write a
global and the parent observe the write -- 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 passed because
NuttX implemented fork() and vfork() as the same sharing primitive, which
apache/nuttx#19562 separates.

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.  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 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().

Both 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.

Each test gates on the one primitive it tests, ARCH_HAVE_VFORK and
ARCH_HAVE_FORK respectively.  There is no compatibility layer and no mapping
between symbols.  vfork.c no longer requires SCHED_WAITPID:  the suspension is
in the kernel primitive now, so the test's core assertion holds without it and
only the status check is conditional.

The simulator is the one exception.  It selects ARCH_HAVE_VFORK, but ostest
takes the sim down as soon as the test runs there, so the call keeps the
!ARCH_SIM guard that apps ee76427 put on the old test in 2024.  The old gate
hid this:  ARCH_HAVE_FORK is not set on the sim, so the test was not built
there at all.

The other in-tree callers are audited for which primitive they actually meant:

* interpreters/python's _posixsubprocess and netutils/libwebsockets'
  LWS_HAVE_WORKING_VFORK want the fork-then-exec path -- ARCH_HAVE_VFORK.
* python's os.fork() and libwebsockets' LWS_HAVE_FORK mean real fork() and stay
  on ARCH_HAVE_FORK, so they become *absent* rather than silently wrong.
* testing/fs/fdsantest's vfork case follows ARCH_HAVE_VFORK.

interpreters/bas is deliberately left alone.  Its SHELL and EDIT statements
reach for vfork() under an ARCH_HAVE_FORK guard and want the same treatment,
but checkpatch.sh checks the whole of any file a patch touches and
bas_statement.c produces 1681 pre-existing findings against master, so a
one-line change there fails CI on its own.  The consequence is small:
EXAMPLES_BAS_SHELL is EXPERIMENTAL and already depends on ARCH_HAVE_FORK, so it
becomes unselectable rather than misbehaving.

Assisted-by: Claude Code:claude-opus-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@cederom

cederom commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

@casaroli: (..) In fact, I think we should model real powerful boards in Renode, and test what we can there, instead of qemu. Say the word and I can add some renode configs to our CI. (..)

That sounds nice @casaroli but out CI is already overloaded and frequently over assigned quotas so we cannot stretch it anymore with yet another framework. But if you want prepare something like that on a separate account/repo so it could be replicated and also launched on local workstations that could be interesting :-)

@casaroli

casaroli commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

@casaroli: (..) In fact, I think we should model real powerful boards in Renode, and test what we can there, instead of qemu. Say the word and I can add some renode configs to our CI. (..)

That sounds nice @casaroli but out CI is already overloaded and frequently over assigned quotas so we cannot stretch it anymore with yet another framework. But if you want prepare something like that on a separate account/repo so it could be replicated and also launched on local workstations that could be interesting :-)

I believe the best option is to partition CI into more granular checks. We already split by arch, but I think we can go further and split by config: compute the blast radius of a change and rebuild/test only the configs it actually affects. It is a lot of engineering, but I believe it is worth it, since it could cut GitHub Actions consumption considerably. I am not sure what the ultimate benefit would be, but it is probably worth measuring.

I am still not sure whether we can use ccache: that could be a short-term improvement. The alternative is fixing the build system so it can reuse artifacts from previous builds instead of requiring a distclean every time.

I think all of this is possible, and all of it is worth a measurement.

IMHO what we cannot afford is buying back a few KB every time this platform goes over the limit - for a platform nobody uses, kept only because it is our single config that we can test under QEMU. So either we trim it properly (i.e. remove networking and telnet), or we drop it entirely: protected build is not feasible on that tiva board.

WDYT?

@cederom

cederom commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Yeah, we just need to reduce complexity, and usage. Thus before we change anything we need to have measurements and approvals from @lupyuen and @simbit18 not otherwise :-)

Also what would be nice to have is to trigger next steps even if checkpatch/codespell fails, sometimes this comes from existing code camel cases or nomenclature and we cannot change that :-P

@github-actions

Copy link
Copy Markdown

🔗 Cross-repo PR dependencies

The read-only Build run reported the following dependent PR(s) and fetched head SHA(s):

CI run: https://github.com/apache/nuttx-apps/actions/runs/31747547407

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.

4 participants