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