]>
Commit | Line | Data |
---|---|---|
a12fe13d TT |
1 | // defineclass.cc - defining a class from .class format. |
2 | ||
3 | /* Copyright (C) 2001 Free Software Foundation | |
4 | ||
5 | This file is part of libgcj. | |
6 | ||
7 | This software is copyrighted work licensed under the terms of the | |
8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for | |
9 | details. */ | |
10 | ||
11 | // Writte by Tom Tromey <tromey@redhat.com> | |
12 | ||
13 | #include <config.h> | |
14 | ||
15 | #include <jvm.h> | |
16 | #include <gcj/cni.h> | |
17 | #include <java-insns.h> | |
18 | #include <java-interp.h> | |
19 | ||
75b17b74 JS |
20 | #ifdef INTERPRETER |
21 | ||
a12fe13d TT |
22 | #include <java/lang/Class.h> |
23 | #include <java/lang/VerifyError.h> | |
24 | #include <java/lang/Throwable.h> | |
25 | #include <java/lang/reflect/Modifier.h> | |
60440707 | 26 | #include <java/lang/StringBuffer.h> |
a12fe13d TT |
27 | |
28 | ||
29 | // TO DO | |
30 | // * read more about when classes must be loaded | |
31 | // * there are bugs with boolean arrays? | |
32 | // * class loader madness | |
33 | // * Lots and lots of debugging and testing | |
34 | // * type representation is still ugly. look for the big switches | |
35 | // * at least one GC problem :-( | |
36 | ||
37 | ||
38 | // This is global because __attribute__ doesn't seem to work on static | |
39 | // methods. | |
60440707 TT |
40 | static void verify_fail (char *msg, jint pc = -1) |
41 | __attribute__ ((__noreturn__)); | |
a12fe13d TT |
42 | |
43 | class _Jv_BytecodeVerifier | |
44 | { | |
45 | private: | |
46 | ||
47 | static const int FLAG_INSN_START = 1; | |
48 | static const int FLAG_BRANCH_TARGET = 2; | |
49 | static const int FLAG_JSR_TARGET = 4; | |
50 | ||
51 | struct state; | |
52 | struct type; | |
53 | struct subr_info; | |
54 | ||
55 | // The current PC. | |
56 | int PC; | |
57 | // The PC corresponding to the start of the current instruction. | |
58 | int start_PC; | |
59 | ||
60 | // The current state of the stack, locals, etc. | |
61 | state *current_state; | |
62 | ||
63 | // We store the state at branch targets, for merging. This holds | |
64 | // such states. | |
65 | state **states; | |
66 | ||
67 | // We keep a linked list of all the PCs which we must reverify. | |
68 | // The link is done using the PC values. This is the head of the | |
69 | // list. | |
70 | int next_verify_pc; | |
71 | ||
72 | // We keep some flags for each instruction. The values are the | |
73 | // FLAG_* constants defined above. | |
74 | char *flags; | |
75 | ||
76 | // We need to keep track of which instructions can call a given | |
77 | // subroutine. FIXME: this is inefficient. We keep a linked list | |
78 | // of all calling `jsr's at at each jsr target. | |
79 | subr_info **jsr_ptrs; | |
80 | ||
81 | // The current top of the stack, in terms of slots. | |
82 | int stacktop; | |
83 | // The current depth of the stack. This will be larger than | |
84 | // STACKTOP when wide types are on the stack. | |
85 | int stackdepth; | |
86 | ||
87 | // The bytecode itself. | |
88 | unsigned char *bytecode; | |
89 | // The exceptions. | |
90 | _Jv_InterpException *exception; | |
91 | ||
92 | // Defining class. | |
93 | jclass current_class; | |
94 | // This method. | |
95 | _Jv_InterpMethod *current_method; | |
96 | ||
97 | // This enum holds a list of tags for all the different types we | |
98 | // need to handle. Reference types are treated specially by the | |
99 | // type class. | |
100 | enum type_val | |
101 | { | |
102 | void_type, | |
103 | ||
104 | // The values for primitive types are chosen to correspond to values | |
105 | // specified to newarray. | |
106 | boolean_type = 4, | |
107 | char_type = 5, | |
108 | float_type = 6, | |
109 | double_type = 7, | |
110 | byte_type = 8, | |
111 | short_type = 9, | |
112 | int_type = 10, | |
113 | long_type = 11, | |
114 | ||
115 | // Used when overwriting second word of a double or long in the | |
116 | // local variables. Also used after merging local variable states | |
117 | // to indicate an unusable value. | |
118 | unsuitable_type, | |
119 | return_address_type, | |
120 | continuation_type, | |
121 | ||
122 | // Everything after `reference_type' must be a reference type. | |
123 | reference_type, | |
124 | null_type, | |
125 | unresolved_reference_type, | |
126 | uninitialized_reference_type, | |
127 | uninitialized_unresolved_reference_type | |
128 | }; | |
129 | ||
130 | // Return the type_val corresponding to a primitive signature | |
131 | // character. For instance `I' returns `int.class'. | |
132 | static type_val get_type_val_for_signature (jchar sig) | |
133 | { | |
134 | type_val rt; | |
135 | switch (sig) | |
136 | { | |
137 | case 'Z': | |
138 | rt = boolean_type; | |
139 | break; | |
140 | case 'C': | |
141 | rt = char_type; | |
142 | break; | |
143 | case 'S': | |
144 | rt = short_type; | |
145 | break; | |
146 | case 'I': | |
147 | rt = int_type; | |
148 | break; | |
149 | case 'J': | |
150 | rt = long_type; | |
151 | break; | |
152 | case 'F': | |
153 | rt = float_type; | |
154 | break; | |
155 | case 'D': | |
156 | rt = double_type; | |
157 | break; | |
158 | case 'V': | |
159 | rt = void_type; | |
160 | break; | |
161 | default: | |
162 | verify_fail ("invalid signature"); | |
163 | } | |
164 | return rt; | |
165 | } | |
166 | ||
167 | // Return the type_val corresponding to a primitive class. | |
168 | static type_val get_type_val_for_signature (jclass k) | |
169 | { | |
170 | return get_type_val_for_signature ((jchar) k->method_count); | |
171 | } | |
172 | ||
f6b733ed TT |
173 | // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or |
174 | // TARGET haven't been prepared. | |
175 | static bool is_assignable_from_slow (jclass target, jclass source) | |
176 | { | |
177 | // This will terminate when SOURCE==Object. | |
178 | while (true) | |
179 | { | |
180 | if (source == target) | |
181 | return true; | |
182 | ||
183 | if (target->isPrimitive () || source->isPrimitive ()) | |
184 | return false; | |
185 | ||
186 | // _Jv_IsAssignableFrom can handle a target which is an | |
187 | // interface even if it hasn't been prepared. | |
188 | if ((target->state > JV_STATE_LINKED || target->isInterface ()) | |
189 | && source->state > JV_STATE_LINKED) | |
190 | return _Jv_IsAssignableFrom (target, source); | |
191 | ||
192 | if (target->isArray ()) | |
193 | { | |
194 | if (! source->isArray ()) | |
195 | return false; | |
196 | target = target->getComponentType (); | |
197 | source = source->getComponentType (); | |
198 | } | |
199 | else if (target->isInterface ()) | |
200 | { | |
201 | for (int i = 0; i < source->interface_count; ++i) | |
202 | { | |
203 | // We use a recursive call because we also need to | |
204 | // check superinterfaces. | |
205 | if (is_assignable_from_slow (target, source->interfaces[i])) | |
206 | return true; | |
207 | } | |
208 | return false; | |
209 | } | |
210 | else if (target == &java::lang::Object::class$) | |
211 | return true; | |
212 | else if (source->isInterface () | |
213 | || source == &java::lang::Object::class$) | |
214 | return false; | |
215 | else | |
216 | source = source->getSuperclass (); | |
217 | } | |
218 | } | |
219 | ||
a12fe13d TT |
220 | // This is used to keep track of which `jsr's correspond to a given |
221 | // jsr target. | |
222 | struct subr_info | |
223 | { | |
224 | // PC of the instruction just after the jsr. | |
225 | int pc; | |
226 | // Link. | |
227 | subr_info *next; | |
228 | }; | |
229 | ||
230 | // The `type' class is used to represent a single type in the | |
231 | // verifier. | |
232 | struct type | |
233 | { | |
234 | // The type. | |
235 | type_val key; | |
236 | // Some associated data. | |
237 | union | |
238 | { | |
239 | // For a resolved reference type, this is a pointer to the class. | |
240 | jclass klass; | |
241 | // For other reference types, this it the name of the class. | |
242 | _Jv_Utf8Const *name; | |
243 | } data; | |
244 | // This is used when constructing a new object. It is the PC of the | |
245 | // `new' instruction which created the object. We use the special | |
246 | // value -2 to mean that this is uninitialized, and the special | |
247 | // value -1 for the case where the current method is itself the | |
248 | // <init> method. | |
249 | int pc; | |
250 | ||
251 | static const int UNINIT = -2; | |
252 | static const int SELF = -1; | |
253 | ||
254 | // Basic constructor. | |
255 | type () | |
256 | { | |
257 | key = unsuitable_type; | |
258 | data.klass = NULL; | |
259 | pc = UNINIT; | |
260 | } | |
261 | ||
262 | // Make a new instance given the type tag. We assume a generic | |
263 | // `reference_type' means Object. | |
264 | type (type_val k) | |
265 | { | |
266 | key = k; | |
267 | data.klass = NULL; | |
268 | if (key == reference_type) | |
269 | data.klass = &java::lang::Object::class$; | |
270 | pc = UNINIT; | |
271 | } | |
272 | ||
273 | // Make a new instance given a class. | |
274 | type (jclass klass) | |
275 | { | |
276 | key = reference_type; | |
277 | data.klass = klass; | |
278 | pc = UNINIT; | |
279 | } | |
280 | ||
281 | // Make a new instance given the name of a class. | |
282 | type (_Jv_Utf8Const *n) | |
283 | { | |
284 | key = unresolved_reference_type; | |
285 | data.name = n; | |
286 | pc = UNINIT; | |
287 | } | |
288 | ||
289 | // Copy constructor. | |
290 | type (const type &t) | |
291 | { | |
292 | key = t.key; | |
293 | data = t.data; | |
294 | pc = t.pc; | |
295 | } | |
296 | ||
297 | // These operators are required because libgcj can't link in | |
298 | // -lstdc++. | |
299 | void *operator new[] (size_t bytes) | |
300 | { | |
301 | return _Jv_Malloc (bytes); | |
302 | } | |
303 | ||
304 | void operator delete[] (void *mem) | |
305 | { | |
306 | _Jv_Free (mem); | |
307 | } | |
308 | ||
309 | type& operator= (type_val k) | |
310 | { | |
311 | key = k; | |
312 | data.klass = NULL; | |
313 | pc = UNINIT; | |
314 | return *this; | |
315 | } | |
316 | ||
317 | type& operator= (const type& t) | |
318 | { | |
319 | key = t.key; | |
320 | data = t.data; | |
321 | pc = t.pc; | |
322 | return *this; | |
323 | } | |
324 | ||
325 | // Promote a numeric type. | |
f6b733ed | 326 | type &promote () |
a12fe13d TT |
327 | { |
328 | if (key == boolean_type || key == char_type | |
329 | || key == byte_type || key == short_type) | |
330 | key = int_type; | |
f6b733ed | 331 | return *this; |
a12fe13d TT |
332 | } |
333 | ||
334 | // If *THIS is an unresolved reference type, resolve it. | |
335 | void resolve () | |
336 | { | |
337 | if (key != unresolved_reference_type | |
338 | && key != uninitialized_unresolved_reference_type) | |
339 | return; | |
340 | ||
341 | // FIXME: class loader | |
342 | using namespace java::lang; | |
343 | // We might see either kind of name. Sigh. | |
344 | if (data.name->data[0] == 'L' | |
345 | && data.name->data[data.name->length - 1] == ';') | |
346 | data.klass = _Jv_FindClassFromSignature (data.name->data, NULL); | |
347 | else | |
348 | data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name), | |
349 | false, NULL); | |
350 | key = (key == unresolved_reference_type | |
351 | ? reference_type | |
352 | : uninitialized_reference_type); | |
353 | } | |
354 | ||
355 | // Mark this type as the uninitialized result of `new'. | |
e7b35eec | 356 | void set_uninitialized (int npc) |
a12fe13d | 357 | { |
e7b35eec TT |
358 | if (key == reference_type) |
359 | key = uninitialized_reference_type; | |
360 | else if (key == unresolved_reference_type) | |
361 | key = uninitialized_unresolved_reference_type; | |
362 | else | |
a12fe13d | 363 | verify_fail ("internal error in type::uninitialized"); |
e7b35eec | 364 | pc = npc; |
a12fe13d TT |
365 | } |
366 | ||
367 | // Mark this type as now initialized. | |
368 | void set_initialized (int npc) | |
369 | { | |
e7b35eec TT |
370 | if (npc != UNINIT && pc == npc |
371 | && (key == uninitialized_reference_type | |
372 | || key == uninitialized_unresolved_reference_type)) | |
a12fe13d TT |
373 | { |
374 | key = (key == uninitialized_reference_type | |
375 | ? reference_type | |
376 | : unresolved_reference_type); | |
377 | pc = UNINIT; | |
378 | } | |
379 | } | |
380 | ||
381 | ||
382 | // Return true if an object of type K can be assigned to a variable | |
383 | // of type *THIS. Handle various special cases too. Might modify | |
384 | // *THIS or K. Note however that this does not perform numeric | |
385 | // promotion. | |
386 | bool compatible (type &k) | |
387 | { | |
388 | // Any type is compatible with the unsuitable type. | |
389 | if (key == unsuitable_type) | |
390 | return true; | |
391 | ||
392 | if (key < reference_type || k.key < reference_type) | |
393 | return key == k.key; | |
394 | ||
395 | // The `null' type is convertible to any reference type. | |
396 | // FIXME: is this correct for THIS? | |
397 | if (key == null_type || k.key == null_type) | |
398 | return true; | |
399 | ||
400 | // Any reference type is convertible to Object. This is a special | |
401 | // case so we don't need to unnecessarily resolve a class. | |
402 | if (key == reference_type | |
403 | && data.klass == &java::lang::Object::class$) | |
404 | return true; | |
405 | ||
406 | // An initialized type and an uninitialized type are not | |
407 | // compatible. | |
408 | if (isinitialized () != k.isinitialized ()) | |
409 | return false; | |
410 | ||
411 | // Two uninitialized objects are compatible if either: | |
412 | // * The PCs are identical, or | |
413 | // * One PC is UNINIT. | |
414 | if (! isinitialized ()) | |
415 | { | |
416 | if (pc != k.pc && pc != UNINIT && k.pc != UNINIT) | |
417 | return false; | |
418 | } | |
419 | ||
420 | // Two unresolved types are equal if their names are the same. | |
421 | if (! isresolved () | |
422 | && ! k.isresolved () | |
423 | && _Jv_equalUtf8Consts (data.name, k.data.name)) | |
424 | return true; | |
425 | ||
426 | // We must resolve both types and check assignability. | |
427 | resolve (); | |
428 | k.resolve (); | |
f6b733ed | 429 | return is_assignable_from_slow (data.klass, k.data.klass); |
a12fe13d TT |
430 | } |
431 | ||
432 | bool isvoid () const | |
433 | { | |
434 | return key == void_type; | |
435 | } | |
436 | ||
437 | bool iswide () const | |
438 | { | |
439 | return key == long_type || key == double_type; | |
440 | } | |
441 | ||
442 | // Return number of stack or local variable slots taken by this | |
443 | // type. | |
444 | int depth () const | |
445 | { | |
446 | return iswide () ? 2 : 1; | |
447 | } | |
448 | ||
449 | bool isarray () const | |
450 | { | |
451 | // We treat null_type as not an array. This is ok based on the | |
452 | // current uses of this method. | |
453 | if (key == reference_type) | |
454 | return data.klass->isArray (); | |
455 | else if (key == unresolved_reference_type) | |
456 | return data.name->data[0] == '['; | |
457 | return false; | |
458 | } | |
459 | ||
460 | bool isinterface () | |
461 | { | |
462 | resolve (); | |
463 | if (key != reference_type) | |
464 | return false; | |
465 | return data.klass->isInterface (); | |
466 | } | |
467 | ||
468 | bool isabstract () | |
469 | { | |
470 | resolve (); | |
471 | if (key != reference_type) | |
472 | return false; | |
473 | using namespace java::lang::reflect; | |
474 | return Modifier::isAbstract (data.klass->getModifiers ()); | |
475 | } | |
476 | ||
477 | // Return the element type of an array. | |
478 | type element_type () | |
479 | { | |
480 | // FIXME: maybe should do string manipulation here. | |
481 | resolve (); | |
482 | if (key != reference_type) | |
483 | verify_fail ("programmer error in type::element_type()"); | |
484 | ||
485 | jclass k = data.klass->getComponentType (); | |
486 | if (k->isPrimitive ()) | |
487 | return type (get_type_val_for_signature (k)); | |
488 | return type (k); | |
489 | } | |
490 | ||
491 | bool isreference () const | |
492 | { | |
493 | return key >= reference_type; | |
494 | } | |
495 | ||
496 | int get_pc () const | |
497 | { | |
498 | return pc; | |
499 | } | |
500 | ||
501 | bool isinitialized () const | |
502 | { | |
503 | return (key == reference_type | |
504 | || key == null_type | |
505 | || key == unresolved_reference_type); | |
506 | } | |
507 | ||
508 | bool isresolved () const | |
509 | { | |
510 | return (key == reference_type | |
511 | || key == null_type | |
512 | || key == uninitialized_reference_type); | |
513 | } | |
514 | ||
515 | void verify_dimensions (int ndims) | |
516 | { | |
517 | // The way this is written, we don't need to check isarray(). | |
518 | if (key == reference_type) | |
519 | { | |
520 | jclass k = data.klass; | |
521 | while (k->isArray () && ndims > 0) | |
522 | { | |
523 | k = k->getComponentType (); | |
524 | --ndims; | |
525 | } | |
526 | } | |
527 | else | |
528 | { | |
529 | // We know KEY == unresolved_reference_type. | |
530 | char *p = data.name->data; | |
531 | while (*p++ == '[' && ndims-- > 0) | |
532 | ; | |
533 | } | |
534 | ||
535 | if (ndims > 0) | |
536 | verify_fail ("array type has fewer dimensions than required"); | |
537 | } | |
538 | ||
539 | // Merge OLD_TYPE into this. On error throw exception. | |
540 | bool merge (type& old_type, bool local_semantics = false) | |
541 | { | |
542 | bool changed = false; | |
543 | bool refo = old_type.isreference (); | |
544 | bool refn = isreference (); | |
545 | if (refo && refn) | |
546 | { | |
547 | if (old_type.key == null_type) | |
548 | ; | |
549 | else if (key == null_type) | |
550 | { | |
551 | *this = old_type; | |
552 | changed = true; | |
553 | } | |
554 | else if (isinitialized () != old_type.isinitialized ()) | |
555 | verify_fail ("merging initialized and uninitialized types"); | |
556 | else | |
557 | { | |
558 | if (! isinitialized ()) | |
559 | { | |
560 | if (pc == UNINIT) | |
561 | pc = old_type.pc; | |
562 | else if (old_type.pc == UNINIT) | |
563 | ; | |
564 | else if (pc != old_type.pc) | |
565 | verify_fail ("merging different uninitialized types"); | |
566 | } | |
567 | ||
568 | if (! isresolved () | |
569 | && ! old_type.isresolved () | |
570 | && _Jv_equalUtf8Consts (data.name, old_type.data.name)) | |
571 | { | |
572 | // Types are identical. | |
573 | } | |
574 | else | |
575 | { | |
576 | resolve (); | |
577 | old_type.resolve (); | |
578 | ||
579 | jclass k = data.klass; | |
580 | jclass oldk = old_type.data.klass; | |
581 | ||
582 | int arraycount = 0; | |
583 | while (k->isArray () && oldk->isArray ()) | |
584 | { | |
585 | ++arraycount; | |
586 | k = k->getComponentType (); | |
587 | oldk = oldk->getComponentType (); | |
588 | } | |
589 | ||
590 | // This loop will end when we hit Object. | |
591 | while (true) | |
592 | { | |
f6b733ed | 593 | if (is_assignable_from_slow (k, oldk)) |
a12fe13d TT |
594 | break; |
595 | k = k->getSuperclass (); | |
596 | changed = true; | |
597 | } | |
598 | ||
599 | if (changed) | |
600 | { | |
601 | while (arraycount > 0) | |
602 | { | |
603 | // FIXME: Class loader. | |
604 | k = _Jv_GetArrayClass (k, NULL); | |
605 | --arraycount; | |
606 | } | |
607 | data.klass = k; | |
608 | } | |
609 | } | |
610 | } | |
611 | } | |
612 | else if (refo || refn || key != old_type.key) | |
613 | { | |
614 | if (local_semantics) | |
615 | { | |
616 | key = unsuitable_type; | |
617 | changed = true; | |
618 | } | |
619 | else | |
620 | verify_fail ("unmergeable type"); | |
621 | } | |
622 | return changed; | |
623 | } | |
624 | }; | |
625 | ||
626 | // This class holds all the state information we need for a given | |
627 | // location. | |
628 | struct state | |
629 | { | |
630 | // Current top of stack. | |
631 | int stacktop; | |
632 | // Current stack depth. This is like the top of stack but it | |
633 | // includes wide variable information. | |
634 | int stackdepth; | |
635 | // The stack. | |
636 | type *stack; | |
637 | // The local variables. | |
638 | type *locals; | |
639 | // This is used in subroutines to keep track of which local | |
640 | // variables have been accessed. | |
641 | bool *local_changed; | |
642 | // If not 0, then we are in a subroutine. The value is the PC of | |
643 | // the subroutine's entry point. We can use 0 as an exceptional | |
644 | // value because PC=0 can never be a subroutine. | |
645 | int subroutine; | |
646 | // This is used to keep a linked list of all the states which | |
647 | // require re-verification. We use the PC to keep track. | |
648 | int next; | |
649 | ||
650 | // INVALID marks a state which is not on the linked list of states | |
651 | // requiring reverification. | |
652 | static const int INVALID = -1; | |
653 | // NO_NEXT marks the state at the end of the reverification list. | |
654 | static const int NO_NEXT = -2; | |
655 | ||
656 | state () | |
657 | { | |
658 | stack = NULL; | |
659 | locals = NULL; | |
660 | local_changed = NULL; | |
661 | } | |
662 | ||
663 | state (int max_stack, int max_locals) | |
664 | { | |
665 | stacktop = 0; | |
666 | stackdepth = 0; | |
667 | stack = new type[max_stack]; | |
668 | for (int i = 0; i < max_stack; ++i) | |
669 | stack[i] = unsuitable_type; | |
670 | locals = new type[max_locals]; | |
671 | local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals); | |
672 | for (int i = 0; i < max_locals; ++i) | |
673 | { | |
674 | locals[i] = unsuitable_type; | |
675 | local_changed[i] = false; | |
676 | } | |
677 | next = INVALID; | |
678 | subroutine = 0; | |
679 | } | |
680 | ||
681 | state (const state *copy, int max_stack, int max_locals) | |
682 | { | |
683 | stack = new type[max_stack]; | |
684 | locals = new type[max_locals]; | |
685 | local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals); | |
686 | *this = *copy; | |
687 | next = INVALID; | |
688 | } | |
689 | ||
690 | ~state () | |
691 | { | |
692 | if (stack) | |
693 | delete[] stack; | |
694 | if (locals) | |
695 | delete[] locals; | |
696 | if (local_changed) | |
697 | _Jv_Free (local_changed); | |
698 | } | |
699 | ||
700 | void *operator new[] (size_t bytes) | |
701 | { | |
702 | return _Jv_Malloc (bytes); | |
703 | } | |
704 | ||
705 | void operator delete[] (void *mem) | |
706 | { | |
707 | _Jv_Free (mem); | |
708 | } | |
709 | ||
710 | void *operator new (size_t bytes) | |
711 | { | |
712 | return _Jv_Malloc (bytes); | |
713 | } | |
714 | ||
715 | void operator delete (void *mem) | |
716 | { | |
717 | _Jv_Free (mem); | |
718 | } | |
719 | ||
720 | void copy (const state *copy, int max_stack, int max_locals) | |
721 | { | |
722 | stacktop = copy->stacktop; | |
723 | stackdepth = copy->stackdepth; | |
724 | subroutine = copy->subroutine; | |
725 | for (int i = 0; i < max_stack; ++i) | |
726 | stack[i] = copy->stack[i]; | |
727 | for (int i = 0; i < max_locals; ++i) | |
728 | { | |
729 | locals[i] = copy->locals[i]; | |
730 | local_changed[i] = copy->local_changed[i]; | |
731 | } | |
732 | // Don't modify `next'. | |
733 | } | |
734 | ||
735 | // Modify this state to reflect entry to an exception handler. | |
736 | void set_exception (type t, int max_stack) | |
737 | { | |
738 | stackdepth = 1; | |
739 | stacktop = 1; | |
740 | stack[0] = t; | |
741 | for (int i = stacktop; i < max_stack; ++i) | |
742 | stack[i] = unsuitable_type; | |
743 | ||
744 | // FIXME: subroutine handling? | |
745 | } | |
746 | ||
747 | // Merge STATE into this state. Destructively modifies this state. | |
748 | // Returns true if the new state was in fact changed. Will throw an | |
749 | // exception if the states are not mergeable. | |
750 | bool merge (state *state_old, bool ret_semantics, | |
751 | int max_locals) | |
752 | { | |
753 | bool changed = false; | |
754 | ||
755 | // Merge subroutine states. *THIS and *STATE_OLD must be in the | |
756 | // same subroutine. Also, recursive subroutine calls must be | |
757 | // avoided. | |
758 | if (subroutine == state_old->subroutine) | |
759 | { | |
760 | // Nothing. | |
761 | } | |
762 | else if (subroutine == 0) | |
763 | { | |
764 | subroutine = state_old->subroutine; | |
765 | changed = true; | |
766 | } | |
767 | else | |
768 | verify_fail ("subroutines merged"); | |
769 | ||
770 | // Merge stacks. | |
771 | if (state_old->stacktop != stacktop) | |
772 | verify_fail ("stack sizes differ"); | |
773 | for (int i = 0; i < state_old->stacktop; ++i) | |
774 | { | |
775 | if (stack[i].merge (state_old->stack[i])) | |
776 | changed = true; | |
777 | } | |
778 | ||
779 | // Merge local variables. | |
780 | for (int i = 0; i < max_locals; ++i) | |
781 | { | |
782 | if (! ret_semantics || local_changed[i]) | |
783 | { | |
784 | if (locals[i].merge (state_old->locals[i], true)) | |
785 | { | |
786 | changed = true; | |
787 | note_variable (i); | |
788 | } | |
789 | } | |
790 | ||
791 | // If we're in a subroutine, we must compute the union of | |
792 | // all the changed local variables. | |
793 | if (state_old->local_changed[i]) | |
794 | note_variable (i); | |
795 | } | |
796 | ||
797 | return changed; | |
798 | } | |
799 | ||
800 | // Throw an exception if there is an uninitialized object on the | |
801 | // stack or in a local variable. EXCEPTION_SEMANTICS controls | |
802 | // whether we're using backwards-branch or exception-handing | |
803 | // semantics. | |
804 | void check_no_uninitialized_objects (int max_locals, | |
805 | bool exception_semantics = false) | |
806 | { | |
807 | if (! exception_semantics) | |
808 | { | |
809 | for (int i = 0; i < stacktop; ++i) | |
810 | if (stack[i].isreference () && ! stack[i].isinitialized ()) | |
811 | verify_fail ("uninitialized object on stack"); | |
812 | } | |
813 | ||
814 | for (int i = 0; i < max_locals; ++i) | |
815 | if (locals[i].isreference () && ! locals[i].isinitialized ()) | |
816 | verify_fail ("uninitialized object in local variable"); | |
817 | } | |
818 | ||
819 | // Note that a local variable was accessed or modified. | |
820 | void note_variable (int index) | |
821 | { | |
822 | if (subroutine > 0) | |
823 | local_changed[index] = true; | |
824 | } | |
825 | ||
826 | // Mark each `new'd object we know of that was allocated at PC as | |
827 | // initialized. | |
828 | void set_initialized (int pc, int max_locals) | |
829 | { | |
830 | for (int i = 0; i < stacktop; ++i) | |
831 | stack[i].set_initialized (pc); | |
832 | for (int i = 0; i < max_locals; ++i) | |
833 | locals[i].set_initialized (pc); | |
834 | } | |
835 | }; | |
836 | ||
837 | type pop_raw () | |
838 | { | |
839 | if (current_state->stacktop <= 0) | |
e7b35eec | 840 | verify_fail ("stack empty", start_PC); |
a12fe13d TT |
841 | type r = current_state->stack[--current_state->stacktop]; |
842 | current_state->stackdepth -= r.depth (); | |
843 | if (current_state->stackdepth < 0) | |
e7b35eec | 844 | verify_fail ("stack empty", start_PC); |
a12fe13d TT |
845 | return r; |
846 | } | |
847 | ||
848 | type pop32 () | |
849 | { | |
850 | type r = pop_raw (); | |
851 | if (r.iswide ()) | |
e7b35eec | 852 | verify_fail ("narrow pop of wide type", start_PC); |
a12fe13d TT |
853 | return r; |
854 | } | |
855 | ||
856 | type pop64 () | |
857 | { | |
858 | type r = pop_raw (); | |
859 | if (! r.iswide ()) | |
e7b35eec | 860 | verify_fail ("wide pop of narrow type", start_PC); |
a12fe13d TT |
861 | return r; |
862 | } | |
863 | ||
864 | type pop_type (type match) | |
865 | { | |
e7b35eec | 866 | match.promote (); |
a12fe13d TT |
867 | type t = pop_raw (); |
868 | if (! match.compatible (t)) | |
e7b35eec | 869 | verify_fail ("incompatible type on stack", start_PC); |
a12fe13d TT |
870 | return t; |
871 | } | |
872 | ||
873 | void push_type (type t) | |
874 | { | |
875 | // If T is a numeric type like short, promote it to int. | |
876 | t.promote (); | |
877 | ||
878 | int depth = t.depth (); | |
879 | if (current_state->stackdepth + depth > current_method->max_stack) | |
880 | verify_fail ("stack overflow"); | |
881 | current_state->stack[current_state->stacktop++] = t; | |
882 | current_state->stackdepth += depth; | |
883 | } | |
884 | ||
885 | void set_variable (int index, type t) | |
886 | { | |
887 | // If T is a numeric type like short, promote it to int. | |
888 | t.promote (); | |
889 | ||
890 | int depth = t.depth (); | |
891 | if (index > current_method->max_locals - depth) | |
892 | verify_fail ("invalid local variable"); | |
893 | current_state->locals[index] = t; | |
894 | current_state->note_variable (index); | |
895 | ||
896 | if (depth == 2) | |
897 | { | |
898 | current_state->locals[index + 1] = continuation_type; | |
899 | current_state->note_variable (index + 1); | |
900 | } | |
901 | if (index > 0 && current_state->locals[index - 1].iswide ()) | |
902 | { | |
903 | current_state->locals[index - 1] = unsuitable_type; | |
904 | // There's no need to call note_variable here. | |
905 | } | |
906 | } | |
907 | ||
908 | type get_variable (int index, type t) | |
909 | { | |
910 | int depth = t.depth (); | |
911 | if (index > current_method->max_locals - depth) | |
60440707 | 912 | verify_fail ("invalid local variable", start_PC); |
a12fe13d | 913 | if (! t.compatible (current_state->locals[index])) |
60440707 | 914 | verify_fail ("incompatible type in local variable", start_PC); |
a12fe13d TT |
915 | if (depth == 2) |
916 | { | |
917 | type t (continuation_type); | |
918 | if (! current_state->locals[index + 1].compatible (t)) | |
60440707 | 919 | verify_fail ("invalid local variable", start_PC); |
a12fe13d TT |
920 | } |
921 | current_state->note_variable (index); | |
922 | return current_state->locals[index]; | |
923 | } | |
924 | ||
925 | // Make sure ARRAY is an array type and that its elements are | |
926 | // compatible with type ELEMENT. Returns the actual element type. | |
927 | type require_array_type (type array, type element) | |
928 | { | |
929 | if (! array.isarray ()) | |
930 | verify_fail ("array required"); | |
931 | ||
932 | type t = array.element_type (); | |
933 | if (! element.compatible (t)) | |
934 | verify_fail ("incompatible array element type"); | |
935 | ||
936 | // Return T and not ELEMENT, because T might be specialized. | |
937 | return t; | |
938 | } | |
939 | ||
940 | jint get_byte () | |
941 | { | |
942 | if (PC >= current_method->code_length) | |
943 | verify_fail ("premature end of bytecode"); | |
944 | return (jint) bytecode[PC++] & 0xff; | |
945 | } | |
946 | ||
947 | jint get_ushort () | |
948 | { | |
949 | jbyte b1 = get_byte (); | |
950 | jbyte b2 = get_byte (); | |
951 | return (jint) ((b1 << 8) | b2) & 0xffff; | |
952 | } | |
953 | ||
954 | jint get_short () | |
955 | { | |
956 | jbyte b1 = get_byte (); | |
957 | jbyte b2 = get_byte (); | |
958 | jshort s = (b1 << 8) | b2; | |
959 | return (jint) s; | |
960 | } | |
961 | ||
962 | jint get_int () | |
963 | { | |
964 | jbyte b1 = get_byte (); | |
965 | jbyte b2 = get_byte (); | |
966 | jbyte b3 = get_byte (); | |
967 | jbyte b4 = get_byte (); | |
968 | return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4; | |
969 | } | |
970 | ||
971 | int compute_jump (int offset) | |
972 | { | |
973 | int npc = start_PC + offset; | |
974 | if (npc < 0 || npc >= current_method->code_length) | |
975 | verify_fail ("branch out of range"); | |
976 | return npc; | |
977 | } | |
978 | ||
979 | // Merge the indicated state into a new state and schedule a new PC if | |
980 | // there is a change. If RET_SEMANTICS is true, then we are merging | |
981 | // from a `ret' instruction into the instruction after a `jsr'. This | |
982 | // is a special case with its own modified semantics. | |
983 | void push_jump_merge (int npc, state *nstate, bool ret_semantics = false) | |
984 | { | |
985 | bool changed = true; | |
986 | if (states[npc] == NULL) | |
987 | { | |
988 | // FIXME: what if we reach this code from a `ret'? | |
989 | ||
990 | states[npc] = new state (nstate, current_method->max_stack, | |
991 | current_method->max_locals); | |
992 | } | |
993 | else | |
994 | changed = nstate->merge (states[npc], ret_semantics, | |
995 | current_method->max_stack); | |
996 | ||
997 | if (changed && states[npc]->next == state::INVALID) | |
998 | { | |
999 | // The merge changed the state, and the new PC isn't yet on our | |
1000 | // list of PCs to re-verify. | |
1001 | states[npc]->next = next_verify_pc; | |
1002 | next_verify_pc = npc; | |
1003 | } | |
1004 | } | |
1005 | ||
1006 | void push_jump (int offset) | |
1007 | { | |
1008 | int npc = compute_jump (offset); | |
1009 | if (npc < PC) | |
1010 | current_state->check_no_uninitialized_objects (current_method->max_stack); | |
1011 | push_jump_merge (npc, current_state); | |
1012 | } | |
1013 | ||
1014 | void push_exception_jump (type t, int pc) | |
1015 | { | |
1016 | current_state->check_no_uninitialized_objects (current_method->max_stack, | |
1017 | true); | |
1018 | state s (current_state, current_method->max_stack, | |
1019 | current_method->max_locals); | |
1020 | s.set_exception (t, current_method->max_stack); | |
1021 | push_jump_merge (pc, &s); | |
1022 | } | |
1023 | ||
1024 | int pop_jump () | |
1025 | { | |
1026 | int npc = next_verify_pc; | |
1027 | if (npc != state::NO_NEXT) | |
1028 | { | |
1029 | next_verify_pc = states[npc]->next; | |
1030 | states[npc]->next = state::INVALID; | |
1031 | } | |
1032 | return npc; | |
1033 | } | |
1034 | ||
1035 | void invalidate_pc () | |
1036 | { | |
1037 | PC = state::NO_NEXT; | |
1038 | } | |
1039 | ||
1040 | void note_branch_target (int pc, bool is_jsr_target = false) | |
1041 | { | |
1042 | if (pc <= PC && ! (flags[pc] & FLAG_INSN_START)) | |
1043 | verify_fail ("branch not to instruction start"); | |
1044 | flags[pc] |= FLAG_BRANCH_TARGET; | |
1045 | if (is_jsr_target) | |
1046 | { | |
1047 | // Record the jsr which called this instruction. | |
1048 | subr_info *info = (subr_info *) _Jv_Malloc (sizeof (subr_info)); | |
1049 | info->pc = PC; | |
1050 | info->next = jsr_ptrs[pc]; | |
1051 | jsr_ptrs[pc] = info; | |
1052 | flags[pc] |= FLAG_JSR_TARGET; | |
1053 | } | |
1054 | } | |
1055 | ||
1056 | void skip_padding () | |
1057 | { | |
1058 | while ((PC % 4) > 0) | |
b1194618 TT |
1059 | if (get_byte () != 0) |
1060 | verify_fail ("found nonzero padding byte"); | |
a12fe13d TT |
1061 | } |
1062 | ||
1063 | // Return the subroutine to which the instruction at PC belongs. | |
1064 | int get_subroutine (int pc) | |
1065 | { | |
1066 | if (states[pc] == NULL) | |
1067 | return 0; | |
1068 | return states[pc]->subroutine; | |
1069 | } | |
1070 | ||
1071 | // Do the work for a `ret' instruction. INDEX is the index into the | |
1072 | // local variables. | |
1073 | void handle_ret_insn (int index) | |
1074 | { | |
1075 | get_variable (index, return_address_type); | |
1076 | ||
1077 | int csub = current_state->subroutine; | |
1078 | if (csub == 0) | |
1079 | verify_fail ("no subroutine"); | |
1080 | ||
1081 | for (subr_info *subr = jsr_ptrs[csub]; subr != NULL; subr = subr->next) | |
1082 | { | |
1083 | // Temporarily modify the current state so it looks like we're | |
1084 | // in the enclosing context. | |
1085 | current_state->subroutine = get_subroutine (subr->pc); | |
1086 | if (subr->pc < PC) | |
1087 | current_state->check_no_uninitialized_objects (current_method->max_stack); | |
1088 | push_jump_merge (subr->pc, current_state, true); | |
1089 | } | |
1090 | ||
1091 | current_state->subroutine = csub; | |
1092 | invalidate_pc (); | |
1093 | } | |
1094 | ||
1095 | // We're in the subroutine SUB, calling a subroutine at DEST. Make | |
1096 | // sure this subroutine isn't already on the stack. | |
1097 | void check_nonrecursive_call (int sub, int dest) | |
1098 | { | |
1099 | if (sub == 0) | |
1100 | return; | |
1101 | if (sub == dest) | |
1102 | verify_fail ("recursive subroutine call"); | |
1103 | for (subr_info *info = jsr_ptrs[sub]; info != NULL; info = info->next) | |
1104 | check_nonrecursive_call (get_subroutine (info->pc), dest); | |
1105 | } | |
1106 | ||
1107 | void handle_jsr_insn (int offset) | |
1108 | { | |
1109 | int npc = compute_jump (offset); | |
1110 | ||
1111 | if (npc < PC) | |
1112 | current_state->check_no_uninitialized_objects (current_method->max_stack); | |
1113 | check_nonrecursive_call (current_state->subroutine, npc); | |
1114 | ||
1115 | // Temporarily modify the current state so that it looks like we are | |
1116 | // in the subroutine. | |
1117 | push_type (return_address_type); | |
1118 | int save = current_state->subroutine; | |
1119 | current_state->subroutine = npc; | |
1120 | ||
1121 | // Merge into the subroutine. | |
1122 | push_jump_merge (npc, current_state); | |
1123 | ||
1124 | // Undo our modifications. | |
1125 | current_state->subroutine = save; | |
1126 | pop_type (return_address_type); | |
1127 | } | |
1128 | ||
1129 | jclass construct_primitive_array_type (type_val prim) | |
1130 | { | |
1131 | jclass k = NULL; | |
1132 | switch (prim) | |
1133 | { | |
1134 | case boolean_type: | |
1135 | k = JvPrimClass (boolean); | |
1136 | break; | |
1137 | case char_type: | |
1138 | k = JvPrimClass (char); | |
1139 | break; | |
1140 | case float_type: | |
1141 | k = JvPrimClass (float); | |
1142 | break; | |
1143 | case double_type: | |
1144 | k = JvPrimClass (double); | |
1145 | break; | |
1146 | case byte_type: | |
1147 | k = JvPrimClass (byte); | |
1148 | break; | |
1149 | case short_type: | |
1150 | k = JvPrimClass (short); | |
1151 | break; | |
1152 | case int_type: | |
1153 | k = JvPrimClass (int); | |
1154 | break; | |
1155 | case long_type: | |
1156 | k = JvPrimClass (long); | |
1157 | break; | |
1158 | default: | |
1159 | verify_fail ("unknown type in construct_primitive_array_type"); | |
1160 | } | |
1161 | k = _Jv_GetArrayClass (k, NULL); | |
1162 | return k; | |
1163 | } | |
1164 | ||
1165 | // This pass computes the location of branch targets and also | |
1166 | // instruction starts. | |
1167 | void branch_prepass () | |
1168 | { | |
1169 | flags = (char *) _Jv_Malloc (current_method->code_length); | |
1170 | jsr_ptrs = (subr_info **) _Jv_Malloc (sizeof (subr_info *) | |
1171 | * current_method->code_length); | |
1172 | ||
1173 | for (int i = 0; i < current_method->code_length; ++i) | |
1174 | { | |
1175 | flags[i] = 0; | |
1176 | jsr_ptrs[i] = NULL; | |
1177 | } | |
1178 | ||
1179 | bool last_was_jsr = false; | |
1180 | ||
1181 | PC = 0; | |
1182 | while (PC < current_method->code_length) | |
1183 | { | |
1184 | flags[PC] |= FLAG_INSN_START; | |
1185 | ||
1186 | // If the previous instruction was a jsr, then the next | |
1187 | // instruction is a branch target -- the branch being the | |
1188 | // corresponding `ret'. | |
1189 | if (last_was_jsr) | |
1190 | note_branch_target (PC); | |
1191 | last_was_jsr = false; | |
1192 | ||
1193 | start_PC = PC; | |
1194 | unsigned char opcode = bytecode[PC++]; | |
1195 | switch (opcode) | |
1196 | { | |
1197 | case op_nop: | |
1198 | case op_aconst_null: | |
1199 | case op_iconst_m1: | |
1200 | case op_iconst_0: | |
1201 | case op_iconst_1: | |
1202 | case op_iconst_2: | |
1203 | case op_iconst_3: | |
1204 | case op_iconst_4: | |
1205 | case op_iconst_5: | |
1206 | case op_lconst_0: | |
1207 | case op_lconst_1: | |
1208 | case op_fconst_0: | |
1209 | case op_fconst_1: | |
1210 | case op_fconst_2: | |
1211 | case op_dconst_0: | |
1212 | case op_dconst_1: | |
1213 | case op_iload_0: | |
1214 | case op_iload_1: | |
1215 | case op_iload_2: | |
1216 | case op_iload_3: | |
1217 | case op_lload_0: | |
1218 | case op_lload_1: | |
1219 | case op_lload_2: | |
1220 | case op_lload_3: | |
1221 | case op_fload_0: | |
1222 | case op_fload_1: | |
1223 | case op_fload_2: | |
1224 | case op_fload_3: | |
1225 | case op_dload_0: | |
1226 | case op_dload_1: | |
1227 | case op_dload_2: | |
1228 | case op_dload_3: | |
1229 | case op_aload_0: | |
1230 | case op_aload_1: | |
1231 | case op_aload_2: | |
1232 | case op_aload_3: | |
1233 | case op_iaload: | |
1234 | case op_laload: | |
1235 | case op_faload: | |
1236 | case op_daload: | |
1237 | case op_aaload: | |
1238 | case op_baload: | |
1239 | case op_caload: | |
1240 | case op_saload: | |
1241 | case op_istore_0: | |
1242 | case op_istore_1: | |
1243 | case op_istore_2: | |
1244 | case op_istore_3: | |
1245 | case op_lstore_0: | |
1246 | case op_lstore_1: | |
1247 | case op_lstore_2: | |
1248 | case op_lstore_3: | |
1249 | case op_fstore_0: | |
1250 | case op_fstore_1: | |
1251 | case op_fstore_2: | |
1252 | case op_fstore_3: | |
1253 | case op_dstore_0: | |
1254 | case op_dstore_1: | |
1255 | case op_dstore_2: | |
1256 | case op_dstore_3: | |
1257 | case op_astore_0: | |
1258 | case op_astore_1: | |
1259 | case op_astore_2: | |
1260 | case op_astore_3: | |
1261 | case op_iastore: | |
1262 | case op_lastore: | |
1263 | case op_fastore: | |
1264 | case op_dastore: | |
1265 | case op_aastore: | |
1266 | case op_bastore: | |
1267 | case op_castore: | |
1268 | case op_sastore: | |
1269 | case op_pop: | |
1270 | case op_pop2: | |
1271 | case op_dup: | |
1272 | case op_dup_x1: | |
1273 | case op_dup_x2: | |
1274 | case op_dup2: | |
1275 | case op_dup2_x1: | |
1276 | case op_dup2_x2: | |
1277 | case op_swap: | |
1278 | case op_iadd: | |
1279 | case op_isub: | |
1280 | case op_imul: | |
1281 | case op_idiv: | |
1282 | case op_irem: | |
1283 | case op_ishl: | |
1284 | case op_ishr: | |
1285 | case op_iushr: | |
1286 | case op_iand: | |
1287 | case op_ior: | |
1288 | case op_ixor: | |
1289 | case op_ladd: | |
1290 | case op_lsub: | |
1291 | case op_lmul: | |
1292 | case op_ldiv: | |
1293 | case op_lrem: | |
1294 | case op_lshl: | |
1295 | case op_lshr: | |
1296 | case op_lushr: | |
1297 | case op_land: | |
1298 | case op_lor: | |
1299 | case op_lxor: | |
1300 | case op_fadd: | |
1301 | case op_fsub: | |
1302 | case op_fmul: | |
1303 | case op_fdiv: | |
1304 | case op_frem: | |
1305 | case op_dadd: | |
1306 | case op_dsub: | |
1307 | case op_dmul: | |
1308 | case op_ddiv: | |
1309 | case op_drem: | |
1310 | case op_ineg: | |
1311 | case op_i2b: | |
1312 | case op_i2c: | |
1313 | case op_i2s: | |
1314 | case op_lneg: | |
1315 | case op_fneg: | |
1316 | case op_dneg: | |
1317 | case op_iinc: | |
1318 | case op_i2l: | |
1319 | case op_i2f: | |
1320 | case op_i2d: | |
1321 | case op_l2i: | |
1322 | case op_l2f: | |
1323 | case op_l2d: | |
1324 | case op_f2i: | |
1325 | case op_f2l: | |
1326 | case op_f2d: | |
1327 | case op_d2i: | |
1328 | case op_d2l: | |
1329 | case op_d2f: | |
1330 | case op_lcmp: | |
1331 | case op_fcmpl: | |
1332 | case op_fcmpg: | |
1333 | case op_dcmpl: | |
1334 | case op_dcmpg: | |
1335 | case op_monitorenter: | |
1336 | case op_monitorexit: | |
1337 | case op_ireturn: | |
1338 | case op_lreturn: | |
1339 | case op_freturn: | |
1340 | case op_dreturn: | |
1341 | case op_areturn: | |
1342 | case op_return: | |
1343 | case op_athrow: | |
1344 | break; | |
1345 | ||
1346 | case op_bipush: | |
1347 | case op_sipush: | |
1348 | case op_ldc: | |
1349 | case op_iload: | |
1350 | case op_lload: | |
1351 | case op_fload: | |
1352 | case op_dload: | |
1353 | case op_aload: | |
1354 | case op_istore: | |
1355 | case op_lstore: | |
1356 | case op_fstore: | |
1357 | case op_dstore: | |
1358 | case op_astore: | |
1359 | case op_arraylength: | |
1360 | case op_ret: | |
1361 | get_byte (); | |
1362 | break; | |
1363 | ||
1364 | case op_ldc_w: | |
1365 | case op_ldc2_w: | |
1366 | case op_getstatic: | |
1367 | case op_getfield: | |
1368 | case op_putfield: | |
1369 | case op_putstatic: | |
1370 | case op_new: | |
6c5a8271 | 1371 | case op_newarray: |
a12fe13d TT |
1372 | case op_anewarray: |
1373 | case op_instanceof: | |
1374 | case op_checkcast: | |
1375 | case op_invokespecial: | |
1376 | case op_invokestatic: | |
1377 | case op_invokevirtual: | |
1378 | get_short (); | |
1379 | break; | |
1380 | ||
1381 | case op_multianewarray: | |
1382 | get_short (); | |
1383 | get_byte (); | |
1384 | break; | |
1385 | ||
1386 | case op_jsr: | |
1387 | last_was_jsr = true; | |
1388 | // Fall through. | |
1389 | case op_ifeq: | |
1390 | case op_ifne: | |
1391 | case op_iflt: | |
1392 | case op_ifge: | |
1393 | case op_ifgt: | |
1394 | case op_ifle: | |
1395 | case op_if_icmpeq: | |
1396 | case op_if_icmpne: | |
1397 | case op_if_icmplt: | |
1398 | case op_if_icmpge: | |
1399 | case op_if_icmpgt: | |
1400 | case op_if_icmple: | |
1401 | case op_if_acmpeq: | |
1402 | case op_if_acmpne: | |
1403 | case op_ifnull: | |
1404 | case op_ifnonnull: | |
1405 | case op_goto: | |
1406 | note_branch_target (compute_jump (get_short ()), last_was_jsr); | |
1407 | break; | |
1408 | ||
1409 | case op_tableswitch: | |
1410 | { | |
1411 | skip_padding (); | |
1412 | note_branch_target (compute_jump (get_int ())); | |
1413 | jint low = get_int (); | |
1414 | jint hi = get_int (); | |
1415 | if (low > hi) | |
60440707 | 1416 | verify_fail ("invalid tableswitch", start_PC); |
a12fe13d TT |
1417 | for (int i = low; i <= hi; ++i) |
1418 | note_branch_target (compute_jump (get_int ())); | |
1419 | } | |
1420 | break; | |
1421 | ||
1422 | case op_lookupswitch: | |
1423 | { | |
1424 | skip_padding (); | |
1425 | note_branch_target (compute_jump (get_int ())); | |
1426 | int npairs = get_int (); | |
1427 | if (npairs < 0) | |
60440707 | 1428 | verify_fail ("too few pairs in lookupswitch", start_PC); |
a12fe13d TT |
1429 | while (npairs-- > 0) |
1430 | { | |
1431 | get_int (); | |
1432 | note_branch_target (compute_jump (get_int ())); | |
1433 | } | |
1434 | } | |
1435 | break; | |
1436 | ||
1437 | case op_invokeinterface: | |
1438 | get_short (); | |
1439 | get_byte (); | |
1440 | get_byte (); | |
1441 | break; | |
1442 | ||
1443 | case op_wide: | |
1444 | { | |
1445 | opcode = get_byte (); | |
1446 | get_short (); | |
1447 | if (opcode == (unsigned char) op_iinc) | |
1448 | get_short (); | |
1449 | } | |
1450 | break; | |
1451 | ||
1452 | case op_jsr_w: | |
1453 | last_was_jsr = true; | |
1454 | // Fall through. | |
1455 | case op_goto_w: | |
1456 | note_branch_target (compute_jump (get_int ()), last_was_jsr); | |
1457 | break; | |
1458 | ||
1459 | default: | |
60440707 TT |
1460 | verify_fail ("unrecognized instruction in branch_prepass", |
1461 | start_PC); | |
a12fe13d TT |
1462 | } |
1463 | ||
1464 | // See if any previous branch tried to branch to the middle of | |
1465 | // this instruction. | |
1466 | for (int pc = start_PC + 1; pc < PC; ++pc) | |
1467 | { | |
1468 | if ((flags[pc] & FLAG_BRANCH_TARGET)) | |
60440707 | 1469 | verify_fail ("branch to middle of instruction", pc); |
a12fe13d TT |
1470 | } |
1471 | } | |
1472 | ||
1473 | // Verify exception handlers. | |
1474 | for (int i = 0; i < current_method->exc_count; ++i) | |
1475 | { | |
1476 | if (! (flags[exception[i].handler_pc] & FLAG_INSN_START)) | |
60440707 TT |
1477 | verify_fail ("exception handler not at instruction start", |
1478 | exception[i].handler_pc); | |
a12fe13d TT |
1479 | if (exception[i].start_pc > exception[i].end_pc) |
1480 | verify_fail ("exception range inverted"); | |
60440707 TT |
1481 | if (! (flags[exception[i].start_pc] & FLAG_INSN_START)) |
1482 | verify_fail ("exception start not at instruction start", | |
1483 | exception[i].start_pc); | |
1484 | else if (! (flags[exception[i].end_pc] & FLAG_INSN_START)) | |
1485 | verify_fail ("exception end not at instruction start", | |
1486 | exception[i].end_pc); | |
a12fe13d TT |
1487 | |
1488 | flags[exception[i].handler_pc] |= FLAG_BRANCH_TARGET; | |
1489 | } | |
1490 | } | |
1491 | ||
1492 | void check_pool_index (int index) | |
1493 | { | |
1494 | if (index < 0 || index >= current_class->constants.size) | |
60440707 | 1495 | verify_fail ("constant pool index out of range", start_PC); |
a12fe13d TT |
1496 | } |
1497 | ||
1498 | type check_class_constant (int index) | |
1499 | { | |
1500 | check_pool_index (index); | |
1501 | _Jv_Constants *pool = ¤t_class->constants; | |
1502 | if (pool->tags[index] == JV_CONSTANT_ResolvedClass) | |
1503 | return type (pool->data[index].clazz); | |
1504 | else if (pool->tags[index] == JV_CONSTANT_Class) | |
1505 | return type (pool->data[index].utf8); | |
60440707 | 1506 | verify_fail ("expected class constant", start_PC); |
a12fe13d TT |
1507 | } |
1508 | ||
1509 | type check_constant (int index) | |
1510 | { | |
1511 | check_pool_index (index); | |
1512 | _Jv_Constants *pool = ¤t_class->constants; | |
1513 | if (pool->tags[index] == JV_CONSTANT_ResolvedString | |
1514 | || pool->tags[index] == JV_CONSTANT_String) | |
1515 | return type (&java::lang::String::class$); | |
1516 | else if (pool->tags[index] == JV_CONSTANT_Integer) | |
1517 | return type (int_type); | |
1518 | else if (pool->tags[index] == JV_CONSTANT_Float) | |
1519 | return type (float_type); | |
60440707 TT |
1520 | verify_fail ("String, int, or float constant expected", start_PC); |
1521 | } | |
1522 | ||
1523 | type check_wide_constant (int index) | |
1524 | { | |
1525 | check_pool_index (index); | |
1526 | _Jv_Constants *pool = ¤t_class->constants; | |
1527 | if (pool->tags[index] == JV_CONSTANT_Long) | |
1528 | return type (long_type); | |
1529 | else if (pool->tags[index] == JV_CONSTANT_Double) | |
1530 | return type (double_type); | |
1531 | verify_fail ("long or double constant expected", start_PC); | |
a12fe13d TT |
1532 | } |
1533 | ||
1534 | // Helper for both field and method. These are laid out the same in | |
1535 | // the constant pool. | |
1536 | type handle_field_or_method (int index, int expected, | |
1537 | _Jv_Utf8Const **name, | |
1538 | _Jv_Utf8Const **fmtype) | |
1539 | { | |
1540 | check_pool_index (index); | |
1541 | _Jv_Constants *pool = ¤t_class->constants; | |
1542 | if (pool->tags[index] != expected) | |
60440707 | 1543 | verify_fail ("didn't see expected constant", start_PC); |
a12fe13d TT |
1544 | // Once we know we have a Fieldref or Methodref we assume that it |
1545 | // is correctly laid out in the constant pool. I think the code | |
1546 | // in defineclass.cc guarantees this. | |
1547 | _Jv_ushort class_index, name_and_type_index; | |
1548 | _Jv_loadIndexes (&pool->data[index], | |
1549 | class_index, | |
1550 | name_and_type_index); | |
1551 | _Jv_ushort name_index, desc_index; | |
1552 | _Jv_loadIndexes (&pool->data[name_and_type_index], | |
1553 | name_index, desc_index); | |
1554 | ||
1555 | *name = pool->data[name_index].utf8; | |
1556 | *fmtype = pool->data[desc_index].utf8; | |
1557 | ||
1558 | return check_class_constant (class_index); | |
1559 | } | |
1560 | ||
1561 | // Return field's type, compute class' type if requested. | |
1562 | type check_field_constant (int index, type *class_type = NULL) | |
1563 | { | |
1564 | _Jv_Utf8Const *name, *field_type; | |
1565 | type ct = handle_field_or_method (index, | |
1566 | JV_CONSTANT_Fieldref, | |
1567 | &name, &field_type); | |
1568 | if (class_type) | |
1569 | *class_type = ct; | |
b34e9a5b TT |
1570 | if (field_type->data[0] == '[' || field_type->data[0] == 'L') |
1571 | return type (field_type); | |
1572 | return get_type_val_for_signature (field_type->data[0]); | |
a12fe13d TT |
1573 | } |
1574 | ||
1575 | type check_method_constant (int index, bool is_interface, | |
1576 | _Jv_Utf8Const **method_name, | |
1577 | _Jv_Utf8Const **method_signature) | |
1578 | { | |
1579 | return handle_field_or_method (index, | |
1580 | (is_interface | |
1581 | ? JV_CONSTANT_InterfaceMethodref | |
1582 | : JV_CONSTANT_Methodref), | |
1583 | method_name, method_signature); | |
1584 | } | |
1585 | ||
1586 | type get_one_type (char *&p) | |
1587 | { | |
1588 | char *start = p; | |
1589 | ||
1590 | int arraycount = 0; | |
1591 | while (*p == '[') | |
1592 | { | |
1593 | ++arraycount; | |
1594 | ++p; | |
1595 | } | |
1596 | ||
1597 | char v = *p++; | |
1598 | ||
1599 | if (v == 'L') | |
1600 | { | |
1601 | while (*p != ';') | |
1602 | ++p; | |
1603 | ++p; | |
1604 | // FIXME! This will get collected! | |
1605 | _Jv_Utf8Const *name = _Jv_makeUtf8Const (start, p - start); | |
1606 | return type (name); | |
1607 | } | |
1608 | ||
1609 | // Casting to jchar here is ok since we are looking at an ASCII | |
1610 | // character. | |
1611 | type_val rt = get_type_val_for_signature (jchar (v)); | |
1612 | ||
1613 | if (arraycount == 0) | |
f6b733ed TT |
1614 | { |
1615 | // Callers of this function eventually push their arguments on | |
1616 | // the stack. So, promote them here. | |
1617 | return type (rt).promote (); | |
1618 | } | |
a12fe13d TT |
1619 | |
1620 | jclass k = construct_primitive_array_type (rt); | |
1621 | while (--arraycount > 0) | |
1622 | k = _Jv_GetArrayClass (k, NULL); | |
1623 | return type (k); | |
1624 | } | |
1625 | ||
1626 | void compute_argument_types (_Jv_Utf8Const *signature, | |
1627 | type *types) | |
1628 | { | |
1629 | char *p = signature->data; | |
1630 | // Skip `('. | |
1631 | ++p; | |
1632 | ||
1633 | int i = 0; | |
1634 | while (*p != ')') | |
1635 | types[i++] = get_one_type (p); | |
1636 | } | |
1637 | ||
1638 | type compute_return_type (_Jv_Utf8Const *signature) | |
1639 | { | |
1640 | char *p = signature->data; | |
1641 | while (*p != ')') | |
1642 | ++p; | |
1643 | ++p; | |
1644 | return get_one_type (p); | |
1645 | } | |
1646 | ||
1647 | void check_return_type (type expected) | |
1648 | { | |
1649 | type rt = compute_return_type (current_method->self->signature); | |
1650 | if (! expected.compatible (rt)) | |
60440707 | 1651 | verify_fail ("incompatible return type", start_PC); |
a12fe13d TT |
1652 | } |
1653 | ||
1654 | void verify_instructions_0 () | |
1655 | { | |
1656 | current_state = new state (current_method->max_stack, | |
1657 | current_method->max_locals); | |
1658 | ||
1659 | PC = 0; | |
60440707 | 1660 | start_PC = 0; |
a12fe13d TT |
1661 | |
1662 | { | |
1663 | int var = 0; | |
1664 | ||
1665 | using namespace java::lang::reflect; | |
1666 | if (! Modifier::isStatic (current_method->self->accflags)) | |
1667 | { | |
1668 | type kurr (current_class); | |
1669 | if (_Jv_equalUtf8Consts (current_method->self->name, gcj::init_name)) | |
1670 | kurr.set_uninitialized (type::SELF); | |
1671 | set_variable (0, kurr); | |
1672 | ++var; | |
1673 | } | |
1674 | ||
60440707 TT |
1675 | // We have to handle wide arguments specially here. |
1676 | int arg_count = _Jv_count_arguments (current_method->self->signature); | |
1677 | type arg_types[arg_count]; | |
1678 | compute_argument_types (current_method->self->signature, arg_types); | |
1679 | for (int i = 0; i < arg_count; ++i) | |
1680 | { | |
1681 | set_variable (var, arg_types[i]); | |
1682 | ++var; | |
1683 | if (arg_types[i].iswide ()) | |
1684 | ++var; | |
1685 | } | |
a12fe13d TT |
1686 | } |
1687 | ||
1688 | states = (state **) _Jv_Malloc (sizeof (state *) | |
1689 | * current_method->code_length); | |
1690 | for (int i = 0; i < current_method->code_length; ++i) | |
1691 | states[i] = NULL; | |
1692 | ||
1693 | next_verify_pc = state::NO_NEXT; | |
1694 | ||
1695 | while (true) | |
1696 | { | |
1697 | // If the PC was invalidated, get a new one from the work list. | |
1698 | if (PC == state::NO_NEXT) | |
1699 | { | |
1700 | PC = pop_jump (); | |
1701 | if (PC == state::INVALID) | |
60440707 | 1702 | verify_fail ("saw state::INVALID", start_PC); |
a12fe13d TT |
1703 | if (PC == state::NO_NEXT) |
1704 | break; | |
1705 | // Set up the current state. | |
1706 | *current_state = *states[PC]; | |
1707 | } | |
1708 | ||
1709 | // Control can't fall off the end of the bytecode. | |
1710 | if (PC >= current_method->code_length) | |
1711 | verify_fail ("fell off end"); | |
1712 | ||
1713 | if (states[PC] != NULL) | |
1714 | { | |
1715 | // We've already visited this instruction. So merge the | |
1716 | // states together. If this yields no change then we don't | |
1717 | // have to re-verify. | |
1718 | if (! current_state->merge (states[PC], false, | |
1719 | current_method->max_stack)) | |
1720 | { | |
1721 | invalidate_pc (); | |
1722 | continue; | |
1723 | } | |
1724 | // Save a copy of it for later. | |
1725 | states[PC]->copy (current_state, current_method->max_stack, | |
1726 | current_method->max_locals); | |
1727 | } | |
1728 | else if ((flags[PC] & FLAG_BRANCH_TARGET)) | |
1729 | { | |
1730 | // We only have to keep saved state at branch targets. | |
1731 | states[PC] = new state (current_state, current_method->max_stack, | |
1732 | current_method->max_locals); | |
1733 | } | |
1734 | ||
1735 | // Update states for all active exception handlers. Ordinarily | |
1736 | // there are not many exception handlers. So we simply run | |
1737 | // through them all. | |
1738 | for (int i = 0; i < current_method->exc_count; ++i) | |
1739 | { | |
1740 | if (PC >= exception[i].start_pc && PC < exception[i].end_pc) | |
1741 | { | |
1742 | type handler = reference_type; | |
1743 | if (exception[i].handler_type != 0) | |
1744 | handler = check_class_constant (exception[i].handler_type); | |
1745 | push_exception_jump (handler, exception[i].handler_pc); | |
1746 | } | |
1747 | } | |
1748 | ||
1749 | start_PC = PC; | |
1750 | unsigned char opcode = bytecode[PC++]; | |
1751 | switch (opcode) | |
1752 | { | |
1753 | case op_nop: | |
1754 | break; | |
1755 | ||
1756 | case op_aconst_null: | |
1757 | push_type (null_type); | |
1758 | break; | |
1759 | ||
1760 | case op_iconst_m1: | |
1761 | case op_iconst_0: | |
1762 | case op_iconst_1: | |
1763 | case op_iconst_2: | |
1764 | case op_iconst_3: | |
1765 | case op_iconst_4: | |
1766 | case op_iconst_5: | |
1767 | push_type (int_type); | |
1768 | break; | |
1769 | ||
1770 | case op_lconst_0: | |
1771 | case op_lconst_1: | |
1772 | push_type (long_type); | |
1773 | break; | |
1774 | ||
1775 | case op_fconst_0: | |
1776 | case op_fconst_1: | |
1777 | case op_fconst_2: | |
1778 | push_type (float_type); | |
1779 | break; | |
1780 | ||
1781 | case op_dconst_0: | |
1782 | case op_dconst_1: | |
1783 | push_type (double_type); | |
1784 | break; | |
1785 | ||
1786 | case op_bipush: | |
1787 | get_byte (); | |
1788 | push_type (int_type); | |
1789 | break; | |
1790 | ||
1791 | case op_sipush: | |
1792 | get_short (); | |
1793 | push_type (int_type); | |
1794 | break; | |
1795 | ||
1796 | case op_ldc: | |
1797 | push_type (check_constant (get_byte ())); | |
1798 | break; | |
1799 | case op_ldc_w: | |
1800 | push_type (check_constant (get_ushort ())); | |
1801 | break; | |
1802 | case op_ldc2_w: | |
60440707 | 1803 | push_type (check_wide_constant (get_ushort ())); |
a12fe13d TT |
1804 | break; |
1805 | ||
1806 | case op_iload: | |
1807 | push_type (get_variable (get_byte (), int_type)); | |
1808 | break; | |
1809 | case op_lload: | |
1810 | push_type (get_variable (get_byte (), long_type)); | |
1811 | break; | |
1812 | case op_fload: | |
1813 | push_type (get_variable (get_byte (), float_type)); | |
1814 | break; | |
1815 | case op_dload: | |
1816 | push_type (get_variable (get_byte (), double_type)); | |
1817 | break; | |
1818 | case op_aload: | |
1819 | push_type (get_variable (get_byte (), reference_type)); | |
1820 | break; | |
1821 | ||
1822 | case op_iload_0: | |
1823 | case op_iload_1: | |
1824 | case op_iload_2: | |
1825 | case op_iload_3: | |
1826 | push_type (get_variable (opcode - op_iload_0, int_type)); | |
1827 | break; | |
1828 | case op_lload_0: | |
1829 | case op_lload_1: | |
1830 | case op_lload_2: | |
1831 | case op_lload_3: | |
1832 | push_type (get_variable (opcode - op_lload_0, long_type)); | |
1833 | break; | |
1834 | case op_fload_0: | |
1835 | case op_fload_1: | |
1836 | case op_fload_2: | |
1837 | case op_fload_3: | |
1838 | push_type (get_variable (opcode - op_fload_0, float_type)); | |
1839 | break; | |
1840 | case op_dload_0: | |
1841 | case op_dload_1: | |
1842 | case op_dload_2: | |
1843 | case op_dload_3: | |
1844 | push_type (get_variable (opcode - op_dload_0, double_type)); | |
1845 | break; | |
1846 | case op_aload_0: | |
1847 | case op_aload_1: | |
1848 | case op_aload_2: | |
1849 | case op_aload_3: | |
1850 | push_type (get_variable (opcode - op_aload_0, reference_type)); | |
1851 | break; | |
1852 | case op_iaload: | |
1853 | pop_type (int_type); | |
1854 | push_type (require_array_type (pop_type (reference_type), | |
1855 | int_type)); | |
1856 | break; | |
1857 | case op_laload: | |
1858 | pop_type (int_type); | |
1859 | push_type (require_array_type (pop_type (reference_type), | |
1860 | long_type)); | |
1861 | break; | |
1862 | case op_faload: | |
1863 | pop_type (int_type); | |
1864 | push_type (require_array_type (pop_type (reference_type), | |
1865 | float_type)); | |
1866 | break; | |
1867 | case op_daload: | |
1868 | pop_type (int_type); | |
1869 | push_type (require_array_type (pop_type (reference_type), | |
1870 | double_type)); | |
1871 | break; | |
1872 | case op_aaload: | |
1873 | pop_type (int_type); | |
1874 | push_type (require_array_type (pop_type (reference_type), | |
1875 | reference_type)); | |
1876 | break; | |
1877 | case op_baload: | |
1878 | pop_type (int_type); | |
1879 | require_array_type (pop_type (reference_type), byte_type); | |
1880 | push_type (int_type); | |
1881 | break; | |
1882 | case op_caload: | |
1883 | pop_type (int_type); | |
1884 | require_array_type (pop_type (reference_type), char_type); | |
1885 | push_type (int_type); | |
1886 | break; | |
1887 | case op_saload: | |
1888 | pop_type (int_type); | |
1889 | require_array_type (pop_type (reference_type), short_type); | |
1890 | push_type (int_type); | |
1891 | break; | |
1892 | case op_istore: | |
1893 | set_variable (get_byte (), pop_type (int_type)); | |
1894 | break; | |
1895 | case op_lstore: | |
1896 | set_variable (get_byte (), pop_type (long_type)); | |
1897 | break; | |
1898 | case op_fstore: | |
1899 | set_variable (get_byte (), pop_type (float_type)); | |
1900 | break; | |
1901 | case op_dstore: | |
1902 | set_variable (get_byte (), pop_type (double_type)); | |
1903 | break; | |
1904 | case op_astore: | |
1905 | set_variable (get_byte (), pop_type (reference_type)); | |
1906 | break; | |
1907 | case op_istore_0: | |
1908 | case op_istore_1: | |
1909 | case op_istore_2: | |
1910 | case op_istore_3: | |
1911 | set_variable (opcode - op_istore_0, pop_type (int_type)); | |
1912 | break; | |
1913 | case op_lstore_0: | |
1914 | case op_lstore_1: | |
1915 | case op_lstore_2: | |
1916 | case op_lstore_3: | |
1917 | set_variable (opcode - op_lstore_0, pop_type (long_type)); | |
1918 | break; | |
1919 | case op_fstore_0: | |
1920 | case op_fstore_1: | |
1921 | case op_fstore_2: | |
1922 | case op_fstore_3: | |
1923 | set_variable (opcode - op_fstore_0, pop_type (float_type)); | |
1924 | break; | |
1925 | case op_dstore_0: | |
1926 | case op_dstore_1: | |
1927 | case op_dstore_2: | |
1928 | case op_dstore_3: | |
1929 | set_variable (opcode - op_dstore_0, pop_type (double_type)); | |
1930 | break; | |
1931 | case op_astore_0: | |
1932 | case op_astore_1: | |
1933 | case op_astore_2: | |
1934 | case op_astore_3: | |
1935 | set_variable (opcode - op_astore_0, pop_type (reference_type)); | |
1936 | break; | |
1937 | case op_iastore: | |
1938 | pop_type (int_type); | |
1939 | pop_type (int_type); | |
1940 | require_array_type (pop_type (reference_type), int_type); | |
1941 | break; | |
1942 | case op_lastore: | |
1943 | pop_type (long_type); | |
1944 | pop_type (int_type); | |
1945 | require_array_type (pop_type (reference_type), long_type); | |
1946 | break; | |
1947 | case op_fastore: | |
1948 | pop_type (float_type); | |
1949 | pop_type (int_type); | |
1950 | require_array_type (pop_type (reference_type), float_type); | |
1951 | break; | |
1952 | case op_dastore: | |
1953 | pop_type (double_type); | |
1954 | pop_type (int_type); | |
1955 | require_array_type (pop_type (reference_type), double_type); | |
1956 | break; | |
1957 | case op_aastore: | |
1958 | pop_type (reference_type); | |
1959 | pop_type (int_type); | |
1960 | require_array_type (pop_type (reference_type), reference_type); | |
1961 | break; | |
1962 | case op_bastore: | |
1963 | pop_type (int_type); | |
1964 | pop_type (int_type); | |
1965 | require_array_type (pop_type (reference_type), byte_type); | |
1966 | break; | |
1967 | case op_castore: | |
1968 | pop_type (int_type); | |
1969 | pop_type (int_type); | |
1970 | require_array_type (pop_type (reference_type), char_type); | |
1971 | break; | |
1972 | case op_sastore: | |
1973 | pop_type (int_type); | |
1974 | pop_type (int_type); | |
1975 | require_array_type (pop_type (reference_type), short_type); | |
1976 | break; | |
1977 | case op_pop: | |
1978 | pop32 (); | |
1979 | break; | |
1980 | case op_pop2: | |
1981 | pop64 (); | |
1982 | break; | |
1983 | case op_dup: | |
1984 | { | |
1985 | type t = pop32 (); | |
1986 | push_type (t); | |
1987 | push_type (t); | |
1988 | } | |
1989 | break; | |
1990 | case op_dup_x1: | |
1991 | { | |
1992 | type t1 = pop32 (); | |
1993 | type t2 = pop32 (); | |
1994 | push_type (t1); | |
1995 | push_type (t2); | |
1996 | push_type (t1); | |
1997 | } | |
1998 | break; | |
1999 | case op_dup_x2: | |
2000 | { | |
2001 | type t1 = pop32 (); | |
6c5a8271 TT |
2002 | type t2 = pop_raw (); |
2003 | if (! t2.iswide ()) | |
2004 | { | |
2005 | type t3 = pop32 (); | |
2006 | push_type (t1); | |
2007 | push_type (t3); | |
2008 | } | |
2009 | else | |
2010 | push_type (t1); | |
a12fe13d TT |
2011 | push_type (t2); |
2012 | push_type (t1); | |
2013 | } | |
2014 | break; | |
2015 | case op_dup2: | |
2016 | { | |
6c5a8271 TT |
2017 | type t = pop_raw (); |
2018 | if (! t.iswide ()) | |
2019 | { | |
2020 | type t2 = pop32 (); | |
2021 | push_type (t2); | |
2022 | push_type (t); | |
2023 | push_type (t2); | |
2024 | } | |
a12fe13d TT |
2025 | push_type (t); |
2026 | } | |
2027 | break; | |
2028 | case op_dup2_x1: | |
2029 | { | |
6c5a8271 TT |
2030 | type t1 = pop_raw (); |
2031 | type t2 = pop32 (); | |
2032 | if (! t1.iswide ()) | |
2033 | { | |
2034 | type t3 = pop32 (); | |
2035 | push_type (t2); | |
2036 | push_type (t1); | |
2037 | push_type (t3); | |
2038 | } | |
2039 | else | |
2040 | push_type (t1); | |
a12fe13d TT |
2041 | push_type (t2); |
2042 | push_type (t1); | |
2043 | } | |
2044 | break; | |
2045 | case op_dup2_x2: | |
2046 | { | |
6c5a8271 TT |
2047 | // FIXME |
2048 | type t1 = pop_raw (); | |
2049 | if (t1.iswide ()) | |
2050 | { | |
2051 | type t2 = pop_raw (); | |
2052 | if (t2.iswide ()) | |
2053 | { | |
2054 | push_type (t1); | |
2055 | push_type (t2); | |
2056 | } | |
2057 | else | |
2058 | { | |
2059 | type t3 = pop32 (); | |
2060 | push_type (t1); | |
2061 | push_type (t3); | |
2062 | push_type (t2); | |
2063 | } | |
2064 | push_type (t1); | |
2065 | } | |
2066 | else | |
2067 | { | |
2068 | type t2 = pop32 (); | |
2069 | type t3 = pop_raw (); | |
2070 | if (t3.iswide ()) | |
2071 | { | |
2072 | push_type (t2); | |
2073 | push_type (t1); | |
2074 | } | |
2075 | else | |
2076 | { | |
2077 | type t4 = pop32 (); | |
2078 | push_type (t2); | |
2079 | push_type (t1); | |
2080 | push_type (t4); | |
2081 | } | |
2082 | push_type (t3); | |
2083 | push_type (t2); | |
2084 | push_type (t1); | |
2085 | } | |
a12fe13d TT |
2086 | } |
2087 | break; | |
2088 | case op_swap: | |
2089 | { | |
2090 | type t1 = pop32 (); | |
2091 | type t2 = pop32 (); | |
2092 | push_type (t1); | |
2093 | push_type (t2); | |
2094 | } | |
2095 | break; | |
2096 | case op_iadd: | |
2097 | case op_isub: | |
2098 | case op_imul: | |
2099 | case op_idiv: | |
2100 | case op_irem: | |
2101 | case op_ishl: | |
2102 | case op_ishr: | |
2103 | case op_iushr: | |
2104 | case op_iand: | |
2105 | case op_ior: | |
2106 | case op_ixor: | |
2107 | pop_type (int_type); | |
2108 | push_type (pop_type (int_type)); | |
2109 | break; | |
2110 | case op_ladd: | |
2111 | case op_lsub: | |
2112 | case op_lmul: | |
2113 | case op_ldiv: | |
2114 | case op_lrem: | |
2115 | case op_lshl: | |
2116 | case op_lshr: | |
2117 | case op_lushr: | |
2118 | case op_land: | |
2119 | case op_lor: | |
2120 | case op_lxor: | |
2121 | pop_type (long_type); | |
2122 | push_type (pop_type (long_type)); | |
2123 | break; | |
2124 | case op_fadd: | |
2125 | case op_fsub: | |
2126 | case op_fmul: | |
2127 | case op_fdiv: | |
2128 | case op_frem: | |
2129 | pop_type (float_type); | |
2130 | push_type (pop_type (float_type)); | |
2131 | break; | |
2132 | case op_dadd: | |
2133 | case op_dsub: | |
2134 | case op_dmul: | |
2135 | case op_ddiv: | |
2136 | case op_drem: | |
2137 | pop_type (double_type); | |
2138 | push_type (pop_type (double_type)); | |
2139 | break; | |
2140 | case op_ineg: | |
2141 | case op_i2b: | |
2142 | case op_i2c: | |
2143 | case op_i2s: | |
2144 | push_type (pop_type (int_type)); | |
2145 | break; | |
2146 | case op_lneg: | |
2147 | push_type (pop_type (long_type)); | |
2148 | break; | |
2149 | case op_fneg: | |
2150 | push_type (pop_type (float_type)); | |
2151 | break; | |
2152 | case op_dneg: | |
2153 | push_type (pop_type (double_type)); | |
2154 | break; | |
2155 | case op_iinc: | |
2156 | get_variable (get_byte (), int_type); | |
2157 | get_byte (); | |
2158 | break; | |
2159 | case op_i2l: | |
2160 | pop_type (int_type); | |
2161 | push_type (long_type); | |
2162 | break; | |
2163 | case op_i2f: | |
2164 | pop_type (int_type); | |
2165 | push_type (float_type); | |
2166 | break; | |
2167 | case op_i2d: | |
2168 | pop_type (int_type); | |
2169 | push_type (double_type); | |
2170 | break; | |
2171 | case op_l2i: | |
2172 | pop_type (long_type); | |
2173 | push_type (int_type); | |
2174 | break; | |
2175 | case op_l2f: | |
2176 | pop_type (long_type); | |
2177 | push_type (float_type); | |
2178 | break; | |
2179 | case op_l2d: | |
2180 | pop_type (long_type); | |
2181 | push_type (double_type); | |
2182 | break; | |
2183 | case op_f2i: | |
2184 | pop_type (float_type); | |
2185 | push_type (int_type); | |
2186 | break; | |
2187 | case op_f2l: | |
2188 | pop_type (float_type); | |
2189 | push_type (long_type); | |
2190 | break; | |
2191 | case op_f2d: | |
2192 | pop_type (float_type); | |
2193 | push_type (double_type); | |
2194 | break; | |
2195 | case op_d2i: | |
2196 | pop_type (double_type); | |
2197 | push_type (int_type); | |
2198 | break; | |
2199 | case op_d2l: | |
2200 | pop_type (double_type); | |
2201 | push_type (long_type); | |
2202 | break; | |
2203 | case op_d2f: | |
2204 | pop_type (double_type); | |
2205 | push_type (float_type); | |
2206 | break; | |
2207 | case op_lcmp: | |
2208 | pop_type (long_type); | |
2209 | pop_type (long_type); | |
2210 | push_type (int_type); | |
2211 | break; | |
2212 | case op_fcmpl: | |
2213 | case op_fcmpg: | |
2214 | pop_type (float_type); | |
2215 | pop_type (float_type); | |
2216 | push_type (int_type); | |
2217 | break; | |
2218 | case op_dcmpl: | |
2219 | case op_dcmpg: | |
2220 | pop_type (double_type); | |
2221 | pop_type (double_type); | |
2222 | push_type (int_type); | |
2223 | break; | |
2224 | case op_ifeq: | |
2225 | case op_ifne: | |
2226 | case op_iflt: | |
2227 | case op_ifge: | |
2228 | case op_ifgt: | |
2229 | case op_ifle: | |
2230 | pop_type (int_type); | |
2231 | push_jump (get_short ()); | |
2232 | break; | |
2233 | case op_if_icmpeq: | |
2234 | case op_if_icmpne: | |
2235 | case op_if_icmplt: | |
2236 | case op_if_icmpge: | |
2237 | case op_if_icmpgt: | |
2238 | case op_if_icmple: | |
2239 | pop_type (int_type); | |
2240 | pop_type (int_type); | |
2241 | push_jump (get_short ()); | |
2242 | break; | |
2243 | case op_if_acmpeq: | |
2244 | case op_if_acmpne: | |
2245 | pop_type (reference_type); | |
2246 | pop_type (reference_type); | |
2247 | push_jump (get_short ()); | |
2248 | break; | |
2249 | case op_goto: | |
2250 | push_jump (get_short ()); | |
2251 | invalidate_pc (); | |
2252 | break; | |
2253 | case op_jsr: | |
2254 | handle_jsr_insn (get_short ()); | |
2255 | break; | |
2256 | case op_ret: | |
2257 | handle_ret_insn (get_byte ()); | |
2258 | break; | |
2259 | case op_tableswitch: | |
2260 | { | |
2261 | pop_type (int_type); | |
2262 | skip_padding (); | |
2263 | push_jump (get_int ()); | |
2264 | jint low = get_int (); | |
2265 | jint high = get_int (); | |
2266 | // Already checked LOW -vs- HIGH. | |
2267 | for (int i = low; i <= high; ++i) | |
2268 | push_jump (get_int ()); | |
2269 | invalidate_pc (); | |
2270 | } | |
2271 | break; | |
2272 | ||
2273 | case op_lookupswitch: | |
2274 | { | |
2275 | pop_type (int_type); | |
2276 | skip_padding (); | |
2277 | push_jump (get_int ()); | |
2278 | jint npairs = get_int (); | |
2279 | // Already checked NPAIRS >= 0. | |
2280 | jint lastkey = 0; | |
2281 | for (int i = 0; i < npairs; ++i) | |
2282 | { | |
2283 | jint key = get_int (); | |
2284 | if (i > 0 && key <= lastkey) | |
60440707 | 2285 | verify_fail ("lookupswitch pairs unsorted", start_PC); |
a12fe13d TT |
2286 | lastkey = key; |
2287 | push_jump (get_int ()); | |
2288 | } | |
2289 | invalidate_pc (); | |
2290 | } | |
2291 | break; | |
2292 | case op_ireturn: | |
2293 | check_return_type (pop_type (int_type)); | |
2294 | invalidate_pc (); | |
2295 | break; | |
2296 | case op_lreturn: | |
2297 | check_return_type (pop_type (long_type)); | |
2298 | invalidate_pc (); | |
2299 | break; | |
2300 | case op_freturn: | |
2301 | check_return_type (pop_type (float_type)); | |
2302 | invalidate_pc (); | |
2303 | break; | |
2304 | case op_dreturn: | |
2305 | check_return_type (pop_type (double_type)); | |
2306 | invalidate_pc (); | |
2307 | break; | |
2308 | case op_areturn: | |
2309 | check_return_type (pop_type (reference_type)); | |
2310 | invalidate_pc (); | |
2311 | break; | |
2312 | case op_return: | |
2313 | check_return_type (void_type); | |
2314 | invalidate_pc (); | |
2315 | break; | |
2316 | case op_getstatic: | |
2317 | push_type (check_field_constant (get_ushort ())); | |
2318 | break; | |
2319 | case op_putstatic: | |
2320 | pop_type (check_field_constant (get_ushort ())); | |
2321 | break; | |
2322 | case op_getfield: | |
2323 | { | |
2324 | type klass; | |
2325 | type field = check_field_constant (get_ushort (), &klass); | |
2326 | pop_type (klass); | |
2327 | push_type (field); | |
2328 | } | |
2329 | break; | |
2330 | case op_putfield: | |
2331 | { | |
2332 | type klass; | |
2333 | type field = check_field_constant (get_ushort (), &klass); | |
2334 | pop_type (field); | |
2335 | pop_type (klass); | |
2336 | } | |
2337 | break; | |
2338 | ||
2339 | case op_invokevirtual: | |
2340 | case op_invokespecial: | |
2341 | case op_invokestatic: | |
2342 | case op_invokeinterface: | |
2343 | { | |
2344 | _Jv_Utf8Const *method_name, *method_signature; | |
2345 | type class_type | |
2346 | = check_method_constant (get_ushort (), | |
2347 | opcode == (unsigned char) op_invokeinterface, | |
2348 | &method_name, | |
2349 | &method_signature); | |
2350 | int arg_count = _Jv_count_arguments (method_signature); | |
2351 | if (opcode == (unsigned char) op_invokeinterface) | |
2352 | { | |
2353 | int nargs = get_byte (); | |
2354 | if (nargs == 0) | |
60440707 TT |
2355 | verify_fail ("too few arguments to invokeinterface", |
2356 | start_PC); | |
a12fe13d | 2357 | if (get_byte () != 0) |
60440707 TT |
2358 | verify_fail ("invokeinterface dummy byte is wrong", |
2359 | start_PC); | |
a12fe13d | 2360 | if (nargs - 1 != arg_count) |
60440707 TT |
2361 | verify_fail ("wrong argument count for invokeinterface", |
2362 | start_PC); | |
a12fe13d TT |
2363 | } |
2364 | ||
2365 | bool is_init = false; | |
2366 | if (_Jv_equalUtf8Consts (method_name, gcj::init_name)) | |
2367 | { | |
2368 | is_init = true; | |
2369 | if (opcode != (unsigned char) op_invokespecial) | |
60440707 | 2370 | verify_fail ("can't invoke <init>", start_PC); |
a12fe13d TT |
2371 | } |
2372 | else if (method_name->data[0] == '<') | |
60440707 TT |
2373 | verify_fail ("can't invoke method starting with `<'", |
2374 | start_PC); | |
a12fe13d TT |
2375 | |
2376 | // Pop arguments and check types. | |
2377 | type arg_types[arg_count]; | |
2378 | compute_argument_types (method_signature, arg_types); | |
2379 | for (int i = arg_count - 1; i >= 0; --i) | |
2380 | pop_type (arg_types[i]); | |
2381 | ||
2382 | if (opcode != (unsigned char) op_invokestatic) | |
2383 | { | |
2384 | type t = class_type; | |
2385 | if (is_init) | |
2386 | { | |
2387 | // In this case the PC doesn't matter. | |
2388 | t.set_uninitialized (type::UNINIT); | |
2389 | } | |
2390 | t = pop_type (t); | |
2391 | if (is_init) | |
2392 | current_state->set_initialized (t.get_pc (), | |
2393 | current_method->max_locals); | |
2394 | } | |
2395 | ||
2396 | type rt = compute_return_type (method_signature); | |
2397 | if (! rt.isvoid ()) | |
2398 | push_type (rt); | |
2399 | } | |
2400 | break; | |
2401 | ||
2402 | case op_new: | |
2403 | { | |
2404 | type t = check_class_constant (get_ushort ()); | |
2405 | if (t.isarray () || t.isinterface () || t.isabstract ()) | |
60440707 TT |
2406 | verify_fail ("type is array, interface, or abstract", |
2407 | start_PC); | |
a12fe13d TT |
2408 | t.set_uninitialized (start_PC); |
2409 | push_type (t); | |
2410 | } | |
2411 | break; | |
2412 | ||
2413 | case op_newarray: | |
2414 | { | |
2415 | int atype = get_byte (); | |
2416 | // We intentionally have chosen constants to make this | |
2417 | // valid. | |
2418 | if (atype < boolean_type || atype > long_type) | |
60440707 | 2419 | verify_fail ("type not primitive", start_PC); |
a12fe13d TT |
2420 | pop_type (int_type); |
2421 | push_type (construct_primitive_array_type (type_val (atype))); | |
2422 | } | |
2423 | break; | |
2424 | case op_anewarray: | |
2425 | pop_type (int_type); | |
2426 | push_type (check_class_constant (get_ushort ())); | |
2427 | break; | |
2428 | case op_arraylength: | |
2429 | { | |
2430 | type t = pop_type (reference_type); | |
2431 | if (! t.isarray ()) | |
60440707 | 2432 | verify_fail ("array type expected", start_PC); |
a12fe13d TT |
2433 | push_type (int_type); |
2434 | } | |
2435 | break; | |
2436 | case op_athrow: | |
2437 | pop_type (type (&java::lang::Throwable::class$)); | |
2438 | invalidate_pc (); | |
2439 | break; | |
2440 | case op_checkcast: | |
2441 | pop_type (reference_type); | |
2442 | push_type (check_class_constant (get_ushort ())); | |
2443 | break; | |
2444 | case op_instanceof: | |
2445 | pop_type (reference_type); | |
2446 | check_class_constant (get_ushort ()); | |
2447 | push_type (int_type); | |
2448 | break; | |
2449 | case op_monitorenter: | |
2450 | pop_type (reference_type); | |
2451 | break; | |
2452 | case op_monitorexit: | |
2453 | pop_type (reference_type); | |
2454 | break; | |
2455 | case op_wide: | |
2456 | { | |
2457 | switch (get_byte ()) | |
2458 | { | |
2459 | case op_iload: | |
2460 | push_type (get_variable (get_ushort (), int_type)); | |
2461 | break; | |
2462 | case op_lload: | |
2463 | push_type (get_variable (get_ushort (), long_type)); | |
2464 | break; | |
2465 | case op_fload: | |
2466 | push_type (get_variable (get_ushort (), float_type)); | |
2467 | break; | |
2468 | case op_dload: | |
2469 | push_type (get_variable (get_ushort (), double_type)); | |
2470 | break; | |
2471 | case op_aload: | |
2472 | push_type (get_variable (get_ushort (), reference_type)); | |
2473 | break; | |
2474 | case op_istore: | |
2475 | set_variable (get_ushort (), pop_type (int_type)); | |
2476 | break; | |
2477 | case op_lstore: | |
2478 | set_variable (get_ushort (), pop_type (long_type)); | |
2479 | break; | |
2480 | case op_fstore: | |
2481 | set_variable (get_ushort (), pop_type (float_type)); | |
2482 | break; | |
2483 | case op_dstore: | |
2484 | set_variable (get_ushort (), pop_type (double_type)); | |
2485 | break; | |
2486 | case op_astore: | |
2487 | set_variable (get_ushort (), pop_type (reference_type)); | |
2488 | break; | |
2489 | case op_ret: | |
2490 | handle_ret_insn (get_short ()); | |
2491 | break; | |
2492 | case op_iinc: | |
2493 | get_variable (get_ushort (), int_type); | |
2494 | get_short (); | |
2495 | break; | |
2496 | default: | |
60440707 | 2497 | verify_fail ("unrecognized wide instruction", start_PC); |
a12fe13d TT |
2498 | } |
2499 | } | |
2500 | break; | |
2501 | case op_multianewarray: | |
2502 | { | |
2503 | type atype = check_class_constant (get_ushort ()); | |
2504 | int dim = get_byte (); | |
2505 | if (dim < 1) | |
60440707 | 2506 | verify_fail ("too few dimensions to multianewarray", start_PC); |
a12fe13d TT |
2507 | atype.verify_dimensions (dim); |
2508 | for (int i = 0; i < dim; ++i) | |
2509 | pop_type (int_type); | |
2510 | push_type (atype); | |
2511 | } | |
2512 | break; | |
2513 | case op_ifnull: | |
2514 | case op_ifnonnull: | |
2515 | pop_type (reference_type); | |
2516 | push_jump (get_short ()); | |
2517 | break; | |
2518 | case op_goto_w: | |
2519 | push_jump (get_int ()); | |
2520 | invalidate_pc (); | |
2521 | break; | |
2522 | case op_jsr_w: | |
2523 | handle_jsr_insn (get_int ()); | |
2524 | break; | |
2525 | ||
2526 | default: | |
2527 | // Unrecognized opcode. | |
60440707 TT |
2528 | verify_fail ("unrecognized instruction in verify_instructions_0", |
2529 | start_PC); | |
a12fe13d TT |
2530 | } |
2531 | } | |
2532 | } | |
2533 | ||
2534 | public: | |
2535 | ||
2536 | void verify_instructions () | |
2537 | { | |
2538 | branch_prepass (); | |
2539 | verify_instructions_0 (); | |
2540 | } | |
2541 | ||
2542 | _Jv_BytecodeVerifier (_Jv_InterpMethod *m) | |
2543 | { | |
2544 | current_method = m; | |
2545 | bytecode = m->bytecode (); | |
2546 | exception = m->exceptions (); | |
2547 | current_class = m->defining_class; | |
2548 | ||
2549 | states = NULL; | |
2550 | flags = NULL; | |
2551 | jsr_ptrs = NULL; | |
2552 | } | |
2553 | ||
2554 | ~_Jv_BytecodeVerifier () | |
2555 | { | |
2556 | if (states) | |
2557 | _Jv_Free (states); | |
2558 | if (flags) | |
2559 | _Jv_Free (flags); | |
2560 | if (jsr_ptrs) | |
2561 | _Jv_Free (jsr_ptrs); | |
2562 | } | |
2563 | }; | |
2564 | ||
2565 | void | |
2566 | _Jv_VerifyMethod (_Jv_InterpMethod *meth) | |
2567 | { | |
2568 | _Jv_BytecodeVerifier v (meth); | |
2569 | v.verify_instructions (); | |
2570 | } | |
2571 | ||
2572 | // FIXME: add more info, like PC, when required. | |
2573 | static void | |
60440707 | 2574 | verify_fail (char *s, jint pc) |
a12fe13d | 2575 | { |
60440707 TT |
2576 | using namespace java::lang; |
2577 | StringBuffer *buf = new StringBuffer (); | |
2578 | ||
2579 | buf->append (JvNewStringLatin1 ("verification failed")); | |
2580 | if (pc != -1) | |
2581 | { | |
2582 | buf->append (JvNewStringLatin1 (" at PC ")); | |
2583 | buf->append (pc); | |
2584 | } | |
2585 | buf->append (JvNewStringLatin1 (": ")); | |
2586 | buf->append (JvNewStringLatin1 (s)); | |
2587 | throw new java::lang::VerifyError (buf->toString ()); | |
a12fe13d | 2588 | } |
75b17b74 JS |
2589 | |
2590 | #endif /* INTERPRETER */ |