mirror of
https://github.com/apache/nuttx.git
synced 2026-08-01 20:28:58 +00:00
NuttX implemented fork() and vfork() as the same function. Both were libc
wrappers around a single up_fork() syscall; vfork() differed only by a
trailing waitpid(). Underneath, the child joined the parent's address
environment -- the same addrenv_join() that pthread_create() uses -- and got
a private copy of the stack. So the child shared .data, .bss and the heap
with its parent and ran concurrently with it.
That is not fork(). It is vfork()-with-a-private-stack under fork()'s name,
and the history says so: today's fork() is NuttX's old vfork(), renamed in
c33d1c9c97 (2023) without any change of behaviour. The failure was silent --
a program written against POSIX fork() compiled, ran, and had its child's
writes land in the parent's variables.
Separate them into three primitives, chosen by which function the caller
called rather than by what the hardware happens to be:
fork() child gets its own copy of the parent's memory at the same
virtual addresses; runs concurrently. Only where an address
environment can be duplicated -- elsewhere it is not declared
at all, so calling it is a build error naming the function.
vfork() child shares the parent's memory; parent suspended until the
child _exit()s or exec()s. Implementable everywhere.
task_fork() the historical behaviour under an honest name: shares memory,
private stack copy, both running. Non-POSIX, in sched.h.
Below libc there are now three syscalls -- up_task_fork(), up_vfork() and
up_fork(). The per-arch register snapshot is common to all three; each
architecture's entry points share one sequence and differ only in a
FORK_TYPE_* selector (include/nuttx/fork.h) handed to nxtask_setup_fork(),
which is the single place the memory semantics are decided.
The vfork() parent suspension moves out of libc into nxtask_start_vfork(),
released from nxsched_release_tcb(). Two things follow: the parent is
resumed at exec(), since exec_swap() has already handed the child's pid to
the loaded program by the time the vfork stub exits, and vfork() no longer
depends on CONFIG_SCHED_WAITPID.
Releasing there requires one fix in nxtask_exit(). It raises rtcb->lockcount
directly rather than through sched_lock() while it tears the TCB down, so the
nxsem_post() that wakes the vfork() parent queues it on g_pendingtasks -- and
the matching raw lockcount-- does not merge that list the way sched_unlock()
would, leaving the parent stranded with nothing left to move it off. A
nxsched_merge_pending() after the decrement publishes it. The call is a
no-op while pre-emption is still disabled, and up_exit() re-reads this_task()
afterwards, so a change of the ready-to-run head is honoured. Without it
vfork() deadlocks on any configuration where no other task happens to call
sched_unlock() afterwards -- rv-virt:nsh64 and rv-virt:pnsh64, for instance,
where NSH is blocked in waitpid() holding the lock.
fork() is built on a new addrenv_fork(), backed by an up_addrenv_fork() hook
that duplicates an address environment into freshly allocated pages mapped at
the same virtual addresses -- unlike up_addrenv_clone(), which copies only
the representation and leaves both pointing at the same page tables. The
child then adopts the parent's stack geometry rather than being given a
relocated copy: a pointer to a stack local taken before fork() must name the
same object in the child that it named in the parent, and the parent's stack
is already in the duplicate, with its contents, at the parent's address.
No architecture implements up_addrenv_fork() yet, so this commit leaves
fork() unavailable everywhere. That is the intended state. It withdraws
fork() from ARCH_ARM, flat ARCH_ARM64, ARCH_RISCV, ARCH_SIM and ARCH_X86_64,
where until now it named the sharing primitive; per-architecture patches
restore it, with POSIX semantics, as up_addrenv_fork() lands. Nothing is
lost in the meantime: task_fork() is that same sharing primitive under its
own name, and CONFIG_FORK_IS_TASK_FORK (default n) aliases fork() back to it
for legacy code, on exactly the configurations that had fork() before.
Kconfig: ARCH_HAVE_TASK_FORK and ARCH_HAVE_VFORK inherit ARCH_HAVE_FORK's
select lines, conditions included, so no configuration gains machinery;
ARCH_HAVE_FORK is redefined to mean "can provide POSIX fork() semantics" and
derives from the new ARCH_HAVE_ADDRENV_FORK.
There is one deliberate departure from "verbatim". ARCH_ARM selected the
fork family unconditionally, BUILD_KERNEL included, and that has never
worked: on a kernel build the architecture's fork entry point sees the
kernel's return address and stack pointer rather than the caller's, so the
child resumes at a kernel address. On qemu-armv7a:knsh master faults in
ostest's task_fork case with "Child did not run" and then a data abort;
without the condition this change faults the same way through vfork().
ARCH_ARM64 and ARCH_X86_64 already carried "if !BUILD_KERNEL" for exactly
this reason -- ARM was the outlier. Conditioning it turns a runtime fault
into an honest absence, which is the whole point of the change; arch/arm
takes the condition off again in the patch that adds its saved-syscall-frame
path. Only the MMU-capable ARM ports are affected, since Cortex-M cannot
build BUILD_KERNEL at all.
Also fixes two latent syntax errors found on the way: a missing comma in
riscv_fork.c and mips_fork.c, both in *_FRAMEPOINTER && !SAVE_GP branches
that are never compiled today.
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
30 KiB
30 KiB
| 1 | __assert | assert.h | void | FAR const char * | int | FAR const char * | |
|---|---|---|---|---|---|---|---|
| 2 | __cxa_atexit | stdlib.h | int | FAR CODE void (*)(FAR void *)|FAR void * | FAR void * | FAR void * | |
| 3 | aio_suspend | aio.h | defined(CONFIG_FS_AIO) | int | FAR const struct aiocb * const []|FAR const struct aiocb * const * | int | FAR const struct timespec * |
| 4 | asprintf | stdio.h | int | FAR char ** | FAR const IPTR char * | ... | |
| 5 | crc32part | nuttx/crc32.h | uint32_t | FAR const uint8_t * | size_t | uint32_t | |
| 6 | dq_addafter | nuttx/queue.h | void | FAR dq_entry_t * | FAR dq_entry_t * | FAR dq_queue_t * | |
| 7 | fgets | stdio.h | defined(CONFIG_FILE_STREAM) | FAR char * | FAR char * | int | FAR FILE * |
| 8 | fnmatch | fnmatch.h | int | FAR const char * | FAR const char * | int | |
| 9 | fprintf | stdio.h | defined(CONFIG_FILE_STREAM) | int | FAR FILE * | FAR const IPTR char * | ... |
| 10 | fscanf | stdio.h | int | FAR FILE * | FAR const char * | ... | |
| 11 | fseek | stdio.h | defined(CONFIG_FILE_STREAM) | int | FAR FILE * | long int | int |
| 12 | getopt | unistd.h | int | int | FAR char * const []|FAR char * const * | FAR const char * | |
| 13 | getrandom | sys/random.h | !defined(CONFIG_BUILD_KERNEL) | ssize_t | FAR void * | size_t | unsigned int |
| 14 | inet_pton | arpa/inet.h | int | int | FAR const char * | FAR void * | |
| 15 | lib_dumpbuffer | debug.h | void | FAR const char * | FAR const uint8_t * | unsigned int | |
| 16 | mbrlen | wchar.h | size_t | FAR const char * | size_t | FAR mbstate_t * | |
| 17 | mbstowcs | stdlib.h | size_t | FAR wchar_t * | FAR const char * | size_t | |
| 18 | mbtowc | stdlib.h | int | FAR wchar_t * | FAR const char * | size_t | |
| 19 | memchr | string.h | FAR void * | FAR const void * | int | size_t | |
| 20 | memcmp | string.h | int | FAR const void * | FAR const void * | size_t | |
| 21 | memcpy | string.h | FAR void * | FAR void * | FAR const void * | size_t | |
| 22 | memmove | string.h | FAR void * | FAR void * | FAR const void * | size_t | |
| 23 | memset | string.h | FAR void * | FAR void * | int | size_t | |
| 24 | posix_fallocate | fcntl.h | int | int | off_t | off_t | |
| 25 | posix_memalign | stdlib.h | int | FAR void ** | size_t | size_t | |
| 26 | pthread_barrier_init | pthread.h | !defined(CONFIG_DISABLE_PTHREAD) | int | FAR pthread_barrier_t * | FAR const pthread_barrierattr_t * | unsigned int |
| 27 | pthread_cond_timedwait | pthread.h | !defined(CONFIG_DISABLE_PTHREAD) | int | FAR pthread_cond_t * | FAR pthread_mutex_t * | FAR const struct timespec * |
| 28 | pthread_getname_np | pthread.h | !defined(CONFIG_DISABLE_PTHREAD) | int | pthread_t | char * | size_t |
| 29 | pthread_mutex_setprioceiling | pthread.h | !defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_PRIORITY_PROTECT) | int | FAR pthread_mutex_t * | int | FAR int * |
| 30 | readdir_r | dirent.h | int | FAR DIR * | FAR struct dirent * | FAR struct dirent ** | |
| 31 | readv | sys/uio.h | ssize_t | int | FAR const struct iovec * | int | |
| 32 | sem_init | semaphore.h | int | FAR sem_t * | int | unsigned int | |
| 33 | setpriority | sys/resource.h | int | int | id_t | int | |
| 34 | sprintf | stdio.h | int | FAR char * | FAR const IPTR char * | ... | |
| 35 | sprintf | stdio.h | int | FAR char * | FAR const IPTR char * | ... | |
| 36 | sq_addafter | nuttx/queue.h | void | FAR sq_entry_t * | FAR sq_entry_t * | FAR sq_queue_t * | |
| 37 | sscanf | stdio.h | int | FAR const IPTR char * | FAR const char * | ... | |
| 38 | sscanf | stdio.h | int | FAR const char * | FAR const char * | ... | |
| 39 | strerror_r | string.h | int | int | FAR char * | size_t | |
| 40 | strlcpy | string.h | size_t | FAR char * | FAR const char * | size_t | |
| 41 | strncasecmp | strings.h | int | FAR const char * | FAR const char * | size_t | |
| 42 | strncat | string.h | FAR char * | FAR char * | FAR const char * | size_t | |
| 43 | strncmp | string.h | int | FAR const char * | FAR const char * | size_t | |
| 44 | strncpy | string.h | FAR char * | FAR char * | FAR const char * | size_t | |
| 45 | strtoimax | inttypes.h | intmax_t | FAR const char * | FAR char ** | int | |
| 46 | strtok_r | string.h | FAR char * | FAR char * | FAR const char * | FAR char ** | |
| 47 | strtol | stdlib.h | long | FAR const char * | FAR char ** | int | |
| 48 | strtoll | stdlib.h | long long | FAR const char * | FAR char ** | int | |
| 49 | strtoul | stdlib.h | unsigned long | FAR const char * | FAR char ** | int | |
| 50 | strtoull | stdlib.h | unsigned long long int | FAR const char * | FAR char ** | int | |
| 51 | strtoull | stdlib.h | unsigned long long | FAR const char * | FAR char ** | int | |
| 52 | strtoumax | inttypes.h | uintmax_t | FAR const char * | FAR char ** | int | |
| 53 | strxfrm | string.h | defined(CONFIG_LIBC_LOCALE) | size_t | FAR char * | FAR const char * | size_t |
| 54 | swab | unistd.h | void | FAR const void * | FAR void * | ssize_t | |
| 55 | syslog | syslog.h | void | int | FAR const IPTR char * | ... | |
| 56 | tcsetattr | termios.h | int | int | int | FAR const struct termios * | |
| 57 | vasprintf | stdio.h | int | FAR char ** | FAR const IPTR char * | va_list | |
| 58 | vfprintf | stdio.h | defined(CONFIG_FILE_STREAM) | int | FAR FILE * | FAR const IPTR char * | va_list |
| 59 | vsprintf | stdio.h | int | FAR char * | FAR const IPTR char * | va_list | |
| 60 | vsscanf | stdio.h | int | FAR const char * | FAR const IPTR char * | va_list | |
| 61 | vsyslog | syslog.h | void | int | FAR const IPTR char * | va_list | |
| 62 | wcrtomb | wchar.h | size_t | FAR char * | wchar_t | FAR mbstate_t * | |
| 63 | wcslcat | wchar.h | size_t | FAR wchar_t * | FAR const wchar_t * | size_t | |
| 64 | wcsncat | wchar.h | FAR wchar_t * | FAR wchar_t * | FAR const wchar_t * | size_t | |
| 65 | wcsncmp | wchar.h | int | FAR const wchar_t * | FAR const wchar_t * | size_t | |
| 66 | wcsncpy | wchar.h | FAR wchar_t * | FAR wchar_t * | FAR const wchar_t * | size_t | |
| 67 | wcslcpy | wchar.h | size_t | FAR wchar_t * | FAR const wchar_t * | size_t | |
| 68 | wcstok | wchar.h | FAR wchar_t * | FAR wchar_t * | FAR const wchar_t * | FAR wchar_t ** | |
| 69 | wcstol | wchar.h | long int | FAR const wchar_t * | FAR wchar_t ** | int | |
| 70 | wcstoll | wchar.h | long long int | FAR const wchar_t * | FAR wchar_t ** | int | |
| 71 | wcstombs | stdlib.h | size_t | FAR char * | FAR const wchar_t * | size_t | |
| 72 | wcstoul | wchar.h | unsigned long int | FAR const wchar_t * | FAR wchar_t ** | int | |
| 73 | wcsxfrm | wchar.h | size_t | FAR wchar_t * | FAR const wchar_t * | size_t | |
| 74 | wmemchr | wchar.h | FAR wchar_t * | FAR const wchar_t * | wchar_t | size_t | |
| 75 | wmemcmp | wchar.h | int | FAR const wchar_t * | FAR const wchar_t * | size_t | |
| 76 | wmemcpy | wchar.h | FAR wchat_t * | FAR wchar_t * | FAR const wchar_t * | size_t | |
| 77 | wmemmove | wchar.h | FAR wchat_t * | FAR wchar_t * | FAR const wchar_t * | size_t | |
| 78 | wmemset | wchar.h | FAR wchat_t * | FAR wchar_t * | wchar_t | size_t | |
| 79 | writev | sys/uio.h | ssize_t | int | FAR const struct iovec * | int |