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