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;
43 void processOptions() {
46 for(i=0;environ[i]!=0;i++) {
47 if (strncmp(environ[i],"BRISTLECONE=",12)==0) {
48 options=environ[i]+12;
53 while(options!=NULL) {
54 if (strncmp(options,"-injectfailures",sizeof("-injectfailures")-1)==0) {
55 options=strchr(options,' ');
56 if (options!=NULL) options++;
59 sscanf(options, "%f", &failurechance);
61 printf("Injecting errors with chance=%f\n",failurechance);
62 options=strchr(options,' ');
63 if (options!=NULL) options++;
64 } else if (strncmp(options,"-injectinstructionfailures",sizeof("-injectinstructionfailures")-1)==0) {
65 options=strchr(options,' ');
66 if (options!=NULL) options++;
69 sscanf(options, "%d", &failurecount);
70 options=strchr(options,' ');
71 if (options!=NULL) options++;
76 sscanf(options, "%f", &instfailurechance);
77 options=strchr(options,' ');
78 if (options!=NULL) options++;
82 sscanf(options, "%d", &numfailures);
83 options=strchr(options,' ');
84 if (options!=NULL) options++;
86 instaccum=failurecount;
87 instructioncount=failurecount;
88 injectinstructionfailures=1;
89 printf("Number of failures=%d\n",numfailures);
90 printf("Injecting errors with count=%d\n",failurecount);
91 printf("Injecting errors with chance=%f\n",instfailurechance);
92 } else if (strncmp(options, "-debugtask",sizeof("-debugtask")-1)==0) {
93 options=strchr(options,' ');
94 if (options!=NULL) options++;
96 printf("Debug task option on.\n");
102 int main(int argc, char **argv) {
103 GC_init(); // Initialize the garbage collector
111 /* Allocate startup object */
112 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
113 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
114 failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
115 (int (*)(void *,void *)) &comparetpd);
116 /* failedobjects=genallocatehashtable(NULL,NULL);*/
118 activetasks=createQueue();
122 flagorand(startupobject,1,0xFFFFFFFF);
124 /* Build array of strings */
126 startupobject->___parameters___=stringarray;
128 for(i=1;i<argc;i++) {
129 int length=strlen(argv[i]);
130 struct ___String___ *newstring=NewString(argv[i],length);
131 ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
137 int hashCodetpd(struct taskparamdescriptor *ftd) {
138 int hash=(int)ftd->task;
140 for(i=0;i<ftd->numParameters;i++) {
141 hash^=(int)ftd->parameterArray[i];
146 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
148 if (ftd1->task!=ftd2->task)
150 for(i=0;i<ftd1->numParameters;i++)
151 if (ftd1->parameterArray[i]!=ftd2->parameterArray[i])
156 void flagorand(void * ptr, int ormask, int andmask) {
157 int flag=((int *)ptr)[1];
158 struct RuntimeHash *flagptr=(struct RuntimeHash *)(((int*)ptr)[2]);
162 /*Remove from all queues */
163 while(flagptr!=NULL) {
164 struct RuntimeHash *next;
165 RuntimeHashget(flagptr, (int) ptr, (int *) &next);
166 RuntimeHashremove(flagptr, (int)ptr, (int) next);
171 struct QueueItem *tmpptr;
172 struct parameterwrapper * parameter=objectqueues[((int *)ptr)[0]];
174 struct RuntimeHash * prevptr=NULL;
175 while(parameter!=NULL) {
176 for(i=0;i<parameter->numberofterms;i++) {
177 int andmask=parameter->intarray[i*2];
178 int checkmask=parameter->intarray[i*2+1];
179 if ((flag&andmask)==checkmask) {
180 RuntimeHashadd(parameter->objectset, (int) ptr, (int) prevptr);
181 prevptr=parameter->objectset;
183 struct RuntimeIterator iteratorarray[MAXTASKPARAMS];
184 void * taskpointerarray[MAXTASKPARAMS];
186 int numparams=parameter->task->numParameters;
188 struct taskdescriptor * task=parameter->task;
190 for(j=0;j<numparams;j++) {
191 struct parameterwrapper *pw=(struct parameterwrapper *)task->descriptorarray[j]->queue;
193 taskpointerarray[j]=ptr;
196 RuntimeHashiterator(pw->objectset, &iteratorarray[j]);
197 if (RunhasNext(&iteratorarray[j])) {
198 taskpointerarray[j]=(void *) Runkey(&iteratorarray[j]);
199 Runnext(&iteratorarray[j]);
202 break; /* No tasks to dispatch */
206 /* Queue task items... */
209 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
211 tpd->numParameters=numparams;
212 tpd->parameterArray=RUNMALLOC(sizeof(void *)*numparams);
213 for(j=0;j<numparams;j++)
214 tpd->parameterArray[j]=taskpointerarray[j];
216 if (!gencontains(failedtasks, tpd))
217 addNewItem(activetasks, tpd);
219 /* This loop iterates to the next parameter combination */
220 for(j=0;j<numparams;j++) {
222 if ((j+1)==numparams)
226 if (RunhasNext(&iteratorarray[j])) {
227 taskpointerarray[j]=(void *) Runkey(&iteratorarray[j]);
228 Runnext(&iteratorarray[j]);
230 } else if ((j+1)!=numparams) {
231 RuntimeHashiterator(task->descriptorarray[j]->queue, &iteratorarray[j]);
242 parameter=parameter->next;
244 ((struct RuntimeHash **)ptr)[2]=prevptr;
248 /* Handler for signals. The signals catch null pointer errors and
249 arithmatic errors. */
250 void myhandler(int sig, siginfo_t *info, void *uap) {
252 printf("sig=%d\n",sig);
255 longjmp(error_handler,1);
260 struct RuntimeHash *fdtoobject;
262 void addreadfd(int fd) {
265 FD_SET(fd, &readfds);
268 void removereadfd(int fd) {
269 FD_CLR(fd, &readfds);
270 if (maxreadfd==(fd+1)) {
272 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
278 void restoreObject(void * obj) {
279 if (gencontains(failedobjects, obj)) {
280 struct tpdlist *tpd=gengettable(failedobjects, obj);
281 genfreekey(failedobjects, obj);
284 struct taskparamdescriptor *task=tpd->task;
285 genfreekey(failedtasks, task);
286 for(i=0;i<task->numParameters;i++) {
287 void *objother=task->parameterArray[i];
288 struct tpdlist *tpdother=gengettable(failedobjects, objother);
289 struct tpdlist *tmpptr;
290 genfreekey(failedobjects, objother);
291 struct tpdlist **tpdptr=&tpdother;
292 while((*tpdptr)->task!=task)
293 tpdptr=&((*tpdptr)->next);
295 (*tpdptr)=(*tpdptr)->next;
298 genputtable(failedobjects, objother, tpdother);
302 struct tpdlist *old=tpd;
311 void executetasks() {
312 void * taskpointerarray[MAXTASKPARAMS];
314 /* Set up signal handlers */
315 struct sigaction sig;
316 sig.sa_sigaction=&myhandler;
317 sig.sa_flags=SA_SIGINFO;
318 sigemptyset(&sig.sa_mask);
320 /* Catch bus errors, segmentation faults, and floating point exceptions*/
321 sigaction(SIGBUS,&sig,0);
322 sigaction(SIGSEGV,&sig,0);
323 sigaction(SIGFPE,&sig,0);
328 fdtoobject=allocateRuntimeHash(100);
330 /* Map first block of memory to protected, anonymous page */
331 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
334 while(!isEmpty(activetasks)||(maxreadfd>0)) {
336 /* Check if any filedescriptors have IO pending */
339 struct timeval timeout={0,0};
343 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
345 /* Process ready fd's */
347 for(fd=0;fd<maxreadfd;fd++) {
348 if (FD_ISSET(fd, &tmpreadfds)) {
349 /* Set ready flag on object */
351 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
352 flagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
359 /* See if there are any active tasks */
360 if (!isEmpty(activetasks)) {
362 struct QueueItem * qi=(struct QueueItem *) getTail(activetasks);
363 struct taskparamdescriptor *tpd=(struct taskparamdescriptor *) qi->objectptr;
364 removeItem(activetasks, qi);
366 /* Check if this task has failed */
367 if (gencontains(failedtasks, tpd))
370 /* Make sure that the parameters are still in the queues */
371 for(i=0;i<tpd->task->numParameters;i++) {
372 void * parameter=tpd->parameterArray[i];
373 struct parameterdescriptor * pd=tpd->task->descriptorarray[i];
374 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
375 if (!RuntimeHashcontainskey(pw->objectset, (int) parameter))
377 taskpointerarray[i]=parameter;
380 /* Checkpoint the state */
381 struct RuntimeHash * forward=allocateRuntimeHash(100);
382 struct RuntimeHash * reverse=allocateRuntimeHash(100);
383 void ** checkpoint=makecheckpoint(tpd->task->numParameters, taskpointerarray, forward, reverse);
385 if (x=setjmp(error_handler)) {
389 printf("Fatal Error=%d, Recovering!\n",x);
391 genputtable(failedtasks,tpd,tpd);
392 /* for(i=0;i<tpd->task->numParameters;i++) {
393 void *parameter=tpd->parameterArray[i];
395 // Create mapping from object -> failed tasks
396 struct tpdlist * tpnew=RUNMALLOC(sizeof(struct tpdlist));
398 if (gencontains(failedobjects, parameter)) {
399 struct tpdlist * tpdptr=gengettable(failedobjects, parameter);
400 tpnew->next=tpdptr->next;
404 genputtable(failedobjects, parameter, tpnew);
408 restorecheckpoint(tpd->task->numParameters, taskpointerarray, checkpoint, forward, reverse);
410 if (injectfailures) {
411 if ((((double)random())/RAND_MAX)<failurechance) {
412 printf("\nINJECTING TASK FAILURE to %s\n", tpd->task->name);
413 longjmp(error_handler,10);
416 /* Actually call task */
418 printf("ENTER %s count=%d\n",tpd->task->name, (instaccum-instructioncount));
419 ((void (*) (void **)) tpd->task->taskptr)(taskpointerarray);
420 printf("EXIT %s count=%d\n",tpd->task->name, (instaccum-instructioncount));
422 ((void (*) (void **)) tpd->task->taskptr)(taskpointerarray);
429 /* This function processes the task information to create queues for
430 each parameter type. */
432 void processtasks() {
434 for(i=0;i<numtasks;i++) {
435 struct taskdescriptor * task=taskarray[i];
438 for(j=0;j<task->numParameters;j++) {
439 struct parameterdescriptor *param=task->descriptorarray[j];
440 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
441 struct parameterwrapper ** ptr=&objectqueues[param->type];
443 param->queue=parameter;
444 parameter->objectset=allocateRuntimeHash(10);
445 parameter->numberofterms=param->numberterms;
446 parameter->intarray=param->intarray;
447 parameter->task=task;
448 /* Link new queue in */
458 /* This function inject failures */
460 void injectinstructionfailure() {
462 if (injectinstructionfailures) {
467 instructioncount=failurecount;
468 instaccum+=failurecount;
469 if ((((double)random())/RAND_MAX)<instfailurechance) {
470 printf("FAILURE!!!\n");
471 longjmp(error_handler,11);
478 int ___Object______hashCode____(struct ___Object___ * ___this___) {
479 return (int) ___this___;
482 int ___Object______getType____(struct ___Object___ * ptr) {
483 return ((int *)ptr)[0];
486 void ___System______printString____L___String___(struct ___String___ * s) {
487 struct ArrayObject * chararray=s->___value___;
489 int offset=s->___offset___;
490 for(i=0;i<s->___count___;i++) {
491 short s= ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i+offset];
496 void * allocate_new(int type) {
497 void * v=FREEMALLOC(classsize[type]);
502 struct ArrayObject * allocate_newarray(int type, int length) {
503 struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
505 v->___length___=length;
509 struct ___String___ * NewString(const char *str,int length) {
510 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
511 struct ___String___ * strobj=allocate_new(STRINGTYPE);
513 strobj->___value___=chararray;
514 strobj->___count___=length;
515 strobj->___offset___=0;
517 for(i=0;i<length;i++) {
518 ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i]; }
522 /* Generated code calls this if we fail a bounds check */
523 void failedboundschk() {
525 printf("Array out of bounds\n");
528 longjmp(error_handler,2);
534 longjmp(error_handler,4);
536 printf("Aborting\n");