|
Packit |
6c4009 |
/* Check if clone (CLONE_THREAD) does not call exit_group (BZ #21512)
|
|
Packit |
6c4009 |
Copyright (C) 2017-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <sched.h>
|
|
Packit |
6c4009 |
#include <signal.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
#include <sys/syscall.h>
|
|
Packit |
6c4009 |
#include <sys/wait.h>
|
|
Packit |
6c4009 |
#include <sys/types.h>
|
|
Packit |
6c4009 |
#include <linux/futex.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <stackinfo.h> /* For _STACK_GROWS_{UP,DOWN}. */
|
|
Packit |
6c4009 |
#include <support/check.h>
|
|
Packit Service |
a3b3f7 |
#include <stdatomic.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Test if clone call with CLONE_THREAD does not call exit_group. The 'f'
|
|
Packit |
6c4009 |
function returns '1', which will be used by clone thread to call the
|
|
Packit |
6c4009 |
'exit' syscall directly. If _exit is used instead, exit_group will be
|
|
Packit |
6c4009 |
used and thus the thread group will finish with return value of '1'
|
|
Packit |
6c4009 |
(where '2' from main thread is expected. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
f (void *a)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Futex wait for TID argument, similar to pthread_join internal
|
|
Packit |
6c4009 |
implementation. */
|
|
Packit Service |
a3b3f7 |
#define wait_tid(ctid_ptr, ctid_val) \
|
|
Packit Service |
a3b3f7 |
do { \
|
|
Packit Service |
a3b3f7 |
__typeof (*(ctid_ptr)) __tid; \
|
|
Packit Service |
a3b3f7 |
/* We need acquire MO here so that we synchronize with the \
|
|
Packit Service |
a3b3f7 |
kernel's store to 0 when the clone terminates. */ \
|
|
Packit Service |
a3b3f7 |
while ((__tid = atomic_load_explicit (ctid_ptr, \
|
|
Packit Service |
a3b3f7 |
memory_order_acquire)) != 0) \
|
|
Packit Service |
a3b3f7 |
futex_wait (ctid_ptr, ctid_val); \
|
|
Packit |
6c4009 |
} while (0)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static inline int
|
|
Packit |
6c4009 |
futex_wait (int *futexp, int val)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
return syscall (__NR_futex, futexp, FUTEX_WAIT, val);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
do_test (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char st[1024] __attribute__ ((aligned));
|
|
Packit |
6c4009 |
int clone_flags = CLONE_THREAD;
|
|
Packit |
6c4009 |
/* Minimum required flags to used along with CLONE_THREAD. */
|
|
Packit |
6c4009 |
clone_flags |= CLONE_VM | CLONE_SIGHAND;
|
|
Packit |
6c4009 |
/* We will used ctid to call on futex to wait for thread exit. */
|
|
Packit |
6c4009 |
clone_flags |= CLONE_CHILD_CLEARTID;
|
|
Packit Service |
a3b3f7 |
/* Initialize with a known value. ctid is set to zero by the kernel after the
|
|
Packit Service |
a3b3f7 |
cloned thread has exited. */
|
|
Packit Service |
a3b3f7 |
#define CTID_INIT_VAL 1
|
|
Packit Service |
a3b3f7 |
pid_t ctid = CTID_INIT_VAL;
|
|
Packit Service |
a3b3f7 |
pid_t tid;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifdef __ia64__
|
|
Packit |
6c4009 |
extern int __clone2 (int (*__fn) (void *__arg), void *__child_stack_base,
|
|
Packit |
6c4009 |
size_t __child_stack_size, int __flags,
|
|
Packit |
6c4009 |
void *__arg, ...);
|
|
Packit |
6c4009 |
tid = __clone2 (f, st, sizeof (st), clone_flags, NULL, /* ptid */ NULL,
|
|
Packit |
6c4009 |
/* tls */ NULL, &ctid);
|
|
Packit |
6c4009 |
#else
|
|
Packit |
6c4009 |
#if _STACK_GROWS_DOWN
|
|
Packit |
6c4009 |
tid = clone (f, st + sizeof (st), clone_flags, NULL, /* ptid */ NULL,
|
|
Packit |
6c4009 |
/* tls */ NULL, &ctid);
|
|
Packit |
6c4009 |
#elif _STACK_GROWS_UP
|
|
Packit |
6c4009 |
tid = clone (f, st, clone_flags, NULL, /* ptid */ NULL, /* tls */ NULL,
|
|
Packit |
6c4009 |
&ctid);
|
|
Packit |
6c4009 |
#else
|
|
Packit |
6c4009 |
#error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
if (tid == -1)
|
|
Packit |
6c4009 |
FAIL_EXIT1 ("clone failed: %m");
|
|
Packit |
6c4009 |
|
|
Packit Service |
a3b3f7 |
wait_tid (&ctid, CTID_INIT_VAL);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return 2;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define EXPECTED_STATUS 2
|
|
Packit |
6c4009 |
#include <support/test-driver.c>
|