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