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