(rfc3484_sort): Make sure that even if qsort doesn't support
[glibc.git] / nptl / tst-stackguard1.c
blob15c30aeb6b300e8299736922530dbe224b2b5e3f
1 /* Copyright (C) 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Jakub Jelinek <jakub@redhat.com>, 2005.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/wait.h>
27 #include <elf/stackguard-macros.h>
28 #include <unistd.h>
30 static const char *command;
31 static bool child;
32 static uintptr_t stack_chk_guard_copy;
33 static bool stack_chk_guard_copy_set;
34 static int fds[2];
36 static void __attribute__ ((constructor))
37 con (void)
39 stack_chk_guard_copy = STACK_CHK_GUARD;
40 stack_chk_guard_copy_set = true;
43 static int
44 uintptr_t_cmp (const void *a, const void *b)
46 if (*(uintptr_t *) a < *(uintptr_t *) b)
47 return 1;
48 if (*(uintptr_t *) a > *(uintptr_t *) b)
49 return -1;
50 return 0;
53 static void *
54 tf (void *arg)
56 if (stack_chk_guard_copy != STACK_CHK_GUARD)
58 puts ("STACK_CHK_GUARD changed in thread");
59 return (void *) 1L;
61 return NULL;
64 static int
65 do_test (void)
67 if (!stack_chk_guard_copy_set)
69 puts ("constructor has not been run");
70 return 1;
73 if (stack_chk_guard_copy != STACK_CHK_GUARD)
75 puts ("STACK_CHK_GUARD changed between constructor and do_test");
76 return 1;
79 if (child)
81 int i;
82 pthread_t th[4];
83 void *ret;
84 for (i = 0; i < 4; ++i)
85 if (pthread_create (&th[i], NULL, tf, NULL))
87 puts ("thread creation failed");
88 return 1;
90 for (i = 0; i < 4; ++i)
91 if (pthread_join (th[i], &ret))
93 puts ("thread join failed");
94 return 1;
96 else if (ret != NULL)
97 return 1;
99 write (2, &stack_chk_guard_copy, sizeof (stack_chk_guard_copy));
100 return 0;
103 if (command == NULL)
105 puts ("missing --command or --child argument");
106 return 1;
109 #define N 16
110 uintptr_t child_stack_chk_guards[N + 1];
111 child_stack_chk_guards[N] = stack_chk_guard_copy;
112 int i;
113 for (i = 0; i < N; ++i)
115 if (pipe (fds) < 0)
117 printf ("couldn't create pipe: %m\n");
118 return 1;
121 pid_t pid = fork ();
122 if (pid < 0)
124 printf ("fork failed: %m\n");
125 return 1;
128 if (!pid)
130 if (stack_chk_guard_copy != STACK_CHK_GUARD)
132 puts ("STACK_CHK_GUARD changed after fork");
133 exit (1);
136 close (fds[0]);
137 close (2);
138 dup2 (fds[1], 2);
139 close (fds[1]);
141 system (command);
142 exit (0);
145 close (fds[1]);
147 if (TEMP_FAILURE_RETRY (read (fds[0], &child_stack_chk_guards[i],
148 sizeof (uintptr_t))) != sizeof (uintptr_t))
150 puts ("could not read stack_chk_guard value from child");
151 return 1;
154 close (fds[0]);
156 pid_t termpid;
157 int status;
158 termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
159 if (termpid == -1)
161 printf ("waitpid failed: %m\n");
162 return 1;
164 else if (termpid != pid)
166 printf ("waitpid returned %ld != %ld\n",
167 (long int) termpid, (long int) pid);
168 return 1;
170 else if (!WIFEXITED (status) || WEXITSTATUS (status))
172 puts ("child hasn't exited with exit status 0");
173 return 1;
177 qsort (child_stack_chk_guards, N + 1, sizeof (uintptr_t), uintptr_t_cmp);
179 uintptr_t default_guard = 0;
180 unsigned char *p = (unsigned char *) &default_guard;
181 p[sizeof (uintptr_t) - 1] = 255;
182 p[sizeof (uintptr_t) - 2] = '\n';
183 p[0] = 0;
185 /* Test if the stack guard canaries are either randomized,
186 or equal to the default stack guard canary value.
187 Even with randomized stack guards it might happen
188 that the random number generator generates the same
189 values, but if that happens in more than half from
190 the 16 runs, something is very wrong. */
191 int ndifferences = 0;
192 int ndefaults = 0;
193 for (i = 0; i < N; ++i)
195 if (child_stack_chk_guards[i] != child_stack_chk_guards[i+1])
196 ndifferences++;
197 else if (child_stack_chk_guards[i] == default_guard)
198 ndefaults++;
201 printf ("differences %d defaults %d\n", ndifferences, ndefaults);
203 if (ndifferences < N / 2 && ndefaults < N / 2)
205 puts ("stack guard canaries are not randomized enough");
206 puts ("nor equal to the default canary value");
207 return 1;
210 return 0;
213 #define OPT_COMMAND 10000
214 #define OPT_CHILD 10001
215 #define CMDLINE_OPTIONS \
216 { "command", required_argument, NULL, OPT_COMMAND }, \
217 { "child", no_argument, NULL, OPT_CHILD },
218 #define CMDLINE_PROCESS \
219 case OPT_COMMAND: \
220 command = optarg; \
221 break; \
222 case OPT_CHILD: \
223 child = true; \
224 break;
225 #define TEST_FUNCTION do_test ()
226 #include "../test-skeleton.c"