Mention BZ #15674
[glibc.git] / benchtests / bench-strcpy_chk.c
blob29e57285b0793249154c41195258e51e29d3bdf4
1 /* Measure __strcpy_chk functions.
2 Copyright (C) 2013 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 <http://www.gnu.org/licenses/>. */
19 #ifndef STRCPY_RESULT
20 # define STRCPY_RESULT(dst, len) dst
21 # define TEST_MAIN
22 # define TEST_NAME "strcpy_chk"
23 # include "bench-string.h"
25 /* This test case implicitly tests the availability of the __chk_fail
26 symbol, which is part of the public ABI and may be used
27 externally. */
28 extern void __attribute__ ((noreturn)) __chk_fail (void);
29 char *simple_strcpy_chk (char *, const char *, size_t);
30 extern char *normal_strcpy (char *, const char *, size_t)
31 __asm ("strcpy");
32 extern char *__strcpy_chk (char *, const char *, size_t);
34 IMPL (simple_strcpy_chk, 0)
35 IMPL (normal_strcpy, 1)
36 IMPL (__strcpy_chk, 2)
38 char *
39 simple_strcpy_chk (char *dst, const char *src, size_t len)
41 char *ret = dst;
42 if (! len)
43 __chk_fail ();
44 while ((*dst++ = *src++) != '\0')
45 if (--len == 0)
46 __chk_fail ();
47 return ret;
49 #endif
51 #include <fcntl.h>
52 #include <paths.h>
53 #include <setjmp.h>
54 #include <signal.h>
56 volatile int chk_fail_ok;
57 jmp_buf chk_fail_buf;
59 static void
60 handler (int sig)
62 if (chk_fail_ok)
64 chk_fail_ok = 0;
65 longjmp (chk_fail_buf, 1);
67 else
68 _exit (127);
71 typedef char *(*proto_t) (char *, const char *, size_t);
73 static void
74 do_one_test (impl_t *impl, char *dst, const char *src,
75 size_t len, size_t dlen)
77 char *res;
78 if (dlen <= len)
80 if (impl->test == 1)
81 return;
83 chk_fail_ok = 1;
84 if (setjmp (chk_fail_buf) == 0)
86 res = CALL (impl, dst, src, dlen);
87 printf ("*** Function %s (%zd; %zd) did not __chk_fail\n",
88 impl->name, len, dlen);
89 chk_fail_ok = 0;
90 ret = 1;
92 return;
94 else
95 res = CALL (impl, dst, src, dlen);
97 if (res != STRCPY_RESULT (dst, len))
99 printf ("Wrong result in function %s %p %p\n", impl->name,
100 res, STRCPY_RESULT (dst, len));
101 ret = 1;
102 return;
105 if (strcmp (dst, src) != 0)
107 printf ("Wrong result in function %s dst \"%s\" src \"%s\"\n",
108 impl->name, dst, src);
109 ret = 1;
110 return;
113 if (HP_TIMING_AVAIL)
115 hp_timing_t start __attribute ((unused));
116 hp_timing_t stop __attribute ((unused));;
117 hp_timing_t best_time = ~ (hp_timing_t) 0;
118 size_t i;
120 for (i = 0; i < 32; ++i)
122 HP_TIMING_NOW (start);
123 CALL (impl, dst, src, dlen);
124 HP_TIMING_NOW (stop);
125 HP_TIMING_BEST (best_time, start, stop);
128 printf ("\t%zd", (size_t) best_time);
132 static void
133 do_test (size_t align1, size_t align2, size_t len, size_t dlen, int max_char)
135 size_t i;
136 char *s1, *s2;
138 align1 &= 7;
139 if (align1 + len >= page_size)
140 return;
142 align2 &= 7;
143 if (align2 + len >= page_size)
144 return;
146 s1 = (char *) buf1 + align1;
147 s2 = (char *) buf2 + align2;
149 for (i = 0; i < len; i++)
150 s1[i] = 32 + 23 * i % (max_char - 32);
151 s1[len] = 0;
153 if (HP_TIMING_AVAIL && dlen > len)
154 printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2);
156 FOR_EACH_IMPL (impl, 0)
157 do_one_test (impl, s2, s1, len, dlen);
159 if (HP_TIMING_AVAIL && dlen > len)
160 putchar ('\n');
164 test_main (void)
166 size_t i;
168 struct sigaction sa;
169 sa.sa_handler = handler;
170 sa.sa_flags = 0;
171 sigemptyset (&sa.sa_mask);
173 sigaction (SIGABRT, &sa, NULL);
175 /* Avoid all the buffer overflow messages on stderr. */
176 int fd = open (_PATH_DEVNULL, O_WRONLY);
177 if (fd == -1)
178 close (STDERR_FILENO);
179 else
181 dup2 (fd, STDERR_FILENO);
182 close (fd);
184 setenv ("LIBC_FATAL_STDERR_", "1", 1);
186 test_init ();
188 printf ("%23s", "");
189 FOR_EACH_IMPL (impl, 0)
190 printf ("\t%s", impl->name);
191 putchar ('\n');
193 for (i = 0; i < 16; ++i)
195 do_test (0, 0, i, i + 1, 127);
196 do_test (0, 0, i, i + 1, 255);
197 do_test (0, i, i, i + 1, 127);
198 do_test (i, 0, i, i + 1, 255);
201 for (i = 1; i < 8; ++i)
203 do_test (0, 0, 8 << i, (8 << i) + 1, 127);
204 do_test (8 - i, 2 * i, (8 << i), (8 << i) + 1, 127);
207 for (i = 1; i < 8; ++i)
209 do_test (i, 2 * i, (8 << i), (8 << i) + 1, 127);
210 do_test (2 * i, i, (8 << i), (8 << i) + 1, 255);
211 do_test (i, i, (8 << i), (8 << i) + 1, 127);
212 do_test (i, i, (8 << i), (8 << i) + 1, 255);
215 for (i = 0; i < 16; ++i)
217 do_test (0, 0, i, i + 256, 127);
218 do_test (0, 0, i, i + 256, 255);
219 do_test (0, i, i, i + 256, 127);
220 do_test (i, 0, i, i + 256, 255);
223 for (i = 1; i < 8; ++i)
225 do_test (0, 0, 8 << i, (8 << i) + 256, 127);
226 do_test (8 - i, 2 * i, (8 << i), (8 << i) + 256, 127);
229 for (i = 1; i < 8; ++i)
231 do_test (i, 2 * i, (8 << i), (8 << i) + 256, 127);
232 do_test (2 * i, i, (8 << i), (8 << i) + 256, 255);
233 do_test (i, i, (8 << i), (8 << i) + 256, 127);
234 do_test (i, i, (8 << i), (8 << i) + 256, 255);
237 for (i = 0; i < 16; ++i)
239 do_test (0, 0, i, i, 127);
240 do_test (0, 0, i, i + 2, 255);
241 do_test (0, i, i, i + 3, 127);
242 do_test (i, 0, i, i + 4, 255);
245 for (i = 1; i < 8; ++i)
247 do_test (0, 0, 8 << i, (8 << i) - 15, 127);
248 do_test (8 - i, 2 * i, (8 << i), (8 << i) + 5, 127);
251 for (i = 1; i < 8; ++i)
253 do_test (i, 2 * i, (8 << i), (8 << i) + i, 127);
254 do_test (2 * i, i, (8 << i), (8 << i) + (i - 1), 255);
255 do_test (i, i, (8 << i), (8 << i) + i + 2, 127);
256 do_test (i, i, (8 << i), (8 << i) + i + 3, 255);
259 return 0;
262 #include "../test-skeleton.c"