2 #include "structdefs.h"
13 extern int classsize[];
14 jmp_buf error_handler;
17 #include "checkpoint.h"
19 #include "SimpleHash.h"
20 #include "GenericHashtable.h"
21 #include <sys/select.h>
22 #include <sys/socket.h>
24 #include <arpa/inet.h>
27 #include "instrument.h"
30 struct Queue * activetasks;
31 struct parameterwrapper * objectqueues[NUMCLASSES];
32 struct genhashtable * failedtasks;
34 int main(int argc, char **argv) {
41 /* Allocate startup object */
42 struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
43 struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc-1);
44 failedtasks=genallocatehashtable((unsigned int (*)(void *)) &hashCodetpd,
45 (int (*)(void *,void *)) &comparetpd);
47 activetasks=createQueue();
51 flagorand(startupobject,1,0xFFFFFFFF);
53 /* Build array of strings */
55 startupobject->___parameters___=stringarray;
58 int length=strlen(argv[i]);
59 struct ___String___ *newstring=NewString(argv[i],length);
60 ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i-1]=newstring;
66 int hashCodetpd(struct taskparamdescriptor *ftd) {
67 int hash=(int)ftd->task;
69 for(i=0;i<ftd->numParameters;i++) {
70 hash^=(int)ftd->parameterArray[i];
75 int comparetpd(struct taskparamdescriptor *ftd1, struct taskparamdescriptor *ftd2) {
77 if (ftd1->task!=ftd2->task)
79 for(i=0;i<ftd1->numParameters;i++)
80 if (ftd1->parameterArray[i]!=ftd2->parameterArray[i])
85 void flagorand(void * ptr, int ormask, int andmask) {
86 int flag=((int *)ptr)[1];
87 struct RuntimeHash *flagptr=(struct RuntimeHash *)(((int*)ptr)[2]);
91 /*Remove from all queues */
92 while(flagptr!=NULL) {
93 struct RuntimeHash *next;
94 RuntimeHashget(flagptr, (int) ptr, (int *) &next);
95 RuntimeHashremove(flagptr, (int)ptr, (int) next);
100 struct QueueItem *tmpptr;
101 struct parameterwrapper * parameter=objectqueues[((int *)ptr)[0]];
103 struct RuntimeHash * prevptr=NULL;
104 while(parameter!=NULL) {
105 for(i=0;i<parameter->numberofterms;i++) {
106 int andmask=parameter->intarray[i*2];
107 int checkmask=parameter->intarray[i*2+1];
108 if ((flag&andmask)==checkmask) {
109 RuntimeHashadd(parameter->objectset, (int) ptr, (int) prevptr);
110 prevptr=parameter->objectset;
112 struct RuntimeIterator iteratorarray[MAXTASKPARAMS];
113 void * taskpointerarray[MAXTASKPARAMS];
115 int numparams=parameter->task->numParameters;
117 struct taskdescriptor * task=parameter->task;
119 for(j=0;j<numparams;j++) {
120 struct parameterwrapper *pw=(struct parameterwrapper *)task->descriptorarray[j]->queue;
122 taskpointerarray[j]=ptr;
125 RuntimeHashiterator(pw->objectset, &iteratorarray[j]);
126 if (RunhasNext(&iteratorarray[j]))
127 taskpointerarray[j]=(void *) Runnext(&iteratorarray[j]);
129 break; /* No tasks to dispatch */
132 /* Queue task items... */
135 struct taskparamdescriptor *tpd=RUNMALLOC(sizeof(struct taskparamdescriptor));
137 tpd->numParameters=numparams;
138 tpd->parameterArray=RUNMALLOC(sizeof(void *)*numparams);
139 for(j=0;j<numparams;j++)
140 tpd->parameterArray[j]=taskpointerarray[j];
142 if (!gencontains(failedtasks, tpd))
143 addNewItem(activetasks, tpd);
145 /* This loop iterates to the next paramter combination */
146 for(j=0;j<numparams;j++) {
148 if ((j+1)==numparams)
152 if (RunhasNext(&iteratorarray[j])) {
153 taskpointerarray[j]=(void *) Runnext(&iteratorarray[j]);
155 } else if ((j+1)!=numparams) {
156 RuntimeHashiterator(task->descriptorarray[j]->queue, &iteratorarray[j]);
167 parameter=parameter->next;
169 ((struct RuntimeHash **)ptr)[2]=prevptr;
173 /* Handler for signals */
174 void myhandler(int sig, struct __siginfo *info, void *uap) {
176 printf("sig=%d\n",sig);
179 longjmp(error_handler,1);
185 struct RuntimeHash *fdtoobject;
187 void addreadfd(int fd) {
190 FD_SET(fd, &readfds);
193 void removereadfd(int fd) {
194 FD_CLR(fd, &readfds);
195 if (maxreadfd==(fd+1)) {
197 while(maxreadfd>0&&!FD_ISSET(maxreadfd-1, &readfds))
202 void executetasks() {
203 void * taskpointerarray[MAXTASKPARAMS];
205 /* Set up signal handlers */
206 struct sigaction sig;
207 sig.sa_sigaction=&myhandler;
208 sig.sa_flags=SA_SIGINFO;
209 sigemptyset(&sig.sa_mask);
211 /* Catch bus errors, segmentation faults, and floating point exceptions*/
212 sigaction(SIGBUS,&sig,0);
213 sigaction(SIGSEGV,&sig,0);
214 sigaction(SIGFPE,&sig,0);
219 fdtoobject=allocateRuntimeHash(100);
221 /* Map first block of memory to protected, anonymous page */
222 mmap(0, 0x1000, 0, MAP_SHARED|MAP_FIXED|MAP_ANON, -1, 0);
225 while(!isEmpty(activetasks)||(maxreadfd>0)) {
229 struct timeval timeout={0,0};
232 FD_COPY(&readfds, &tmpreadfds);
233 numselect=select(maxreadfd, &tmpreadfds, NULL, NULL, &timeout);
235 /* Process ready fd's */
237 for(fd=0;fd<maxreadfd;fd++) {
238 if (FD_ISSET(fd, &tmpreadfds)) {
239 /* Set ready flag on object */
241 if (RuntimeHashget(fdtoobject, fd,(int *) &objptr)) {
242 flagorand(objptr,1,0xFFFFFFFF); /* Set the first flag to 1 */
249 if (!isEmpty(activetasks)) {
251 struct QueueItem * qi=(struct QueueItem *) getTail(activetasks);
252 struct taskparamdescriptor *tpd=(struct taskparamdescriptor *) qi->objectptr;
253 removeItem(activetasks, qi);
255 for(i=0;i<tpd->task->numParameters;i++) {
256 void * parameter=tpd->parameterArray[i];
257 struct parameterdescriptor * pd=tpd->task->descriptorarray[i];
258 struct parameterwrapper *pw=(struct parameterwrapper *) pd->queue;
259 if (!RuntimeHashcontainskey(pw->objectset, (int) parameter))
261 taskpointerarray[i]=parameter;
264 struct RuntimeHash * forward=allocateRuntimeHash(100);
265 struct RuntimeHash * reverse=allocateRuntimeHash(100);
266 void ** checkpoint=makecheckpoint(tpd->task->numParameters, taskpointerarray, forward, reverse);
267 if (setjmp(error_handler)) {
271 printf("Recovering\n");
273 genputtable(failedtasks,tpd,tpd);
274 restorecheckpoint(tpd->task->numParameters, taskpointerarray, checkpoint, forward, reverse);
276 /* Actually call task */
277 ((void (*) (void **)) tpd->task->taskptr)(taskpointerarray);
284 void processtasks() {
286 for(i=0;i<numtasks;i++) {
287 struct taskdescriptor * task=taskarray[i];
290 for(j=0;j<task->numParameters;j++) {
291 struct parameterdescriptor *param=task->descriptorarray[j];
292 struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
293 struct parameterwrapper ** ptr=&objectqueues[param->type];
295 param->queue=parameter;
296 parameter->objectset=allocateRuntimeHash(10);
297 parameter->numberofterms=param->numberterms;
298 parameter->intarray=param->intarray;
299 parameter->task=task;
300 /* Link new queue in */
310 int ___ServerSocket______createSocket____I(struct ___ServerSocket___ * sock, int port) {
314 struct sockaddr_in sin;
316 bzero (&sin, sizeof (sin));
317 sin.sin_family = AF_INET;
318 sin.sin_port = htons (port);
319 sin.sin_addr.s_addr = htonl (INADDR_ANY);
320 fd=socket(AF_INET, SOCK_STREAM, 0);
324 printf("createSocket error #1\n");
326 longjmp(error_handler,5);
329 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof (n)) < 0) {
333 printf("createSocket error #2\n");
335 longjmp(error_handler, 6);
337 fcntl(fd, F_SETFD, 1);
338 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
341 if (bind(fd, (struct sockaddr *) &sin, sizeof(sin))<0) {
345 printf("createSocket error #3\n");
347 longjmp(error_handler, 7);
351 if (listen(fd, 5)<0) {
355 printf("createSocket error #4\n");
357 longjmp(error_handler, 8);
360 /* Store the fd/socket object mapping */
361 RuntimeHashadd(fdtoobject, fd, (int) sock);
366 int ___ServerSocket______nativeaccept____L___Socket___(struct ___ServerSocket___ * serversock, struct ___Socket___ * sock) {
367 struct sockaddr_in sin;
368 unsigned int sinlen=sizeof(sin);
369 int fd=serversock->___fd___;
371 newfd=accept(fd, (struct sockaddr *)&sin, &sinlen);
377 printf("acceptSocket error #1\n");
379 longjmp(error_handler, 9);
381 fcntl(newfd, F_SETFL, fcntl(fd, F_GETFL)|O_NONBLOCK);
383 RuntimeHashadd(fdtoobject, newfd, (int) sock);
385 flagorand(serversock,0,0xFFFFFFFE);
390 void ___Socket______nativeWrite_____AR_B(struct ___Socket___ * sock, struct ArrayObject * ao) {
391 int fd=sock->___fd___;
392 int length=ao->___length___;
393 char * charstr=((char *)& ao->___length___)+sizeof(int);
394 int bytewritten=write(fd, charstr, length);
395 if (bytewritten!=length) {
396 printf("ERROR IN NATIVEWRITE\n");
398 flagorand(sock,0,0xFFFFFFFE);
401 int ___Socket______nativeRead_____AR_B(struct ___Socket___ * sock, struct ArrayObject * ao) {
402 int fd=sock->___fd___;
403 int length=ao->___length___;
404 char * charstr=((char *)& ao->___length___)+sizeof(int);
405 int byteread=read(fd, charstr, length);
408 printf("ERROR IN NATIVEREAD\n");
410 flagorand(sock,0,0xFFFFFFFE);
414 void ___Socket______nativeClose____(struct ___Socket___ * sock) {
415 int fd=sock->___fd___;
417 RuntimeHashget(fdtoobject, fd, &data);
418 RuntimeHashremove(fdtoobject, fd, data);
421 flagorand(sock,0,0xFFFFFFFE);
425 int ___Object______hashcode____(struct ___Object___ * ___this___) {
426 return (int) ___this___;
429 void ___System______printString____L___String___(struct ___String___ * s) {
430 struct ArrayObject * chararray=s->___string___;
432 for(i=0;i<chararray->___length___;i++) {
433 short s= ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i];
438 void * allocate_new(int type) {
439 void * v=FREEMALLOC(classsize[type]);
444 struct ArrayObject * allocate_newarray(int type, int length) {
445 struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
447 v->___length___=length;
451 struct ___String___ * NewString(const char *str,int length) {
452 struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
453 struct ___String___ * strobj=allocate_new(STRINGTYPE);
455 strobj->___string___=chararray;
456 for(i=0;i<length;i++) {
457 ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i]; }
461 void failedboundschk() {
463 printf("Array out of bounds\n");
466 longjmp(error_handler,2);
470 void failednullptr() {
472 printf("Dereferenced a null pointer\n");
475 longjmp(error_handler,3);
481 printf("Aborting\n");
484 longjmp(error_handler,4);