memcmp.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include "utils.h"
  7. #define SIZE 256
  8. #define ITERATIONS 10000
  9. #define LARGE_SIZE (5 * 1024)
  10. #define LARGE_ITERATIONS 1000
  11. #define LARGE_MAX_OFFSET 32
  12. #define LARGE_SIZE_START 4096
  13. #define MAX_OFFSET_DIFF_S1_S2 48
  14. int vmx_count;
  15. int enter_vmx_ops(void)
  16. {
  17. vmx_count++;
  18. return 1;
  19. }
  20. void exit_vmx_ops(void)
  21. {
  22. vmx_count--;
  23. }
  24. int test_memcmp(const void *s1, const void *s2, size_t n);
  25. /* test all offsets and lengths */
  26. static void test_one(char *s1, char *s2, unsigned long max_offset,
  27. unsigned long size_start, unsigned long max_size)
  28. {
  29. unsigned long offset, size;
  30. for (offset = 0; offset < max_offset; offset++) {
  31. for (size = size_start; size < (max_size - offset); size++) {
  32. int x, y;
  33. unsigned long i;
  34. y = memcmp(s1+offset, s2+offset, size);
  35. x = test_memcmp(s1+offset, s2+offset, size);
  36. if (((x ^ y) < 0) && /* Trick to compare sign */
  37. ((x | y) != 0)) { /* check for zero */
  38. printf("memcmp returned %d, should have returned %d (offset %ld size %ld)\n", x, y, offset, size);
  39. for (i = offset; i < offset+size; i++)
  40. printf("%02x ", s1[i]);
  41. printf("\n");
  42. for (i = offset; i < offset+size; i++)
  43. printf("%02x ", s2[i]);
  44. printf("\n");
  45. abort();
  46. }
  47. if (vmx_count != 0) {
  48. printf("vmx enter/exit not paired.(offset:%ld size:%ld s1:%p s2:%p vc:%d\n",
  49. offset, size, s1, s2, vmx_count);
  50. printf("\n");
  51. abort();
  52. }
  53. }
  54. }
  55. }
  56. static int testcase(bool islarge)
  57. {
  58. char *s1;
  59. char *s2;
  60. unsigned long i;
  61. unsigned long comp_size = (islarge ? LARGE_SIZE : SIZE);
  62. unsigned long alloc_size = comp_size + MAX_OFFSET_DIFF_S1_S2;
  63. int iterations = islarge ? LARGE_ITERATIONS : ITERATIONS;
  64. s1 = memalign(128, alloc_size);
  65. if (!s1) {
  66. perror("memalign");
  67. exit(1);
  68. }
  69. s2 = memalign(128, alloc_size);
  70. if (!s2) {
  71. perror("memalign");
  72. exit(1);
  73. }
  74. srandom(time(0));
  75. for (i = 0; i < iterations; i++) {
  76. unsigned long j;
  77. unsigned long change;
  78. char *rand_s1 = s1;
  79. char *rand_s2 = s2;
  80. for (j = 0; j < alloc_size; j++)
  81. s1[j] = random();
  82. rand_s1 += random() % MAX_OFFSET_DIFF_S1_S2;
  83. rand_s2 += random() % MAX_OFFSET_DIFF_S1_S2;
  84. memcpy(rand_s2, rand_s1, comp_size);
  85. /* change one byte */
  86. change = random() % comp_size;
  87. rand_s2[change] = random() & 0xff;
  88. if (islarge)
  89. test_one(rand_s1, rand_s2, LARGE_MAX_OFFSET,
  90. LARGE_SIZE_START, comp_size);
  91. else
  92. test_one(rand_s1, rand_s2, SIZE, 0, comp_size);
  93. }
  94. srandom(time(0));
  95. for (i = 0; i < iterations; i++) {
  96. unsigned long j;
  97. unsigned long change;
  98. char *rand_s1 = s1;
  99. char *rand_s2 = s2;
  100. for (j = 0; j < alloc_size; j++)
  101. s1[j] = random();
  102. rand_s1 += random() % MAX_OFFSET_DIFF_S1_S2;
  103. rand_s2 += random() % MAX_OFFSET_DIFF_S1_S2;
  104. memcpy(rand_s2, rand_s1, comp_size);
  105. /* change multiple bytes, 1/8 of total */
  106. for (j = 0; j < comp_size / 8; j++) {
  107. change = random() % comp_size;
  108. s2[change] = random() & 0xff;
  109. }
  110. if (islarge)
  111. test_one(rand_s1, rand_s2, LARGE_MAX_OFFSET,
  112. LARGE_SIZE_START, comp_size);
  113. else
  114. test_one(rand_s1, rand_s2, SIZE, 0, comp_size);
  115. }
  116. return 0;
  117. }
  118. static int testcases(void)
  119. {
  120. testcase(0);
  121. testcase(1);
  122. return 0;
  123. }
  124. int main(void)
  125. {
  126. test_harness_set_timeout(300);
  127. return test_harness(testcases, "memcmp");
  128. }