1*c54f35caSApple OSS Distributions #include <darwintest.h>
2*c54f35caSApple OSS Distributions #include <mach/mach.h>
3*c54f35caSApple OSS Distributions #include <sys/sysctl.h>
4*c54f35caSApple OSS Distributions #include <stdio.h>
5*c54f35caSApple OSS Distributions #include <stdbool.h>
6*c54f35caSApple OSS Distributions #include <stdlib.h>
7*c54f35caSApple OSS Distributions #include <unistd.h>
8*c54f35caSApple OSS Distributions #include <inttypes.h>
9*c54f35caSApple OSS Distributions #include <pthread.h>
10*c54f35caSApple OSS Distributions #include <TargetConditionals.h>
11*c54f35caSApple OSS Distributions #include "excserver.h"
12*c54f35caSApple OSS Distributions #include "exc_helpers.h"
13*c54f35caSApple OSS Distributions
14*c54f35caSApple OSS Distributions extern int pid_hibernate(int pid);
15*c54f35caSApple OSS Distributions
16*c54f35caSApple OSS Distributions static vm_address_t page_size;
17*c54f35caSApple OSS Distributions
18*c54f35caSApple OSS Distributions T_GLOBAL_META(
19*c54f35caSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"),
20*c54f35caSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("arm"),
21*c54f35caSApple OSS Distributions T_META_OWNER("peter_newman"),
22*c54f35caSApple OSS Distributions T_META_REQUIRES_SYSCTL_EQ("hw.optional.wkdm_popcount", 1)
23*c54f35caSApple OSS Distributions );
24*c54f35caSApple OSS Distributions
25*c54f35caSApple OSS Distributions static vm_address_t *blocks;
26*c54f35caSApple OSS Distributions static uint64_t block_count;
27*c54f35caSApple OSS Distributions static const uint64_t block_length = 0x800000;
28*c54f35caSApple OSS Distributions
29*c54f35caSApple OSS Distributions static uint32_t vm_pagesize;
30*c54f35caSApple OSS Distributions
31*c54f35caSApple OSS Distributions static void
dirty_page(const vm_address_t address)32*c54f35caSApple OSS Distributions dirty_page(const vm_address_t address)
33*c54f35caSApple OSS Distributions {
34*c54f35caSApple OSS Distributions assert((address & (page_size - 1)) == 0UL);
35*c54f35caSApple OSS Distributions uint32_t *const page_as_u32 = (uint32_t *)address;
36*c54f35caSApple OSS Distributions for (uint32_t i = 0; i < page_size / sizeof(uint32_t); i += 2) {
37*c54f35caSApple OSS Distributions page_as_u32[i + 0] = i % 4;
38*c54f35caSApple OSS Distributions page_as_u32[i + 1] = 0xcdcdcdcd;
39*c54f35caSApple OSS Distributions }
40*c54f35caSApple OSS Distributions }
41*c54f35caSApple OSS Distributions
42*c54f35caSApple OSS Distributions static bool
try_to_corrupt_page(vm_address_t page_va)43*c54f35caSApple OSS Distributions try_to_corrupt_page(vm_address_t page_va)
44*c54f35caSApple OSS Distributions {
45*c54f35caSApple OSS Distributions int val;
46*c54f35caSApple OSS Distributions size_t size = sizeof(val);
47*c54f35caSApple OSS Distributions int result = sysctlbyname("vm.compressor_inject_error", &val, &size,
48*c54f35caSApple OSS Distributions &page_va, sizeof(page_va));
49*c54f35caSApple OSS Distributions return result == 0;
50*c54f35caSApple OSS Distributions }
51*c54f35caSApple OSS Distributions
52*c54f35caSApple OSS Distributions static void
create_corrupted_regions(void)53*c54f35caSApple OSS Distributions create_corrupted_regions(void)
54*c54f35caSApple OSS Distributions {
55*c54f35caSApple OSS Distributions uint64_t hw_memsize;
56*c54f35caSApple OSS Distributions
57*c54f35caSApple OSS Distributions size_t size = sizeof(unsigned int);
58*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("vm.pagesize", &vm_pagesize, &size,
59*c54f35caSApple OSS Distributions NULL, 0), "read vm.pagesize");
60*c54f35caSApple OSS Distributions size = sizeof(uint64_t);
61*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("hw.memsize", &hw_memsize, &size,
62*c54f35caSApple OSS Distributions NULL, 0), "read hw.memsize");
63*c54f35caSApple OSS Distributions
64*c54f35caSApple OSS Distributions #if TARGET_OS_OSX
65*c54f35caSApple OSS Distributions const uint64_t max_memsize = 32ULL * 0x40000000ULL; // 32 GB
66*c54f35caSApple OSS Distributions #else
67*c54f35caSApple OSS Distributions const uint64_t max_memsize = 8ULL * 0x100000ULL; // 8 MB
68*c54f35caSApple OSS Distributions #endif
69*c54f35caSApple OSS Distributions const uint64_t effective_memsize = (hw_memsize > max_memsize) ?
70*c54f35caSApple OSS Distributions max_memsize : hw_memsize;
71*c54f35caSApple OSS Distributions
72*c54f35caSApple OSS Distributions const uint64_t total_pages = effective_memsize / vm_pagesize;
73*c54f35caSApple OSS Distributions const uint64_t pages_per_block = block_length / vm_pagesize;
74*c54f35caSApple OSS Distributions
75*c54f35caSApple OSS Distributions // Map a as much memory as we have physical memory to back. Dirtying all
76*c54f35caSApple OSS Distributions // of these pages will force a compressor sweep. The mapping is done using
77*c54f35caSApple OSS Distributions // the smallest number of malloc() calls to allocate the necessary VAs.
78*c54f35caSApple OSS Distributions block_count = total_pages / pages_per_block;
79*c54f35caSApple OSS Distributions
80*c54f35caSApple OSS Distributions blocks = (vm_address_t *)malloc(sizeof(*blocks) * block_count);
81*c54f35caSApple OSS Distributions for (uint64_t i = 0; i < block_count; i++) {
82*c54f35caSApple OSS Distributions void *bufferp = malloc(block_length);
83*c54f35caSApple OSS Distributions blocks[i] = (vm_address_t)bufferp;
84*c54f35caSApple OSS Distributions }
85*c54f35caSApple OSS Distributions
86*c54f35caSApple OSS Distributions for (uint32_t i = 0; i < block_count; i++) {
87*c54f35caSApple OSS Distributions for (size_t buffer_offset = 0; buffer_offset < block_length;
88*c54f35caSApple OSS Distributions buffer_offset += vm_pagesize) {
89*c54f35caSApple OSS Distributions dirty_page(blocks[i] + buffer_offset);
90*c54f35caSApple OSS Distributions }
91*c54f35caSApple OSS Distributions }
92*c54f35caSApple OSS Distributions
93*c54f35caSApple OSS Distributions #if !TARGET_OS_OSX
94*c54f35caSApple OSS Distributions // We can't use a substantial amount of memory on embedded platforms, so
95*c54f35caSApple OSS Distributions // freeze the current process instead to cause everything to be compressed.
96*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(pid_hibernate(-2), NULL);
97*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(pid_hibernate(-2), NULL);
98*c54f35caSApple OSS Distributions #endif
99*c54f35caSApple OSS Distributions
100*c54f35caSApple OSS Distributions uint32_t corrupt = 0;
101*c54f35caSApple OSS Distributions for (uint32_t i = 0; i < block_count; i++) {
102*c54f35caSApple OSS Distributions for (size_t buffer_offset = 0; buffer_offset < block_length;
103*c54f35caSApple OSS Distributions buffer_offset += vm_pagesize) {
104*c54f35caSApple OSS Distributions if (try_to_corrupt_page(blocks[i] + buffer_offset)) {
105*c54f35caSApple OSS Distributions corrupt++;
106*c54f35caSApple OSS Distributions }
107*c54f35caSApple OSS Distributions }
108*c54f35caSApple OSS Distributions }
109*c54f35caSApple OSS Distributions
110*c54f35caSApple OSS Distributions T_LOG("corrupted %u/%llu pages. accessing...\n", corrupt, total_pages);
111*c54f35caSApple OSS Distributions if (corrupt == 0) {
112*c54f35caSApple OSS Distributions T_SKIP("no pages corrupted");
113*c54f35caSApple OSS Distributions }
114*c54f35caSApple OSS Distributions }
115*c54f35caSApple OSS Distributions
116*c54f35caSApple OSS Distributions static bool
try_write(volatile uint32_t * word __unused)117*c54f35caSApple OSS Distributions try_write(volatile uint32_t *word __unused)
118*c54f35caSApple OSS Distributions {
119*c54f35caSApple OSS Distributions #ifdef __arm64__
120*c54f35caSApple OSS Distributions uint64_t val = 1;
121*c54f35caSApple OSS Distributions __asm__ volatile (
122*c54f35caSApple OSS Distributions "str %w0, %1\n"
123*c54f35caSApple OSS Distributions "mov %0, 0\n"
124*c54f35caSApple OSS Distributions : "+r"(val) : "m"(*word));
125*c54f35caSApple OSS Distributions // The exception handler skips over the instruction that zeroes val when a
126*c54f35caSApple OSS Distributions // decompression failure is detected.
127*c54f35caSApple OSS Distributions return val == 0;
128*c54f35caSApple OSS Distributions #else
129*c54f35caSApple OSS Distributions return false;
130*c54f35caSApple OSS Distributions #endif
131*c54f35caSApple OSS Distributions }
132*c54f35caSApple OSS Distributions
133*c54f35caSApple OSS Distributions static bool
read_blocks(void)134*c54f35caSApple OSS Distributions read_blocks(void)
135*c54f35caSApple OSS Distributions {
136*c54f35caSApple OSS Distributions for (uint32_t i = 0; i < block_count; i++) {
137*c54f35caSApple OSS Distributions for (size_t buffer_offset = 0; buffer_offset < block_length;
138*c54f35caSApple OSS Distributions buffer_offset += vm_pagesize) {
139*c54f35caSApple OSS Distributions // Access pages until the fault is detected.
140*c54f35caSApple OSS Distributions if (!try_write((volatile uint32_t *)(blocks[i] + buffer_offset))) {
141*c54f35caSApple OSS Distributions T_LOG("test_thread breaking");
142*c54f35caSApple OSS Distributions return true;
143*c54f35caSApple OSS Distributions }
144*c54f35caSApple OSS Distributions }
145*c54f35caSApple OSS Distributions }
146*c54f35caSApple OSS Distributions return false;
147*c54f35caSApple OSS Distributions }
148*c54f35caSApple OSS Distributions
149*c54f35caSApple OSS Distributions static size_t
kern_memory_failure_handler(__unused mach_port_t task,__unused mach_port_t thread,exception_type_t exception,mach_exception_data_t code)150*c54f35caSApple OSS Distributions kern_memory_failure_handler(
151*c54f35caSApple OSS Distributions __unused mach_port_t task,
152*c54f35caSApple OSS Distributions __unused mach_port_t thread,
153*c54f35caSApple OSS Distributions exception_type_t exception,
154*c54f35caSApple OSS Distributions mach_exception_data_t code)
155*c54f35caSApple OSS Distributions {
156*c54f35caSApple OSS Distributions T_EXPECT_EQ(exception, EXC_BAD_ACCESS,
157*c54f35caSApple OSS Distributions "Verified bad address exception");
158*c54f35caSApple OSS Distributions T_EXPECT_EQ((int)code[0], KERN_MEMORY_FAILURE, "caught KERN_MEMORY_FAILURE");
159*c54f35caSApple OSS Distributions T_PASS("received KERN_MEMORY_FAILURE from test thread");
160*c54f35caSApple OSS Distributions // Skip the next instruction as well so that the faulting code can detect
161*c54f35caSApple OSS Distributions // the exception.
162*c54f35caSApple OSS Distributions return 8;
163*c54f35caSApple OSS Distributions }
164*c54f35caSApple OSS Distributions
165*c54f35caSApple OSS Distributions T_DECL(decompression_failure,
166*c54f35caSApple OSS Distributions "Confirm that exception is raised on decompression failure",
167*c54f35caSApple OSS Distributions // Disable software checks in development builds, as these would result in
168*c54f35caSApple OSS Distributions // panics.
169*c54f35caSApple OSS Distributions T_META_BOOTARGS_SET("vm_compressor_validation=0"),
170*c54f35caSApple OSS Distributions T_META_ASROOT(true),
171*c54f35caSApple OSS Distributions // This test intentionally corrupts pages backing heap memory, so it's
172*c54f35caSApple OSS Distributions // not practical for it to release all the buffers properly.
173*c54f35caSApple OSS Distributions T_META_CHECK_LEAKS(false))
174*c54f35caSApple OSS Distributions {
175*c54f35caSApple OSS Distributions T_SETUPBEGIN;
176*c54f35caSApple OSS Distributions
177*c54f35caSApple OSS Distributions #if !TARGET_OS_OSX
178*c54f35caSApple OSS Distributions if (pid_hibernate(-2) != 0) {
179*c54f35caSApple OSS Distributions T_SKIP("compressor not active");
180*c54f35caSApple OSS Distributions }
181*c54f35caSApple OSS Distributions #endif
182*c54f35caSApple OSS Distributions
183*c54f35caSApple OSS Distributions int value;
184*c54f35caSApple OSS Distributions size_t size = sizeof(value);
185*c54f35caSApple OSS Distributions if (sysctlbyname("vm.compressor_inject_error", &value, &size, NULL, 0)
186*c54f35caSApple OSS Distributions != 0) {
187*c54f35caSApple OSS Distributions T_SKIP("vm.compressor_inject_error not present");
188*c54f35caSApple OSS Distributions }
189*c54f35caSApple OSS Distributions
190*c54f35caSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("vm.pagesize", &value, &size, NULL, 0),
191*c54f35caSApple OSS Distributions NULL);
192*c54f35caSApple OSS Distributions T_ASSERT_EQ_ULONG(size, sizeof(value), NULL);
193*c54f35caSApple OSS Distributions page_size = (vm_address_t)value;
194*c54f35caSApple OSS Distributions
195*c54f35caSApple OSS Distributions mach_port_t exc_port = create_exception_port(EXC_MASK_BAD_ACCESS);
196*c54f35caSApple OSS Distributions create_corrupted_regions();
197*c54f35caSApple OSS Distributions T_SETUPEND;
198*c54f35caSApple OSS Distributions
199*c54f35caSApple OSS Distributions run_exception_handler(exc_port, kern_memory_failure_handler);
200*c54f35caSApple OSS Distributions
201*c54f35caSApple OSS Distributions if (!read_blocks()) {
202*c54f35caSApple OSS Distributions T_SKIP("no faults");
203*c54f35caSApple OSS Distributions }
204*c54f35caSApple OSS Distributions }
205