2003-09-04 Eric Christopher <echristo@redhat.com>
[official-gcc.git] / libjava / verify.cc
blobba408aa98bee90f669c9580c662b1bafb23726b0
1 // verify.cc - verify bytecode
3 /* Copyright (C) 2001, 2002, 2003 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;
61 struct ref_intersection;
63 // The current PC.
64 int PC;
65 // The PC corresponding to the start of the current instruction.
66 int start_PC;
68 // The current state of the stack, locals, etc.
69 state *current_state;
71 // We store the state at branch targets, for merging. This holds
72 // such states.
73 state **states;
75 // We keep a linked list of all the PCs which we must reverify.
76 // The link is done using the PC values. This is the head of the
77 // list.
78 int next_verify_pc;
80 // We keep some flags for each instruction. The values are the
81 // FLAG_* constants defined above.
82 char *flags;
84 // We need to keep track of which instructions can call a given
85 // subroutine. FIXME: this is inefficient. We keep a linked list
86 // of all calling `jsr's at at each jsr target.
87 subr_info **jsr_ptrs;
89 // We keep a linked list of entries which map each `ret' instruction
90 // to its unique subroutine entry point. We expect that there won't
91 // be many `ret' instructions, so a linked list is ok.
92 subr_entry_info *entry_points;
94 // The bytecode itself.
95 unsigned char *bytecode;
96 // The exceptions.
97 _Jv_InterpException *exception;
99 // Defining class.
100 jclass current_class;
101 // This method.
102 _Jv_InterpMethod *current_method;
104 // A linked list of utf8 objects we allocate. This is really ugly,
105 // but without this our utf8 objects would be collected.
106 linked_utf8 *utf8_list;
108 // A linked list of all ref_intersection objects we allocate.
109 ref_intersection *isect_list;
111 struct linked_utf8
113 _Jv_Utf8Const *val;
114 linked_utf8 *next;
117 _Jv_Utf8Const *make_utf8_const (char *s, int len)
119 _Jv_Utf8Const *val = _Jv_makeUtf8Const (s, len);
120 _Jv_Utf8Const *r = (_Jv_Utf8Const *) _Jv_Malloc (sizeof (_Jv_Utf8Const)
121 + val->length
122 + 1);
123 r->length = val->length;
124 r->hash = val->hash;
125 memcpy (r->data, val->data, val->length + 1);
127 linked_utf8 *lu = (linked_utf8 *) _Jv_Malloc (sizeof (linked_utf8));
128 lu->val = r;
129 lu->next = utf8_list;
130 utf8_list = lu;
132 return r;
135 __attribute__ ((__noreturn__)) void verify_fail (char *s, jint pc = -1)
137 using namespace java::lang;
138 StringBuffer *buf = new StringBuffer ();
140 buf->append (JvNewStringLatin1 ("verification failed"));
141 if (pc == -1)
142 pc = start_PC;
143 if (pc != -1)
145 buf->append (JvNewStringLatin1 (" at PC "));
146 buf->append (pc);
149 _Jv_InterpMethod *method = current_method;
150 buf->append (JvNewStringLatin1 (" in "));
151 buf->append (current_class->getName());
152 buf->append ((jchar) ':');
153 buf->append (JvNewStringUTF (method->get_method()->name->data));
154 buf->append ((jchar) '(');
155 buf->append (JvNewStringUTF (method->get_method()->signature->data));
156 buf->append ((jchar) ')');
158 buf->append (JvNewStringLatin1 (": "));
159 buf->append (JvNewStringLatin1 (s));
160 throw new java::lang::VerifyError (buf->toString ());
163 // This enum holds a list of tags for all the different types we
164 // need to handle. Reference types are treated specially by the
165 // type class.
166 enum type_val
168 void_type,
170 // The values for primitive types are chosen to correspond to values
171 // specified to newarray.
172 boolean_type = 4,
173 char_type = 5,
174 float_type = 6,
175 double_type = 7,
176 byte_type = 8,
177 short_type = 9,
178 int_type = 10,
179 long_type = 11,
181 // Used when overwriting second word of a double or long in the
182 // local variables. Also used after merging local variable states
183 // to indicate an unusable value.
184 unsuitable_type,
185 return_address_type,
186 continuation_type,
188 // There is an obscure special case which requires us to note when
189 // a local variable has not been used by a subroutine. See
190 // push_jump_merge for more information.
191 unused_by_subroutine_type,
193 // Everything after `reference_type' must be a reference type.
194 reference_type,
195 null_type,
196 uninitialized_reference_type
199 // This represents a merged class type. Some verifiers (including
200 // earlier versions of this one) will compute the intersection of
201 // two class types when merging states. However, this loses
202 // critical information about interfaces implemented by the various
203 // classes. So instead we keep track of all the actual classes that
204 // have been merged.
205 struct ref_intersection
207 // Whether or not this type has been resolved.
208 bool is_resolved;
210 // Actual type data.
211 union
213 // For a resolved reference type, this is a pointer to the class.
214 jclass klass;
215 // For other reference types, this it the name of the class.
216 _Jv_Utf8Const *name;
217 } data;
219 // Link to the next reference in the intersection.
220 ref_intersection *ref_next;
222 // This is used to keep track of all the allocated
223 // ref_intersection objects, so we can free them.
224 // FIXME: we should allocate these in chunks.
225 ref_intersection *alloc_next;
227 ref_intersection (jclass klass, _Jv_BytecodeVerifier *verifier)
228 : ref_next (NULL)
230 is_resolved = true;
231 data.klass = klass;
232 alloc_next = verifier->isect_list;
233 verifier->isect_list = this;
236 ref_intersection (_Jv_Utf8Const *name, _Jv_BytecodeVerifier *verifier)
237 : ref_next (NULL)
239 is_resolved = false;
240 data.name = name;
241 alloc_next = verifier->isect_list;
242 verifier->isect_list = this;
245 ref_intersection (ref_intersection *dup, ref_intersection *tail,
246 _Jv_BytecodeVerifier *verifier)
247 : ref_next (tail)
249 is_resolved = dup->is_resolved;
250 data = dup->data;
251 alloc_next = verifier->isect_list;
252 verifier->isect_list = this;
255 bool equals (ref_intersection *other, _Jv_BytecodeVerifier *verifier)
257 if (! is_resolved && ! other->is_resolved
258 && _Jv_equalUtf8Consts (data.name, other->data.name))
259 return true;
260 if (! is_resolved)
261 resolve (verifier);
262 if (! other->is_resolved)
263 other->resolve (verifier);
264 return data.klass == other->data.klass;
267 // Merge THIS type into OTHER, returning the result. This will
268 // return OTHER if all the classes in THIS already appear in
269 // OTHER.
270 ref_intersection *merge (ref_intersection *other,
271 _Jv_BytecodeVerifier *verifier)
273 ref_intersection *tail = other;
274 for (ref_intersection *self = this; self != NULL; self = self->ref_next)
276 bool add = true;
277 for (ref_intersection *iter = other; iter != NULL;
278 iter = iter->ref_next)
280 if (iter->equals (self, verifier))
282 add = false;
283 break;
287 if (add)
288 tail = new ref_intersection (self, tail, verifier);
290 return tail;
293 void resolve (_Jv_BytecodeVerifier *verifier)
295 if (is_resolved)
296 return;
298 using namespace java::lang;
299 java::lang::ClassLoader *loader
300 = verifier->current_class->getClassLoaderInternal();
301 // We might see either kind of name. Sigh.
302 if (data.name->data[0] == 'L'
303 && data.name->data[data.name->length - 1] == ';')
304 data.klass = _Jv_FindClassFromSignature (data.name->data, loader);
305 else
306 data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
307 false, loader);
308 is_resolved = true;
311 // See if an object of type OTHER can be assigned to an object of
312 // type *THIS. This might resolve classes in one chain or the
313 // other.
314 bool compatible (ref_intersection *other,
315 _Jv_BytecodeVerifier *verifier)
317 ref_intersection *self = this;
319 for (; self != NULL; self = self->ref_next)
321 ref_intersection *other_iter = other;
323 for (; other_iter != NULL; other_iter = other_iter->ref_next)
325 // Avoid resolving if possible.
326 if (! self->is_resolved
327 && ! other_iter->is_resolved
328 && _Jv_equalUtf8Consts (self->data.name,
329 other_iter->data.name))
330 continue;
332 if (! self->is_resolved)
333 self->resolve(verifier);
334 if (! other_iter->is_resolved)
335 other_iter->resolve(verifier);
337 if (! is_assignable_from_slow (self->data.klass,
338 other_iter->data.klass))
339 return false;
343 return true;
346 bool isarray ()
348 // assert (ref_next == NULL);
349 if (is_resolved)
350 return data.klass->isArray ();
351 else
352 return data.name->data[0] == '[';
355 bool isinterface (_Jv_BytecodeVerifier *verifier)
357 // assert (ref_next == NULL);
358 if (! is_resolved)
359 resolve (verifier);
360 return data.klass->isInterface ();
363 bool isabstract (_Jv_BytecodeVerifier *verifier)
365 // assert (ref_next == NULL);
366 if (! is_resolved)
367 resolve (verifier);
368 using namespace java::lang::reflect;
369 return Modifier::isAbstract (data.klass->getModifiers ());
372 jclass getclass (_Jv_BytecodeVerifier *verifier)
374 if (! is_resolved)
375 resolve (verifier);
376 return data.klass;
379 int count_dimensions ()
381 int ndims = 0;
382 if (is_resolved)
384 jclass k = data.klass;
385 while (k->isArray ())
387 k = k->getComponentType ();
388 ++ndims;
391 else
393 char *p = data.name->data;
394 while (*p++ == '[')
395 ++ndims;
397 return ndims;
400 void *operator new (size_t bytes)
402 return _Jv_Malloc (bytes);
405 void operator delete (void *mem)
407 _Jv_Free (mem);
411 // Return the type_val corresponding to a primitive signature
412 // character. For instance `I' returns `int.class'.
413 type_val get_type_val_for_signature (jchar sig)
415 type_val rt;
416 switch (sig)
418 case 'Z':
419 rt = boolean_type;
420 break;
421 case 'B':
422 rt = byte_type;
423 break;
424 case 'C':
425 rt = char_type;
426 break;
427 case 'S':
428 rt = short_type;
429 break;
430 case 'I':
431 rt = int_type;
432 break;
433 case 'J':
434 rt = long_type;
435 break;
436 case 'F':
437 rt = float_type;
438 break;
439 case 'D':
440 rt = double_type;
441 break;
442 case 'V':
443 rt = void_type;
444 break;
445 default:
446 verify_fail ("invalid signature");
448 return rt;
451 // Return the type_val corresponding to a primitive class.
452 type_val get_type_val_for_signature (jclass k)
454 return get_type_val_for_signature ((jchar) k->method_count);
457 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
458 // TARGET haven't been prepared.
459 static bool is_assignable_from_slow (jclass target, jclass source)
461 // First, strip arrays.
462 while (target->isArray ())
464 // If target is array, source must be as well.
465 if (! source->isArray ())
466 return false;
467 target = target->getComponentType ();
468 source = source->getComponentType ();
471 // Quick success.
472 if (target == &java::lang::Object::class$)
473 return true;
477 if (source == target)
478 return true;
480 if (target->isPrimitive () || source->isPrimitive ())
481 return false;
483 if (target->isInterface ())
485 for (int i = 0; i < source->interface_count; ++i)
487 // We use a recursive call because we also need to
488 // check superinterfaces.
489 if (is_assignable_from_slow (target, source->interfaces[i]))
490 return true;
493 source = source->getSuperclass ();
495 while (source != NULL);
497 return false;
500 // This is used to keep track of which `jsr's correspond to a given
501 // jsr target.
502 struct subr_info
504 // PC of the instruction just after the jsr.
505 int pc;
506 // Link.
507 subr_info *next;
510 // This is used to keep track of which subroutine entry point
511 // corresponds to which `ret' instruction.
512 struct subr_entry_info
514 // PC of the subroutine entry point.
515 int pc;
516 // PC of the `ret' instruction.
517 int ret_pc;
518 // Link.
519 subr_entry_info *next;
522 // The `type' class is used to represent a single type in the
523 // verifier.
524 struct type
526 // The type key.
527 type_val key;
529 // For reference types, the representation of the type.
530 ref_intersection *klass;
532 // This is used when constructing a new object. It is the PC of the
533 // `new' instruction which created the object. We use the special
534 // value -2 to mean that this is uninitialized, and the special
535 // value -1 for the case where the current method is itself the
536 // <init> method.
537 int pc;
539 static const int UNINIT = -2;
540 static const int SELF = -1;
542 // Basic constructor.
543 type ()
545 key = unsuitable_type;
546 klass = NULL;
547 pc = UNINIT;
550 // Make a new instance given the type tag. We assume a generic
551 // `reference_type' means Object.
552 type (type_val k)
554 key = k;
555 // For reference_type, if KLASS==NULL then that means we are
556 // looking for a generic object of any kind, including an
557 // uninitialized reference.
558 klass = NULL;
559 pc = UNINIT;
562 // Make a new instance given a class.
563 type (jclass k, _Jv_BytecodeVerifier *verifier)
565 key = reference_type;
566 klass = new ref_intersection (k, verifier);
567 pc = UNINIT;
570 // Make a new instance given the name of a class.
571 type (_Jv_Utf8Const *n, _Jv_BytecodeVerifier *verifier)
573 key = reference_type;
574 klass = new ref_intersection (n, verifier);
575 pc = UNINIT;
578 // Copy constructor.
579 type (const type &t)
581 key = t.key;
582 klass = t.klass;
583 pc = t.pc;
586 // These operators are required because libgcj can't link in
587 // -lstdc++.
588 void *operator new[] (size_t bytes)
590 return _Jv_Malloc (bytes);
593 void operator delete[] (void *mem)
595 _Jv_Free (mem);
598 type& operator= (type_val k)
600 key = k;
601 klass = NULL;
602 pc = UNINIT;
603 return *this;
606 type& operator= (const type& t)
608 key = t.key;
609 klass = t.klass;
610 pc = t.pc;
611 return *this;
614 // Promote a numeric type.
615 type &promote ()
617 if (key == boolean_type || key == char_type
618 || key == byte_type || key == short_type)
619 key = int_type;
620 return *this;
623 // Mark this type as the uninitialized result of `new'.
624 void set_uninitialized (int npc, _Jv_BytecodeVerifier *verifier)
626 if (key == reference_type)
627 key = uninitialized_reference_type;
628 else
629 verifier->verify_fail ("internal error in type::uninitialized");
630 pc = npc;
633 // Mark this type as now initialized.
634 void set_initialized (int npc)
636 if (npc != UNINIT && pc == npc && key == uninitialized_reference_type)
638 key = reference_type;
639 pc = UNINIT;
644 // Return true if an object of type K can be assigned to a variable
645 // of type *THIS. Handle various special cases too. Might modify
646 // *THIS or K. Note however that this does not perform numeric
647 // promotion.
648 bool compatible (type &k, _Jv_BytecodeVerifier *verifier)
650 // Any type is compatible with the unsuitable type.
651 if (key == unsuitable_type)
652 return true;
654 if (key < reference_type || k.key < reference_type)
655 return key == k.key;
657 // The `null' type is convertible to any initialized reference
658 // type.
659 if (key == null_type)
660 return k.key != uninitialized_reference_type;
661 if (k.key == null_type)
662 return key != uninitialized_reference_type;
664 // A special case for a generic reference.
665 if (klass == NULL)
666 return true;
667 if (k.klass == NULL)
668 verifier->verify_fail ("programmer error in type::compatible");
670 // An initialized type and an uninitialized type are not
671 // compatible.
672 if (isinitialized () != k.isinitialized ())
673 return false;
675 // Two uninitialized objects are compatible if either:
676 // * The PCs are identical, or
677 // * One PC is UNINIT.
678 if (! isinitialized ())
680 if (pc != k.pc && pc != UNINIT && k.pc != UNINIT)
681 return false;
684 return klass->compatible(k.klass, verifier);
687 bool isvoid () const
689 return key == void_type;
692 bool iswide () const
694 return key == long_type || key == double_type;
697 // Return number of stack or local variable slots taken by this
698 // type.
699 int depth () const
701 return iswide () ? 2 : 1;
704 bool isarray () const
706 // We treat null_type as not an array. This is ok based on the
707 // current uses of this method.
708 if (key == reference_type)
709 return klass->isarray ();
710 return false;
713 bool isnull () const
715 return key == null_type;
718 bool isinterface (_Jv_BytecodeVerifier *verifier)
720 if (key != reference_type)
721 return false;
722 return klass->isinterface (verifier);
725 bool isabstract (_Jv_BytecodeVerifier *verifier)
727 if (key != reference_type)
728 return false;
729 return klass->isabstract (verifier);
732 // Return the element type of an array.
733 type element_type (_Jv_BytecodeVerifier *verifier)
735 if (key != reference_type)
736 verifier->verify_fail ("programmer error in type::element_type()", -1);
738 jclass k = klass->getclass (verifier)->getComponentType ();
739 if (k->isPrimitive ())
740 return type (verifier->get_type_val_for_signature (k));
741 return type (k, verifier);
744 // Return the array type corresponding to an initialized
745 // reference. We could expand this to work for other kinds of
746 // types, but currently we don't need to.
747 type to_array (_Jv_BytecodeVerifier *verifier)
749 if (key != reference_type)
750 verifier->verify_fail ("internal error in type::to_array()");
752 jclass k = klass->getclass (verifier);
753 return type (_Jv_GetArrayClass (k, k->getClassLoaderInternal()),
754 verifier);
757 bool isreference () const
759 return key >= reference_type;
762 int get_pc () const
764 return pc;
767 bool isinitialized () const
769 return key == reference_type || key == null_type;
772 bool isresolved () const
774 return (key == reference_type
775 || key == null_type
776 || key == uninitialized_reference_type);
779 void verify_dimensions (int ndims, _Jv_BytecodeVerifier *verifier)
781 // The way this is written, we don't need to check isarray().
782 if (key != reference_type)
783 verifier->verify_fail ("internal error in verify_dimensions: not a reference type");
785 if (klass->count_dimensions () < ndims)
786 verifier->verify_fail ("array type has fewer dimensions than required");
789 // Merge OLD_TYPE into this. On error throw exception.
790 bool merge (type& old_type, bool local_semantics,
791 _Jv_BytecodeVerifier *verifier)
793 bool changed = false;
794 bool refo = old_type.isreference ();
795 bool refn = isreference ();
796 if (refo && refn)
798 if (old_type.key == null_type)
800 else if (key == null_type)
802 *this = old_type;
803 changed = true;
805 else if (isinitialized () != old_type.isinitialized ())
806 verifier->verify_fail ("merging initialized and uninitialized types");
807 else
809 if (! isinitialized ())
811 if (pc == UNINIT)
812 pc = old_type.pc;
813 else if (old_type.pc == UNINIT)
815 else if (pc != old_type.pc)
816 verifier->verify_fail ("merging different uninitialized types");
819 ref_intersection *merged = old_type.klass->merge (klass,
820 verifier);
821 if (merged != klass)
823 klass = merged;
824 changed = true;
828 else if (refo || refn || key != old_type.key)
830 if (local_semantics)
832 // If we're merging into an "unused" slot, then we
833 // simply accept whatever we're merging from.
834 if (key == unused_by_subroutine_type)
836 *this = old_type;
837 changed = true;
839 else if (old_type.key == unused_by_subroutine_type)
841 // Do nothing.
843 // If we already have an `unsuitable' type, then we
844 // don't need to change again.
845 else if (key != unsuitable_type)
847 key = unsuitable_type;
848 changed = true;
851 else
852 verifier->verify_fail ("unmergeable type");
854 return changed;
857 #ifdef VERIFY_DEBUG
858 void print (void) const
860 char c = '?';
861 switch (key)
863 case boolean_type: c = 'Z'; break;
864 case byte_type: c = 'B'; break;
865 case char_type: c = 'C'; break;
866 case short_type: c = 'S'; break;
867 case int_type: c = 'I'; break;
868 case long_type: c = 'J'; break;
869 case float_type: c = 'F'; break;
870 case double_type: c = 'D'; break;
871 case void_type: c = 'V'; break;
872 case unsuitable_type: c = '-'; break;
873 case return_address_type: c = 'r'; break;
874 case continuation_type: c = '+'; break;
875 case unused_by_subroutine_type: c = '_'; break;
876 case reference_type: c = 'L'; break;
877 case null_type: c = '@'; break;
878 case uninitialized_reference_type: c = 'U'; break;
880 debug_print ("%c", c);
882 #endif /* VERIFY_DEBUG */
885 // This class holds all the state information we need for a given
886 // location.
887 struct state
889 // The current top of the stack, in terms of slots.
890 int stacktop;
891 // The current depth of the stack. This will be larger than
892 // STACKTOP when wide types are on the stack.
893 int stackdepth;
894 // The stack.
895 type *stack;
896 // The local variables.
897 type *locals;
898 // This is used in subroutines to keep track of which local
899 // variables have been accessed.
900 bool *local_changed;
901 // If not 0, then we are in a subroutine. The value is the PC of
902 // the subroutine's entry point. We can use 0 as an exceptional
903 // value because PC=0 can never be a subroutine.
904 int subroutine;
905 // This is used to keep a linked list of all the states which
906 // require re-verification. We use the PC to keep track.
907 int next;
908 // We keep track of the type of `this' specially. This is used to
909 // ensure that an instance initializer invokes another initializer
910 // on `this' before returning. We must keep track of this
911 // specially because otherwise we might be confused by code which
912 // assigns to locals[0] (overwriting `this') and then returns
913 // without really initializing.
914 type this_type;
915 // This is a list of all subroutines that have been seen at this
916 // point. Ordinarily this is NULL; it is only allocated and used
917 // in relatively weird situations involving non-ret exit from a
918 // subroutine. We have to keep track of this in this way to avoid
919 // endless recursion in these cases.
920 subr_info *seen_subrs;
922 // INVALID marks a state which is not on the linked list of states
923 // requiring reverification.
924 static const int INVALID = -1;
925 // NO_NEXT marks the state at the end of the reverification list.
926 static const int NO_NEXT = -2;
928 // This is used to mark the stack depth at the instruction just
929 // after a `jsr' when we haven't yet processed the corresponding
930 // `ret'. See handle_jsr_insn for more information.
931 static const int NO_STACK = -1;
933 state ()
934 : this_type ()
936 stack = NULL;
937 locals = NULL;
938 local_changed = NULL;
939 seen_subrs = NULL;
942 state (int max_stack, int max_locals)
943 : this_type ()
945 stacktop = 0;
946 stackdepth = 0;
947 stack = new type[max_stack];
948 for (int i = 0; i < max_stack; ++i)
949 stack[i] = unsuitable_type;
950 locals = new type[max_locals];
951 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
952 seen_subrs = NULL;
953 for (int i = 0; i < max_locals; ++i)
955 locals[i] = unsuitable_type;
956 local_changed[i] = false;
958 next = INVALID;
959 subroutine = 0;
962 state (const state *orig, int max_stack, int max_locals,
963 bool ret_semantics = false)
965 stack = new type[max_stack];
966 locals = new type[max_locals];
967 local_changed = (bool *) _Jv_Malloc (sizeof (bool) * max_locals);
968 seen_subrs = NULL;
969 copy (orig, max_stack, max_locals, ret_semantics);
970 next = INVALID;
973 ~state ()
975 if (stack)
976 delete[] stack;
977 if (locals)
978 delete[] locals;
979 if (local_changed)
980 _Jv_Free (local_changed);
981 clean_subrs ();
984 void *operator new[] (size_t bytes)
986 return _Jv_Malloc (bytes);
989 void operator delete[] (void *mem)
991 _Jv_Free (mem);
994 void *operator new (size_t bytes)
996 return _Jv_Malloc (bytes);
999 void operator delete (void *mem)
1001 _Jv_Free (mem);
1004 void clean_subrs ()
1006 subr_info *info = seen_subrs;
1007 while (info != NULL)
1009 subr_info *next = info->next;
1010 _Jv_Free (info);
1011 info = next;
1015 void copy (const state *copy, int max_stack, int max_locals,
1016 bool ret_semantics = false)
1018 stacktop = copy->stacktop;
1019 stackdepth = copy->stackdepth;
1020 subroutine = copy->subroutine;
1021 for (int i = 0; i < max_stack; ++i)
1022 stack[i] = copy->stack[i];
1023 for (int i = 0; i < max_locals; ++i)
1025 // See push_jump_merge to understand this case.
1026 if (ret_semantics)
1027 locals[i] = type (copy->local_changed[i]
1028 ? unsuitable_type
1029 : unused_by_subroutine_type);
1030 else
1031 locals[i] = copy->locals[i];
1032 local_changed[i] = copy->local_changed[i];
1035 clean_subrs ();
1036 if (copy->seen_subrs)
1038 for (subr_info *info = seen_subrs; info != NULL; info = info->next)
1039 add_subr (info->pc);
1041 else
1042 seen_subrs = NULL;
1044 this_type = copy->this_type;
1045 // Don't modify `next'.
1048 // Modify this state to reflect entry to an exception handler.
1049 void set_exception (type t, int max_stack)
1051 stackdepth = 1;
1052 stacktop = 1;
1053 stack[0] = t;
1054 for (int i = stacktop; i < max_stack; ++i)
1055 stack[i] = unsuitable_type;
1058 // Modify this state to reflect entry into a subroutine.
1059 void enter_subroutine (int npc, int max_locals)
1061 subroutine = npc;
1062 // Mark all items as unchanged. Each subroutine needs to keep
1063 // track of its `changed' state independently. In the case of
1064 // nested subroutines, this information will be merged back into
1065 // parent by the `ret'.
1066 for (int i = 0; i < max_locals; ++i)
1067 local_changed[i] = false;
1070 // Indicate that we've been in this this subroutine.
1071 void add_subr (int pc)
1073 subr_info *n = (subr_info *) _Jv_Malloc (sizeof (subr_info));
1074 n->pc = pc;
1075 n->next = seen_subrs;
1076 seen_subrs = n;
1079 // Merge STATE_OLD into this state. Destructively modifies this
1080 // state. Returns true if the new state was in fact changed.
1081 // Will throw an exception if the states are not mergeable.
1082 bool merge (state *state_old, bool ret_semantics,
1083 int max_locals, _Jv_BytecodeVerifier *verifier)
1085 bool changed = false;
1087 // Special handling for `this'. If one or the other is
1088 // uninitialized, then the merge is uninitialized.
1089 if (this_type.isinitialized ())
1090 this_type = state_old->this_type;
1092 // Merge subroutine states. Here we just keep track of what
1093 // subroutine we think we're in. We only check for a merge
1094 // (which is invalid) when we see a `ret'.
1095 if (subroutine == state_old->subroutine)
1097 // Nothing.
1099 else if (subroutine == 0)
1101 subroutine = state_old->subroutine;
1102 changed = true;
1104 else
1106 // If the subroutines differ, and we haven't seen this
1107 // subroutine before, indicate that the state changed. This
1108 // is needed to detect when subroutines have merged.
1109 bool found = false;
1110 for (subr_info *info = seen_subrs; info != NULL; info = info->next)
1112 if (info->pc == state_old->subroutine)
1114 found = true;
1115 break;
1118 if (! found)
1120 add_subr (state_old->subroutine);
1121 changed = true;
1125 // Merge stacks. Special handling for NO_STACK case.
1126 if (state_old->stacktop == NO_STACK)
1128 // Nothing to do in this case; we don't care about modifying
1129 // the old state.
1131 else if (stacktop == NO_STACK)
1133 stacktop = state_old->stacktop;
1134 stackdepth = state_old->stackdepth;
1135 for (int i = 0; i < stacktop; ++i)
1136 stack[i] = state_old->stack[i];
1137 changed = true;
1139 else if (state_old->stacktop != stacktop)
1140 verifier->verify_fail ("stack sizes differ");
1141 else
1143 for (int i = 0; i < state_old->stacktop; ++i)
1145 if (stack[i].merge (state_old->stack[i], false, verifier))
1146 changed = true;
1150 // Merge local variables.
1151 for (int i = 0; i < max_locals; ++i)
1153 // If we're not processing a `ret', then we merge every
1154 // local variable. If we are processing a `ret', then we
1155 // only merge locals which changed in the subroutine. When
1156 // processing a `ret', STATE_OLD is the state at the point
1157 // of the `ret', and THIS is the state just after the `jsr'.
1158 if (! ret_semantics || state_old->local_changed[i])
1160 if (locals[i].merge (state_old->locals[i], true, verifier))
1162 // Note that we don't call `note_variable' here.
1163 // This change doesn't represent a real change to a
1164 // local, but rather a merge artifact. If we're in
1165 // a subroutine which is called with two
1166 // incompatible types in a slot that is unused by
1167 // the subroutine, then we don't want to mark that
1168 // variable as having been modified.
1169 changed = true;
1173 // If we're in a subroutine, we must compute the union of
1174 // all the changed local variables.
1175 if (state_old->local_changed[i])
1176 note_variable (i);
1179 return changed;
1182 // Throw an exception if there is an uninitialized object on the
1183 // stack or in a local variable. EXCEPTION_SEMANTICS controls
1184 // whether we're using backwards-branch or exception-handing
1185 // semantics.
1186 void check_no_uninitialized_objects (int max_locals,
1187 _Jv_BytecodeVerifier *verifier,
1188 bool exception_semantics = false)
1190 if (! exception_semantics)
1192 for (int i = 0; i < stacktop; ++i)
1193 if (stack[i].isreference () && ! stack[i].isinitialized ())
1194 verifier->verify_fail ("uninitialized object on stack");
1197 for (int i = 0; i < max_locals; ++i)
1198 if (locals[i].isreference () && ! locals[i].isinitialized ())
1199 verifier->verify_fail ("uninitialized object in local variable");
1201 check_this_initialized (verifier);
1204 // Ensure that `this' has been initialized.
1205 void check_this_initialized (_Jv_BytecodeVerifier *verifier)
1207 if (this_type.isreference () && ! this_type.isinitialized ())
1208 verifier->verify_fail ("`this' is uninitialized");
1211 // Set type of `this'.
1212 void set_this_type (const type &k)
1214 this_type = k;
1217 // Note that a local variable was modified.
1218 void note_variable (int index)
1220 if (subroutine > 0)
1221 local_changed[index] = true;
1224 // Mark each `new'd object we know of that was allocated at PC as
1225 // initialized.
1226 void set_initialized (int pc, int max_locals)
1228 for (int i = 0; i < stacktop; ++i)
1229 stack[i].set_initialized (pc);
1230 for (int i = 0; i < max_locals; ++i)
1231 locals[i].set_initialized (pc);
1232 this_type.set_initialized (pc);
1235 // Return true if this state is the unmerged result of a `ret'.
1236 bool is_unmerged_ret_state (int max_locals) const
1238 if (stacktop == NO_STACK)
1239 return true;
1240 for (int i = 0; i < max_locals; ++i)
1242 if (locals[i].key == unused_by_subroutine_type)
1243 return true;
1245 return false;
1248 #ifdef VERIFY_DEBUG
1249 void print (const char *leader, int pc,
1250 int max_stack, int max_locals) const
1252 debug_print ("%s [%4d]: [stack] ", leader, pc);
1253 int i;
1254 for (i = 0; i < stacktop; ++i)
1255 stack[i].print ();
1256 for (; i < max_stack; ++i)
1257 debug_print (".");
1258 debug_print (" [local] ");
1259 for (i = 0; i < max_locals; ++i)
1261 locals[i].print ();
1262 debug_print (local_changed[i] ? "+" : " ");
1264 if (subroutine == 0)
1265 debug_print (" | None");
1266 else
1267 debug_print (" | %4d", subroutine);
1268 debug_print (" | %p\n", this);
1270 #else
1271 inline void print (const char *, int, int, int) const
1274 #endif /* VERIFY_DEBUG */
1277 type pop_raw ()
1279 if (current_state->stacktop <= 0)
1280 verify_fail ("stack empty");
1281 type r = current_state->stack[--current_state->stacktop];
1282 current_state->stackdepth -= r.depth ();
1283 if (current_state->stackdepth < 0)
1284 verify_fail ("stack empty", start_PC);
1285 return r;
1288 type pop32 ()
1290 type r = pop_raw ();
1291 if (r.iswide ())
1292 verify_fail ("narrow pop of wide type");
1293 return r;
1296 type pop_type (type match)
1298 match.promote ();
1299 type t = pop_raw ();
1300 if (! match.compatible (t, this))
1301 verify_fail ("incompatible type on stack");
1302 return t;
1305 // Pop a reference which is guaranteed to be initialized. MATCH
1306 // doesn't have to be a reference type; in this case this acts like
1307 // pop_type.
1308 type pop_init_ref (type match)
1310 type t = pop_raw ();
1311 if (t.isreference () && ! t.isinitialized ())
1312 verify_fail ("initialized reference required");
1313 else if (! match.compatible (t, this))
1314 verify_fail ("incompatible type on stack");
1315 return t;
1318 // Pop a reference type or a return address.
1319 type pop_ref_or_return ()
1321 type t = pop_raw ();
1322 if (! t.isreference () && t.key != return_address_type)
1323 verify_fail ("expected reference or return address on stack");
1324 return t;
1327 void push_type (type t)
1329 // If T is a numeric type like short, promote it to int.
1330 t.promote ();
1332 int depth = t.depth ();
1333 if (current_state->stackdepth + depth > current_method->max_stack)
1334 verify_fail ("stack overflow");
1335 current_state->stack[current_state->stacktop++] = t;
1336 current_state->stackdepth += depth;
1339 void set_variable (int index, type t)
1341 // If T is a numeric type like short, promote it to int.
1342 t.promote ();
1344 int depth = t.depth ();
1345 if (index > current_method->max_locals - depth)
1346 verify_fail ("invalid local variable");
1347 current_state->locals[index] = t;
1348 current_state->note_variable (index);
1350 if (depth == 2)
1352 current_state->locals[index + 1] = continuation_type;
1353 current_state->note_variable (index + 1);
1355 if (index > 0 && current_state->locals[index - 1].iswide ())
1357 current_state->locals[index - 1] = unsuitable_type;
1358 // There's no need to call note_variable here.
1362 type get_variable (int index, type t)
1364 int depth = t.depth ();
1365 if (index > current_method->max_locals - depth)
1366 verify_fail ("invalid local variable");
1367 if (! t.compatible (current_state->locals[index], this))
1368 verify_fail ("incompatible type in local variable");
1369 if (depth == 2)
1371 type t (continuation_type);
1372 if (! current_state->locals[index + 1].compatible (t, this))
1373 verify_fail ("invalid local variable");
1375 return current_state->locals[index];
1378 // Make sure ARRAY is an array type and that its elements are
1379 // compatible with type ELEMENT. Returns the actual element type.
1380 type require_array_type (type array, type element)
1382 // An odd case. Here we just pretend that everything went ok. If
1383 // the requested element type is some kind of reference, return
1384 // the null type instead.
1385 if (array.isnull ())
1386 return element.isreference () ? type (null_type) : element;
1388 if (! array.isarray ())
1389 verify_fail ("array required");
1391 type t = array.element_type (this);
1392 if (! element.compatible (t, this))
1394 // Special case for byte arrays, which must also be boolean
1395 // arrays.
1396 bool ok = true;
1397 if (element.key == byte_type)
1399 type e2 (boolean_type);
1400 ok = e2.compatible (t, this);
1402 if (! ok)
1403 verify_fail ("incompatible array element type");
1406 // Return T and not ELEMENT, because T might be specialized.
1407 return t;
1410 jint get_byte ()
1412 if (PC >= current_method->code_length)
1413 verify_fail ("premature end of bytecode");
1414 return (jint) bytecode[PC++] & 0xff;
1417 jint get_ushort ()
1419 jint b1 = get_byte ();
1420 jint b2 = get_byte ();
1421 return (jint) ((b1 << 8) | b2) & 0xffff;
1424 jint get_short ()
1426 jint b1 = get_byte ();
1427 jint b2 = get_byte ();
1428 jshort s = (b1 << 8) | b2;
1429 return (jint) s;
1432 jint get_int ()
1434 jint b1 = get_byte ();
1435 jint b2 = get_byte ();
1436 jint b3 = get_byte ();
1437 jint b4 = get_byte ();
1438 return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
1441 int compute_jump (int offset)
1443 int npc = start_PC + offset;
1444 if (npc < 0 || npc >= current_method->code_length)
1445 verify_fail ("branch out of range", start_PC);
1446 return npc;
1449 // Merge the indicated state into the state at the branch target and
1450 // schedule a new PC if there is a change. If RET_SEMANTICS is
1451 // true, then we are merging from a `ret' instruction into the
1452 // instruction after a `jsr'. This is a special case with its own
1453 // modified semantics.
1454 void push_jump_merge (int npc, state *nstate, bool ret_semantics = false)
1456 bool changed = true;
1457 if (states[npc] == NULL)
1459 // There's a weird situation here. If are examining the
1460 // branch that results from a `ret', and there is not yet a
1461 // state available at the branch target (the instruction just
1462 // after the `jsr'), then we have to construct a special kind
1463 // of state at that point for future merging. This special
1464 // state has the type `unused_by_subroutine_type' in each slot
1465 // which was not modified by the subroutine.
1466 states[npc] = new state (nstate, current_method->max_stack,
1467 current_method->max_locals, ret_semantics);
1468 debug_print ("== New state in push_jump_merge\n");
1469 states[npc]->print ("New", npc, current_method->max_stack,
1470 current_method->max_locals);
1472 else
1474 debug_print ("== Merge states in push_jump_merge\n");
1475 nstate->print ("Frm", start_PC, current_method->max_stack,
1476 current_method->max_locals);
1477 states[npc]->print (" To", npc, current_method->max_stack,
1478 current_method->max_locals);
1479 changed = states[npc]->merge (nstate, ret_semantics,
1480 current_method->max_locals, this);
1481 states[npc]->print ("New", npc, current_method->max_stack,
1482 current_method->max_locals);
1485 if (changed && states[npc]->next == state::INVALID)
1487 // The merge changed the state, and the new PC isn't yet on our
1488 // list of PCs to re-verify.
1489 states[npc]->next = next_verify_pc;
1490 next_verify_pc = npc;
1494 void push_jump (int offset)
1496 int npc = compute_jump (offset);
1497 if (npc < PC)
1498 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1499 push_jump_merge (npc, current_state);
1502 void push_exception_jump (type t, int pc)
1504 current_state->check_no_uninitialized_objects (current_method->max_locals,
1505 this, true);
1506 state s (current_state, current_method->max_stack,
1507 current_method->max_locals);
1508 if (current_method->max_stack < 1)
1509 verify_fail ("stack overflow at exception handler");
1510 s.set_exception (t, current_method->max_stack);
1511 push_jump_merge (pc, &s);
1514 int pop_jump ()
1516 int *prev_loc = &next_verify_pc;
1517 int npc = next_verify_pc;
1519 while (npc != state::NO_NEXT)
1521 // If the next available PC is an unmerged `ret' state, then
1522 // we aren't yet ready to handle it. That's because we would
1523 // need all kind of special cases to do so. So instead we
1524 // defer this jump until after we've processed it via a
1525 // fall-through. This has to happen because the instruction
1526 // before this one must be a `jsr'.
1527 if (! states[npc]->is_unmerged_ret_state (current_method->max_locals))
1529 *prev_loc = states[npc]->next;
1530 states[npc]->next = state::INVALID;
1531 return npc;
1534 prev_loc = &states[npc]->next;
1535 npc = states[npc]->next;
1538 // Note that we might have gotten here even when there are
1539 // remaining states to process. That can happen if we find a
1540 // `jsr' without a `ret'.
1541 return state::NO_NEXT;
1544 void invalidate_pc ()
1546 PC = state::NO_NEXT;
1549 void note_branch_target (int pc, bool is_jsr_target = false)
1551 // Don't check `pc <= PC', because we've advanced PC after
1552 // fetching the target and we haven't yet checked the next
1553 // instruction.
1554 if (pc < PC && ! (flags[pc] & FLAG_INSN_START))
1555 verify_fail ("branch not to instruction start", start_PC);
1556 flags[pc] |= FLAG_BRANCH_TARGET;
1557 if (is_jsr_target)
1559 // Record the jsr which called this instruction.
1560 subr_info *info = (subr_info *) _Jv_Malloc (sizeof (subr_info));
1561 info->pc = PC;
1562 info->next = jsr_ptrs[pc];
1563 jsr_ptrs[pc] = info;
1567 void skip_padding ()
1569 while ((PC % 4) > 0)
1570 if (get_byte () != 0)
1571 verify_fail ("found nonzero padding byte");
1574 // Return the subroutine to which the instruction at PC belongs.
1575 int get_subroutine (int pc)
1577 if (states[pc] == NULL)
1578 return 0;
1579 return states[pc]->subroutine;
1582 // Do the work for a `ret' instruction. INDEX is the index into the
1583 // local variables.
1584 void handle_ret_insn (int index)
1586 get_variable (index, return_address_type);
1588 int csub = current_state->subroutine;
1589 if (csub == 0)
1590 verify_fail ("no subroutine");
1592 // Check to see if we've merged subroutines.
1593 subr_entry_info *entry;
1594 for (entry = entry_points; entry != NULL; entry = entry->next)
1596 if (entry->ret_pc == start_PC)
1597 break;
1599 if (entry == NULL)
1601 entry = (subr_entry_info *) _Jv_Malloc (sizeof (subr_entry_info));
1602 entry->pc = csub;
1603 entry->ret_pc = start_PC;
1604 entry->next = entry_points;
1605 entry_points = entry;
1607 else if (entry->pc != csub)
1608 verify_fail ("subroutines merged");
1610 for (subr_info *subr = jsr_ptrs[csub]; subr != NULL; subr = subr->next)
1612 // We might be returning to a `jsr' that is at the end of the
1613 // bytecode. This is ok if we never return from the called
1614 // subroutine, but if we see this here it is an error.
1615 if (subr->pc >= current_method->code_length)
1616 verify_fail ("fell off end");
1618 // Temporarily modify the current state so it looks like we're
1619 // in the enclosing context.
1620 current_state->subroutine = get_subroutine (subr->pc);
1621 if (subr->pc < PC)
1622 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1623 push_jump_merge (subr->pc, current_state, true);
1626 current_state->subroutine = csub;
1627 invalidate_pc ();
1630 // We're in the subroutine SUB, calling a subroutine at DEST. Make
1631 // sure this subroutine isn't already on the stack.
1632 void check_nonrecursive_call (int sub, int dest)
1634 if (sub == 0)
1635 return;
1636 if (sub == dest)
1637 verify_fail ("recursive subroutine call");
1638 for (subr_info *info = jsr_ptrs[sub]; info != NULL; info = info->next)
1639 check_nonrecursive_call (get_subroutine (info->pc), dest);
1642 void handle_jsr_insn (int offset)
1644 int npc = compute_jump (offset);
1646 if (npc < PC)
1647 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1648 check_nonrecursive_call (current_state->subroutine, npc);
1650 // Modify our state as appropriate for entry into a subroutine.
1651 push_type (return_address_type);
1652 push_jump_merge (npc, current_state);
1653 // Clean up.
1654 pop_type (return_address_type);
1656 // On entry to the subroutine, the subroutine number must be set
1657 // and the locals must be marked as cleared. We do this after
1658 // merging state so that we don't erroneously "notice" a variable
1659 // change merely on entry.
1660 states[npc]->enter_subroutine (npc, current_method->max_locals);
1662 // Indicate that we don't know the stack depth of the instruction
1663 // following the `jsr'. The idea here is that we need to merge
1664 // the local variable state across the jsr, but the subroutine
1665 // might change the stack depth, so we can't make any assumptions
1666 // about it. So we have yet another special case. We know that
1667 // at this point PC points to the instruction after the jsr. Note
1668 // that it is ok to have a `jsr' at the end of the bytecode,
1669 // provided that the called subroutine never returns. So, we have
1670 // a special case here and another one when we handle the ret.
1671 if (PC < current_method->code_length)
1673 current_state->stacktop = state::NO_STACK;
1674 push_jump_merge (PC, current_state);
1676 invalidate_pc ();
1679 jclass construct_primitive_array_type (type_val prim)
1681 jclass k = NULL;
1682 switch (prim)
1684 case boolean_type:
1685 k = JvPrimClass (boolean);
1686 break;
1687 case char_type:
1688 k = JvPrimClass (char);
1689 break;
1690 case float_type:
1691 k = JvPrimClass (float);
1692 break;
1693 case double_type:
1694 k = JvPrimClass (double);
1695 break;
1696 case byte_type:
1697 k = JvPrimClass (byte);
1698 break;
1699 case short_type:
1700 k = JvPrimClass (short);
1701 break;
1702 case int_type:
1703 k = JvPrimClass (int);
1704 break;
1705 case long_type:
1706 k = JvPrimClass (long);
1707 break;
1709 // These aren't used here but we call them out to avoid
1710 // warnings.
1711 case void_type:
1712 case unsuitable_type:
1713 case return_address_type:
1714 case continuation_type:
1715 case unused_by_subroutine_type:
1716 case reference_type:
1717 case null_type:
1718 case uninitialized_reference_type:
1719 default:
1720 verify_fail ("unknown type in construct_primitive_array_type");
1722 k = _Jv_GetArrayClass (k, NULL);
1723 return k;
1726 // This pass computes the location of branch targets and also
1727 // instruction starts.
1728 void branch_prepass ()
1730 flags = (char *) _Jv_Malloc (current_method->code_length);
1731 jsr_ptrs = (subr_info **) _Jv_Malloc (sizeof (subr_info *)
1732 * current_method->code_length);
1734 for (int i = 0; i < current_method->code_length; ++i)
1736 flags[i] = 0;
1737 jsr_ptrs[i] = NULL;
1740 bool last_was_jsr = false;
1742 PC = 0;
1743 while (PC < current_method->code_length)
1745 // Set `start_PC' early so that error checking can have the
1746 // correct value.
1747 start_PC = PC;
1748 flags[PC] |= FLAG_INSN_START;
1750 // If the previous instruction was a jsr, then the next
1751 // instruction is a branch target -- the branch being the
1752 // corresponding `ret'.
1753 if (last_was_jsr)
1754 note_branch_target (PC);
1755 last_was_jsr = false;
1757 java_opcode opcode = (java_opcode) bytecode[PC++];
1758 switch (opcode)
1760 case op_nop:
1761 case op_aconst_null:
1762 case op_iconst_m1:
1763 case op_iconst_0:
1764 case op_iconst_1:
1765 case op_iconst_2:
1766 case op_iconst_3:
1767 case op_iconst_4:
1768 case op_iconst_5:
1769 case op_lconst_0:
1770 case op_lconst_1:
1771 case op_fconst_0:
1772 case op_fconst_1:
1773 case op_fconst_2:
1774 case op_dconst_0:
1775 case op_dconst_1:
1776 case op_iload_0:
1777 case op_iload_1:
1778 case op_iload_2:
1779 case op_iload_3:
1780 case op_lload_0:
1781 case op_lload_1:
1782 case op_lload_2:
1783 case op_lload_3:
1784 case op_fload_0:
1785 case op_fload_1:
1786 case op_fload_2:
1787 case op_fload_3:
1788 case op_dload_0:
1789 case op_dload_1:
1790 case op_dload_2:
1791 case op_dload_3:
1792 case op_aload_0:
1793 case op_aload_1:
1794 case op_aload_2:
1795 case op_aload_3:
1796 case op_iaload:
1797 case op_laload:
1798 case op_faload:
1799 case op_daload:
1800 case op_aaload:
1801 case op_baload:
1802 case op_caload:
1803 case op_saload:
1804 case op_istore_0:
1805 case op_istore_1:
1806 case op_istore_2:
1807 case op_istore_3:
1808 case op_lstore_0:
1809 case op_lstore_1:
1810 case op_lstore_2:
1811 case op_lstore_3:
1812 case op_fstore_0:
1813 case op_fstore_1:
1814 case op_fstore_2:
1815 case op_fstore_3:
1816 case op_dstore_0:
1817 case op_dstore_1:
1818 case op_dstore_2:
1819 case op_dstore_3:
1820 case op_astore_0:
1821 case op_astore_1:
1822 case op_astore_2:
1823 case op_astore_3:
1824 case op_iastore:
1825 case op_lastore:
1826 case op_fastore:
1827 case op_dastore:
1828 case op_aastore:
1829 case op_bastore:
1830 case op_castore:
1831 case op_sastore:
1832 case op_pop:
1833 case op_pop2:
1834 case op_dup:
1835 case op_dup_x1:
1836 case op_dup_x2:
1837 case op_dup2:
1838 case op_dup2_x1:
1839 case op_dup2_x2:
1840 case op_swap:
1841 case op_iadd:
1842 case op_isub:
1843 case op_imul:
1844 case op_idiv:
1845 case op_irem:
1846 case op_ishl:
1847 case op_ishr:
1848 case op_iushr:
1849 case op_iand:
1850 case op_ior:
1851 case op_ixor:
1852 case op_ladd:
1853 case op_lsub:
1854 case op_lmul:
1855 case op_ldiv:
1856 case op_lrem:
1857 case op_lshl:
1858 case op_lshr:
1859 case op_lushr:
1860 case op_land:
1861 case op_lor:
1862 case op_lxor:
1863 case op_fadd:
1864 case op_fsub:
1865 case op_fmul:
1866 case op_fdiv:
1867 case op_frem:
1868 case op_dadd:
1869 case op_dsub:
1870 case op_dmul:
1871 case op_ddiv:
1872 case op_drem:
1873 case op_ineg:
1874 case op_i2b:
1875 case op_i2c:
1876 case op_i2s:
1877 case op_lneg:
1878 case op_fneg:
1879 case op_dneg:
1880 case op_i2l:
1881 case op_i2f:
1882 case op_i2d:
1883 case op_l2i:
1884 case op_l2f:
1885 case op_l2d:
1886 case op_f2i:
1887 case op_f2l:
1888 case op_f2d:
1889 case op_d2i:
1890 case op_d2l:
1891 case op_d2f:
1892 case op_lcmp:
1893 case op_fcmpl:
1894 case op_fcmpg:
1895 case op_dcmpl:
1896 case op_dcmpg:
1897 case op_monitorenter:
1898 case op_monitorexit:
1899 case op_ireturn:
1900 case op_lreturn:
1901 case op_freturn:
1902 case op_dreturn:
1903 case op_areturn:
1904 case op_return:
1905 case op_athrow:
1906 case op_arraylength:
1907 break;
1909 case op_bipush:
1910 case op_ldc:
1911 case op_iload:
1912 case op_lload:
1913 case op_fload:
1914 case op_dload:
1915 case op_aload:
1916 case op_istore:
1917 case op_lstore:
1918 case op_fstore:
1919 case op_dstore:
1920 case op_astore:
1921 case op_ret:
1922 case op_newarray:
1923 get_byte ();
1924 break;
1926 case op_iinc:
1927 case op_sipush:
1928 case op_ldc_w:
1929 case op_ldc2_w:
1930 case op_getstatic:
1931 case op_getfield:
1932 case op_putfield:
1933 case op_putstatic:
1934 case op_new:
1935 case op_anewarray:
1936 case op_instanceof:
1937 case op_checkcast:
1938 case op_invokespecial:
1939 case op_invokestatic:
1940 case op_invokevirtual:
1941 get_short ();
1942 break;
1944 case op_multianewarray:
1945 get_short ();
1946 get_byte ();
1947 break;
1949 case op_jsr:
1950 last_was_jsr = true;
1951 // Fall through.
1952 case op_ifeq:
1953 case op_ifne:
1954 case op_iflt:
1955 case op_ifge:
1956 case op_ifgt:
1957 case op_ifle:
1958 case op_if_icmpeq:
1959 case op_if_icmpne:
1960 case op_if_icmplt:
1961 case op_if_icmpge:
1962 case op_if_icmpgt:
1963 case op_if_icmple:
1964 case op_if_acmpeq:
1965 case op_if_acmpne:
1966 case op_ifnull:
1967 case op_ifnonnull:
1968 case op_goto:
1969 note_branch_target (compute_jump (get_short ()), last_was_jsr);
1970 break;
1972 case op_tableswitch:
1974 skip_padding ();
1975 note_branch_target (compute_jump (get_int ()));
1976 jint low = get_int ();
1977 jint hi = get_int ();
1978 if (low > hi)
1979 verify_fail ("invalid tableswitch", start_PC);
1980 for (int i = low; i <= hi; ++i)
1981 note_branch_target (compute_jump (get_int ()));
1983 break;
1985 case op_lookupswitch:
1987 skip_padding ();
1988 note_branch_target (compute_jump (get_int ()));
1989 int npairs = get_int ();
1990 if (npairs < 0)
1991 verify_fail ("too few pairs in lookupswitch", start_PC);
1992 while (npairs-- > 0)
1994 get_int ();
1995 note_branch_target (compute_jump (get_int ()));
1998 break;
2000 case op_invokeinterface:
2001 get_short ();
2002 get_byte ();
2003 get_byte ();
2004 break;
2006 case op_wide:
2008 opcode = (java_opcode) get_byte ();
2009 get_short ();
2010 if (opcode == op_iinc)
2011 get_short ();
2013 break;
2015 case op_jsr_w:
2016 last_was_jsr = true;
2017 // Fall through.
2018 case op_goto_w:
2019 note_branch_target (compute_jump (get_int ()), last_was_jsr);
2020 break;
2022 // These are unused here, but we call them out explicitly
2023 // so that -Wswitch-enum doesn't complain.
2024 case op_putfield_1:
2025 case op_putfield_2:
2026 case op_putfield_4:
2027 case op_putfield_8:
2028 case op_putfield_a:
2029 case op_putstatic_1:
2030 case op_putstatic_2:
2031 case op_putstatic_4:
2032 case op_putstatic_8:
2033 case op_putstatic_a:
2034 case op_getfield_1:
2035 case op_getfield_2s:
2036 case op_getfield_2u:
2037 case op_getfield_4:
2038 case op_getfield_8:
2039 case op_getfield_a:
2040 case op_getstatic_1:
2041 case op_getstatic_2s:
2042 case op_getstatic_2u:
2043 case op_getstatic_4:
2044 case op_getstatic_8:
2045 case op_getstatic_a:
2046 default:
2047 verify_fail ("unrecognized instruction in branch_prepass",
2048 start_PC);
2051 // See if any previous branch tried to branch to the middle of
2052 // this instruction.
2053 for (int pc = start_PC + 1; pc < PC; ++pc)
2055 if ((flags[pc] & FLAG_BRANCH_TARGET))
2056 verify_fail ("branch to middle of instruction", pc);
2060 // Verify exception handlers.
2061 for (int i = 0; i < current_method->exc_count; ++i)
2063 if (! (flags[exception[i].handler_pc.i] & FLAG_INSN_START))
2064 verify_fail ("exception handler not at instruction start",
2065 exception[i].handler_pc.i);
2066 if (! (flags[exception[i].start_pc.i] & FLAG_INSN_START))
2067 verify_fail ("exception start not at instruction start",
2068 exception[i].start_pc.i);
2069 if (exception[i].end_pc.i != current_method->code_length
2070 && ! (flags[exception[i].end_pc.i] & FLAG_INSN_START))
2071 verify_fail ("exception end not at instruction start",
2072 exception[i].end_pc.i);
2074 flags[exception[i].handler_pc.i] |= FLAG_BRANCH_TARGET;
2078 void check_pool_index (int index)
2080 if (index < 0 || index >= current_class->constants.size)
2081 verify_fail ("constant pool index out of range", start_PC);
2084 type check_class_constant (int index)
2086 check_pool_index (index);
2087 _Jv_Constants *pool = &current_class->constants;
2088 if (pool->tags[index] == JV_CONSTANT_ResolvedClass)
2089 return type (pool->data[index].clazz, this);
2090 else if (pool->tags[index] == JV_CONSTANT_Class)
2091 return type (pool->data[index].utf8, this);
2092 verify_fail ("expected class constant", start_PC);
2095 type check_constant (int index)
2097 check_pool_index (index);
2098 _Jv_Constants *pool = &current_class->constants;
2099 if (pool->tags[index] == JV_CONSTANT_ResolvedString
2100 || pool->tags[index] == JV_CONSTANT_String)
2101 return type (&java::lang::String::class$, this);
2102 else if (pool->tags[index] == JV_CONSTANT_Integer)
2103 return type (int_type);
2104 else if (pool->tags[index] == JV_CONSTANT_Float)
2105 return type (float_type);
2106 verify_fail ("String, int, or float constant expected", start_PC);
2109 type check_wide_constant (int index)
2111 check_pool_index (index);
2112 _Jv_Constants *pool = &current_class->constants;
2113 if (pool->tags[index] == JV_CONSTANT_Long)
2114 return type (long_type);
2115 else if (pool->tags[index] == JV_CONSTANT_Double)
2116 return type (double_type);
2117 verify_fail ("long or double constant expected", start_PC);
2120 // Helper for both field and method. These are laid out the same in
2121 // the constant pool.
2122 type handle_field_or_method (int index, int expected,
2123 _Jv_Utf8Const **name,
2124 _Jv_Utf8Const **fmtype)
2126 check_pool_index (index);
2127 _Jv_Constants *pool = &current_class->constants;
2128 if (pool->tags[index] != expected)
2129 verify_fail ("didn't see expected constant", start_PC);
2130 // Once we know we have a Fieldref or Methodref we assume that it
2131 // is correctly laid out in the constant pool. I think the code
2132 // in defineclass.cc guarantees this.
2133 _Jv_ushort class_index, name_and_type_index;
2134 _Jv_loadIndexes (&pool->data[index],
2135 class_index,
2136 name_and_type_index);
2137 _Jv_ushort name_index, desc_index;
2138 _Jv_loadIndexes (&pool->data[name_and_type_index],
2139 name_index, desc_index);
2141 *name = pool->data[name_index].utf8;
2142 *fmtype = pool->data[desc_index].utf8;
2144 return check_class_constant (class_index);
2147 // Return field's type, compute class' type if requested.
2148 type check_field_constant (int index, type *class_type = NULL)
2150 _Jv_Utf8Const *name, *field_type;
2151 type ct = handle_field_or_method (index,
2152 JV_CONSTANT_Fieldref,
2153 &name, &field_type);
2154 if (class_type)
2155 *class_type = ct;
2156 if (field_type->data[0] == '[' || field_type->data[0] == 'L')
2157 return type (field_type, this);
2158 return get_type_val_for_signature (field_type->data[0]);
2161 type check_method_constant (int index, bool is_interface,
2162 _Jv_Utf8Const **method_name,
2163 _Jv_Utf8Const **method_signature)
2165 return handle_field_or_method (index,
2166 (is_interface
2167 ? JV_CONSTANT_InterfaceMethodref
2168 : JV_CONSTANT_Methodref),
2169 method_name, method_signature);
2172 type get_one_type (char *&p)
2174 char *start = p;
2176 int arraycount = 0;
2177 while (*p == '[')
2179 ++arraycount;
2180 ++p;
2183 char v = *p++;
2185 if (v == 'L')
2187 while (*p != ';')
2188 ++p;
2189 ++p;
2190 _Jv_Utf8Const *name = make_utf8_const (start, p - start);
2191 return type (name, this);
2194 // Casting to jchar here is ok since we are looking at an ASCII
2195 // character.
2196 type_val rt = get_type_val_for_signature (jchar (v));
2198 if (arraycount == 0)
2200 // Callers of this function eventually push their arguments on
2201 // the stack. So, promote them here.
2202 return type (rt).promote ();
2205 jclass k = construct_primitive_array_type (rt);
2206 while (--arraycount > 0)
2207 k = _Jv_GetArrayClass (k, NULL);
2208 return type (k, this);
2211 void compute_argument_types (_Jv_Utf8Const *signature,
2212 type *types)
2214 char *p = signature->data;
2215 // Skip `('.
2216 ++p;
2218 int i = 0;
2219 while (*p != ')')
2220 types[i++] = get_one_type (p);
2223 type compute_return_type (_Jv_Utf8Const *signature)
2225 char *p = signature->data;
2226 while (*p != ')')
2227 ++p;
2228 ++p;
2229 return get_one_type (p);
2232 void check_return_type (type onstack)
2234 type rt = compute_return_type (current_method->self->signature);
2235 if (! rt.compatible (onstack, this))
2236 verify_fail ("incompatible return type");
2239 // Initialize the stack for the new method. Returns true if this
2240 // method is an instance initializer.
2241 bool initialize_stack ()
2243 int var = 0;
2244 bool is_init = _Jv_equalUtf8Consts (current_method->self->name,
2245 gcj::init_name);
2246 bool is_clinit = _Jv_equalUtf8Consts (current_method->self->name,
2247 gcj::clinit_name);
2249 using namespace java::lang::reflect;
2250 if (! Modifier::isStatic (current_method->self->accflags))
2252 type kurr (current_class, this);
2253 if (is_init)
2255 kurr.set_uninitialized (type::SELF, this);
2256 is_init = true;
2258 else if (is_clinit)
2259 verify_fail ("<clinit> method must be static");
2260 set_variable (0, kurr);
2261 current_state->set_this_type (kurr);
2262 ++var;
2264 else
2266 if (is_init)
2267 verify_fail ("<init> method must be non-static");
2270 // We have to handle wide arguments specially here.
2271 int arg_count = _Jv_count_arguments (current_method->self->signature);
2272 type arg_types[arg_count];
2273 compute_argument_types (current_method->self->signature, arg_types);
2274 for (int i = 0; i < arg_count; ++i)
2276 set_variable (var, arg_types[i]);
2277 ++var;
2278 if (arg_types[i].iswide ())
2279 ++var;
2282 return is_init;
2285 void verify_instructions_0 ()
2287 current_state = new state (current_method->max_stack,
2288 current_method->max_locals);
2290 PC = 0;
2291 start_PC = 0;
2293 // True if we are verifying an instance initializer.
2294 bool this_is_init = initialize_stack ();
2296 states = (state **) _Jv_Malloc (sizeof (state *)
2297 * current_method->code_length);
2298 for (int i = 0; i < current_method->code_length; ++i)
2299 states[i] = NULL;
2301 next_verify_pc = state::NO_NEXT;
2303 while (true)
2305 // If the PC was invalidated, get a new one from the work list.
2306 if (PC == state::NO_NEXT)
2308 PC = pop_jump ();
2309 if (PC == state::INVALID)
2310 verify_fail ("can't happen: saw state::INVALID");
2311 if (PC == state::NO_NEXT)
2312 break;
2313 debug_print ("== State pop from pending list\n");
2314 // Set up the current state.
2315 current_state->copy (states[PC], current_method->max_stack,
2316 current_method->max_locals);
2318 else
2320 // Control can't fall off the end of the bytecode. We
2321 // only need to check this in the fall-through case,
2322 // because branch bounds are checked when they are
2323 // pushed.
2324 if (PC >= current_method->code_length)
2325 verify_fail ("fell off end");
2327 // We only have to do this checking in the situation where
2328 // control flow falls through from the previous
2329 // instruction. Otherwise merging is done at the time we
2330 // push the branch.
2331 if (states[PC] != NULL)
2333 // We've already visited this instruction. So merge
2334 // the states together. If this yields no change then
2335 // we don't have to re-verify. However, if the new
2336 // state is an the result of an unmerged `ret', we
2337 // must continue through it.
2338 debug_print ("== Fall through merge\n");
2339 states[PC]->print ("Old", PC, current_method->max_stack,
2340 current_method->max_locals);
2341 current_state->print ("Cur", PC, current_method->max_stack,
2342 current_method->max_locals);
2343 if (! current_state->merge (states[PC], false,
2344 current_method->max_locals, this)
2345 && ! states[PC]->is_unmerged_ret_state (current_method->max_locals))
2347 debug_print ("== Fall through optimization\n");
2348 invalidate_pc ();
2349 continue;
2351 // Save a copy of it for later.
2352 states[PC]->copy (current_state, current_method->max_stack,
2353 current_method->max_locals);
2354 current_state->print ("New", PC, current_method->max_stack,
2355 current_method->max_locals);
2359 // We only have to keep saved state at branch targets. If
2360 // we're at a branch target and the state here hasn't been set
2361 // yet, we set it now.
2362 if (states[PC] == NULL && (flags[PC] & FLAG_BRANCH_TARGET))
2364 states[PC] = new state (current_state, current_method->max_stack,
2365 current_method->max_locals);
2368 // Set this before handling exceptions so that debug output is
2369 // sane.
2370 start_PC = PC;
2372 // Update states for all active exception handlers. Ordinarily
2373 // there are not many exception handlers. So we simply run
2374 // through them all.
2375 for (int i = 0; i < current_method->exc_count; ++i)
2377 if (PC >= exception[i].start_pc.i && PC < exception[i].end_pc.i)
2379 type handler (&java::lang::Throwable::class$, this);
2380 if (exception[i].handler_type.i != 0)
2381 handler = check_class_constant (exception[i].handler_type.i);
2382 push_exception_jump (handler, exception[i].handler_pc.i);
2386 current_state->print (" ", PC, current_method->max_stack,
2387 current_method->max_locals);
2388 java_opcode opcode = (java_opcode) bytecode[PC++];
2389 switch (opcode)
2391 case op_nop:
2392 break;
2394 case op_aconst_null:
2395 push_type (null_type);
2396 break;
2398 case op_iconst_m1:
2399 case op_iconst_0:
2400 case op_iconst_1:
2401 case op_iconst_2:
2402 case op_iconst_3:
2403 case op_iconst_4:
2404 case op_iconst_5:
2405 push_type (int_type);
2406 break;
2408 case op_lconst_0:
2409 case op_lconst_1:
2410 push_type (long_type);
2411 break;
2413 case op_fconst_0:
2414 case op_fconst_1:
2415 case op_fconst_2:
2416 push_type (float_type);
2417 break;
2419 case op_dconst_0:
2420 case op_dconst_1:
2421 push_type (double_type);
2422 break;
2424 case op_bipush:
2425 get_byte ();
2426 push_type (int_type);
2427 break;
2429 case op_sipush:
2430 get_short ();
2431 push_type (int_type);
2432 break;
2434 case op_ldc:
2435 push_type (check_constant (get_byte ()));
2436 break;
2437 case op_ldc_w:
2438 push_type (check_constant (get_ushort ()));
2439 break;
2440 case op_ldc2_w:
2441 push_type (check_wide_constant (get_ushort ()));
2442 break;
2444 case op_iload:
2445 push_type (get_variable (get_byte (), int_type));
2446 break;
2447 case op_lload:
2448 push_type (get_variable (get_byte (), long_type));
2449 break;
2450 case op_fload:
2451 push_type (get_variable (get_byte (), float_type));
2452 break;
2453 case op_dload:
2454 push_type (get_variable (get_byte (), double_type));
2455 break;
2456 case op_aload:
2457 push_type (get_variable (get_byte (), reference_type));
2458 break;
2460 case op_iload_0:
2461 case op_iload_1:
2462 case op_iload_2:
2463 case op_iload_3:
2464 push_type (get_variable (opcode - op_iload_0, int_type));
2465 break;
2466 case op_lload_0:
2467 case op_lload_1:
2468 case op_lload_2:
2469 case op_lload_3:
2470 push_type (get_variable (opcode - op_lload_0, long_type));
2471 break;
2472 case op_fload_0:
2473 case op_fload_1:
2474 case op_fload_2:
2475 case op_fload_3:
2476 push_type (get_variable (opcode - op_fload_0, float_type));
2477 break;
2478 case op_dload_0:
2479 case op_dload_1:
2480 case op_dload_2:
2481 case op_dload_3:
2482 push_type (get_variable (opcode - op_dload_0, double_type));
2483 break;
2484 case op_aload_0:
2485 case op_aload_1:
2486 case op_aload_2:
2487 case op_aload_3:
2488 push_type (get_variable (opcode - op_aload_0, reference_type));
2489 break;
2490 case op_iaload:
2491 pop_type (int_type);
2492 push_type (require_array_type (pop_init_ref (reference_type),
2493 int_type));
2494 break;
2495 case op_laload:
2496 pop_type (int_type);
2497 push_type (require_array_type (pop_init_ref (reference_type),
2498 long_type));
2499 break;
2500 case op_faload:
2501 pop_type (int_type);
2502 push_type (require_array_type (pop_init_ref (reference_type),
2503 float_type));
2504 break;
2505 case op_daload:
2506 pop_type (int_type);
2507 push_type (require_array_type (pop_init_ref (reference_type),
2508 double_type));
2509 break;
2510 case op_aaload:
2511 pop_type (int_type);
2512 push_type (require_array_type (pop_init_ref (reference_type),
2513 reference_type));
2514 break;
2515 case op_baload:
2516 pop_type (int_type);
2517 require_array_type (pop_init_ref (reference_type), byte_type);
2518 push_type (int_type);
2519 break;
2520 case op_caload:
2521 pop_type (int_type);
2522 require_array_type (pop_init_ref (reference_type), char_type);
2523 push_type (int_type);
2524 break;
2525 case op_saload:
2526 pop_type (int_type);
2527 require_array_type (pop_init_ref (reference_type), short_type);
2528 push_type (int_type);
2529 break;
2530 case op_istore:
2531 set_variable (get_byte (), pop_type (int_type));
2532 break;
2533 case op_lstore:
2534 set_variable (get_byte (), pop_type (long_type));
2535 break;
2536 case op_fstore:
2537 set_variable (get_byte (), pop_type (float_type));
2538 break;
2539 case op_dstore:
2540 set_variable (get_byte (), pop_type (double_type));
2541 break;
2542 case op_astore:
2543 set_variable (get_byte (), pop_ref_or_return ());
2544 break;
2545 case op_istore_0:
2546 case op_istore_1:
2547 case op_istore_2:
2548 case op_istore_3:
2549 set_variable (opcode - op_istore_0, pop_type (int_type));
2550 break;
2551 case op_lstore_0:
2552 case op_lstore_1:
2553 case op_lstore_2:
2554 case op_lstore_3:
2555 set_variable (opcode - op_lstore_0, pop_type (long_type));
2556 break;
2557 case op_fstore_0:
2558 case op_fstore_1:
2559 case op_fstore_2:
2560 case op_fstore_3:
2561 set_variable (opcode - op_fstore_0, pop_type (float_type));
2562 break;
2563 case op_dstore_0:
2564 case op_dstore_1:
2565 case op_dstore_2:
2566 case op_dstore_3:
2567 set_variable (opcode - op_dstore_0, pop_type (double_type));
2568 break;
2569 case op_astore_0:
2570 case op_astore_1:
2571 case op_astore_2:
2572 case op_astore_3:
2573 set_variable (opcode - op_astore_0, pop_ref_or_return ());
2574 break;
2575 case op_iastore:
2576 pop_type (int_type);
2577 pop_type (int_type);
2578 require_array_type (pop_init_ref (reference_type), int_type);
2579 break;
2580 case op_lastore:
2581 pop_type (long_type);
2582 pop_type (int_type);
2583 require_array_type (pop_init_ref (reference_type), long_type);
2584 break;
2585 case op_fastore:
2586 pop_type (float_type);
2587 pop_type (int_type);
2588 require_array_type (pop_init_ref (reference_type), float_type);
2589 break;
2590 case op_dastore:
2591 pop_type (double_type);
2592 pop_type (int_type);
2593 require_array_type (pop_init_ref (reference_type), double_type);
2594 break;
2595 case op_aastore:
2596 pop_type (reference_type);
2597 pop_type (int_type);
2598 require_array_type (pop_init_ref (reference_type), reference_type);
2599 break;
2600 case op_bastore:
2601 pop_type (int_type);
2602 pop_type (int_type);
2603 require_array_type (pop_init_ref (reference_type), byte_type);
2604 break;
2605 case op_castore:
2606 pop_type (int_type);
2607 pop_type (int_type);
2608 require_array_type (pop_init_ref (reference_type), char_type);
2609 break;
2610 case op_sastore:
2611 pop_type (int_type);
2612 pop_type (int_type);
2613 require_array_type (pop_init_ref (reference_type), short_type);
2614 break;
2615 case op_pop:
2616 pop32 ();
2617 break;
2618 case op_pop2:
2620 type t = pop_raw ();
2621 if (! t.iswide ())
2622 pop32 ();
2624 break;
2625 case op_dup:
2627 type t = pop32 ();
2628 push_type (t);
2629 push_type (t);
2631 break;
2632 case op_dup_x1:
2634 type t1 = pop32 ();
2635 type t2 = pop32 ();
2636 push_type (t1);
2637 push_type (t2);
2638 push_type (t1);
2640 break;
2641 case op_dup_x2:
2643 type t1 = pop32 ();
2644 type t2 = pop_raw ();
2645 if (! t2.iswide ())
2647 type t3 = pop32 ();
2648 push_type (t1);
2649 push_type (t3);
2651 else
2652 push_type (t1);
2653 push_type (t2);
2654 push_type (t1);
2656 break;
2657 case op_dup2:
2659 type t = pop_raw ();
2660 if (! t.iswide ())
2662 type t2 = pop32 ();
2663 push_type (t2);
2664 push_type (t);
2665 push_type (t2);
2667 else
2668 push_type (t);
2669 push_type (t);
2671 break;
2672 case op_dup2_x1:
2674 type t1 = pop_raw ();
2675 type t2 = pop32 ();
2676 if (! t1.iswide ())
2678 type t3 = pop32 ();
2679 push_type (t2);
2680 push_type (t1);
2681 push_type (t3);
2683 else
2684 push_type (t1);
2685 push_type (t2);
2686 push_type (t1);
2688 break;
2689 case op_dup2_x2:
2691 type t1 = pop_raw ();
2692 if (t1.iswide ())
2694 type t2 = pop_raw ();
2695 if (t2.iswide ())
2697 push_type (t1);
2698 push_type (t2);
2700 else
2702 type t3 = pop32 ();
2703 push_type (t1);
2704 push_type (t3);
2705 push_type (t2);
2707 push_type (t1);
2709 else
2711 type t2 = pop32 ();
2712 type t3 = pop_raw ();
2713 if (t3.iswide ())
2715 push_type (t2);
2716 push_type (t1);
2718 else
2720 type t4 = pop32 ();
2721 push_type (t2);
2722 push_type (t1);
2723 push_type (t4);
2725 push_type (t3);
2726 push_type (t2);
2727 push_type (t1);
2730 break;
2731 case op_swap:
2733 type t1 = pop32 ();
2734 type t2 = pop32 ();
2735 push_type (t1);
2736 push_type (t2);
2738 break;
2739 case op_iadd:
2740 case op_isub:
2741 case op_imul:
2742 case op_idiv:
2743 case op_irem:
2744 case op_ishl:
2745 case op_ishr:
2746 case op_iushr:
2747 case op_iand:
2748 case op_ior:
2749 case op_ixor:
2750 pop_type (int_type);
2751 push_type (pop_type (int_type));
2752 break;
2753 case op_ladd:
2754 case op_lsub:
2755 case op_lmul:
2756 case op_ldiv:
2757 case op_lrem:
2758 case op_land:
2759 case op_lor:
2760 case op_lxor:
2761 pop_type (long_type);
2762 push_type (pop_type (long_type));
2763 break;
2764 case op_lshl:
2765 case op_lshr:
2766 case op_lushr:
2767 pop_type (int_type);
2768 push_type (pop_type (long_type));
2769 break;
2770 case op_fadd:
2771 case op_fsub:
2772 case op_fmul:
2773 case op_fdiv:
2774 case op_frem:
2775 pop_type (float_type);
2776 push_type (pop_type (float_type));
2777 break;
2778 case op_dadd:
2779 case op_dsub:
2780 case op_dmul:
2781 case op_ddiv:
2782 case op_drem:
2783 pop_type (double_type);
2784 push_type (pop_type (double_type));
2785 break;
2786 case op_ineg:
2787 case op_i2b:
2788 case op_i2c:
2789 case op_i2s:
2790 push_type (pop_type (int_type));
2791 break;
2792 case op_lneg:
2793 push_type (pop_type (long_type));
2794 break;
2795 case op_fneg:
2796 push_type (pop_type (float_type));
2797 break;
2798 case op_dneg:
2799 push_type (pop_type (double_type));
2800 break;
2801 case op_iinc:
2802 get_variable (get_byte (), int_type);
2803 get_byte ();
2804 break;
2805 case op_i2l:
2806 pop_type (int_type);
2807 push_type (long_type);
2808 break;
2809 case op_i2f:
2810 pop_type (int_type);
2811 push_type (float_type);
2812 break;
2813 case op_i2d:
2814 pop_type (int_type);
2815 push_type (double_type);
2816 break;
2817 case op_l2i:
2818 pop_type (long_type);
2819 push_type (int_type);
2820 break;
2821 case op_l2f:
2822 pop_type (long_type);
2823 push_type (float_type);
2824 break;
2825 case op_l2d:
2826 pop_type (long_type);
2827 push_type (double_type);
2828 break;
2829 case op_f2i:
2830 pop_type (float_type);
2831 push_type (int_type);
2832 break;
2833 case op_f2l:
2834 pop_type (float_type);
2835 push_type (long_type);
2836 break;
2837 case op_f2d:
2838 pop_type (float_type);
2839 push_type (double_type);
2840 break;
2841 case op_d2i:
2842 pop_type (double_type);
2843 push_type (int_type);
2844 break;
2845 case op_d2l:
2846 pop_type (double_type);
2847 push_type (long_type);
2848 break;
2849 case op_d2f:
2850 pop_type (double_type);
2851 push_type (float_type);
2852 break;
2853 case op_lcmp:
2854 pop_type (long_type);
2855 pop_type (long_type);
2856 push_type (int_type);
2857 break;
2858 case op_fcmpl:
2859 case op_fcmpg:
2860 pop_type (float_type);
2861 pop_type (float_type);
2862 push_type (int_type);
2863 break;
2864 case op_dcmpl:
2865 case op_dcmpg:
2866 pop_type (double_type);
2867 pop_type (double_type);
2868 push_type (int_type);
2869 break;
2870 case op_ifeq:
2871 case op_ifne:
2872 case op_iflt:
2873 case op_ifge:
2874 case op_ifgt:
2875 case op_ifle:
2876 pop_type (int_type);
2877 push_jump (get_short ());
2878 break;
2879 case op_if_icmpeq:
2880 case op_if_icmpne:
2881 case op_if_icmplt:
2882 case op_if_icmpge:
2883 case op_if_icmpgt:
2884 case op_if_icmple:
2885 pop_type (int_type);
2886 pop_type (int_type);
2887 push_jump (get_short ());
2888 break;
2889 case op_if_acmpeq:
2890 case op_if_acmpne:
2891 pop_type (reference_type);
2892 pop_type (reference_type);
2893 push_jump (get_short ());
2894 break;
2895 case op_goto:
2896 push_jump (get_short ());
2897 invalidate_pc ();
2898 break;
2899 case op_jsr:
2900 handle_jsr_insn (get_short ());
2901 break;
2902 case op_ret:
2903 handle_ret_insn (get_byte ());
2904 break;
2905 case op_tableswitch:
2907 pop_type (int_type);
2908 skip_padding ();
2909 push_jump (get_int ());
2910 jint low = get_int ();
2911 jint high = get_int ();
2912 // Already checked LOW -vs- HIGH.
2913 for (int i = low; i <= high; ++i)
2914 push_jump (get_int ());
2915 invalidate_pc ();
2917 break;
2919 case op_lookupswitch:
2921 pop_type (int_type);
2922 skip_padding ();
2923 push_jump (get_int ());
2924 jint npairs = get_int ();
2925 // Already checked NPAIRS >= 0.
2926 jint lastkey = 0;
2927 for (int i = 0; i < npairs; ++i)
2929 jint key = get_int ();
2930 if (i > 0 && key <= lastkey)
2931 verify_fail ("lookupswitch pairs unsorted", start_PC);
2932 lastkey = key;
2933 push_jump (get_int ());
2935 invalidate_pc ();
2937 break;
2938 case op_ireturn:
2939 check_return_type (pop_type (int_type));
2940 invalidate_pc ();
2941 break;
2942 case op_lreturn:
2943 check_return_type (pop_type (long_type));
2944 invalidate_pc ();
2945 break;
2946 case op_freturn:
2947 check_return_type (pop_type (float_type));
2948 invalidate_pc ();
2949 break;
2950 case op_dreturn:
2951 check_return_type (pop_type (double_type));
2952 invalidate_pc ();
2953 break;
2954 case op_areturn:
2955 check_return_type (pop_init_ref (reference_type));
2956 invalidate_pc ();
2957 break;
2958 case op_return:
2959 // We only need to check this when the return type is
2960 // void, because all instance initializers return void.
2961 if (this_is_init)
2962 current_state->check_this_initialized (this);
2963 check_return_type (void_type);
2964 invalidate_pc ();
2965 break;
2966 case op_getstatic:
2967 push_type (check_field_constant (get_ushort ()));
2968 break;
2969 case op_putstatic:
2970 pop_type (check_field_constant (get_ushort ()));
2971 break;
2972 case op_getfield:
2974 type klass;
2975 type field = check_field_constant (get_ushort (), &klass);
2976 pop_type (klass);
2977 push_type (field);
2979 break;
2980 case op_putfield:
2982 type klass;
2983 type field = check_field_constant (get_ushort (), &klass);
2984 pop_type (field);
2986 // We have an obscure special case here: we can use
2987 // `putfield' on a field declared in this class, even if
2988 // `this' has not yet been initialized.
2989 if (! current_state->this_type.isinitialized ()
2990 && current_state->this_type.pc == type::SELF)
2991 klass.set_uninitialized (type::SELF, this);
2992 pop_type (klass);
2994 break;
2996 case op_invokevirtual:
2997 case op_invokespecial:
2998 case op_invokestatic:
2999 case op_invokeinterface:
3001 _Jv_Utf8Const *method_name, *method_signature;
3002 type class_type
3003 = check_method_constant (get_ushort (),
3004 opcode == op_invokeinterface,
3005 &method_name,
3006 &method_signature);
3007 // NARGS is only used when we're processing
3008 // invokeinterface. It is simplest for us to compute it
3009 // here and then verify it later.
3010 int nargs = 0;
3011 if (opcode == op_invokeinterface)
3013 nargs = get_byte ();
3014 if (get_byte () != 0)
3015 verify_fail ("invokeinterface dummy byte is wrong");
3018 bool is_init = false;
3019 if (_Jv_equalUtf8Consts (method_name, gcj::init_name))
3021 is_init = true;
3022 if (opcode != op_invokespecial)
3023 verify_fail ("can't invoke <init>");
3025 else if (method_name->data[0] == '<')
3026 verify_fail ("can't invoke method starting with `<'");
3028 // Pop arguments and check types.
3029 int arg_count = _Jv_count_arguments (method_signature);
3030 type arg_types[arg_count];
3031 compute_argument_types (method_signature, arg_types);
3032 for (int i = arg_count - 1; i >= 0; --i)
3034 // This is only used for verifying the byte for
3035 // invokeinterface.
3036 nargs -= arg_types[i].depth ();
3037 pop_init_ref (arg_types[i]);
3040 if (opcode == op_invokeinterface
3041 && nargs != 1)
3042 verify_fail ("wrong argument count for invokeinterface");
3044 if (opcode != op_invokestatic)
3046 type t = class_type;
3047 if (is_init)
3049 // In this case the PC doesn't matter.
3050 t.set_uninitialized (type::UNINIT, this);
3051 // FIXME: check to make sure that the <init>
3052 // call is to the right class.
3053 // It must either be super or an exact class
3054 // match.
3056 type raw = pop_raw ();
3057 if (! t.compatible (raw, this))
3058 verify_fail ("incompatible type on stack");
3060 if (is_init)
3061 current_state->set_initialized (raw.get_pc (),
3062 current_method->max_locals);
3065 type rt = compute_return_type (method_signature);
3066 if (! rt.isvoid ())
3067 push_type (rt);
3069 break;
3071 case op_new:
3073 type t = check_class_constant (get_ushort ());
3074 if (t.isarray () || t.isinterface (this) || t.isabstract (this))
3075 verify_fail ("type is array, interface, or abstract");
3076 t.set_uninitialized (start_PC, this);
3077 push_type (t);
3079 break;
3081 case op_newarray:
3083 int atype = get_byte ();
3084 // We intentionally have chosen constants to make this
3085 // valid.
3086 if (atype < boolean_type || atype > long_type)
3087 verify_fail ("type not primitive", start_PC);
3088 pop_type (int_type);
3089 type t (construct_primitive_array_type (type_val (atype)), this);
3090 push_type (t);
3092 break;
3093 case op_anewarray:
3094 pop_type (int_type);
3095 push_type (check_class_constant (get_ushort ()).to_array (this));
3096 break;
3097 case op_arraylength:
3099 type t = pop_init_ref (reference_type);
3100 if (! t.isarray () && ! t.isnull ())
3101 verify_fail ("array type expected");
3102 push_type (int_type);
3104 break;
3105 case op_athrow:
3106 pop_type (type (&java::lang::Throwable::class$, this));
3107 invalidate_pc ();
3108 break;
3109 case op_checkcast:
3110 pop_init_ref (reference_type);
3111 push_type (check_class_constant (get_ushort ()));
3112 break;
3113 case op_instanceof:
3114 pop_init_ref (reference_type);
3115 check_class_constant (get_ushort ());
3116 push_type (int_type);
3117 break;
3118 case op_monitorenter:
3119 pop_init_ref (reference_type);
3120 break;
3121 case op_monitorexit:
3122 pop_init_ref (reference_type);
3123 break;
3124 case op_wide:
3126 switch (get_byte ())
3128 case op_iload:
3129 push_type (get_variable (get_ushort (), int_type));
3130 break;
3131 case op_lload:
3132 push_type (get_variable (get_ushort (), long_type));
3133 break;
3134 case op_fload:
3135 push_type (get_variable (get_ushort (), float_type));
3136 break;
3137 case op_dload:
3138 push_type (get_variable (get_ushort (), double_type));
3139 break;
3140 case op_aload:
3141 push_type (get_variable (get_ushort (), reference_type));
3142 break;
3143 case op_istore:
3144 set_variable (get_ushort (), pop_type (int_type));
3145 break;
3146 case op_lstore:
3147 set_variable (get_ushort (), pop_type (long_type));
3148 break;
3149 case op_fstore:
3150 set_variable (get_ushort (), pop_type (float_type));
3151 break;
3152 case op_dstore:
3153 set_variable (get_ushort (), pop_type (double_type));
3154 break;
3155 case op_astore:
3156 set_variable (get_ushort (), pop_init_ref (reference_type));
3157 break;
3158 case op_ret:
3159 handle_ret_insn (get_short ());
3160 break;
3161 case op_iinc:
3162 get_variable (get_ushort (), int_type);
3163 get_short ();
3164 break;
3165 default:
3166 verify_fail ("unrecognized wide instruction", start_PC);
3169 break;
3170 case op_multianewarray:
3172 type atype = check_class_constant (get_ushort ());
3173 int dim = get_byte ();
3174 if (dim < 1)
3175 verify_fail ("too few dimensions to multianewarray", start_PC);
3176 atype.verify_dimensions (dim, this);
3177 for (int i = 0; i < dim; ++i)
3178 pop_type (int_type);
3179 push_type (atype);
3181 break;
3182 case op_ifnull:
3183 case op_ifnonnull:
3184 pop_type (reference_type);
3185 push_jump (get_short ());
3186 break;
3187 case op_goto_w:
3188 push_jump (get_int ());
3189 invalidate_pc ();
3190 break;
3191 case op_jsr_w:
3192 handle_jsr_insn (get_int ());
3193 break;
3195 // These are unused here, but we call them out explicitly
3196 // so that -Wswitch-enum doesn't complain.
3197 case op_putfield_1:
3198 case op_putfield_2:
3199 case op_putfield_4:
3200 case op_putfield_8:
3201 case op_putfield_a:
3202 case op_putstatic_1:
3203 case op_putstatic_2:
3204 case op_putstatic_4:
3205 case op_putstatic_8:
3206 case op_putstatic_a:
3207 case op_getfield_1:
3208 case op_getfield_2s:
3209 case op_getfield_2u:
3210 case op_getfield_4:
3211 case op_getfield_8:
3212 case op_getfield_a:
3213 case op_getstatic_1:
3214 case op_getstatic_2s:
3215 case op_getstatic_2u:
3216 case op_getstatic_4:
3217 case op_getstatic_8:
3218 case op_getstatic_a:
3219 default:
3220 // Unrecognized opcode.
3221 verify_fail ("unrecognized instruction in verify_instructions_0",
3222 start_PC);
3227 public:
3229 void verify_instructions ()
3231 branch_prepass ();
3232 verify_instructions_0 ();
3235 _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
3237 // We just print the text as utf-8. This is just for debugging
3238 // anyway.
3239 debug_print ("--------------------------------\n");
3240 debug_print ("-- Verifying method `%s'\n", m->self->name->data);
3242 current_method = m;
3243 bytecode = m->bytecode ();
3244 exception = m->exceptions ();
3245 current_class = m->defining_class;
3247 states = NULL;
3248 flags = NULL;
3249 jsr_ptrs = NULL;
3250 utf8_list = NULL;
3251 isect_list = NULL;
3252 entry_points = NULL;
3255 ~_Jv_BytecodeVerifier ()
3257 if (states)
3258 _Jv_Free (states);
3259 if (flags)
3260 _Jv_Free (flags);
3262 if (jsr_ptrs)
3264 for (int i = 0; i < current_method->code_length; ++i)
3266 if (jsr_ptrs[i] != NULL)
3268 subr_info *info = jsr_ptrs[i];
3269 while (info != NULL)
3271 subr_info *next = info->next;
3272 _Jv_Free (info);
3273 info = next;
3277 _Jv_Free (jsr_ptrs);
3280 while (utf8_list != NULL)
3282 linked_utf8 *n = utf8_list->next;
3283 _Jv_Free (utf8_list->val);
3284 _Jv_Free (utf8_list);
3285 utf8_list = n;
3288 while (entry_points != NULL)
3290 subr_entry_info *next = entry_points->next;
3291 _Jv_Free (entry_points);
3292 entry_points = next;
3295 while (isect_list != NULL)
3297 ref_intersection *next = isect_list->alloc_next;
3298 delete isect_list;
3299 isect_list = next;
3304 void
3305 _Jv_VerifyMethod (_Jv_InterpMethod *meth)
3307 _Jv_BytecodeVerifier v (meth);
3308 v.verify_instructions ();
3310 #endif /* INTERPRETER */