memcpy-1.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* This program is free software; you can redistribute it and/or modify
  2. it under the terms of the GNU General Public License as published by
  3. the Free Software Foundation; either version 2 of the License, or
  4. (at your option) any later version.
  5. This program is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  8. General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  12. 02110-1301, USA. */
  13. /* Verify memcpy operation. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <sys/mman.h>
  18. #include <libitm.h>
  19. #define BEG_TRANSACTION \
  20. _ITM_beginTransaction (pr_instrumentedCode | pr_hasNoAbort \
  21. | pr_hasNoIrrevocable)
  22. #define END_TRANSACTION \
  23. _ITM_commitTransaction ()
  24. #define MEMCPY _ITM_memcpyRtWt
  25. static unsigned char *buf1, *buf2;
  26. static size_t bufsize, page_size;
  27. static int fail;
  28. int getpagesize (void);
  29. static void
  30. do_test (size_t align1, size_t align2, size_t len)
  31. {
  32. size_t i, j;
  33. unsigned char *s1, *s2;
  34. unsigned char c1, c2;
  35. if (align1 + len >= bufsize)
  36. return;
  37. if (align2 + len >= bufsize)
  38. return;
  39. c1 = random () >> 8;
  40. c2 = random () >> 8;
  41. memset (buf1, c1, bufsize);
  42. memset (buf2, c2, bufsize);
  43. s1 = buf1 + align1;
  44. s2 = buf2 + align2;
  45. for (i = 0, j = 1; i < len; i++, j += 23)
  46. s1[i] = (j == c1 ? j + 1 : j);
  47. BEG_TRANSACTION;
  48. MEMCPY (s2, s1, len);
  49. END_TRANSACTION;
  50. if (memcmp (s1, s2, len) != 0)
  51. {
  52. printf ("Wrong result: dalign %zd salign %zd len %zd\n",
  53. align2, align1, len);
  54. fail = 1;
  55. return;
  56. }
  57. for (i = (align2 > 64 ? align2 - 64 : 0); i < align2; ++i)
  58. if (buf2[i] != c2)
  59. {
  60. printf ("Garbage before: ofs %zd\n", i);
  61. fail = 1;
  62. break;
  63. }
  64. for (i = align2 + len, j = i+64 < bufsize ? i+64 : bufsize; i < j; ++i)
  65. if (buf2[i] != c2)
  66. {
  67. printf ("Garbage after: ofs %zd\n", i);
  68. fail = 1;
  69. break;
  70. }
  71. }
  72. #ifndef MAP_ANONYMOUS
  73. # ifdef MAP_ANON
  74. # define MAP_ANONYMOUS MAP_ANON
  75. # endif
  76. #endif
  77. int main()
  78. {
  79. size_t i, j;
  80. page_size = getpagesize ();
  81. bufsize = 2 * page_size;
  82. buf1 = mmap (NULL, bufsize + 2*page_size, PROT_READ | PROT_WRITE,
  83. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  84. if (buf1 == MAP_FAILED)
  85. return 1;
  86. buf2 = mmap (NULL, bufsize + 2*page_size, PROT_READ | PROT_WRITE,
  87. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  88. if (buf2 == MAP_FAILED)
  89. return 1;
  90. if (mprotect (buf1, page_size, PROT_NONE))
  91. return 1;
  92. buf1 += page_size;
  93. if (mprotect (buf1 + bufsize, page_size, PROT_NONE))
  94. return 1;
  95. if (mprotect (buf2, page_size, PROT_NONE))
  96. return 1;
  97. buf2 += page_size;
  98. if (mprotect (buf2 + bufsize, page_size, PROT_NONE))
  99. return 1;
  100. for (i = 0; i < 18; ++i)
  101. {
  102. size_t len = 1 << i;
  103. do_test (0, 0, len);
  104. do_test (i, 0, len);
  105. do_test (0, i, len);
  106. do_test (i, i, len);
  107. do_test (0, bufsize - len, len);
  108. do_test (bufsize - len, 0, len);
  109. do_test (i, bufsize - len, len);
  110. do_test (bufsize - len, i, len);
  111. }
  112. for (i = 0; i < 32; ++i)
  113. {
  114. do_test (i, 0, i);
  115. do_test (0, i, i);
  116. do_test (i, i, i);
  117. for (j = 0; j < 32; ++j)
  118. {
  119. do_test (i, bufsize - i - j, i);
  120. do_test (bufsize - i - j, i, i);
  121. }
  122. }
  123. for (i = 3; i < 32; ++i)
  124. {
  125. if ((i & (i - 1)) == 0)
  126. continue;
  127. do_test (0, 0, 16 * i);
  128. do_test (i, 0, 16 * i);
  129. do_test (0, i, 16 * i);
  130. do_test (i, i, 16 * i);
  131. }
  132. return fail;
  133. }