1 /* Test if CLONE_VM does not change pthread pid/tid field (BZ #19957)
2 Copyright (C) 2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
29 #include <sys/types.h>
32 #include <tls.h> /* for THREAD_* macros. */
42 pid_t pid
= THREAD_GETMEM (THREAD_SELF
, pid
);
43 pid_t tid
= THREAD_GETMEM (THREAD_SELF
, tid
);
45 while (write (pipefd
[1], &pid
, sizeof pid
) < 0)
47 while (write (pipefd
[1], &tid
, sizeof tid
) < 0)
55 clone_test (int clone_flags
)
61 if (sigprocmask (SIG_BLOCK
, &ss
, NULL
) != 0)
63 printf ("sigprocmask failed: %m\n");
67 if (pipe2 (pipefd
, O_CLOEXEC
))
69 printf ("sigprocmask failed: %m\n");
73 pid_t ppid
= getpid ();
76 extern int __clone2 (int (*__fn
) (void *__arg
), void *__child_stack_base
,
77 size_t __child_stack_size
, int __flags
,
79 char st
[256 * 1024] __attribute__ ((aligned
));
80 pid_t p
= __clone2 (f
, st
, sizeof (st
), clone_flags
, 0);
82 char st
[128 * 1024] __attribute__ ((aligned
));
84 pid_t p
= clone (f
, st
+ sizeof (st
), clone_flags
, 0);
86 pid_t p
= clone (f
, st
, clone_flags
, 0);
88 #error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
95 printf ("clone failed: %m\n");
100 if (read (pipefd
[0], &pid
, sizeof pid
) != sizeof pid
)
102 printf ("read pid failed: %m\n");
106 if (read (pipefd
[0], &tid
, sizeof tid
) != sizeof tid
)
108 printf ("read pid failed: %m\n");
117 /* For CLONE_VM glibc clone implementation does not change the pthread
119 if ((clone_flags
& CLONE_VM
) == CLONE_VM
)
121 if ((ppid
!= pid
) || (ppid
!= tid
))
123 printf ("parent pid (%i) != received pid/tid (%i/%i)\n",
124 (int)ppid
, (int)pid
, (int)tid
);
128 /* For any other flag clone updates the new pthread pid and tid with
129 the clone return value. */
132 if ((p
!= pid
) || (p
!= tid
))
134 printf ("child pid (%i) != received pid/tid (%i/%i)\n",
135 (int)p
, (int)pid
, (int)tid
);
141 if (waitpid (p
, &e
, __WCLONE
) != p
)
143 puts ("waitpid failed");
150 printf ("died from signal %s\n", strsignal (WTERMSIG (e
)));
152 puts ("did not terminate correctly");
155 if (WEXITSTATUS (e
) != 0)
157 printf ("exit code %d\n", WEXITSTATUS (e
));
167 /* First, check that the clone implementation, without any flag, updates
168 the struct pthread to contain the new PID and TID. */
169 int ret
= clone_test (0);
170 /* Second, check that with CLONE_VM the struct pthread PID and TID fields
171 remain unmodified after the clone. Any modifications would cause problem
172 for the parent as described in bug 19957. */
173 ret
+= clone_test (CLONE_VM
);
177 #define TEST_FUNCTION do_test ()
178 #include "../test-skeleton.c"