* ggc-common.c (ggc_rlimit_bound): Don't check RSS limit.
[official-gcc.git] / libjava / verify.cc
blob7ba2ba4daee54cdf0a51263c3171025a36bb3b9d
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. This is really ugly,
183 // but without this our utf8 objects would be collected.
184 linked<_Jv_Utf8Const> *utf8_list;
186 // A linked list of all ref_intersection objects we allocate.
187 ref_intersection *isect_list;
189 // Create a new Utf-8 constant and return it. We do this to avoid
190 // having our Utf-8 constants prematurely collected. FIXME this is
191 // ugly.
192 _Jv_Utf8Const *make_utf8_const (char *s, int len)
194 _Jv_Utf8Const *val = _Jv_makeUtf8Const (s, len);
195 _Jv_Utf8Const *r = (_Jv_Utf8Const *) _Jv_Malloc (sizeof (_Jv_Utf8Const)
196 + val->length
197 + 1);
198 r->length = val->length;
199 r->hash = val->hash;
200 memcpy (r->data, val->data, val->length + 1);
202 linked<_Jv_Utf8Const> *lu
203 = (linked<_Jv_Utf8Const> *) _Jv_Malloc (sizeof (linked<_Jv_Utf8Const>));
204 lu->val = r;
205 lu->next = utf8_list;
206 utf8_list = lu;
208 return r;
211 __attribute__ ((__noreturn__)) void verify_fail (char *s, jint pc = -1)
213 using namespace java::lang;
214 StringBuffer *buf = new StringBuffer ();
216 buf->append (JvNewStringLatin1 ("verification failed"));
217 if (pc == -1)
218 pc = start_PC;
219 if (pc != -1)
221 buf->append (JvNewStringLatin1 (" at PC "));
222 buf->append (pc);
225 _Jv_InterpMethod *method = current_method;
226 buf->append (JvNewStringLatin1 (" in "));
227 buf->append (current_class->getName());
228 buf->append ((jchar) ':');
229 buf->append (JvNewStringUTF (method->get_method()->name->data));
230 buf->append ((jchar) '(');
231 buf->append (JvNewStringUTF (method->get_method()->signature->data));
232 buf->append ((jchar) ')');
234 buf->append (JvNewStringLatin1 (": "));
235 buf->append (JvNewStringLatin1 (s));
236 throw new java::lang::VerifyError (buf->toString ());
239 // This enum holds a list of tags for all the different types we
240 // need to handle. Reference types are treated specially by the
241 // type class.
242 enum type_val
244 void_type,
246 // The values for primitive types are chosen to correspond to values
247 // specified to newarray.
248 boolean_type = 4,
249 char_type = 5,
250 float_type = 6,
251 double_type = 7,
252 byte_type = 8,
253 short_type = 9,
254 int_type = 10,
255 long_type = 11,
257 // Used when overwriting second word of a double or long in the
258 // local variables. Also used after merging local variable states
259 // to indicate an unusable value.
260 unsuitable_type,
261 return_address_type,
262 // This is the second word of a two-word value, i.e., a double or
263 // a long.
264 continuation_type,
266 // Everything after `reference_type' must be a reference type.
267 reference_type,
268 null_type,
269 uninitialized_reference_type
272 // This represents a merged class type. Some verifiers (including
273 // earlier versions of this one) will compute the intersection of
274 // two class types when merging states. However, this loses
275 // critical information about interfaces implemented by the various
276 // classes. So instead we keep track of all the actual classes that
277 // have been merged.
278 struct ref_intersection
280 // Whether or not this type has been resolved.
281 bool is_resolved;
283 // Actual type data.
284 union
286 // For a resolved reference type, this is a pointer to the class.
287 jclass klass;
288 // For other reference types, this it the name of the class.
289 _Jv_Utf8Const *name;
290 } data;
292 // Link to the next reference in the intersection.
293 ref_intersection *ref_next;
295 // This is used to keep track of all the allocated
296 // ref_intersection objects, so we can free them.
297 // FIXME: we should allocate these in chunks.
298 ref_intersection *alloc_next;
300 ref_intersection (jclass klass, _Jv_BytecodeVerifier *verifier)
301 : ref_next (NULL)
303 is_resolved = true;
304 data.klass = klass;
305 alloc_next = verifier->isect_list;
306 verifier->isect_list = this;
309 ref_intersection (_Jv_Utf8Const *name, _Jv_BytecodeVerifier *verifier)
310 : ref_next (NULL)
312 is_resolved = false;
313 data.name = name;
314 alloc_next = verifier->isect_list;
315 verifier->isect_list = this;
318 ref_intersection (ref_intersection *dup, ref_intersection *tail,
319 _Jv_BytecodeVerifier *verifier)
320 : ref_next (tail)
322 is_resolved = dup->is_resolved;
323 data = dup->data;
324 alloc_next = verifier->isect_list;
325 verifier->isect_list = this;
328 bool equals (ref_intersection *other, _Jv_BytecodeVerifier *verifier)
330 if (! is_resolved && ! other->is_resolved
331 && _Jv_equalUtf8Consts (data.name, other->data.name))
332 return true;
333 if (! is_resolved)
334 resolve (verifier);
335 if (! other->is_resolved)
336 other->resolve (verifier);
337 return data.klass == other->data.klass;
340 // Merge THIS type into OTHER, returning the result. This will
341 // return OTHER if all the classes in THIS already appear in
342 // OTHER.
343 ref_intersection *merge (ref_intersection *other,
344 _Jv_BytecodeVerifier *verifier)
346 ref_intersection *tail = other;
347 for (ref_intersection *self = this; self != NULL; self = self->ref_next)
349 bool add = true;
350 for (ref_intersection *iter = other; iter != NULL;
351 iter = iter->ref_next)
353 if (iter->equals (self, verifier))
355 add = false;
356 break;
360 if (add)
361 tail = new ref_intersection (self, tail, verifier);
363 return tail;
366 void resolve (_Jv_BytecodeVerifier *verifier)
368 if (is_resolved)
369 return;
371 using namespace java::lang;
372 java::lang::ClassLoader *loader
373 = verifier->current_class->getClassLoaderInternal();
374 // We might see either kind of name. Sigh.
375 if (data.name->data[0] == 'L'
376 && data.name->data[data.name->length - 1] == ';')
377 data.klass = _Jv_FindClassFromSignature (data.name->data, loader);
378 else
379 data.klass = Class::forName (_Jv_NewStringUtf8Const (data.name),
380 false, loader);
381 is_resolved = true;
384 // See if an object of type OTHER can be assigned to an object of
385 // type *THIS. This might resolve classes in one chain or the
386 // other.
387 bool compatible (ref_intersection *other,
388 _Jv_BytecodeVerifier *verifier)
390 ref_intersection *self = this;
392 for (; self != NULL; self = self->ref_next)
394 ref_intersection *other_iter = other;
396 for (; other_iter != NULL; other_iter = other_iter->ref_next)
398 // Avoid resolving if possible.
399 if (! self->is_resolved
400 && ! other_iter->is_resolved
401 && _Jv_equalUtf8Consts (self->data.name,
402 other_iter->data.name))
403 continue;
405 if (! self->is_resolved)
406 self->resolve(verifier);
407 if (! other_iter->is_resolved)
408 other_iter->resolve(verifier);
410 if (! is_assignable_from_slow (self->data.klass,
411 other_iter->data.klass))
412 return false;
416 return true;
419 bool isarray ()
421 // assert (ref_next == NULL);
422 if (is_resolved)
423 return data.klass->isArray ();
424 else
425 return data.name->data[0] == '[';
428 bool isinterface (_Jv_BytecodeVerifier *verifier)
430 // assert (ref_next == NULL);
431 if (! is_resolved)
432 resolve (verifier);
433 return data.klass->isInterface ();
436 bool isabstract (_Jv_BytecodeVerifier *verifier)
438 // assert (ref_next == NULL);
439 if (! is_resolved)
440 resolve (verifier);
441 using namespace java::lang::reflect;
442 return Modifier::isAbstract (data.klass->getModifiers ());
445 jclass getclass (_Jv_BytecodeVerifier *verifier)
447 if (! is_resolved)
448 resolve (verifier);
449 return data.klass;
452 int count_dimensions ()
454 int ndims = 0;
455 if (is_resolved)
457 jclass k = data.klass;
458 while (k->isArray ())
460 k = k->getComponentType ();
461 ++ndims;
464 else
466 char *p = data.name->data;
467 while (*p++ == '[')
468 ++ndims;
470 return ndims;
473 void *operator new (size_t bytes)
475 return _Jv_Malloc (bytes);
478 void operator delete (void *mem)
480 _Jv_Free (mem);
484 // Return the type_val corresponding to a primitive signature
485 // character. For instance `I' returns `int.class'.
486 type_val get_type_val_for_signature (jchar sig)
488 type_val rt;
489 switch (sig)
491 case 'Z':
492 rt = boolean_type;
493 break;
494 case 'B':
495 rt = byte_type;
496 break;
497 case 'C':
498 rt = char_type;
499 break;
500 case 'S':
501 rt = short_type;
502 break;
503 case 'I':
504 rt = int_type;
505 break;
506 case 'J':
507 rt = long_type;
508 break;
509 case 'F':
510 rt = float_type;
511 break;
512 case 'D':
513 rt = double_type;
514 break;
515 case 'V':
516 rt = void_type;
517 break;
518 default:
519 verify_fail ("invalid signature");
521 return rt;
524 // Return the type_val corresponding to a primitive class.
525 type_val get_type_val_for_signature (jclass k)
527 return get_type_val_for_signature ((jchar) k->method_count);
530 // This is like _Jv_IsAssignableFrom, but it works even if SOURCE or
531 // TARGET haven't been prepared.
532 static bool is_assignable_from_slow (jclass target, jclass source)
534 // First, strip arrays.
535 while (target->isArray ())
537 // If target is array, source must be as well.
538 if (! source->isArray ())
539 return false;
540 target = target->getComponentType ();
541 source = source->getComponentType ();
544 // Quick success.
545 if (target == &java::lang::Object::class$)
546 return true;
550 if (source == target)
551 return true;
553 if (target->isPrimitive () || source->isPrimitive ())
554 return false;
556 if (target->isInterface ())
558 for (int i = 0; i < source->interface_count; ++i)
560 // We use a recursive call because we also need to
561 // check superinterfaces.
562 if (is_assignable_from_slow (target, source->interfaces[i]))
563 return true;
566 source = source->getSuperclass ();
568 while (source != NULL);
570 return false;
573 // The `type' class is used to represent a single type in the
574 // verifier.
575 struct type
577 // The type key.
578 type_val key;
580 // For reference types, the representation of the type.
581 ref_intersection *klass;
583 // This is used in two situations.
585 // First, when constructing a new object, it is the PC of the
586 // `new' instruction which created the object. We use the special
587 // value UNINIT to mean that this is uninitialized, and the
588 // special value SELF for the case where the current method is
589 // itself the <init> method.
591 // Second, when the key is return_address_type, this holds the PC
592 // of the instruction following the `jsr'.
593 int pc;
595 static const int UNINIT = -2;
596 static const int SELF = -1;
598 // Basic constructor.
599 type ()
601 key = unsuitable_type;
602 klass = NULL;
603 pc = UNINIT;
606 // Make a new instance given the type tag. We assume a generic
607 // `reference_type' means Object.
608 type (type_val k)
610 key = k;
611 // For reference_type, if KLASS==NULL then that means we are
612 // looking for a generic object of any kind, including an
613 // uninitialized reference.
614 klass = NULL;
615 pc = UNINIT;
618 // Make a new instance given a class.
619 type (jclass k, _Jv_BytecodeVerifier *verifier)
621 key = reference_type;
622 klass = new ref_intersection (k, verifier);
623 pc = UNINIT;
626 // Make a new instance given the name of a class.
627 type (_Jv_Utf8Const *n, _Jv_BytecodeVerifier *verifier)
629 key = reference_type;
630 klass = new ref_intersection (n, verifier);
631 pc = UNINIT;
634 // Copy constructor.
635 type (const type &t)
637 key = t.key;
638 klass = t.klass;
639 pc = t.pc;
642 // These operators are required because libgcj can't link in
643 // -lstdc++.
644 void *operator new[] (size_t bytes)
646 return _Jv_Malloc (bytes);
649 void operator delete[] (void *mem)
651 _Jv_Free (mem);
654 type& operator= (type_val k)
656 key = k;
657 klass = NULL;
658 pc = UNINIT;
659 return *this;
662 type& operator= (const type& t)
664 key = t.key;
665 klass = t.klass;
666 pc = t.pc;
667 return *this;
670 // Promote a numeric type.
671 type &promote ()
673 if (key == boolean_type || key == char_type
674 || key == byte_type || key == short_type)
675 key = int_type;
676 return *this;
679 // Mark this type as the uninitialized result of `new'.
680 void set_uninitialized (int npc, _Jv_BytecodeVerifier *verifier)
682 if (key == reference_type)
683 key = uninitialized_reference_type;
684 else
685 verifier->verify_fail ("internal error in type::uninitialized");
686 pc = npc;
689 // Mark this type as now initialized.
690 void set_initialized (int npc)
692 if (npc != UNINIT && pc == npc && key == uninitialized_reference_type)
694 key = reference_type;
695 pc = UNINIT;
699 // Mark this type as a particular return address.
700 void set_return_address (int npc)
702 pc = npc;
705 // Return true if this type and type OTHER are considered
706 // mergeable for the purposes of state merging. This is related
707 // to subroutine handling. For this purpose two types are
708 // considered unmergeable if they are both return-addresses but
709 // have different PCs.
710 bool state_mergeable_p (const type &other) const
712 return (key != return_address_type
713 || other.key != return_address_type
714 || pc == other.pc);
717 // Return true if an object of type K can be assigned to a variable
718 // of type *THIS. Handle various special cases too. Might modify
719 // *THIS or K. Note however that this does not perform numeric
720 // promotion.
721 bool compatible (type &k, _Jv_BytecodeVerifier *verifier)
723 // Any type is compatible with the unsuitable type.
724 if (key == unsuitable_type)
725 return true;
727 if (key < reference_type || k.key < reference_type)
728 return key == k.key;
730 // The `null' type is convertible to any initialized reference
731 // type.
732 if (key == null_type)
733 return k.key != uninitialized_reference_type;
734 if (k.key == null_type)
735 return key != uninitialized_reference_type;
737 // A special case for a generic reference.
738 if (klass == NULL)
739 return true;
740 if (k.klass == NULL)
741 verifier->verify_fail ("programmer error in type::compatible");
743 // An initialized type and an uninitialized type are not
744 // compatible.
745 if (isinitialized () != k.isinitialized ())
746 return false;
748 // Two uninitialized objects are compatible if either:
749 // * The PCs are identical, or
750 // * One PC is UNINIT.
751 if (! isinitialized ())
753 if (pc != k.pc && pc != UNINIT && k.pc != UNINIT)
754 return false;
757 return klass->compatible(k.klass, verifier);
760 bool isvoid () const
762 return key == void_type;
765 bool iswide () const
767 return key == long_type || key == double_type;
770 // Return number of stack or local variable slots taken by this
771 // type.
772 int depth () const
774 return iswide () ? 2 : 1;
777 bool isarray () const
779 // We treat null_type as not an array. This is ok based on the
780 // current uses of this method.
781 if (key == reference_type)
782 return klass->isarray ();
783 return false;
786 bool isnull () const
788 return key == null_type;
791 bool isinterface (_Jv_BytecodeVerifier *verifier)
793 if (key != reference_type)
794 return false;
795 return klass->isinterface (verifier);
798 bool isabstract (_Jv_BytecodeVerifier *verifier)
800 if (key != reference_type)
801 return false;
802 return klass->isabstract (verifier);
805 // Return the element type of an array.
806 type element_type (_Jv_BytecodeVerifier *verifier)
808 if (key != reference_type)
809 verifier->verify_fail ("programmer error in type::element_type()", -1);
811 jclass k = klass->getclass (verifier)->getComponentType ();
812 if (k->isPrimitive ())
813 return type (verifier->get_type_val_for_signature (k));
814 return type (k, verifier);
817 // Return the array type corresponding to an initialized
818 // reference. We could expand this to work for other kinds of
819 // types, but currently we don't need to.
820 type to_array (_Jv_BytecodeVerifier *verifier)
822 if (key != reference_type)
823 verifier->verify_fail ("internal error in type::to_array()");
825 jclass k = klass->getclass (verifier);
826 return type (_Jv_GetArrayClass (k, k->getClassLoaderInternal()),
827 verifier);
830 bool isreference () const
832 return key >= reference_type;
835 int get_pc () const
837 return pc;
840 bool isinitialized () const
842 return key == reference_type || key == null_type;
845 bool isresolved () const
847 return (key == reference_type
848 || key == null_type
849 || key == uninitialized_reference_type);
852 void verify_dimensions (int ndims, _Jv_BytecodeVerifier *verifier)
854 // The way this is written, we don't need to check isarray().
855 if (key != reference_type)
856 verifier->verify_fail ("internal error in verify_dimensions:"
857 " not a reference type");
859 if (klass->count_dimensions () < ndims)
860 verifier->verify_fail ("array type has fewer dimensions"
861 " than required");
864 // Merge OLD_TYPE into this. On error throw exception. Return
865 // true if the merge caused a type change.
866 bool merge (type& old_type, bool local_semantics,
867 _Jv_BytecodeVerifier *verifier)
869 bool changed = false;
870 bool refo = old_type.isreference ();
871 bool refn = isreference ();
872 if (refo && refn)
874 if (old_type.key == null_type)
876 else if (key == null_type)
878 *this = old_type;
879 changed = true;
881 else if (isinitialized () != old_type.isinitialized ())
882 verifier->verify_fail ("merging initialized and uninitialized types");
883 else
885 if (! isinitialized ())
887 if (pc == UNINIT)
888 pc = old_type.pc;
889 else if (old_type.pc == UNINIT)
891 else if (pc != old_type.pc)
892 verifier->verify_fail ("merging different uninitialized types");
895 ref_intersection *merged = old_type.klass->merge (klass,
896 verifier);
897 if (merged != klass)
899 klass = merged;
900 changed = true;
904 else if (refo || refn || key != old_type.key)
906 if (local_semantics)
908 // If we already have an `unsuitable' type, then we
909 // don't need to change again.
910 if (key != unsuitable_type)
912 key = unsuitable_type;
913 changed = true;
916 else
917 verifier->verify_fail ("unmergeable type");
919 return changed;
922 #ifdef VERIFY_DEBUG
923 void print (void) const
925 char c = '?';
926 switch (key)
928 case boolean_type: c = 'Z'; break;
929 case byte_type: c = 'B'; break;
930 case char_type: c = 'C'; break;
931 case short_type: c = 'S'; break;
932 case int_type: c = 'I'; break;
933 case long_type: c = 'J'; break;
934 case float_type: c = 'F'; break;
935 case double_type: c = 'D'; break;
936 case void_type: c = 'V'; break;
937 case unsuitable_type: c = '-'; break;
938 case return_address_type: c = 'r'; break;
939 case continuation_type: c = '+'; break;
940 case reference_type: c = 'L'; break;
941 case null_type: c = '@'; break;
942 case uninitialized_reference_type: c = 'U'; break;
944 debug_print ("%c", c);
946 #endif /* VERIFY_DEBUG */
949 // This class holds all the state information we need for a given
950 // location.
951 struct state
953 // The current top of the stack, in terms of slots.
954 int stacktop;
955 // The current depth of the stack. This will be larger than
956 // STACKTOP when wide types are on the stack.
957 int stackdepth;
958 // The stack.
959 type *stack;
960 // The local variables.
961 type *locals;
962 // We keep track of the type of `this' specially. This is used to
963 // ensure that an instance initializer invokes another initializer
964 // on `this' before returning. We must keep track of this
965 // specially because otherwise we might be confused by code which
966 // assigns to locals[0] (overwriting `this') and then returns
967 // without really initializing.
968 type this_type;
970 // The PC for this state. This is only valid on states which are
971 // permanently attached to a given PC. For an object like
972 // `current_state', which is used transiently, this has no
973 // meaning.
974 int pc;
975 // We keep a linked list of all states requiring reverification.
976 // If this is the special value INVALID_STATE then this state is
977 // not on the list. NULL marks the end of the linked list.
978 state *next;
980 // NO_NEXT is the PC value meaning that a new state must be
981 // acquired from the verification list.
982 static const int NO_NEXT = -1;
984 state ()
985 : this_type ()
987 stack = NULL;
988 locals = NULL;
989 next = INVALID_STATE;
992 state (int max_stack, int max_locals)
993 : this_type ()
995 stacktop = 0;
996 stackdepth = 0;
997 stack = new type[max_stack];
998 for (int i = 0; i < max_stack; ++i)
999 stack[i] = unsuitable_type;
1000 locals = new type[max_locals];
1001 for (int i = 0; i < max_locals; ++i)
1002 locals[i] = unsuitable_type;
1003 pc = NO_NEXT;
1004 next = INVALID_STATE;
1007 state (const state *orig, int max_stack, int max_locals)
1009 stack = new type[max_stack];
1010 locals = new type[max_locals];
1011 copy (orig, max_stack, max_locals);
1012 pc = NO_NEXT;
1013 next = INVALID_STATE;
1016 ~state ()
1018 if (stack)
1019 delete[] stack;
1020 if (locals)
1021 delete[] locals;
1024 void *operator new[] (size_t bytes)
1026 return _Jv_Malloc (bytes);
1029 void operator delete[] (void *mem)
1031 _Jv_Free (mem);
1034 void *operator new (size_t bytes)
1036 return _Jv_Malloc (bytes);
1039 void operator delete (void *mem)
1041 _Jv_Free (mem);
1044 void copy (const state *copy, int max_stack, int max_locals)
1046 stacktop = copy->stacktop;
1047 stackdepth = copy->stackdepth;
1048 for (int i = 0; i < max_stack; ++i)
1049 stack[i] = copy->stack[i];
1050 for (int i = 0; i < max_locals; ++i)
1051 locals[i] = copy->locals[i];
1053 this_type = copy->this_type;
1054 // Don't modify `next' or `pc'.
1057 // Modify this state to reflect entry to an exception handler.
1058 void set_exception (type t, int max_stack)
1060 stackdepth = 1;
1061 stacktop = 1;
1062 stack[0] = t;
1063 for (int i = stacktop; i < max_stack; ++i)
1064 stack[i] = unsuitable_type;
1067 inline int get_pc () const
1069 return pc;
1072 void set_pc (int npc)
1074 pc = npc;
1077 // Merge STATE_OLD into this state. Destructively modifies this
1078 // state. Returns true if the new state was in fact changed.
1079 // Will throw an exception if the states are not mergeable.
1080 bool merge (state *state_old, int max_locals,
1081 _Jv_BytecodeVerifier *verifier)
1083 bool changed = false;
1085 // Special handling for `this'. If one or the other is
1086 // uninitialized, then the merge is uninitialized.
1087 if (this_type.isinitialized ())
1088 this_type = state_old->this_type;
1090 // Merge stacks.
1091 if (state_old->stacktop != stacktop) // FIXME stackdepth instead?
1092 verifier->verify_fail ("stack sizes differ");
1093 for (int i = 0; i < state_old->stacktop; ++i)
1095 if (stack[i].merge (state_old->stack[i], false, verifier))
1096 changed = true;
1099 // Merge local variables.
1100 for (int i = 0; i < max_locals; ++i)
1102 if (locals[i].merge (state_old->locals[i], true, verifier))
1103 changed = true;
1106 return changed;
1109 // Throw an exception if there is an uninitialized object on the
1110 // stack or in a local variable. EXCEPTION_SEMANTICS controls
1111 // whether we're using backwards-branch or exception-handing
1112 // semantics.
1113 void check_no_uninitialized_objects (int max_locals,
1114 _Jv_BytecodeVerifier *verifier,
1115 bool exception_semantics = false)
1117 if (! exception_semantics)
1119 for (int i = 0; i < stacktop; ++i)
1120 if (stack[i].isreference () && ! stack[i].isinitialized ())
1121 verifier->verify_fail ("uninitialized object on stack");
1124 for (int i = 0; i < max_locals; ++i)
1125 if (locals[i].isreference () && ! locals[i].isinitialized ())
1126 verifier->verify_fail ("uninitialized object in local variable");
1128 check_this_initialized (verifier);
1131 // Ensure that `this' has been initialized.
1132 void check_this_initialized (_Jv_BytecodeVerifier *verifier)
1134 if (this_type.isreference () && ! this_type.isinitialized ())
1135 verifier->verify_fail ("`this' is uninitialized");
1138 // Set type of `this'.
1139 void set_this_type (const type &k)
1141 this_type = k;
1144 // Mark each `new'd object we know of that was allocated at PC as
1145 // initialized.
1146 void set_initialized (int pc, int max_locals)
1148 for (int i = 0; i < stacktop; ++i)
1149 stack[i].set_initialized (pc);
1150 for (int i = 0; i < max_locals; ++i)
1151 locals[i].set_initialized (pc);
1152 this_type.set_initialized (pc);
1155 // This tests to see whether two states can be considered "merge
1156 // compatible". If both states have a return-address in the same
1157 // slot, and the return addresses are different, then they are not
1158 // compatible and we must not try to merge them.
1159 bool state_mergeable_p (state *other, int max_locals,
1160 _Jv_BytecodeVerifier *verifier)
1162 // This is tricky: if the stack sizes differ, then not only are
1163 // these not mergeable, but in fact we should give an error, as
1164 // we've found two execution paths that reach a branch target
1165 // with different stack depths. FIXME stackdepth instead?
1166 if (stacktop != other->stacktop)
1167 verifier->verify_fail ("stack sizes differ");
1169 for (int i = 0; i < stacktop; ++i)
1170 if (! stack[i].state_mergeable_p (other->stack[i]))
1171 return false;
1172 for (int i = 0; i < max_locals; ++i)
1173 if (! locals[i].state_mergeable_p (other->locals[i]))
1174 return false;
1175 return true;
1178 void reverify (_Jv_BytecodeVerifier *verifier)
1180 if (next == INVALID_STATE)
1182 next = verifier->next_verify_state;
1183 verifier->next_verify_state = this;
1187 #ifdef VERIFY_DEBUG
1188 void print (const char *leader, int pc,
1189 int max_stack, int max_locals) const
1191 debug_print ("%s [%4d]: [stack] ", leader, pc);
1192 int i;
1193 for (i = 0; i < stacktop; ++i)
1194 stack[i].print ();
1195 for (; i < max_stack; ++i)
1196 debug_print (".");
1197 debug_print (" [local] ");
1198 for (i = 0; i < max_locals; ++i)
1199 locals[i].print ();
1200 debug_print (" | %p\n", this);
1202 #else
1203 inline void print (const char *, int, int, int) const
1206 #endif /* VERIFY_DEBUG */
1209 type pop_raw ()
1211 if (current_state->stacktop <= 0)
1212 verify_fail ("stack empty");
1213 type r = current_state->stack[--current_state->stacktop];
1214 current_state->stackdepth -= r.depth ();
1215 if (current_state->stackdepth < 0)
1216 verify_fail ("stack empty", start_PC);
1217 return r;
1220 type pop32 ()
1222 type r = pop_raw ();
1223 if (r.iswide ())
1224 verify_fail ("narrow pop of wide type");
1225 return r;
1228 type pop_type (type match)
1230 match.promote ();
1231 type t = pop_raw ();
1232 if (! match.compatible (t, this))
1233 verify_fail ("incompatible type on stack");
1234 return t;
1237 // Pop a reference which is guaranteed to be initialized. MATCH
1238 // doesn't have to be a reference type; in this case this acts like
1239 // pop_type.
1240 type pop_init_ref (type match)
1242 type t = pop_raw ();
1243 if (t.isreference () && ! t.isinitialized ())
1244 verify_fail ("initialized reference required");
1245 else if (! match.compatible (t, this))
1246 verify_fail ("incompatible type on stack");
1247 return t;
1250 // Pop a reference type or a return address.
1251 type pop_ref_or_return ()
1253 type t = pop_raw ();
1254 if (! t.isreference () && t.key != return_address_type)
1255 verify_fail ("expected reference or return address on stack");
1256 return t;
1259 void push_type (type t)
1261 // If T is a numeric type like short, promote it to int.
1262 t.promote ();
1264 int depth = t.depth ();
1265 if (current_state->stackdepth + depth > current_method->max_stack)
1266 verify_fail ("stack overflow");
1267 current_state->stack[current_state->stacktop++] = t;
1268 current_state->stackdepth += depth;
1271 void set_variable (int index, type t)
1273 // If T is a numeric type like short, promote it to int.
1274 t.promote ();
1276 int depth = t.depth ();
1277 if (index > current_method->max_locals - depth)
1278 verify_fail ("invalid local variable");
1279 current_state->locals[index] = t;
1281 if (depth == 2)
1282 current_state->locals[index + 1] = continuation_type;
1283 if (index > 0 && current_state->locals[index - 1].iswide ())
1284 current_state->locals[index - 1] = unsuitable_type;
1287 type get_variable (int index, type t)
1289 int depth = t.depth ();
1290 if (index > current_method->max_locals - depth)
1291 verify_fail ("invalid local variable");
1292 if (! t.compatible (current_state->locals[index], this))
1293 verify_fail ("incompatible type in local variable");
1294 if (depth == 2)
1296 type t (continuation_type);
1297 if (! current_state->locals[index + 1].compatible (t, this))
1298 verify_fail ("invalid local variable");
1300 return current_state->locals[index];
1303 // Make sure ARRAY is an array type and that its elements are
1304 // compatible with type ELEMENT. Returns the actual element type.
1305 type require_array_type (type array, type element)
1307 // An odd case. Here we just pretend that everything went ok. If
1308 // the requested element type is some kind of reference, return
1309 // the null type instead.
1310 if (array.isnull ())
1311 return element.isreference () ? type (null_type) : element;
1313 if (! array.isarray ())
1314 verify_fail ("array required");
1316 type t = array.element_type (this);
1317 if (! element.compatible (t, this))
1319 // Special case for byte arrays, which must also be boolean
1320 // arrays.
1321 bool ok = true;
1322 if (element.key == byte_type)
1324 type e2 (boolean_type);
1325 ok = e2.compatible (t, this);
1327 if (! ok)
1328 verify_fail ("incompatible array element type");
1331 // Return T and not ELEMENT, because T might be specialized.
1332 return t;
1335 jint get_byte ()
1337 if (PC >= current_method->code_length)
1338 verify_fail ("premature end of bytecode");
1339 return (jint) bytecode[PC++] & 0xff;
1342 jint get_ushort ()
1344 jint b1 = get_byte ();
1345 jint b2 = get_byte ();
1346 return (jint) ((b1 << 8) | b2) & 0xffff;
1349 jint get_short ()
1351 jint b1 = get_byte ();
1352 jint b2 = get_byte ();
1353 jshort s = (b1 << 8) | b2;
1354 return (jint) s;
1357 jint get_int ()
1359 jint b1 = get_byte ();
1360 jint b2 = get_byte ();
1361 jint b3 = get_byte ();
1362 jint b4 = get_byte ();
1363 return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
1366 int compute_jump (int offset)
1368 int npc = start_PC + offset;
1369 if (npc < 0 || npc >= current_method->code_length)
1370 verify_fail ("branch out of range", start_PC);
1371 return npc;
1374 // Add a new state to the state list at NPC.
1375 state *add_new_state (int npc, state *old_state)
1377 state *new_state = new state (old_state, current_method->max_stack,
1378 current_method->max_locals);
1379 debug_print ("== New state in add_new_state\n");
1380 new_state->print ("New", npc, current_method->max_stack,
1381 current_method->max_locals);
1382 linked<state> *nlink
1383 = (linked<state> *) _Jv_Malloc (sizeof (linked<state>));
1384 nlink->val = new_state;
1385 nlink->next = states[npc];
1386 states[npc] = nlink;
1387 new_state->set_pc (npc);
1388 return new_state;
1391 // Merge the indicated state into the state at the branch target and
1392 // schedule a new PC if there is a change. NPC is the PC of the
1393 // branch target, and FROM_STATE is the state at the source of the
1394 // branch. This method returns true if the destination state
1395 // changed and requires reverification, false otherwise.
1396 void merge_into (int npc, state *from_state)
1398 // Iterate over all target states and merge our state into each,
1399 // if applicable. FIXME one improvement we could make here is
1400 // "state destruction". Merging a new state into an existing one
1401 // might cause a return_address_type to be merged to
1402 // unsuitable_type. In this case the resulting state may now be
1403 // mergeable with other states currently held in parallel at this
1404 // location. So in this situation we could pairwise compare and
1405 // reduce the number of parallel states.
1406 bool applicable = false;
1407 for (linked<state> *iter = states[npc]; iter != NULL; iter = iter->next)
1409 state *new_state = iter->val;
1410 if (new_state->state_mergeable_p (from_state,
1411 current_method->max_locals, this))
1413 applicable = true;
1415 debug_print ("== Merge states in merge_into\n");
1416 from_state->print ("Frm", start_PC, current_method->max_stack,
1417 current_method->max_locals);
1418 new_state->print (" To", npc, current_method->max_stack,
1419 current_method->max_locals);
1420 bool changed = new_state->merge (from_state,
1421 current_method->max_locals,
1422 this);
1423 new_state->print ("New", npc, current_method->max_stack,
1424 current_method->max_locals);
1426 if (changed)
1427 new_state->reverify (this);
1431 if (! applicable)
1433 // Either we don't yet have a state at NPC, or we have a
1434 // return-address type that is in conflict with all existing
1435 // state. So, we need to create a new entry.
1436 state *new_state = add_new_state (npc, from_state);
1437 // A new state added in this way must always be reverified.
1438 new_state->reverify (this);
1442 void push_jump (int offset)
1444 int npc = compute_jump (offset);
1445 if (npc < PC)
1446 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1447 merge_into (npc, current_state);
1450 void push_exception_jump (type t, int pc)
1452 current_state->check_no_uninitialized_objects (current_method->max_locals,
1453 this, true);
1454 state s (current_state, current_method->max_stack,
1455 current_method->max_locals);
1456 if (current_method->max_stack < 1)
1457 verify_fail ("stack overflow at exception handler");
1458 s.set_exception (t, current_method->max_stack);
1459 merge_into (pc, &s);
1462 state *pop_jump ()
1464 state *new_state = next_verify_state;
1465 if (new_state == INVALID_STATE)
1466 verify_fail ("programmer error in pop_jump");
1467 if (new_state != NULL)
1469 next_verify_state = new_state->next;
1470 new_state->next = INVALID_STATE;
1472 return new_state;
1475 void invalidate_pc ()
1477 PC = state::NO_NEXT;
1480 void note_branch_target (int pc)
1482 // Don't check `pc <= PC', because we've advanced PC after
1483 // fetching the target and we haven't yet checked the next
1484 // instruction.
1485 if (pc < PC && ! (flags[pc] & FLAG_INSN_START))
1486 verify_fail ("branch not to instruction start", start_PC);
1487 flags[pc] |= FLAG_BRANCH_TARGET;
1490 void skip_padding ()
1492 while ((PC % 4) > 0)
1493 if (get_byte () != 0)
1494 verify_fail ("found nonzero padding byte");
1497 // Do the work for a `ret' instruction. INDEX is the index into the
1498 // local variables.
1499 void handle_ret_insn (int index)
1501 type ret_addr = get_variable (index, return_address_type);
1502 // It would be nice if we could do this. However, the JVM Spec
1503 // doesn't say that this is what happens. It is implied that
1504 // reusing a return address is invalid, but there's no actual
1505 // prohibition against it.
1506 // set_variable (index, unsuitable_type);
1508 int npc = ret_addr.get_pc ();
1509 // We might be returning to a `jsr' that is at the end of the
1510 // bytecode. This is ok if we never return from the called
1511 // subroutine, but if we see this here it is an error.
1512 if (npc >= current_method->code_length)
1513 verify_fail ("fell off end");
1515 if (npc < PC)
1516 current_state->check_no_uninitialized_objects (current_method->max_locals,
1517 this);
1518 merge_into (npc, current_state);
1519 invalidate_pc ();
1522 void handle_jsr_insn (int offset)
1524 int npc = compute_jump (offset);
1526 if (npc < PC)
1527 current_state->check_no_uninitialized_objects (current_method->max_locals, this);
1529 // Modify our state as appropriate for entry into a subroutine.
1530 type ret_addr (return_address_type);
1531 ret_addr.set_return_address (PC);
1532 push_type (ret_addr);
1533 merge_into (npc, current_state);
1534 invalidate_pc ();
1537 jclass construct_primitive_array_type (type_val prim)
1539 jclass k = NULL;
1540 switch (prim)
1542 case boolean_type:
1543 k = JvPrimClass (boolean);
1544 break;
1545 case char_type:
1546 k = JvPrimClass (char);
1547 break;
1548 case float_type:
1549 k = JvPrimClass (float);
1550 break;
1551 case double_type:
1552 k = JvPrimClass (double);
1553 break;
1554 case byte_type:
1555 k = JvPrimClass (byte);
1556 break;
1557 case short_type:
1558 k = JvPrimClass (short);
1559 break;
1560 case int_type:
1561 k = JvPrimClass (int);
1562 break;
1563 case long_type:
1564 k = JvPrimClass (long);
1565 break;
1567 // These aren't used here but we call them out to avoid
1568 // warnings.
1569 case void_type:
1570 case unsuitable_type:
1571 case return_address_type:
1572 case continuation_type:
1573 case reference_type:
1574 case null_type:
1575 case uninitialized_reference_type:
1576 default:
1577 verify_fail ("unknown type in construct_primitive_array_type");
1579 k = _Jv_GetArrayClass (k, NULL);
1580 return k;
1583 // This pass computes the location of branch targets and also
1584 // instruction starts.
1585 void branch_prepass ()
1587 flags = (char *) _Jv_Malloc (current_method->code_length);
1589 for (int i = 0; i < current_method->code_length; ++i)
1590 flags[i] = 0;
1592 PC = 0;
1593 while (PC < current_method->code_length)
1595 // Set `start_PC' early so that error checking can have the
1596 // correct value.
1597 start_PC = PC;
1598 flags[PC] |= FLAG_INSN_START;
1600 java_opcode opcode = (java_opcode) bytecode[PC++];
1601 switch (opcode)
1603 case op_nop:
1604 case op_aconst_null:
1605 case op_iconst_m1:
1606 case op_iconst_0:
1607 case op_iconst_1:
1608 case op_iconst_2:
1609 case op_iconst_3:
1610 case op_iconst_4:
1611 case op_iconst_5:
1612 case op_lconst_0:
1613 case op_lconst_1:
1614 case op_fconst_0:
1615 case op_fconst_1:
1616 case op_fconst_2:
1617 case op_dconst_0:
1618 case op_dconst_1:
1619 case op_iload_0:
1620 case op_iload_1:
1621 case op_iload_2:
1622 case op_iload_3:
1623 case op_lload_0:
1624 case op_lload_1:
1625 case op_lload_2:
1626 case op_lload_3:
1627 case op_fload_0:
1628 case op_fload_1:
1629 case op_fload_2:
1630 case op_fload_3:
1631 case op_dload_0:
1632 case op_dload_1:
1633 case op_dload_2:
1634 case op_dload_3:
1635 case op_aload_0:
1636 case op_aload_1:
1637 case op_aload_2:
1638 case op_aload_3:
1639 case op_iaload:
1640 case op_laload:
1641 case op_faload:
1642 case op_daload:
1643 case op_aaload:
1644 case op_baload:
1645 case op_caload:
1646 case op_saload:
1647 case op_istore_0:
1648 case op_istore_1:
1649 case op_istore_2:
1650 case op_istore_3:
1651 case op_lstore_0:
1652 case op_lstore_1:
1653 case op_lstore_2:
1654 case op_lstore_3:
1655 case op_fstore_0:
1656 case op_fstore_1:
1657 case op_fstore_2:
1658 case op_fstore_3:
1659 case op_dstore_0:
1660 case op_dstore_1:
1661 case op_dstore_2:
1662 case op_dstore_3:
1663 case op_astore_0:
1664 case op_astore_1:
1665 case op_astore_2:
1666 case op_astore_3:
1667 case op_iastore:
1668 case op_lastore:
1669 case op_fastore:
1670 case op_dastore:
1671 case op_aastore:
1672 case op_bastore:
1673 case op_castore:
1674 case op_sastore:
1675 case op_pop:
1676 case op_pop2:
1677 case op_dup:
1678 case op_dup_x1:
1679 case op_dup_x2:
1680 case op_dup2:
1681 case op_dup2_x1:
1682 case op_dup2_x2:
1683 case op_swap:
1684 case op_iadd:
1685 case op_isub:
1686 case op_imul:
1687 case op_idiv:
1688 case op_irem:
1689 case op_ishl:
1690 case op_ishr:
1691 case op_iushr:
1692 case op_iand:
1693 case op_ior:
1694 case op_ixor:
1695 case op_ladd:
1696 case op_lsub:
1697 case op_lmul:
1698 case op_ldiv:
1699 case op_lrem:
1700 case op_lshl:
1701 case op_lshr:
1702 case op_lushr:
1703 case op_land:
1704 case op_lor:
1705 case op_lxor:
1706 case op_fadd:
1707 case op_fsub:
1708 case op_fmul:
1709 case op_fdiv:
1710 case op_frem:
1711 case op_dadd:
1712 case op_dsub:
1713 case op_dmul:
1714 case op_ddiv:
1715 case op_drem:
1716 case op_ineg:
1717 case op_i2b:
1718 case op_i2c:
1719 case op_i2s:
1720 case op_lneg:
1721 case op_fneg:
1722 case op_dneg:
1723 case op_i2l:
1724 case op_i2f:
1725 case op_i2d:
1726 case op_l2i:
1727 case op_l2f:
1728 case op_l2d:
1729 case op_f2i:
1730 case op_f2l:
1731 case op_f2d:
1732 case op_d2i:
1733 case op_d2l:
1734 case op_d2f:
1735 case op_lcmp:
1736 case op_fcmpl:
1737 case op_fcmpg:
1738 case op_dcmpl:
1739 case op_dcmpg:
1740 case op_monitorenter:
1741 case op_monitorexit:
1742 case op_ireturn:
1743 case op_lreturn:
1744 case op_freturn:
1745 case op_dreturn:
1746 case op_areturn:
1747 case op_return:
1748 case op_athrow:
1749 case op_arraylength:
1750 break;
1752 case op_bipush:
1753 case op_ldc:
1754 case op_iload:
1755 case op_lload:
1756 case op_fload:
1757 case op_dload:
1758 case op_aload:
1759 case op_istore:
1760 case op_lstore:
1761 case op_fstore:
1762 case op_dstore:
1763 case op_astore:
1764 case op_ret:
1765 case op_newarray:
1766 get_byte ();
1767 break;
1769 case op_iinc:
1770 case op_sipush:
1771 case op_ldc_w:
1772 case op_ldc2_w:
1773 case op_getstatic:
1774 case op_getfield:
1775 case op_putfield:
1776 case op_putstatic:
1777 case op_new:
1778 case op_anewarray:
1779 case op_instanceof:
1780 case op_checkcast:
1781 case op_invokespecial:
1782 case op_invokestatic:
1783 case op_invokevirtual:
1784 get_short ();
1785 break;
1787 case op_multianewarray:
1788 get_short ();
1789 get_byte ();
1790 break;
1792 case op_jsr:
1793 case op_ifeq:
1794 case op_ifne:
1795 case op_iflt:
1796 case op_ifge:
1797 case op_ifgt:
1798 case op_ifle:
1799 case op_if_icmpeq:
1800 case op_if_icmpne:
1801 case op_if_icmplt:
1802 case op_if_icmpge:
1803 case op_if_icmpgt:
1804 case op_if_icmple:
1805 case op_if_acmpeq:
1806 case op_if_acmpne:
1807 case op_ifnull:
1808 case op_ifnonnull:
1809 case op_goto:
1810 note_branch_target (compute_jump (get_short ()));
1811 break;
1813 case op_tableswitch:
1815 skip_padding ();
1816 note_branch_target (compute_jump (get_int ()));
1817 jint low = get_int ();
1818 jint hi = get_int ();
1819 if (low > hi)
1820 verify_fail ("invalid tableswitch", start_PC);
1821 for (int i = low; i <= hi; ++i)
1822 note_branch_target (compute_jump (get_int ()));
1824 break;
1826 case op_lookupswitch:
1828 skip_padding ();
1829 note_branch_target (compute_jump (get_int ()));
1830 int npairs = get_int ();
1831 if (npairs < 0)
1832 verify_fail ("too few pairs in lookupswitch", start_PC);
1833 while (npairs-- > 0)
1835 get_int ();
1836 note_branch_target (compute_jump (get_int ()));
1839 break;
1841 case op_invokeinterface:
1842 get_short ();
1843 get_byte ();
1844 get_byte ();
1845 break;
1847 case op_wide:
1849 opcode = (java_opcode) get_byte ();
1850 get_short ();
1851 if (opcode == op_iinc)
1852 get_short ();
1854 break;
1856 case op_jsr_w:
1857 case op_goto_w:
1858 note_branch_target (compute_jump (get_int ()));
1859 break;
1861 // These are unused here, but we call them out explicitly
1862 // so that -Wswitch-enum doesn't complain.
1863 case op_putfield_1:
1864 case op_putfield_2:
1865 case op_putfield_4:
1866 case op_putfield_8:
1867 case op_putfield_a:
1868 case op_putstatic_1:
1869 case op_putstatic_2:
1870 case op_putstatic_4:
1871 case op_putstatic_8:
1872 case op_putstatic_a:
1873 case op_getfield_1:
1874 case op_getfield_2s:
1875 case op_getfield_2u:
1876 case op_getfield_4:
1877 case op_getfield_8:
1878 case op_getfield_a:
1879 case op_getstatic_1:
1880 case op_getstatic_2s:
1881 case op_getstatic_2u:
1882 case op_getstatic_4:
1883 case op_getstatic_8:
1884 case op_getstatic_a:
1885 default:
1886 verify_fail ("unrecognized instruction in branch_prepass",
1887 start_PC);
1890 // See if any previous branch tried to branch to the middle of
1891 // this instruction.
1892 for (int pc = start_PC + 1; pc < PC; ++pc)
1894 if ((flags[pc] & FLAG_BRANCH_TARGET))
1895 verify_fail ("branch to middle of instruction", pc);
1899 // Verify exception handlers.
1900 for (int i = 0; i < current_method->exc_count; ++i)
1902 if (! (flags[exception[i].handler_pc.i] & FLAG_INSN_START))
1903 verify_fail ("exception handler not at instruction start",
1904 exception[i].handler_pc.i);
1905 if (! (flags[exception[i].start_pc.i] & FLAG_INSN_START))
1906 verify_fail ("exception start not at instruction start",
1907 exception[i].start_pc.i);
1908 if (exception[i].end_pc.i != current_method->code_length
1909 && ! (flags[exception[i].end_pc.i] & FLAG_INSN_START))
1910 verify_fail ("exception end not at instruction start",
1911 exception[i].end_pc.i);
1913 flags[exception[i].handler_pc.i] |= FLAG_BRANCH_TARGET;
1917 void check_pool_index (int index)
1919 if (index < 0 || index >= current_class->constants.size)
1920 verify_fail ("constant pool index out of range", start_PC);
1923 type check_class_constant (int index)
1925 check_pool_index (index);
1926 _Jv_Constants *pool = &current_class->constants;
1927 if (pool->tags[index] == JV_CONSTANT_ResolvedClass)
1928 return type (pool->data[index].clazz, this);
1929 else if (pool->tags[index] == JV_CONSTANT_Class)
1930 return type (pool->data[index].utf8, this);
1931 verify_fail ("expected class constant", start_PC);
1934 type check_constant (int index)
1936 check_pool_index (index);
1937 _Jv_Constants *pool = &current_class->constants;
1938 if (pool->tags[index] == JV_CONSTANT_ResolvedString
1939 || pool->tags[index] == JV_CONSTANT_String)
1940 return type (&java::lang::String::class$, this);
1941 else if (pool->tags[index] == JV_CONSTANT_Integer)
1942 return type (int_type);
1943 else if (pool->tags[index] == JV_CONSTANT_Float)
1944 return type (float_type);
1945 verify_fail ("String, int, or float constant expected", start_PC);
1948 type check_wide_constant (int index)
1950 check_pool_index (index);
1951 _Jv_Constants *pool = &current_class->constants;
1952 if (pool->tags[index] == JV_CONSTANT_Long)
1953 return type (long_type);
1954 else if (pool->tags[index] == JV_CONSTANT_Double)
1955 return type (double_type);
1956 verify_fail ("long or double constant expected", start_PC);
1959 // Helper for both field and method. These are laid out the same in
1960 // the constant pool.
1961 type handle_field_or_method (int index, int expected,
1962 _Jv_Utf8Const **name,
1963 _Jv_Utf8Const **fmtype)
1965 check_pool_index (index);
1966 _Jv_Constants *pool = &current_class->constants;
1967 if (pool->tags[index] != expected)
1968 verify_fail ("didn't see expected constant", start_PC);
1969 // Once we know we have a Fieldref or Methodref we assume that it
1970 // is correctly laid out in the constant pool. I think the code
1971 // in defineclass.cc guarantees this.
1972 _Jv_ushort class_index, name_and_type_index;
1973 _Jv_loadIndexes (&pool->data[index],
1974 class_index,
1975 name_and_type_index);
1976 _Jv_ushort name_index, desc_index;
1977 _Jv_loadIndexes (&pool->data[name_and_type_index],
1978 name_index, desc_index);
1980 *name = pool->data[name_index].utf8;
1981 *fmtype = pool->data[desc_index].utf8;
1983 return check_class_constant (class_index);
1986 // Return field's type, compute class' type if requested.
1987 type check_field_constant (int index, type *class_type = NULL)
1989 _Jv_Utf8Const *name, *field_type;
1990 type ct = handle_field_or_method (index,
1991 JV_CONSTANT_Fieldref,
1992 &name, &field_type);
1993 if (class_type)
1994 *class_type = ct;
1995 if (field_type->data[0] == '[' || field_type->data[0] == 'L')
1996 return type (field_type, this);
1997 return get_type_val_for_signature (field_type->data[0]);
2000 type check_method_constant (int index, bool is_interface,
2001 _Jv_Utf8Const **method_name,
2002 _Jv_Utf8Const **method_signature)
2004 return handle_field_or_method (index,
2005 (is_interface
2006 ? JV_CONSTANT_InterfaceMethodref
2007 : JV_CONSTANT_Methodref),
2008 method_name, method_signature);
2011 type get_one_type (char *&p)
2013 char *start = p;
2015 int arraycount = 0;
2016 while (*p == '[')
2018 ++arraycount;
2019 ++p;
2022 char v = *p++;
2024 if (v == 'L')
2026 while (*p != ';')
2027 ++p;
2028 ++p;
2029 _Jv_Utf8Const *name = make_utf8_const (start, p - start);
2030 return type (name, this);
2033 // Casting to jchar here is ok since we are looking at an ASCII
2034 // character.
2035 type_val rt = get_type_val_for_signature (jchar (v));
2037 if (arraycount == 0)
2039 // Callers of this function eventually push their arguments on
2040 // the stack. So, promote them here.
2041 return type (rt).promote ();
2044 jclass k = construct_primitive_array_type (rt);
2045 while (--arraycount > 0)
2046 k = _Jv_GetArrayClass (k, NULL);
2047 return type (k, this);
2050 void compute_argument_types (_Jv_Utf8Const *signature,
2051 type *types)
2053 char *p = signature->data;
2054 // Skip `('.
2055 ++p;
2057 int i = 0;
2058 while (*p != ')')
2059 types[i++] = get_one_type (p);
2062 type compute_return_type (_Jv_Utf8Const *signature)
2064 char *p = signature->data;
2065 while (*p != ')')
2066 ++p;
2067 ++p;
2068 return get_one_type (p);
2071 void check_return_type (type onstack)
2073 type rt = compute_return_type (current_method->self->signature);
2074 if (! rt.compatible (onstack, this))
2075 verify_fail ("incompatible return type");
2078 // Initialize the stack for the new method. Returns true if this
2079 // method is an instance initializer.
2080 bool initialize_stack ()
2082 int var = 0;
2083 bool is_init = _Jv_equalUtf8Consts (current_method->self->name,
2084 gcj::init_name);
2085 bool is_clinit = _Jv_equalUtf8Consts (current_method->self->name,
2086 gcj::clinit_name);
2088 using namespace java::lang::reflect;
2089 if (! Modifier::isStatic (current_method->self->accflags))
2091 type kurr (current_class, this);
2092 if (is_init)
2094 kurr.set_uninitialized (type::SELF, this);
2095 is_init = true;
2097 else if (is_clinit)
2098 verify_fail ("<clinit> method must be static");
2099 set_variable (0, kurr);
2100 current_state->set_this_type (kurr);
2101 ++var;
2103 else
2105 if (is_init)
2106 verify_fail ("<init> method must be non-static");
2109 // We have to handle wide arguments specially here.
2110 int arg_count = _Jv_count_arguments (current_method->self->signature);
2111 type arg_types[arg_count];
2112 compute_argument_types (current_method->self->signature, arg_types);
2113 for (int i = 0; i < arg_count; ++i)
2115 set_variable (var, arg_types[i]);
2116 ++var;
2117 if (arg_types[i].iswide ())
2118 ++var;
2121 return is_init;
2124 void verify_instructions_0 ()
2126 current_state = new state (current_method->max_stack,
2127 current_method->max_locals);
2129 PC = 0;
2130 start_PC = 0;
2132 // True if we are verifying an instance initializer.
2133 bool this_is_init = initialize_stack ();
2135 states = (linked<state> **) _Jv_Malloc (sizeof (linked<state> *)
2136 * current_method->code_length);
2137 for (int i = 0; i < current_method->code_length; ++i)
2138 states[i] = NULL;
2140 next_verify_state = NULL;
2142 while (true)
2144 // If the PC was invalidated, get a new one from the work list.
2145 if (PC == state::NO_NEXT)
2147 state *new_state = pop_jump ();
2148 // If it is null, we're done.
2149 if (new_state == NULL)
2150 break;
2152 PC = new_state->get_pc ();
2153 debug_print ("== State pop from pending list\n");
2154 // Set up the current state.
2155 current_state->copy (new_state, current_method->max_stack,
2156 current_method->max_locals);
2158 else
2160 // We only have to do this checking in the situation where
2161 // control flow falls through from the previous
2162 // instruction. Otherwise merging is done at the time we
2163 // push the branch.
2164 if (states[PC] != NULL)
2166 // We've already visited this instruction. So merge
2167 // the states together. It is simplest, but not most
2168 // efficient, to just always invalidate the PC here.
2169 merge_into (PC, current_state);
2170 invalidate_pc ();
2171 continue;
2175 // Control can't fall off the end of the bytecode. We need to
2176 // check this in both cases, not just the fall-through case,
2177 // because we don't check to see whether a `jsr' appears at
2178 // the end of the bytecode until we process a `ret'.
2179 if (PC >= current_method->code_length)
2180 verify_fail ("fell off end");
2182 // We only have to keep saved state at branch targets. If
2183 // we're at a branch target and the state here hasn't been set
2184 // yet, we set it now. You might notice that `ret' targets
2185 // won't necessarily have FLAG_BRANCH_TARGET set. This
2186 // doesn't matter, since those states will be filled in by
2187 // merge_into.
2188 if (states[PC] == NULL && (flags[PC] & FLAG_BRANCH_TARGET))
2189 add_new_state (PC, current_state);
2191 // Set this before handling exceptions so that debug output is
2192 // sane.
2193 start_PC = PC;
2195 // Update states for all active exception handlers. Ordinarily
2196 // there are not many exception handlers. So we simply run
2197 // through them all.
2198 for (int i = 0; i < current_method->exc_count; ++i)
2200 if (PC >= exception[i].start_pc.i && PC < exception[i].end_pc.i)
2202 type handler (&java::lang::Throwable::class$, this);
2203 if (exception[i].handler_type.i != 0)
2204 handler = check_class_constant (exception[i].handler_type.i);
2205 push_exception_jump (handler, exception[i].handler_pc.i);
2209 current_state->print (" ", PC, current_method->max_stack,
2210 current_method->max_locals);
2211 java_opcode opcode = (java_opcode) bytecode[PC++];
2212 switch (opcode)
2214 case op_nop:
2215 break;
2217 case op_aconst_null:
2218 push_type (null_type);
2219 break;
2221 case op_iconst_m1:
2222 case op_iconst_0:
2223 case op_iconst_1:
2224 case op_iconst_2:
2225 case op_iconst_3:
2226 case op_iconst_4:
2227 case op_iconst_5:
2228 push_type (int_type);
2229 break;
2231 case op_lconst_0:
2232 case op_lconst_1:
2233 push_type (long_type);
2234 break;
2236 case op_fconst_0:
2237 case op_fconst_1:
2238 case op_fconst_2:
2239 push_type (float_type);
2240 break;
2242 case op_dconst_0:
2243 case op_dconst_1:
2244 push_type (double_type);
2245 break;
2247 case op_bipush:
2248 get_byte ();
2249 push_type (int_type);
2250 break;
2252 case op_sipush:
2253 get_short ();
2254 push_type (int_type);
2255 break;
2257 case op_ldc:
2258 push_type (check_constant (get_byte ()));
2259 break;
2260 case op_ldc_w:
2261 push_type (check_constant (get_ushort ()));
2262 break;
2263 case op_ldc2_w:
2264 push_type (check_wide_constant (get_ushort ()));
2265 break;
2267 case op_iload:
2268 push_type (get_variable (get_byte (), int_type));
2269 break;
2270 case op_lload:
2271 push_type (get_variable (get_byte (), long_type));
2272 break;
2273 case op_fload:
2274 push_type (get_variable (get_byte (), float_type));
2275 break;
2276 case op_dload:
2277 push_type (get_variable (get_byte (), double_type));
2278 break;
2279 case op_aload:
2280 push_type (get_variable (get_byte (), reference_type));
2281 break;
2283 case op_iload_0:
2284 case op_iload_1:
2285 case op_iload_2:
2286 case op_iload_3:
2287 push_type (get_variable (opcode - op_iload_0, int_type));
2288 break;
2289 case op_lload_0:
2290 case op_lload_1:
2291 case op_lload_2:
2292 case op_lload_3:
2293 push_type (get_variable (opcode - op_lload_0, long_type));
2294 break;
2295 case op_fload_0:
2296 case op_fload_1:
2297 case op_fload_2:
2298 case op_fload_3:
2299 push_type (get_variable (opcode - op_fload_0, float_type));
2300 break;
2301 case op_dload_0:
2302 case op_dload_1:
2303 case op_dload_2:
2304 case op_dload_3:
2305 push_type (get_variable (opcode - op_dload_0, double_type));
2306 break;
2307 case op_aload_0:
2308 case op_aload_1:
2309 case op_aload_2:
2310 case op_aload_3:
2311 push_type (get_variable (opcode - op_aload_0, reference_type));
2312 break;
2313 case op_iaload:
2314 pop_type (int_type);
2315 push_type (require_array_type (pop_init_ref (reference_type),
2316 int_type));
2317 break;
2318 case op_laload:
2319 pop_type (int_type);
2320 push_type (require_array_type (pop_init_ref (reference_type),
2321 long_type));
2322 break;
2323 case op_faload:
2324 pop_type (int_type);
2325 push_type (require_array_type (pop_init_ref (reference_type),
2326 float_type));
2327 break;
2328 case op_daload:
2329 pop_type (int_type);
2330 push_type (require_array_type (pop_init_ref (reference_type),
2331 double_type));
2332 break;
2333 case op_aaload:
2334 pop_type (int_type);
2335 push_type (require_array_type (pop_init_ref (reference_type),
2336 reference_type));
2337 break;
2338 case op_baload:
2339 pop_type (int_type);
2340 require_array_type (pop_init_ref (reference_type), byte_type);
2341 push_type (int_type);
2342 break;
2343 case op_caload:
2344 pop_type (int_type);
2345 require_array_type (pop_init_ref (reference_type), char_type);
2346 push_type (int_type);
2347 break;
2348 case op_saload:
2349 pop_type (int_type);
2350 require_array_type (pop_init_ref (reference_type), short_type);
2351 push_type (int_type);
2352 break;
2353 case op_istore:
2354 set_variable (get_byte (), pop_type (int_type));
2355 break;
2356 case op_lstore:
2357 set_variable (get_byte (), pop_type (long_type));
2358 break;
2359 case op_fstore:
2360 set_variable (get_byte (), pop_type (float_type));
2361 break;
2362 case op_dstore:
2363 set_variable (get_byte (), pop_type (double_type));
2364 break;
2365 case op_astore:
2366 set_variable (get_byte (), pop_ref_or_return ());
2367 break;
2368 case op_istore_0:
2369 case op_istore_1:
2370 case op_istore_2:
2371 case op_istore_3:
2372 set_variable (opcode - op_istore_0, pop_type (int_type));
2373 break;
2374 case op_lstore_0:
2375 case op_lstore_1:
2376 case op_lstore_2:
2377 case op_lstore_3:
2378 set_variable (opcode - op_lstore_0, pop_type (long_type));
2379 break;
2380 case op_fstore_0:
2381 case op_fstore_1:
2382 case op_fstore_2:
2383 case op_fstore_3:
2384 set_variable (opcode - op_fstore_0, pop_type (float_type));
2385 break;
2386 case op_dstore_0:
2387 case op_dstore_1:
2388 case op_dstore_2:
2389 case op_dstore_3:
2390 set_variable (opcode - op_dstore_0, pop_type (double_type));
2391 break;
2392 case op_astore_0:
2393 case op_astore_1:
2394 case op_astore_2:
2395 case op_astore_3:
2396 set_variable (opcode - op_astore_0, pop_ref_or_return ());
2397 break;
2398 case op_iastore:
2399 pop_type (int_type);
2400 pop_type (int_type);
2401 require_array_type (pop_init_ref (reference_type), int_type);
2402 break;
2403 case op_lastore:
2404 pop_type (long_type);
2405 pop_type (int_type);
2406 require_array_type (pop_init_ref (reference_type), long_type);
2407 break;
2408 case op_fastore:
2409 pop_type (float_type);
2410 pop_type (int_type);
2411 require_array_type (pop_init_ref (reference_type), float_type);
2412 break;
2413 case op_dastore:
2414 pop_type (double_type);
2415 pop_type (int_type);
2416 require_array_type (pop_init_ref (reference_type), double_type);
2417 break;
2418 case op_aastore:
2419 pop_type (reference_type);
2420 pop_type (int_type);
2421 require_array_type (pop_init_ref (reference_type), reference_type);
2422 break;
2423 case op_bastore:
2424 pop_type (int_type);
2425 pop_type (int_type);
2426 require_array_type (pop_init_ref (reference_type), byte_type);
2427 break;
2428 case op_castore:
2429 pop_type (int_type);
2430 pop_type (int_type);
2431 require_array_type (pop_init_ref (reference_type), char_type);
2432 break;
2433 case op_sastore:
2434 pop_type (int_type);
2435 pop_type (int_type);
2436 require_array_type (pop_init_ref (reference_type), short_type);
2437 break;
2438 case op_pop:
2439 pop32 ();
2440 break;
2441 case op_pop2:
2443 type t = pop_raw ();
2444 if (! t.iswide ())
2445 pop32 ();
2447 break;
2448 case op_dup:
2450 type t = pop32 ();
2451 push_type (t);
2452 push_type (t);
2454 break;
2455 case op_dup_x1:
2457 type t1 = pop32 ();
2458 type t2 = pop32 ();
2459 push_type (t1);
2460 push_type (t2);
2461 push_type (t1);
2463 break;
2464 case op_dup_x2:
2466 type t1 = pop32 ();
2467 type t2 = pop_raw ();
2468 if (! t2.iswide ())
2470 type t3 = pop32 ();
2471 push_type (t1);
2472 push_type (t3);
2474 else
2475 push_type (t1);
2476 push_type (t2);
2477 push_type (t1);
2479 break;
2480 case op_dup2:
2482 type t = pop_raw ();
2483 if (! t.iswide ())
2485 type t2 = pop32 ();
2486 push_type (t2);
2487 push_type (t);
2488 push_type (t2);
2490 else
2491 push_type (t);
2492 push_type (t);
2494 break;
2495 case op_dup2_x1:
2497 type t1 = pop_raw ();
2498 type t2 = pop32 ();
2499 if (! t1.iswide ())
2501 type t3 = pop32 ();
2502 push_type (t2);
2503 push_type (t1);
2504 push_type (t3);
2506 else
2507 push_type (t1);
2508 push_type (t2);
2509 push_type (t1);
2511 break;
2512 case op_dup2_x2:
2514 type t1 = pop_raw ();
2515 if (t1.iswide ())
2517 type t2 = pop_raw ();
2518 if (t2.iswide ())
2520 push_type (t1);
2521 push_type (t2);
2523 else
2525 type t3 = pop32 ();
2526 push_type (t1);
2527 push_type (t3);
2528 push_type (t2);
2530 push_type (t1);
2532 else
2534 type t2 = pop32 ();
2535 type t3 = pop_raw ();
2536 if (t3.iswide ())
2538 push_type (t2);
2539 push_type (t1);
2541 else
2543 type t4 = pop32 ();
2544 push_type (t2);
2545 push_type (t1);
2546 push_type (t4);
2548 push_type (t3);
2549 push_type (t2);
2550 push_type (t1);
2553 break;
2554 case op_swap:
2556 type t1 = pop32 ();
2557 type t2 = pop32 ();
2558 push_type (t1);
2559 push_type (t2);
2561 break;
2562 case op_iadd:
2563 case op_isub:
2564 case op_imul:
2565 case op_idiv:
2566 case op_irem:
2567 case op_ishl:
2568 case op_ishr:
2569 case op_iushr:
2570 case op_iand:
2571 case op_ior:
2572 case op_ixor:
2573 pop_type (int_type);
2574 push_type (pop_type (int_type));
2575 break;
2576 case op_ladd:
2577 case op_lsub:
2578 case op_lmul:
2579 case op_ldiv:
2580 case op_lrem:
2581 case op_land:
2582 case op_lor:
2583 case op_lxor:
2584 pop_type (long_type);
2585 push_type (pop_type (long_type));
2586 break;
2587 case op_lshl:
2588 case op_lshr:
2589 case op_lushr:
2590 pop_type (int_type);
2591 push_type (pop_type (long_type));
2592 break;
2593 case op_fadd:
2594 case op_fsub:
2595 case op_fmul:
2596 case op_fdiv:
2597 case op_frem:
2598 pop_type (float_type);
2599 push_type (pop_type (float_type));
2600 break;
2601 case op_dadd:
2602 case op_dsub:
2603 case op_dmul:
2604 case op_ddiv:
2605 case op_drem:
2606 pop_type (double_type);
2607 push_type (pop_type (double_type));
2608 break;
2609 case op_ineg:
2610 case op_i2b:
2611 case op_i2c:
2612 case op_i2s:
2613 push_type (pop_type (int_type));
2614 break;
2615 case op_lneg:
2616 push_type (pop_type (long_type));
2617 break;
2618 case op_fneg:
2619 push_type (pop_type (float_type));
2620 break;
2621 case op_dneg:
2622 push_type (pop_type (double_type));
2623 break;
2624 case op_iinc:
2625 get_variable (get_byte (), int_type);
2626 get_byte ();
2627 break;
2628 case op_i2l:
2629 pop_type (int_type);
2630 push_type (long_type);
2631 break;
2632 case op_i2f:
2633 pop_type (int_type);
2634 push_type (float_type);
2635 break;
2636 case op_i2d:
2637 pop_type (int_type);
2638 push_type (double_type);
2639 break;
2640 case op_l2i:
2641 pop_type (long_type);
2642 push_type (int_type);
2643 break;
2644 case op_l2f:
2645 pop_type (long_type);
2646 push_type (float_type);
2647 break;
2648 case op_l2d:
2649 pop_type (long_type);
2650 push_type (double_type);
2651 break;
2652 case op_f2i:
2653 pop_type (float_type);
2654 push_type (int_type);
2655 break;
2656 case op_f2l:
2657 pop_type (float_type);
2658 push_type (long_type);
2659 break;
2660 case op_f2d:
2661 pop_type (float_type);
2662 push_type (double_type);
2663 break;
2664 case op_d2i:
2665 pop_type (double_type);
2666 push_type (int_type);
2667 break;
2668 case op_d2l:
2669 pop_type (double_type);
2670 push_type (long_type);
2671 break;
2672 case op_d2f:
2673 pop_type (double_type);
2674 push_type (float_type);
2675 break;
2676 case op_lcmp:
2677 pop_type (long_type);
2678 pop_type (long_type);
2679 push_type (int_type);
2680 break;
2681 case op_fcmpl:
2682 case op_fcmpg:
2683 pop_type (float_type);
2684 pop_type (float_type);
2685 push_type (int_type);
2686 break;
2687 case op_dcmpl:
2688 case op_dcmpg:
2689 pop_type (double_type);
2690 pop_type (double_type);
2691 push_type (int_type);
2692 break;
2693 case op_ifeq:
2694 case op_ifne:
2695 case op_iflt:
2696 case op_ifge:
2697 case op_ifgt:
2698 case op_ifle:
2699 pop_type (int_type);
2700 push_jump (get_short ());
2701 break;
2702 case op_if_icmpeq:
2703 case op_if_icmpne:
2704 case op_if_icmplt:
2705 case op_if_icmpge:
2706 case op_if_icmpgt:
2707 case op_if_icmple:
2708 pop_type (int_type);
2709 pop_type (int_type);
2710 push_jump (get_short ());
2711 break;
2712 case op_if_acmpeq:
2713 case op_if_acmpne:
2714 pop_type (reference_type);
2715 pop_type (reference_type);
2716 push_jump (get_short ());
2717 break;
2718 case op_goto:
2719 push_jump (get_short ());
2720 invalidate_pc ();
2721 break;
2722 case op_jsr:
2723 handle_jsr_insn (get_short ());
2724 break;
2725 case op_ret:
2726 handle_ret_insn (get_byte ());
2727 break;
2728 case op_tableswitch:
2730 pop_type (int_type);
2731 skip_padding ();
2732 push_jump (get_int ());
2733 jint low = get_int ();
2734 jint high = get_int ();
2735 // Already checked LOW -vs- HIGH.
2736 for (int i = low; i <= high; ++i)
2737 push_jump (get_int ());
2738 invalidate_pc ();
2740 break;
2742 case op_lookupswitch:
2744 pop_type (int_type);
2745 skip_padding ();
2746 push_jump (get_int ());
2747 jint npairs = get_int ();
2748 // Already checked NPAIRS >= 0.
2749 jint lastkey = 0;
2750 for (int i = 0; i < npairs; ++i)
2752 jint key = get_int ();
2753 if (i > 0 && key <= lastkey)
2754 verify_fail ("lookupswitch pairs unsorted", start_PC);
2755 lastkey = key;
2756 push_jump (get_int ());
2758 invalidate_pc ();
2760 break;
2761 case op_ireturn:
2762 check_return_type (pop_type (int_type));
2763 invalidate_pc ();
2764 break;
2765 case op_lreturn:
2766 check_return_type (pop_type (long_type));
2767 invalidate_pc ();
2768 break;
2769 case op_freturn:
2770 check_return_type (pop_type (float_type));
2771 invalidate_pc ();
2772 break;
2773 case op_dreturn:
2774 check_return_type (pop_type (double_type));
2775 invalidate_pc ();
2776 break;
2777 case op_areturn:
2778 check_return_type (pop_init_ref (reference_type));
2779 invalidate_pc ();
2780 break;
2781 case op_return:
2782 // We only need to check this when the return type is
2783 // void, because all instance initializers return void.
2784 if (this_is_init)
2785 current_state->check_this_initialized (this);
2786 check_return_type (void_type);
2787 invalidate_pc ();
2788 break;
2789 case op_getstatic:
2790 push_type (check_field_constant (get_ushort ()));
2791 break;
2792 case op_putstatic:
2793 pop_type (check_field_constant (get_ushort ()));
2794 break;
2795 case op_getfield:
2797 type klass;
2798 type field = check_field_constant (get_ushort (), &klass);
2799 pop_type (klass);
2800 push_type (field);
2802 break;
2803 case op_putfield:
2805 type klass;
2806 type field = check_field_constant (get_ushort (), &klass);
2807 pop_type (field);
2809 // We have an obscure special case here: we can use
2810 // `putfield' on a field declared in this class, even if
2811 // `this' has not yet been initialized.
2812 if (! current_state->this_type.isinitialized ()
2813 && current_state->this_type.pc == type::SELF)
2814 klass.set_uninitialized (type::SELF, this);
2815 pop_type (klass);
2817 break;
2819 case op_invokevirtual:
2820 case op_invokespecial:
2821 case op_invokestatic:
2822 case op_invokeinterface:
2824 _Jv_Utf8Const *method_name, *method_signature;
2825 type class_type
2826 = check_method_constant (get_ushort (),
2827 opcode == op_invokeinterface,
2828 &method_name,
2829 &method_signature);
2830 // NARGS is only used when we're processing
2831 // invokeinterface. It is simplest for us to compute it
2832 // here and then verify it later.
2833 int nargs = 0;
2834 if (opcode == op_invokeinterface)
2836 nargs = get_byte ();
2837 if (get_byte () != 0)
2838 verify_fail ("invokeinterface dummy byte is wrong");
2841 bool is_init = false;
2842 if (_Jv_equalUtf8Consts (method_name, gcj::init_name))
2844 is_init = true;
2845 if (opcode != op_invokespecial)
2846 verify_fail ("can't invoke <init>");
2848 else if (method_name->data[0] == '<')
2849 verify_fail ("can't invoke method starting with `<'");
2851 // Pop arguments and check types.
2852 int arg_count = _Jv_count_arguments (method_signature);
2853 type arg_types[arg_count];
2854 compute_argument_types (method_signature, arg_types);
2855 for (int i = arg_count - 1; i >= 0; --i)
2857 // This is only used for verifying the byte for
2858 // invokeinterface.
2859 nargs -= arg_types[i].depth ();
2860 pop_init_ref (arg_types[i]);
2863 if (opcode == op_invokeinterface
2864 && nargs != 1)
2865 verify_fail ("wrong argument count for invokeinterface");
2867 if (opcode != op_invokestatic)
2869 type t = class_type;
2870 if (is_init)
2872 // In this case the PC doesn't matter.
2873 t.set_uninitialized (type::UNINIT, this);
2874 // FIXME: check to make sure that the <init>
2875 // call is to the right class.
2876 // It must either be super or an exact class
2877 // match.
2879 type raw = pop_raw ();
2880 if (! t.compatible (raw, this))
2881 verify_fail ("incompatible type on stack");
2883 if (is_init)
2884 current_state->set_initialized (raw.get_pc (),
2885 current_method->max_locals);
2888 type rt = compute_return_type (method_signature);
2889 if (! rt.isvoid ())
2890 push_type (rt);
2892 break;
2894 case op_new:
2896 type t = check_class_constant (get_ushort ());
2897 if (t.isarray () || t.isinterface (this) || t.isabstract (this))
2898 verify_fail ("type is array, interface, or abstract");
2899 t.set_uninitialized (start_PC, this);
2900 push_type (t);
2902 break;
2904 case op_newarray:
2906 int atype = get_byte ();
2907 // We intentionally have chosen constants to make this
2908 // valid.
2909 if (atype < boolean_type || atype > long_type)
2910 verify_fail ("type not primitive", start_PC);
2911 pop_type (int_type);
2912 type t (construct_primitive_array_type (type_val (atype)), this);
2913 push_type (t);
2915 break;
2916 case op_anewarray:
2917 pop_type (int_type);
2918 push_type (check_class_constant (get_ushort ()).to_array (this));
2919 break;
2920 case op_arraylength:
2922 type t = pop_init_ref (reference_type);
2923 if (! t.isarray () && ! t.isnull ())
2924 verify_fail ("array type expected");
2925 push_type (int_type);
2927 break;
2928 case op_athrow:
2929 pop_type (type (&java::lang::Throwable::class$, this));
2930 invalidate_pc ();
2931 break;
2932 case op_checkcast:
2933 pop_init_ref (reference_type);
2934 push_type (check_class_constant (get_ushort ()));
2935 break;
2936 case op_instanceof:
2937 pop_init_ref (reference_type);
2938 check_class_constant (get_ushort ());
2939 push_type (int_type);
2940 break;
2941 case op_monitorenter:
2942 pop_init_ref (reference_type);
2943 break;
2944 case op_monitorexit:
2945 pop_init_ref (reference_type);
2946 break;
2947 case op_wide:
2949 switch (get_byte ())
2951 case op_iload:
2952 push_type (get_variable (get_ushort (), int_type));
2953 break;
2954 case op_lload:
2955 push_type (get_variable (get_ushort (), long_type));
2956 break;
2957 case op_fload:
2958 push_type (get_variable (get_ushort (), float_type));
2959 break;
2960 case op_dload:
2961 push_type (get_variable (get_ushort (), double_type));
2962 break;
2963 case op_aload:
2964 push_type (get_variable (get_ushort (), reference_type));
2965 break;
2966 case op_istore:
2967 set_variable (get_ushort (), pop_type (int_type));
2968 break;
2969 case op_lstore:
2970 set_variable (get_ushort (), pop_type (long_type));
2971 break;
2972 case op_fstore:
2973 set_variable (get_ushort (), pop_type (float_type));
2974 break;
2975 case op_dstore:
2976 set_variable (get_ushort (), pop_type (double_type));
2977 break;
2978 case op_astore:
2979 set_variable (get_ushort (), pop_init_ref (reference_type));
2980 break;
2981 case op_ret:
2982 handle_ret_insn (get_short ());
2983 break;
2984 case op_iinc:
2985 get_variable (get_ushort (), int_type);
2986 get_short ();
2987 break;
2988 default:
2989 verify_fail ("unrecognized wide instruction", start_PC);
2992 break;
2993 case op_multianewarray:
2995 type atype = check_class_constant (get_ushort ());
2996 int dim = get_byte ();
2997 if (dim < 1)
2998 verify_fail ("too few dimensions to multianewarray", start_PC);
2999 atype.verify_dimensions (dim, this);
3000 for (int i = 0; i < dim; ++i)
3001 pop_type (int_type);
3002 push_type (atype);
3004 break;
3005 case op_ifnull:
3006 case op_ifnonnull:
3007 pop_type (reference_type);
3008 push_jump (get_short ());
3009 break;
3010 case op_goto_w:
3011 push_jump (get_int ());
3012 invalidate_pc ();
3013 break;
3014 case op_jsr_w:
3015 handle_jsr_insn (get_int ());
3016 break;
3018 // These are unused here, but we call them out explicitly
3019 // so that -Wswitch-enum doesn't complain.
3020 case op_putfield_1:
3021 case op_putfield_2:
3022 case op_putfield_4:
3023 case op_putfield_8:
3024 case op_putfield_a:
3025 case op_putstatic_1:
3026 case op_putstatic_2:
3027 case op_putstatic_4:
3028 case op_putstatic_8:
3029 case op_putstatic_a:
3030 case op_getfield_1:
3031 case op_getfield_2s:
3032 case op_getfield_2u:
3033 case op_getfield_4:
3034 case op_getfield_8:
3035 case op_getfield_a:
3036 case op_getstatic_1:
3037 case op_getstatic_2s:
3038 case op_getstatic_2u:
3039 case op_getstatic_4:
3040 case op_getstatic_8:
3041 case op_getstatic_a:
3042 default:
3043 // Unrecognized opcode.
3044 verify_fail ("unrecognized instruction in verify_instructions_0",
3045 start_PC);
3050 public:
3052 void verify_instructions ()
3054 branch_prepass ();
3055 verify_instructions_0 ();
3058 _Jv_BytecodeVerifier (_Jv_InterpMethod *m)
3060 // We just print the text as utf-8. This is just for debugging
3061 // anyway.
3062 debug_print ("--------------------------------\n");
3063 debug_print ("-- Verifying method `%s'\n", m->self->name->data);
3065 current_method = m;
3066 bytecode = m->bytecode ();
3067 exception = m->exceptions ();
3068 current_class = m->defining_class;
3070 states = NULL;
3071 flags = NULL;
3072 utf8_list = NULL;
3073 isect_list = NULL;
3076 ~_Jv_BytecodeVerifier ()
3078 if (flags)
3079 _Jv_Free (flags);
3081 while (utf8_list != NULL)
3083 linked<_Jv_Utf8Const> *n = utf8_list->next;
3084 _Jv_Free (utf8_list->val);
3085 _Jv_Free (utf8_list);
3086 utf8_list = n;
3089 while (isect_list != NULL)
3091 ref_intersection *next = isect_list->alloc_next;
3092 delete isect_list;
3093 isect_list = next;
3096 if (states)
3098 for (int i = 0; i < current_method->code_length; ++i)
3100 linked<state> *iter = states[i];
3101 while (iter != NULL)
3103 linked<state> *next = iter->next;
3104 delete iter->val;
3105 _Jv_Free (iter);
3106 iter = next;
3109 _Jv_Free (states);
3114 void
3115 _Jv_VerifyMethod (_Jv_InterpMethod *meth)
3117 _Jv_BytecodeVerifier v (meth);
3118 v.verify_instructions ();
3121 #endif /* INTERPRETER */