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