* c-decl.c (c_expand_body): Update call tree_rest_of_compilation.
[official-gcc.git] / libjava / verify.cc
blob85a50aaba679c2af84ab5cc2bce207124c983b97
1 // verify.cc - verify bytecode
3 /* Copyright (C) 2001, 2002, 2003, 2004 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 // On Solaris 10/x86, <signal.h> indirectly includes <ia32/sys/reg.h>, which
23 // defines PC since g++ predefines __EXTENSIONS__. Undef here to avoid clash
24 // with PC member of class _Jv_BytecodeVerifier below.
25 #undef PC
27 #ifdef INTERPRETER
29 #include <java/lang/Class.h>
30 #include <java/lang/VerifyError.h>
31 #include <java/lang/Throwable.h>
32 #include <java/lang/reflect/Modifier.h>
33 #include <java/lang/StringBuffer.h>
35 #ifdef VERIFY_DEBUG
36 #include <stdio.h>
37 #endif /* VERIFY_DEBUG */
40 // This is used to mark states which are not scheduled for
41 // verification.
42 #define INVALID_STATE ((state *) -1)
44 static void debug_print (const char *fmt, ...)
45 __attribute__ ((format (printf, 1, 2)));
47 static inline void
48 debug_print (MAYBE_UNUSED const char *fmt, ...)
50 #ifdef VERIFY_DEBUG
51 va_list ap;
52 va_start (ap, fmt);
53 vfprintf (stderr, fmt, ap);
54 va_end (ap);
55 #endif /* VERIFY_DEBUG */
58 // This started as a fairly ordinary verifier, and for the most part
59 // it remains so. It works in the obvious way, by modeling the effect
60 // of each opcode as it is encountered. For most opcodes, this is a
61 // straightforward operation.
63 // This verifier does not do type merging. It used to, but this
64 // results in difficulty verifying some relatively simple code
65 // involving interfaces, and it pushed some verification work into the
66 // interpreter.
68 // Instead of merging reference types, when we reach a point where two
69 // flows of control merge, we simply keep the union of reference types
70 // from each branch. Then, when we need to verify a fact about a
71 // reference on the stack (e.g., that it is compatible with the
72 // argument type of a method), we check to ensure that all possible
73 // types satisfy the requirement.
75 // Another area this verifier differs from the norm is in its handling
76 // of subroutines. The JVM specification has some confusing things to
77 // say about subroutines. For instance, it makes claims about not
78 // allowing subroutines to merge and it rejects recursive subroutines.
79 // For the most part these are red herrings; we used to try to follow
80 // these things but they lead to problems. For example, the notion of
81 // "being in a subroutine" is not well-defined: is an exception
82 // handler in a subroutine? If you never execute the `ret' but
83 // instead `goto 1' do you remain in the subroutine?
85 // For clarity on what is really required for type safety, read
86 // "Simple Verification Technique for Complex Java Bytecode
87 // Subroutines" by Alessandro Coglio. Among other things this paper
88 // shows that recursive subroutines are not harmful to type safety.
89 // We implement something similar to what he proposes. Note that this
90 // means that this verifier will accept code that is rejected by some
91 // other verifiers.
93 // For those not wanting to read the paper, the basic observation is
94 // that we can maintain split states in subroutines. We maintain one
95 // state for each calling `jsr'. In other words, we re-verify a
96 // subroutine once for each caller, using the exact types held by the
97 // callers (as opposed to the old approach of merging types and
98 // keeping a bitmap registering what did or did not change). This
99 // approach lets us continue to verify correctly even when a
100 // subroutine is exited via `goto' or `athrow' and not `ret'.
102 // In some other areas the JVM specification is (mildly) incorrect,
103 // but we still implement what is specified. For instance, you cannot
104 // violate type safety by allocating an object with `new' and then
105 // failing to initialize it, no matter how one branches or where one
106 // stores the uninitialized reference. See "Improving the official
107 // specification of Java bytecode verification" by Alessandro Coglio.
108 // Similarly, there's no real point in enforcing that padding bytes or
109 // the mystery byte of invokeinterface must be 0, but we do that too.
111 // The verifier is currently neither completely lazy nor eager when it
112 // comes to loading classes. It tries to represent types by name when
113 // possible, and then loads them when it needs to verify a fact about
114 // the type. Checking types by name is valid because we only use
115 // names which come from the current class' constant pool. Since all
116 // such names are looked up using the same class loader, there is no
117 // danger that we might be fooled into comparing different types with
118 // the same name.
120 // In the future we plan to allow for a completely lazy mode of
121 // operation, where the verifier will construct a list of type
122 // assertions to be checked later.
124 // Some test cases for the verifier live in the "verify" module of the
125 // Mauve test suite. However, some of these are presently
126 // (2004-01-20) believed to be incorrect. (More precisely the notion
127 // of "correct" is not well-defined, and this verifier differs from
128 // others while remaining type-safe.) Some other tests live in the
129 // libgcj test suite.
130 class _Jv_BytecodeVerifier
132 private:
134 static const int FLAG_INSN_START = 1;
135 static const int FLAG_BRANCH_TARGET = 2;
137 struct state;
138 struct type;
139 struct linked_utf8;
140 struct ref_intersection;
142 template<typename T>
143 struct linked
145 T *val;
146 linked<T> *next;
149 // The current PC.
150 int PC;
151 // The PC corresponding to the start of the current instruction.
152 int start_PC;
154 // The current state of the stack, locals, etc.
155 state *current_state;
157 // At each branch target we keep a linked list of all the states we
158 // can process at that point. We'll only have multiple states at a
159 // given PC if they both have different return-address types in the
160 // same stack or local slot. This array is indexed by PC and holds
161 // the list of all such states.
162 linked<state> **states;
164 // We keep a linked list of all the states which we must reverify.
165 // This is the head of the list.
166 state *next_verify_state;
168 // We keep some flags for each instruction. The values are the
169 // FLAG_* constants defined above. This is an array indexed by PC.
170 char *flags;
172 // The bytecode itself.
173 unsigned char *bytecode;
174 // The exceptions.
175 _Jv_InterpException *exception;
177 // Defining class.
178 jclass current_class;
179 // This method.
180 _Jv_InterpMethod *current_method;
182 // A linked list of utf8 objects we allocate.
183 linked<_Jv_Utf8Const> *utf8_list;
185 // A linked list of all ref_intersection objects we allocate.
186 ref_intersection *isect_list;
188 // Create a new Utf-8 constant and return it. We do this to avoid
189 // having our Utf-8 constants prematurely collected.
190 _Jv_Utf8Const *make_utf8_const (char *s, int len)
192 linked<_Jv_Utf8Const> *lu = (linked<_Jv_Utf8Const> *)
193 _Jv_Malloc (sizeof (linked<_Jv_Utf8Const>)
194 + _Jv_Utf8Const::space_needed(s, len));
195 _Jv_Utf8Const *r = (_Jv_Utf8Const *) (lu + 1);
196 r->init(s, len);
197 lu->val = r;
198 lu->next = utf8_list;
199 utf8_list = lu;
201 return r;
204 __attribute__ ((__noreturn__)) void verify_fail (char *s, jint pc = -1)
206 using namespace java::lang;
207 StringBuffer *buf = new StringBuffer ();
209 buf->append (JvNewStringLatin1 ("verification failed"));
210 if (pc == -1)
211 pc = start_PC;
212 if (pc != -1)
214 buf->append (JvNewStringLatin1 (" at PC "));
215 buf->append (pc);
218 _Jv_InterpMethod *method = current_method;
219 buf->append (JvNewStringLatin1 (" in "));
220 buf->append (current_class->getName());
221 buf->append ((jchar) ':');
222 buf->append (method->get_method()->name->toString());
223 buf->append ((jchar) '(');
224 buf->append (method->get_method()->signature->toString());
225 buf->append ((jchar) ')');
227 buf->append (JvNewStringLatin1 (": "));
228 buf->append (JvNewStringLatin1 (s));
229 throw new java::lang::VerifyError (buf->toString ());
232 // This enum holds a list of tags for all the different types we
233 // need to handle. Reference types are treated specially by the
234 // type class.
235 enum type_val
237 void_type,
239 // The values for primitive types are chosen to correspond to values
240 // specified to newarray.
241 boolean_type = 4,
242 char_type = 5,
243 float_type = 6,
244 double_type = 7,
245 byte_type = 8,
246 short_type = 9,
247 int_type = 10,
248 long_type = 11,
250 // Used when overwriting second word of a double or long in the
251 // local variables. Also used after merging local variable states
252 // to indicate an unusable value.
253 unsuitable_type,
254 return_address_type,
255 // This is the second word of a two-word value, i.e., a double or
256 // a long.
257 continuation_type,
259 // Everything after `reference_type' must be a reference type.
260 reference_type,
261 null_type,
262 uninitialized_reference_type
265 // This represents a merged class type. Some verifiers (including
266 // earlier versions of this one) will compute the intersection of
267 // two class types when merging states. However, this loses
268 // critical information about interfaces implemented by the various
269 // classes. So instead we keep track of all the actual classes that
270 // have been merged.
271 struct ref_intersection
273 // Whether or not this type has been resolved.
274 bool is_resolved;
276 // Actual type data.
277 union
279 // For a resolved reference type, this is a pointer to the class.
280 jclass klass;
281 // For other reference types, this it the name of the class.
282 _Jv_Utf8Const *name;
283 } data;
285 // Link to the next reference in the intersection.
286 ref_intersection *ref_next;
288 // This is used to keep track of all the allocated
289 // ref_intersection objects, so we can free them.
290 // FIXME: we should allocate these in chunks.
291 ref_intersection *alloc_next;
293 ref_intersection (jclass klass, _Jv_BytecodeVerifier *verifier)
294 : ref_next (NULL)
296 is_resolved = true;
297 data.klass = klass;
298 alloc_next = verifier->isect_list;
299 verifier->isect_list = this;
302 ref_intersection (_Jv_Utf8Const *name, _Jv_BytecodeVerifier *verifier)
303 : ref_next (NULL)
305 is_resolved = false;
306 data.name = name;
307 alloc_next = verifier->isect_list;
308 verifier->isect_list = this;
311 ref_intersection (ref_intersection *dup, ref_intersection *tail,
312 _Jv_BytecodeVerifier *verifier)
313 : ref_next (tail)
315 is_resolved = dup->is_resolved;
316 data = dup->data;
317 alloc_next = verifier->isect_list;
318 verifier->isect_list = this;
321 bool equals (ref_intersection *other, _Jv_BytecodeVerifier *verifier)
323 if (! is_resolved && ! other->is_resolved
324 && _Jv_equalUtf8Consts (data.name, other->data.name))
325 return true;
326 if (! is_resolved)
327 resolve (verifier);
328 if (! other->is_resolved)
329 other->resolve (verifier);
330 return data.klass == other->data.klass;
333 // Merge THIS type into OTHER, returning the result. This will
334 // return OTHER if all the classes in THIS already appear in
335 // OTHER.
336 ref_intersection *merge (ref_intersection *other,
337 _Jv_BytecodeVerifier *verifier)
339 ref_intersection *tail = other;
340 for (ref_intersection *self = this; self != NULL; self = self->ref_next)
342 bool add = true;
343 for (ref_intersection *iter = other; iter != NULL;
344 iter = iter->ref_next)
346 if (iter->equals (self, verifier))
348 add = false;
349 break;
353 if (add)
354 tail = new ref_intersection (self, tail, verifier);
356 return tail;
359 void resolve (_Jv_BytecodeVerifier *verifier)
361 if (is_resolved)
362 return;
364 using namespace java::lang;
365 java::lang::ClassLoader *loader
366 = verifier->current_class->getClassLoaderInternal();
367 // We might see either kind of name. Sigh.
368 if (data.name->first() == 'L' && data.name->limit()[-1] == ';')
369 data.klass = _Jv_FindClassFromSignature (data.name->chars(), loader);
370 else
371 data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
372 false, loader);
373 is_resolved = true;
376 // See if an object of type OTHER can be assigned to an object of
377 // type *THIS. This might resolve classes in one chain or the
378 // other.
379 bool compatible (ref_intersection *other,
380 _Jv_BytecodeVerifier *verifier)
382 ref_intersection *self = this;
384 for (; self != NULL; self = self->ref_next)
386 ref_intersection *other_iter = other;
388 for (; other_iter != NULL; other_iter = other_iter->ref_next)
390 // Avoid resolving if possible.
391 if (! self->is_resolved
392 && ! other_iter->is_resolved
393 && _Jv_equalUtf8Consts (self->data.name,
394 other_iter->data.name))
395 continue;
397 if (! self->is_resolved)
398 self->resolve(verifier);
399 if (! other_iter->is_resolved)
400 other_iter->resolve(verifier);
402 if (! is_assignable_from_slow (self->data.klass,
403 other_iter->data.klass))
404 return false;
408 return true;
411 bool isarray ()
413 // assert (ref_next == NULL);
414 if (is_resolved)
415 return data.klass->isArray ();
416 else
417 return data.name->first() == '[';
420 bool isinterface (_Jv_BytecodeVerifier *verifier)
422 // assert (ref_next == NULL);
423 if (! is_resolved)
424 resolve (verifier);
425 return data.klass->isInterface ();
428 bool isabstract (_Jv_BytecodeVerifier *verifier)
430 // assert (ref_next == NULL);
431 if (! is_resolved)
432 resolve (verifier);
433 using namespace java::lang::reflect;
434 return Modifier::isAbstract (data.klass->getModifiers ());
437 jclass getclass (_Jv_BytecodeVerifier *verifier)
439 if (! is_resolved)
440 resolve (verifier);
441 return data.klass;
444 int count_dimensions ()
446 int ndims = 0;
447 if (is_resolved)
449 jclass k = data.klass;
450 while (k->isArray ())
452 k = k->getComponentType ();
453 ++ndims;
456 else
458 char *p = data.name->chars();
459 while (*p++ == '[')
460 ++ndims;
462 return ndims;
465 void *operator new (size_t bytes)
467 return _Jv_Malloc (bytes);
470 void operator delete (void *mem)
472 _Jv_Free (mem);
476 // Return the type_val corresponding to a primitive signature
477 // character. For instance `I' returns `int.class'.
478 type_val get_type_val_for_signature (jchar sig)
480 type_val rt;
481 switch (sig)
483 case 'Z':
484 rt = boolean_type;
485 break;
486 case 'B':
487 rt = byte_type;
488 break;
489 case 'C':
490 rt = char_type;
491 break;
492 case 'S':
493 rt = short_type;
494 break;
495 case 'I':
496 rt = int_type;
497 break;
498 case 'J':
499 rt = long_type;
500 break;
501 case 'F':
502 rt = float_type;
503 break;
504 case 'D':
505 rt = double_type;
506 break;
507 case 'V':
508 rt = void_type;
509 break;
510 default:
511 verify_fail ("invalid signature");
513 return rt;
516 // Return the type_val corresponding to a primitive class.
517 type_val get_type_val_for_signature (jclass k)
519 return get_type_val_for_signature ((jchar) k->method_count);
522 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
523 // TARGET haven't been prepared.
524 static bool is_assignable_from_slow (jclass target, jclass source)
526 // First, strip arrays.
527 while (target->isArray ())
529 // If target is array, source must be as well.
530 if (! source->isArray ())
531 return false;
532 target = target->getComponentType ();
533 source = source->getComponentType ();
536 // Quick success.
537 if (target == &java::lang::Object::class$)
538 return true;
542 if (source == target)
543 return true;
545 if (target->isPrimitive () || source->isPrimitive ())
546 return false;
548 if (target->isInterface ())
550 for (int i = 0; i < source->interface_count; ++i)
552 // We use a recursive call because we also need to
553 // check superinterfaces.
554 if (is_assignable_from_slow (target, source->interfaces[i]))
555 return true;
558 source = source->getSuperclass ();
560 while (source != NULL);
562 return false;
565 // The `type' class is used to represent a single type in the
566 // verifier.
567 struct type
569 // The type key.
570 type_val key;
572 // For reference types, the representation of the type.
573 ref_intersection *klass;
575 // This is used in two situations.
577 // First, when constructing a new object, it is the PC of the
578 // `new' instruction which created the object. We use the special
579 // value UNINIT to mean that this is uninitialized, and the
580 // special value SELF for the case where the current method is
581 // itself the <init> method.
583 // Second, when the key is return_address_type, this holds the PC
584 // of the instruction following the `jsr'.
585 int pc;
587 static const int UNINIT = -2;
588 static const int SELF = -1;
590 // Basic constructor.
591 type ()
593 key = unsuitable_type;
594 klass = NULL;
595 pc = UNINIT;
598 // Make a new instance given the type tag. We assume a generic
599 // `reference_type' means Object.
600 type (type_val k)
602 key = k;
603 // For reference_type, if KLASS==NULL then that means we are
604 // looking for a generic object of any kind, including an
605 // uninitialized reference.
606 klass = NULL;
607 pc = UNINIT;
610 // Make a new instance given a class.
611 type (jclass k, _Jv_BytecodeVerifier *verifier)
613 key = reference_type;
614 klass = new ref_intersection (k, verifier);
615 pc = UNINIT;
618 // Make a new instance given the name of a class.
619 type (_Jv_Utf8Const *n, _Jv_BytecodeVerifier *verifier)
621 key = reference_type;
622 klass = new ref_intersection (n, verifier);
623 pc = UNINIT;
626 // Copy constructor.
627 type (const type &t)
629 key = t.key;
630 klass = t.klass;
631 pc = t.pc;
634 // These operators are required because libgcj can't link in
635 // -lstdc++.
636 void *operator new[] (size_t bytes)
638 return _Jv_Malloc (bytes);
641 void operator delete[] (void *mem)
643 _Jv_Free (mem);
646 type& operator= (type_val k)
648 key = k;
649 klass = NULL;
650 pc = UNINIT;
651 return *this;
654 type& operator= (const type& t)
656 key = t.key;
657 klass = t.klass;
658 pc = t.pc;
659 return *this;
662 // Promote a numeric type.
663 type &promote ()
665 if (key == boolean_type || key == char_type
666 || key == byte_type || key == short_type)
667 key = int_type;
668 return *this;
671 // Mark this type as the uninitialized result of `new'.
672 void set_uninitialized (int npc, _Jv_BytecodeVerifier *verifier)
674 if (key == reference_type)
675 key = uninitialized_reference_type;
676 else
677 verifier->verify_fail ("internal error in type::uninitialized");
678 pc = npc;
681 // Mark this type as now initialized.
682 void set_initialized (int npc)
684 if (npc != UNINIT && pc == npc && key == uninitialized_reference_type)
686 key = reference_type;
687 pc = UNINIT;
691 // Mark this type as a particular return address.
692 void set_return_address (int npc)
694 pc = npc;
697 // Return true if this type and type OTHER are considered
698 // mergeable for the purposes of state merging. This is related
699 // to subroutine handling. For this purpose two types are
700 // considered unmergeable if they are both return-addresses but
701 // have different PCs.
702 bool state_mergeable_p (const type &other) const
704 return (key != return_address_type
705 || other.key != return_address_type
706 || pc == other.pc);
709 // Return true if an object of type K can be assigned to a variable
710 // of type *THIS. Handle various special cases too. Might modify
711 // *THIS or K. Note however that this does not perform numeric
712 // promotion.
713 bool compatible (type &k, _Jv_BytecodeVerifier *verifier)
715 // Any type is compatible with the unsuitable type.
716 if (key == unsuitable_type)
717 return true;
719 if (key < reference_type || k.key < reference_type)
720 return key == k.key;
722 // The `null' type is convertible to any initialized reference
723 // type.
724 if (key == null_type)
725 return k.key != uninitialized_reference_type;
726 if (k.key == null_type)
727 return key != uninitialized_reference_type;
729 // A special case for a generic reference.
730 if (klass == NULL)
731 return true;
732 if (k.klass == NULL)
733 verifier->verify_fail ("programmer error in type::compatible");
735 // An initialized type and an uninitialized type are not
736 // compatible.
737 if (isinitialized () != k.isinitialized ())
738 return false;
740 // Two uninitialized objects are compatible if either:
741 // * The PCs are identical, or
742 // * One PC is UNINIT.
743 if (! isinitialized ())
745 if (pc != k.pc && pc != UNINIT && k.pc != UNINIT)
746 return false;
749 return klass->compatible(k.klass, verifier);
752 bool isvoid () const
754 return key == void_type;
757 bool iswide () const
759 return key == long_type || key == double_type;
762 // Return number of stack or local variable slots taken by this
763 // type.
764 int depth () const
766 return iswide () ? 2 : 1;
769 bool isarray () const
771 // We treat null_type as not an array. This is ok based on the
772 // current uses of this method.
773 if (key == reference_type)
774 return klass->isarray ();
775 return false;
778 bool isnull () const
780 return key == null_type;
783 bool isinterface (_Jv_BytecodeVerifier *verifier)
785 if (key != reference_type)
786 return false;
787 return klass->isinterface (verifier);
790 bool isabstract (_Jv_BytecodeVerifier *verifier)
792 if (key != reference_type)
793 return false;
794 return klass->isabstract (verifier);
797 // Return the element type of an array.
798 type element_type (_Jv_BytecodeVerifier *verifier)
800 if (key != reference_type)
801 verifier->verify_fail ("programmer error in type::element_type()", -1);
803 jclass k = klass->getclass (verifier)->getComponentType ();
804 if (k->isPrimitive ())
805 return type (verifier->get_type_val_for_signature (k));
806 return type (k, verifier);
809 // Return the array type corresponding to an initialized
810 // reference. We could expand this to work for other kinds of
811 // types, but currently we don't need to.
812 type to_array (_Jv_BytecodeVerifier *verifier)
814 if (key != reference_type)
815 verifier->verify_fail ("internal error in type::to_array()");
817 jclass k = klass->getclass (verifier);
818 return type (_Jv_GetArrayClass (k, k->getClassLoaderInternal()),
819 verifier);
822 bool isreference () const
824 return key >= reference_type;
827 int get_pc () const
829 return pc;
832 bool isinitialized () const
834 return key == reference_type || key == null_type;
837 bool isresolved () const
839 return (key == reference_type
840 || key == null_type
841 || key == uninitialized_reference_type);
844 void verify_dimensions (int ndims, _Jv_BytecodeVerifier *verifier)
846 // The way this is written, we don't need to check isarray().
847 if (key != reference_type)
848 verifier->verify_fail ("internal error in verify_dimensions:"
849 " not a reference type");
851 if (klass->count_dimensions () < ndims)
852 verifier->verify_fail ("array type has fewer dimensions"
853 " than required");
856 // Merge OLD_TYPE into this. On error throw exception. Return
857 // true if the merge caused a type change.
858 bool merge (type& old_type, bool local_semantics,
859 _Jv_BytecodeVerifier *verifier)
861 bool changed = false;
862 bool refo = old_type.isreference ();
863 bool refn = isreference ();
864 if (refo && refn)
866 if (old_type.key == null_type)
868 else if (key == null_type)
870 *this = old_type;
871 changed = true;
873 else if (isinitialized () != old_type.isinitialized ())
874 verifier->verify_fail ("merging initialized and uninitialized types");
875 else
877 if (! isinitialized ())
879 if (pc == UNINIT)
880 pc = old_type.pc;
881 else if (old_type.pc == UNINIT)
883 else if (pc != old_type.pc)
884 verifier->verify_fail ("merging different uninitialized types");
887 ref_intersection *merged = old_type.klass->merge (klass,
888 verifier);
889 if (merged != klass)
891 klass = merged;
892 changed = true;
896 else if (refo || refn || key != old_type.key)
898 if (local_semantics)
900 // If we already have an `unsuitable' type, then we
901 // don't need to change again.
902 if (key != unsuitable_type)
904 key = unsuitable_type;
905 changed = true;
908 else
909 verifier->verify_fail ("unmergeable type");
911 return changed;
914 #ifdef VERIFY_DEBUG
915 void print (void) const
917 char c = '?';
918 switch (key)
920 case boolean_type: c = 'Z'; break;
921 case byte_type: c = 'B'; break;
922 case char_type: c = 'C'; break;
923 case short_type: c = 'S'; break;
924 case int_type: c = 'I'; break;
925 case long_type: c = 'J'; break;
926 case float_type: c = 'F'; break;
927 case double_type: c = 'D'; break;
928 case void_type: c = 'V'; break;
929 case unsuitable_type: c = '-'; break;
930 case return_address_type: c = 'r'; break;
931 case continuation_type: c = '+'; break;
932 case reference_type: c = 'L'; break;
933 case null_type: c = '@'; break;
934 case uninitialized_reference_type: c = 'U'; break;
936 debug_print ("%c", c);
938 #endif /* VERIFY_DEBUG */
941 // This class holds all the state information we need for a given
942 // location.
943 struct state
945 // The current top of the stack, in terms of slots.
946 int stacktop;
947 // The current depth of the stack. This will be larger than
948 // STACKTOP when wide types are on the stack.
949 int stackdepth;
950 // The stack.
951 type *stack;
952 // The local variables.
953 type *locals;
954 // We keep track of the type of `this' specially. This is used to
955 // ensure that an instance initializer invokes another initializer
956 // on `this' before returning. We must keep track of this
957 // specially because otherwise we might be confused by code which
958 // assigns to locals[0] (overwriting `this') and then returns
959 // without really initializing.
960 type this_type;
962 // The PC for this state. This is only valid on states which are
963 // permanently attached to a given PC. For an object like
964 // `current_state', which is used transiently, this has no
965 // meaning.
966 int pc;
967 // We keep a linked list of all states requiring reverification.
968 // If this is the special value INVALID_STATE then this state is
969 // not on the list. NULL marks the end of the linked list.
970 state *next;
972 // NO_NEXT is the PC value meaning that a new state must be
973 // acquired from the verification list.
974 static const int NO_NEXT = -1;
976 state ()
977 : this_type ()
979 stack = NULL;
980 locals = NULL;
981 next = INVALID_STATE;
984 state (int max_stack, int max_locals)
985 : this_type ()
987 stacktop = 0;
988 stackdepth = 0;
989 stack = new type[max_stack];
990 for (int i = 0; i < max_stack; ++i)
991 stack[i] = unsuitable_type;
992 locals = new type[max_locals];
993 for (int i = 0; i < max_locals; ++i)
994 locals[i] = unsuitable_type;
995 pc = NO_NEXT;
996 next = INVALID_STATE;
999 state (const state *orig, int max_stack, int max_locals)
1001 stack = new type[max_stack];
1002 locals = new type[max_locals];
1003 copy (orig, max_stack, max_locals);
1004 pc = NO_NEXT;
1005 next = INVALID_STATE;
1008 ~state ()
1010 if (stack)
1011 delete[] stack;
1012 if (locals)
1013 delete[] locals;
1016 void *operator new[] (size_t bytes)
1018 return _Jv_Malloc (bytes);
1021 void operator delete[] (void *mem)
1023 _Jv_Free (mem);
1026 void *operator new (size_t bytes)
1028 return _Jv_Malloc (bytes);
1031 void operator delete (void *mem)
1033 _Jv_Free (mem);
1036 void copy (const state *copy, int max_stack, int max_locals)
1038 stacktop = copy->stacktop;
1039 stackdepth = copy->stackdepth;
1040 for (int i = 0; i < max_stack; ++i)
1041 stack[i] = copy->stack[i];
1042 for (int i = 0; i < max_locals; ++i)
1043 locals[i] = copy->locals[i];
1045 this_type = copy->this_type;
1046 // Don't modify `next' or `pc'.
1049 // Modify this state to reflect entry to an exception handler.
1050 void set_exception (type t, int max_stack)
1052 stackdepth = 1;
1053 stacktop = 1;
1054 stack[0] = t;
1055 for (int i = stacktop; i < max_stack; ++i)
1056 stack[i] = unsuitable_type;
1059 inline int get_pc () const
1061 return pc;
1064 void set_pc (int npc)
1066 pc = npc;
1069 // Merge STATE_OLD into this state. Destructively modifies this
1070 // state. Returns true if the new state was in fact changed.
1071 // Will throw an exception if the states are not mergeable.
1072 bool merge (state *state_old, int max_locals,
1073 _Jv_BytecodeVerifier *verifier)
1075 bool changed = false;
1077 // Special handling for `this'. If one or the other is
1078 // uninitialized, then the merge is uninitialized.
1079 if (this_type.isinitialized ())
1080 this_type = state_old->this_type;
1082 // Merge stacks.
1083 if (state_old->stacktop != stacktop) // FIXME stackdepth instead?
1084 verifier->verify_fail ("stack sizes differ");
1085 for (int i = 0; i < state_old->stacktop; ++i)
1087 if (stack[i].merge (state_old->stack[i], false, verifier))
1088 changed = true;
1091 // Merge local variables.
1092 for (int i = 0; i < max_locals; ++i)
1094 if (locals[i].merge (state_old->locals[i], true, verifier))
1095 changed = true;
1098 return changed;
1101 // Throw an exception if there is an uninitialized object on the
1102 // stack or in a local variable. EXCEPTION_SEMANTICS controls
1103 // whether we're using backwards-branch or exception-handing
1104 // semantics.
1105 void check_no_uninitialized_objects (int max_locals,
1106 _Jv_BytecodeVerifier *verifier,
1107 bool exception_semantics = false)
1109 if (! exception_semantics)
1111 for (int i = 0; i < stacktop; ++i)
1112 if (stack[i].isreference () && ! stack[i].isinitialized ())
1113 verifier->verify_fail ("uninitialized object on stack");
1116 for (int i = 0; i < max_locals; ++i)
1117 if (locals[i].isreference () && ! locals[i].isinitialized ())
1118 verifier->verify_fail ("uninitialized object in local variable");
1120 check_this_initialized (verifier);
1123 // Ensure that `this' has been initialized.
1124 void check_this_initialized (_Jv_BytecodeVerifier *verifier)
1126 if (this_type.isreference () && ! this_type.isinitialized ())
1127 verifier->verify_fail ("`this' is uninitialized");
1130 // Set type of `this'.
1131 void set_this_type (const type &k)
1133 this_type = k;
1136 // Mark each `new'd object we know of that was allocated at PC as
1137 // initialized.
1138 void set_initialized (int pc, int max_locals)
1140 for (int i = 0; i < stacktop; ++i)
1141 stack[i].set_initialized (pc);
1142 for (int i = 0; i < max_locals; ++i)
1143 locals[i].set_initialized (pc);
1144 this_type.set_initialized (pc);
1147 // This tests to see whether two states can be considered "merge
1148 // compatible". If both states have a return-address in the same
1149 // slot, and the return addresses are different, then they are not
1150 // compatible and we must not try to merge them.
1151 bool state_mergeable_p (state *other, int max_locals,
1152 _Jv_BytecodeVerifier *verifier)
1154 // This is tricky: if the stack sizes differ, then not only are
1155 // these not mergeable, but in fact we should give an error, as
1156 // we've found two execution paths that reach a branch target
1157 // with different stack depths. FIXME stackdepth instead?
1158 if (stacktop != other->stacktop)
1159 verifier->verify_fail ("stack sizes differ");
1161 for (int i = 0; i < stacktop; ++i)
1162 if (! stack[i].state_mergeable_p (other->stack[i]))
1163 return false;
1164 for (int i = 0; i < max_locals; ++i)
1165 if (! locals[i].state_mergeable_p (other->locals[i]))
1166 return false;
1167 return true;
1170 void reverify (_Jv_BytecodeVerifier *verifier)
1172 if (next == INVALID_STATE)
1174 next = verifier->next_verify_state;
1175 verifier->next_verify_state = this;
1179 #ifdef VERIFY_DEBUG
1180 void print (const char *leader, int pc,
1181 int max_stack, int max_locals) const
1183 debug_print ("%s [%4d]: [stack] ", leader, pc);
1184 int i;
1185 for (i = 0; i < stacktop; ++i)
1186 stack[i].print ();
1187 for (; i < max_stack; ++i)
1188 debug_print (".");
1189 debug_print (" [local] ");
1190 for (i = 0; i < max_locals; ++i)
1191 locals[i].print ();
1192 debug_print (" | %p\n", this);
1194 #else
1195 inline void print (const char *, int, int, int) const
1198 #endif /* VERIFY_DEBUG */
1201 type pop_raw ()
1203 if (current_state->stacktop <= 0)
1204 verify_fail ("stack empty");
1205 type r = current_state->stack[--current_state->stacktop];
1206 current_state->stackdepth -= r.depth ();
1207 if (current_state->stackdepth < 0)
1208 verify_fail ("stack empty", start_PC);
1209 return r;
1212 type pop32 ()
1214 type r = pop_raw ();
1215 if (r.iswide ())
1216 verify_fail ("narrow pop of wide type");
1217 return r;
1220 type pop_type (type match)
1222 match.promote ();
1223 type t = pop_raw ();
1224 if (! match.compatible (t, this))
1225 verify_fail ("incompatible type on stack");
1226 return t;
1229 // Pop a reference which is guaranteed to be initialized. MATCH
1230 // doesn't have to be a reference type; in this case this acts like
1231 // pop_type.
1232 type pop_init_ref (type match)
1234 type t = pop_raw ();
1235 if (t.isreference () && ! t.isinitialized ())
1236 verify_fail ("initialized reference required");
1237 else if (! match.compatible (t, this))
1238 verify_fail ("incompatible type on stack");
1239 return t;
1242 // Pop a reference type or a return address.
1243 type pop_ref_or_return ()
1245 type t = pop_raw ();
1246 if (! t.isreference () && t.key != return_address_type)
1247 verify_fail ("expected reference or return address on stack");
1248 return t;
1251 void push_type (type t)
1253 // If T is a numeric type like short, promote it to int.
1254 t.promote ();
1256 int depth = t.depth ();
1257 if (current_state->stackdepth + depth > current_method->max_stack)
1258 verify_fail ("stack overflow");
1259 current_state->stack[current_state->stacktop++] = t;
1260 current_state->stackdepth += depth;
1263 void set_variable (int index, type t)
1265 // If T is a numeric type like short, promote it to int.
1266 t.promote ();
1268 int depth = t.depth ();
1269 if (index > current_method->max_locals - depth)
1270 verify_fail ("invalid local variable");
1271 current_state->locals[index] = t;
1273 if (depth == 2)
1274 current_state->locals[index + 1] = continuation_type;
1275 if (index > 0 && current_state->locals[index - 1].iswide ())
1276 current_state->locals[index - 1] = unsuitable_type;
1279 type get_variable (int index, type t)
1281 int depth = t.depth ();
1282 if (index > current_method->max_locals - depth)
1283 verify_fail ("invalid local variable");
1284 if (! t.compatible (current_state->locals[index], this))
1285 verify_fail ("incompatible type in local variable");
1286 if (depth == 2)
1288 type t (continuation_type);
1289 if (! current_state->locals[index + 1].compatible (t, this))
1290 verify_fail ("invalid local variable");
1292 return current_state->locals[index];
1295 // Make sure ARRAY is an array type and that its elements are
1296 // compatible with type ELEMENT. Returns the actual element type.
1297 type require_array_type (type array, type element)
1299 // An odd case. Here we just pretend that everything went ok. If
1300 // the requested element type is some kind of reference, return
1301 // the null type instead.
1302 if (array.isnull ())
1303 return element.isreference () ? type (null_type) : element;
1305 if (! array.isarray ())
1306 verify_fail ("array required");
1308 type t = array.element_type (this);
1309 if (! element.compatible (t, this))
1311 // Special case for byte arrays, which must also be boolean
1312 // arrays.
1313 bool ok = true;
1314 if (element.key == byte_type)
1316 type e2 (boolean_type);
1317 ok = e2.compatible (t, this);
1319 if (! ok)
1320 verify_fail ("incompatible array element type");
1323 // Return T and not ELEMENT, because T might be specialized.
1324 return t;
1327 jint get_byte ()
1329 if (PC >= current_method->code_length)
1330 verify_fail ("premature end of bytecode");
1331 return (jint) bytecode[PC++] & 0xff;
1334 jint get_ushort ()
1336 jint b1 = get_byte ();
1337 jint b2 = get_byte ();
1338 return (jint) ((b1 << 8) | b2) & 0xffff;
1341 jint get_short ()
1343 jint b1 = get_byte ();
1344 jint b2 = get_byte ();
1345 jshort s = (b1 << 8) | b2;
1346 return (jint) s;
1349 jint get_int ()
1351 jint b1 = get_byte ();
1352 jint b2 = get_byte ();
1353 jint b3 = get_byte ();
1354 jint b4 = get_byte ();
1355 return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
1358 int compute_jump (int offset)
1360 int npc = start_PC + offset;
1361 if (npc < 0 || npc >= current_method->code_length)
1362 verify_fail ("branch out of range", start_PC);
1363 return npc;
1366 // Add a new state to the state list at NPC.
1367 state *add_new_state (int npc, state *old_state)
1369 state *new_state = new state (old_state, current_method->max_stack,
1370 current_method->max_locals);
1371 debug_print ("== New state in add_new_state\n");
1372 new_state->print ("New", npc, current_method->max_stack,
1373 current_method->max_locals);
1374 linked<state> *nlink
1375 = (linked<state> *) _Jv_Malloc (sizeof (linked<state>));
1376 nlink->val = new_state;
1377 nlink->next = states[npc];
1378 states[npc] = nlink;
1379 new_state->set_pc (npc);
1380 return new_state;
1383 // Merge the indicated state into the state at the branch target and
1384 // schedule a new PC if there is a change. NPC is the PC of the
1385 // branch target, and FROM_STATE is the state at the source of the
1386 // branch. This method returns true if the destination state
1387 // changed and requires reverification, false otherwise.
1388 void merge_into (int npc, state *from_state)
1390 // Iterate over all target states and merge our state into each,
1391 // if applicable. FIXME one improvement we could make here is
1392 // "state destruction". Merging a new state into an existing one
1393 // might cause a return_address_type to be merged to
1394 // unsuitable_type. In this case the resulting state may now be
1395 // mergeable with other states currently held in parallel at this
1396 // location. So in this situation we could pairwise compare and
1397 // reduce the number of parallel states.
1398 bool applicable = false;
1399 for (linked<state> *iter = states[npc]; iter != NULL; iter = iter->next)
1401 state *new_state = iter->val;
1402 if (new_state->state_mergeable_p (from_state,
1403 current_method->max_locals, this))
1405 applicable = true;
1407 debug_print ("== Merge states in merge_into\n");
1408 from_state->print ("Frm", start_PC, current_method->max_stack,
1409 current_method->max_locals);
1410 new_state->print (" To", npc, current_method->max_stack,
1411 current_method->max_locals);
1412 bool changed = new_state->merge (from_state,
1413 current_method->max_locals,
1414 this);
1415 new_state->print ("New", npc, current_method->max_stack,
1416 current_method->max_locals);
1418 if (changed)
1419 new_state->reverify (this);
1423 if (! applicable)
1425 // Either we don't yet have a state at NPC, or we have a
1426 // return-address type that is in conflict with all existing
1427 // state. So, we need to create a new entry.
1428 state *new_state = add_new_state (npc, from_state);
1429 // A new state added in this way must always be reverified.
1430 new_state->reverify (this);
1434 void push_jump (int offset)
1436 int npc = compute_jump (offset);
1437 if (npc < PC)
1438 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1439 merge_into (npc, current_state);
1442 void push_exception_jump (type t, int pc)
1444 current_state->check_no_uninitialized_objects (current_method->max_locals,
1445 this, true);
1446 state s (current_state, current_method->max_stack,
1447 current_method->max_locals);
1448 if (current_method->max_stack < 1)
1449 verify_fail ("stack overflow at exception handler");
1450 s.set_exception (t, current_method->max_stack);
1451 merge_into (pc, &s);
1454 state *pop_jump ()
1456 state *new_state = next_verify_state;
1457 if (new_state == INVALID_STATE)
1458 verify_fail ("programmer error in pop_jump");
1459 if (new_state != NULL)
1461 next_verify_state = new_state->next;
1462 new_state->next = INVALID_STATE;
1464 return new_state;
1467 void invalidate_pc ()
1469 PC = state::NO_NEXT;
1472 void note_branch_target (int pc)
1474 // Don't check `pc <= PC', because we've advanced PC after
1475 // fetching the target and we haven't yet checked the next
1476 // instruction.
1477 if (pc < PC && ! (flags[pc] & FLAG_INSN_START))
1478 verify_fail ("branch not to instruction start", start_PC);
1479 flags[pc] |= FLAG_BRANCH_TARGET;
1482 void skip_padding ()
1484 while ((PC % 4) > 0)
1485 if (get_byte () != 0)
1486 verify_fail ("found nonzero padding byte");
1489 // Do the work for a `ret' instruction. INDEX is the index into the
1490 // local variables.
1491 void handle_ret_insn (int index)
1493 type ret_addr = get_variable (index, return_address_type);
1494 // It would be nice if we could do this. However, the JVM Spec
1495 // doesn't say that this is what happens. It is implied that
1496 // reusing a return address is invalid, but there's no actual
1497 // prohibition against it.
1498 // set_variable (index, unsuitable_type);
1500 int npc = ret_addr.get_pc ();
1501 // We might be returning to a `jsr' that is at the end of the
1502 // bytecode. This is ok if we never return from the called
1503 // subroutine, but if we see this here it is an error.
1504 if (npc >= current_method->code_length)
1505 verify_fail ("fell off end");
1507 if (npc < PC)
1508 current_state->check_no_uninitialized_objects (current_method->max_locals,
1509 this);
1510 merge_into (npc, current_state);
1511 invalidate_pc ();
1514 void handle_jsr_insn (int offset)
1516 int npc = compute_jump (offset);
1518 if (npc < PC)
1519 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1521 // Modify our state as appropriate for entry into a subroutine.
1522 type ret_addr (return_address_type);
1523 ret_addr.set_return_address (PC);
1524 push_type (ret_addr);
1525 merge_into (npc, current_state);
1526 invalidate_pc ();
1529 jclass construct_primitive_array_type (type_val prim)
1531 jclass k = NULL;
1532 switch (prim)
1534 case boolean_type:
1535 k = JvPrimClass (boolean);
1536 break;
1537 case char_type:
1538 k = JvPrimClass (char);
1539 break;
1540 case float_type:
1541 k = JvPrimClass (float);
1542 break;
1543 case double_type:
1544 k = JvPrimClass (double);
1545 break;
1546 case byte_type:
1547 k = JvPrimClass (byte);
1548 break;
1549 case short_type:
1550 k = JvPrimClass (short);
1551 break;
1552 case int_type:
1553 k = JvPrimClass (int);
1554 break;
1555 case long_type:
1556 k = JvPrimClass (long);
1557 break;
1559 // These aren't used here but we call them out to avoid
1560 // warnings.
1561 case void_type:
1562 case unsuitable_type:
1563 case return_address_type:
1564 case continuation_type:
1565 case reference_type:
1566 case null_type:
1567 case uninitialized_reference_type:
1568 default:
1569 verify_fail ("unknown type in construct_primitive_array_type");
1571 k = _Jv_GetArrayClass (k, NULL);
1572 return k;
1575 // This pass computes the location of branch targets and also
1576 // instruction starts.
1577 void branch_prepass ()
1579 flags = (char *) _Jv_Malloc (current_method->code_length);
1581 for (int i = 0; i < current_method->code_length; ++i)
1582 flags[i] = 0;
1584 PC = 0;
1585 while (PC < current_method->code_length)
1587 // Set `start_PC' early so that error checking can have the
1588 // correct value.
1589 start_PC = PC;
1590 flags[PC] |= FLAG_INSN_START;
1592 java_opcode opcode = (java_opcode) bytecode[PC++];
1593 switch (opcode)
1595 case op_nop:
1596 case op_aconst_null:
1597 case op_iconst_m1:
1598 case op_iconst_0:
1599 case op_iconst_1:
1600 case op_iconst_2:
1601 case op_iconst_3:
1602 case op_iconst_4:
1603 case op_iconst_5:
1604 case op_lconst_0:
1605 case op_lconst_1:
1606 case op_fconst_0:
1607 case op_fconst_1:
1608 case op_fconst_2:
1609 case op_dconst_0:
1610 case op_dconst_1:
1611 case op_iload_0:
1612 case op_iload_1:
1613 case op_iload_2:
1614 case op_iload_3:
1615 case op_lload_0:
1616 case op_lload_1:
1617 case op_lload_2:
1618 case op_lload_3:
1619 case op_fload_0:
1620 case op_fload_1:
1621 case op_fload_2:
1622 case op_fload_3:
1623 case op_dload_0:
1624 case op_dload_1:
1625 case op_dload_2:
1626 case op_dload_3:
1627 case op_aload_0:
1628 case op_aload_1:
1629 case op_aload_2:
1630 case op_aload_3:
1631 case op_iaload:
1632 case op_laload:
1633 case op_faload:
1634 case op_daload:
1635 case op_aaload:
1636 case op_baload:
1637 case op_caload:
1638 case op_saload:
1639 case op_istore_0:
1640 case op_istore_1:
1641 case op_istore_2:
1642 case op_istore_3:
1643 case op_lstore_0:
1644 case op_lstore_1:
1645 case op_lstore_2:
1646 case op_lstore_3:
1647 case op_fstore_0:
1648 case op_fstore_1:
1649 case op_fstore_2:
1650 case op_fstore_3:
1651 case op_dstore_0:
1652 case op_dstore_1:
1653 case op_dstore_2:
1654 case op_dstore_3:
1655 case op_astore_0:
1656 case op_astore_1:
1657 case op_astore_2:
1658 case op_astore_3:
1659 case op_iastore:
1660 case op_lastore:
1661 case op_fastore:
1662 case op_dastore:
1663 case op_aastore:
1664 case op_bastore:
1665 case op_castore:
1666 case op_sastore:
1667 case op_pop:
1668 case op_pop2:
1669 case op_dup:
1670 case op_dup_x1:
1671 case op_dup_x2:
1672 case op_dup2:
1673 case op_dup2_x1:
1674 case op_dup2_x2:
1675 case op_swap:
1676 case op_iadd:
1677 case op_isub:
1678 case op_imul:
1679 case op_idiv:
1680 case op_irem:
1681 case op_ishl:
1682 case op_ishr:
1683 case op_iushr:
1684 case op_iand:
1685 case op_ior:
1686 case op_ixor:
1687 case op_ladd:
1688 case op_lsub:
1689 case op_lmul:
1690 case op_ldiv:
1691 case op_lrem:
1692 case op_lshl:
1693 case op_lshr:
1694 case op_lushr:
1695 case op_land:
1696 case op_lor:
1697 case op_lxor:
1698 case op_fadd:
1699 case op_fsub:
1700 case op_fmul:
1701 case op_fdiv:
1702 case op_frem:
1703 case op_dadd:
1704 case op_dsub:
1705 case op_dmul:
1706 case op_ddiv:
1707 case op_drem:
1708 case op_ineg:
1709 case op_i2b:
1710 case op_i2c:
1711 case op_i2s:
1712 case op_lneg:
1713 case op_fneg:
1714 case op_dneg:
1715 case op_i2l:
1716 case op_i2f:
1717 case op_i2d:
1718 case op_l2i:
1719 case op_l2f:
1720 case op_l2d:
1721 case op_f2i:
1722 case op_f2l:
1723 case op_f2d:
1724 case op_d2i:
1725 case op_d2l:
1726 case op_d2f:
1727 case op_lcmp:
1728 case op_fcmpl:
1729 case op_fcmpg:
1730 case op_dcmpl:
1731 case op_dcmpg:
1732 case op_monitorenter:
1733 case op_monitorexit:
1734 case op_ireturn:
1735 case op_lreturn:
1736 case op_freturn:
1737 case op_dreturn:
1738 case op_areturn:
1739 case op_return:
1740 case op_athrow:
1741 case op_arraylength:
1742 break;
1744 case op_bipush:
1745 case op_ldc:
1746 case op_iload:
1747 case op_lload:
1748 case op_fload:
1749 case op_dload:
1750 case op_aload:
1751 case op_istore:
1752 case op_lstore:
1753 case op_fstore:
1754 case op_dstore:
1755 case op_astore:
1756 case op_ret:
1757 case op_newarray:
1758 get_byte ();
1759 break;
1761 case op_iinc:
1762 case op_sipush:
1763 case op_ldc_w:
1764 case op_ldc2_w:
1765 case op_getstatic:
1766 case op_getfield:
1767 case op_putfield:
1768 case op_putstatic:
1769 case op_new:
1770 case op_anewarray:
1771 case op_instanceof:
1772 case op_checkcast:
1773 case op_invokespecial:
1774 case op_invokestatic:
1775 case op_invokevirtual:
1776 get_short ();
1777 break;
1779 case op_multianewarray:
1780 get_short ();
1781 get_byte ();
1782 break;
1784 case op_jsr:
1785 case op_ifeq:
1786 case op_ifne:
1787 case op_iflt:
1788 case op_ifge:
1789 case op_ifgt:
1790 case op_ifle:
1791 case op_if_icmpeq:
1792 case op_if_icmpne:
1793 case op_if_icmplt:
1794 case op_if_icmpge:
1795 case op_if_icmpgt:
1796 case op_if_icmple:
1797 case op_if_acmpeq:
1798 case op_if_acmpne:
1799 case op_ifnull:
1800 case op_ifnonnull:
1801 case op_goto:
1802 note_branch_target (compute_jump (get_short ()));
1803 break;
1805 case op_tableswitch:
1807 skip_padding ();
1808 note_branch_target (compute_jump (get_int ()));
1809 jint low = get_int ();
1810 jint hi = get_int ();
1811 if (low > hi)
1812 verify_fail ("invalid tableswitch", start_PC);
1813 for (int i = low; i <= hi; ++i)
1814 note_branch_target (compute_jump (get_int ()));
1816 break;
1818 case op_lookupswitch:
1820 skip_padding ();
1821 note_branch_target (compute_jump (get_int ()));
1822 int npairs = get_int ();
1823 if (npairs < 0)
1824 verify_fail ("too few pairs in lookupswitch", start_PC);
1825 while (npairs-- > 0)
1827 get_int ();
1828 note_branch_target (compute_jump (get_int ()));
1831 break;
1833 case op_invokeinterface:
1834 get_short ();
1835 get_byte ();
1836 get_byte ();
1837 break;
1839 case op_wide:
1841 opcode = (java_opcode) get_byte ();
1842 get_short ();
1843 if (opcode == op_iinc)
1844 get_short ();
1846 break;
1848 case op_jsr_w:
1849 case op_goto_w:
1850 note_branch_target (compute_jump (get_int ()));
1851 break;
1853 // These are unused here, but we call them out explicitly
1854 // so that -Wswitch-enum doesn't complain.
1855 case op_putfield_1:
1856 case op_putfield_2:
1857 case op_putfield_4:
1858 case op_putfield_8:
1859 case op_putfield_a:
1860 case op_putstatic_1:
1861 case op_putstatic_2:
1862 case op_putstatic_4:
1863 case op_putstatic_8:
1864 case op_putstatic_a:
1865 case op_getfield_1:
1866 case op_getfield_2s:
1867 case op_getfield_2u:
1868 case op_getfield_4:
1869 case op_getfield_8:
1870 case op_getfield_a:
1871 case op_getstatic_1:
1872 case op_getstatic_2s:
1873 case op_getstatic_2u:
1874 case op_getstatic_4:
1875 case op_getstatic_8:
1876 case op_getstatic_a:
1877 default:
1878 verify_fail ("unrecognized instruction in branch_prepass",
1879 start_PC);
1882 // See if any previous branch tried to branch to the middle of
1883 // this instruction.
1884 for (int pc = start_PC + 1; pc < PC; ++pc)
1886 if ((flags[pc] & FLAG_BRANCH_TARGET))
1887 verify_fail ("branch to middle of instruction", pc);
1891 // Verify exception handlers.
1892 for (int i = 0; i < current_method->exc_count; ++i)
1894 if (! (flags[exception[i].handler_pc.i] & FLAG_INSN_START))
1895 verify_fail ("exception handler not at instruction start",
1896 exception[i].handler_pc.i);
1897 if (! (flags[exception[i].start_pc.i] & FLAG_INSN_START))
1898 verify_fail ("exception start not at instruction start",
1899 exception[i].start_pc.i);
1900 if (exception[i].end_pc.i != current_method->code_length
1901 && ! (flags[exception[i].end_pc.i] & FLAG_INSN_START))
1902 verify_fail ("exception end not at instruction start",
1903 exception[i].end_pc.i);
1905 flags[exception[i].handler_pc.i] |= FLAG_BRANCH_TARGET;
1909 void check_pool_index (int index)
1911 if (index < 0 || index >= current_class->constants.size)
1912 verify_fail ("constant pool index out of range", start_PC);
1915 type check_class_constant (int index)
1917 check_pool_index (index);
1918 _Jv_Constants *pool = &current_class->constants;
1919 if (pool->tags[index] == JV_CONSTANT_ResolvedClass)
1920 return type (pool->data[index].clazz, this);
1921 else if (pool->tags[index] == JV_CONSTANT_Class)
1922 return type (pool->data[index].utf8, this);
1923 verify_fail ("expected class constant", start_PC);
1926 type check_constant (int index)
1928 check_pool_index (index);
1929 _Jv_Constants *pool = &current_class->constants;
1930 if (pool->tags[index] == JV_CONSTANT_ResolvedString
1931 || pool->tags[index] == JV_CONSTANT_String)
1932 return type (&java::lang::String::class$, this);
1933 else if (pool->tags[index] == JV_CONSTANT_Integer)
1934 return type (int_type);
1935 else if (pool->tags[index] == JV_CONSTANT_Float)
1936 return type (float_type);
1937 verify_fail ("String, int, or float constant expected", start_PC);
1940 type check_wide_constant (int index)
1942 check_pool_index (index);
1943 _Jv_Constants *pool = &current_class->constants;
1944 if (pool->tags[index] == JV_CONSTANT_Long)
1945 return type (long_type);
1946 else if (pool->tags[index] == JV_CONSTANT_Double)
1947 return type (double_type);
1948 verify_fail ("long or double constant expected", start_PC);
1951 // Helper for both field and method. These are laid out the same in
1952 // the constant pool.
1953 type handle_field_or_method (int index, int expected,
1954 _Jv_Utf8Const **name,
1955 _Jv_Utf8Const **fmtype)
1957 check_pool_index (index);
1958 _Jv_Constants *pool = &current_class->constants;
1959 if (pool->tags[index] != expected)
1960 verify_fail ("didn't see expected constant", start_PC);
1961 // Once we know we have a Fieldref or Methodref we assume that it
1962 // is correctly laid out in the constant pool. I think the code
1963 // in defineclass.cc guarantees this.
1964 _Jv_ushort class_index, name_and_type_index;
1965 _Jv_loadIndexes (&pool->data[index],
1966 class_index,
1967 name_and_type_index);
1968 _Jv_ushort name_index, desc_index;
1969 _Jv_loadIndexes (&pool->data[name_and_type_index],
1970 name_index, desc_index);
1972 *name = pool->data[name_index].utf8;
1973 *fmtype = pool->data[desc_index].utf8;
1975 return check_class_constant (class_index);
1978 // Return field's type, compute class' type if requested.
1979 type check_field_constant (int index, type *class_type = NULL)
1981 _Jv_Utf8Const *name, *field_type;
1982 type ct = handle_field_or_method (index,
1983 JV_CONSTANT_Fieldref,
1984 &name, &field_type);
1985 if (class_type)
1986 *class_type = ct;
1987 if (field_type->first() == '[' || field_type->first() == 'L')
1988 return type (field_type, this);
1989 return get_type_val_for_signature (field_type->first());
1992 type check_method_constant (int index, bool is_interface,
1993 _Jv_Utf8Const **method_name,
1994 _Jv_Utf8Const **method_signature)
1996 return handle_field_or_method (index,
1997 (is_interface
1998 ? JV_CONSTANT_InterfaceMethodref
1999 : JV_CONSTANT_Methodref),
2000 method_name, method_signature);
2003 type get_one_type (char *&p)
2005 char *start = p;
2007 int arraycount = 0;
2008 while (*p == '[')
2010 ++arraycount;
2011 ++p;
2014 char v = *p++;
2016 if (v == 'L')
2018 while (*p != ';')
2019 ++p;
2020 ++p;
2021 _Jv_Utf8Const *name = make_utf8_const (start, p - start);
2022 return type (name, this);
2025 // Casting to jchar here is ok since we are looking at an ASCII
2026 // character.
2027 type_val rt = get_type_val_for_signature (jchar (v));
2029 if (arraycount == 0)
2031 // Callers of this function eventually push their arguments on
2032 // the stack. So, promote them here.
2033 return type (rt).promote ();
2036 jclass k = construct_primitive_array_type (rt);
2037 while (--arraycount > 0)
2038 k = _Jv_GetArrayClass (k, NULL);
2039 return type (k, this);
2042 void compute_argument_types (_Jv_Utf8Const *signature,
2043 type *types)
2045 char *p = signature->chars();
2047 // Skip `('.
2048 ++p;
2050 int i = 0;
2051 while (*p != ')')
2052 types[i++] = get_one_type (p);
2055 type compute_return_type (_Jv_Utf8Const *signature)
2057 char *p = signature->chars();
2058 while (*p != ')')
2059 ++p;
2060 ++p;
2061 return get_one_type (p);
2064 void check_return_type (type onstack)
2066 type rt = compute_return_type (current_method->self->signature);
2067 if (! rt.compatible (onstack, this))
2068 verify_fail ("incompatible return type");
2071 // Initialize the stack for the new method. Returns true if this
2072 // method is an instance initializer.
2073 bool initialize_stack ()
2075 int var = 0;
2076 bool is_init = _Jv_equalUtf8Consts (current_method->self->name,
2077 gcj::init_name);
2078 bool is_clinit = _Jv_equalUtf8Consts (current_method->self->name,
2079 gcj::clinit_name);
2081 using namespace java::lang::reflect;
2082 if (! Modifier::isStatic (current_method->self->accflags))
2084 type kurr (current_class, this);
2085 if (is_init)
2087 kurr.set_uninitialized (type::SELF, this);
2088 is_init = true;
2090 else if (is_clinit)
2091 verify_fail ("<clinit> method must be static");
2092 set_variable (0, kurr);
2093 current_state->set_this_type (kurr);
2094 ++var;
2096 else
2098 if (is_init)
2099 verify_fail ("<init> method must be non-static");
2102 // We have to handle wide arguments specially here.
2103 int arg_count = _Jv_count_arguments (current_method->self->signature);
2104 type arg_types[arg_count];
2105 compute_argument_types (current_method->self->signature, arg_types);
2106 for (int i = 0; i < arg_count; ++i)
2108 set_variable (var, arg_types[i]);
2109 ++var;
2110 if (arg_types[i].iswide ())
2111 ++var;
2114 return is_init;
2117 void verify_instructions_0 ()
2119 current_state = new state (current_method->max_stack,
2120 current_method->max_locals);
2122 PC = 0;
2123 start_PC = 0;
2125 // True if we are verifying an instance initializer.
2126 bool this_is_init = initialize_stack ();
2128 states = (linked<state> **) _Jv_Malloc (sizeof (linked<state> *)
2129 * current_method->code_length);
2130 for (int i = 0; i < current_method->code_length; ++i)
2131 states[i] = NULL;
2133 next_verify_state = NULL;
2135 while (true)
2137 // If the PC was invalidated, get a new one from the work list.
2138 if (PC == state::NO_NEXT)
2140 state *new_state = pop_jump ();
2141 // If it is null, we're done.
2142 if (new_state == NULL)
2143 break;
2145 PC = new_state->get_pc ();
2146 debug_print ("== State pop from pending list\n");
2147 // Set up the current state.
2148 current_state->copy (new_state, current_method->max_stack,
2149 current_method->max_locals);
2151 else
2153 // We only have to do this checking in the situation where
2154 // control flow falls through from the previous
2155 // instruction. Otherwise merging is done at the time we
2156 // push the branch.
2157 if (states[PC] != NULL)
2159 // We've already visited this instruction. So merge
2160 // the states together. It is simplest, but not most
2161 // efficient, to just always invalidate the PC here.
2162 merge_into (PC, current_state);
2163 invalidate_pc ();
2164 continue;
2168 // Control can't fall off the end of the bytecode. We need to
2169 // check this in both cases, not just the fall-through case,
2170 // because we don't check to see whether a `jsr' appears at
2171 // the end of the bytecode until we process a `ret'.
2172 if (PC >= current_method->code_length)
2173 verify_fail ("fell off end");
2175 // We only have to keep saved state at branch targets. If
2176 // we're at a branch target and the state here hasn't been set
2177 // yet, we set it now. You might notice that `ret' targets
2178 // won't necessarily have FLAG_BRANCH_TARGET set. This
2179 // doesn't matter, since those states will be filled in by
2180 // merge_into.
2181 if (states[PC] == NULL && (flags[PC] & FLAG_BRANCH_TARGET))
2182 add_new_state (PC, current_state);
2184 // Set this before handling exceptions so that debug output is
2185 // sane.
2186 start_PC = PC;
2188 // Update states for all active exception handlers. Ordinarily
2189 // there are not many exception handlers. So we simply run
2190 // through them all.
2191 for (int i = 0; i < current_method->exc_count; ++i)
2193 if (PC >= exception[i].start_pc.i && PC < exception[i].end_pc.i)
2195 type handler (&java::lang::Throwable::class$, this);
2196 if (exception[i].handler_type.i != 0)
2197 handler = check_class_constant (exception[i].handler_type.i);
2198 push_exception_jump (handler, exception[i].handler_pc.i);
2202 current_state->print (" ", PC, current_method->max_stack,
2203 current_method->max_locals);
2204 java_opcode opcode = (java_opcode) bytecode[PC++];
2205 switch (opcode)
2207 case op_nop:
2208 break;
2210 case op_aconst_null:
2211 push_type (null_type);
2212 break;
2214 case op_iconst_m1:
2215 case op_iconst_0:
2216 case op_iconst_1:
2217 case op_iconst_2:
2218 case op_iconst_3:
2219 case op_iconst_4:
2220 case op_iconst_5:
2221 push_type (int_type);
2222 break;
2224 case op_lconst_0:
2225 case op_lconst_1:
2226 push_type (long_type);
2227 break;
2229 case op_fconst_0:
2230 case op_fconst_1:
2231 case op_fconst_2:
2232 push_type (float_type);
2233 break;
2235 case op_dconst_0:
2236 case op_dconst_1:
2237 push_type (double_type);
2238 break;
2240 case op_bipush:
2241 get_byte ();
2242 push_type (int_type);
2243 break;
2245 case op_sipush:
2246 get_short ();
2247 push_type (int_type);
2248 break;
2250 case op_ldc:
2251 push_type (check_constant (get_byte ()));
2252 break;
2253 case op_ldc_w:
2254 push_type (check_constant (get_ushort ()));
2255 break;
2256 case op_ldc2_w:
2257 push_type (check_wide_constant (get_ushort ()));
2258 break;
2260 case op_iload:
2261 push_type (get_variable (get_byte (), int_type));
2262 break;
2263 case op_lload:
2264 push_type (get_variable (get_byte (), long_type));
2265 break;
2266 case op_fload:
2267 push_type (get_variable (get_byte (), float_type));
2268 break;
2269 case op_dload:
2270 push_type (get_variable (get_byte (), double_type));
2271 break;
2272 case op_aload:
2273 push_type (get_variable (get_byte (), reference_type));
2274 break;
2276 case op_iload_0:
2277 case op_iload_1:
2278 case op_iload_2:
2279 case op_iload_3:
2280 push_type (get_variable (opcode - op_iload_0, int_type));
2281 break;
2282 case op_lload_0:
2283 case op_lload_1:
2284 case op_lload_2:
2285 case op_lload_3:
2286 push_type (get_variable (opcode - op_lload_0, long_type));
2287 break;
2288 case op_fload_0:
2289 case op_fload_1:
2290 case op_fload_2:
2291 case op_fload_3:
2292 push_type (get_variable (opcode - op_fload_0, float_type));
2293 break;
2294 case op_dload_0:
2295 case op_dload_1:
2296 case op_dload_2:
2297 case op_dload_3:
2298 push_type (get_variable (opcode - op_dload_0, double_type));
2299 break;
2300 case op_aload_0:
2301 case op_aload_1:
2302 case op_aload_2:
2303 case op_aload_3:
2304 push_type (get_variable (opcode - op_aload_0, reference_type));
2305 break;
2306 case op_iaload:
2307 pop_type (int_type);
2308 push_type (require_array_type (pop_init_ref (reference_type),
2309 int_type));
2310 break;
2311 case op_laload:
2312 pop_type (int_type);
2313 push_type (require_array_type (pop_init_ref (reference_type),
2314 long_type));
2315 break;
2316 case op_faload:
2317 pop_type (int_type);
2318 push_type (require_array_type (pop_init_ref (reference_type),
2319 float_type));
2320 break;
2321 case op_daload:
2322 pop_type (int_type);
2323 push_type (require_array_type (pop_init_ref (reference_type),
2324 double_type));
2325 break;
2326 case op_aaload:
2327 pop_type (int_type);
2328 push_type (require_array_type (pop_init_ref (reference_type),
2329 reference_type));
2330 break;
2331 case op_baload:
2332 pop_type (int_type);
2333 require_array_type (pop_init_ref (reference_type), byte_type);
2334 push_type (int_type);
2335 break;
2336 case op_caload:
2337 pop_type (int_type);
2338 require_array_type (pop_init_ref (reference_type), char_type);
2339 push_type (int_type);
2340 break;
2341 case op_saload:
2342 pop_type (int_type);
2343 require_array_type (pop_init_ref (reference_type), short_type);
2344 push_type (int_type);
2345 break;
2346 case op_istore:
2347 set_variable (get_byte (), pop_type (int_type));
2348 break;
2349 case op_lstore:
2350 set_variable (get_byte (), pop_type (long_type));
2351 break;
2352 case op_fstore:
2353 set_variable (get_byte (), pop_type (float_type));
2354 break;
2355 case op_dstore:
2356 set_variable (get_byte (), pop_type (double_type));
2357 break;
2358 case op_astore:
2359 set_variable (get_byte (), pop_ref_or_return ());
2360 break;
2361 case op_istore_0:
2362 case op_istore_1:
2363 case op_istore_2:
2364 case op_istore_3:
2365 set_variable (opcode - op_istore_0, pop_type (int_type));
2366 break;
2367 case op_lstore_0:
2368 case op_lstore_1:
2369 case op_lstore_2:
2370 case op_lstore_3:
2371 set_variable (opcode - op_lstore_0, pop_type (long_type));
2372 break;
2373 case op_fstore_0:
2374 case op_fstore_1:
2375 case op_fstore_2:
2376 case op_fstore_3:
2377 set_variable (opcode - op_fstore_0, pop_type (float_type));
2378 break;
2379 case op_dstore_0:
2380 case op_dstore_1:
2381 case op_dstore_2:
2382 case op_dstore_3:
2383 set_variable (opcode - op_dstore_0, pop_type (double_type));
2384 break;
2385 case op_astore_0:
2386 case op_astore_1:
2387 case op_astore_2:
2388 case op_astore_3:
2389 set_variable (opcode - op_astore_0, pop_ref_or_return ());
2390 break;
2391 case op_iastore:
2392 pop_type (int_type);
2393 pop_type (int_type);
2394 require_array_type (pop_init_ref (reference_type), int_type);
2395 break;
2396 case op_lastore:
2397 pop_type (long_type);
2398 pop_type (int_type);
2399 require_array_type (pop_init_ref (reference_type), long_type);
2400 break;
2401 case op_fastore:
2402 pop_type (float_type);
2403 pop_type (int_type);
2404 require_array_type (pop_init_ref (reference_type), float_type);
2405 break;
2406 case op_dastore:
2407 pop_type (double_type);
2408 pop_type (int_type);
2409 require_array_type (pop_init_ref (reference_type), double_type);
2410 break;
2411 case op_aastore:
2412 pop_type (reference_type);
2413 pop_type (int_type);
2414 require_array_type (pop_init_ref (reference_type), reference_type);
2415 break;
2416 case op_bastore:
2417 pop_type (int_type);
2418 pop_type (int_type);
2419 require_array_type (pop_init_ref (reference_type), byte_type);
2420 break;
2421 case op_castore:
2422 pop_type (int_type);
2423 pop_type (int_type);
2424 require_array_type (pop_init_ref (reference_type), char_type);
2425 break;
2426 case op_sastore:
2427 pop_type (int_type);
2428 pop_type (int_type);
2429 require_array_type (pop_init_ref (reference_type), short_type);
2430 break;
2431 case op_pop:
2432 pop32 ();
2433 break;
2434 case op_pop2:
2436 type t = pop_raw ();
2437 if (! t.iswide ())
2438 pop32 ();
2440 break;
2441 case op_dup:
2443 type t = pop32 ();
2444 push_type (t);
2445 push_type (t);
2447 break;
2448 case op_dup_x1:
2450 type t1 = pop32 ();
2451 type t2 = pop32 ();
2452 push_type (t1);
2453 push_type (t2);
2454 push_type (t1);
2456 break;
2457 case op_dup_x2:
2459 type t1 = pop32 ();
2460 type t2 = pop_raw ();
2461 if (! t2.iswide ())
2463 type t3 = pop32 ();
2464 push_type (t1);
2465 push_type (t3);
2467 else
2468 push_type (t1);
2469 push_type (t2);
2470 push_type (t1);
2472 break;
2473 case op_dup2:
2475 type t = pop_raw ();
2476 if (! t.iswide ())
2478 type t2 = pop32 ();
2479 push_type (t2);
2480 push_type (t);
2481 push_type (t2);
2483 else
2484 push_type (t);
2485 push_type (t);
2487 break;
2488 case op_dup2_x1:
2490 type t1 = pop_raw ();
2491 type t2 = pop32 ();
2492 if (! t1.iswide ())
2494 type t3 = pop32 ();
2495 push_type (t2);
2496 push_type (t1);
2497 push_type (t3);
2499 else
2500 push_type (t1);
2501 push_type (t2);
2502 push_type (t1);
2504 break;
2505 case op_dup2_x2:
2507 type t1 = pop_raw ();
2508 if (t1.iswide ())
2510 type t2 = pop_raw ();
2511 if (t2.iswide ())
2513 push_type (t1);
2514 push_type (t2);
2516 else
2518 type t3 = pop32 ();
2519 push_type (t1);
2520 push_type (t3);
2521 push_type (t2);
2523 push_type (t1);
2525 else
2527 type t2 = pop32 ();
2528 type t3 = pop_raw ();
2529 if (t3.iswide ())
2531 push_type (t2);
2532 push_type (t1);
2534 else
2536 type t4 = pop32 ();
2537 push_type (t2);
2538 push_type (t1);
2539 push_type (t4);
2541 push_type (t3);
2542 push_type (t2);
2543 push_type (t1);
2546 break;
2547 case op_swap:
2549 type t1 = pop32 ();
2550 type t2 = pop32 ();
2551 push_type (t1);
2552 push_type (t2);
2554 break;
2555 case op_iadd:
2556 case op_isub:
2557 case op_imul:
2558 case op_idiv:
2559 case op_irem:
2560 case op_ishl:
2561 case op_ishr:
2562 case op_iushr:
2563 case op_iand:
2564 case op_ior:
2565 case op_ixor:
2566 pop_type (int_type);
2567 push_type (pop_type (int_type));
2568 break;
2569 case op_ladd:
2570 case op_lsub:
2571 case op_lmul:
2572 case op_ldiv:
2573 case op_lrem:
2574 case op_land:
2575 case op_lor:
2576 case op_lxor:
2577 pop_type (long_type);
2578 push_type (pop_type (long_type));
2579 break;
2580 case op_lshl:
2581 case op_lshr:
2582 case op_lushr:
2583 pop_type (int_type);
2584 push_type (pop_type (long_type));
2585 break;
2586 case op_fadd:
2587 case op_fsub:
2588 case op_fmul:
2589 case op_fdiv:
2590 case op_frem:
2591 pop_type (float_type);
2592 push_type (pop_type (float_type));
2593 break;
2594 case op_dadd:
2595 case op_dsub:
2596 case op_dmul:
2597 case op_ddiv:
2598 case op_drem:
2599 pop_type (double_type);
2600 push_type (pop_type (double_type));
2601 break;
2602 case op_ineg:
2603 case op_i2b:
2604 case op_i2c:
2605 case op_i2s:
2606 push_type (pop_type (int_type));
2607 break;
2608 case op_lneg:
2609 push_type (pop_type (long_type));
2610 break;
2611 case op_fneg:
2612 push_type (pop_type (float_type));
2613 break;
2614 case op_dneg:
2615 push_type (pop_type (double_type));
2616 break;
2617 case op_iinc:
2618 get_variable (get_byte (), int_type);
2619 get_byte ();
2620 break;
2621 case op_i2l:
2622 pop_type (int_type);
2623 push_type (long_type);
2624 break;
2625 case op_i2f:
2626 pop_type (int_type);
2627 push_type (float_type);
2628 break;
2629 case op_i2d:
2630 pop_type (int_type);
2631 push_type (double_type);
2632 break;
2633 case op_l2i:
2634 pop_type (long_type);
2635 push_type (int_type);
2636 break;
2637 case op_l2f:
2638 pop_type (long_type);
2639 push_type (float_type);
2640 break;
2641 case op_l2d:
2642 pop_type (long_type);
2643 push_type (double_type);
2644 break;
2645 case op_f2i:
2646 pop_type (float_type);
2647 push_type (int_type);
2648 break;
2649 case op_f2l:
2650 pop_type (float_type);
2651 push_type (long_type);
2652 break;
2653 case op_f2d:
2654 pop_type (float_type);
2655 push_type (double_type);
2656 break;
2657 case op_d2i:
2658 pop_type (double_type);
2659 push_type (int_type);
2660 break;
2661 case op_d2l:
2662 pop_type (double_type);
2663 push_type (long_type);
2664 break;
2665 case op_d2f:
2666 pop_type (double_type);
2667 push_type (float_type);
2668 break;
2669 case op_lcmp:
2670 pop_type (long_type);
2671 pop_type (long_type);
2672 push_type (int_type);
2673 break;
2674 case op_fcmpl:
2675 case op_fcmpg:
2676 pop_type (float_type);
2677 pop_type (float_type);
2678 push_type (int_type);
2679 break;
2680 case op_dcmpl:
2681 case op_dcmpg:
2682 pop_type (double_type);
2683 pop_type (double_type);
2684 push_type (int_type);
2685 break;
2686 case op_ifeq:
2687 case op_ifne:
2688 case op_iflt:
2689 case op_ifge:
2690 case op_ifgt:
2691 case op_ifle:
2692 pop_type (int_type);
2693 push_jump (get_short ());
2694 break;
2695 case op_if_icmpeq:
2696 case op_if_icmpne:
2697 case op_if_icmplt:
2698 case op_if_icmpge:
2699 case op_if_icmpgt:
2700 case op_if_icmple:
2701 pop_type (int_type);
2702 pop_type (int_type);
2703 push_jump (get_short ());
2704 break;
2705 case op_if_acmpeq:
2706 case op_if_acmpne:
2707 pop_type (reference_type);
2708 pop_type (reference_type);
2709 push_jump (get_short ());
2710 break;
2711 case op_goto:
2712 push_jump (get_short ());
2713 invalidate_pc ();
2714 break;
2715 case op_jsr:
2716 handle_jsr_insn (get_short ());
2717 break;
2718 case op_ret:
2719 handle_ret_insn (get_byte ());
2720 break;
2721 case op_tableswitch:
2723 pop_type (int_type);
2724 skip_padding ();
2725 push_jump (get_int ());
2726 jint low = get_int ();
2727 jint high = get_int ();
2728 // Already checked LOW -vs- HIGH.
2729 for (int i = low; i <= high; ++i)
2730 push_jump (get_int ());
2731 invalidate_pc ();
2733 break;
2735 case op_lookupswitch:
2737 pop_type (int_type);
2738 skip_padding ();
2739 push_jump (get_int ());
2740 jint npairs = get_int ();
2741 // Already checked NPAIRS >= 0.
2742 jint lastkey = 0;
2743 for (int i = 0; i < npairs; ++i)
2745 jint key = get_int ();
2746 if (i > 0 && key <= lastkey)
2747 verify_fail ("lookupswitch pairs unsorted", start_PC);
2748 lastkey = key;
2749 push_jump (get_int ());
2751 invalidate_pc ();
2753 break;
2754 case op_ireturn:
2755 check_return_type (pop_type (int_type));
2756 invalidate_pc ();
2757 break;
2758 case op_lreturn:
2759 check_return_type (pop_type (long_type));
2760 invalidate_pc ();
2761 break;
2762 case op_freturn:
2763 check_return_type (pop_type (float_type));
2764 invalidate_pc ();
2765 break;
2766 case op_dreturn:
2767 check_return_type (pop_type (double_type));
2768 invalidate_pc ();
2769 break;
2770 case op_areturn:
2771 check_return_type (pop_init_ref (reference_type));
2772 invalidate_pc ();
2773 break;
2774 case op_return:
2775 // We only need to check this when the return type is
2776 // void, because all instance initializers return void.
2777 if (this_is_init)
2778 current_state->check_this_initialized (this);
2779 check_return_type (void_type);
2780 invalidate_pc ();
2781 break;
2782 case op_getstatic:
2783 push_type (check_field_constant (get_ushort ()));
2784 break;
2785 case op_putstatic:
2786 pop_type (check_field_constant (get_ushort ()));
2787 break;
2788 case op_getfield:
2790 type klass;
2791 type field = check_field_constant (get_ushort (), &klass);
2792 pop_type (klass);
2793 push_type (field);
2795 break;
2796 case op_putfield:
2798 type klass;
2799 type field = check_field_constant (get_ushort (), &klass);
2800 pop_type (field);
2802 // We have an obscure special case here: we can use
2803 // `putfield' on a field declared in this class, even if
2804 // `this' has not yet been initialized.
2805 if (! current_state->this_type.isinitialized ()
2806 && current_state->this_type.pc == type::SELF)
2807 klass.set_uninitialized (type::SELF, this);
2808 pop_type (klass);
2810 break;
2812 case op_invokevirtual:
2813 case op_invokespecial:
2814 case op_invokestatic:
2815 case op_invokeinterface:
2817 _Jv_Utf8Const *method_name, *method_signature;
2818 type class_type
2819 = check_method_constant (get_ushort (),
2820 opcode == op_invokeinterface,
2821 &method_name,
2822 &method_signature);
2823 // NARGS is only used when we're processing
2824 // invokeinterface. It is simplest for us to compute it
2825 // here and then verify it later.
2826 int nargs = 0;
2827 if (opcode == op_invokeinterface)
2829 nargs = get_byte ();
2830 if (get_byte () != 0)
2831 verify_fail ("invokeinterface dummy byte is wrong");
2834 bool is_init = false;
2835 if (_Jv_equalUtf8Consts (method_name, gcj::init_name))
2837 is_init = true;
2838 if (opcode != op_invokespecial)
2839 verify_fail ("can't invoke <init>");
2841 else if (method_name->first() == '<')
2842 verify_fail ("can't invoke method starting with `<'");
2844 // Pop arguments and check types.
2845 int arg_count = _Jv_count_arguments (method_signature);
2846 type arg_types[arg_count];
2847 compute_argument_types (method_signature, arg_types);
2848 for (int i = arg_count - 1; i >= 0; --i)
2850 // This is only used for verifying the byte for
2851 // invokeinterface.
2852 nargs -= arg_types[i].depth ();
2853 pop_init_ref (arg_types[i]);
2856 if (opcode == op_invokeinterface
2857 && nargs != 1)
2858 verify_fail ("wrong argument count for invokeinterface");
2860 if (opcode != op_invokestatic)
2862 type t = class_type;
2863 if (is_init)
2865 // In this case the PC doesn't matter.
2866 t.set_uninitialized (type::UNINIT, this);
2867 // FIXME: check to make sure that the <init>
2868 // call is to the right class.
2869 // It must either be super or an exact class
2870 // match.
2872 type raw = pop_raw ();
2873 if (! t.compatible (raw, this))
2874 verify_fail ("incompatible type on stack");
2876 if (is_init)
2877 current_state->set_initialized (raw.get_pc (),
2878 current_method->max_locals);
2881 type rt = compute_return_type (method_signature);
2882 if (! rt.isvoid ())
2883 push_type (rt);
2885 break;
2887 case op_new:
2889 type t = check_class_constant (get_ushort ());
2890 if (t.isarray () || t.isinterface (this) || t.isabstract (this))
2891 verify_fail ("type is array, interface, or abstract");
2892 t.set_uninitialized (start_PC, this);
2893 push_type (t);
2895 break;
2897 case op_newarray:
2899 int atype = get_byte ();
2900 // We intentionally have chosen constants to make this
2901 // valid.
2902 if (atype < boolean_type || atype > long_type)
2903 verify_fail ("type not primitive", start_PC);
2904 pop_type (int_type);
2905 type t (construct_primitive_array_type (type_val (atype)), this);
2906 push_type (t);
2908 break;
2909 case op_anewarray:
2910 pop_type (int_type);
2911 push_type (check_class_constant (get_ushort ()).to_array (this));
2912 break;
2913 case op_arraylength:
2915 type t = pop_init_ref (reference_type);
2916 if (! t.isarray () && ! t.isnull ())
2917 verify_fail ("array type expected");
2918 push_type (int_type);
2920 break;
2921 case op_athrow:
2922 pop_type (type (&java::lang::Throwable::class$, this));
2923 invalidate_pc ();
2924 break;
2925 case op_checkcast:
2926 pop_init_ref (reference_type);
2927 push_type (check_class_constant (get_ushort ()));
2928 break;
2929 case op_instanceof:
2930 pop_init_ref (reference_type);
2931 check_class_constant (get_ushort ());
2932 push_type (int_type);
2933 break;
2934 case op_monitorenter:
2935 pop_init_ref (reference_type);
2936 break;
2937 case op_monitorexit:
2938 pop_init_ref (reference_type);
2939 break;
2940 case op_wide:
2942 switch (get_byte ())
2944 case op_iload:
2945 push_type (get_variable (get_ushort (), int_type));
2946 break;
2947 case op_lload:
2948 push_type (get_variable (get_ushort (), long_type));
2949 break;
2950 case op_fload:
2951 push_type (get_variable (get_ushort (), float_type));
2952 break;
2953 case op_dload:
2954 push_type (get_variable (get_ushort (), double_type));
2955 break;
2956 case op_aload:
2957 push_type (get_variable (get_ushort (), reference_type));
2958 break;
2959 case op_istore:
2960 set_variable (get_ushort (), pop_type (int_type));
2961 break;
2962 case op_lstore:
2963 set_variable (get_ushort (), pop_type (long_type));
2964 break;
2965 case op_fstore:
2966 set_variable (get_ushort (), pop_type (float_type));
2967 break;
2968 case op_dstore:
2969 set_variable (get_ushort (), pop_type (double_type));
2970 break;
2971 case op_astore:
2972 set_variable (get_ushort (), pop_init_ref (reference_type));
2973 break;
2974 case op_ret:
2975 handle_ret_insn (get_short ());
2976 break;
2977 case op_iinc:
2978 get_variable (get_ushort (), int_type);
2979 get_short ();
2980 break;
2981 default:
2982 verify_fail ("unrecognized wide instruction", start_PC);
2985 break;
2986 case op_multianewarray:
2988 type atype = check_class_constant (get_ushort ());
2989 int dim = get_byte ();
2990 if (dim < 1)
2991 verify_fail ("too few dimensions to multianewarray", start_PC);
2992 atype.verify_dimensions (dim, this);
2993 for (int i = 0; i < dim; ++i)
2994 pop_type (int_type);
2995 push_type (atype);
2997 break;
2998 case op_ifnull:
2999 case op_ifnonnull:
3000 pop_type (reference_type);
3001 push_jump (get_short ());
3002 break;
3003 case op_goto_w:
3004 push_jump (get_int ());
3005 invalidate_pc ();
3006 break;
3007 case op_jsr_w:
3008 handle_jsr_insn (get_int ());
3009 break;
3011 // These are unused here, but we call them out explicitly
3012 // so that -Wswitch-enum doesn't complain.
3013 case op_putfield_1:
3014 case op_putfield_2:
3015 case op_putfield_4:
3016 case op_putfield_8:
3017 case op_putfield_a:
3018 case op_putstatic_1:
3019 case op_putstatic_2:
3020 case op_putstatic_4:
3021 case op_putstatic_8:
3022 case op_putstatic_a:
3023 case op_getfield_1:
3024 case op_getfield_2s:
3025 case op_getfield_2u:
3026 case op_getfield_4:
3027 case op_getfield_8:
3028 case op_getfield_a:
3029 case op_getstatic_1:
3030 case op_getstatic_2s:
3031 case op_getstatic_2u:
3032 case op_getstatic_4:
3033 case op_getstatic_8:
3034 case op_getstatic_a:
3035 default:
3036 // Unrecognized opcode.
3037 verify_fail ("unrecognized instruction in verify_instructions_0",
3038 start_PC);
3043 public:
3045 void verify_instructions ()
3047 branch_prepass ();
3048 verify_instructions_0 ();
3051 _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
3053 // We just print the text as utf-8. This is just for debugging
3054 // anyway.
3055 debug_print ("--------------------------------\n");
3056 debug_print ("-- Verifying method `%s'\n", m->self->name->chars());
3058 current_method = m;
3059 bytecode = m->bytecode ();
3060 exception = m->exceptions ();
3061 current_class = m->defining_class;
3063 states = NULL;
3064 flags = NULL;
3065 utf8_list = NULL;
3066 isect_list = NULL;
3069 ~_Jv_BytecodeVerifier ()
3071 if (flags)
3072 _Jv_Free (flags);
3074 while (utf8_list != NULL)
3076 linked<_Jv_Utf8Const> *n = utf8_list->next;
3077 _Jv_Free (utf8_list);
3078 utf8_list = n;
3081 while (isect_list != NULL)
3083 ref_intersection *next = isect_list->alloc_next;
3084 delete isect_list;
3085 isect_list = next;
3088 if (states)
3090 for (int i = 0; i < current_method->code_length; ++i)
3092 linked<state> *iter = states[i];
3093 while (iter != NULL)
3095 linked<state> *next = iter->next;
3096 delete iter->val;
3097 _Jv_Free (iter);
3098 iter = next;
3101 _Jv_Free (states);
3106 void
3107 _Jv_VerifyMethod (_Jv_InterpMethod *meth)
3109 _Jv_BytecodeVerifier v (meth);
3110 v.verify_instructions ();
3113 #endif /* INTERPRETER */