~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Minix Cross Reference
Minix/test/test9.c


  1 /* Test 9 setjmp with register variables.       Author: Ceriel Jacobs */
  2 
  3 #include <sys/types.h>
  4 #include <setjmp.h>
  5 #include <signal.h>
  6 
  7 #define MAX_ERROR 4
  8 
  9 #include "common.c"
 10 
 11 char *tmpa;
 12 
 13 _PROTOTYPE(int main, (int argc, char *argv []));
 14 _PROTOTYPE(void test9a, (void));
 15 _PROTOTYPE(void test9b, (void));
 16 _PROTOTYPE(void test9c, (void));
 17 _PROTOTYPE(void test9d, (void));
 18 _PROTOTYPE(void test9e, (void));
 19 _PROTOTYPE(void test9f, (void));
 20 _PROTOTYPE(char *addr, (void));
 21 _PROTOTYPE(void garbage, (void));
 22 _PROTOTYPE(void  level1, (void));
 23 _PROTOTYPE(void level2, (void));
 24 _PROTOTYPE(void dolev, (void));
 25 _PROTOTYPE(void catch, (int s));
 26 _PROTOTYPE(void hard, (void));
 27 
 28 int main(argc, argv)
 29 int argc;
 30 char *argv[];
 31 {
 32   jmp_buf envm;
 33   int i, j, m = 0xFFFF;
 34 
 35   start_test(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();
 44   }
 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;
 59         }
 60   }
 61   quit();
 62   return(-1);                   /* impossible */
 63 }
 64 
 65 
 66 void test9a()
 67 {
 68   register p;
 69 
 70   subtest = 1;
 71   p = 200;
 72   garbage();
 73   if (p != 200) e(1);
 74 }
 75 
 76 void test9b()
 77 {
 78   register p, q;
 79 
 80   subtest = 2;
 81   p = 200;
 82   q = 300;
 83   garbage();
 84   if (p != 200) e(1);
 85   if (q != 300) e(2);
 86 }
 87 
 88 void test9c()
 89 {
 90   register p, q, r;
 91 
 92   subtest = 3;
 93   p = 200;
 94   q = 300;
 95   r = 400;
 96   garbage();
 97   if (p != 200) e(1);
 98   if (q != 300) e(2);
 99   if (r != 400) e(3);
100 }
101 
102 char buf[512];
103 
104 void test9d()
105 {
106   register char *p;
107 
108   subtest = 4;
109   p = &buf[100];
110   garbage();
111   if (p != &buf[100]) e(1);
112 }
113 
114 void test9e()
115 {
116   register char *p, *q;
117 
118   subtest = 5;
119   p = &buf[100];
120   q = &buf[200];
121   garbage();
122   if (p != &buf[100]) e(1);
123   if (q != &buf[200]) e(2);
124 }
125 
126 void test9f()
127 {
128   register char *p, *q, *r;
129 
130   subtest = 6;
131   p = &buf[100];
132   q = &buf[200];
133   r = &buf[300];
134   garbage();
135   if (p != &buf[100]) e(1);
136   if (q != &buf[200]) e(2);
137   if (r != &buf[300]) e(3);
138 }
139 
140 jmp_buf env;
141 
142 /*      return address of local variable.
143   This way we can check that the stack is not polluted.
144 */
145 char *
146  addr()
147 {
148   char a;
149 
150   return &a;
151 }
152 
153 void garbage()
154 {
155   register i, j, k;
156   register char *p, *q, *r;
157   char *a;
158 
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__
169         /*
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'.
174          */
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);
223   }
224   e(200);
225 }
226 
227 void level1()
228 {
229   register char *p;
230   register i;
231 
232   i = 1000;
233   p = &buf[10];
234   i = 200;
235   p = &buf[20];
236   longjmp(env, 2);
237 }
238 
239 void level2()
240 {
241   register char *p;
242   register i;
243 
244   i = 0200;
245   p = &buf[2];
246   *p = i;
247   dolev();
248 }
249 
250 void dolev()
251 {
252   register char *p;
253   register i;
254 
255   i = 010;
256   p = &buf[3];
257   *p = i;
258   longjmp(env, 3);
259 }
260 
261 void catch(s)
262 int s;
263 {
264   longjmp(env, 4);
265 }
266 
267 void hard()
268 {
269   register char *p;
270 
271   signal(SIGHUP, catch);
272   for (p = buf; p <= &buf[511]; p++) *p = 025;
273   kill(getpid(), SIGHUP);
274 }
275 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.