1 // SPDX-License-Identifier: GPL-2.0 2 #include <malloc.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include <sys/mman.h> 6 #include <time.h> 7 #include "utils.h" 8 9 #define SIZE 256 10 #define ITERATIONS 10000 11 12 #define LARGE_SIZE (5 * 1024) 13 #define LARGE_ITERATIONS 1000 14 #define LARGE_MAX_OFFSET 32 15 #define LARGE_SIZE_START 4096 16 17 /* This is big enough to fit LARGE_SIZE and works on 4K & 64K kernels */ 18 #define MAP_SIZE (64 * 1024) 19 20 #define MAX_OFFSET_DIFF_S1_S2 48 21 22 int vmx_count; 23 int enter_vmx_ops(void) 24 { 25 vmx_count++; 26 return 1; 27 } 28 29 void exit_vmx_ops(void) 30 { 31 vmx_count--; 32 } 33 int test_memcmp(const void *s1, const void *s2, size_t n); 34 35 /* test all offsets and lengths */ 36 static void test_one(char *s1, char *s2, unsigned long max_offset, 37 unsigned long size_start, unsigned long max_size) 38 { 39 unsigned long offset, size; 40 41 for (offset = 0; offset < max_offset; offset++) { 42 for (size = size_start; size < (max_size - offset); size++) { 43 int x, y; 44 unsigned long i; 45 46 y = memcmp(s1+offset, s2+offset, size); 47 x = test_memcmp(s1+offset, s2+offset, size); 48 49 if (((x ^ y) < 0) && /* Trick to compare sign */ 50 ((x | y) != 0)) { /* check for zero */ 51 printf("memcmp returned %d, should have returned %d (offset %ld size %ld)\n", x, y, offset, size); 52 53 for (i = offset; i < offset+size; i++) 54 printf("%02x ", s1[i]); 55 printf("\n"); 56 57 for (i = offset; i < offset+size; i++) 58 printf("%02x ", s2[i]); 59 printf("\n"); 60 abort(); 61 } 62 63 if (vmx_count != 0) { 64 printf("vmx enter/exit not paired.(offset:%ld size:%ld s1:%p s2:%p vc:%d\n", 65 offset, size, s1, s2, vmx_count); 66 printf("\n"); 67 abort(); 68 } 69 } 70 } 71 } 72 73 static int testcase(bool islarge) 74 { 75 unsigned long i, comp_size, alloc_size; 76 char *p, *s1, *s2; 77 int iterations; 78 79 comp_size = (islarge ? LARGE_SIZE : SIZE); 80 alloc_size = comp_size + MAX_OFFSET_DIFF_S1_S2; 81 iterations = islarge ? LARGE_ITERATIONS : ITERATIONS; 82 83 p = mmap(NULL, 4 * MAP_SIZE, PROT_READ | PROT_WRITE, 84 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 85 FAIL_IF(p == MAP_FAILED); 86 87 /* Put s1/s2 at the end of a page */ 88 s1 = p + MAP_SIZE - alloc_size; 89 s2 = p + 3 * MAP_SIZE - alloc_size; 90 91 /* And unmap the subsequent page to force a fault if we overread */ 92 munmap(p + MAP_SIZE, MAP_SIZE); 93 munmap(p + 3 * MAP_SIZE, MAP_SIZE); 94 95 srandom(time(0)); 96 97 for (i = 0; i < iterations; i++) { 98 unsigned long j; 99 unsigned long change; 100 char *rand_s1 = s1; 101 char *rand_s2 = s2; 102 103 for (j = 0; j < alloc_size; j++) 104 s1[j] = random(); 105 106 rand_s1 += random() % MAX_OFFSET_DIFF_S1_S2; 107 rand_s2 += random() % MAX_OFFSET_DIFF_S1_S2; 108 memcpy(rand_s2, rand_s1, comp_size); 109 110 /* change one byte */ 111 change = random() % comp_size; 112 rand_s2[change] = random() & 0xff; 113 114 if (islarge) 115 test_one(rand_s1, rand_s2, LARGE_MAX_OFFSET, 116 LARGE_SIZE_START, comp_size); 117 else 118 test_one(rand_s1, rand_s2, SIZE, 0, comp_size); 119 } 120 121 srandom(time(0)); 122 123 for (i = 0; i < iterations; i++) { 124 unsigned long j; 125 unsigned long change; 126 char *rand_s1 = s1; 127 char *rand_s2 = s2; 128 129 for (j = 0; j < alloc_size; j++) 130 s1[j] = random(); 131 132 rand_s1 += random() % MAX_OFFSET_DIFF_S1_S2; 133 rand_s2 += random() % MAX_OFFSET_DIFF_S1_S2; 134 memcpy(rand_s2, rand_s1, comp_size); 135 136 /* change multiple bytes, 1/8 of total */ 137 for (j = 0; j < comp_size / 8; j++) { 138 change = random() % comp_size; 139 s2[change] = random() & 0xff; 140 } 141 142 if (islarge) 143 test_one(rand_s1, rand_s2, LARGE_MAX_OFFSET, 144 LARGE_SIZE_START, comp_size); 145 else 146 test_one(rand_s1, rand_s2, SIZE, 0, comp_size); 147 } 148 149 return 0; 150 } 151 152 static int testcases(void) 153 { 154 testcase(0); 155 testcase(1); 156 return 0; 157 } 158 159 int main(void) 160 { 161 test_harness_set_timeout(300); 162 return test_harness(testcases, "memcmp"); 163 } 164