]>
Commit | Line | Data |
---|---|---|
94aca5e7 | 1 | /* Measure __strcpy_chk functions. |
d4697bc9 | 2 | Copyright (C) 2013-2014 Free Software Foundation, Inc. |
94aca5e7 SP |
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 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
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" | |
24 | ||
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); | |
33 | ||
34 | IMPL (simple_strcpy_chk, 0) | |
35 | IMPL (normal_strcpy, 1) | |
36 | IMPL (__strcpy_chk, 2) | |
37 | ||
38 | char * | |
39 | simple_strcpy_chk (char *dst, const char *src, size_t len) | |
40 | { | |
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; | |
48 | } | |
49 | #endif | |
50 | ||
51 | #include <fcntl.h> | |
52 | #include <paths.h> | |
53 | #include <setjmp.h> | |
54 | #include <signal.h> | |
55 | ||
56 | volatile int chk_fail_ok; | |
57 | jmp_buf chk_fail_buf; | |
58 | ||
59 | static void | |
60 | handler (int sig) | |
61 | { | |
62 | if (chk_fail_ok) | |
63 | { | |
64 | chk_fail_ok = 0; | |
65 | longjmp (chk_fail_buf, 1); | |
66 | } | |
67 | else | |
68 | _exit (127); | |
69 | } | |
70 | ||
71 | typedef char *(*proto_t) (char *, const char *, size_t); | |
72 | ||
73 | static void | |
74 | do_one_test (impl_t *impl, char *dst, const char *src, | |
75 | size_t len, size_t dlen) | |
76 | { | |
77 | char *res; | |
44558701 WN |
78 | size_t i, iters = INNER_LOOP_ITERS; |
79 | timing_t start, stop, cur; | |
80 | ||
94aca5e7 SP |
81 | if (dlen <= len) |
82 | { | |
83 | if (impl->test == 1) | |
84 | return; | |
85 | ||
86 | chk_fail_ok = 1; | |
87 | if (setjmp (chk_fail_buf) == 0) | |
88 | { | |
89 | res = CALL (impl, dst, src, dlen); | |
90 | printf ("*** Function %s (%zd; %zd) did not __chk_fail\n", | |
91 | impl->name, len, dlen); | |
92 | chk_fail_ok = 0; | |
93 | ret = 1; | |
94 | } | |
95 | return; | |
96 | } | |
97 | else | |
98 | res = CALL (impl, dst, src, dlen); | |
99 | ||
100 | if (res != STRCPY_RESULT (dst, len)) | |
101 | { | |
102 | printf ("Wrong result in function %s %p %p\n", impl->name, | |
103 | res, STRCPY_RESULT (dst, len)); | |
104 | ret = 1; | |
105 | return; | |
106 | } | |
107 | ||
108 | if (strcmp (dst, src) != 0) | |
109 | { | |
110 | printf ("Wrong result in function %s dst \"%s\" src \"%s\"\n", | |
111 | impl->name, dst, src); | |
112 | ret = 1; | |
113 | return; | |
114 | } | |
115 | ||
44558701 WN |
116 | TIMING_NOW (start); |
117 | for (i = 0; i < iters; ++i) | |
94aca5e7 | 118 | { |
44558701 WN |
119 | CALL (impl, dst, src, dlen); |
120 | } | |
121 | TIMING_NOW (stop); | |
94aca5e7 | 122 | |
44558701 | 123 | TIMING_DIFF (cur, start, stop); |
94aca5e7 | 124 | |
44558701 | 125 | TIMING_PRINT_MEAN ((double) cur, (double) iters); |
94aca5e7 SP |
126 | } |
127 | ||
128 | static void | |
129 | do_test (size_t align1, size_t align2, size_t len, size_t dlen, int max_char) | |
130 | { | |
131 | size_t i; | |
132 | char *s1, *s2; | |
133 | ||
134 | align1 &= 7; | |
135 | if (align1 + len >= page_size) | |
136 | return; | |
137 | ||
138 | align2 &= 7; | |
139 | if (align2 + len >= page_size) | |
140 | return; | |
141 | ||
142 | s1 = (char *) buf1 + align1; | |
143 | s2 = (char *) buf2 + align2; | |
144 | ||
145 | for (i = 0; i < len; i++) | |
146 | s1[i] = 32 + 23 * i % (max_char - 32); | |
147 | s1[len] = 0; | |
148 | ||
44558701 | 149 | if (dlen > len) |
94aca5e7 SP |
150 | printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2); |
151 | ||
152 | FOR_EACH_IMPL (impl, 0) | |
153 | do_one_test (impl, s2, s1, len, dlen); | |
154 | ||
44558701 | 155 | if (dlen > len) |
94aca5e7 SP |
156 | putchar ('\n'); |
157 | } | |
158 | ||
159 | int | |
160 | test_main (void) | |
161 | { | |
162 | size_t i; | |
163 | ||
164 | struct sigaction sa; | |
165 | sa.sa_handler = handler; | |
166 | sa.sa_flags = 0; | |
167 | sigemptyset (&sa.sa_mask); | |
168 | ||
169 | sigaction (SIGABRT, &sa, NULL); | |
170 | ||
171 | /* Avoid all the buffer overflow messages on stderr. */ | |
172 | int fd = open (_PATH_DEVNULL, O_WRONLY); | |
173 | if (fd == -1) | |
174 | close (STDERR_FILENO); | |
175 | else | |
176 | { | |
177 | dup2 (fd, STDERR_FILENO); | |
178 | close (fd); | |
179 | } | |
180 | setenv ("LIBC_FATAL_STDERR_", "1", 1); | |
181 | ||
182 | test_init (); | |
183 | ||
184 | printf ("%23s", ""); | |
185 | FOR_EACH_IMPL (impl, 0) | |
186 | printf ("\t%s", impl->name); | |
187 | putchar ('\n'); | |
188 | ||
189 | for (i = 0; i < 16; ++i) | |
190 | { | |
191 | do_test (0, 0, i, i + 1, 127); | |
192 | do_test (0, 0, i, i + 1, 255); | |
193 | do_test (0, i, i, i + 1, 127); | |
194 | do_test (i, 0, i, i + 1, 255); | |
195 | } | |
196 | ||
197 | for (i = 1; i < 8; ++i) | |
198 | { | |
199 | do_test (0, 0, 8 << i, (8 << i) + 1, 127); | |
200 | do_test (8 - i, 2 * i, (8 << i), (8 << i) + 1, 127); | |
201 | } | |
202 | ||
203 | for (i = 1; i < 8; ++i) | |
204 | { | |
205 | do_test (i, 2 * i, (8 << i), (8 << i) + 1, 127); | |
206 | do_test (2 * i, i, (8 << i), (8 << i) + 1, 255); | |
207 | do_test (i, i, (8 << i), (8 << i) + 1, 127); | |
208 | do_test (i, i, (8 << i), (8 << i) + 1, 255); | |
209 | } | |
210 | ||
211 | for (i = 0; i < 16; ++i) | |
212 | { | |
213 | do_test (0, 0, i, i + 256, 127); | |
214 | do_test (0, 0, i, i + 256, 255); | |
215 | do_test (0, i, i, i + 256, 127); | |
216 | do_test (i, 0, i, i + 256, 255); | |
217 | } | |
218 | ||
219 | for (i = 1; i < 8; ++i) | |
220 | { | |
221 | do_test (0, 0, 8 << i, (8 << i) + 256, 127); | |
222 | do_test (8 - i, 2 * i, (8 << i), (8 << i) + 256, 127); | |
223 | } | |
224 | ||
225 | for (i = 1; i < 8; ++i) | |
226 | { | |
227 | do_test (i, 2 * i, (8 << i), (8 << i) + 256, 127); | |
228 | do_test (2 * i, i, (8 << i), (8 << i) + 256, 255); | |
229 | do_test (i, i, (8 << i), (8 << i) + 256, 127); | |
230 | do_test (i, i, (8 << i), (8 << i) + 256, 255); | |
231 | } | |
232 | ||
233 | for (i = 0; i < 16; ++i) | |
234 | { | |
235 | do_test (0, 0, i, i, 127); | |
236 | do_test (0, 0, i, i + 2, 255); | |
237 | do_test (0, i, i, i + 3, 127); | |
238 | do_test (i, 0, i, i + 4, 255); | |
239 | } | |
240 | ||
241 | for (i = 1; i < 8; ++i) | |
242 | { | |
243 | do_test (0, 0, 8 << i, (8 << i) - 15, 127); | |
244 | do_test (8 - i, 2 * i, (8 << i), (8 << i) + 5, 127); | |
245 | } | |
246 | ||
247 | for (i = 1; i < 8; ++i) | |
248 | { | |
249 | do_test (i, 2 * i, (8 << i), (8 << i) + i, 127); | |
250 | do_test (2 * i, i, (8 << i), (8 << i) + (i - 1), 255); | |
251 | do_test (i, i, (8 << i), (8 << i) + i + 2, 127); | |
252 | do_test (i, i, (8 << i), (8 << i) + i + 3, 255); | |
253 | } | |
254 | ||
255 | return 0; | |
256 | } | |
257 | ||
258 | #include "../test-skeleton.c" |