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