-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcep.cpp
374 lines (305 loc) · 12 KB
/
excep.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#include "std.h"
#include "rope.h"
#include "lock.h"
#include "symbol.h"
#include "excep.h"
#include "SpmtThread.h"
#include "Loggers.h"
#include "Helper.h"
static Class *ste_class, *ste_array_class, *throw_class, *vmthrow_class;
static MethodBlock *vmthrow_init_mb;
static int backtrace_offset;
static int inited = FALSE;
static Class *exceptions[MAX_EXCEPTION_ENUM];
static int exception_symbols[] = {
EXCEPTIONS_DO(SYMBOL_NAME_ENUM)
};
void initialiseException() {
FieldBlock *bcktrce;
int i;
ste_class = findSystemClass0(SYMBOL(java_lang_StackTraceElement));
ste_array_class = findArrayClass(SYMBOL(array_java_lang_StackTraceElement));
vmthrow_class = findSystemClass0(SYMBOL(java_lang_VMThrowable));
throw_class = findSystemClass0(SYMBOL(java_lang_Throwable));
bcktrce = findField(vmthrow_class, SYMBOL(backtrace), SYMBOL(sig_java_lang_Object));
vmthrow_init_mb = findMethod(ste_class, SYMBOL(object_init),
SYMBOL(_java_lang_String_I_java_lang_String_java_lang_String_Z__V));
if((bcktrce == NULL) || (vmthrow_init_mb == NULL)) {
jam_fprintf(stderr, "Error initialising VM (initialiseException)\n");
exitVM(1);
}
CLASS_CB(vmthrow_class)->flags |= VMTHROWABLE;
backtrace_offset = bcktrce->offset;
registerStaticClassRef(&ste_class);
registerStaticClassRef(&ste_array_class);
registerStaticClassRef(&vmthrow_class);
registerStaticClassRef(&throw_class);
/* Load and register the exceptions used within the VM.
These are preloaded to speed up access. The VM will
abort if any can't be loaded */
for(i = 0; i < MAX_EXCEPTION_ENUM; i++) {
exceptions[i] = findSystemClass0(symbol_values[exception_symbols[i]]);
registerStaticClassRef(&exceptions[i]);
}
inited = TRUE;
}
Object *exceptionOccurred() {
//return getExecEnv()->exception;
return g_get_current_thread()->exception;
}
void setException(Object *exp) {
//getExecEnv()->exception = exp;
assert(g_get_current_spmt_thread()->is_certain_mode());
g_get_current_thread()->exception = exp;
}
void clearException() {
assert(g_get_current_spmt_thread()->is_certain_mode());
//ExecEnv *ee = getExecEnv();
// if(ee->overflow) {
// ee->overflow = FALSE;
// // ee->stack_end -= STACK_RED_ZONE_SIZE;
// }
//ee->exception = NULL;
g_get_current_thread()->exception = 0;
}
void signalChainedExceptionClass(Class *exception, const char* message, Object *cause) {
SpmtThread* current_st = g_get_current_spmt_thread();
current_st->before_signal_exception(exception);
Object *exp = allocObject(exception);
Object *str = message == NULL ? NULL : Cstr2String(message);
MethodBlock *init = lookupMethod(exception, SYMBOL(object_init),
SYMBOL(_java_lang_String__V));
if(exp && init) {
DummyFrame dummy;
executeMethod(&dummy, exp, init, str);
if(cause && !exceptionOccurred()) {
MethodBlock *mb = lookupMethod(exception, SYMBOL(initCause),
SYMBOL(_java_lang_Throwable__java_lang_Throwable));
if(mb) {
DummyFrame dummy;
executeMethod(&dummy, exp, mb, cause);
}
}
setException(exp);
}
}
void signalChainedExceptionName(char *excep_name, char *message, Object *cause) {
if(!inited) {
jam_fprintf(stderr, "Exception occurred while VM initialising.\n");
if(message)
jam_fprintf(stderr, "%s: %s\n", excep_name, message);
else
jam_fprintf(stderr, "%s\n", excep_name);
exit(1);
} else {
Class *exception = findSystemClass(excep_name);
if(!exceptionOccurred())
signalChainedExceptionClass(exception, message, cause);
}
}
void signalChainedExceptionEnum(int excep_enum, const char* message, Object *cause) {
if(!inited) {
const char *excep_name = symbol_values[exception_symbols[excep_enum]];
jam_fprintf(stderr, "Exception occurred while VM initialising.\n");
if(message)
jam_fprintf(stderr, "%s: %s\n", excep_name, message);
else
jam_fprintf(stderr, "%s\n", excep_name);
exit(1);
}
signalChainedExceptionClass(exceptions[excep_enum], message, cause);
}
void printException() {
//ExecEnv *ee = getExecEnv();
//Object *exception = ee->exception;
Object *exception = g_get_current_thread()->exception;
if(exception != NULL) {
MethodBlock *mb = lookupMethod(exception->classobj, SYMBOL(printStackTrace),
SYMBOL(___V));
clearException();
DummyFrame dummy;
executeMethod(&dummy, exception, mb);
/* If we're really low on memory we might have been able to throw
* OutOfMemory, but then been unable to print any part of it! In
* this case the VM just seems to stop... */
//if(ee->exception) {
if(g_get_current_thread()->exception) {
jam_fprintf(stderr, "Exception occurred while printing exception (%s)...\n",
//CLASS_CB(ee->exception->classobj)->name);
CLASS_CB(g_get_current_thread()->exception->classobj)->name);
jam_fprintf(stderr, "Original exception was %s\n", CLASS_CB(exception->classobj)->name);
}
}
}
/*
mb exception occured in this method
exception exception
pc_pntr points to instruction which caused the exception
*/
CodePntr
findCatchBlockInMethod(MethodBlock *mb, Class *exception, CodePntr pc_pntr)
{
ExceptionTableEntry *table = mb->exception_table;
int size = mb->exception_table_size;
int pc = pc_pntr - ((CodePntr)mb->code);
int i;
for(i = 0; i < size; i++)
if ((pc >= table[i].start_pc) and (pc < table[i].end_pc)) {
/* If the catch_type is 0 it's a finally block, which matches
any exception. Otherwise, the thrown exception class must
be an instance of the caught exception class to catch it */
if (table[i].catch_type != 0) {
Class *caught_class = resolveClass(mb->classobj, table[i].catch_type, FALSE);
if (caught_class == NULL) {
clearException();
continue;
}
if (!isInstanceOf(caught_class, exception))
continue;
}
return ((CodePntr)mb->code) + table[i].handler_pc;
}
return NULL;
}
// 本函数已被替代
CodePntr
findCatchBlock(Class *exception)
{
//Frame *frame = getExecEnv()->last_frame;
Frame* frame = g_get_current_spmt_thread()->get_current_mode()->frame;
CodePntr handler_pc = NULL;
MINILOG(c_exception_logger, "#" << threadSelf()->get_current_spmt_thread()->id() << " finding handler"
<< " in: " << info(frame)
<< " on: #" << frame->get_object()->get_st()->id()
);
while (((handler_pc = findCatchBlockInMethod(frame->mb, exception, frame->last_pc)) == NULL)
and (frame->prev->mb != NULL)) {
if (frame->mb->is_synchronized()) {
Object *sync_ob = frame->mb->access_flags & ACC_STATIC ?
(Object*)frame->mb->classobj : (Object*)frame->lvars[0];
objectUnlock(sync_ob);
}
frame = frame->prev;
MINILOG(c_exception_logger, "#" << threadSelf()->get_current_spmt_thread()->id() << " finding handler"
<< " in: " << info(frame)
<< " on: #" << frame->get_object()->get_st()->id()
);
}
//getExecEnv()->last_frame = frame;
threadSelf()->get_current_spmt_thread()->get_current_mode()->frame = frame;
return handler_pc;
}
int mapPC2LineNo(MethodBlock *mb, CodePntr pc_pntr) {
int pc = pc_pntr - (CodePntr) mb->code;
int i;
if(mb->line_no_table_size > 0) {
for(i = mb->line_no_table_size-1; i && pc < mb->line_no_table[i].start_pc; i--);
return mb->line_no_table[i].line_no;
}
return -1;
}
//Object *setStackTrace0(ExecEnv *ee, int max_depth)
Object *setStackTrace0(int max_depth)
{
//assert(false);
Frame* bottom;
//Frame* last = ee->last_frame;
SpmtThread* current_st = threadSelf()->get_current_spmt_thread();
Frame* last = current_st->get_current_mode()->frame;
Object *array, *vmthrwble;
uintptr_t *data;
int depth = 0;
if(last->prev == NULL) {
if((array = allocTypeArray(sizeof(uintptr_t) == 4 ? T_INT : T_LONG, 0)) == NULL)
return NULL;
goto out2;
}
// for(; last->mb != NULL && isInstanceOf(vmthrow_class, last->mb->classobj);
// last = last->prev);
while (last->mb != NULL and isInstanceOf(vmthrow_class, last->mb->classobj)) {
last = last->prev;
}
// for(; last->mb != NULL && isInstanceOf(throw_class, last->mb->classobj);
// last = last->prev);
while (last->mb != NULL and isInstanceOf(throw_class, last->mb->classobj)) {
last = last->prev;
}
bottom = last;
do {
// for(; last->mb != NULL; last = last->prev, depth++)
// if(depth == max_depth)
// goto out;
while (last->mb != NULL) {
if(depth == max_depth)
goto out;
last = last->prev;
depth++;
}
} while((last = last->prev)->prev != NULL);
out:
if((array = allocTypeArray(sizeof(uintptr_t) == 4 ? T_INT : T_LONG, depth*2)) == NULL)
return NULL;
data = (uintptr_t*)ARRAY_DATA(array);
depth = 0;
do {
for(; bottom->mb != NULL; bottom = bottom->prev) {
if(depth == max_depth)
goto out2;
data[depth++] = (uintptr_t)bottom->mb;
data[depth++] = (uintptr_t)bottom->last_pc;
}
} while((bottom = bottom->prev)->prev != NULL);
out2:
if((vmthrwble = allocObject(vmthrow_class)))
INST_DATA(vmthrwble)[backtrace_offset] = (uintptr_t)array;
return vmthrwble;
}
Object *convertStackTrace(Object *vmthrwble) {
Object *array, *ste_array;
int depth, i, j;
uintptr_t *src;
Object **dest;
if((array = (Object *)INST_DATA(vmthrwble)[backtrace_offset]) == NULL)
return NULL;
src = (uintptr_t*)ARRAY_DATA(array);
depth = ARRAY_LEN(array);
if((ste_array = allocArray(ste_array_class, depth/2, sizeof(Object*))) == NULL)
return NULL;
dest = (Object**)ARRAY_DATA(ste_array);
for(i = 0, j = 0; i < depth; j++) {
MethodBlock *mb = (MethodBlock*)src[i++];
CodePntr pc = (CodePntr)src[i++];
ClassBlock *cb = CLASS_CB(mb->classobj);
char *dot_name = slash2dots(cb->name);
int isNative = mb->is_native();
Object *filename = isNative ? NULL : (cb->source_file_name ?
createString(cb->source_file_name) : NULL);
Object *classname = createString(dot_name);
Object *methodname = createString(mb->name);
Object *ste = allocObject(ste_class);
sysFree(dot_name);
if(exceptionOccurred())
return NULL;
DummyFrame dummy;
executeMethod(&dummy, ste, vmthrow_init_mb, filename, isNative ? -1 : mapPC2LineNo(mb, pc),
classname, methodname, isNative);
if(exceptionOccurred())
return NULL;
dest[j] = ste;
}
return ste_array;
}
/* GC support for marking classes referenced by a VMThrowable.
In rare circumstances a stack backtrace may hold the only
reference to a class */
void markVMThrowable(Object *vmthrwble, int mark, int mark_soft_refs) {
Object *array;
if((array = (Object *)INST_DATA(vmthrwble)[backtrace_offset]) != NULL) {
uintptr_t *src = (uintptr_t*)ARRAY_DATA(array);
int i, depth = ARRAY_LEN(array);
for(i = 0; i < depth; i += 2) {
MethodBlock *mb = (MethodBlock*)src[i];
markObject(mb->classobj, mark, mark_soft_refs);
}
}
}