Daily bump.
[official-gcc.git] / libjava / verify.cc
blobcbadd9f2f2f96d7d29ad4d9734ce7d989e66d5f0
1 // defineclass.cc - defining a class from .class format.
3 /* Copyright (C) 2001, 2002 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 // Written by Tom Tromey <tromey@redhat.com>
13 // Define VERIFY_DEBUG to enable debugging output.
15 #include <config.h>
17 #include <jvm.h>
18 #include <gcj/cni.h>
19 #include <java-insns.h>
20 #include <java-interp.h>
22 #ifdef INTERPRETER
24 #include <java/lang/Class.h>
25 #include <java/lang/VerifyError.h>
26 #include <java/lang/Throwable.h>
27 #include <java/lang/reflect/Modifier.h>
28 #include <java/lang/StringBuffer.h>
30 #ifdef VERIFY_DEBUG
31 #include <stdio.h>
32 #endif /* VERIFY_DEBUG */
35 static void debug_print (const char *fmt, ...)
36 __attribute__ ((format (printf, 1, 2)));
38 static inline void
39 debug_print (const char *fmt, ...)
41 #ifdef VERIFY_DEBUG
42 va_list ap;
43 va_start (ap, fmt);
44 vfprintf (stderr, fmt, ap);
45 va_end (ap);
46 #endif /* VERIFY_DEBUG */
49 class _Jv_BytecodeVerifier
51 private:
53 static const int FLAG_INSN_START = 1;
54 static const int FLAG_BRANCH_TARGET = 2;
56 struct state;
57 struct type;
58 struct subr_info;
59 struct subr_entry_info;
60 struct linked_utf8;
62 // The current PC.
63 int PC;
64 // The PC corresponding to the start of the current instruction.
65 int start_PC;
67 // The current state of the stack, locals, etc.
68 state *current_state;
70 // We store the state at branch targets, for merging. This holds
71 // such states.
72 state **states;
74 // We keep a linked list of all the PCs which we must reverify.
75 // The link is done using the PC values. This is the head of the
76 // list.
77 int next_verify_pc;
79 // We keep some flags for each instruction. The values are the
80 // FLAG_* constants defined above.
81 char *flags;
83 // We need to keep track of which instructions can call a given
84 // subroutine. FIXME: this is inefficient. We keep a linked list
85 // of all calling `jsr's at at each jsr target.
86 subr_info **jsr_ptrs;
88 // We keep a linked list of entries which map each `ret' instruction
89 // to its unique subroutine entry point. We expect that there won't
90 // be many `ret' instructions, so a linked list is ok.
91 subr_entry_info *entry_points;
93 // The current top of the stack, in terms of slots.
94 int stacktop;
95 // The current depth of the stack. This will be larger than
96 // STACKTOP when wide types are on the stack.
97 int stackdepth;
99 // The bytecode itself.
100 unsigned char *bytecode;
101 // The exceptions.
102 _Jv_InterpException *exception;
104 // Defining class.
105 jclass current_class;
106 // This method.
107 _Jv_InterpMethod *current_method;
109 // A linked list of utf8 objects we allocate. This is really ugly,
110 // but without this our utf8 objects would be collected.
111 linked_utf8 *utf8_list;
113 struct linked_utf8
115 _Jv_Utf8Const *val;
116 linked_utf8 *next;
119 _Jv_Utf8Const *make_utf8_const (char *s, int len)
121 _Jv_Utf8Const *val = _Jv_makeUtf8Const (s, len);
122 _Jv_Utf8Const *r = (_Jv_Utf8Const *) _Jv_Malloc (sizeof (_Jv_Utf8Const)
123 + val->length
124 + 1);
125 r->length = val->length;
126 r->hash = val->hash;
127 memcpy (r->data, val->data, val->length + 1);
129 linked_utf8 *lu = (linked_utf8 *) _Jv_Malloc (sizeof (linked_utf8));
130 lu->val = r;
131 lu->next = utf8_list;
132 utf8_list = lu;
134 return r;
137 // This enum holds a list of tags for all the different types we
138 // need to handle. Reference types are treated specially by the
139 // type class.
140 enum type_val
142 void_type,
144 // The values for primitive types are chosen to correspond to values
145 // specified to newarray.
146 boolean_type = 4,
147 char_type = 5,
148 float_type = 6,
149 double_type = 7,
150 byte_type = 8,
151 short_type = 9,
152 int_type = 10,
153 long_type = 11,
155 // Used when overwriting second word of a double or long in the
156 // local variables. Also used after merging local variable states
157 // to indicate an unusable value.
158 unsuitable_type,
159 return_address_type,
160 continuation_type,
162 // There is an obscure special case which requires us to note when
163 // a local variable has not been used by a subroutine. See
164 // push_jump_merge for more information.
165 unused_by_subroutine_type,
167 // Everything after `reference_type' must be a reference type.
168 reference_type,
169 null_type,
170 unresolved_reference_type,
171 uninitialized_reference_type,
172 uninitialized_unresolved_reference_type
175 // Return the type_val corresponding to a primitive signature
176 // character. For instance `I' returns `int.class'.
177 type_val get_type_val_for_signature (jchar sig)
179 type_val rt;
180 switch (sig)
182 case 'Z':
183 rt = boolean_type;
184 break;
185 case 'B':
186 rt = byte_type;
187 break;
188 case 'C':
189 rt = char_type;
190 break;
191 case 'S':
192 rt = short_type;
193 break;
194 case 'I':
195 rt = int_type;
196 break;
197 case 'J':
198 rt = long_type;
199 break;
200 case 'F':
201 rt = float_type;
202 break;
203 case 'D':
204 rt = double_type;
205 break;
206 case 'V':
207 rt = void_type;
208 break;
209 default:
210 verify_fail ("invalid signature");
212 return rt;
215 // Return the type_val corresponding to a primitive class.
216 type_val get_type_val_for_signature (jclass k)
218 return get_type_val_for_signature ((jchar) k->method_count);
221 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
222 // TARGET haven't been prepared.
223 static bool is_assignable_from_slow (jclass target, jclass source)
225 // This will terminate when SOURCE==Object.
226 while (true)
228 if (source == target)
229 return true;
231 if (target->isPrimitive () || source->isPrimitive ())
232 return false;
234 // Check array case first because we can have an array whose
235 // component type is not prepared; _Jv_IsAssignableFrom
236 // doesn't handle this correctly.
237 if (target->isArray ())
239 if (! source->isArray ())
240 return false;
241 target = target->getComponentType ();
242 source = source->getComponentType ();
244 // _Jv_IsAssignableFrom can handle a target which is an
245 // interface even if it hasn't been prepared.
246 else if ((target->state > JV_STATE_LINKED || target->isInterface ())
247 && source->state > JV_STATE_LINKED)
248 return _Jv_IsAssignableFrom (target, source);
249 else if (target->isInterface ())
251 for (int i = 0; i < source->interface_count; ++i)
253 // We use a recursive call because we also need to
254 // check superinterfaces.
255 if (is_assignable_from_slow (target, source->interfaces[i]))
256 return true;
258 source = source->getSuperclass ();
259 if (source == NULL)
260 return false;
262 // We must do this check before we check to see if SOURCE is
263 // an interface. This way we know that any interface is
264 // assignable to an Object.
265 else if (target == &java::lang::Object::class$)
266 return true;
267 else if (source->isInterface ())
269 for (int i = 0; i < target->interface_count; ++i)
271 // We use a recursive call because we also need to
272 // check superinterfaces.
273 if (is_assignable_from_slow (target->interfaces[i], source))
274 return true;
276 target = target->getSuperclass ();
277 if (target == NULL)
278 return false;
280 else if (source == &java::lang::Object::class$)
281 return false;
282 else
283 source = source->getSuperclass ();
287 // This is used to keep track of which `jsr's correspond to a given
288 // jsr target.
289 struct subr_info
291 // PC of the instruction just after the jsr.
292 int pc;
293 // Link.
294 subr_info *next;
297 // This is used to keep track of which subroutine entry point
298 // corresponds to which `ret' instruction.
299 struct subr_entry_info
301 // PC of the subroutine entry point.
302 int pc;
303 // PC of the `ret' instruction.
304 int ret_pc;
305 // Link.
306 subr_entry_info *next;
309 // The `type' class is used to represent a single type in the
310 // verifier.
311 struct type
313 // The type.
314 type_val key;
315 // Some associated data.
316 union
318 // For a resolved reference type, this is a pointer to the class.
319 jclass klass;
320 // For other reference types, this it the name of the class.
321 _Jv_Utf8Const *name;
322 } data;
323 // This is used when constructing a new object. It is the PC of the
324 // `new' instruction which created the object. We use the special
325 // value -2 to mean that this is uninitialized, and the special
326 // value -1 for the case where the current method is itself the
327 // <init> method.
328 int pc;
330 static const int UNINIT = -2;
331 static const int SELF = -1;
333 // Basic constructor.
334 type ()
336 key = unsuitable_type;
337 data.klass = NULL;
338 pc = UNINIT;
341 // Make a new instance given the type tag. We assume a generic
342 // `reference_type' means Object.
343 type (type_val k)
345 key = k;
346 data.klass = NULL;
347 if (key == reference_type)
348 data.klass = &java::lang::Object::class$;
349 pc = UNINIT;
352 // Make a new instance given a class.
353 type (jclass klass)
355 key = reference_type;
356 data.klass = klass;
357 pc = UNINIT;
360 // Make a new instance given the name of a class.
361 type (_Jv_Utf8Const *n)
363 key = unresolved_reference_type;
364 data.name = n;
365 pc = UNINIT;
368 // Copy constructor.
369 type (const type &t)
371 key = t.key;
372 data = t.data;
373 pc = t.pc;
376 // These operators are required because libgcj can't link in
377 // -lstdc++.
378 void *operator new[] (size_t bytes)
380 return _Jv_Malloc (bytes);
383 void operator delete[] (void *mem)
385 _Jv_Free (mem);
388 type& operator= (type_val k)
390 key = k;
391 data.klass = NULL;
392 pc = UNINIT;
393 return *this;
396 type& operator= (const type& t)
398 key = t.key;
399 data = t.data;
400 pc = t.pc;
401 return *this;
404 // Promote a numeric type.
405 type &promote ()
407 if (key == boolean_type || key == char_type
408 || key == byte_type || key == short_type)
409 key = int_type;
410 return *this;
413 // If *THIS is an unresolved reference type, resolve it.
414 void resolve (_Jv_BytecodeVerifier *verifier)
416 if (key != unresolved_reference_type
417 && key != uninitialized_unresolved_reference_type)
418 return;
420 using namespace java::lang;
421 java::lang::ClassLoader *loader
422 = verifier->current_class->getClassLoader();
423 // We might see either kind of name. Sigh.
424 if (data.name->data[0] == 'L'
425 && data.name->data[data.name->length - 1] == ';')
426 data.klass = _Jv_FindClassFromSignature (data.name->data, loader);
427 else
428 data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
429 false, loader);
430 key = (key == unresolved_reference_type
431 ? reference_type
432 : uninitialized_reference_type);
435 // Mark this type as the uninitialized result of `new'.
436 void set_uninitialized (int npc, _Jv_BytecodeVerifier *verifier)
438 if (key == reference_type)
439 key = uninitialized_reference_type;
440 else if (key == unresolved_reference_type)
441 key = uninitialized_unresolved_reference_type;
442 else
443 verifier->verify_fail ("internal error in type::uninitialized");
444 pc = npc;
447 // Mark this type as now initialized.
448 void set_initialized (int npc)
450 if (npc != UNINIT && pc == npc
451 && (key == uninitialized_reference_type
452 || key == uninitialized_unresolved_reference_type))
454 key = (key == uninitialized_reference_type
455 ? reference_type
456 : unresolved_reference_type);
457 pc = UNINIT;
462 // Return true if an object of type K can be assigned to a variable
463 // of type *THIS. Handle various special cases too. Might modify
464 // *THIS or K. Note however that this does not perform numeric
465 // promotion.
466 bool compatible (type &k, _Jv_BytecodeVerifier *verifier)
468 // Any type is compatible with the unsuitable type.
469 if (key == unsuitable_type)
470 return true;
472 if (key < reference_type || k.key < reference_type)
473 return key == k.key;
475 // The `null' type is convertible to any reference type.
476 // FIXME: is this correct for THIS?
477 if (key == null_type || k.key == null_type)
478 return true;
480 // Any reference type is convertible to Object. This is a special
481 // case so we don't need to unnecessarily resolve a class.
482 if (key == reference_type
483 && data.klass == &java::lang::Object::class$)
484 return true;
486 // An initialized type and an uninitialized type are not
487 // compatible.
488 if (isinitialized () != k.isinitialized ())
489 return false;
491 // Two uninitialized objects are compatible if either:
492 // * The PCs are identical, or
493 // * One PC is UNINIT.
494 if (! isinitialized ())
496 if (pc != k.pc && pc != UNINIT && k.pc != UNINIT)
497 return false;
500 // Two unresolved types are equal if their names are the same.
501 if (! isresolved ()
502 && ! k.isresolved ()
503 && _Jv_equalUtf8Consts (data.name, k.data.name))
504 return true;
506 // We must resolve both types and check assignability.
507 resolve (verifier);
508 k.resolve (verifier);
509 return is_assignable_from_slow (data.klass, k.data.klass);
512 bool isvoid () const
514 return key == void_type;
517 bool iswide () const
519 return key == long_type || key == double_type;
522 // Return number of stack or local variable slots taken by this
523 // type.
524 int depth () const
526 return iswide () ? 2 : 1;
529 bool isarray () const
531 // We treat null_type as not an array. This is ok based on the
532 // current uses of this method.
533 if (key == reference_type)
534 return data.klass->isArray ();
535 else if (key == unresolved_reference_type)
536 return data.name->data[0] == '[';
537 return false;
540 bool isnull () const
542 return key == null_type;
545 bool isinterface (_Jv_BytecodeVerifier *verifier)
547 resolve (verifier);
548 if (key != reference_type)
549 return false;
550 return data.klass->isInterface ();
553 bool isabstract (_Jv_BytecodeVerifier *verifier)
555 resolve (verifier);
556 if (key != reference_type)
557 return false;
558 using namespace java::lang::reflect;
559 return Modifier::isAbstract (data.klass->getModifiers ());
562 // Return the element type of an array.
563 type element_type (_Jv_BytecodeVerifier *verifier)
565 // FIXME: maybe should do string manipulation here.
566 resolve (verifier);
567 if (key != reference_type)
568 verifier->verify_fail ("programmer error in type::element_type()", -1);
570 jclass k = data.klass->getComponentType ();
571 if (k->isPrimitive ())
572 return type (verifier->get_type_val_for_signature (k));
573 return type (k);
576 // Return the array type corresponding to an initialized
577 // reference. We could expand this to work for other kinds of
578 // types, but currently we don't need to.
579 type to_array (_Jv_BytecodeVerifier *verifier)
581 // Resolving isn't ideal, because it might force us to load
582 // another class, but it's easy. FIXME?
583 if (key == unresolved_reference_type)
584 resolve (verifier);
586 if (key == reference_type)
587 return type (_Jv_GetArrayClass (data.klass,
588 data.klass->getClassLoader ()));
589 else
590 verifier->verify_fail ("internal error in type::to_array()");
593 bool isreference () const
595 return key >= reference_type;
598 int get_pc () const
600 return pc;
603 bool isinitialized () const
605 return (key == reference_type
606 || key == null_type
607 || key == unresolved_reference_type);
610 bool isresolved () const
612 return (key == reference_type
613 || key == null_type
614 || key == uninitialized_reference_type);
617 void verify_dimensions (int ndims, _Jv_BytecodeVerifier *verifier)
619 // The way this is written, we don't need to check isarray().
620 if (key == reference_type)
622 jclass k = data.klass;
623 while (k->isArray () && ndims > 0)
625 k = k->getComponentType ();
626 --ndims;
629 else
631 // We know KEY == unresolved_reference_type.
632 char *p = data.name->data;
633 while (*p++ == '[' && ndims-- > 0)
637 if (ndims > 0)
638 verifier->verify_fail ("array type has fewer dimensions than required");
641 // Merge OLD_TYPE into this. On error throw exception.
642 bool merge (type& old_type, bool local_semantics,
643 _Jv_BytecodeVerifier *verifier)
645 bool changed = false;
646 bool refo = old_type.isreference ();
647 bool refn = isreference ();
648 if (refo && refn)
650 if (old_type.key == null_type)
652 else if (key == null_type)
654 *this = old_type;
655 changed = true;
657 else if (isinitialized () != old_type.isinitialized ())
658 verifier->verify_fail ("merging initialized and uninitialized types");
659 else
661 if (! isinitialized ())
663 if (pc == UNINIT)
664 pc = old_type.pc;
665 else if (old_type.pc == UNINIT)
667 else if (pc != old_type.pc)
668 verifier->verify_fail ("merging different uninitialized types");
671 if (! isresolved ()
672 && ! old_type.isresolved ()
673 && _Jv_equalUtf8Consts (data.name, old_type.data.name))
675 // Types are identical.
677 else
679 resolve (verifier);
680 old_type.resolve (verifier);
682 jclass k = data.klass;
683 jclass oldk = old_type.data.klass;
685 int arraycount = 0;
686 while (k->isArray () && oldk->isArray ())
688 ++arraycount;
689 k = k->getComponentType ();
690 oldk = oldk->getComponentType ();
693 // Ordinarily this terminates when we hit Object...
694 while (k != NULL)
696 if (is_assignable_from_slow (k, oldk))
697 break;
698 k = k->getSuperclass ();
699 changed = true;
701 // ... but K could have been an interface, in which
702 // case we'll end up here. We just convert this
703 // into Object.
704 if (k == NULL)
705 k = &java::lang::Object::class$;
707 if (changed)
709 while (arraycount > 0)
711 java::lang::ClassLoader *loader
712 = verifier->current_class->getClassLoader();
713 k = _Jv_GetArrayClass (k, loader);
714 --arraycount;
716 data.klass = k;
721 else if (refo || refn || key != old_type.key)
723 if (local_semantics)
725 // If we're merging into an "unused" slot, then we
726 // simply accept whatever we're merging from.
727 if (key == unused_by_subroutine_type)
729 *this = old_type;
730 changed = true;
732 else if (old_type.key == unused_by_subroutine_type)
734 // Do nothing.
736 // If we already have an `unsuitable' type, then we
737 // don't need to change again.
738 else if (key != unsuitable_type)
740 key = unsuitable_type;
741 changed = true;
744 else
745 verifier->verify_fail ("unmergeable type");
747 return changed;
750 #ifdef VERIFY_DEBUG
751 void print (void) const
753 char c = '?';
754 switch (key)
756 case boolean_type: c = 'Z'; break;
757 case byte_type: c = 'B'; break;
758 case char_type: c = 'C'; break;
759 case short_type: c = 'S'; break;
760 case int_type: c = 'I'; break;
761 case long_type: c = 'J'; break;
762 case float_type: c = 'F'; break;
763 case double_type: c = 'D'; break;
764 case void_type: c = 'V'; break;
765 case unsuitable_type: c = '-'; break;
766 case return_address_type: c = 'r'; break;
767 case continuation_type: c = '+'; break;
768 case unused_by_subroutine_type: c = '_'; break;
769 case reference_type: c = 'L'; break;
770 case null_type: c = '@'; break;
771 case unresolved_reference_type: c = 'l'; break;
772 case uninitialized_reference_type: c = 'U'; break;
773 case uninitialized_unresolved_reference_type: c = 'u'; break;
775 debug_print ("%c", c);
777 #endif /* VERIFY_DEBUG */
780 // This class holds all the state information we need for a given
781 // location.
782 struct state
784 // Current top of stack.
785 int stacktop;
786 // Current stack depth. This is like the top of stack but it
787 // includes wide variable information.
788 int stackdepth;
789 // The stack.
790 type *stack;
791 // The local variables.
792 type *locals;
793 // This is used in subroutines to keep track of which local
794 // variables have been accessed.
795 bool *local_changed;
796 // If not 0, then we are in a subroutine. The value is the PC of
797 // the subroutine's entry point. We can use 0 as an exceptional
798 // value because PC=0 can never be a subroutine.
799 int subroutine;
800 // This is used to keep a linked list of all the states which
801 // require re-verification. We use the PC to keep track.
802 int next;
803 // We keep track of the type of `this' specially. This is used to
804 // ensure that an instance initializer invokes another initializer
805 // on `this' before returning. We must keep track of this
806 // specially because otherwise we might be confused by code which
807 // assigns to locals[0] (overwriting `this') and then returns
808 // without really initializing.
809 type this_type;
811 // INVALID marks a state which is not on the linked list of states
812 // requiring reverification.
813 static const int INVALID = -1;
814 // NO_NEXT marks the state at the end of the reverification list.
815 static const int NO_NEXT = -2;
817 state ()
818 : this_type ()
820 stack = NULL;
821 locals = NULL;
822 local_changed = NULL;
825 state (int max_stack, int max_locals)
826 : this_type ()
828 stacktop = 0;
829 stackdepth = 0;
830 stack = new type[max_stack];
831 for (int i = 0; i < max_stack; ++i)
832 stack[i] = unsuitable_type;
833 locals = new type[max_locals];
834 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
835 for (int i = 0; i < max_locals; ++i)
837 locals[i] = unsuitable_type;
838 local_changed[i] = false;
840 next = INVALID;
841 subroutine = 0;
844 state (const state *orig, int max_stack, int max_locals,
845 bool ret_semantics = false)
847 stack = new type[max_stack];
848 locals = new type[max_locals];
849 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
850 copy (orig, max_stack, max_locals, ret_semantics);
851 next = INVALID;
854 ~state ()
856 if (stack)
857 delete[] stack;
858 if (locals)
859 delete[] locals;
860 if (local_changed)
861 _Jv_Free (local_changed);
864 void *operator new[] (size_t bytes)
866 return _Jv_Malloc (bytes);
869 void operator delete[] (void *mem)
871 _Jv_Free (mem);
874 void *operator new (size_t bytes)
876 return _Jv_Malloc (bytes);
879 void operator delete (void *mem)
881 _Jv_Free (mem);
884 void copy (const state *copy, int max_stack, int max_locals,
885 bool ret_semantics = false)
887 stacktop = copy->stacktop;
888 stackdepth = copy->stackdepth;
889 subroutine = copy->subroutine;
890 for (int i = 0; i < max_stack; ++i)
891 stack[i] = copy->stack[i];
892 for (int i = 0; i < max_locals; ++i)
894 // See push_jump_merge to understand this case.
895 if (ret_semantics)
896 locals[i] = type (copy->local_changed[i]
897 ? unsuitable_type
898 : unused_by_subroutine_type);
899 else
900 locals[i] = copy->locals[i];
901 local_changed[i] = copy->local_changed[i];
903 this_type = copy->this_type;
904 // Don't modify `next'.
907 // Modify this state to reflect entry to an exception handler.
908 void set_exception (type t, int max_stack)
910 stackdepth = 1;
911 stacktop = 1;
912 stack[0] = t;
913 for (int i = stacktop; i < max_stack; ++i)
914 stack[i] = unsuitable_type;
916 // FIXME: subroutine handling?
919 // Modify this state to reflect entry into a subroutine.
920 void enter_subroutine (int npc, int max_locals)
922 subroutine = npc;
923 // Mark all items as unchanged. Each subroutine needs to keep
924 // track of its `changed' state independently. In the case of
925 // nested subroutines, this information will be merged back into
926 // parent by the `ret'.
927 for (int i = 0; i < max_locals; ++i)
928 local_changed[i] = false;
931 // Merge STATE_OLD into this state. Destructively modifies this
932 // state. Returns true if the new state was in fact changed.
933 // Will throw an exception if the states are not mergeable.
934 bool merge (state *state_old, bool ret_semantics,
935 int max_locals, _Jv_BytecodeVerifier *verifier)
937 bool changed = false;
939 // Special handling for `this'. If one or the other is
940 // uninitialized, then the merge is uninitialized.
941 if (this_type.isinitialized ())
942 this_type = state_old->this_type;
944 // Merge subroutine states. Here we just keep track of what
945 // subroutine we think we're in. We only check for a merge
946 // (which is invalid) when we see a `ret'.
947 if (subroutine == state_old->subroutine)
949 // Nothing.
951 else if (subroutine == 0)
953 subroutine = state_old->subroutine;
954 changed = true;
956 else
958 // If the subroutines differ, indicate that the state
959 // changed. This is needed to detect when subroutines have
960 // merged.
961 changed = true;
964 // Merge stacks.
965 if (state_old->stacktop != stacktop)
966 verifier->verify_fail ("stack sizes differ");
967 for (int i = 0; i < state_old->stacktop; ++i)
969 if (stack[i].merge (state_old->stack[i], false, verifier))
970 changed = true;
973 // Merge local variables.
974 for (int i = 0; i < max_locals; ++i)
976 // If we're not processing a `ret', then we merge every
977 // local variable. If we are processing a `ret', then we
978 // only merge locals which changed in the subroutine. When
979 // processing a `ret', STATE_OLD is the state at the point
980 // of the `ret', and THIS is the state just after the `jsr'.
981 if (! ret_semantics || state_old->local_changed[i])
983 if (locals[i].merge (state_old->locals[i], true, verifier))
985 changed = true;
986 note_variable (i);
990 // If we're in a subroutine, we must compute the union of
991 // all the changed local variables.
992 if (state_old->local_changed[i])
993 note_variable (i);
996 return changed;
999 // Throw an exception if there is an uninitialized object on the
1000 // stack or in a local variable. EXCEPTION_SEMANTICS controls
1001 // whether we're using backwards-branch or exception-handing
1002 // semantics.
1003 void check_no_uninitialized_objects (int max_locals,
1004 _Jv_BytecodeVerifier *verifier,
1005 bool exception_semantics = false)
1007 if (! exception_semantics)
1009 for (int i = 0; i < stacktop; ++i)
1010 if (stack[i].isreference () && ! stack[i].isinitialized ())
1011 verifier->verify_fail ("uninitialized object on stack");
1014 for (int i = 0; i < max_locals; ++i)
1015 if (locals[i].isreference () && ! locals[i].isinitialized ())
1016 verifier->verify_fail ("uninitialized object in local variable");
1018 check_this_initialized (verifier);
1021 // Ensure that `this' has been initialized.
1022 void check_this_initialized (_Jv_BytecodeVerifier *verifier)
1024 if (this_type.isreference () && ! this_type.isinitialized ())
1025 verifier->verify_fail ("`this' is uninitialized");
1028 // Set type of `this'.
1029 void set_this_type (const type &k)
1031 this_type = k;
1034 // Note that a local variable was modified.
1035 void note_variable (int index)
1037 if (subroutine > 0)
1038 local_changed[index] = true;
1041 // Mark each `new'd object we know of that was allocated at PC as
1042 // initialized.
1043 void set_initialized (int pc, int max_locals)
1045 for (int i = 0; i < stacktop; ++i)
1046 stack[i].set_initialized (pc);
1047 for (int i = 0; i < max_locals; ++i)
1048 locals[i].set_initialized (pc);
1049 this_type.set_initialized (pc);
1052 // Return true if this state is the unmerged result of a `ret'.
1053 bool is_unmerged_ret_state (int max_locals) const
1055 for (int i = 0; i < max_locals; ++i)
1057 if (locals[i].key == unused_by_subroutine_type)
1058 return true;
1060 return false;
1063 #ifdef VERIFY_DEBUG
1064 void print (const char *leader, int pc,
1065 int max_stack, int max_locals) const
1067 debug_print ("%s [%4d]: [stack] ", leader, pc);
1068 int i;
1069 for (i = 0; i < stacktop; ++i)
1070 stack[i].print ();
1071 for (; i < max_stack; ++i)
1072 debug_print (".");
1073 debug_print (" [local] ");
1074 for (i = 0; i < max_locals; ++i)
1075 locals[i].print ();
1076 if (subroutine == 0)
1077 debug_print (" | None");
1078 else
1079 debug_print (" | %4d", subroutine);
1080 debug_print (" | %p\n", this);
1082 #else
1083 inline void print (const char *, int, int, int) const
1086 #endif /* VERIFY_DEBUG */
1089 type pop_raw ()
1091 if (current_state->stacktop <= 0)
1092 verify_fail ("stack empty");
1093 type r = current_state->stack[--current_state->stacktop];
1094 current_state->stackdepth -= r.depth ();
1095 if (current_state->stackdepth < 0)
1096 verify_fail ("stack empty", start_PC);
1097 return r;
1100 type pop32 ()
1102 type r = pop_raw ();
1103 if (r.iswide ())
1104 verify_fail ("narrow pop of wide type");
1105 return r;
1108 type pop64 ()
1110 type r = pop_raw ();
1111 if (! r.iswide ())
1112 verify_fail ("wide pop of narrow type");
1113 return r;
1116 type pop_type (type match)
1118 match.promote ();
1119 type t = pop_raw ();
1120 if (! match.compatible (t, this))
1121 verify_fail ("incompatible type on stack");
1122 return t;
1125 // Pop a reference type or a return address.
1126 type pop_ref_or_return ()
1128 type t = pop_raw ();
1129 if (! t.isreference () && t.key != return_address_type)
1130 verify_fail ("expected reference or return address on stack");
1131 return t;
1134 void push_type (type t)
1136 // If T is a numeric type like short, promote it to int.
1137 t.promote ();
1139 int depth = t.depth ();
1140 if (current_state->stackdepth + depth > current_method->max_stack)
1141 verify_fail ("stack overflow");
1142 current_state->stack[current_state->stacktop++] = t;
1143 current_state->stackdepth += depth;
1146 void set_variable (int index, type t)
1148 // If T is a numeric type like short, promote it to int.
1149 t.promote ();
1151 int depth = t.depth ();
1152 if (index > current_method->max_locals - depth)
1153 verify_fail ("invalid local variable");
1154 current_state->locals[index] = t;
1155 current_state->note_variable (index);
1157 if (depth == 2)
1159 current_state->locals[index + 1] = continuation_type;
1160 current_state->note_variable (index + 1);
1162 if (index > 0 && current_state->locals[index - 1].iswide ())
1164 current_state->locals[index - 1] = unsuitable_type;
1165 // There's no need to call note_variable here.
1169 type get_variable (int index, type t)
1171 int depth = t.depth ();
1172 if (index > current_method->max_locals - depth)
1173 verify_fail ("invalid local variable");
1174 if (! t.compatible (current_state->locals[index], this))
1175 verify_fail ("incompatible type in local variable");
1176 if (depth == 2)
1178 type t (continuation_type);
1179 if (! current_state->locals[index + 1].compatible (t, this))
1180 verify_fail ("invalid local variable");
1182 return current_state->locals[index];
1185 // Make sure ARRAY is an array type and that its elements are
1186 // compatible with type ELEMENT. Returns the actual element type.
1187 type require_array_type (type array, type element)
1189 // An odd case. Here we just pretend that everything went ok. If
1190 // the requested element type is some kind of reference, return
1191 // the null type instead.
1192 if (array.isnull ())
1193 return element.isreference () ? type (null_type) : element;
1195 if (! array.isarray ())
1196 verify_fail ("array required");
1198 type t = array.element_type (this);
1199 if (! element.compatible (t, this))
1201 // Special case for byte arrays, which must also be boolean
1202 // arrays.
1203 bool ok = true;
1204 if (element.key == byte_type)
1206 type e2 (boolean_type);
1207 ok = e2.compatible (t, this);
1209 if (! ok)
1210 verify_fail ("incompatible array element type");
1213 // Return T and not ELEMENT, because T might be specialized.
1214 return t;
1217 jint get_byte ()
1219 if (PC >= current_method->code_length)
1220 verify_fail ("premature end of bytecode");
1221 return (jint) bytecode[PC++] & 0xff;
1224 jint get_ushort ()
1226 jint b1 = get_byte ();
1227 jint b2 = get_byte ();
1228 return (jint) ((b1 << 8) | b2) & 0xffff;
1231 jint get_short ()
1233 jint b1 = get_byte ();
1234 jint b2 = get_byte ();
1235 jshort s = (b1 << 8) | b2;
1236 return (jint) s;
1239 jint get_int ()
1241 jint b1 = get_byte ();
1242 jint b2 = get_byte ();
1243 jint b3 = get_byte ();
1244 jint b4 = get_byte ();
1245 return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
1248 int compute_jump (int offset)
1250 int npc = start_PC + offset;
1251 if (npc < 0 || npc >= current_method->code_length)
1252 verify_fail ("branch out of range", start_PC);
1253 return npc;
1256 // Merge the indicated state into the state at the branch target and
1257 // schedule a new PC if there is a change. If RET_SEMANTICS is
1258 // true, then we are merging from a `ret' instruction into the
1259 // instruction after a `jsr'. This is a special case with its own
1260 // modified semantics.
1261 void push_jump_merge (int npc, state *nstate, bool ret_semantics = false)
1263 bool changed = true;
1264 if (states[npc] == NULL)
1266 // There's a weird situation here. If are examining the
1267 // branch that results from a `ret', and there is not yet a
1268 // state available at the branch target (the instruction just
1269 // after the `jsr'), then we have to construct a special kind
1270 // of state at that point for future merging. This special
1271 // state has the type `unused_by_subroutine_type' in each slot
1272 // which was not modified by the subroutine.
1273 states[npc] = new state (nstate, current_method->max_stack,
1274 current_method->max_locals, ret_semantics);
1275 debug_print ("== New state in push_jump_merge\n");
1276 states[npc]->print ("New", npc, current_method->max_stack,
1277 current_method->max_locals);
1279 else
1281 debug_print ("== Merge states in push_jump_merge\n");
1282 nstate->print ("Frm", start_PC, current_method->max_stack,
1283 current_method->max_locals);
1284 states[npc]->print (" To", npc, current_method->max_stack,
1285 current_method->max_locals);
1286 changed = states[npc]->merge (nstate, ret_semantics,
1287 current_method->max_locals, this);
1288 states[npc]->print ("New", npc, current_method->max_stack,
1289 current_method->max_locals);
1292 if (changed && states[npc]->next == state::INVALID)
1294 // The merge changed the state, and the new PC isn't yet on our
1295 // list of PCs to re-verify.
1296 states[npc]->next = next_verify_pc;
1297 next_verify_pc = npc;
1301 void push_jump (int offset)
1303 int npc = compute_jump (offset);
1304 if (npc < PC)
1305 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1306 push_jump_merge (npc, current_state);
1309 void push_exception_jump (type t, int pc)
1311 current_state->check_no_uninitialized_objects (current_method->max_locals,
1312 this, true);
1313 state s (current_state, current_method->max_stack,
1314 current_method->max_locals);
1315 if (current_method->max_stack < 1)
1316 verify_fail ("stack overflow at exception handler");
1317 s.set_exception (t, current_method->max_stack);
1318 push_jump_merge (pc, &s);
1321 int pop_jump ()
1323 int *prev_loc = &next_verify_pc;
1324 int npc = next_verify_pc;
1325 bool skipped = false;
1327 while (npc != state::NO_NEXT)
1329 // If the next available PC is an unmerged `ret' state, then
1330 // we aren't yet ready to handle it. That's because we would
1331 // need all kind of special cases to do so. So instead we
1332 // defer this jump until after we've processed it via a
1333 // fall-through. This has to happen because the instruction
1334 // before this one must be a `jsr'.
1335 if (! states[npc]->is_unmerged_ret_state (current_method->max_locals))
1337 *prev_loc = states[npc]->next;
1338 states[npc]->next = state::INVALID;
1339 return npc;
1342 skipped = true;
1343 prev_loc = &states[npc]->next;
1344 npc = states[npc]->next;
1347 // If we've skipped states and there is nothing else, that's a
1348 // bug.
1349 if (skipped)
1350 verify_fail ("pop_jump: can't happen");
1351 return state::NO_NEXT;
1354 void invalidate_pc ()
1356 PC = state::NO_NEXT;
1359 void note_branch_target (int pc, bool is_jsr_target = false)
1361 // Don't check `pc <= PC', because we've advanced PC after
1362 // fetching the target and we haven't yet checked the next
1363 // instruction.
1364 if (pc < PC && ! (flags[pc] & FLAG_INSN_START))
1365 verify_fail ("branch not to instruction start", start_PC);
1366 flags[pc] |= FLAG_BRANCH_TARGET;
1367 if (is_jsr_target)
1369 // Record the jsr which called this instruction.
1370 subr_info *info = (subr_info *) _Jv_Malloc (sizeof (subr_info));
1371 info->pc = PC;
1372 info->next = jsr_ptrs[pc];
1373 jsr_ptrs[pc] = info;
1377 void skip_padding ()
1379 while ((PC % 4) > 0)
1380 if (get_byte () != 0)
1381 verify_fail ("found nonzero padding byte");
1384 // Return the subroutine to which the instruction at PC belongs.
1385 int get_subroutine (int pc)
1387 if (states[pc] == NULL)
1388 return 0;
1389 return states[pc]->subroutine;
1392 // Do the work for a `ret' instruction. INDEX is the index into the
1393 // local variables.
1394 void handle_ret_insn (int index)
1396 get_variable (index, return_address_type);
1398 int csub = current_state->subroutine;
1399 if (csub == 0)
1400 verify_fail ("no subroutine");
1402 // Check to see if we've merged subroutines.
1403 subr_entry_info *entry;
1404 for (entry = entry_points; entry != NULL; entry = entry->next)
1406 if (entry->ret_pc == start_PC)
1407 break;
1409 if (entry == NULL)
1411 entry = (subr_entry_info *) _Jv_Malloc (sizeof (subr_entry_info));
1412 entry->pc = csub;
1413 entry->ret_pc = start_PC;
1414 entry->next = entry_points;
1415 entry_points = entry;
1417 else if (entry->pc != csub)
1418 verify_fail ("subroutines merged");
1420 for (subr_info *subr = jsr_ptrs[csub]; subr != NULL; subr = subr->next)
1422 // Temporarily modify the current state so it looks like we're
1423 // in the enclosing context.
1424 current_state->subroutine = get_subroutine (subr->pc);
1425 if (subr->pc < PC)
1426 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1427 push_jump_merge (subr->pc, current_state, true);
1430 current_state->subroutine = csub;
1431 invalidate_pc ();
1434 // We're in the subroutine SUB, calling a subroutine at DEST. Make
1435 // sure this subroutine isn't already on the stack.
1436 void check_nonrecursive_call (int sub, int dest)
1438 if (sub == 0)
1439 return;
1440 if (sub == dest)
1441 verify_fail ("recursive subroutine call");
1442 for (subr_info *info = jsr_ptrs[sub]; info != NULL; info = info->next)
1443 check_nonrecursive_call (get_subroutine (info->pc), dest);
1446 void handle_jsr_insn (int offset)
1448 int npc = compute_jump (offset);
1450 if (npc < PC)
1451 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1452 check_nonrecursive_call (current_state->subroutine, npc);
1454 // Create a new state and modify it as appropriate for entry into
1455 // a subroutine. We're writing this in a weird way because,
1456 // unfortunately, push_type only works on the current state.
1457 push_type (return_address_type);
1458 push_jump_merge (npc, current_state);
1459 // Clean up the weirdness.
1460 pop_type (return_address_type);
1462 // On entry to the subroutine, the subroutine number must be set
1463 // and the locals must be marked as cleared. We do this after
1464 // merging state so that we don't erroneously "notice" a variable
1465 // change merely on entry.
1466 states[npc]->enter_subroutine (npc, current_method->max_locals);
1469 jclass construct_primitive_array_type (type_val prim)
1471 jclass k = NULL;
1472 switch (prim)
1474 case boolean_type:
1475 k = JvPrimClass (boolean);
1476 break;
1477 case char_type:
1478 k = JvPrimClass (char);
1479 break;
1480 case float_type:
1481 k = JvPrimClass (float);
1482 break;
1483 case double_type:
1484 k = JvPrimClass (double);
1485 break;
1486 case byte_type:
1487 k = JvPrimClass (byte);
1488 break;
1489 case short_type:
1490 k = JvPrimClass (short);
1491 break;
1492 case int_type:
1493 k = JvPrimClass (int);
1494 break;
1495 case long_type:
1496 k = JvPrimClass (long);
1497 break;
1498 default:
1499 verify_fail ("unknown type in construct_primitive_array_type");
1501 k = _Jv_GetArrayClass (k, NULL);
1502 return k;
1505 // This pass computes the location of branch targets and also
1506 // instruction starts.
1507 void branch_prepass ()
1509 flags = (char *) _Jv_Malloc (current_method->code_length);
1510 jsr_ptrs = (subr_info **) _Jv_Malloc (sizeof (subr_info *)
1511 * current_method->code_length);
1513 for (int i = 0; i < current_method->code_length; ++i)
1515 flags[i] = 0;
1516 jsr_ptrs[i] = NULL;
1519 bool last_was_jsr = false;
1521 PC = 0;
1522 while (PC < current_method->code_length)
1524 // Set `start_PC' early so that error checking can have the
1525 // correct value.
1526 start_PC = PC;
1527 flags[PC] |= FLAG_INSN_START;
1529 // If the previous instruction was a jsr, then the next
1530 // instruction is a branch target -- the branch being the
1531 // corresponding `ret'.
1532 if (last_was_jsr)
1533 note_branch_target (PC);
1534 last_was_jsr = false;
1536 java_opcode opcode = (java_opcode) bytecode[PC++];
1537 switch (opcode)
1539 case op_nop:
1540 case op_aconst_null:
1541 case op_iconst_m1:
1542 case op_iconst_0:
1543 case op_iconst_1:
1544 case op_iconst_2:
1545 case op_iconst_3:
1546 case op_iconst_4:
1547 case op_iconst_5:
1548 case op_lconst_0:
1549 case op_lconst_1:
1550 case op_fconst_0:
1551 case op_fconst_1:
1552 case op_fconst_2:
1553 case op_dconst_0:
1554 case op_dconst_1:
1555 case op_iload_0:
1556 case op_iload_1:
1557 case op_iload_2:
1558 case op_iload_3:
1559 case op_lload_0:
1560 case op_lload_1:
1561 case op_lload_2:
1562 case op_lload_3:
1563 case op_fload_0:
1564 case op_fload_1:
1565 case op_fload_2:
1566 case op_fload_3:
1567 case op_dload_0:
1568 case op_dload_1:
1569 case op_dload_2:
1570 case op_dload_3:
1571 case op_aload_0:
1572 case op_aload_1:
1573 case op_aload_2:
1574 case op_aload_3:
1575 case op_iaload:
1576 case op_laload:
1577 case op_faload:
1578 case op_daload:
1579 case op_aaload:
1580 case op_baload:
1581 case op_caload:
1582 case op_saload:
1583 case op_istore_0:
1584 case op_istore_1:
1585 case op_istore_2:
1586 case op_istore_3:
1587 case op_lstore_0:
1588 case op_lstore_1:
1589 case op_lstore_2:
1590 case op_lstore_3:
1591 case op_fstore_0:
1592 case op_fstore_1:
1593 case op_fstore_2:
1594 case op_fstore_3:
1595 case op_dstore_0:
1596 case op_dstore_1:
1597 case op_dstore_2:
1598 case op_dstore_3:
1599 case op_astore_0:
1600 case op_astore_1:
1601 case op_astore_2:
1602 case op_astore_3:
1603 case op_iastore:
1604 case op_lastore:
1605 case op_fastore:
1606 case op_dastore:
1607 case op_aastore:
1608 case op_bastore:
1609 case op_castore:
1610 case op_sastore:
1611 case op_pop:
1612 case op_pop2:
1613 case op_dup:
1614 case op_dup_x1:
1615 case op_dup_x2:
1616 case op_dup2:
1617 case op_dup2_x1:
1618 case op_dup2_x2:
1619 case op_swap:
1620 case op_iadd:
1621 case op_isub:
1622 case op_imul:
1623 case op_idiv:
1624 case op_irem:
1625 case op_ishl:
1626 case op_ishr:
1627 case op_iushr:
1628 case op_iand:
1629 case op_ior:
1630 case op_ixor:
1631 case op_ladd:
1632 case op_lsub:
1633 case op_lmul:
1634 case op_ldiv:
1635 case op_lrem:
1636 case op_lshl:
1637 case op_lshr:
1638 case op_lushr:
1639 case op_land:
1640 case op_lor:
1641 case op_lxor:
1642 case op_fadd:
1643 case op_fsub:
1644 case op_fmul:
1645 case op_fdiv:
1646 case op_frem:
1647 case op_dadd:
1648 case op_dsub:
1649 case op_dmul:
1650 case op_ddiv:
1651 case op_drem:
1652 case op_ineg:
1653 case op_i2b:
1654 case op_i2c:
1655 case op_i2s:
1656 case op_lneg:
1657 case op_fneg:
1658 case op_dneg:
1659 case op_i2l:
1660 case op_i2f:
1661 case op_i2d:
1662 case op_l2i:
1663 case op_l2f:
1664 case op_l2d:
1665 case op_f2i:
1666 case op_f2l:
1667 case op_f2d:
1668 case op_d2i:
1669 case op_d2l:
1670 case op_d2f:
1671 case op_lcmp:
1672 case op_fcmpl:
1673 case op_fcmpg:
1674 case op_dcmpl:
1675 case op_dcmpg:
1676 case op_monitorenter:
1677 case op_monitorexit:
1678 case op_ireturn:
1679 case op_lreturn:
1680 case op_freturn:
1681 case op_dreturn:
1682 case op_areturn:
1683 case op_return:
1684 case op_athrow:
1685 case op_arraylength:
1686 break;
1688 case op_bipush:
1689 case op_ldc:
1690 case op_iload:
1691 case op_lload:
1692 case op_fload:
1693 case op_dload:
1694 case op_aload:
1695 case op_istore:
1696 case op_lstore:
1697 case op_fstore:
1698 case op_dstore:
1699 case op_astore:
1700 case op_ret:
1701 case op_newarray:
1702 get_byte ();
1703 break;
1705 case op_iinc:
1706 case op_sipush:
1707 case op_ldc_w:
1708 case op_ldc2_w:
1709 case op_getstatic:
1710 case op_getfield:
1711 case op_putfield:
1712 case op_putstatic:
1713 case op_new:
1714 case op_anewarray:
1715 case op_instanceof:
1716 case op_checkcast:
1717 case op_invokespecial:
1718 case op_invokestatic:
1719 case op_invokevirtual:
1720 get_short ();
1721 break;
1723 case op_multianewarray:
1724 get_short ();
1725 get_byte ();
1726 break;
1728 case op_jsr:
1729 last_was_jsr = true;
1730 // Fall through.
1731 case op_ifeq:
1732 case op_ifne:
1733 case op_iflt:
1734 case op_ifge:
1735 case op_ifgt:
1736 case op_ifle:
1737 case op_if_icmpeq:
1738 case op_if_icmpne:
1739 case op_if_icmplt:
1740 case op_if_icmpge:
1741 case op_if_icmpgt:
1742 case op_if_icmple:
1743 case op_if_acmpeq:
1744 case op_if_acmpne:
1745 case op_ifnull:
1746 case op_ifnonnull:
1747 case op_goto:
1748 note_branch_target (compute_jump (get_short ()), last_was_jsr);
1749 break;
1751 case op_tableswitch:
1753 skip_padding ();
1754 note_branch_target (compute_jump (get_int ()));
1755 jint low = get_int ();
1756 jint hi = get_int ();
1757 if (low > hi)
1758 verify_fail ("invalid tableswitch", start_PC);
1759 for (int i = low; i <= hi; ++i)
1760 note_branch_target (compute_jump (get_int ()));
1762 break;
1764 case op_lookupswitch:
1766 skip_padding ();
1767 note_branch_target (compute_jump (get_int ()));
1768 int npairs = get_int ();
1769 if (npairs < 0)
1770 verify_fail ("too few pairs in lookupswitch", start_PC);
1771 while (npairs-- > 0)
1773 get_int ();
1774 note_branch_target (compute_jump (get_int ()));
1777 break;
1779 case op_invokeinterface:
1780 get_short ();
1781 get_byte ();
1782 get_byte ();
1783 break;
1785 case op_wide:
1787 opcode = (java_opcode) get_byte ();
1788 get_short ();
1789 if (opcode == op_iinc)
1790 get_short ();
1792 break;
1794 case op_jsr_w:
1795 last_was_jsr = true;
1796 // Fall through.
1797 case op_goto_w:
1798 note_branch_target (compute_jump (get_int ()), last_was_jsr);
1799 break;
1801 default:
1802 verify_fail ("unrecognized instruction in branch_prepass",
1803 start_PC);
1806 // See if any previous branch tried to branch to the middle of
1807 // this instruction.
1808 for (int pc = start_PC + 1; pc < PC; ++pc)
1810 if ((flags[pc] & FLAG_BRANCH_TARGET))
1811 verify_fail ("branch to middle of instruction", pc);
1815 // Verify exception handlers.
1816 for (int i = 0; i < current_method->exc_count; ++i)
1818 if (! (flags[exception[i].handler_pc] & FLAG_INSN_START))
1819 verify_fail ("exception handler not at instruction start",
1820 exception[i].handler_pc);
1821 if (! (flags[exception[i].start_pc] & FLAG_INSN_START))
1822 verify_fail ("exception start not at instruction start",
1823 exception[i].start_pc);
1824 if (exception[i].end_pc != current_method->code_length
1825 && ! (flags[exception[i].end_pc] & FLAG_INSN_START))
1826 verify_fail ("exception end not at instruction start",
1827 exception[i].end_pc);
1829 flags[exception[i].handler_pc] |= FLAG_BRANCH_TARGET;
1833 void check_pool_index (int index)
1835 if (index < 0 || index >= current_class->constants.size)
1836 verify_fail ("constant pool index out of range", start_PC);
1839 type check_class_constant (int index)
1841 check_pool_index (index);
1842 _Jv_Constants *pool = &current_class->constants;
1843 if (pool->tags[index] == JV_CONSTANT_ResolvedClass)
1844 return type (pool->data[index].clazz);
1845 else if (pool->tags[index] == JV_CONSTANT_Class)
1846 return type (pool->data[index].utf8);
1847 verify_fail ("expected class constant", start_PC);
1850 type check_constant (int index)
1852 check_pool_index (index);
1853 _Jv_Constants *pool = &current_class->constants;
1854 if (pool->tags[index] == JV_CONSTANT_ResolvedString
1855 || pool->tags[index] == JV_CONSTANT_String)
1856 return type (&java::lang::String::class$);
1857 else if (pool->tags[index] == JV_CONSTANT_Integer)
1858 return type (int_type);
1859 else if (pool->tags[index] == JV_CONSTANT_Float)
1860 return type (float_type);
1861 verify_fail ("String, int, or float constant expected", start_PC);
1864 type check_wide_constant (int index)
1866 check_pool_index (index);
1867 _Jv_Constants *pool = &current_class->constants;
1868 if (pool->tags[index] == JV_CONSTANT_Long)
1869 return type (long_type);
1870 else if (pool->tags[index] == JV_CONSTANT_Double)
1871 return type (double_type);
1872 verify_fail ("long or double constant expected", start_PC);
1875 // Helper for both field and method. These are laid out the same in
1876 // the constant pool.
1877 type handle_field_or_method (int index, int expected,
1878 _Jv_Utf8Const **name,
1879 _Jv_Utf8Const **fmtype)
1881 check_pool_index (index);
1882 _Jv_Constants *pool = &current_class->constants;
1883 if (pool->tags[index] != expected)
1884 verify_fail ("didn't see expected constant", start_PC);
1885 // Once we know we have a Fieldref or Methodref we assume that it
1886 // is correctly laid out in the constant pool. I think the code
1887 // in defineclass.cc guarantees this.
1888 _Jv_ushort class_index, name_and_type_index;
1889 _Jv_loadIndexes (&pool->data[index],
1890 class_index,
1891 name_and_type_index);
1892 _Jv_ushort name_index, desc_index;
1893 _Jv_loadIndexes (&pool->data[name_and_type_index],
1894 name_index, desc_index);
1896 *name = pool->data[name_index].utf8;
1897 *fmtype = pool->data[desc_index].utf8;
1899 return check_class_constant (class_index);
1902 // Return field's type, compute class' type if requested.
1903 type check_field_constant (int index, type *class_type = NULL)
1905 _Jv_Utf8Const *name, *field_type;
1906 type ct = handle_field_or_method (index,
1907 JV_CONSTANT_Fieldref,
1908 &name, &field_type);
1909 if (class_type)
1910 *class_type = ct;
1911 if (field_type->data[0] == '[' || field_type->data[0] == 'L')
1912 return type (field_type);
1913 return get_type_val_for_signature (field_type->data[0]);
1916 type check_method_constant (int index, bool is_interface,
1917 _Jv_Utf8Const **method_name,
1918 _Jv_Utf8Const **method_signature)
1920 return handle_field_or_method (index,
1921 (is_interface
1922 ? JV_CONSTANT_InterfaceMethodref
1923 : JV_CONSTANT_Methodref),
1924 method_name, method_signature);
1927 type get_one_type (char *&p)
1929 char *start = p;
1931 int arraycount = 0;
1932 while (*p == '[')
1934 ++arraycount;
1935 ++p;
1938 char v = *p++;
1940 if (v == 'L')
1942 while (*p != ';')
1943 ++p;
1944 ++p;
1945 _Jv_Utf8Const *name = make_utf8_const (start, p - start);
1946 return type (name);
1949 // Casting to jchar here is ok since we are looking at an ASCII
1950 // character.
1951 type_val rt = get_type_val_for_signature (jchar (v));
1953 if (arraycount == 0)
1955 // Callers of this function eventually push their arguments on
1956 // the stack. So, promote them here.
1957 return type (rt).promote ();
1960 jclass k = construct_primitive_array_type (rt);
1961 while (--arraycount > 0)
1962 k = _Jv_GetArrayClass (k, NULL);
1963 return type (k);
1966 void compute_argument_types (_Jv_Utf8Const *signature,
1967 type *types)
1969 char *p = signature->data;
1970 // Skip `('.
1971 ++p;
1973 int i = 0;
1974 while (*p != ')')
1975 types[i++] = get_one_type (p);
1978 type compute_return_type (_Jv_Utf8Const *signature)
1980 char *p = signature->data;
1981 while (*p != ')')
1982 ++p;
1983 ++p;
1984 return get_one_type (p);
1987 void check_return_type (type onstack)
1989 type rt = compute_return_type (current_method->self->signature);
1990 if (! rt.compatible (onstack, this))
1991 verify_fail ("incompatible return type");
1994 // Initialize the stack for the new method. Returns true if this
1995 // method is an instance initializer.
1996 bool initialize_stack ()
1998 int var = 0;
1999 bool is_init = false;
2001 using namespace java::lang::reflect;
2002 if (! Modifier::isStatic (current_method->self->accflags))
2004 type kurr (current_class);
2005 if (_Jv_equalUtf8Consts (current_method->self->name, gcj::init_name))
2007 kurr.set_uninitialized (type::SELF, this);
2008 is_init = true;
2010 set_variable (0, kurr);
2011 current_state->set_this_type (kurr);
2012 ++var;
2015 // We have to handle wide arguments specially here.
2016 int arg_count = _Jv_count_arguments (current_method->self->signature);
2017 type arg_types[arg_count];
2018 compute_argument_types (current_method->self->signature, arg_types);
2019 for (int i = 0; i < arg_count; ++i)
2021 set_variable (var, arg_types[i]);
2022 ++var;
2023 if (arg_types[i].iswide ())
2024 ++var;
2027 return is_init;
2030 void verify_instructions_0 ()
2032 current_state = new state (current_method->max_stack,
2033 current_method->max_locals);
2035 PC = 0;
2036 start_PC = 0;
2038 // True if we are verifying an instance initializer.
2039 bool this_is_init = initialize_stack ();
2041 states = (state **) _Jv_Malloc (sizeof (state *)
2042 * current_method->code_length);
2043 for (int i = 0; i < current_method->code_length; ++i)
2044 states[i] = NULL;
2046 next_verify_pc = state::NO_NEXT;
2048 while (true)
2050 // If the PC was invalidated, get a new one from the work list.
2051 if (PC == state::NO_NEXT)
2053 PC = pop_jump ();
2054 if (PC == state::INVALID)
2055 verify_fail ("can't happen: saw state::INVALID");
2056 if (PC == state::NO_NEXT)
2057 break;
2058 // Set up the current state.
2059 current_state->copy (states[PC], current_method->max_stack,
2060 current_method->max_locals);
2062 else
2064 // Control can't fall off the end of the bytecode. We
2065 // only need to check this in the fall-through case,
2066 // because branch bounds are checked when they are
2067 // pushed.
2068 if (PC >= current_method->code_length)
2069 verify_fail ("fell off end");
2071 // We only have to do this checking in the situation where
2072 // control flow falls through from the previous
2073 // instruction. Otherwise merging is done at the time we
2074 // push the branch.
2075 if (states[PC] != NULL)
2077 // We've already visited this instruction. So merge
2078 // the states together. If this yields no change then
2079 // we don't have to re-verify. However, if the new
2080 // state is an the result of an unmerged `ret', we
2081 // must continue through it.
2082 debug_print ("== Fall through merge\n");
2083 states[PC]->print ("Old", PC, current_method->max_stack,
2084 current_method->max_locals);
2085 current_state->print ("Cur", PC, current_method->max_stack,
2086 current_method->max_locals);
2087 if (! current_state->merge (states[PC], false,
2088 current_method->max_locals, this)
2089 && ! states[PC]->is_unmerged_ret_state (current_method->max_locals))
2091 debug_print ("== Fall through optimization\n");
2092 invalidate_pc ();
2093 continue;
2095 // Save a copy of it for later.
2096 states[PC]->copy (current_state, current_method->max_stack,
2097 current_method->max_locals);
2098 current_state->print ("New", PC, current_method->max_stack,
2099 current_method->max_locals);
2103 // We only have to keep saved state at branch targets. If
2104 // we're at a branch target and the state here hasn't been set
2105 // yet, we set it now.
2106 if (states[PC] == NULL && (flags[PC] & FLAG_BRANCH_TARGET))
2108 states[PC] = new state (current_state, current_method->max_stack,
2109 current_method->max_locals);
2112 // Set this before handling exceptions so that debug output is
2113 // sane.
2114 start_PC = PC;
2116 // Update states for all active exception handlers. Ordinarily
2117 // there are not many exception handlers. So we simply run
2118 // through them all.
2119 for (int i = 0; i < current_method->exc_count; ++i)
2121 if (PC >= exception[i].start_pc && PC < exception[i].end_pc)
2123 type handler (&java::lang::Throwable::class$);
2124 if (exception[i].handler_type != 0)
2125 handler = check_class_constant (exception[i].handler_type);
2126 push_exception_jump (handler, exception[i].handler_pc);
2130 current_state->print (" ", PC, current_method->max_stack,
2131 current_method->max_locals);
2132 java_opcode opcode = (java_opcode) bytecode[PC++];
2133 switch (opcode)
2135 case op_nop:
2136 break;
2138 case op_aconst_null:
2139 push_type (null_type);
2140 break;
2142 case op_iconst_m1:
2143 case op_iconst_0:
2144 case op_iconst_1:
2145 case op_iconst_2:
2146 case op_iconst_3:
2147 case op_iconst_4:
2148 case op_iconst_5:
2149 push_type (int_type);
2150 break;
2152 case op_lconst_0:
2153 case op_lconst_1:
2154 push_type (long_type);
2155 break;
2157 case op_fconst_0:
2158 case op_fconst_1:
2159 case op_fconst_2:
2160 push_type (float_type);
2161 break;
2163 case op_dconst_0:
2164 case op_dconst_1:
2165 push_type (double_type);
2166 break;
2168 case op_bipush:
2169 get_byte ();
2170 push_type (int_type);
2171 break;
2173 case op_sipush:
2174 get_short ();
2175 push_type (int_type);
2176 break;
2178 case op_ldc:
2179 push_type (check_constant (get_byte ()));
2180 break;
2181 case op_ldc_w:
2182 push_type (check_constant (get_ushort ()));
2183 break;
2184 case op_ldc2_w:
2185 push_type (check_wide_constant (get_ushort ()));
2186 break;
2188 case op_iload:
2189 push_type (get_variable (get_byte (), int_type));
2190 break;
2191 case op_lload:
2192 push_type (get_variable (get_byte (), long_type));
2193 break;
2194 case op_fload:
2195 push_type (get_variable (get_byte (), float_type));
2196 break;
2197 case op_dload:
2198 push_type (get_variable (get_byte (), double_type));
2199 break;
2200 case op_aload:
2201 push_type (get_variable (get_byte (), reference_type));
2202 break;
2204 case op_iload_0:
2205 case op_iload_1:
2206 case op_iload_2:
2207 case op_iload_3:
2208 push_type (get_variable (opcode - op_iload_0, int_type));
2209 break;
2210 case op_lload_0:
2211 case op_lload_1:
2212 case op_lload_2:
2213 case op_lload_3:
2214 push_type (get_variable (opcode - op_lload_0, long_type));
2215 break;
2216 case op_fload_0:
2217 case op_fload_1:
2218 case op_fload_2:
2219 case op_fload_3:
2220 push_type (get_variable (opcode - op_fload_0, float_type));
2221 break;
2222 case op_dload_0:
2223 case op_dload_1:
2224 case op_dload_2:
2225 case op_dload_3:
2226 push_type (get_variable (opcode - op_dload_0, double_type));
2227 break;
2228 case op_aload_0:
2229 case op_aload_1:
2230 case op_aload_2:
2231 case op_aload_3:
2232 push_type (get_variable (opcode - op_aload_0, reference_type));
2233 break;
2234 case op_iaload:
2235 pop_type (int_type);
2236 push_type (require_array_type (pop_type (reference_type),
2237 int_type));
2238 break;
2239 case op_laload:
2240 pop_type (int_type);
2241 push_type (require_array_type (pop_type (reference_type),
2242 long_type));
2243 break;
2244 case op_faload:
2245 pop_type (int_type);
2246 push_type (require_array_type (pop_type (reference_type),
2247 float_type));
2248 break;
2249 case op_daload:
2250 pop_type (int_type);
2251 push_type (require_array_type (pop_type (reference_type),
2252 double_type));
2253 break;
2254 case op_aaload:
2255 pop_type (int_type);
2256 push_type (require_array_type (pop_type (reference_type),
2257 reference_type));
2258 break;
2259 case op_baload:
2260 pop_type (int_type);
2261 require_array_type (pop_type (reference_type), byte_type);
2262 push_type (int_type);
2263 break;
2264 case op_caload:
2265 pop_type (int_type);
2266 require_array_type (pop_type (reference_type), char_type);
2267 push_type (int_type);
2268 break;
2269 case op_saload:
2270 pop_type (int_type);
2271 require_array_type (pop_type (reference_type), short_type);
2272 push_type (int_type);
2273 break;
2274 case op_istore:
2275 set_variable (get_byte (), pop_type (int_type));
2276 break;
2277 case op_lstore:
2278 set_variable (get_byte (), pop_type (long_type));
2279 break;
2280 case op_fstore:
2281 set_variable (get_byte (), pop_type (float_type));
2282 break;
2283 case op_dstore:
2284 set_variable (get_byte (), pop_type (double_type));
2285 break;
2286 case op_astore:
2287 set_variable (get_byte (), pop_ref_or_return ());
2288 break;
2289 case op_istore_0:
2290 case op_istore_1:
2291 case op_istore_2:
2292 case op_istore_3:
2293 set_variable (opcode - op_istore_0, pop_type (int_type));
2294 break;
2295 case op_lstore_0:
2296 case op_lstore_1:
2297 case op_lstore_2:
2298 case op_lstore_3:
2299 set_variable (opcode - op_lstore_0, pop_type (long_type));
2300 break;
2301 case op_fstore_0:
2302 case op_fstore_1:
2303 case op_fstore_2:
2304 case op_fstore_3:
2305 set_variable (opcode - op_fstore_0, pop_type (float_type));
2306 break;
2307 case op_dstore_0:
2308 case op_dstore_1:
2309 case op_dstore_2:
2310 case op_dstore_3:
2311 set_variable (opcode - op_dstore_0, pop_type (double_type));
2312 break;
2313 case op_astore_0:
2314 case op_astore_1:
2315 case op_astore_2:
2316 case op_astore_3:
2317 set_variable (opcode - op_astore_0, pop_ref_or_return ());
2318 break;
2319 case op_iastore:
2320 pop_type (int_type);
2321 pop_type (int_type);
2322 require_array_type (pop_type (reference_type), int_type);
2323 break;
2324 case op_lastore:
2325 pop_type (long_type);
2326 pop_type (int_type);
2327 require_array_type (pop_type (reference_type), long_type);
2328 break;
2329 case op_fastore:
2330 pop_type (float_type);
2331 pop_type (int_type);
2332 require_array_type (pop_type (reference_type), float_type);
2333 break;
2334 case op_dastore:
2335 pop_type (double_type);
2336 pop_type (int_type);
2337 require_array_type (pop_type (reference_type), double_type);
2338 break;
2339 case op_aastore:
2340 pop_type (reference_type);
2341 pop_type (int_type);
2342 require_array_type (pop_type (reference_type), reference_type);
2343 break;
2344 case op_bastore:
2345 pop_type (int_type);
2346 pop_type (int_type);
2347 require_array_type (pop_type (reference_type), byte_type);
2348 break;
2349 case op_castore:
2350 pop_type (int_type);
2351 pop_type (int_type);
2352 require_array_type (pop_type (reference_type), char_type);
2353 break;
2354 case op_sastore:
2355 pop_type (int_type);
2356 pop_type (int_type);
2357 require_array_type (pop_type (reference_type), short_type);
2358 break;
2359 case op_pop:
2360 pop32 ();
2361 break;
2362 case op_pop2:
2363 pop64 ();
2364 break;
2365 case op_dup:
2367 type t = pop32 ();
2368 push_type (t);
2369 push_type (t);
2371 break;
2372 case op_dup_x1:
2374 type t1 = pop32 ();
2375 type t2 = pop32 ();
2376 push_type (t1);
2377 push_type (t2);
2378 push_type (t1);
2380 break;
2381 case op_dup_x2:
2383 type t1 = pop32 ();
2384 type t2 = pop_raw ();
2385 if (! t2.iswide ())
2387 type t3 = pop32 ();
2388 push_type (t1);
2389 push_type (t3);
2391 else
2392 push_type (t1);
2393 push_type (t2);
2394 push_type (t1);
2396 break;
2397 case op_dup2:
2399 type t = pop_raw ();
2400 if (! t.iswide ())
2402 type t2 = pop32 ();
2403 push_type (t2);
2404 push_type (t);
2405 push_type (t2);
2407 else
2408 push_type (t);
2409 push_type (t);
2411 break;
2412 case op_dup2_x1:
2414 type t1 = pop_raw ();
2415 type t2 = pop32 ();
2416 if (! t1.iswide ())
2418 type t3 = pop32 ();
2419 push_type (t2);
2420 push_type (t1);
2421 push_type (t3);
2423 else
2424 push_type (t1);
2425 push_type (t2);
2426 push_type (t1);
2428 break;
2429 case op_dup2_x2:
2431 type t1 = pop_raw ();
2432 if (t1.iswide ())
2434 type t2 = pop_raw ();
2435 if (t2.iswide ())
2437 push_type (t1);
2438 push_type (t2);
2440 else
2442 type t3 = pop32 ();
2443 push_type (t1);
2444 push_type (t3);
2445 push_type (t2);
2447 push_type (t1);
2449 else
2451 type t2 = pop32 ();
2452 type t3 = pop_raw ();
2453 if (t3.iswide ())
2455 push_type (t2);
2456 push_type (t1);
2458 else
2460 type t4 = pop32 ();
2461 push_type (t2);
2462 push_type (t1);
2463 push_type (t4);
2465 push_type (t3);
2466 push_type (t2);
2467 push_type (t1);
2470 break;
2471 case op_swap:
2473 type t1 = pop32 ();
2474 type t2 = pop32 ();
2475 push_type (t1);
2476 push_type (t2);
2478 break;
2479 case op_iadd:
2480 case op_isub:
2481 case op_imul:
2482 case op_idiv:
2483 case op_irem:
2484 case op_ishl:
2485 case op_ishr:
2486 case op_iushr:
2487 case op_iand:
2488 case op_ior:
2489 case op_ixor:
2490 pop_type (int_type);
2491 push_type (pop_type (int_type));
2492 break;
2493 case op_ladd:
2494 case op_lsub:
2495 case op_lmul:
2496 case op_ldiv:
2497 case op_lrem:
2498 case op_land:
2499 case op_lor:
2500 case op_lxor:
2501 pop_type (long_type);
2502 push_type (pop_type (long_type));
2503 break;
2504 case op_lshl:
2505 case op_lshr:
2506 case op_lushr:
2507 pop_type (int_type);
2508 push_type (pop_type (long_type));
2509 break;
2510 case op_fadd:
2511 case op_fsub:
2512 case op_fmul:
2513 case op_fdiv:
2514 case op_frem:
2515 pop_type (float_type);
2516 push_type (pop_type (float_type));
2517 break;
2518 case op_dadd:
2519 case op_dsub:
2520 case op_dmul:
2521 case op_ddiv:
2522 case op_drem:
2523 pop_type (double_type);
2524 push_type (pop_type (double_type));
2525 break;
2526 case op_ineg:
2527 case op_i2b:
2528 case op_i2c:
2529 case op_i2s:
2530 push_type (pop_type (int_type));
2531 break;
2532 case op_lneg:
2533 push_type (pop_type (long_type));
2534 break;
2535 case op_fneg:
2536 push_type (pop_type (float_type));
2537 break;
2538 case op_dneg:
2539 push_type (pop_type (double_type));
2540 break;
2541 case op_iinc:
2542 get_variable (get_byte (), int_type);
2543 get_byte ();
2544 break;
2545 case op_i2l:
2546 pop_type (int_type);
2547 push_type (long_type);
2548 break;
2549 case op_i2f:
2550 pop_type (int_type);
2551 push_type (float_type);
2552 break;
2553 case op_i2d:
2554 pop_type (int_type);
2555 push_type (double_type);
2556 break;
2557 case op_l2i:
2558 pop_type (long_type);
2559 push_type (int_type);
2560 break;
2561 case op_l2f:
2562 pop_type (long_type);
2563 push_type (float_type);
2564 break;
2565 case op_l2d:
2566 pop_type (long_type);
2567 push_type (double_type);
2568 break;
2569 case op_f2i:
2570 pop_type (float_type);
2571 push_type (int_type);
2572 break;
2573 case op_f2l:
2574 pop_type (float_type);
2575 push_type (long_type);
2576 break;
2577 case op_f2d:
2578 pop_type (float_type);
2579 push_type (double_type);
2580 break;
2581 case op_d2i:
2582 pop_type (double_type);
2583 push_type (int_type);
2584 break;
2585 case op_d2l:
2586 pop_type (double_type);
2587 push_type (long_type);
2588 break;
2589 case op_d2f:
2590 pop_type (double_type);
2591 push_type (float_type);
2592 break;
2593 case op_lcmp:
2594 pop_type (long_type);
2595 pop_type (long_type);
2596 push_type (int_type);
2597 break;
2598 case op_fcmpl:
2599 case op_fcmpg:
2600 pop_type (float_type);
2601 pop_type (float_type);
2602 push_type (int_type);
2603 break;
2604 case op_dcmpl:
2605 case op_dcmpg:
2606 pop_type (double_type);
2607 pop_type (double_type);
2608 push_type (int_type);
2609 break;
2610 case op_ifeq:
2611 case op_ifne:
2612 case op_iflt:
2613 case op_ifge:
2614 case op_ifgt:
2615 case op_ifle:
2616 pop_type (int_type);
2617 push_jump (get_short ());
2618 break;
2619 case op_if_icmpeq:
2620 case op_if_icmpne:
2621 case op_if_icmplt:
2622 case op_if_icmpge:
2623 case op_if_icmpgt:
2624 case op_if_icmple:
2625 pop_type (int_type);
2626 pop_type (int_type);
2627 push_jump (get_short ());
2628 break;
2629 case op_if_acmpeq:
2630 case op_if_acmpne:
2631 pop_type (reference_type);
2632 pop_type (reference_type);
2633 push_jump (get_short ());
2634 break;
2635 case op_goto:
2636 push_jump (get_short ());
2637 invalidate_pc ();
2638 break;
2639 case op_jsr:
2640 handle_jsr_insn (get_short ());
2641 break;
2642 case op_ret:
2643 handle_ret_insn (get_byte ());
2644 break;
2645 case op_tableswitch:
2647 pop_type (int_type);
2648 skip_padding ();
2649 push_jump (get_int ());
2650 jint low = get_int ();
2651 jint high = get_int ();
2652 // Already checked LOW -vs- HIGH.
2653 for (int i = low; i <= high; ++i)
2654 push_jump (get_int ());
2655 invalidate_pc ();
2657 break;
2659 case op_lookupswitch:
2661 pop_type (int_type);
2662 skip_padding ();
2663 push_jump (get_int ());
2664 jint npairs = get_int ();
2665 // Already checked NPAIRS >= 0.
2666 jint lastkey = 0;
2667 for (int i = 0; i < npairs; ++i)
2669 jint key = get_int ();
2670 if (i > 0 && key <= lastkey)
2671 verify_fail ("lookupswitch pairs unsorted", start_PC);
2672 lastkey = key;
2673 push_jump (get_int ());
2675 invalidate_pc ();
2677 break;
2678 case op_ireturn:
2679 check_return_type (pop_type (int_type));
2680 invalidate_pc ();
2681 break;
2682 case op_lreturn:
2683 check_return_type (pop_type (long_type));
2684 invalidate_pc ();
2685 break;
2686 case op_freturn:
2687 check_return_type (pop_type (float_type));
2688 invalidate_pc ();
2689 break;
2690 case op_dreturn:
2691 check_return_type (pop_type (double_type));
2692 invalidate_pc ();
2693 break;
2694 case op_areturn:
2695 check_return_type (pop_type (reference_type));
2696 invalidate_pc ();
2697 break;
2698 case op_return:
2699 // We only need to check this when the return type is
2700 // void, because all instance initializers return void.
2701 if (this_is_init)
2702 current_state->check_this_initialized (this);
2703 check_return_type (void_type);
2704 invalidate_pc ();
2705 break;
2706 case op_getstatic:
2707 push_type (check_field_constant (get_ushort ()));
2708 break;
2709 case op_putstatic:
2710 pop_type (check_field_constant (get_ushort ()));
2711 break;
2712 case op_getfield:
2714 type klass;
2715 type field = check_field_constant (get_ushort (), &klass);
2716 pop_type (klass);
2717 push_type (field);
2719 break;
2720 case op_putfield:
2722 type klass;
2723 type field = check_field_constant (get_ushort (), &klass);
2724 pop_type (field);
2726 // We have an obscure special case here: we can use
2727 // `putfield' on a field declared in this class, even if
2728 // `this' has not yet been initialized.
2729 if (! current_state->this_type.isinitialized ()
2730 && current_state->this_type.pc == type::SELF)
2731 klass.set_uninitialized (type::SELF, this);
2732 pop_type (klass);
2734 break;
2736 case op_invokevirtual:
2737 case op_invokespecial:
2738 case op_invokestatic:
2739 case op_invokeinterface:
2741 _Jv_Utf8Const *method_name, *method_signature;
2742 type class_type
2743 = check_method_constant (get_ushort (),
2744 opcode == op_invokeinterface,
2745 &method_name,
2746 &method_signature);
2747 // NARGS is only used when we're processing
2748 // invokeinterface. It is simplest for us to compute it
2749 // here and then verify it later.
2750 int nargs = 0;
2751 if (opcode == op_invokeinterface)
2753 nargs = get_byte ();
2754 if (get_byte () != 0)
2755 verify_fail ("invokeinterface dummy byte is wrong");
2758 bool is_init = false;
2759 if (_Jv_equalUtf8Consts (method_name, gcj::init_name))
2761 is_init = true;
2762 if (opcode != op_invokespecial)
2763 verify_fail ("can't invoke <init>");
2765 else if (method_name->data[0] == '<')
2766 verify_fail ("can't invoke method starting with `<'");
2768 // Pop arguments and check types.
2769 int arg_count = _Jv_count_arguments (method_signature);
2770 type arg_types[arg_count];
2771 compute_argument_types (method_signature, arg_types);
2772 for (int i = arg_count - 1; i >= 0; --i)
2774 // This is only used for verifying the byte for
2775 // invokeinterface.
2776 nargs -= arg_types[i].depth ();
2777 pop_type (arg_types[i]);
2780 if (opcode == op_invokeinterface
2781 && nargs != 1)
2782 verify_fail ("wrong argument count for invokeinterface");
2784 if (opcode != op_invokestatic)
2786 type t = class_type;
2787 if (is_init)
2789 // In this case the PC doesn't matter.
2790 t.set_uninitialized (type::UNINIT, this);
2792 type raw = pop_raw ();
2793 bool ok = false;
2794 if (t.compatible (raw, this))
2796 ok = true;
2798 else if (opcode == op_invokeinterface)
2800 // This is a hack. We might have merged two
2801 // items and gotten `Object'. This can happen
2802 // because we don't keep track of where merges
2803 // come from. This is safe as long as the
2804 // interpreter checks interfaces at runtime.
2805 type obj (&java::lang::Object::class$);
2806 ok = raw.compatible (obj, this);
2809 if (! ok)
2810 verify_fail ("incompatible type on stack");
2812 if (is_init)
2813 current_state->set_initialized (raw.get_pc (),
2814 current_method->max_locals);
2817 type rt = compute_return_type (method_signature);
2818 if (! rt.isvoid ())
2819 push_type (rt);
2821 break;
2823 case op_new:
2825 type t = check_class_constant (get_ushort ());
2826 if (t.isarray () || t.isinterface (this) || t.isabstract (this))
2827 verify_fail ("type is array, interface, or abstract");
2828 t.set_uninitialized (start_PC, this);
2829 push_type (t);
2831 break;
2833 case op_newarray:
2835 int atype = get_byte ();
2836 // We intentionally have chosen constants to make this
2837 // valid.
2838 if (atype < boolean_type || atype > long_type)
2839 verify_fail ("type not primitive", start_PC);
2840 pop_type (int_type);
2841 push_type (construct_primitive_array_type (type_val (atype)));
2843 break;
2844 case op_anewarray:
2845 pop_type (int_type);
2846 push_type (check_class_constant (get_ushort ()).to_array (this));
2847 break;
2848 case op_arraylength:
2850 type t = pop_type (reference_type);
2851 if (! t.isarray () && ! t.isnull ())
2852 verify_fail ("array type expected");
2853 push_type (int_type);
2855 break;
2856 case op_athrow:
2857 pop_type (type (&java::lang::Throwable::class$));
2858 invalidate_pc ();
2859 break;
2860 case op_checkcast:
2861 pop_type (reference_type);
2862 push_type (check_class_constant (get_ushort ()));
2863 break;
2864 case op_instanceof:
2865 pop_type (reference_type);
2866 check_class_constant (get_ushort ());
2867 push_type (int_type);
2868 break;
2869 case op_monitorenter:
2870 pop_type (reference_type);
2871 break;
2872 case op_monitorexit:
2873 pop_type (reference_type);
2874 break;
2875 case op_wide:
2877 switch (get_byte ())
2879 case op_iload:
2880 push_type (get_variable (get_ushort (), int_type));
2881 break;
2882 case op_lload:
2883 push_type (get_variable (get_ushort (), long_type));
2884 break;
2885 case op_fload:
2886 push_type (get_variable (get_ushort (), float_type));
2887 break;
2888 case op_dload:
2889 push_type (get_variable (get_ushort (), double_type));
2890 break;
2891 case op_aload:
2892 push_type (get_variable (get_ushort (), reference_type));
2893 break;
2894 case op_istore:
2895 set_variable (get_ushort (), pop_type (int_type));
2896 break;
2897 case op_lstore:
2898 set_variable (get_ushort (), pop_type (long_type));
2899 break;
2900 case op_fstore:
2901 set_variable (get_ushort (), pop_type (float_type));
2902 break;
2903 case op_dstore:
2904 set_variable (get_ushort (), pop_type (double_type));
2905 break;
2906 case op_astore:
2907 set_variable (get_ushort (), pop_type (reference_type));
2908 break;
2909 case op_ret:
2910 handle_ret_insn (get_short ());
2911 break;
2912 case op_iinc:
2913 get_variable (get_ushort (), int_type);
2914 get_short ();
2915 break;
2916 default:
2917 verify_fail ("unrecognized wide instruction", start_PC);
2920 break;
2921 case op_multianewarray:
2923 type atype = check_class_constant (get_ushort ());
2924 int dim = get_byte ();
2925 if (dim < 1)
2926 verify_fail ("too few dimensions to multianewarray", start_PC);
2927 atype.verify_dimensions (dim, this);
2928 for (int i = 0; i < dim; ++i)
2929 pop_type (int_type);
2930 push_type (atype);
2932 break;
2933 case op_ifnull:
2934 case op_ifnonnull:
2935 pop_type (reference_type);
2936 push_jump (get_short ());
2937 break;
2938 case op_goto_w:
2939 push_jump (get_int ());
2940 invalidate_pc ();
2941 break;
2942 case op_jsr_w:
2943 handle_jsr_insn (get_int ());
2944 break;
2946 default:
2947 // Unrecognized opcode.
2948 verify_fail ("unrecognized instruction in verify_instructions_0",
2949 start_PC);
2954 __attribute__ ((__noreturn__)) void verify_fail (char *s, jint pc = -1)
2956 using namespace java::lang;
2957 StringBuffer *buf = new StringBuffer ();
2959 buf->append (JvNewStringLatin1 ("verification failed"));
2960 if (pc == -1)
2961 pc = start_PC;
2962 if (pc != -1)
2964 buf->append (JvNewStringLatin1 (" at PC "));
2965 buf->append (pc);
2968 _Jv_InterpMethod *method = current_method;
2969 buf->append (JvNewStringLatin1 (" in "));
2970 buf->append (current_class->getName());
2971 buf->append ((jchar) ':');
2972 buf->append (JvNewStringUTF (method->get_method()->name->data));
2973 buf->append ((jchar) '(');
2974 buf->append (JvNewStringUTF (method->get_method()->signature->data));
2975 buf->append ((jchar) ')');
2977 buf->append (JvNewStringLatin1 (": "));
2978 buf->append (JvNewStringLatin1 (s));
2979 throw new java::lang::VerifyError (buf->toString ());
2982 public:
2984 void verify_instructions ()
2986 branch_prepass ();
2987 verify_instructions_0 ();
2990 _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
2992 // We just print the text as utf-8. This is just for debugging
2993 // anyway.
2994 debug_print ("--------------------------------\n");
2995 debug_print ("-- Verifying method `%s'\n", m->self->name->data);
2997 current_method = m;
2998 bytecode = m->bytecode ();
2999 exception = m->exceptions ();
3000 current_class = m->defining_class;
3002 states = NULL;
3003 flags = NULL;
3004 jsr_ptrs = NULL;
3005 utf8_list = NULL;
3006 entry_points = NULL;
3009 ~_Jv_BytecodeVerifier ()
3011 if (states)
3012 _Jv_Free (states);
3013 if (flags)
3014 _Jv_Free (flags);
3016 if (jsr_ptrs)
3018 for (int i = 0; i < current_method->code_length; ++i)
3020 if (jsr_ptrs[i] != NULL)
3022 subr_info *info = jsr_ptrs[i];
3023 while (info != NULL)
3025 subr_info *next = info->next;
3026 _Jv_Free (info);
3027 info = next;
3031 _Jv_Free (jsr_ptrs);
3034 while (utf8_list != NULL)
3036 linked_utf8 *n = utf8_list->next;
3037 _Jv_Free (utf8_list->val);
3038 _Jv_Free (utf8_list);
3039 utf8_list = n;
3042 while (entry_points != NULL)
3044 subr_entry_info *next = entry_points->next;
3045 _Jv_Free (entry_points);
3046 entry_points = next;
3051 void
3052 _Jv_VerifyMethod (_Jv_InterpMethod *meth)
3054 _Jv_BytecodeVerifier v (meth);
3055 v.verify_instructions ();
3057 #endif /* INTERPRETER */