test-memcpy-support.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* Support for testing and measuring memcpy functions.
  2. Copyright (C) 1999-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* This fail contains the actual memcpy test functions. It is included
  16. in test-memcpy.c and test-memcpy-large.c. They are split because
  17. the tests take a long time to run and splitting them allows for
  18. simpler parallel testing. */
  19. #ifndef MEMCPY_RESULT
  20. #define MEMCPY_RESULT(dst, len) dst
  21. #define MIN_PAGE_SIZE 131072
  22. #define TEST_MAIN
  23. #define TEST_NAME "memcpy"
  24. #define TIMEOUT (8 * 60)
  25. #include "test-string.h"
  26. IMPL (memcpy, 1)
  27. /* Naive implementation to verify results. */
  28. char *
  29. simple_memcpy (char *dst, const char *src, size_t n)
  30. {
  31. char *ret = dst;
  32. while (n--)
  33. *dst++ = *src++;
  34. return ret;
  35. }
  36. #endif
  37. typedef char *(*proto_t) (char *, const char *, size_t);
  38. typedef uint32_t __attribute__ ((may_alias, aligned (1))) unaligned_uint32_t;
  39. static void
  40. do_one_test (impl_t *impl, char *dst, const char *src, size_t len)
  41. {
  42. size_t i;
  43. /* Must clear the destination buffer set by the previous run. */
  44. for (i = 0; i < len; i++)
  45. dst[i] = 0;
  46. if (CALL (impl, dst, src, len) != MEMCPY_RESULT (dst, len))
  47. {
  48. error (0, 0, "Wrong result in function %s %p %p", impl->name,
  49. CALL (impl, dst, src, len), MEMCPY_RESULT (dst, len));
  50. ret = 1;
  51. return;
  52. }
  53. if (memcmp (dst, src, len) != 0)
  54. {
  55. error (0, 0,
  56. "Wrong result in function %s dst %p \"%.*s\" src %p \"%.*s\" len "
  57. "%zu",
  58. impl->name, dst, (int)len, dst, src, (int)len, src, len);
  59. ret = 1;
  60. return;
  61. }
  62. }
  63. static void
  64. do_test (size_t align1, size_t align2, size_t len)
  65. {
  66. size_t i, j;
  67. char *s1, *s2;
  68. align1 &= 4095;
  69. if (align1 + len >= page_size)
  70. return;
  71. align2 &= 4095;
  72. if (align2 + len >= page_size)
  73. return;
  74. s1 = (char *)(buf1 + align1);
  75. s2 = (char *)(buf2 + align2);
  76. for (size_t repeats = 0; repeats < 2; ++repeats)
  77. {
  78. for (i = 0, j = 1; i < len; i++, j += 23)
  79. s1[i] = j;
  80. FOR_EACH_IMPL (impl, 0)
  81. do_one_test (impl, s2, s1, len);
  82. s1 = (char *) (buf2 + align1);
  83. s2 = (char *) (buf1 + align2);
  84. }
  85. }
  86. static void
  87. do_test1 (size_t align1, size_t align2, size_t size)
  88. {
  89. void *large_buf;
  90. size_t mmap_size, region_size;
  91. align1 &= (page_size - 1);
  92. if (align1 == 0)
  93. align1 = page_size;
  94. align2 &= (page_size - 1);
  95. if (align2 == 0)
  96. align2 = page_size;
  97. region_size = (size + page_size - 1) & (~(page_size - 1));
  98. mmap_size = region_size * 2 + 3 * page_size;
  99. large_buf = mmap (NULL, mmap_size, PROT_READ | PROT_WRITE,
  100. MAP_PRIVATE | MAP_ANON, -1, 0);
  101. if (large_buf == MAP_FAILED)
  102. {
  103. puts ("Failed to allocate large_buf, skipping do_test");
  104. return;
  105. }
  106. if (mprotect (large_buf + region_size + page_size, page_size, PROT_NONE))
  107. error (EXIT_FAILURE, errno, "mprotect failed");
  108. size_t array_size = size / sizeof (uint32_t);
  109. unaligned_uint32_t *dest = large_buf + align1;
  110. unaligned_uint32_t *src = large_buf + region_size + 2 * page_size + align2;
  111. size_t i;
  112. for (size_t repeats = 0; repeats < 2; repeats++)
  113. {
  114. for (i = 0; i < array_size; i++)
  115. src[i] = (uint32_t)i;
  116. FOR_EACH_IMPL (impl, 0)
  117. {
  118. memset (dest, -1, size);
  119. CALL (impl, (char *)dest, (char *)src, size);
  120. if (memcmp (src, dest, size))
  121. {
  122. for (i = 0; i < array_size; i++)
  123. if (dest[i] != src[i])
  124. {
  125. error (0, 0,
  126. "Wrong result in function %s dst \"%p\" src \"%p\" "
  127. "offset \"%zd\"",
  128. impl->name, dest, src, i);
  129. ret = 1;
  130. munmap ((void *)large_buf, mmap_size);
  131. return;
  132. }
  133. }
  134. }
  135. dest = large_buf + region_size + 2 * page_size + align1;
  136. src = large_buf + align2;
  137. }
  138. munmap ((void *)large_buf, mmap_size);
  139. }