| xf.li | bdd93d5 | 2023-05-12 07:10:14 -0700 | [diff] [blame] | 1 | /* Measure __strcpy_chk functions. | 
|  | 2 | Copyright (C) 2013-2016 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 | <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 | static int test_main (void); | 
|  | 57 | #include "../test-skeleton.c" | 
|  | 58 |  | 
|  | 59 | volatile int chk_fail_ok; | 
|  | 60 | jmp_buf chk_fail_buf; | 
|  | 61 |  | 
|  | 62 | static void | 
|  | 63 | handler (int sig) | 
|  | 64 | { | 
|  | 65 | if (chk_fail_ok) | 
|  | 66 | { | 
|  | 67 | chk_fail_ok = 0; | 
|  | 68 | longjmp (chk_fail_buf, 1); | 
|  | 69 | } | 
|  | 70 | else | 
|  | 71 | _exit (127); | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | typedef char *(*proto_t) (char *, const char *, size_t); | 
|  | 75 |  | 
|  | 76 | static void | 
|  | 77 | do_one_test (impl_t *impl, char *dst, const char *src, | 
|  | 78 | size_t len, size_t dlen) | 
|  | 79 | { | 
|  | 80 | char *res; | 
|  | 81 | size_t i, iters = INNER_LOOP_ITERS; | 
|  | 82 | timing_t start, stop, cur; | 
|  | 83 |  | 
|  | 84 | if (dlen <= len) | 
|  | 85 | { | 
|  | 86 | if (impl->test == 1) | 
|  | 87 | return; | 
|  | 88 |  | 
|  | 89 | chk_fail_ok = 1; | 
|  | 90 | if (setjmp (chk_fail_buf) == 0) | 
|  | 91 | { | 
|  | 92 | res = CALL (impl, dst, src, dlen); | 
|  | 93 | printf ("*** Function %s (%zd; %zd) did not __chk_fail\n", | 
|  | 94 | impl->name, len, dlen); | 
|  | 95 | chk_fail_ok = 0; | 
|  | 96 | ret = 1; | 
|  | 97 | } | 
|  | 98 | return; | 
|  | 99 | } | 
|  | 100 | else | 
|  | 101 | res = CALL (impl, dst, src, dlen); | 
|  | 102 |  | 
|  | 103 | if (res != STRCPY_RESULT (dst, len)) | 
|  | 104 | { | 
|  | 105 | printf ("Wrong result in function %s %p %p\n", impl->name, | 
|  | 106 | res, STRCPY_RESULT (dst, len)); | 
|  | 107 | ret = 1; | 
|  | 108 | return; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | if (strcmp (dst, src) != 0) | 
|  | 112 | { | 
|  | 113 | printf ("Wrong result in function %s dst \"%s\" src \"%s\"\n", | 
|  | 114 | impl->name, dst, src); | 
|  | 115 | ret = 1; | 
|  | 116 | return; | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | TIMING_NOW (start); | 
|  | 120 | for (i = 0; i < iters; ++i) | 
|  | 121 | { | 
|  | 122 | CALL (impl, dst, src, dlen); | 
|  | 123 | } | 
|  | 124 | TIMING_NOW (stop); | 
|  | 125 |  | 
|  | 126 | TIMING_DIFF (cur, start, stop); | 
|  | 127 |  | 
|  | 128 | TIMING_PRINT_MEAN ((double) cur, (double) iters); | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | static void | 
|  | 132 | do_test (size_t align1, size_t align2, size_t len, size_t dlen, int max_char) | 
|  | 133 | { | 
|  | 134 | size_t i; | 
|  | 135 | char *s1, *s2; | 
|  | 136 |  | 
|  | 137 | align1 &= 7; | 
|  | 138 | if (align1 + len >= page_size) | 
|  | 139 | return; | 
|  | 140 |  | 
|  | 141 | align2 &= 7; | 
|  | 142 | if (align2 + len >= page_size) | 
|  | 143 | return; | 
|  | 144 |  | 
|  | 145 | s1 = (char *) buf1 + align1; | 
|  | 146 | s2 = (char *) buf2 + align2; | 
|  | 147 |  | 
|  | 148 | for (i = 0; i < len; i++) | 
|  | 149 | s1[i] = 32 + 23 * i % (max_char - 32); | 
|  | 150 | s1[len] = 0; | 
|  | 151 |  | 
|  | 152 | if (dlen > len) | 
|  | 153 | printf ("Length %4zd, alignment %2zd/%2zd:", len, align1, align2); | 
|  | 154 |  | 
|  | 155 | FOR_EACH_IMPL (impl, 0) | 
|  | 156 | do_one_test (impl, s2, s1, len, dlen); | 
|  | 157 |  | 
|  | 158 | if (dlen > len) | 
|  | 159 | putchar ('\n'); | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | static int | 
|  | 163 | test_main (void) | 
|  | 164 | { | 
|  | 165 | size_t i; | 
|  | 166 |  | 
|  | 167 | set_fortify_handler (handler); | 
|  | 168 |  | 
|  | 169 | test_init (); | 
|  | 170 |  | 
|  | 171 | printf ("%23s", ""); | 
|  | 172 | FOR_EACH_IMPL (impl, 0) | 
|  | 173 | printf ("\t%s", impl->name); | 
|  | 174 | putchar ('\n'); | 
|  | 175 |  | 
|  | 176 | for (i = 0; i < 16; ++i) | 
|  | 177 | { | 
|  | 178 | do_test (0, 0, i, i + 1, 127); | 
|  | 179 | do_test (0, 0, i, i + 1, 255); | 
|  | 180 | do_test (0, i, i, i + 1, 127); | 
|  | 181 | do_test (i, 0, i, i + 1, 255); | 
|  | 182 | } | 
|  | 183 |  | 
|  | 184 | for (i = 1; i < 8; ++i) | 
|  | 185 | { | 
|  | 186 | do_test (0, 0, 8 << i, (8 << i) + 1, 127); | 
|  | 187 | do_test (8 - i, 2 * i, (8 << i), (8 << i) + 1, 127); | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | for (i = 1; i < 8; ++i) | 
|  | 191 | { | 
|  | 192 | do_test (i, 2 * i, (8 << i), (8 << i) + 1, 127); | 
|  | 193 | do_test (2 * i, i, (8 << i), (8 << i) + 1, 255); | 
|  | 194 | do_test (i, i, (8 << i), (8 << i) + 1, 127); | 
|  | 195 | do_test (i, i, (8 << i), (8 << i) + 1, 255); | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | for (i = 0; i < 16; ++i) | 
|  | 199 | { | 
|  | 200 | do_test (0, 0, i, i + 256, 127); | 
|  | 201 | do_test (0, 0, i, i + 256, 255); | 
|  | 202 | do_test (0, i, i, i + 256, 127); | 
|  | 203 | do_test (i, 0, i, i + 256, 255); | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | for (i = 1; i < 8; ++i) | 
|  | 207 | { | 
|  | 208 | do_test (0, 0, 8 << i, (8 << i) + 256, 127); | 
|  | 209 | do_test (8 - i, 2 * i, (8 << i), (8 << i) + 256, 127); | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | for (i = 1; i < 8; ++i) | 
|  | 213 | { | 
|  | 214 | do_test (i, 2 * i, (8 << i), (8 << i) + 256, 127); | 
|  | 215 | do_test (2 * i, i, (8 << i), (8 << i) + 256, 255); | 
|  | 216 | do_test (i, i, (8 << i), (8 << i) + 256, 127); | 
|  | 217 | do_test (i, i, (8 << i), (8 << i) + 256, 255); | 
|  | 218 | } | 
|  | 219 |  | 
|  | 220 | for (i = 0; i < 16; ++i) | 
|  | 221 | { | 
|  | 222 | do_test (0, 0, i, i, 127); | 
|  | 223 | do_test (0, 0, i, i + 2, 255); | 
|  | 224 | do_test (0, i, i, i + 3, 127); | 
|  | 225 | do_test (i, 0, i, i + 4, 255); | 
|  | 226 | } | 
|  | 227 |  | 
|  | 228 | for (i = 1; i < 8; ++i) | 
|  | 229 | { | 
|  | 230 | do_test (0, 0, 8 << i, (8 << i) - 15, 127); | 
|  | 231 | do_test (8 - i, 2 * i, (8 << i), (8 << i) + 5, 127); | 
|  | 232 | } | 
|  | 233 |  | 
|  | 234 | for (i = 1; i < 8; ++i) | 
|  | 235 | { | 
|  | 236 | do_test (i, 2 * i, (8 << i), (8 << i) + i, 127); | 
|  | 237 | do_test (2 * i, i, (8 << i), (8 << i) + (i - 1), 255); | 
|  | 238 | do_test (i, i, (8 << i), (8 << i) + i + 2, 127); | 
|  | 239 | do_test (i, i, (8 << i), (8 << i) + i + 3, 255); | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | return 0; | 
|  | 243 | } |