xref: /xnu-10002.41.9/tests/ecc_test_helper.c (revision 699cd48037512bf4380799317ca44ca453c82f57)
1*699cd480SApple OSS Distributions #include <stdlib.h>
2*699cd480SApple OSS Distributions #include <unistd.h>
3*699cd480SApple OSS Distributions #include <stdio.h>
4*699cd480SApple OSS Distributions #include <sys/sysctl.h>
5*699cd480SApple OSS Distributions #include <ptrauth.h>
6*699cd480SApple OSS Distributions #include <math.h>
7*699cd480SApple OSS Distributions #include <string.h>
8*699cd480SApple OSS Distributions #include <errno.h>
9*699cd480SApple OSS Distributions 
10*699cd480SApple OSS Distributions int verbose = 0;
11*699cd480SApple OSS Distributions #define PRINTF(...) \
12*699cd480SApple OSS Distributions 	if (verbose) { \
13*699cd480SApple OSS Distributions 	        printf(__VA_ARGS__); \
14*699cd480SApple OSS Distributions 	}
15*699cd480SApple OSS Distributions 
16*699cd480SApple OSS Distributions __attribute__((noinline))
17*699cd480SApple OSS Distributions static void
foo(void)18*699cd480SApple OSS Distributions foo(void)
19*699cd480SApple OSS Distributions {
20*699cd480SApple OSS Distributions 	PRINTF("In foo()\n");
21*699cd480SApple OSS Distributions 	fflush(stdout);
22*699cd480SApple OSS Distributions }
23*699cd480SApple OSS Distributions 
24*699cd480SApple OSS Distributions volatile struct data {
25*699cd480SApple OSS Distributions 	char buffer1[16 * 1024];
26*699cd480SApple OSS Distributions 	int big_data[16 * 1204];
27*699cd480SApple OSS Distributions 	char buffer2[16 * 1024];
28*699cd480SApple OSS Distributions } x = {
29*699cd480SApple OSS Distributions 	.big_data = {
30*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
31*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
32*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
33*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
34*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
35*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
36*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
37*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
38*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
39*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
40*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
41*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
42*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
43*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
44*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
45*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
46*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
47*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
48*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
49*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
50*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
51*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
52*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
53*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
54*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
55*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
56*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
57*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
58*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
59*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
60*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
61*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
62*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
63*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
64*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
65*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
66*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
67*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
68*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
69*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
70*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
71*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
72*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
73*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
74*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
75*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
76*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
77*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
78*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
79*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
80*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
81*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
82*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
83*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
84*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
85*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
86*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
87*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
88*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
89*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
90*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
91*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
92*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
93*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
94*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
95*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
96*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
97*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
98*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
99*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
100*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
101*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
102*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
103*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
104*699cd480SApple OSS Distributions 		1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
105*699cd480SApple OSS Distributions 	}
106*699cd480SApple OSS Distributions };
107*699cd480SApple OSS Distributions 
108*699cd480SApple OSS Distributions /*
109*699cd480SApple OSS Distributions  * volatile to stop the compiler from optimizing away calls to atan()
110*699cd480SApple OSS Distributions  */
111*699cd480SApple OSS Distributions volatile double zero = 0.0;
112*699cd480SApple OSS Distributions 
113*699cd480SApple OSS Distributions int
main(int argc,char ** argv)114*699cd480SApple OSS Distributions main(int argc, char **argv)
115*699cd480SApple OSS Distributions {
116*699cd480SApple OSS Distributions 	void *addr;
117*699cd480SApple OSS Distributions 	size_t s = sizeof(addr);
118*699cd480SApple OSS Distributions 	int err;
119*699cd480SApple OSS Distributions 	int a;
120*699cd480SApple OSS Distributions 
121*699cd480SApple OSS Distributions 	/*
122*699cd480SApple OSS Distributions 	 * needs to run as root for sysctl.
123*699cd480SApple OSS Distributions 	 */
124*699cd480SApple OSS Distributions 	if (geteuid() != 0) {
125*699cd480SApple OSS Distributions 		PRINTF("Test not running as root\n");
126*699cd480SApple OSS Distributions 		exit(-1);
127*699cd480SApple OSS Distributions 	}
128*699cd480SApple OSS Distributions 
129*699cd480SApple OSS Distributions 	/*
130*699cd480SApple OSS Distributions 	 * check for -v for verbose output
131*699cd480SApple OSS Distributions 	 */
132*699cd480SApple OSS Distributions 	if (strcmp(argv[1], "-v") == 0) {
133*699cd480SApple OSS Distributions 		verbose = 1;
134*699cd480SApple OSS Distributions 	}
135*699cd480SApple OSS Distributions 
136*699cd480SApple OSS Distributions 	/*
137*699cd480SApple OSS Distributions 	 * The argument determines what test to try.
138*699cd480SApple OSS Distributions 	 *  "blahblah" is a test, "Xblahblah" does the test after inject an ECC error.
139*699cd480SApple OSS Distributions 	 * Tests:
140*699cd480SApple OSS Distributions 	 * "foo" - invoke a local text function.
141*699cd480SApple OSS Distributions 	 * "atan" - invoke a shared library text function.
142*699cd480SApple OSS Distributions 	 * "clean" - read from a clean data page
143*699cd480SApple OSS Distributions 	 * "dirty" - read from a dirty data page
144*699cd480SApple OSS Distributions 	 */
145*699cd480SApple OSS Distributions 	if (strcmp(argv[argc - 1], "foo") == 0) {
146*699cd480SApple OSS Distributions 		foo();
147*699cd480SApple OSS Distributions 	} else if (strcmp(argv[argc - 1], "Xfoo") == 0) {
148*699cd480SApple OSS Distributions 		PRINTF("Warm up call to foo()\n");
149*699cd480SApple OSS Distributions 		foo();
150*699cd480SApple OSS Distributions 		addr = (void *)ptrauth_strip(&foo, ptrauth_key_function_pointer);
151*699cd480SApple OSS Distributions 		err = sysctlbyname("vm.inject_ecc", NULL, NULL, &addr, s);
152*699cd480SApple OSS Distributions 		foo();
153*699cd480SApple OSS Distributions 	} else if (strcmp(argv[argc - 1], "atan") == 0) {
154*699cd480SApple OSS Distributions 		PRINTF("atan(0) is %g\n", atan(zero));
155*699cd480SApple OSS Distributions 	} else if (strcmp(argv[argc - 1], "Xatan") == 0) {
156*699cd480SApple OSS Distributions 		PRINTF("Warmup call to atan(0) is %g\n", atan(zero));
157*699cd480SApple OSS Distributions 		addr = (void *)ptrauth_strip(&atan, ptrauth_key_function_pointer);
158*699cd480SApple OSS Distributions 		err = sysctlbyname("vm.inject_ecc", NULL, NULL, &addr, s);
159*699cd480SApple OSS Distributions 		PRINTF("atan(0) is %g\n", atan(zero));
160*699cd480SApple OSS Distributions 	} else if (strcmp(argv[argc - 1], "clean") == 0) {
161*699cd480SApple OSS Distributions 		PRINTF("x.big_data[35] is %d\n", x.big_data[35]);
162*699cd480SApple OSS Distributions 	} else if (strcmp(argv[argc - 1], "Xclean") == 0) {
163*699cd480SApple OSS Distributions 		PRINTF("initial read of x.big_data[35] is %d\n", x.big_data[35]);
164*699cd480SApple OSS Distributions 
165*699cd480SApple OSS Distributions 		addr = (void *)&x.big_data[35];
166*699cd480SApple OSS Distributions 		err = sysctlbyname("vm.inject_ecc", NULL, NULL, &addr, s);
167*699cd480SApple OSS Distributions 		PRINTF("second read of x.big_data[35] is %d\n", x.big_data[35]);
168*699cd480SApple OSS Distributions 	} else if (strcmp(argv[argc - 1], "dirty") == 0) {
169*699cd480SApple OSS Distributions 		x.big_data[35] = (int)random();
170*699cd480SApple OSS Distributions 		PRINTF("x.big_data[35] is %d\n", x.big_data[35]);
171*699cd480SApple OSS Distributions 	} else if (strcmp(argv[argc - 1], "Xdirty") == 0) {
172*699cd480SApple OSS Distributions 		x.big_data[35] = (int)random();
173*699cd480SApple OSS Distributions 		PRINTF("initial read of dirty x.big_data[35] is %d\n", x.big_data[35]);
174*699cd480SApple OSS Distributions 
175*699cd480SApple OSS Distributions 		addr = (void *)&x.big_data[35];
176*699cd480SApple OSS Distributions 		err = sysctlbyname("vm.inject_ecc", NULL, NULL, &addr, s);
177*699cd480SApple OSS Distributions 		PRINTF("second read of x.big_data[35] is %d\n", x.big_data[35]);
178*699cd480SApple OSS Distributions 	} else if (strcmp(argv[argc - 1], "Xcopyout") == 0) {
179*699cd480SApple OSS Distributions 		x.big_data[35] = (int)random();
180*699cd480SApple OSS Distributions 		PRINTF("initial read of dirty x.big_data[35] is %d\n", x.big_data[35]);
181*699cd480SApple OSS Distributions 
182*699cd480SApple OSS Distributions 		addr = (void *)&x.big_data[35];
183*699cd480SApple OSS Distributions 		err = sysctlbyname("vm.inject_ecc_copyout", NULL, NULL, &addr, s);
184*699cd480SApple OSS Distributions 		if (err) {
185*699cd480SApple OSS Distributions 			PRINTF("copyout return %d\n", err);
186*699cd480SApple OSS Distributions 			exit(err);
187*699cd480SApple OSS Distributions 		}
188*699cd480SApple OSS Distributions 		PRINTF("2nd read of dirty x.big_data[35] is %d\n", x.big_data[35]);
189*699cd480SApple OSS Distributions 	} else if (strcmp(argv[argc - 1], "kernel") == 0) {
190*699cd480SApple OSS Distributions 		PRINTF("Inducing ECC on kernel page\n");
191*699cd480SApple OSS Distributions 		addr = (void *)1; /* used to flag some kernel page */
192*699cd480SApple OSS Distributions 		err = sysctlbyname("vm.inject_ecc", NULL, NULL, &addr, s);
193*699cd480SApple OSS Distributions 		exit(0);
194*699cd480SApple OSS Distributions 	} else {
195*699cd480SApple OSS Distributions 		exit(-1);
196*699cd480SApple OSS Distributions 	}
197*699cd480SApple OSS Distributions 	exit(0);
198*699cd480SApple OSS Distributions }
199