Skip to content
Permalink
Browse files

posix: New Linux posix_spawn{p} implementation

This patch implements a new posix_spawn{p} implementation for Linux.  The main
difference is it uses the clone syscall directly with CLONE_VM and CLONE_VFORK
flags and a direct allocated stack.  The new stack and start function solves
most the vfork limitation (possible parent clobber due stack spilling).  The
remaning issue are related to signal handling:

  1. That no signal handlers must run in child context, to avoid corrupt
     parent's state.
  2. Child must synchronize with parent to enforce stack deallocation and
     to possible return execv issues.

The first one is solved by blocking all signals in child, even NPTL-internal
ones (SIGCANCEL and SIGSETXID).  The second issue is done by a stack allocation
in parent and a synchronization with using a pipe or waitpid (in case or error).
The pipe has the advantage of allowing the child signal an exec error (checked
with new tst-spawn2 test).

There is an inherent race condition in pipe2 usage for architectures that do not
support the syscall directly.  In such cases the a pipe plus fctnl is used
instead and it may lead to file descriptor leak in parent (as decribed by fcntl
documentation).

The child process stack is allocate with a mmap with MAP_STACK flag using
default architecture stack size.  Although it is slower than use a stack buffer
from parent, it allows some slack for the compatibility code to run scripts
with no shebang (which may use a buffer with size depending of argument list
count).

Performance should be similar to the vfork default posix implementation and
way faster than fork path (vfork on mostly linux ports are basically
clone with CLONE_VM plus CLONE_VFORK).  The only difference is the syscalls
required for the stack allocation/deallocation.

It fixes BZ#10354, BZ#14750, and BZ#18433.

