Skip to content

Commit 7aac6c5

Browse files
committed
testing/ostest: split the fork test into task_fork, vfork and fork
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>
1 parent 636047a commit 7aac6c5

16 files changed

Lines changed: 589 additions & 33 deletions

File tree

interpreters/python/Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ ifeq ($(CONFIG_ARCH_HAVE_FORK),y)
114114
else
115115
@echo "export ac_cv_func_fork=\"no\"" >> $@
116116
endif
117+
ifneq ($(CONFIG_ARCH_HAVE_VFORK)$(CONFIG_ARCH_HAVE_FORK),)
118+
@echo "export ac_cv_func_vfork=\"yes\"" >> $@
119+
else
120+
@echo "export ac_cv_func_vfork=\"no\"" >> $@
121+
endif
117122
ifeq ($(CONFIG_SYSTEM_SYSTEM),y)
118123
@echo "export ac_cv_func_system=\"yes\"" >> $@
119124
else
@@ -135,7 +140,10 @@ endif
135140

136141
$(SETUP_LOCAL):
137142
$(Q) ( cp $(SETUP_LOCAL).in $(SETUP_LOCAL))
138-
ifneq ($(CONFIG_ARCH_HAVE_FORK),y)
143+
# _posixsubprocess is the fork-then-exec path, so vfork() is enough for it;
144+
# os.fork() itself needs a real fork() and is governed by ac_cv_func_fork
145+
# above.
146+
ifeq ($(CONFIG_ARCH_HAVE_FORK)$(CONFIG_ARCH_HAVE_VFORK),)
139147
@echo "_posixsubprocess" >> $@
140148
endif
141149
ifneq ($(CONFIG_LIBC_DLFCN),y)

netutils/libwebsockets/lws_config_private.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@
4343
/* #undef USE_CYASSL */
4444

4545
/* Define to 1 if you have the `fork' function. */
46+
47+
#ifdef CONFIG_ARCH_HAVE_FORK
4648
#define LWS_HAVE_FORK
49+
#endif
4750

4851
#ifndef CONFIG_DISABLE_ENVIRON
4952
/* Define to 1 if you have the `getenv' function. */
@@ -111,10 +114,14 @@
111114
/* #undef LWS_HAVE_VFORK_H */
112115

113116
/* Define to 1 if `fork' works. */
117+
#ifdef CONFIG_ARCH_HAVE_FORK
114118
#define LWS_HAVE_WORKING_FORK
119+
#endif
115120

116121
/* Define to 1 if `vfork' works. */
122+
#if defined(CONFIG_ARCH_HAVE_VFORK) || defined(CONFIG_ARCH_HAVE_FORK)
117123
#define LWS_HAVE_WORKING_VFORK
124+
#endif
118125

119126
/* Define to 1 if execvpe() exists */
120127
#define LWS_HAVE_EXECVPE

