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