xref: /xnu-8796.141.3/tests/memcmp_zero.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <darwintest.h>
2*1b191cb5SApple OSS Distributions #include <stdio.h>
3*1b191cb5SApple OSS Distributions #include <stdlib.h>
4*1b191cb5SApple OSS Distributions #include <string.h>
5*1b191cb5SApple OSS Distributions #include <sys/mman.h>
6*1b191cb5SApple OSS Distributions #include <mach/machine/vm_param.h>
7*1b191cb5SApple OSS Distributions 
8*1b191cb5SApple OSS Distributions static inline unsigned char *
get_guarded_page(void)9*1b191cb5SApple OSS Distributions get_guarded_page(void)
10*1b191cb5SApple OSS Distributions {
11*1b191cb5SApple OSS Distributions 	unsigned char *p = mmap(NULL, 3 * PAGE_SIZE, PROT_NONE, MAP_SHARED | MAP_ANON, 0, 0);
12*1b191cb5SApple OSS Distributions 	p += PAGE_SIZE;
13*1b191cb5SApple OSS Distributions 	mprotect(p, PAGE_SIZE, PROT_READ | PROT_WRITE);
14*1b191cb5SApple OSS Distributions 	return p;
15*1b191cb5SApple OSS Distributions }
16*1b191cb5SApple OSS Distributions 
17*1b191cb5SApple OSS Distributions static inline void
free_guarded_page(unsigned char * p)18*1b191cb5SApple OSS Distributions free_guarded_page(unsigned char *p)
19*1b191cb5SApple OSS Distributions {
20*1b191cb5SApple OSS Distributions 	munmap(p - PAGE_SIZE, 3 * PAGE_SIZE);
21*1b191cb5SApple OSS Distributions }
22*1b191cb5SApple OSS Distributions 
23*1b191cb5SApple OSS Distributions /* memcmp_zero_ptr_aligned() checks string s of n bytes contains all zeros.
24*1b191cb5SApple OSS Distributions  * Address and size of the string s must be pointer-aligned.
25*1b191cb5SApple OSS Distributions  * Return 0 if true, 1 otherwise. Also return 0 if n is 0.
26*1b191cb5SApple OSS Distributions  */
27*1b191cb5SApple OSS Distributions extern int
28*1b191cb5SApple OSS Distributions memcmp_zero_ptr_aligned(const void *s, size_t n);
29*1b191cb5SApple OSS Distributions 
30*1b191cb5SApple OSS Distributions T_DECL(memcmp_zero, "memcmp_zero")
31*1b191cb5SApple OSS Distributions {
32*1b191cb5SApple OSS Distributions 	// the assembly version is for the kernel and doesn't support arm64_32
33*1b191cb5SApple OSS Distributions #if defined(__arm64__) && __LP64__
34*1b191cb5SApple OSS Distributions 	unsigned char *buffer = get_guarded_page();
35*1b191cb5SApple OSS Distributions 	unsigned char *right = buffer + PAGE_SIZE - 512;
36*1b191cb5SApple OSS Distributions 	const int ptr_size = sizeof(buffer);
37*1b191cb5SApple OSS Distributions 
38*1b191cb5SApple OSS Distributions 	for (size_t i = 0; i < 256; i += ptr_size) {
39*1b191cb5SApple OSS Distributions 		for (size_t j = i; j < 256; ++j) {
40*1b191cb5SApple OSS Distributions 			for (size_t k = 0; k < 256; ++k) {
41*1b191cb5SApple OSS Distributions 				if (k < i) {
42*1b191cb5SApple OSS Distributions 					buffer[k] = (unsigned char)rand();
43*1b191cb5SApple OSS Distributions 				} else if (k < j) {
44*1b191cb5SApple OSS Distributions 					buffer[k] = '\0';
45*1b191cb5SApple OSS Distributions 				} else if (k == j) {
46*1b191cb5SApple OSS Distributions 					do {
47*1b191cb5SApple OSS Distributions 						buffer[k] = (unsigned char)rand();
48*1b191cb5SApple OSS Distributions 					} while (!buffer[k]);
49*1b191cb5SApple OSS Distributions 				} else {
50*1b191cb5SApple OSS Distributions 					buffer[k] = '\0';
51*1b191cb5SApple OSS Distributions 				}
52*1b191cb5SApple OSS Distributions 			}
53*1b191cb5SApple OSS Distributions 			for (size_t m = 0; m < 128; m += ptr_size) {
54*1b191cb5SApple OSS Distributions 				int result = memcmp_zero_ptr_aligned(&buffer[i], m);
55*1b191cb5SApple OSS Distributions 				int ref = j - i < m ? 1 : 0;
56*1b191cb5SApple OSS Distributions 				T_QUIET; T_ASSERT_EQ(result, ref, "expected %d, saw %d\n"
57*1b191cb5SApple OSS Distributions 				    "memcmp_zero_ptr_aligned(buf[%zd], %zd)\n",
58*1b191cb5SApple OSS Distributions 				    ref, result, i, m);
59*1b191cb5SApple OSS Distributions 			}
60*1b191cb5SApple OSS Distributions 
61*1b191cb5SApple OSS Distributions 
62*1b191cb5SApple OSS Distributions 			for (size_t k = 0; k < 256; ++k) {
63*1b191cb5SApple OSS Distributions 				if (k < i) {
64*1b191cb5SApple OSS Distributions 					right[k] = (unsigned char)rand();
65*1b191cb5SApple OSS Distributions 				} else if (k < j) {
66*1b191cb5SApple OSS Distributions 					right[k] = '\0';
67*1b191cb5SApple OSS Distributions 				} else if (k == j) {
68*1b191cb5SApple OSS Distributions 					do {
69*1b191cb5SApple OSS Distributions 						right[k] = (unsigned char)rand();
70*1b191cb5SApple OSS Distributions 					} while (!right[k]);
71*1b191cb5SApple OSS Distributions 				} else {
72*1b191cb5SApple OSS Distributions 					right[k] = '\0';
73*1b191cb5SApple OSS Distributions 				}
74*1b191cb5SApple OSS Distributions 			}
75*1b191cb5SApple OSS Distributions 			for (size_t m = 0; m < 256; m += ptr_size) {
76*1b191cb5SApple OSS Distributions 				int result = memcmp_zero_ptr_aligned(&right[i], m);
77*1b191cb5SApple OSS Distributions 				int ref = j - i < m ? 1 : 0;
78*1b191cb5SApple OSS Distributions 				T_QUIET; T_ASSERT_EQ(result, ref, "expected %d, saw %d\n"
79*1b191cb5SApple OSS Distributions 				    "memcmp_zero_ptr_aligned(buf[%zd], %zd)\n",
80*1b191cb5SApple OSS Distributions 				    ref, result, i, m);
81*1b191cb5SApple OSS Distributions 			}
82*1b191cb5SApple OSS Distributions 		}
83*1b191cb5SApple OSS Distributions 	}
84*1b191cb5SApple OSS Distributions 
85*1b191cb5SApple OSS Distributions 	T_PASS("success");
86*1b191cb5SApple OSS Distributions 
87*1b191cb5SApple OSS Distributions 	free_guarded_page(buffer);
88*1b191cb5SApple OSS Distributions #else
89*1b191cb5SApple OSS Distributions 	T_SKIP("no optimized version to test");
90*1b191cb5SApple OSS Distributions #endif
91*1b191cb5SApple OSS Distributions }
92