retire nonsymbolic rootdev, dev2name
[minix.git] / test / test9.c
blob035f48e84f188b9437c553423aa925352e2f6093
1 /* Test 9 setjmp with register variables. Author: Ceriel Jacobs */
3 #include <sys/types.h>
4 #include <setjmp.h>
5 #include <signal.h>
7 #define MAX_ERROR 4
9 #include "common.c"
11 char *tmpa;
13 int main(int argc, char *argv []);
14 void test9a(void);
15 void test9b(void);
16 void test9c(void);
17 void test9d(void);
18 void test9e(void);
19 void test9f(void);
20 char *addr(void);
21 void garbage(void);
22 void level1(void);
23 void level2(void);
24 void dolev(void);
25 void catch(int s);
26 void hard(void);
28 int main(argc, argv)
29 int argc;
30 char *argv[];
32 jmp_buf envm;
33 int i, j, m = 0xFFFF;
35 start(9);
36 if (argc == 2) m = atoi(argv[1]);
37 for (j = 0; j < 100; j++) {
38 if (m & 00001) test9a();
39 if (m & 00002) test9b();
40 if (m & 00004) test9c();
41 if (m & 00010) test9d();
42 if (m & 00020) test9e();
43 if (m & 00040) test9f();
45 if (errct) quit();
46 i = 1;
47 if (setjmp(envm) == 0) {
48 i = 2;
49 longjmp(envm, 1);
50 } else {
51 if (i == 2) {
52 /* Correct */
53 } else if (i == 1) {
54 printf("WARNING: The setjmp/longjmp of this machine restore register variables\n\
55 to the value they had at the time of the Setjmp\n");
56 } else {
57 printf("Aha, I just found one last error\n");
58 return 1;
61 quit();
62 return(-1); /* impossible */
65 void test9a()
67 register int p;
69 subtest = 1;
70 p = 200;
71 garbage();
72 if (p != 200) e(1);
75 void test9b()
77 register int p, q;
79 subtest = 2;
80 p = 200;
81 q = 300;
82 garbage();
83 if (p != 200) e(1);
84 if (q != 300) e(2);
87 void test9c()
89 register int p, q, r;
91 subtest = 3;
92 p = 200;
93 q = 300;
94 r = 400;
95 garbage();
96 if (p != 200) e(1);
97 if (q != 300) e(2);
98 if (r != 400) e(3);
101 char buf[512];
103 void test9d()
105 register char *p;
107 subtest = 4;
108 p = &buf[100];
109 garbage();
110 if (p != &buf[100]) e(1);
113 void test9e()
115 register char *p, *q;
117 subtest = 5;
118 p = &buf[100];
119 q = &buf[200];
120 garbage();
121 if (p != &buf[100]) e(1);
122 if (q != &buf[200]) e(2);
125 void test9f()
127 register char *p, *q, *r;
129 subtest = 6;
130 p = &buf[100];
131 q = &buf[200];
132 r = &buf[300];
133 garbage();
134 if (p != &buf[100]) e(1);
135 if (q != &buf[200]) e(2);
136 if (r != &buf[300]) e(3);
139 jmp_buf env;
141 /* return address of local variable.
142 This way we can check that the stack is not polluted.
144 char *
145 addr()
147 char a, *ret;
149 ret = &a;
150 return(ret);
153 void garbage()
155 register int i, j, k;
156 register char *p, *q, *r;
157 char *a = NULL;
159 p = &buf[300];
160 q = &buf[400];
161 r = &buf[500];
162 i = 10;
163 j = 20;
164 k = 30;
165 switch (setjmp(env)) {
166 case 0:
167 a = addr();
168 #ifdef __GNUC__
170 * to defeat the smartness of the GNU C optimizer we pretend we
171 * use 'a'. Otherwise the optimizer will not detect the looping
172 * effectuated by setjmp/longjmp, so that it thinks it can get
173 * rid of the assignment to 'a'.
175 srand((unsigned)&a);
176 #endif
177 longjmp(env, 1);
178 break;
179 case 1:
180 if (i != 10) e(11);
181 if (j != 20) e(12);
182 if (k != 30) e(13);
183 if (p != &buf[300]) e(14);
184 if (q != &buf[400]) e(15);
185 if (r != &buf[500]) e(16);
186 tmpa = addr();
187 if (a != tmpa) e(17);
188 level1();
189 break;
190 case 2:
191 if (i != 10) e(21);
192 if (j != 20) e(22);
193 if (k != 30) e(23);
194 if (p != &buf[300]) e(24);
195 if (q != &buf[400]) e(25);
196 if (r != &buf[500]) e(26);
197 tmpa = addr();
198 if (a != tmpa) e(27);
199 level2();
200 break;
201 case 3:
202 if (i != 10) e(31);
203 if (j != 20) e(32);
204 if (k != 30) e(33);
205 if (p != &buf[300]) e(34);
206 if (q != &buf[400]) e(35);
207 if (r != &buf[500]) e(36);
208 tmpa = addr();
209 if (a != tmpa) e(37);
210 hard();
211 case 4:
212 if (i != 10) e(41);
213 if (j != 20) e(42);
214 if (k != 30) e(43);
215 if (p != &buf[300]) e(44);
216 if (q != &buf[400]) e(45);
217 if (r != &buf[500]) e(46);
218 tmpa = addr();
219 if (a != tmpa) e(47);
220 return;
221 break;
222 default: e(100);
224 e(200);
227 void level1()
229 register char *p;
230 register int i;
232 i = 1000;
233 p = &buf[10];
234 i = 200;
235 p = &buf[20];
236 longjmp(env, 2);
239 void level2()
241 register char *p;
242 register int i;
244 i = 0200;
245 p = &buf[2];
246 *p = i;
247 dolev();
250 void dolev()
252 register char *p;
253 register int i;
255 i = 010;
256 p = &buf[3];
257 *p = i;
258 longjmp(env, 3);
261 void catch(s)
262 int s;
264 longjmp(env, 4);
267 void hard()
269 register char *p;
271 signal(SIGHUP, catch);
272 for (p = buf; p <= &buf[511]; p++) *p = 025;
273 kill(getpid(), SIGHUP);