system/libuv/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ if(CONFIG_LIBUV)
171171
${LIBUV_TEST_DIR}/run-tests.c ${LIBUV_TEST_DIR}/runner.c
172172
${LIBUV_TEST_DIR}/runner-unix.c ${LIBUV_TEST_DIR}/echo-server.c)
173173
file(GLOB TEST_CSRCS ${LIBUV_TEST_DIR}/test-*.c)
174+
175+
# See system/libuv/Makefile.
176+
177+
list(REMOVE_ITEM TEST_CSRCS ${LIBUV_TEST_DIR}/test-fork.c
178+
${LIBUV_TEST_DIR}/test-pipe-close-stdout-read-stdin.c)
174179
list(APPEND LIBUV_UTILS_TEST_SRCS ${TEST_CSRCS})
175180
nuttx_add_application(
176181
NAME

system/libuv/Makefile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,15 @@ CSRCS += runner.c
144144
CSRCS += runner-unix.c
145145
CSRCS += echo-server.c
146146

147-
CSRCS += $(wildcard libuv/test/test-*.c)
147+
# test-fork.c and test-pipe-close-stdout-read-stdin.c call fork(). Every
148+
# test they define is already excluded from the task list on NuttX by
149+
# 0001-libuv-port-for-nuttx.patch, so they are dead code here.
150+
151+
LIBUV_TEST_CSRCS = $(wildcard libuv/test/test-*.c)
152+
LIBUV_TEST_CSRCS := $(filter-out libuv/test/test-fork.c,$(LIBUV_TEST_CSRCS))
153+
LIBUV_TEST_CSRCS := $(filter-out libuv/test/test-pipe-close-stdout-read-stdin.c,$(LIBUV_TEST_CSRCS))
154+
155+
CSRCS += $(LIBUV_TEST_CSRCS)
148156
endif
149157

150158
ifneq ($(CONFIG_LIBUV_UTILS_BENCHMARK),)

testing/drivers/nand_sim/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
config TESTING_NAND_SIM
77
boolean "NAND Flash Simulator"
88
depends on MTD_NAND_RAM && ENABLE_ALL_SIGNALS
9+
depends on TASK_FORK || (ARCH_HAVE_FORK && !ARCH_HAVE_TASK_FORK)
910
default n
1011
---help---
1112
Enable the NAND Flash Simulator device.

testing/drivers/nand_sim/nand_sim_main.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525
****************************************************************************/
2626

2727
#include <nuttx/debug.h>
28+
#include <sched.h>
2829
#include <stdio.h>
30+
#include <unistd.h>
2931

3032
#include <nuttx/drivers/drivers.h>
3133
#include <nuttx/mtd/nand.h>
@@ -140,9 +142,16 @@ int main(int argc, FAR char *argv[])
140142
int ret;
141143
pid_t pid;
142144

143-
/* Daemon */
145+
/* task_fork() rather than fork(): this wants a clone that outlives the
146+
* caller and shares its memory. The fallback below covers a nuttx that
147+
* does not have task_fork() yet.
148+
*/
144149

145-
pid = fork();
150+
#ifndef CONFIG_TASK_FORK
151+
# define task_fork() fork()
152+
#endif
153+
154+
pid = task_fork();
146155

147156
if (pid > 0)
148157
{

testing/fs/fdsantest/fdsantest_simple.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ static void test_case_overflow(void **state)
9696
assert_int_equal(open_count, close_count);
9797
}
9898

99+
#if defined(CONFIG_ARCH_HAVE_VFORK) || defined(CONFIG_ARCH_HAVE_FORK)
99100
static void test_case_vfork(void **state)
100101
{
101102
int fd = open("/dev/null", O_RDONLY);
@@ -112,6 +113,7 @@ static void test_case_vfork(void **state)
112113

113114
android_fdsan_close_with_tag(fd, 0xbadc0de);
114115
}
116+
#endif
115117

116118
/****************************************************************************
117119
* Public Functions
@@ -129,7 +131,9 @@ int main(int argc, FAR char *argv[])
129131
cmocka_unit_test(test_case_unowned_tagged_close),
130132
cmocka_unit_test(test_case_owned_tagged_close),
131133
cmocka_unit_test(test_case_overflow),
134+
#if defined(CONFIG_ARCH_HAVE_VFORK) || defined(CONFIG_ARCH_HAVE_FORK)
132135
cmocka_unit_test(test_case_vfork),
136+
#endif
133137
};
134138

135139
return cmocka_run_group_tests(tests, NULL, NULL);

testing/ltp/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ if(CONFIG_TESTING_LTP)
8686
list(APPEND BLACKWORDS "pthread_spin_init" "pthread_spin_destroy"
8787
"pthread_spin_trylock")
8888
endif()
89+
90+
# See testing/ltp/Makefile.
91+
92+
if(NOT CONFIG_ARCH_HAVE_FORK AND NOT CONFIG_FORK_IS_TASK_FORK)
93+
list(APPEND BLACKWORDS "[^v_]fork(")
94+
endif()
8995
list(
9096
APPEND
9197
BLACKWORDS

testing/ltp/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ BLACKWORDS += "pthread_spin_destroy"
4444
BLACKWORDS += "pthread_spin_trylock"
4545
endif
4646

47+
# Where NuttX does not declare fork(), a test that calls it cannot be built.
48+
# The pattern spares vfork() and task_fork(), which remain available.
49+
50+
ifeq ($(CONFIG_ARCH_HAVE_FORK)$(CONFIG_FORK_IS_TASK_FORK),)
51+
BLACKWORDS += "[^v_]fork("
52+
endif
53+
4754
BLACKWORDS += "CHILD_MAX"
4855
BLACKWORDS += "setpgid("
4956
BLACKWORDS += "PTHREAD_SCOPE_PROCESS"

testing/ostest/CMakeLists.txt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,20 @@ if(CONFIG_TESTING_OSTEST)
143143
endif()
144144
endif()
145145

146-
if(CONFIG_ARCH_HAVE_FORK)
147-
if(CONFIG_SCHED_WAITPID)
148-
list(APPEND SRCS vfork.c)
149-
endif()
146+
# See testing/ostest/ostest.h for the one transitional exception.
147+
148+
if(CONFIG_TASK_FORK)
149+
list(APPEND SRCS task_fork.c)
150+
elseif(CONFIG_ARCH_HAVE_FORK AND NOT CONFIG_ARCH_HAVE_TASK_FORK)
151+
list(APPEND SRCS task_fork.c)
152+
endif()
153+
154+
if(CONFIG_ARCH_HAVE_VFORK)
155+
list(APPEND SRCS vfork.c)
156+
endif()
157+
158+
if(CONFIG_ARCH_HAVE_FORK AND CONFIG_ARCH_HAVE_VFORK)
159+
list(APPEND SRCS fork.c)
150160
endif()
151161

152162
if(CONFIG_ARCH_SETJMP_H)

0 commit comments

Comments
 (0)