Revert "Allow glibc to be compiled without EXEC_PAGESIZE"
[glibc.git] / sysdeps / unix / sysv / linux / tst-clone2.c
blob683a0d76482b0fe5d1665ea2b492f231f975ae95
1 /* Test if CLONE_VM does not change pthread pid/tid field (BZ #19957)
2 Copyright (C) 2016-2024 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 <https://www.gnu.org/licenses/>. */
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>
33 #include <stackinfo.h> /* For _STACK_GROWS_{UP,DOWN}. */
35 #include <support/check.h>
37 static int sig;
38 static int pipefd[2];
40 static int
41 f (void *a)
43 close (pipefd[0]);
45 pid_t ppid = getppid ();
46 pid_t pid = getpid ();
47 pid_t tid = syscall (__NR_gettid);
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");
56 return 0;
60 static int
61 do_test (void)
63 sig = SIGRTMIN;
64 sigset_t ss;
65 sigemptyset (&ss);
66 sigaddset (&ss, sig);
67 if (sigprocmask (SIG_BLOCK, &ss, NULL) != 0)
68 FAIL_EXIT1 ("sigprocmask failed: %m");
70 if (pipe2 (pipefd, O_CLOEXEC))
71 FAIL_EXIT1 ("pipe failed: %m");
73 int clone_flags = 0;
74 char st[128 * 1024] __attribute__ ((aligned));
75 #if _STACK_GROWS_DOWN
76 pid_t p = clone (f, st + sizeof (st), clone_flags, 0);
77 #elif _STACK_GROWS_UP
78 pid_t p = clone (f, st, clone_flags, 0);
79 #else
80 #error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
81 #endif
83 close (pipefd[1]);
85 if (p == -1)
86 FAIL_EXIT1("clone failed: %m");
88 pid_t ppid, pid, tid;
89 if (read (pipefd[0], &ppid, sizeof pid) != sizeof pid)
91 kill (p, SIGKILL);
92 FAIL_EXIT1 ("read ppid failed: %m");
94 if (read (pipefd[0], &pid, sizeof pid) != sizeof pid)
96 kill (p, SIGKILL);
97 FAIL_EXIT1 ("read pid failed: %m");
99 if (read (pipefd[0], &tid, sizeof tid) != sizeof tid)
101 kill (p, SIGKILL);
102 FAIL_EXIT1 ("read tid failed: %m");
105 close (pipefd[0]);
107 int ret = 0;
109 pid_t own_pid = getpid ();
110 pid_t own_tid = syscall (__NR_gettid);
112 /* Some sanity checks for clone syscall: returned ppid should be current
113 pid and both returned tid/pid should be different from current one. */
114 if ((ppid != own_pid) || (pid == own_pid) || (tid == own_tid))
115 FAIL_RET ("ppid=%i pid=%i tid=%i | own_pid=%i own_tid=%i",
116 (int)ppid, (int)pid, (int)tid, (int)own_pid, (int)own_tid);
118 int e;
119 if (waitpid (p, &e, __WCLONE) != p)
121 kill (p, SIGKILL);
122 FAIL_EXIT1 ("waitpid failed");
124 if (!WIFEXITED (e))
126 if (WIFSIGNALED (e))
127 printf ("died from signal %s\n", strsignal (WTERMSIG (e)));
128 else
129 puts ("did not terminate correctly");
130 exit (EXIT_FAILURE);
132 if (WEXITSTATUS (e) != 0)
133 FAIL_EXIT1 ("exit code %d", WEXITSTATUS (e));
135 return ret;
138 #include <support/test-driver.c>