]>
git.saurik.com Git - apple/xnu.git/blob - tests/memcmp_zero.c
1 #include <darwintest.h>
6 #include <mach/machine/vm_param.h>
8 static inline unsigned char *
11 unsigned char *p
= mmap(NULL
, 3 * PAGE_SIZE
, PROT_NONE
, MAP_SHARED
| MAP_ANON
, 0, 0);
13 mprotect(p
, PAGE_SIZE
, PROT_READ
| PROT_WRITE
);
18 free_guarded_page(unsigned char *p
)
20 munmap(p
- PAGE_SIZE
, 3 * PAGE_SIZE
);
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.
28 memcmp_zero_ptr_aligned(const void *s
, size_t n
);
30 T_DECL(memcmp_zero
, "memcmp_zero")
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
);
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
) {
42 buffer
[k
] = (unsigned char)rand();
47 buffer
[k
] = (unsigned char)rand();
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",
62 for (size_t k
= 0; k
< 256; ++k
) {
64 right
[k
] = (unsigned char)rand();
69 right
[k
] = (unsigned char)rand();
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",
87 free_guarded_page(buffer
);
89 T_SKIP("no optimized version to test");