Skip to content

Commit e32964b

Browse files
casaroliclaude
andcommitted
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: 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> Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 636047a commit e32964b

16 files changed

Lines changed: 581 additions & 32 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 ARCH_HAVE_TASK_FORK || ARCH_HAVE_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_ARCH_HAVE_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: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,19 @@ 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/Makefile for why each test also accepts the symbol that
147+
# stands in for it on a nuttx without the fork()/vfork() split.
148+
149+
if(CONFIG_ARCH_HAVE_TASK_FORK OR CONFIG_ARCH_HAVE_FORK)
150+
list(APPEND SRCS task_fork.c)
151+
endif()
152+
153+
if(CONFIG_ARCH_HAVE_VFORK OR (CONFIG_ARCH_HAVE_FORK AND CONFIG_SCHED_WAITPID))
154+
list(APPEND SRCS vfork.c)
155+
endif()
156+
157+
if(CONFIG_ARCH_HAVE_FORK AND CONFIG_ARCH_HAVE_VFORK)
158+
list(APPEND SRCS fork.c)
150159
endif()
151160

152161
if(CONFIG_ARCH_SETJMP_H)

0 commit comments

Comments
 (0)