Change main function
[IRC.git] / Robust / src / Runtime / runtime.c
1 #include "runtime.h"
2 #include "structdefs.h"
3 #include <string.h>
4 #include <signal.h>
5 #include "mem.h"
6 #include<fcntl.h>
7 #include<sys/types.h>
8 #include<sys/mman.h>
9 #include<errno.h>
10 #include<signal.h>
11 #include<stdio.h>
12
13 extern int classsize[];
14 jmp_buf error_handler;
15
16 #ifdef TASK
17 #include "checkpoint.h"
18 #include "Queue.h"
19 #include "SimpleHash.h"
20 #include "GenericHashtable.h"
21 #ifdef CONSCHECK
22 #include "instrument.h"
23 #endif
24
25 struct Queue * activetasks;
26 struct parameterwrapper * objectqueues[NUMCLASSES];
27 struct genhashtable * failedtasks;
28
29 int main(int argc, char **argv) {
30   GC_init();
31 #ifdef CONSCHECK
32   initializemmap();
33 #endif
34   {
35   int i;
36   /* Allocate startup object */
37   struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
38   struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1); 
39   failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd, 
40                                    (int (*)(void *,void *)) &comparetpd);
41   
42   activetasks=createQueue();
43
44   /* Set flags */
45   processtasks();
46   flagorand(startupobject,1,0xFFFFFFFF);
47
48   /* Build array of strings */
49
50   startupobject->___parameters___=stringarray;
51
52   for(i=1;i<argc;i++) {
53     int length=strlen(argv[i]);
54     struct ___String___ *newstring=NewString(argv[i],length);
55     ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
56   }
57   executetasks();
58   }
59 }
60
61 int hashCodetpd(struct taskparamdescriptor *ftd) {
62   int hash=(int)ftd->task;
63   int i;
64   for(i=0;i<ftd->numParameters;i++) {
65     hash^=(int)ftd->parameterArray[i];
66   }
67   return hash;
68 }
69
70 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
71   int i;
72   if (ftd1->task!=ftd2->task)
73     return 0;
74   for(i=0;i<ftd1->numParameters;i++)
75     if (ftd1->parameterArray[i]!=ftd2->parameterArray[i])
76       return 0;
77   return 1;
78 }
79
80 void flagorand(void * ptr, int ormask, int andmask) {
81   int flag=((int *)ptr)[1];
82   struct RuntimeHash *flagptr=(struct RuntimeHash *)(((int*)ptr)[2]);
83   flag|=ormask;
84   flag&=andmask;
85   ((int*)ptr)[1]=flag;
86   /*Remove from all queues */
87   while(flagptr!=NULL) {
88     struct RuntimeHash *next;
89     RuntimeHashget(flagptr, (int) ptr, (int *) &next);
90     RuntimeHashremove(flagptr, (int)ptr, (int) next);
91     flagptr=next;
92   }
93   
94   {
95     struct QueueItem *tmpptr;
96     struct parameterwrapper * parameter=objectqueues[((int *)ptr)[0]];
97     int i;
98     struct RuntimeHash * prevptr=NULL;
99     while(parameter!=NULL) {
100       for(i=0;i<parameter->numberofterms;i++) {
101         int andmask=parameter->intarray[i*2];
102         int checkmask=parameter->intarray[i*2+1];
103         if ((flag&andmask)==checkmask) {
104           RuntimeHashadd(parameter->objectset, (int) ptr, (int) prevptr);
105           prevptr=parameter->objectset;
106           {
107             struct RuntimeIterator iteratorarray[MAXTASKPARAMS];
108             void * taskpointerarray[MAXTASKPARAMS];
109             int j;
110             int numparams=parameter->task->numParameters;
111             int done=1;
112             struct taskdescriptor * task=parameter->task;
113             int newindex=-1;
114             for(j=0;j<numparams;j++) {
115               struct parameterwrapper *pw=(struct parameterwrapper *)task->descriptorarray[j]->queue;
116               if (parameter==pw) {
117                 taskpointerarray[j]=ptr;
118                 newindex=j;
119               } else {
120                 RuntimeHashiterator(pw->objectset, &iteratorarray[j]);
121                 if (RunhasNext(&iteratorarray[j]))
122                   taskpointerarray[j]=(void *) Runnext(&iteratorarray[j]);
123                 else
124                   break; /* No tasks to dispatch */
125               }
126             }
127             /* Queue task items... */
128
129             while(done) {
130               struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
131               tpd->task=task;
132               tpd->numParameters=numparams;
133               tpd->parameterArray=RUNMALLOC(sizeof(void *)*numparams);
134               for(j=0;j<numparams;j++)
135                 tpd->parameterArray[j]=taskpointerarray[j];
136               /* Queue task */
137               if (!gencontains(failedtasks, tpd))
138                 addNewItem(activetasks, tpd);
139               
140               /* This loop iterates to the next paramter combination */
141               for(j=0;j<numparams;j++) {
142                 if (j==newindex) {
143                   if ((j+1)==numparams)
144                     done=0;
145                   continue;
146                 }
147                 if (RunhasNext(&iteratorarray[j])) {
148                   taskpointerarray[j]=(void *) Runnext(&iteratorarray[j]);
149                   break;
150                 } else if ((j+1)!=numparams) {
151                   RuntimeHashiterator(task->descriptorarray[j]->queue, &iteratorarray[j]);
152                 } else {
153                   done=0;
154                   break;
155                 }
156               }
157             }
158           }
159           break;
160         }
161       }
162       parameter=parameter->next;
163     }
164     ((struct RuntimeHash **)ptr)[2]=prevptr;
165   }
166 }
167
168 /* Handler for signals */
169 void myhandler(int sig, struct __siginfo *info, void *uap) {
170 #ifdef DEBUG
171   printf("sig=%d\n",sig);
172   printf("signal\n");
173 #endif
174   longjmp(error_handler,1);
175 }
176
177 void executetasks() {
178   void * taskpointerarray[MAXTASKPARAMS];
179
180   /* Set up signal handlers */
181   struct sigaction sig;
182   sig.sa_sigaction=&myhandler;
183   sig.sa_flags=SA_SIGINFO;
184   sigemptyset(&sig.sa_mask);
185
186   /* Catch bus errors, segmentation faults, and floating point exceptions*/
187   sigaction(SIGBUS,&sig,0);
188   sigaction(SIGSEGV,&sig,0);
189   sigaction(SIGFPE,&sig,0);
190
191   /* Map first block of memory to protected, anonymous page */
192   mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
193
194   newtask:
195   while(!isEmpty(activetasks)) {
196     struct QueueItem * qi=(struct QueueItem *) getTail(activetasks);
197     struct taskparamdescriptor *tpd=(struct taskparamdescriptor *) qi->objectptr;
198     int i;
199     removeItem(activetasks, qi);
200     
201     for(i=0;i<tpd->task->numParameters;i++) {
202       void * parameter=tpd->parameterArray[i];
203       struct parameterdescriptor * pd=tpd->task->descriptorarray[i];
204       struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
205       if (!RuntimeHashcontainskey(pw->objectset, (int) parameter))
206         goto newtask;
207       taskpointerarray[i]=parameter;
208     }
209     {
210       struct RuntimeHash * forward=allocateRuntimeHash(100);
211       struct RuntimeHash * reverse=allocateRuntimeHash(100);
212       void ** checkpoint=makecheckpoint(tpd->task->numParameters, taskpointerarray, forward, reverse);
213       if (setjmp(error_handler)) {
214         /* Recover */
215         int h;
216 #ifdef DEBUG
217         printf("Recovering\n");
218 #endif
219         genputtable(failedtasks,tpd,tpd);
220         restorecheckpoint(tpd->task->numParameters, taskpointerarray, checkpoint, forward, reverse);
221       } else {
222         /* Actually call task */
223         ((void (*) (void **)) tpd->task->taskptr)(taskpointerarray);
224       }
225     }
226   }
227 }
228
229 void processtasks() {
230   int i;
231   for(i=0;i<numtasks;i++) {
232     struct taskdescriptor * task=taskarray[i];
233     int j;
234
235     for(j=0;j<task->numParameters;j++) {
236       struct parameterdescriptor *param=task->descriptorarray[j];
237       struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
238       struct parameterwrapper ** ptr=&objectqueues[param->type];
239
240       param->queue=parameter;
241       parameter->objectset=allocateRuntimeHash(10);
242       parameter->numberofterms=param->numberterms;
243       parameter->intarray=param->intarray;
244       parameter->task=task;
245       /* Link new queue in */
246       while((*ptr)!=NULL)
247         ptr=&((*ptr)->next);
248       (*ptr)=parameter;
249     }
250   }
251 }
252 #endif
253
254 int ___Object______hashcode____(struct ___Object___ * ___this___) {
255   return (int) ___this___;
256 }
257
258 void ___System______printString____L___String___(struct ___String___ * s) {
259     struct ArrayObject * chararray=s->___string___;
260     int i;
261     for(i=0;i<chararray->___length___;i++) {
262         short s= ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i];
263         putchar(s);
264     }
265 }
266
267 void * allocate_new(int type) {
268   void * v=FREEMALLOC(classsize[type]);
269   *((int *)v)=type;
270   return v;
271 }
272
273 struct ArrayObject * allocate_newarray(int type, int length) {
274   struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
275   v->type=type;
276   v->___length___=length;
277   return v;
278 }
279
280 struct ___String___ * NewString(const char *str,int length) {
281   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
282   struct ___String___ * strobj=allocate_new(STRINGTYPE);
283   int i;
284   strobj->___string___=chararray;
285   for(i=0;i<length;i++) {
286     ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i];  }
287   return strobj;
288 }
289
290 void failedboundschk() {
291 #ifndef TASK
292   printf("Array out of bounds\n");
293   exit(-1);
294 #else
295   longjmp(error_handler,2);
296 #endif
297 }
298
299 void failednullptr() {
300 #ifndef TASK
301   printf("Dereferenced a null pointer\n");
302   exit(-1);
303 #else
304   longjmp(error_handler,3);
305 #endif
306 }
307
308 void abort_task() {
309 #ifndef TASK
310   printf("Aborting\n");
311   exit(-1);
312 #else
313   longjmp(error_handler,4);
314 #endif
315 }