lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Test and measure memcpy functions. |
| 2 | Copyright (C) 1999-2015 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | Written by Jakub Jelinek <jakub@redhat.com>, 1999. |
| 5 | |
| 6 | The GNU C Library is free software; you can redistribute it and/or |
| 7 | modify it under the terms of the GNU Lesser General Public |
| 8 | License as published by the Free Software Foundation; either |
| 9 | version 2.1 of the License, or (at your option) any later version. |
| 10 | |
| 11 | The GNU C Library is distributed in the hope that it will be useful, |
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | Lesser General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU Lesser General Public |
| 17 | License along with the GNU C Library; if not, see |
| 18 | <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #ifndef MEMCPY_RESULT |
| 21 | # define MEMCPY_RESULT(dst, len) dst |
| 22 | # define MIN_PAGE_SIZE 131072 |
| 23 | # define TEST_MAIN |
| 24 | # define TEST_NAME "memcpy" |
| 25 | # include "test-string.h" |
| 26 | |
| 27 | char *simple_memcpy (char *, const char *, size_t); |
| 28 | char *builtin_memcpy (char *, const char *, size_t); |
| 29 | |
| 30 | IMPL (simple_memcpy, 0) |
| 31 | IMPL (builtin_memcpy, 0) |
| 32 | IMPL (memcpy, 1) |
| 33 | |
| 34 | char * |
| 35 | simple_memcpy (char *dst, const char *src, size_t n) |
| 36 | { |
| 37 | char *ret = dst; |
| 38 | while (n--) |
| 39 | *dst++ = *src++; |
| 40 | return ret; |
| 41 | } |
| 42 | |
| 43 | char * |
| 44 | builtin_memcpy (char *dst, const char *src, size_t n) |
| 45 | { |
| 46 | return __builtin_memcpy (dst, src, n); |
| 47 | } |
| 48 | #endif |
| 49 | |
| 50 | typedef char *(*proto_t) (char *, const char *, size_t); |
| 51 | |
| 52 | static void |
| 53 | do_one_test (impl_t *impl, char *dst, const char *src, |
| 54 | size_t len) |
| 55 | { |
| 56 | if (CALL (impl, dst, src, len) != MEMCPY_RESULT (dst, len)) |
| 57 | { |
| 58 | error (0, 0, "Wrong result in function %s %p %p", impl->name, |
| 59 | CALL (impl, dst, src, len), MEMCPY_RESULT (dst, len)); |
| 60 | ret = 1; |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | if (memcmp (dst, src, len) != 0) |
| 65 | { |
| 66 | error (0, 0, "Wrong result in function %s dst %p \"%.*s\" src %p \"%.*s\" len %zu", |
| 67 | impl->name, dst, (int) len, dst, src, (int) len, src, len); |
| 68 | ret = 1; |
| 69 | return; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | static void |
| 74 | do_test (size_t align1, size_t align2, size_t len) |
| 75 | { |
| 76 | size_t i, j; |
| 77 | char *s1, *s2; |
| 78 | |
| 79 | align1 &= 63; |
| 80 | if (align1 + len >= page_size) |
| 81 | return; |
| 82 | |
| 83 | align2 &= 63; |
| 84 | if (align2 + len >= page_size) |
| 85 | return; |
| 86 | |
| 87 | s1 = (char *) (buf1 + align1); |
| 88 | s2 = (char *) (buf2 + align2); |
| 89 | |
| 90 | for (i = 0, j = 1; i < len; i++, j += 23) |
| 91 | s1[i] = j; |
| 92 | |
| 93 | FOR_EACH_IMPL (impl, 0) |
| 94 | do_one_test (impl, s2, s1, len); |
| 95 | } |
| 96 | |
| 97 | static void |
| 98 | do_random_tests (void) |
| 99 | { |
| 100 | size_t i, j, n, align1, align2, len, size1, size2, size; |
| 101 | int c; |
| 102 | unsigned char *p1, *p2; |
| 103 | unsigned char *res; |
| 104 | |
| 105 | for (n = 0; n < ITERATIONS; n++) |
| 106 | { |
| 107 | if (n == 0) |
| 108 | { |
| 109 | len = getpagesize (); |
| 110 | size = len + 512; |
| 111 | size1 = size; |
| 112 | size2 = size; |
| 113 | align1 = 512; |
| 114 | align2 = 512; |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | if ((random () & 255) == 0) |
| 119 | size = 65536; |
| 120 | else |
| 121 | size = 768; |
| 122 | if (size > page_size) |
| 123 | size = page_size; |
| 124 | size1 = size; |
| 125 | size2 = size; |
| 126 | i = random (); |
| 127 | if (i & 3) |
| 128 | size -= 256; |
| 129 | if (i & 1) |
| 130 | size1 -= 256; |
| 131 | if (i & 2) |
| 132 | size2 -= 256; |
| 133 | if (i & 4) |
| 134 | { |
| 135 | len = random () % size; |
| 136 | align1 = size1 - len - (random () & 31); |
| 137 | align2 = size2 - len - (random () & 31); |
| 138 | if (align1 > size1) |
| 139 | align1 = 0; |
| 140 | if (align2 > size2) |
| 141 | align2 = 0; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | align1 = random () & 63; |
| 146 | align2 = random () & 63; |
| 147 | len = random () % size; |
| 148 | if (align1 + len > size1) |
| 149 | align1 = size1 - len; |
| 150 | if (align2 + len > size2) |
| 151 | align2 = size2 - len; |
| 152 | } |
| 153 | } |
| 154 | p1 = buf1 + page_size - size1; |
| 155 | p2 = buf2 + page_size - size2; |
| 156 | c = random () & 255; |
| 157 | j = align1 + len + 256; |
| 158 | if (j > size1) |
| 159 | j = size1; |
| 160 | for (i = 0; i < j; ++i) |
| 161 | p1[i] = random () & 255; |
| 162 | |
| 163 | FOR_EACH_IMPL (impl, 1) |
| 164 | { |
| 165 | j = align2 + len + 256; |
| 166 | if (j > size2) |
| 167 | j = size2; |
| 168 | memset (p2, c, j); |
| 169 | res = (unsigned char *) CALL (impl, |
| 170 | (char *) (p2 + align2), |
| 171 | (char *) (p1 + align1), len); |
| 172 | if (res != MEMCPY_RESULT (p2 + align2, len)) |
| 173 | { |
| 174 | error (0, 0, "Iteration %zd - wrong result in function %s (%zd, %zd, %zd) %p != %p", |
| 175 | n, impl->name, align1, align2, len, res, |
| 176 | MEMCPY_RESULT (p2 + align2, len)); |
| 177 | ret = 1; |
| 178 | } |
| 179 | for (i = 0; i < align2; ++i) |
| 180 | { |
| 181 | if (p2[i] != c) |
| 182 | { |
| 183 | error (0, 0, "Iteration %zd - garbage before, %s (%zd, %zd, %zd)", |
| 184 | n, impl->name, align1, align2, len); |
| 185 | ret = 1; |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | for (i = align2 + len; i < j; ++i) |
| 190 | { |
| 191 | if (p2[i] != c) |
| 192 | { |
| 193 | error (0, 0, "Iteration %zd - garbage after, %s (%zd, %zd, %zd)", |
| 194 | n, impl->name, align1, align2, len); |
| 195 | ret = 1; |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | if (memcmp (p1 + align1, p2 + align2, len)) |
| 200 | { |
| 201 | error (0, 0, "Iteration %zd - different strings, %s (%zd, %zd, %zd)", |
| 202 | n, impl->name, align1, align2, len); |
| 203 | ret = 1; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | int |
| 210 | test_main (void) |
| 211 | { |
| 212 | size_t i; |
| 213 | |
| 214 | test_init (); |
| 215 | |
| 216 | printf ("%23s", ""); |
| 217 | FOR_EACH_IMPL (impl, 0) |
| 218 | printf ("\t%s", impl->name); |
| 219 | putchar ('\n'); |
| 220 | |
| 221 | for (i = 0; i < 18; ++i) |
| 222 | { |
| 223 | do_test (0, 0, 1 << i); |
| 224 | do_test (i, 0, 1 << i); |
| 225 | do_test (0, i, 1 << i); |
| 226 | do_test (i, i, 1 << i); |
| 227 | } |
| 228 | |
| 229 | for (i = 0; i < 32; ++i) |
| 230 | { |
| 231 | do_test (0, 0, i); |
| 232 | do_test (i, 0, i); |
| 233 | do_test (0, i, i); |
| 234 | do_test (i, i, i); |
| 235 | } |
| 236 | |
| 237 | for (i = 3; i < 32; ++i) |
| 238 | { |
| 239 | if ((i & (i - 1)) == 0) |
| 240 | continue; |
| 241 | do_test (0, 0, 16 * i); |
| 242 | do_test (i, 0, 16 * i); |
| 243 | do_test (0, i, 16 * i); |
| 244 | do_test (i, i, 16 * i); |
| 245 | } |
| 246 | |
| 247 | do_test (0, 0, getpagesize ()); |
| 248 | |
| 249 | do_random_tests (); |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | #include "../test-skeleton.c" |