2 #include "structdefs.h"
13 extern int classsize[];
14 jmp_buf error_handler;
18 #include "checkpoint.h"
20 #include "SimpleHash.h"
21 #include "GenericHashtable.h"
22 #include <sys/select.h>
25 #include "instrument.h"
28 struct Queue * activetasks;
29 struct parameterwrapper * objectqueues[NUMCLASSES];
30 struct genhashtable * failedtasks;
31 /*struct genhashtable * failedobjects;*/
32 extern char ** environ;
35 float failurechance=0;
37 int injectinstructionfailures;
39 float instfailurechance=0;
41 void processOptions() {
44 for(i=0;environ[i]!=0;i++) {
45 if (strncmp(environ[i],"BRISTLECONE=",12)==0) {
46 options=environ[i]+12;
51 while(options!=NULL) {
52 if (strncmp(options,"-injectfailures",sizeof("-injectfailures")-1)==0) {
53 options=strchr(options,' ');
54 if (options!=NULL) options++;
57 sscanf(options, "%f", &failurechance);
59 printf("Injecting errors with chance=%f\n",failurechance);
60 options=strchr(options,' ');
61 if (options!=NULL) options++;
62 } else if (strncmp(options,"-injectinstructionfailures",sizeof("-injectinstructionfailures")-1)==0) {
63 options=strchr(options,' ');
64 if (options!=NULL) options++;
67 sscanf(options, "%d", &failurecount);
68 options=strchr(options,' ');
69 if (options!=NULL) options++;
72 sscanf(options, "%f", &instfailurechance);
73 options=strchr(options,' ');
74 if (options!=NULL) options++;
75 instructioncount=failurecount;
76 injectinstructionfailures=1;
77 printf("Injecting errors with count=%d\n",failurecount);
78 printf("Injecting errors with chance=%f\n",instfailurechance);
80 } else if (strncmp(options, "-debugtask",sizeof("-debugtask")-1)==0) {
81 options=strchr(options,' ');
82 if (options!=NULL) options++;
84 printf("Debug task option on.\n");
90 int main(int argc, char **argv) {
91 GC_init(); // Initialize the garbage collector
99 /* Allocate startup object */
100 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
101 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
102 failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
103 (int (*)(void *,void *)) &comparetpd);
104 /* failedobjects=genallocatehashtable(NULL,NULL);*/
106 activetasks=createQueue();
110 flagorand(startupobject,1,0xFFFFFFFF);
112 /* Build array of strings */
114 startupobject->___parameters___=stringarray;
116 for(i=1;i<argc;i++) {
117 int length=strlen(argv[i]);
118 struct ___String___ *newstring=NewString(argv[i],length);
119 ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
125 int hashCodetpd(struct taskparamdescriptor *ftd) {
126 int hash=(int)ftd->task;
128 for(i=0;i<ftd->numParameters;i++) {
129 hash^=(int)ftd->parameterArray[i];
134 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
136 if (ftd1->task!=ftd2->task)
138 for(i=0;i<ftd1->numParameters;i++)
139 if (ftd1->parameterArray[i]!=ftd2->parameterArray[i])
144 void flagorand(void * ptr, int ormask, int andmask) {
145 int flag=((int *)ptr)[1];
146 struct RuntimeHash *flagptr=(struct RuntimeHash *)(((int*)ptr)[2]);
150 /*Remove from all queues */
151 while(flagptr!=NULL) {
152 struct RuntimeHash *next;
153 RuntimeHashget(flagptr, (int) ptr, (int *) &next);
154 RuntimeHashremove(flagptr, (int)ptr, (int) next);
159 struct QueueItem *tmpptr;
160 struct parameterwrapper * parameter=objectqueues[((int *)ptr)[0]];
162 struct RuntimeHash * prevptr=NULL;
163 while(parameter!=NULL) {
164 for(i=0;i<parameter->numberofterms;i++) {
165 int andmask=parameter->intarray[i*2];
166 int checkmask=parameter->intarray[i*2+1];
167 if ((flag&andmask)==checkmask) {
168 RuntimeHashadd(parameter->objectset, (int) ptr, (int) prevptr);
169 prevptr=parameter->objectset;
171 struct RuntimeIterator iteratorarray[MAXTASKPARAMS];
172 void * taskpointerarray[MAXTASKPARAMS];
174 int numparams=parameter->task->numParameters;
176 struct taskdescriptor * task=parameter->task;
178 for(j=0;j<numparams;j++) {
179 struct parameterwrapper *pw=(struct parameterwrapper *)task->descriptorarray[j]->queue;
181 taskpointerarray[j]=ptr;
184 RuntimeHashiterator(pw->objectset, &iteratorarray[j]);
185 if (RunhasNext(&iteratorarray[j])) {
186 taskpointerarray[j]=(void *) Runkey(&iteratorarray[j]);
187 Runnext(&iteratorarray[j]);
190 break; /* No tasks to dispatch */
194 /* Queue task items... */
197 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
199 tpd->numParameters=numparams;
200 tpd->parameterArray=RUNMALLOC(sizeof(void *)*numparams);
201 for(j=0;j<numparams;j++)
202 tpd->parameterArray[j]=taskpointerarray[j];
204 if (!gencontains(failedtasks, tpd))
205 addNewItem(activetasks, tpd);
207 /* This loop iterates to the next parameter combination */
208 for(j=0;j<numparams;j++) {
210 if ((j+1)==numparams)
214 if (RunhasNext(&iteratorarray[j])) {
215 taskpointerarray[j]=(void *) Runkey(&iteratorarray[j]);
216 Runnext(&iteratorarray[j]);
218 } else if ((j+1)!=numparams) {
219 RuntimeHashiterator(task->descriptorarray[j]->queue, &iteratorarray[j]);
230 parameter=parameter->next;
232 ((struct RuntimeHash **)ptr)[2]=prevptr;
236 /* Handler for signals. The signals catch null pointer errors and
237 arithmatic errors. */
239 void myhandler(int sig, struct __siginfo *info, void *uap) {
241 printf("sig=%d\n",sig);
244 longjmp(error_handler,1);
249 struct RuntimeHash *fdtoobject;
251 void addreadfd(int fd) {
254 FD_SET(fd, &readfds);
257 void removereadfd(int fd) {
258 FD_CLR(fd, &readfds);
259 if (maxreadfd==(fd+1)) {
261 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
267 void restoreObject(void * obj) {
268 if (gencontains(failedobjects, obj)) {
269 struct tpdlist *tpd=gengettable(failedobjects, obj);
270 genfreekey(failedobjects, obj);
273 struct taskparamdescriptor *task=tpd->task;
274 genfreekey(failedtasks, task);
275 for(i=0;i<task->numParameters;i++) {
276 void *objother=task->parameterArray[i];
277 struct tpdlist *tpdother=gengettable(failedobjects, objother);
278 struct tpdlist *tmpptr;
279 genfreekey(failedobjects, objother);
280 struct tpdlist **tpdptr=&tpdother;
281 while((*tpdptr)->task!=task)
282 tpdptr=&((*tpdptr)->next);
284 (*tpdptr)=(*tpdptr)->next;
287 genputtable(failedobjects, objother, tpdother);
291 struct tpdlist *old=tpd;
300 void executetasks() {
301 void * taskpointerarray[MAXTASKPARAMS];
303 /* Set up signal handlers */
304 struct sigaction sig;
305 sig.sa_sigaction=&myhandler;
306 sig.sa_flags=SA_SIGINFO;
307 sigemptyset(&sig.sa_mask);
309 /* Catch bus errors, segmentation faults, and floating point exceptions*/
310 sigaction(SIGBUS,&sig,0);
311 sigaction(SIGSEGV,&sig,0);
312 sigaction(SIGFPE,&sig,0);
317 fdtoobject=allocateRuntimeHash(100);
319 /* Map first block of memory to protected, anonymous page */
320 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
323 while(!isEmpty(activetasks)||(maxreadfd>0)) {
325 /* Check if any filedescriptors have IO pending */
328 struct timeval timeout={0,0};
331 FD_COPY(&readfds, &tmpreadfds);
332 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
334 /* Process ready fd's */
336 for(fd=0;fd<maxreadfd;fd++) {
337 if (FD_ISSET(fd, &tmpreadfds)) {
338 /* Set ready flag on object */
340 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
341 flagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
348 /* See if there are any active tasks */
349 if (!isEmpty(activetasks)) {
351 struct QueueItem * qi=(struct QueueItem *) getTail(activetasks);
352 struct taskparamdescriptor *tpd=(struct taskparamdescriptor *) qi->objectptr;
353 removeItem(activetasks, qi);
355 /* Check if this task has failed */
356 if (gencontains(failedtasks, tpd))
359 /* Make sure that the parameters are still in the queues */
360 for(i=0;i<tpd->task->numParameters;i++) {
361 void * parameter=tpd->parameterArray[i];
362 struct parameterdescriptor * pd=tpd->task->descriptorarray[i];
363 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
364 if (!RuntimeHashcontainskey(pw->objectset, (int) parameter))
366 taskpointerarray[i]=parameter;
369 /* Checkpoint the state */
370 struct RuntimeHash * forward=allocateRuntimeHash(100);
371 struct RuntimeHash * reverse=allocateRuntimeHash(100);
372 void ** checkpoint=makecheckpoint(tpd->task->numParameters, taskpointerarray, forward, reverse);
374 if (x=setjmp(error_handler)) {
378 printf("Fatal Error=%d, Recovering!\n",x);
380 genputtable(failedtasks,tpd,tpd);
381 /* for(i=0;i<tpd->task->numParameters;i++) {
382 void *parameter=tpd->parameterArray[i];
384 // Create mapping from object -> failed tasks
385 struct tpdlist * tpnew=RUNMALLOC(sizeof(struct tpdlist));
387 if (gencontains(failedobjects, parameter)) {
388 struct tpdlist * tpdptr=gengettable(failedobjects, parameter);
389 tpnew->next=tpdptr->next;
393 genputtable(failedobjects, parameter, tpnew);
397 restorecheckpoint(tpd->task->numParameters, taskpointerarray, checkpoint, forward, reverse);
399 if (injectfailures) {
400 if ((((double)random())/RAND_MAX)<failurechance) {
401 printf("\nINJECTING TASK FAILURE to %s\n", tpd->task->name);
402 longjmp(error_handler,10);
405 /* Actually call task */
407 printf("ENTER %s\n",tpd->task->name);
408 ((void (*) (void **)) tpd->task->taskptr)(taskpointerarray);
409 printf("EXIT %s\n",tpd->task->name);
411 ((void (*) (void **)) tpd->task->taskptr)(taskpointerarray);
418 /* This function processes the task information to create queues for
419 each parameter type. */
421 void processtasks() {
423 for(i=0;i<numtasks;i++) {
424 struct taskdescriptor * task=taskarray[i];
427 for(j=0;j<task->numParameters;j++) {
428 struct parameterdescriptor *param=task->descriptorarray[j];
429 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
430 struct parameterwrapper ** ptr=&objectqueues[param->type];
432 param->queue=parameter;
433 parameter->objectset=allocateRuntimeHash(10);
434 parameter->numberofterms=param->numberterms;
435 parameter->intarray=param->intarray;
436 parameter->task=task;
437 /* Link new queue in */
447 /* This function inject failures */
449 void injectinstructionfailure() {
451 if (injectinstructionfailures) {
452 instructioncount=failurecount;
453 if ((((double)random())/RAND_MAX)<instfailurechance) {
454 printf("FAILURE!!!\n");
455 longjmp(error_handler,11);
462 int ___Object______hashCode____(struct ___Object___ * ___this___) {
463 return (int) ___this___;
466 void ___System______printString____L___String___(struct ___String___ * s) {
467 struct ArrayObject * chararray=s->___value___;
469 int offset=s->___offset___;
470 for(i=0;i<s->___count___;i++) {
471 short s= ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i+offset];
476 void * allocate_new(int type) {
477 void * v=FREEMALLOC(classsize[type]);
482 struct ArrayObject * allocate_newarray(int type, int length) {
483 struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
485 v->___length___=length;
489 struct ___String___ * NewString(const char *str,int length) {
490 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
491 struct ___String___ * strobj=allocate_new(STRINGTYPE);
493 strobj->___value___=chararray;
494 strobj->___count___=length;
495 strobj->___offset___=0;
497 for(i=0;i<length;i++) {
498 ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i]; }
502 /* Generated code calls this if we fail a bounds check */
503 void failedboundschk() {
505 printf("Array out of bounds\n");
508 longjmp(error_handler,2);
514 longjmp(error_handler,4);
516 printf("Aborting\n");