Further harden glibc malloc metadata against 1-byte overflows.
[glibc.git] / nptl / tst-signal6.c
blob4172150eb84bb217f6e1d0a3f2f25d63726be4e3
1 /* Copyright (C) 2003-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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/>. */
19 #include <pthread.h>
20 #include <signal.h>
21 #include <stdint.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
27 #ifdef SIGRTMIN
29 # define N 2
30 static pthread_barrier_t bar;
31 static struct
33 void *p;
34 pthread_t s;
35 } ti[N];
36 static int sig1;
39 static void
40 handler (int sig)
42 pthread_t self = pthread_self ();
43 size_t i;
45 for (i = 0; i < N; ++i)
46 if (ti[i].s == self)
48 if ((uintptr_t) ti[i].p <= (uintptr_t) &self
49 && (uintptr_t) ti[i].p + 2 * MINSIGSTKSZ > (uintptr_t) &self)
51 puts ("alt stack not used");
52 exit (1);
55 printf ("thread %zu used alt stack for signal %d\n", i, sig);
57 return;
60 puts ("handler: thread not found");
61 exit (1);
65 static void *
66 tf (void *arg)
68 size_t nr = (uintptr_t) arg;
69 if (nr >= N)
71 puts ("wrong nr parameter");
72 exit (1);
75 sigset_t ss;
76 sigemptyset (&ss);
77 size_t i;
78 for (i = 0; i < N; ++i)
79 if (i != nr)
80 if (sigaddset (&ss, sig1 + i) != 0)
82 puts ("tf: sigaddset failed");
83 exit (1);
85 if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
87 puts ("tf: sigmask failed");
88 exit (1);
91 void *p = malloc (2 * MINSIGSTKSZ);
92 if (p == NULL)
94 puts ("tf: malloc failed");
95 exit (1);
98 stack_t s;
99 s.ss_sp = p;
100 s.ss_size = 2 * MINSIGSTKSZ;
101 s.ss_flags = 0;
102 if (sigaltstack (&s, NULL) != 0)
104 puts ("tf: sigaltstack failed");
105 exit (1);
108 ti[nr].p = p;
109 ti[nr].s = pthread_self ();
111 pthread_barrier_wait (&bar);
113 pthread_barrier_wait (&bar);
115 return NULL;
119 static int
120 do_test (void)
122 sig1 = SIGRTMIN;
123 if (sig1 + N > SIGRTMAX)
125 puts ("too few RT signals");
126 return 0;
129 struct sigaction sa;
130 sa.sa_handler = handler;
131 sa.sa_flags = 0;
132 sigemptyset (&sa.sa_mask);
134 if (sigaction (sig1, &sa, NULL) != 0
135 || sigaction (sig1 + 1, &sa, NULL) != 0
136 || sigaction (sig1 + 2, &sa, NULL) != 0)
138 puts ("sigaction failed");
139 return 1;
142 if (pthread_barrier_init (&bar, NULL, 1 + N) != 0)
144 puts ("barrier_init failed");
145 return 1;
148 pthread_t th[N];
149 size_t i;
150 for (i = 0; i < N; ++i)
151 if (pthread_create (&th[i], NULL, tf, (void *) (long int) i) != 0)
153 puts ("create failed");
154 return 1;
157 /* Block the three signals. */
158 sigset_t ss;
159 sigemptyset (&ss);
160 for (i = 0; i <= N; ++i)
161 sigaddset (&ss, sig1 + i);
162 if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
164 puts ("main: sigmask failed");
165 return 1;
168 pthread_barrier_wait (&bar);
170 /* Send some signals. */
171 pid_t me = getpid ();
172 kill (me, sig1 + N);
173 for (i = 0; i < N; ++i)
174 kill (me, sig1 + i);
175 kill (me, sig1 + N);
177 /* Give the signals a chance to be worked on. */
178 sleep (1);
180 pthread_barrier_wait (&bar);
182 for (i = 0; i < N; ++i)
183 if (pthread_join (th[i], NULL) != 0)
185 puts ("join failed");
186 return 1;
189 return 0;
192 # define TEST_FUNCTION do_test ()
194 #else
195 # define TEST_FUNCTION 0
196 #endif
197 #include "../test-skeleton.c"