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