1 | /* Test if CLONE_VM does not change pthread pid/tid field (BZ #19957) |
2 | Copyright (C) 2021-2024 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 | <https://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 <errno.h> |
30 | #include <sys/types.h> |
31 | #include <sys/wait.h> |
32 | #include <sys/syscall.h> |
33 | #include <clone_internal.h> |
34 | #include <support/xunistd.h> |
35 | #include <support/check.h> |
36 | |
37 | static int sig; |
38 | static int pipefd[2]; |
39 | |
40 | static int |
41 | f (void *a) |
42 | { |
43 | close (fd: pipefd[0]); |
44 | |
45 | pid_t ppid = getppid (); |
46 | pid_t pid = getpid (); |
47 | pid_t tid = gettid (); |
48 | |
49 | if (write (pipefd[1], &ppid, sizeof ppid) != sizeof (ppid)) |
50 | FAIL_EXIT1 ("write ppid failed\n" ); |
51 | if (write (pipefd[1], &pid, sizeof pid) != sizeof (pid)) |
52 | FAIL_EXIT1 ("write pid failed\n" ); |
53 | if (write (pipefd[1], &tid, sizeof tid) != sizeof (tid)) |
54 | FAIL_EXIT1 ("write tid failed\n" ); |
55 | |
56 | return 0; |
57 | } |
58 | |
59 | |
60 | static int |
61 | do_test (void) |
62 | { |
63 | sig = SIGRTMIN; |
64 | sigset_t ss; |
65 | sigemptyset (&ss); |
66 | sigaddset (&ss, sig); |
67 | if (sigprocmask (SIG_BLOCK, set: &ss, NULL) != 0) |
68 | FAIL_EXIT1 ("sigprocmask failed: %m" ); |
69 | |
70 | if (pipe2 (pipedes: pipefd, O_CLOEXEC)) |
71 | FAIL_EXIT1 ("pipe failed: %m" ); |
72 | |
73 | #define STACK_SIZE 128 * 1024 |
74 | char st[STACK_SIZE] __attribute__ ((aligned)); |
75 | struct clone_args clone_args = |
76 | { |
77 | .stack = (uintptr_t) st, |
78 | .stack_size = sizeof (st), |
79 | }; |
80 | pid_t p = __clone_internal (&clone_args, f, 0); |
81 | |
82 | close (fd: pipefd[1]); |
83 | |
84 | if (p == -1) |
85 | FAIL_EXIT1("clone failed: %m" ); |
86 | |
87 | pid_t ppid, pid, tid; |
88 | if (read (pipefd[0], &ppid, sizeof pid) != sizeof pid) |
89 | { |
90 | kill (pid: p, SIGKILL); |
91 | FAIL_EXIT1 ("read ppid failed: %m" ); |
92 | } |
93 | if (read (pipefd[0], &pid, sizeof pid) != sizeof pid) |
94 | { |
95 | kill (pid: p, SIGKILL); |
96 | FAIL_EXIT1 ("read pid failed: %m" ); |
97 | } |
98 | if (read (pipefd[0], &tid, sizeof tid) != sizeof tid) |
99 | { |
100 | kill (pid: p, SIGKILL); |
101 | FAIL_EXIT1 ("read tid failed: %m" ); |
102 | } |
103 | |
104 | close (fd: pipefd[0]); |
105 | |
106 | pid_t own_pid = getpid (); |
107 | pid_t own_tid = syscall (__NR_gettid); |
108 | |
109 | /* Some sanity checks for clone syscall: returned ppid should be current |
110 | pid and both returned tid/pid should be different from current one. */ |
111 | if ((ppid != own_pid) || (pid == own_pid) || (tid == own_tid)) |
112 | FAIL_RET ("ppid=%i pid=%i tid=%i | own_pid=%i own_tid=%i" , |
113 | (int)ppid, (int)pid, (int)tid, (int)own_pid, (int)own_tid); |
114 | |
115 | int e; |
116 | xwaitpid (p, status: &e, __WCLONE); |
117 | TEST_VERIFY (WIFEXITED (e)); |
118 | TEST_COMPARE (WEXITSTATUS (e), 0); |
119 | return 0; |
120 | } |
121 | |
122 | #include <support/test-driver.c> |
123 | |