Tested on i386, x86_64, powerpc64le, and aarch64.

	[BZ #14750]
	[BZ #10354]
	[BZ #18433]
	* include/sched.h (__clone): Add hidden prototype.
	(__clone2): Likewise.
	* include/unistd.h (__dup): Likewise.
	* posix/Makefile (tests): Add tst-spawn2.
	* posix/tst-spawn2.c: New file.
	* sysdeps/posix/dup.c (__dup): Add hidden definition.
	* sysdeps/unix/sysv/linux/aarch64/clone.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/alpha/clone.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/arm/clone.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/hppa/clone.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/i386/clone.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/ia64/clone2.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/m68k/clone.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/microblaze/clone.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/mips/clone.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/nios2/clone.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S (__clone):
	Likewise.
	* sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S (__clone):
	Likewise.
	* sysdeps/unix/sysv/linux/s390/s390-32/clone.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/s390/s390-64/clone.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/sh/clone.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/sparc/sparc32/clone.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/sparc/sparc64/clone.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/tile/clone.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/x86_64/clone.S (__clone): Likewise.
	* sysdeps/unix/sysv/linux/nptl-signals.h
	(____nptl_is_internal_signal): New function.
	* sysdeps/unix/sysv/linux/spawni.c: New file.
  • Loading branch information...
Adhemerval Zanella Adhemerval Zanella
Adhemerval Zanella authored and Adhemerval Zanella committed Jan 19, 2016
1 parent 1eb8930 commit 9ff72da471a509a8c19791efe469f47fa6977410
@@ -1,5 +1,39 @@
2016-03-07 Adhemerval Zanella <adhemerval.zanella@linaro.org>

[BZ #14750]
[BZ #10354]
[BZ #18433]
* include/sched.h (__clone): Add hidden prototype.
(__clone2): Likewise.
* include/unistd.h (__dup): Likewise.
* posix/Makefile (tests): Add tst-spawn2.
* posix/tst-spawn2.c: New file.
* sysdeps/posix/dup.c (__dup): Add hidden definition.
* sysdeps/unix/sysv/linux/aarch64/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/alpha/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/arm/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/hppa/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/i386/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/ia64/clone2.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/m68k/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/microblaze/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/mips/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/nios2/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/powerpc/powerpc32/clone.S (__clone):
Likewise.
* sysdeps/unix/sysv/linux/powerpc/powerpc64/clone.S (__clone):
Likewise.
* sysdeps/unix/sysv/linux/s390/s390-32/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/s390/s390-64/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/sh/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/sparc/sparc32/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/sparc/sparc64/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/tile/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/x86_64/clone.S (__clone): Likewise.
* sysdeps/unix/sysv/linux/nptl-signals.h
(____nptl_is_internal_signal): New function.
* sysdeps/unix/sysv/linux/spawni.c: New file.

* posix/execvpe.c (__execvpe): Remove dynamic allocation.
* posix/Makefile (tests): Add tst-execvpe{1,2,3,4,5,6}.
* posix/tst-execvp1.c (do_test): Use a macro to call execvp.
@@ -19,7 +19,9 @@ extern int __sched_rr_get_interval (__pid_t __pid, struct timespec *__t);
/* These are Linux specific. */
extern int __clone (int (*__fn) (void *__arg), void *__child_stack,
int __flags, void *__arg, ...);
libc_hidden_proto (__clone)
extern int __clone2 (int (*__fn) (void *__arg), void *__child_stack_base,
size_t __child_stack_size, int __flags, void *__arg, ...);
libc_hidden_proto (__clone2)
#endif
#endif
@@ -81,6 +81,7 @@ char *__canonicalize_directory_name_internal (const char *__thisdir,
size_t __size) attribute_hidden;

extern int __dup (int __fd);
libc_hidden_proto (__dup)
extern int __dup2 (int __fd, int __fd2);
libc_hidden_proto (__dup2)
extern int __dup3 (int __fd, int __fd2, int flags);
@@ -94,7 +94,7 @@ tests := tstgetopt testfnm runtests runptests \
xtests := bug-ga2
ifeq (yes,$(build-shared))
test-srcs := globtest
tests += wordexp-test tst-exec tst-spawn
tests += wordexp-test tst-exec tst-spawn tst-spawn2
endif
tests-static = tst-exec-static tst-spawn-static
tests += $(tests-static)
@@ -0,0 +1,72 @@
/* Further tests for spawn in case of invalid binary paths.
Copyright (C) 2016 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */

#include <spawn.h>
#include <error.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>

#include <stdio.h>

int
posix_spawn_test (void)
{
/* Check if posix_spawn correctly returns an error and an invalid pid
by trying to spawn an invalid binary. */

const char *program = "/path/to/invalid/binary";
char * const args[] = { 0 };
pid_t pid = -1;

int ret = posix_spawn (&pid, program, 0, 0, args, environ);
if (ret != ENOENT)
error (EXIT_FAILURE, errno, "posix_spawn");

/* POSIX states the value returned on pid variable in case of an error
is not specified. GLIBC will update the value iff the child
execution is successful. */
if (pid != -1)
error (EXIT_FAILURE, errno, "posix_spawn returned pid != -1");

/* Check if no child is actually created. */
ret = waitpid (-1, NULL, 0);
if (ret != -1 || errno != ECHILD)
error (EXIT_FAILURE, errno, "waitpid");

/* Same as before, but with posix_spawnp. */
char *args2[] = { (char*) program, 0 };

ret = posix_spawnp (&pid, args2[0], 0, 0, args2, environ);
if (ret != ENOENT)
error (EXIT_FAILURE, errno, "posix_spawnp");

if (pid != -1)
error (EXIT_FAILURE, errno, "posix_spawnp returned pid != -1");

ret = waitpid (-1, NULL, 0);
if (ret != -1 || errno != ECHILD)
error (EXIT_FAILURE, errno, "waitpid");

return 0;
}

#define TEST_FUNCTION posix_spawn_test ()
#include "../test-skeleton.c"
@@ -26,5 +26,5 @@ __dup (int fd)
{
return fcntl (fd, F_DUPFD, 0);
}

libc_hidden_def (__dup)
weak_alias (__dup, dup)
@@ -93,4 +93,5 @@ thread_start:
cfi_endproc
.size thread_start, .-thread_start

libc_hidden_def (__clone)
weak_alias (__clone, clone)
@@ -137,4 +137,5 @@ thread_start:
cfi_endproc
.end thread_start

libc_hidden_def (__clone)
weak_alias (__clone, clone)
@@ -93,4 +93,5 @@ PSEUDO_END (__clone)

.fnend

libc_hidden_def (__clone)
weak_alias (__clone, clone)
@@ -175,4 +175,5 @@ ENTRY(__clone)

PSEUDO_END(__clone)

libc_hidden_def (__clone)
weak_alias (__clone, clone)
@@ -139,4 +139,5 @@ L(nomoregetpid):
cfi_startproc
PSEUDO_END (__clone)

libc_hidden_def (__clone)
weak_alias (__clone, clone)
@@ -96,6 +96,8 @@ ENTRY(__clone2)
ret /* Not reached. */
PSEUDO_END(__clone2)

libc_hidden_def (__clone2)

/* For now we leave __clone undefined. This is unlikely to be a */
/* problem, since at least the i386 __clone in glibc always failed */
/* with a 0 sp (eventhough the kernel explicitly handled it). */
@@ -127,4 +127,5 @@ donepid:
cfi_startproc
PSEUDO_END (__clone)

libc_hidden_def (__clone)
weak_alias (__clone, clone)
@@ -69,4 +69,5 @@ L(thread_start):
nop
PSEUDO_END(__clone)

libc_hidden_def (__clone)
weak_alias (__clone,clone)
@@ -166,4 +166,5 @@ L(gotpid):

END(__thread_start)

libc_hidden_def (__clone)
weak_alias (__clone, clone)
@@ -104,4 +104,5 @@ thread_start:

cfi_startproc
PSEUDO_END (__clone)
libc_hidden_def (__clone)
weak_alias (__clone, clone)
@@ -16,6 +16,8 @@
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */

#include <signal.h>

/* The signal used for asynchronous cancelation. */
#define SIGCANCEL __SIGRTMIN

@@ -29,5 +31,13 @@
/* Signal used to implement the setuid et.al. functions. */
#define SIGSETXID (__SIGRTMIN + 1)


/* Return is sig is used internally. */
static inline int
__nptl_is_internal_signal (int sig)
{
return (sig == SIGCANCEL) || (sig == SIGTIMER) || (sig == SIGSETXID);
}

/* Used to communicate with signal handler. */
extern struct xid_command *__xidcmd attribute_hidden;
@@ -108,4 +108,5 @@ L(badargs):
cfi_startproc
END (__clone)

libc_hidden_def (__clone)
weak_alias (__clone, clone)
@@ -126,4 +126,5 @@ L(parent):

END (__clone)

libc_hidden_def (__clone)
weak_alias (__clone, clone)
@@ -70,4 +70,6 @@ thread_start:
xc 0(4,%r15),0(%r15)
basr %r14,%r1 /* jump to fn */
DO_CALL (exit, 1)

libc_hidden_def (__clone)
weak_alias (__clone, clone)
@@ -73,4 +73,6 @@ thread_start:
xc 0(8,%r15),0(%r15)
basr %r14,%r1 /* jump to fn */
DO_CALL (exit, 1)

libc_hidden_def (__clone)
weak_alias (__clone, clone)
@@ -123,4 +123,5 @@ ENTRY(__clone)
.word TID - TLS_PRE_TCB_SIZE
PSEUDO_END (__clone)

libc_hidden_def (__clone)
weak_alias (__clone, clone)
@@ -100,4 +100,5 @@ __thread_start:

.size __thread_start, .-__thread_start

libc_hidden_def (__clone)
weak_alias (__clone, clone)
@@ -96,4 +96,5 @@ __thread_start:

.size __thread_start, .-__thread_start

libc_hidden_def (__clone)
weak_alias (__clone, clone)
Oops, something went wrong.

0 comments on commit 9ff72da

Please sign in to comment.
You can’t perform that action at this time.