]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/tst-clone2.c
0da9e54ba25259952c80e8e05da266cef1ca098f
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / tst-clone2.c
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.
4
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.
9
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.
14
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/>. */
18
19 #include <sched.h>
20 #include <signal.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <stddef.h>
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 #include <sys/syscall.h>
32
33 #include <stackinfo.h> /* For _STACK_GROWS_{UP,DOWN}. */
34
35 static int do_test (void);
36
37 #define TEST_FUNCTION do_test ()
38 #include <test-skeleton.c>
39
40 static int sig;
41 static int pipefd[2];
42
43 static int
44 f (void *a)
45 {
46 close (pipefd[0]);
47
48 pid_t ppid = getppid ();
49 pid_t pid = getpid ();
50 pid_t tid = syscall (__NR_gettid);
51
52 if (write (pipefd[1], &ppid, sizeof ppid) != sizeof (ppid))
53 FAIL_EXIT1 ("write ppid failed\n");
54 if (write (pipefd[1], &pid, sizeof pid) != sizeof (pid))
55 FAIL_EXIT1 ("write pid failed\n");
56 if (write (pipefd[1], &tid, sizeof tid) != sizeof (tid))
57 FAIL_EXIT1 ("write tid failed\n");
58
59 return 0;
60 }
61
62
63 static int
64 do_test (void)
65 {
66 sig = SIGRTMIN;
67 sigset_t ss;
68 sigemptyset (&ss);
69 sigaddset (&ss, sig);
70 if (sigprocmask (SIG_BLOCK, &ss, NULL) != 0)
71 FAIL_EXIT1 ("sigprocmask failed: %m");
72
73 if (pipe2 (pipefd, O_CLOEXEC))
74 FAIL_EXIT1 ("pipe failed: %m");
75
76 int clone_flags = 0;
77 #ifdef __ia64__
78 extern int __clone2 (int (*__fn) (void *__arg), void *__child_stack_base,
79 size_t __child_stack_size, int __flags,
80 void *__arg, ...);
81 char st[256 * 1024] __attribute__ ((aligned));
82 pid_t p = __clone2 (f, st, sizeof (st), clone_flags, 0);
83 #else
84 char st[128 * 1024] __attribute__ ((aligned));
85 #if _STACK_GROWS_DOWN
86 pid_t p = clone (f, st + sizeof (st), clone_flags, 0);
87 #elif _STACK_GROWS_UP
88 pid_t p = clone (f, st, clone_flags, 0);
89 #else
90 #error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
91 #endif
92 #endif
93
94 close (pipefd[1]);
95
96 if (p == -1)
97 FAIL_EXIT1("clone failed: %m");
98
99 pid_t ppid, pid, tid;
100 if (read (pipefd[0], &ppid, sizeof pid) != sizeof pid)
101 {
102 kill (p, SIGKILL);
103 FAIL_EXIT1 ("read ppid failed: %m");
104 }
105 if (read (pipefd[0], &pid, sizeof pid) != sizeof pid)
106 {
107 kill (p, SIGKILL);
108 FAIL_EXIT1 ("read pid failed: %m");
109 }
110 if (read (pipefd[0], &tid, sizeof tid) != sizeof tid)
111 {
112 kill (p, SIGKILL);
113 FAIL_EXIT1 ("read tid failed: %m");
114 }
115
116 close (pipefd[0]);
117
118 int ret = 0;
119
120 pid_t own_pid = getpid ();
121 pid_t own_tid = syscall (__NR_gettid);
122
123 /* Some sanity checks for clone syscall: returned ppid should be current
124 pid and both returned tid/pid should be different from current one. */
125 if ((ppid != own_pid) || (pid == own_pid) || (tid == own_tid))
126 FAIL_RET ("ppid=%i pid=%i tid=%i | own_pid=%i own_tid=%i",
127 (int)ppid, (int)pid, (int)tid, (int)own_pid, (int)own_tid);
128
129 int e;
130 if (waitpid (p, &e, __WCLONE) != p)
131 {
132 kill (p, SIGKILL);
133 FAIL_EXIT1 ("waitpid failed");
134 }
135 if (!WIFEXITED (e))
136 {
137 if (WIFSIGNALED (e))
138 printf ("died from signal %s\n", strsignal (WTERMSIG (e)));
139 else
140 puts ("did not terminate correctly");
141 exit (EXIT_FAILURE);
142 }
143 if (WEXITSTATUS (e) != 0)
144 FAIL_EXIT1 ("exit code %d", WEXITSTATUS (e));
145
146 return ret;
147 }