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