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