2 #include "structdefs.h"
14 extern int classsize[];
15 jmp_buf error_handler;
20 float failurechance=0;
22 int injectinstructionfailures;
24 float instfailurechance=0;
33 #include "checkpoint.h"
35 #include "SimpleHash.h"
36 #include "GenericHashtable.h"
37 #include <sys/select.h>
40 #include "instrument.h"
43 struct Queue * activetasks;
44 struct parameterwrapper * objectqueues[NUMCLASSES];
45 struct genhashtable * failedtasks;
46 struct taskparamdescriptor * currtpd;
47 struct RuntimeHash * forward;
48 struct RuntimeHash * reverse;
51 int main(int argc, char **argv) {
53 GC_init(); // Initialize the garbage collector
60 /* Create table for failed tasks */
61 failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
62 (int (*)(void *,void *)) &comparetpd);
63 /* Create queue of active tasks */
64 activetasks=createQueue();
66 /* Process task information */
69 /* Create startup object */
70 createstartupobject(argc, argv);
72 /* Start executing the tasks */
76 void createstartupobject(int argc, char ** argv) {
79 /* Allocate startup object */
81 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(NULL, STARTUPTYPE);
82 struct ArrayObject * stringarray=allocate_newarray(NULL, STRINGARRAYTYPE, argc-1);
84 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
85 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
87 /* Build array of strings */
88 startupobject->___parameters___=stringarray;
90 int length=strlen(argv[i]);
92 struct ___String___ *newstring=NewString(NULL, argv[i],length);
94 struct ___String___ *newstring=NewString(argv[i],length);
96 ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
99 /* Set initialized flag for startup object */
100 flagorand(startupobject,1,0xFFFFFFFF);
103 int hashCodetpd(struct taskparamdescriptor *ftd) {
104 int hash=(int)ftd->task;
106 for(i=0;i<ftd->numParameters;i++) {
107 hash^=(int)ftd->parameterArray[i];
112 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
114 if (ftd1->task!=ftd2->task)
116 for(i=0;i<ftd1->numParameters;i++)
117 if (ftd1->parameterArray[i]!=ftd2->parameterArray[i])
122 /* This function updates the flag for object ptr. It or's the flag
123 with the or mask and and's it with the andmask. */
125 void flagorand(void * ptr, int ormask, int andmask) {
126 int oldflag=((int *)ptr)[1];
127 struct RuntimeHash *flagptr=(struct RuntimeHash *)(((int*)ptr)[2]);
128 int flag=ormask|oldflag;
130 if (flag==oldflag) /* Don't do anything */
134 /*Remove object from all queues */
135 while(flagptr!=NULL) {
136 struct RuntimeHash *next;
137 RuntimeHashget(flagptr, (int) ptr, (int *) &next);
138 RuntimeHashremove(flagptr, (int)ptr, (int) next);
143 struct QueueItem *tmpptr;
144 struct parameterwrapper * parameter=objectqueues[((int *)ptr)[0]];
146 struct RuntimeHash * prevptr=NULL;
147 while(parameter!=NULL) {
148 for(i=0;i<parameter->numberofterms;i++) {
149 int andmask=parameter->intarray[i*2];
150 int checkmask=parameter->intarray[i*2+1];
151 if ((flag&andmask)==checkmask) {
152 RuntimeHashadd(parameter->objectset, (int) ptr, (int) prevptr);
153 prevptr=parameter->objectset;
155 struct RuntimeIterator iteratorarray[MAXTASKPARAMS];
156 void * taskpointerarray[MAXTASKPARAMS];
158 int numparams=parameter->task->numParameters;
160 struct taskdescriptor * task=parameter->task;
162 for(j=0;j<numparams;j++) {
163 struct parameterwrapper *pw=(struct parameterwrapper *)task->descriptorarray[j]->queue;
165 taskpointerarray[j]=ptr;
168 RuntimeHashiterator(pw->objectset, &iteratorarray[j]);
169 if (RunhasNext(&iteratorarray[j])) {
170 taskpointerarray[j]=(void *) Runkey(&iteratorarray[j]);
171 Runnext(&iteratorarray[j]);
174 break; /* No tasks to dispatch */
178 /* Queue task items... */
181 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
183 tpd->numParameters=numparams;
184 tpd->parameterArray=RUNMALLOC(sizeof(void *)*numparams);
185 for(j=0;j<numparams;j++)
186 tpd->parameterArray[j]=taskpointerarray[j];
188 if (!gencontains(failedtasks, tpd))
189 addNewItem(activetasks, tpd);
191 RUNFREE(tpd->parameterArray);
195 /* This loop iterates to the next parameter combination */
196 for(j=0;j<numparams;j++) {
198 if ((j+1)==numparams)
202 if (RunhasNext(&iteratorarray[j])) {
203 taskpointerarray[j]=(void *) Runkey(&iteratorarray[j]);
204 Runnext(&iteratorarray[j]);
206 } else if ((j+1)!=numparams) {
207 RuntimeHashiterator(task->descriptorarray[j]->queue, &iteratorarray[j]);
218 parameter=parameter->next;
220 ((struct RuntimeHash **)ptr)[2]=prevptr;
224 /* Handler for signals. The signals catch null pointer errors and
225 arithmatic errors. */
227 void myhandler(int sig, siginfo_t *info, void *uap) {
229 printf("sig=%d\n",sig);
232 longjmp(error_handler,1);
237 struct RuntimeHash *fdtoobject;
239 void addreadfd(int fd) {
242 FD_SET(fd, &readfds);
245 void removereadfd(int fd) {
246 FD_CLR(fd, &readfds);
247 if (maxreadfd==(fd+1)) {
249 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
260 void executetasks() {
261 void * taskpointerarray[MAXTASKPARAMS+OFFSET];
263 /* Set up signal handlers */
264 struct sigaction sig;
265 sig.sa_sigaction=&myhandler;
266 sig.sa_flags=SA_SIGINFO;
267 sigemptyset(&sig.sa_mask);
269 /* Catch bus errors, segmentation faults, and floating point exceptions*/
270 sigaction(SIGBUS,&sig,0);
271 sigaction(SIGSEGV,&sig,0);
272 sigaction(SIGFPE,&sig,0);
273 sigaction(SIGPIPE,&sig,0);
278 fdtoobject=allocateRuntimeHash(100);
280 /* Map first block of memory to protected, anonymous page */
281 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
284 while(!isEmpty(activetasks)||(maxreadfd>0)) {
286 /* Check if any filedescriptors have IO pending */
289 struct timeval timeout={0,0};
293 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
295 /* Process ready fd's */
297 for(fd=0;fd<maxreadfd;fd++) {
298 if (FD_ISSET(fd, &tmpreadfds)) {
299 /* Set ready flag on object */
301 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
302 flagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
309 /* See if there are any active tasks */
310 if (!isEmpty(activetasks)) {
312 struct QueueItem * qi=(struct QueueItem *) getTail(activetasks);
313 currtpd=(struct taskparamdescriptor *) qi->objectptr;
314 removeItem(activetasks, qi);
316 /* Check if this task has failed */
317 if (gencontains(failedtasks, currtpd)) {
318 // Free up task parameter descriptor
319 RUNFREE(currtpd->parameterArray);
324 /* Make sure that the parameters are still in the queues */
325 for(i=0;i<currtpd->task->numParameters;i++) {
326 void * parameter=currtpd->parameterArray[i];
327 struct parameterdescriptor * pd=currtpd->task->descriptorarray[i];
328 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
329 if (!RuntimeHashcontainskey(pw->objectset, (int) parameter)) {
330 RUNFREE(currtpd->parameterArray);
334 taskpointerarray[i+OFFSET]=parameter;
337 /* Checkpoint the state */
338 forward=allocateRuntimeHash(100);
339 reverse=allocateRuntimeHash(100);
340 void ** checkpoint=makecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, forward, reverse);
342 if (x=setjmp(error_handler)) {
346 printf("Fatal Error=%d, Recovering!\n",x);
348 genputtable(failedtasks,currtpd,currtpd);
349 restorecheckpoint(currtpd->task->numParameters, currtpd->parameterArray, checkpoint, forward, reverse);
350 freeRuntimeHash(forward);
351 freeRuntimeHash(reverse);
356 if (injectfailures) {
357 if ((((double)random())/RAND_MAX)<failurechance) {
358 printf("\nINJECTING TASK FAILURE to %s\n", currtpd->task->name);
359 longjmp(error_handler,10);
362 /* Actually call task */
364 ((int *)taskpointerarray)[0]=currtpd->task->numParameters;
365 taskpointerarray[1]=NULL;
369 printf("ENTER %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
370 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
371 printf("EXIT %s count=%d\n",currtpd->task->name, (instaccum-instructioncount));
373 ((void (*) (void **)) currtpd->task->taskptr)(taskpointerarray);
374 freeRuntimeHash(forward);
375 freeRuntimeHash(reverse);
377 // Free up task parameter descriptor
378 RUNFREE(currtpd->parameterArray);
388 /* This function processes the task information to create queues for
389 each parameter type. */
391 void processtasks() {
393 for(i=0;i<numtasks;i++) {
394 struct taskdescriptor * task=taskarray[i];
397 for(j=0;j<task->numParameters;j++) {
398 struct parameterdescriptor *param=task->descriptorarray[j];
399 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
400 struct parameterwrapper ** ptr=&objectqueues[param->type];
402 param->queue=parameter;
403 parameter->objectset=allocateRuntimeHash(10);
404 parameter->numberofterms=param->numberterms;
405 parameter->intarray=param->intarray;
406 parameter->task=task;
407 /* Link new queue in */
417 /* This function inject failures */
419 void injectinstructionfailure() {
421 if (injectinstructionfailures) {
424 instructioncount=failurecount;
425 instaccum+=failurecount;
426 if ((((double)random())/RAND_MAX)<instfailurechance) {
429 printf("FAILURE!!! %d\n",numfailures);
430 longjmp(error_handler,11);
435 if (injectinstructionfailures) {
438 instaccum+=failurecount;
439 if ((((double)random())/RAND_MAX)<instfailurechance) {
442 printf("FAILURE!!! %d\n",numfailures);
450 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
451 struct ArrayObject * chararray=VAR(___s___)->___value___;
453 int offset=VAR(___s___)->___offset___;
454 for(i=0;i<VAR(___s___)->___count___;i++) {
455 short sc=((short *)(((char *)& chararray->___length___)+sizeof(int)))[i+offset];
460 /* Object allocation function */
463 void * allocate_new(void * ptr, int type) {
464 struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
474 /* Array allocation function */
476 struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
477 struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
479 v->___length___=length;
489 void * allocate_new(int type) {
490 void * v=FREEMALLOC(classsize[type]);
495 /* Array allocation function */
497 struct ArrayObject * allocate_newarray(int type, int length) {
498 struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
500 v->___length___=length;
506 /* Converts C character arrays into Java strings */
508 struct ___String___ * NewString(void * ptr, const char *str,int length) {
510 struct ___String___ * NewString(const char *str,int length) {
513 struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
514 struct ___String___ * strobj=allocate_new((struct garbagelist *) ptr, STRINGTYPE);
516 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
517 struct ___String___ * strobj=allocate_new(STRINGTYPE);
520 strobj->___value___=chararray;
521 strobj->___count___=length;
522 strobj->___offset___=0;
524 for(i=0;i<length;i++) {
525 ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i]; }
529 /* Generated code calls this if we fail a bounds check */
531 void failedboundschk() {
533 printf("Array out of bounds\n");
540 longjmp(error_handler,2);
544 /* Abort task call */
547 longjmp(error_handler,4);
549 printf("Aborting\n");