my changes
[IRC.git] / Robust / src / Runtime / runtime.c
1 #include "runtime.h"
2 #include "structdefs.h"
3 #include <signal.h>
4 #include "mem.h"
5 #include<fcntl.h>
6 #include<errno.h>
7 #include<signal.h>
8 #include<stdio.h>
9 #include "option.h"
10 #ifdef DSTM
11 #include "dstm.h"
12 #endif
13
14 extern int classsize[];
15 jmp_buf error_handler;
16 int instructioncount;
17
18 char *options;
19 int injectfailures=0;
20 float failurechance=0;
21 int debugtask=0;
22 int injectinstructionfailures;
23 int failurecount;
24 float instfailurechance=0;
25 int numfailures;
26 int instaccum=0;
27 #ifdef DMALLOC
28 #include "dmalloc.h"
29 #endif
30
31
32
33
34 void exithandler(int sig, siginfo_t *info, void * uap) {
35   exit(0);
36 }
37
38 void initializeexithandler() {
39   struct sigaction sig;
40   sig.sa_sigaction=&exithandler;
41   sig.sa_flags=SA_SIGINFO;
42   sigemptyset(&sig.sa_mask);
43   sigaction(SIGUSR2, &sig, 0);
44 }
45
46
47 /* This function inject failures */
48
49 void injectinstructionfailure() {
50 #ifdef TASK
51   if (injectinstructionfailures) {
52     if (numfailures==0)
53       return;
54     instructioncount=failurecount;    
55     instaccum+=failurecount;
56     if ((((double)random())/RAND_MAX)<instfailurechance) {
57       if (numfailures>0)
58         numfailures--;
59       printf("FAILURE!!! %d\n",numfailures);
60       longjmp(error_handler,11);
61     }
62   }
63 #else
64 #ifdef THREADS
65   if (injectinstructionfailures) {
66     if (numfailures==0)
67       return;
68     instaccum+=failurecount;
69     if ((((double)random())/RAND_MAX)<instfailurechance) {
70       if (numfailures>0)
71         numfailures--;
72       printf("FAILURE!!! %d\n",numfailures);
73       threadexit();
74     }
75   }
76 #endif
77 #endif
78 }
79
80 void CALL01(___System______printString____L___String___,struct ___String___ * ___s___) {
81     struct ArrayObject * chararray=VAR(___s___)->___value___;
82     int i;
83     int offset=VAR(___s___)->___offset___;
84     for(i=0;i<VAR(___s___)->___count___;i++) {
85         short sc=((short *)(((char *)& chararray->___length___)+sizeof(int)))[i+offset];
86         putchar(sc);
87     }
88 }
89
90 /* Object allocation function */
91
92 #ifdef DSTM
93 void * allocate_newglobal(void * ptr, int type) {
94   struct ___Object___ * v=(struct ___Object___ *) dstmalloc(classsize[type]);
95   v->type=type;
96 #ifdef THREADS
97   v->tid=0;
98   v->lockentry=0;
99   v->lockcount=0;
100 #endif
101   return v;
102 }
103
104 /* Array allocation function */
105
106 struct ArrayObject * allocate_newarrayglobal(void * ptr, int type, int length) {
107   struct ArrayObject * v=(struct ArrayObject *)dstmalloc(classsize[type]);
108   v->type=type;
109   if (length<0) {
110     printf("ERROR: negative array\n");
111     return NULL;
112   }
113   v->___length___=length;
114 #ifdef THREADS
115   v->tid=0;
116   v->lockentry=0;
117   v->lockcount=0;
118 #endif
119   return v;
120 }
121 #endif
122
123
124 #ifdef PRECISE_GC
125 void * allocate_new(void * ptr, int type) {
126   struct ___Object___ * v=(struct ___Object___ *) mygcmalloc((struct garbagelist *) ptr, classsize[type]);
127   v->type=type;
128 #ifdef THREADS
129   v->tid=0;
130   v->lockentry=0;
131   v->lockcount=0;
132 #endif
133   return v;
134 }
135
136 /* Array allocation function */
137
138 struct ArrayObject * allocate_newarray(void * ptr, int type, int length) {
139   struct ArrayObject * v=mygcmalloc((struct garbagelist *) ptr, sizeof(struct ArrayObject)+length*classsize[type]);
140   v->type=type;
141   if (length<0) {
142     printf("ERROR: negative array\n");
143     return NULL;
144   }
145   v->___length___=length;
146 #ifdef THREADS
147   v->tid=0;
148   v->lockentry=0;
149   v->lockcount=0;
150 #endif
151   return v;
152 }
153
154 #else
155 void * allocate_new(int type) {
156   void * v=FREEMALLOC(classsize[type]);
157   *((int *)v)=type;
158   return v;
159 }
160
161 /* Array allocation function */
162
163 struct ArrayObject * allocate_newarray(int type, int length) {
164   struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
165   v->type=type;
166   v->___length___=length;
167   return v;
168 }
169 #endif
170
171
172 /* Converts C character arrays into Java strings */
173 #ifdef PRECISE_GC
174 struct ___String___ * NewString(void * ptr, const char *str,int length) {
175 #else
176 struct ___String___ * NewString(const char *str,int length) {
177 #endif
178   int i;
179 #ifdef PRECISE_GC
180   struct ArrayObject * chararray=allocate_newarray((struct garbagelist *)ptr, CHARARRAYTYPE, length);
181   int ptrarray[]={1, (int) ptr, (int) chararray};
182   struct ___String___ * strobj=allocate_new((struct garbagelist *) &ptrarray, STRINGTYPE);
183   chararray=(struct ArrayObject *) ptrarray[2];
184 #else
185   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
186   struct ___String___ * strobj=allocate_new(STRINGTYPE);
187 #endif
188   strobj->___value___=chararray;
189   strobj->___count___=length;
190   strobj->___offset___=0;
191
192   for(i=0;i<length;i++) {
193     ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i];  }
194   return strobj;
195 }
196
197 /* Generated code calls this if we fail a bounds check */
198
199 void failedboundschk() {
200 #ifndef TASK
201   printf("Array out of bounds\n");
202 #ifdef THREADS
203   threadexit();
204 #else
205   exit(-1);
206 #endif
207 #else
208   longjmp(error_handler,2);
209 #endif
210 }
211
212 /* Abort task call */
213 void abort_task() {
214 #ifdef TASK
215   longjmp(error_handler,4);
216 #else
217   printf("Aborting\n");
218   exit(-1);
219 #endif
220 }