xref: /xnu-12377.1.9/tests/ecc_test_helper.c (revision f6217f891ac0bb64f3d375211650a4c1ff8ca1ea)
1*f6217f89SApple OSS Distributions #include <errno.h>
2*f6217f89SApple OSS Distributions #include <stdio.h>
3*f6217f89SApple OSS Distributions #include <stdlib.h>
4*f6217f89SApple OSS Distributions #include <math.h>
5*f6217f89SApple OSS Distributions #include <ptrauth.h>
6*f6217f89SApple OSS Distributions #include <string.h>
7*f6217f89SApple OSS Distributions #include <sys/mman.h>
8*f6217f89SApple OSS Distributions #include <sys/sysctl.h>
9*f6217f89SApple OSS Distributions #include <unistd.h>
10*f6217f89SApple OSS Distributions 
11*f6217f89SApple OSS Distributions #include <mach/mach_vm.h>
12*f6217f89SApple OSS Distributions 
13*f6217f89SApple OSS Distributions /*
14*f6217f89SApple OSS Distributions  * ecc_test_helper is a convenience binary to induce various ECC errors
15*f6217f89SApple OSS Distributions  * it's used by ECC-related tests: XNU unit tests and end-2-end coreos-tests
16*f6217f89SApple OSS Distributions  */
17*f6217f89SApple OSS Distributions 
18*f6217f89SApple OSS Distributions 
19*f6217f89SApple OSS Distributions int verbose = 0;
20*f6217f89SApple OSS Distributions #define PRINTF(...) \
21*f6217f89SApple OSS Distributions 	if (verbose) { \
22*f6217f89SApple OSS Distributions 	        printf(__VA_ARGS__); \
23*f6217f89SApple OSS Distributions 	}
24*f6217f89SApple OSS Distributions 
25*f6217f89SApple OSS Distributions __attribute__((noinline))
26*f6217f89SApple OSS Distributions static void
foo(void)27*f6217f89SApple OSS Distributions foo(void)
28*f6217f89SApple OSS Distributions {
29*f6217f89SApple OSS Distributions 	PRINTF("In foo()\n");
30*f6217f89SApple OSS Distributions 	fflush(stdout);
31*f6217f89SApple OSS Distributions }
32*f6217f89SApple OSS Distributions 
33*f6217f89SApple OSS Distributions volatile struct data {
34*f6217f89SApple OSS Distributions 	char buffer1[16 * 1024];
35*f6217f89SApple OSS Distributions 	int big_data[16 * 1024];
36*f6217f89SApple OSS Distributions 	char buffer2[16 * 1024];
37*f6217f89SApple OSS Distributions } x = {
38*f6217f89SApple OSS Distributions 	.big_data = {
39*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
40*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
41*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
42*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
43*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
44*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
45*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
46*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
47*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
48*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
49*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
50*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
51*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
52*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
53*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
54*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
55*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
56*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
57*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
58*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
59*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
60*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
61*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
62*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
63*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
64*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
65*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
66*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
67*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
68*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
69*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
70*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
71*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
72*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
73*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
74*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
75*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
76*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
77*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
78*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
79*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
80*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
81*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
82*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
83*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
84*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
85*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
86*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
87*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
88*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
89*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
90*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
91*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
92*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
93*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
94*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
95*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
96*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
97*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
98*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
99*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
100*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
101*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
102*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
103*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
104*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
105*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
106*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
107*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
108*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
109*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
110*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
111*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
112*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
113*f6217f89SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
114*f6217f89SApple OSS Distributions 	}
115*f6217f89SApple OSS Distributions };
116*f6217f89SApple OSS Distributions 
117*f6217f89SApple OSS Distributions /*
118*f6217f89SApple OSS Distributions  * volatile to stop the compiler from optimizing away calls to atan()
119*f6217f89SApple OSS Distributions  */
120*f6217f89SApple OSS Distributions volatile double zero = 0.0;
121*f6217f89SApple OSS Distributions 
122*f6217f89SApple OSS Distributions 
123*f6217f89SApple OSS Distributions typedef enum TestCase {
124*f6217f89SApple OSS Distributions 	Yfoo,
125*f6217f89SApple OSS Distributions 	Xfoo,
126*f6217f89SApple OSS Distributions 	Yatan,
127*f6217f89SApple OSS Distributions 	Xatan,
128*f6217f89SApple OSS Distributions 	Xclean,
129*f6217f89SApple OSS Distributions 	Xdirty,
130*f6217f89SApple OSS Distributions 	Xcopyout,
131*f6217f89SApple OSS Distributions 	Xmmap_clean,
132*f6217f89SApple OSS Distributions 	Xmmap_dirty,
133*f6217f89SApple OSS Distributions 	Xwired,
134*f6217f89SApple OSS Distributions 	kernel,
135*f6217f89SApple OSS Distributions 
136*f6217f89SApple OSS Distributions 	BAD_TEST_CASE
137*f6217f89SApple OSS Distributions } TestCase;
138*f6217f89SApple OSS Distributions 
139*f6217f89SApple OSS Distributions typedef struct{
140*f6217f89SApple OSS Distributions 	char *key;
141*f6217f89SApple OSS Distributions 	enum TestCase val;
142*f6217f89SApple OSS Distributions } testcase_t;
143*f6217f89SApple OSS Distributions 
144*f6217f89SApple OSS Distributions #define testCase(name) {#name, name}
145*f6217f89SApple OSS Distributions 
146*f6217f89SApple OSS Distributions static testcase_t testcases[] = {
147*f6217f89SApple OSS Distributions 	testCase(Yfoo),
148*f6217f89SApple OSS Distributions 	testCase(Xfoo),
149*f6217f89SApple OSS Distributions 	testCase(Yatan),
150*f6217f89SApple OSS Distributions 	testCase(Xatan),
151*f6217f89SApple OSS Distributions 	testCase(Xclean),
152*f6217f89SApple OSS Distributions 	testCase(Xdirty),
153*f6217f89SApple OSS Distributions 	testCase(Xmmap_clean),
154*f6217f89SApple OSS Distributions 	testCase(Xmmap_dirty),
155*f6217f89SApple OSS Distributions 	testCase(Xcopyout),
156*f6217f89SApple OSS Distributions 	testCase(kernel),
157*f6217f89SApple OSS Distributions 	testCase(Xwired)
158*f6217f89SApple OSS Distributions };
159*f6217f89SApple OSS Distributions 
160*f6217f89SApple OSS Distributions TestCase
get_testcase(char * key)161*f6217f89SApple OSS Distributions get_testcase(char *key)
162*f6217f89SApple OSS Distributions {
163*f6217f89SApple OSS Distributions 	int i;
164*f6217f89SApple OSS Distributions 	for (i = 0; i < sizeof(testcases) / sizeof(testcase_t); i++) {
165*f6217f89SApple OSS Distributions 		testcase_t elem = testcases[i];
166*f6217f89SApple OSS Distributions 		if (strcmp(elem.key, key) == 0) {
167*f6217f89SApple OSS Distributions 			return elem.val;
168*f6217f89SApple OSS Distributions 		}
169*f6217f89SApple OSS Distributions 	}
170*f6217f89SApple OSS Distributions 	return BAD_TEST_CASE;
171*f6217f89SApple OSS Distributions }
172*f6217f89SApple OSS Distributions 
173*f6217f89SApple OSS Distributions int
main(int argc,char ** argv)174*f6217f89SApple OSS Distributions main(int argc, char **argv)
175*f6217f89SApple OSS Distributions {
176*f6217f89SApple OSS Distributions 	void *addr;
177*f6217f89SApple OSS Distributions 	int *page;
178*f6217f89SApple OSS Distributions 	size_t s = sizeof(addr);
179*f6217f89SApple OSS Distributions 	int err;
180*f6217f89SApple OSS Distributions 	static volatile int readval;
181*f6217f89SApple OSS Distributions 	static volatile double readval_d;
182*f6217f89SApple OSS Distributions 
183*f6217f89SApple OSS Distributions 	/*
184*f6217f89SApple OSS Distributions 	 * check for -v for verbose output
185*f6217f89SApple OSS Distributions 	 */
186*f6217f89SApple OSS Distributions 	if (argc > 1 && strcmp(argv[1], "-v") == 0) {
187*f6217f89SApple OSS Distributions 		verbose = 1;
188*f6217f89SApple OSS Distributions 	}
189*f6217f89SApple OSS Distributions 
190*f6217f89SApple OSS Distributions 	/*
191*f6217f89SApple OSS Distributions 	 * needs to run as root for sysctl
192*f6217f89SApple OSS Distributions 	 */
193*f6217f89SApple OSS Distributions 	if (geteuid() != 0) {
194*f6217f89SApple OSS Distributions 		printf("Test not running as root, exiting\n");
195*f6217f89SApple OSS Distributions 		exit(-1);
196*f6217f89SApple OSS Distributions 	}
197*f6217f89SApple OSS Distributions 
198*f6217f89SApple OSS Distributions 	/*
199*f6217f89SApple OSS Distributions 	 * The argument determines what test to try.
200*f6217f89SApple OSS Distributions 	 * "Y{name}" is a test, "X{name}" does the test after injecting an ECC error
201*f6217f89SApple OSS Distributions 	 *
202*f6217f89SApple OSS Distributions 	 * Tests:
203*f6217f89SApple OSS Distributions 	 * "foo" - invoke a local TEXT function.
204*f6217f89SApple OSS Distributions 	 * "atan" - invoke a shared library TEXT function.
205*f6217f89SApple OSS Distributions 	 * "clean" - read from a clean DATA page
206*f6217f89SApple OSS Distributions 	 * "dirty" - read from a dirty DATA page
207*f6217f89SApple OSS Distributions 	 * "mmap_clean" - read from a clean mmap'd page
208*f6217f89SApple OSS Distributions 	 * "mmap_dirty" - read from a dirty mmap'd page
209*f6217f89SApple OSS Distributions 	 */
210*f6217f89SApple OSS Distributions 	switch (get_testcase(argv[argc - 1])) {
211*f6217f89SApple OSS Distributions 	case Yfoo:
212*f6217f89SApple OSS Distributions 		foo();
213*f6217f89SApple OSS Distributions 		break;
214*f6217f89SApple OSS Distributions 	case Xfoo:
215*f6217f89SApple OSS Distributions 		PRINTF("Warm up call to foo()\n");
216*f6217f89SApple OSS Distributions 		foo();
217*f6217f89SApple OSS Distributions 
218*f6217f89SApple OSS Distributions 		addr = (void *)ptrauth_strip(&foo, ptrauth_key_function_pointer);
219*f6217f89SApple OSS Distributions 		err = sysctlbyname("vm.inject_ecc", NULL, NULL, &addr, s);
220*f6217f89SApple OSS Distributions 
221*f6217f89SApple OSS Distributions 		PRINTF("Calling foo() after injection\n");
222*f6217f89SApple OSS Distributions 		foo();
223*f6217f89SApple OSS Distributions 
224*f6217f89SApple OSS Distributions 		break;
225*f6217f89SApple OSS Distributions 	case Yatan:
226*f6217f89SApple OSS Distributions 		readval_d = atan(zero);
227*f6217f89SApple OSS Distributions 		PRINTF("atan(0) is %g\n", readval_d);
228*f6217f89SApple OSS Distributions 		break;
229*f6217f89SApple OSS Distributions 	case Xatan:
230*f6217f89SApple OSS Distributions 		readval_d = atan(zero);
231*f6217f89SApple OSS Distributions 		PRINTF("Warmup call to atan(0) is %g\n", readval_d);
232*f6217f89SApple OSS Distributions 
233*f6217f89SApple OSS Distributions 		addr = (void *)ptrauth_strip(&atan, ptrauth_key_function_pointer);
234*f6217f89SApple OSS Distributions 		err = sysctlbyname("vm.inject_ecc", NULL, NULL, &addr, s);
235*f6217f89SApple OSS Distributions 
236*f6217f89SApple OSS Distributions 		readval_d = atan(zero);
237*f6217f89SApple OSS Distributions 		PRINTF("After injection, atan(0) is %g\n", readval_d);
238*f6217f89SApple OSS Distributions 		break;
239*f6217f89SApple OSS Distributions 	case Xclean:
240*f6217f89SApple OSS Distributions 		readval = x.big_data[35];
241*f6217f89SApple OSS Distributions 		PRINTF("initial read of clean x.big_data[35] is %d\n", readval);
242*f6217f89SApple OSS Distributions 
243*f6217f89SApple OSS Distributions 		addr = (void *)&x.big_data[35];
244*f6217f89SApple OSS Distributions 		err = sysctlbyname("vm.inject_ecc", NULL, NULL, &addr, s);
245*f6217f89SApple OSS Distributions 
246*f6217f89SApple OSS Distributions 		readval = x.big_data[35];
247*f6217f89SApple OSS Distributions 		PRINTF("After injection, read of x.big_data[35] is %d\n", readval);
248*f6217f89SApple OSS Distributions 		break;
249*f6217f89SApple OSS Distributions 	case Xdirty:
250*f6217f89SApple OSS Distributions 		x.big_data[36] = (int)random();
251*f6217f89SApple OSS Distributions 		PRINTF("initial read of dirty x.big_data[36] is %d\n", x.big_data[36]);
252*f6217f89SApple OSS Distributions 
253*f6217f89SApple OSS Distributions 		addr = (void *)&x.big_data[36];
254*f6217f89SApple OSS Distributions 		err = sysctlbyname("vm.inject_ecc", NULL, NULL, &addr, s);
255*f6217f89SApple OSS Distributions 
256*f6217f89SApple OSS Distributions 		readval = x.big_data[36];
257*f6217f89SApple OSS Distributions 		PRINTF("After injection, read of x.big_data[36] is %d\n", readval);
258*f6217f89SApple OSS Distributions 		break;
259*f6217f89SApple OSS Distributions 	case Xmmap_clean:
260*f6217f89SApple OSS Distributions 		page = (int *)mmap(NULL, PAGE_SIZE * 3, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0);
261*f6217f89SApple OSS Distributions 		page = (int *)((char *)page + PAGE_SIZE);
262*f6217f89SApple OSS Distributions 
263*f6217f89SApple OSS Distributions 		readval = *page;
264*f6217f89SApple OSS Distributions 		PRINTF("initial read of clean page %p is %d\n", page, readval);
265*f6217f89SApple OSS Distributions 
266*f6217f89SApple OSS Distributions 		err = sysctlbyname("vm.inject_ecc", NULL, NULL, &page, s);
267*f6217f89SApple OSS Distributions 
268*f6217f89SApple OSS Distributions 		readval = *page;
269*f6217f89SApple OSS Distributions 		PRINTF("second read of page is %d\n", readval);
270*f6217f89SApple OSS Distributions 		break;
271*f6217f89SApple OSS Distributions 	case Xmmap_dirty:
272*f6217f89SApple OSS Distributions 		page = (int *) mmap(NULL, PAGE_SIZE * 3, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0);
273*f6217f89SApple OSS Distributions 		page = (int *)((char *)page + PAGE_SIZE);
274*f6217f89SApple OSS Distributions 
275*f6217f89SApple OSS Distributions 		*page = 0xFFFF;
276*f6217f89SApple OSS Distributions 		PRINTF("initial read of dirty page %p is %d (after write)\n", page, *page);
277*f6217f89SApple OSS Distributions 
278*f6217f89SApple OSS Distributions 		err = sysctlbyname("vm.inject_ecc", NULL, NULL, &page, s);
279*f6217f89SApple OSS Distributions 
280*f6217f89SApple OSS Distributions 		readval = *page;
281*f6217f89SApple OSS Distributions 		PRINTF("second read of page is %d\n", readval);
282*f6217f89SApple OSS Distributions 		break;
283*f6217f89SApple OSS Distributions 	case Xcopyout:
284*f6217f89SApple OSS Distributions 		x.big_data[37] = (int)random();
285*f6217f89SApple OSS Distributions 		PRINTF("initial read of dirty x.big_data[37] is %d\n", x.big_data[37]);
286*f6217f89SApple OSS Distributions 
287*f6217f89SApple OSS Distributions 		addr = (void *)&x.big_data[37];
288*f6217f89SApple OSS Distributions 		err = sysctlbyname("vm.inject_ecc_copyout", NULL, NULL, &addr, s);
289*f6217f89SApple OSS Distributions 		if (err) {
290*f6217f89SApple OSS Distributions 			PRINTF("copyout return %d\n", err);
291*f6217f89SApple OSS Distributions 			exit(err);
292*f6217f89SApple OSS Distributions 		}
293*f6217f89SApple OSS Distributions 
294*f6217f89SApple OSS Distributions 		readval = x.big_data[37];
295*f6217f89SApple OSS Distributions 		PRINTF("After injection, read of dirty x.big_data[37] is %d\n", readval);
296*f6217f89SApple OSS Distributions 		break;
297*f6217f89SApple OSS Distributions 	case Xwired:
298*f6217f89SApple OSS Distributions 		page = (int *) mmap(NULL, PAGE_SIZE * 3, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, 0, 0);
299*f6217f89SApple OSS Distributions 		page = (int *)((char *)page + PAGE_SIZE);
300*f6217f89SApple OSS Distributions 		PRINTF("page addr %p\n", page);
301*f6217f89SApple OSS Distributions 		if (mlock(page, PAGE_SIZE)) {
302*f6217f89SApple OSS Distributions 			printf("Failed to wire, errno: %d", errno);
303*f6217f89SApple OSS Distributions 			exit(0);
304*f6217f89SApple OSS Distributions 		}
305*f6217f89SApple OSS Distributions 
306*f6217f89SApple OSS Distributions 		err = sysctlbyname("vm.inject_ecc", NULL, NULL, &page, s);
307*f6217f89SApple OSS Distributions 
308*f6217f89SApple OSS Distributions 		readval = *page;
309*f6217f89SApple OSS Distributions 		PRINTF("wire trigger value: %d", readval);
310*f6217f89SApple OSS Distributions 
311*f6217f89SApple OSS Distributions 		break;
312*f6217f89SApple OSS Distributions 	case kernel:
313*f6217f89SApple OSS Distributions 		PRINTF("Inducing ECC on kernel page\n");
314*f6217f89SApple OSS Distributions 
315*f6217f89SApple OSS Distributions 		addr = (void *)1;         /* used to flag some kernel page */
316*f6217f89SApple OSS Distributions 		err = sysctlbyname("vm.inject_ecc", NULL, NULL, &addr, s);
317*f6217f89SApple OSS Distributions 		exit(0);
318*f6217f89SApple OSS Distributions 
319*f6217f89SApple OSS Distributions 		break;
320*f6217f89SApple OSS Distributions 	case BAD_TEST_CASE:
321*f6217f89SApple OSS Distributions 		printf("Unknown test case\n\n");
322*f6217f89SApple OSS Distributions 		printf("Valid tests:\n");
323*f6217f89SApple OSS Distributions 		for (int i = 0; i < sizeof(testcases) / sizeof(testcase_t); i++) {
324*f6217f89SApple OSS Distributions 			testcase_t elem = testcases[i];
325*f6217f89SApple OSS Distributions 			printf("%d. %s\n", i + 1, elem.key);
326*f6217f89SApple OSS Distributions 		}
327*f6217f89SApple OSS Distributions 		printf("\nY{name} is a test, X{name} does the test after injecting an ECC error\n");
328*f6217f89SApple OSS Distributions 
329*f6217f89SApple OSS Distributions 		exit(1);
330*f6217f89SApple OSS Distributions 
331*f6217f89SApple OSS Distributions 		break;
332*f6217f89SApple OSS Distributions 	}
333*f6217f89SApple OSS Distributions 
334*f6217f89SApple OSS Distributions 	exit(0);
335*f6217f89SApple OSS Distributions }
336