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