1/* Test and measure __strcpy_chk functions.
2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
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 <https://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 "../string/test-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. */
28extern void __attribute__ ((noreturn)) __chk_fail (void);
29char *simple_strcpy_chk (char *, const char *, size_t);
30extern char *normal_strcpy (char *, const char *, size_t)
31 __asm ("strcpy");
32extern char *__strcpy_chk (char *, const char *, size_t);
33
34IMPL (simple_strcpy_chk, 0)
35IMPL (normal_strcpy, 1)
36IMPL (__strcpy_chk, 2)
37
38char *
39simple_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
56static int test_main (void);
57#define TEST_FUNCTION test_main
58#include <support/test-driver.c>
59#include <support/support.h>
60
61volatile int chk_fail_ok;
62jmp_buf chk_fail_buf;
63
64static void
65handler (int sig)
66{
67 if (chk_fail_ok)
68 {
69 chk_fail_ok = 0;
70 longjmp (env: chk_fail_buf, val: 1);
71 }
72 else
73 _exit (127);
74}
75
76typedef char *(*proto_t) (char *, const char *, size_t);
77
78static void
79do_one_test (impl_t *impl, char *dst, const char *src,
80 size_t len, size_t dlen)
81{
82 char *res;
83 if (dlen <= len)
84 {
85 if (impl->test == 1)
86 return;
87
88 chk_fail_ok = 1;
89 if (setjmp (chk_fail_buf) == 0)
90 {
91 res = CALL (impl, dst, src, dlen);
92 printf (format: "*** Function %s (%zd; %zd) did not __chk_fail\n",
93 impl->name, len, dlen);
94 chk_fail_ok = 0;
95 ret = 1;
96 }
97 return;
98 }
99 else
100 res = CALL (impl, dst, src, dlen);
101
102 if (res != STRCPY_RESULT (dst, len))
103 {
104 printf (format: "Wrong result in function %s %p %p\n", impl->name,
105 res, STRCPY_RESULT (dst, len));
106 ret = 1;
107 return;
108 }
109
110 if (strcmp (dst, src) != 0)
111 {
112 printf (format: "Wrong result in function %s dst \"%s\" src \"%s\"\n",
113 impl->name, dst, src);
114 ret = 1;
115 return;
116 }
117}
118
119static void
120do_test (size_t align1, size_t align2, size_t len, size_t dlen, int max_char)
121{
122 size_t i;
123 char *s1, *s2;
124
125 align1 &= 7;
126 if (align1 + len >= page_size)
127 return;
128
129 align2 &= 7;
130 if (align2 + len >= page_size)
131 return;
132
133 s1 = (char *) buf1 + align1;
134 s2 = (char *) buf2 + align2;
135
136 for (i = 0; i < len; i++)
137 s1[i] = 32 + 23 * i % (max_char - 32);
138 s1[len] = 0;
139
140 FOR_EACH_IMPL (impl, 0)
141 do_one_test (impl, dst: s2, src: s1, len, dlen);
142}
143
144static void
145do_random_tests (void)
146{
147 size_t i, j, n, align1, align2, len, dlen;
148 unsigned char *p1 = buf1 + page_size - 512;
149 unsigned char *p2 = buf2 + page_size - 512;
150 unsigned char *res;
151
152 for (n = 0; n < ITERATIONS; n++)
153 {
154 align1 = random () & 31;
155 if (random () & 1)
156 align2 = random () & 31;
157 else
158 align2 = align1 + (random () & 24);
159 len = random () & 511;
160 j = align1;
161 if (align2 > j)
162 j = align2;
163 if (len + j >= 511)
164 len = 510 - j - (random () & 7);
165 j = len + align1 + 64;
166 if (j > 512)
167 j = 512;
168 for (i = 0; i < j; i++)
169 {
170 if (i == len + align1)
171 p1[i] = 0;
172 else
173 {
174 p1[i] = random () & 255;
175 if (i >= align1 && i < len + align1 && !p1[i])
176 p1[i] = (random () & 127) + 3;
177 }
178 }
179
180 switch (random () & 7)
181 {
182 case 0:
183 dlen = len - (random () & 31);
184 if (dlen > len)
185 dlen = len;
186 break;
187 case 1:
188 dlen = (size_t) -1;
189 break;
190 case 2:
191 dlen = len + 1 + (random () & 65535);
192 break;
193 case 3:
194 dlen = len + 1 + (random () & 255);
195 break;
196 case 4:
197 dlen = len + 1 + (random () & 31);
198 break;
199 case 5:
200 dlen = len + 1 + (random () & 7);
201 break;
202 case 6:
203 dlen = len + 1 + (random () & 3);
204 break;
205 default:
206 dlen = len + 1;
207 break;
208 }
209
210 FOR_EACH_IMPL (impl, 1)
211 {
212 if (dlen <= len)
213 {
214 if (impl->test != 1)
215 {
216 chk_fail_ok = 1;
217 if (setjmp (chk_fail_buf) == 0)
218 {
219 res = (unsigned char *)
220 CALL (impl, (char *) p2 + align2,
221 (char *) p1 + align1, dlen);
222 printf (format: "Iteration %zd - did not __chk_fail\n", n);
223 chk_fail_ok = 0;
224 ret = 1;
225 }
226 }
227 continue;
228 }
229 memset (p2 - 64, '\1', 512 + 64);
230 res = (unsigned char *)
231 CALL (impl, (char *) p2 + align2, (char *) p1 + align1, dlen);
232 if (res != STRCPY_RESULT (p2 + align2, len))
233 {
234 printf (format: "\
235Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p\n",
236 n, impl->name, align1, align2, len, res,
237 STRCPY_RESULT (p2 + align2, len));
238 ret = 1;
239 }
240 for (j = 0; j < align2 + 64; ++j)
241 {
242 if (p2[j - 64] != '\1')
243 {
244 printf (format: "\
245Iteration %zd - garbage before, %s (%zd, %zd, %zd)\n",
246 n, impl->name, align1, align2, len);
247 ret = 1;
248 break;
249 }
250 }
251 for (j = align2 + len + 1; j < 512; ++j)
252 {
253 if (p2[j] != '\1')
254 {
255 printf (format: "\
256Iteration %zd - garbage after, %s (%zd, %zd, %zd)\n",
257 n, impl->name, align1, align2, len);
258 ret = 1;
259 break;
260 }
261 }
262 if (memcmp (p1 + align1, p2 + align2, len + 1))
263 {
264 printf (format: "\
265Iteration %zd - different strings, %s (%zd, %zd, %zd)\n",
266 n, impl->name, align1, align2, len);
267 ret = 1;
268 }
269 }
270 }
271}
272
273static int
274test_main (void)
275{
276 size_t i;
277
278 set_fortify_handler (handler);
279
280 test_init ();
281
282 printf (format: "%23s", "");
283 FOR_EACH_IMPL (impl, 0)
284 printf (format: "\t%s", impl->name);
285 putchar (c: '\n');
286
287 for (i = 0; i < 16; ++i)
288 {
289 do_test (align1: 0, align2: 0, len: i, dlen: i + 1, max_char: 127);
290 do_test (align1: 0, align2: 0, len: i, dlen: i + 1, max_char: 255);
291 do_test (align1: 0, align2: i, len: i, dlen: i + 1, max_char: 127);
292 do_test (align1: i, align2: 0, len: i, dlen: i + 1, max_char: 255);
293 }
294
295 for (i = 1; i < 8; ++i)
296 {
297 do_test (align1: 0, align2: 0, len: 8 << i, dlen: (8 << i) + 1, max_char: 127);
298 do_test (align1: 8 - i, align2: 2 * i, len: (8 << i), dlen: (8 << i) + 1, max_char: 127);
299 }
300
301 for (i = 1; i < 8; ++i)
302 {
303 do_test (align1: i, align2: 2 * i, len: (8 << i), dlen: (8 << i) + 1, max_char: 127);
304 do_test (align1: 2 * i, align2: i, len: (8 << i), dlen: (8 << i) + 1, max_char: 255);
305 do_test (align1: i, align2: i, len: (8 << i), dlen: (8 << i) + 1, max_char: 127);
306 do_test (align1: i, align2: i, len: (8 << i), dlen: (8 << i) + 1, max_char: 255);
307 }
308
309 for (i = 0; i < 16; ++i)
310 {
311 do_test (align1: 0, align2: 0, len: i, dlen: i + 256, max_char: 127);
312 do_test (align1: 0, align2: 0, len: i, dlen: i + 256, max_char: 255);
313 do_test (align1: 0, align2: i, len: i, dlen: i + 256, max_char: 127);
314 do_test (align1: i, align2: 0, len: i, dlen: i + 256, max_char: 255);
315 }
316
317 for (i = 1; i < 8; ++i)
318 {
319 do_test (align1: 0, align2: 0, len: 8 << i, dlen: (8 << i) + 256, max_char: 127);
320 do_test (align1: 8 - i, align2: 2 * i, len: (8 << i), dlen: (8 << i) + 256, max_char: 127);
321 }
322
323 for (i = 1; i < 8; ++i)
324 {
325 do_test (align1: i, align2: 2 * i, len: (8 << i), dlen: (8 << i) + 256, max_char: 127);
326 do_test (align1: 2 * i, align2: i, len: (8 << i), dlen: (8 << i) + 256, max_char: 255);
327 do_test (align1: i, align2: i, len: (8 << i), dlen: (8 << i) + 256, max_char: 127);
328 do_test (align1: i, align2: i, len: (8 << i), dlen: (8 << i) + 256, max_char: 255);
329 }
330
331 for (i = 0; i < 16; ++i)
332 {
333 do_test (align1: 0, align2: 0, len: i, dlen: i, max_char: 127);
334 do_test (align1: 0, align2: 0, len: i, dlen: i + 2, max_char: 255);
335 do_test (align1: 0, align2: i, len: i, dlen: i + 3, max_char: 127);
336 do_test (align1: i, align2: 0, len: i, dlen: i + 4, max_char: 255);
337 }
338
339 for (i = 1; i < 8; ++i)
340 {
341 do_test (align1: 0, align2: 0, len: 8 << i, dlen: (8 << i) - 15, max_char: 127);
342 do_test (align1: 8 - i, align2: 2 * i, len: (8 << i), dlen: (8 << i) + 5, max_char: 127);
343 }
344
345 for (i = 1; i < 8; ++i)
346 {
347 do_test (align1: i, align2: 2 * i, len: (8 << i), dlen: (8 << i) + i, max_char: 127);
348 do_test (align1: 2 * i, align2: i, len: (8 << i), dlen: (8 << i) + (i - 1), max_char: 255);
349 do_test (align1: i, align2: i, len: (8 << i), dlen: (8 << i) + i + 2, max_char: 127);
350 do_test (align1: i, align2: i, len: (8 << i), dlen: (8 << i) + i + 3, max_char: 255);
351 }
352
353 do_random_tests ();
354 return ret;
355}
356

source code of glibc/debug/test-strcpy_chk.c