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