1 /* Measure __strcpy_chk functions.
2 Copyright (C) 2013-2022 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 <https://www.gnu.org/licenses/>. */
20 # define STRCPY_RESULT(dst, len) dst
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
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)
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)
39 simple_strcpy_chk (char *dst
, const char *src
, size_t len
)
44 while ((*dst
++ = *src
++) != '\0')
56 #include <support/support.h>
58 volatile int chk_fail_ok
;
67 longjmp (chk_fail_buf
, 1);
73 typedef char *(*proto_t
) (char *, const char *, size_t);
76 do_one_test (impl_t
*impl
, char *dst
, const char *src
,
77 size_t len
, size_t dlen
)
80 size_t i
, iters
= INNER_LOOP_ITERS8
;
81 timing_t start
, stop
, cur
;
89 if (setjmp (chk_fail_buf
) == 0)
91 res
= CALL (impl
, dst
, src
, dlen
);
92 printf ("*** Function %s (%zd; %zd) did not __chk_fail\n",
93 impl
->name
, len
, dlen
);
100 res
= CALL (impl
, dst
, src
, dlen
);
102 if (res
!= STRCPY_RESULT (dst
, len
))
104 printf ("Wrong result in function %s %p %p\n", impl
->name
,
105 res
, STRCPY_RESULT (dst
, len
));
110 if (strcmp (dst
, src
) != 0)
112 printf ("Wrong result in function %s dst \"%s\" src \"%s\"\n",
113 impl
->name
, dst
, src
);
119 for (i
= 0; i
< iters
; ++i
)
121 CALL (impl
, dst
, src
, dlen
);
125 TIMING_DIFF (cur
, start
, stop
);
127 TIMING_PRINT_MEAN ((double) cur
, (double) iters
);
131 do_test (size_t align1
, size_t align2
, size_t len
, size_t dlen
, int max_char
)
137 if (align1
+ len
>= page_size
)
141 if (align2
+ len
>= page_size
)
144 s1
= (char *) buf1
+ align1
;
145 s2
= (char *) buf2
+ align2
;
147 for (i
= 0; i
< len
; i
++)
148 s1
[i
] = 32 + 23 * i
% (max_char
- 32);
152 printf ("Length %4zd, alignment %2zd/%2zd:", len
, align1
, align2
);
154 FOR_EACH_IMPL (impl
, 0)
155 do_one_test (impl
, s2
, s1
, len
, dlen
);
166 set_fortify_handler (handler
);
171 FOR_EACH_IMPL (impl
, 0)
172 printf ("\t%s", impl
->name
);
175 for (i
= 0; i
< 16; ++i
)
177 do_test (0, 0, i
, i
+ 1, 127);
178 do_test (0, 0, i
, i
+ 1, 255);
179 do_test (0, i
, i
, i
+ 1, 127);
180 do_test (i
, 0, i
, i
+ 1, 255);
183 for (i
= 1; i
< 8; ++i
)
185 do_test (0, 0, 8 << i
, (8 << i
) + 1, 127);
186 do_test (8 - i
, 2 * i
, (8 << i
), (8 << i
) + 1, 127);
189 for (i
= 1; i
< 8; ++i
)
191 do_test (i
, 2 * i
, (8 << i
), (8 << i
) + 1, 127);
192 do_test (2 * i
, i
, (8 << i
), (8 << i
) + 1, 255);
193 do_test (i
, i
, (8 << i
), (8 << i
) + 1, 127);
194 do_test (i
, i
, (8 << i
), (8 << i
) + 1, 255);
197 for (i
= 0; i
< 16; ++i
)
199 do_test (0, 0, i
, i
+ 256, 127);
200 do_test (0, 0, i
, i
+ 256, 255);
201 do_test (0, i
, i
, i
+ 256, 127);
202 do_test (i
, 0, i
, i
+ 256, 255);
205 for (i
= 1; i
< 8; ++i
)
207 do_test (0, 0, 8 << i
, (8 << i
) + 256, 127);
208 do_test (8 - i
, 2 * i
, (8 << i
), (8 << i
) + 256, 127);
211 for (i
= 1; i
< 8; ++i
)
213 do_test (i
, 2 * i
, (8 << i
), (8 << i
) + 256, 127);
214 do_test (2 * i
, i
, (8 << i
), (8 << i
) + 256, 255);
215 do_test (i
, i
, (8 << i
), (8 << i
) + 256, 127);
216 do_test (i
, i
, (8 << i
), (8 << i
) + 256, 255);
219 for (i
= 0; i
< 16; ++i
)
221 do_test (0, 0, i
, i
, 127);
222 do_test (0, 0, i
, i
+ 2, 255);
223 do_test (0, i
, i
, i
+ 3, 127);
224 do_test (i
, 0, i
, i
+ 4, 255);
227 for (i
= 1; i
< 8; ++i
)
229 do_test (0, 0, 8 << i
, (8 << i
) - 15, 127);
230 do_test (8 - i
, 2 * i
, (8 << i
), (8 << i
) + 5, 127);
233 for (i
= 1; i
< 8; ++i
)
235 do_test (i
, 2 * i
, (8 << i
), (8 << i
) + i
, 127);
236 do_test (2 * i
, i
, (8 << i
), (8 << i
) + (i
- 1), 255);
237 do_test (i
, i
, (8 << i
), (8 << i
) + i
+ 2, 127);
238 do_test (i
, i
, (8 << i
), (8 << i
) + i
+ 3, 255);
244 #include <support/test-driver.c>